]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/sfdisk.c
sfdisk: check fdisk_script_set_header() return code [coverity scan]
[thirdparty/util-linux.git] / disk-utils / sfdisk.c
1 /*
2 * Copyright (C) 1995 Andries E. Brouwer (aeb@cwi.nl)
3 * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
4 *
5 * This program is free software. You can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation: either Version 1
8 * or (at your option) any later version.
9 *
10 * A.V. Le Blanc (LeBlanc@mcc.ac.uk) wrote Linux fdisk 1992-1994,
11 * patched by various people (faith@cs.unc.edu, martin@cs.unc.edu,
12 * leisner@sdsp.mc.xerox.com, esr@snark.thyrsus.com, aeb@cwi.nl)
13 * 1993-1995, with version numbers (as far as I have seen) 0.93 - 2.0e.
14 * This program had (head,sector,cylinder) as basic unit, and was
15 * (therefore) broken in several ways for the use on larger disks -
16 * for example, my last patch (from 2.0d to 2.0e) was required
17 * to allow a partition to cross cylinder 8064, and to write an
18 * extended partition past the 4GB mark.
19 *
20 * Karel Zak wrote new sfdisk based on libfdisk from util-linux
21 * in 2014.
22 */
23
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <getopt.h>
31 #include <sys/stat.h>
32 #include <assert.h>
33 #include <fcntl.h>
34 #include <libsmartcols.h>
35 #ifdef HAVE_LIBREADLINE
36 # define _FUNCTION_DEF
37 # include <readline/readline.h>
38 #endif
39 #include <libgen.h>
40 #include <sys/time.h>
41
42 #include "c.h"
43 #include "xalloc.h"
44 #include "nls.h"
45 #include "debug.h"
46 #include "strutils.h"
47 #include "closestream.h"
48 #include "colors.h"
49 #include "blkdev.h"
50 #include "all-io.h"
51 #include "rpmatch.h"
52 #include "optutils.h"
53 #include "ttyutils.h"
54
55 #include "libfdisk.h"
56 #include "fdisk-list.h"
57
58 /*
59 * sfdisk debug stuff (see fdisk.h and include/debug.h)
60 */
61 static UL_DEBUG_DEFINE_MASK(sfdisk);
62 UL_DEBUG_DEFINE_MASKNAMES(sfdisk) = UL_DEBUG_EMPTY_MASKNAMES;
63
64 #define SFDISKPROG_DEBUG_INIT (1 << 1)
65 #define SFDISKPROG_DEBUG_PARSE (1 << 2)
66 #define SFDISKPROG_DEBUG_MISC (1 << 3)
67 #define SFDISKPROG_DEBUG_ASK (1 << 4)
68 #define SFDISKPROG_DEBUG_ALL 0xFFFF
69
70 #define DBG(m, x) __UL_DBG(sfdisk, SFDISKPROG_DEBUG_, m, x)
71 #define ON_DBG(m, x) __UL_DBG_CALL(sfdisk, SFDISKPROG_DEBUG_, m, x)
72
73 enum {
74 ACT_FDISK = 1,
75 ACT_ACTIVATE,
76 ACT_CHANGE_ID,
77 ACT_DUMP,
78 ACT_LIST,
79 ACT_LIST_FREE,
80 ACT_LIST_TYPES,
81 ACT_REORDER,
82 ACT_SHOW_SIZE,
83 ACT_SHOW_GEOM,
84 ACT_VERIFY,
85 ACT_PARTTYPE,
86 ACT_PARTUUID,
87 ACT_PARTLABEL,
88 ACT_PARTATTRS,
89 ACT_DELETE
90 };
91
92 struct sfdisk {
93 int act; /* ACT_* */
94 int partno; /* -N <partno>, default -1 */
95 int wipemode; /* remove foreign signatures from disk */
96 int pwipemode; /* remove foreign signatures from partitions */
97 const char *label; /* --label <label> */
98 const char *label_nested; /* --label-nested <label> */
99 const char *backup_file; /* -O <path> */
100 const char *move_typescript; /* --movedata <typescript> */
101 char *prompt;
102
103 struct fdisk_context *cxt; /* libfdisk context */
104 struct fdisk_partition *orig_pa; /* -N <partno> before the change */
105
106 unsigned int verify : 1, /* call fdisk_verify_disklabel() */
107 quiet : 1, /* suppress extra messages */
108 interactive : 1, /* running on tty */
109 noreread : 1, /* don't check device is in use */
110 force : 1, /* do also stupid things */
111 backup : 1, /* backup sectors before write PT */
112 container : 1, /* PT contains container (MBR extended) partitions */
113 append : 1, /* don't create new PT, append partitions only */
114 json : 1, /* JSON dump */
115 movedata: 1, /* move data after resize */
116 movefsync: 1, /* use fsync() after each write() */
117 notell : 1, /* don't tell kernel aout new PT */
118 noact : 1; /* do not write to device */
119 };
120
121 #define SFDISK_PROMPT ">>> "
122
123 static void sfdiskprog_init_debug(void)
124 {
125 __UL_INIT_DEBUG_FROM_ENV(sfdisk, SFDISKPROG_DEBUG_, 0, SFDISK_DEBUG);
126 }
127
128
129 static int get_user_reply(const char *prompt, char *buf, size_t bufsz)
130 {
131 char *p;
132 size_t sz;
133
134 #ifdef HAVE_LIBREADLINE
135 if (isatty(STDIN_FILENO)) {
136 p = readline(prompt);
137 if (!p)
138 return 1;
139 xstrncpy(buf, p, bufsz);
140 free(p);
141 } else
142 #endif
143 {
144 fputs(prompt, stdout);
145 fflush(stdout);
146
147 if (!fgets(buf, bufsz, stdin))
148 return 1;
149 }
150
151 for (p = buf; *p && !isgraph(*p); p++); /* get first non-blank */
152
153 if (p > buf)
154 memmove(buf, p, p - buf); /* remove blank space */
155 sz = strlen(buf);
156 if (sz && *(buf + sz - 1) == '\n')
157 *(buf + sz - 1) = '\0';
158
159 DBG(ASK, ul_debug("user's reply: >>>%s<<<", buf));
160 return 0;
161 }
162
163 static int ask_callback(struct fdisk_context *cxt __attribute__((__unused__)),
164 struct fdisk_ask *ask,
165 void *data)
166 {
167 struct sfdisk *sf = (struct sfdisk *) data;
168 int rc = 0;
169
170 assert(ask);
171
172 switch(fdisk_ask_get_type(ask)) {
173 case FDISK_ASKTYPE_INFO:
174 if (sf->quiet)
175 break;
176 fputs(fdisk_ask_print_get_mesg(ask), stdout);
177 fputc('\n', stdout);
178 break;
179 case FDISK_ASKTYPE_WARNX:
180 fflush(stdout);
181 color_scheme_fenable("warn", UL_COLOR_RED, stderr);
182 fputs(fdisk_ask_print_get_mesg(ask), stderr);
183 color_fdisable(stderr);
184 fputc('\n', stderr);
185 break;
186 case FDISK_ASKTYPE_WARN:
187 fflush(stdout);
188 color_scheme_fenable("warn", UL_COLOR_RED, stderr);
189 fputs(fdisk_ask_print_get_mesg(ask), stderr);
190 errno = fdisk_ask_print_get_errno(ask);
191 fprintf(stderr, ": %m\n");
192 color_fdisable(stderr);
193 break;
194 case FDISK_ASKTYPE_YESNO:
195 {
196 char buf[BUFSIZ] = { '\0' };
197 fputc('\n', stdout);
198 do {
199 int x;
200 fputs(fdisk_ask_get_query(ask), stdout);
201 rc = get_user_reply(_(" [Y]es/[N]o: "), buf, sizeof(buf));
202 if (rc)
203 break;
204 x = rpmatch(buf);
205 if (x == RPMATCH_YES || x == RPMATCH_NO) {
206 fdisk_ask_yesno_set_result(ask, x);
207 break;
208 }
209 } while(1);
210 DBG(ASK, ul_debug("yes-no ask: reply '%s' [rc=%d]", buf, rc));
211 break;
212 }
213 default:
214 break;
215 }
216 return rc;
217 }
218
219 static void sfdisk_init(struct sfdisk *sf)
220 {
221 fdisk_init_debug(0);
222 scols_init_debug(0);
223 sfdiskprog_init_debug();
224
225 sf->cxt = fdisk_new_context();
226 if (!sf->cxt)
227 err(EXIT_FAILURE, _("failed to allocate libfdisk context"));
228 fdisk_set_ask(sf->cxt, ask_callback, (void *) sf);
229 fdisk_enable_bootbits_protection(sf->cxt, 1);
230
231 if (sf->label_nested) {
232 struct fdisk_context *x = fdisk_new_nested_context(sf->cxt,
233 sf->label_nested);
234 if (!x)
235 err(EXIT_FAILURE, _("failed to allocate nested libfdisk context"));
236 /* the original context is available by fdisk_get_parent() */
237 sf->cxt = x;
238 }
239 }
240
241 static int sfdisk_deinit(struct sfdisk *sf)
242 {
243 struct fdisk_context *parent;
244
245 assert(sf);
246 assert(sf->cxt);
247
248 parent = fdisk_get_parent(sf->cxt);
249 if (parent) {
250 fdisk_unref_context(sf->cxt);
251 sf->cxt = parent;
252 }
253
254 fdisk_unref_context(sf->cxt);
255 free(sf->prompt);
256
257 memset(sf, 0, sizeof(*sf));
258 return 0;
259 }
260
261 static struct fdisk_partition *get_partition(struct fdisk_context *cxt, size_t partno)
262 {
263 struct fdisk_table *tb = NULL;
264 struct fdisk_partition *pa;
265
266 if (fdisk_get_partitions(cxt, &tb) != 0)
267 return NULL;
268
269 pa = fdisk_table_get_partition_by_partno(tb, partno);
270 if (pa)
271 fdisk_ref_partition(pa);
272 fdisk_unref_table(tb);
273 return pa;
274 }
275
276 static void backup_sectors(struct sfdisk *sf,
277 const char *tpl,
278 const char *name,
279 const char *devname,
280 uint64_t offset, size_t size)
281 {
282 char *fname;
283 int fd, devfd;
284
285 devfd = fdisk_get_devfd(sf->cxt);
286 assert(devfd >= 0);
287
288 xasprintf(&fname, "%s0x%08"PRIx64".bak", tpl, offset);
289
290 fd = open(fname, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
291 if (fd < 0)
292 goto fail;
293
294 if (lseek(devfd, (off_t) offset, SEEK_SET) == (off_t) -1) {
295 fdisk_warn(sf->cxt, _("cannot seek %s"), devname);
296 goto fail;
297 } else {
298 unsigned char *buf = xmalloc(size);
299
300 if (read_all(devfd, (char *) buf, size) != (ssize_t) size) {
301 fdisk_warn(sf->cxt, _("cannot read %s"), devname);
302 free(buf);
303 goto fail;
304 }
305 if (write_all(fd, buf, size) != 0) {
306 fdisk_warn(sf->cxt, _("cannot write %s"), fname);
307 free(buf);
308 goto fail;
309 }
310 free(buf);
311 }
312
313 fdisk_info(sf->cxt, _("%12s (offset %5ju, size %5ju): %s"),
314 name, (uintmax_t) offset, (uintmax_t) size, fname);
315 close(fd);
316 free(fname);
317 return;
318 fail:
319 errx(EXIT_FAILURE, _("%s: failed to create a backup"), devname);
320 }
321
322 static char *mk_backup_filename_tpl(const char *filename, const char *devname, const char *suffix)
323 {
324 char *tpl = NULL;
325 char *name, *buf = xstrdup(devname);
326
327 name = basename(buf);
328
329 if (!filename || strcmp(filename, "@default") == 0) {
330 const char *home = getenv ("HOME");
331 if (!home)
332 errx(EXIT_FAILURE, _("failed to create a backup file, $HOME undefined"));
333 xasprintf(&tpl, "%s/sfdisk-%s%s", home, name, suffix);
334 } else
335 xasprintf(&tpl, "%s-%s%s", filename, name, suffix);
336
337 free(buf);
338 return tpl;
339 }
340
341
342 static void backup_partition_table(struct sfdisk *sf, const char *devname)
343 {
344 const char *name;
345 char *tpl;
346 uint64_t offset = 0;
347 size_t size = 0;
348 int i = 0;
349
350 assert(sf);
351
352 if (!fdisk_has_label(sf->cxt))
353 return;
354
355 tpl = mk_backup_filename_tpl(sf->backup_file, devname, "-");
356
357 color_scheme_enable("header", UL_COLOR_BOLD);
358 fdisk_info(sf->cxt, _("Backup files:"));
359 color_disable();
360
361 while (fdisk_locate_disklabel(sf->cxt, i++, &name, &offset, &size) == 0 && size)
362 backup_sectors(sf, tpl, name, devname, offset, size);
363
364 if (!sf->quiet)
365 fputc('\n', stdout);
366 free(tpl);
367 }
368
369 static int move_partition_data(struct sfdisk *sf, size_t partno, struct fdisk_partition *orig_pa)
370 {
371 struct fdisk_partition *pa = get_partition(sf->cxt, partno);
372 char *devname = NULL, *typescript = NULL, *buf = NULL;
373 FILE *f = NULL;
374 int ok = 0, fd, backward = 0;
375 fdisk_sector_t nsectors, from, to, step, i, prev;
376 size_t io, ss, step_bytes, cc;
377 uintmax_t src, dst;
378 int errsv, progress = 0;
379 struct timeval prev_time;
380 uint64_t bytes_per_sec = 0;
381
382 assert(sf->movedata);
383
384 if (!pa)
385 warnx(_("failed to read new partition from device; ignoring --move-data"));
386 else if (!fdisk_partition_has_size(pa))
387 warnx(_("failed to get size of the new partition; ignoring --move-data"));
388 else if (!fdisk_partition_has_start(pa))
389 warnx(_("failed to get start of the new partition; ignoring --move-data"));
390 else if (!fdisk_partition_has_size(orig_pa))
391 warnx(_("failed to get size of the old partition; ignoring --move-data"));
392 else if (!fdisk_partition_has_start(orig_pa))
393 warnx(_("failed to get start of the old partition; ignoring --move-data"));
394 else if (fdisk_partition_get_start(pa) == fdisk_partition_get_start(orig_pa))
395 warnx(_("start of the partition has not been moved; ignoring --move-data"));
396 else if (fdisk_partition_get_size(orig_pa) < fdisk_partition_get_size(pa))
397 warnx(_("new partition is smaller than original; ignoring --move-data"));
398 else
399 ok = 1;
400 if (!ok)
401 return -EINVAL;
402
403 DBG(MISC, ul_debug("moving data"));
404
405 fd = fdisk_get_devfd(sf->cxt);
406
407 /* set move direction and overlay */
408 nsectors = fdisk_partition_get_size(orig_pa);
409 from = fdisk_partition_get_start(orig_pa);
410 to = fdisk_partition_get_start(pa);
411
412 if ((to >= from && from + nsectors >= to) ||
413 (from >= to && to + nsectors >= from)) {
414 /* source and target overlay, check if we need to copy
415 * backwardly from end of the source */
416 DBG(MISC, ul_debug("overlay between source and target"));
417 backward = from < to;
418 DBG(MISC, ul_debug(" copy order: %s", backward ? "backward" : "forward"));
419 }
420
421 /* set optimal step size -- nearest to 1MiB aligned to optimal I/O */
422 io = fdisk_get_optimal_iosize(sf->cxt);
423 ss = fdisk_get_sector_size(sf->cxt);
424 if (!io)
425 io = ss;
426 if (io < 1024 * 1024)
427 step_bytes = ((1024 * 1024) + io/2) / io * io;
428 else
429 step_bytes = io;
430
431 step = step_bytes / ss;
432
433 /* align the step (note that nsectors does not have to be power of 2) */
434 while (nsectors % step)
435 step--;
436
437 DBG(MISC, ul_debug(" step: %ju (%zu bytes)", (uintmax_t)step, step_bytes));
438
439 #if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
440 if (!backward)
441 posix_fadvise(fd, from * ss, nsectors * ss, POSIX_FADV_SEQUENTIAL);
442 #endif
443 devname = fdisk_partname(fdisk_get_devname(sf->cxt), partno+1);
444 if (sf->move_typescript)
445 typescript = mk_backup_filename_tpl(sf->move_typescript, devname, ".move");
446
447 if (!sf->quiet) {
448 fdisk_info(sf->cxt,"");
449 color_scheme_enable("header", UL_COLOR_BOLD);
450 fdisk_info(sf->cxt, sf->noact ? _("Data move: (--no-act)") : _("Data move:"));
451 color_disable();
452 if (typescript)
453 fdisk_info(sf->cxt, _(" typescript file: %s"), typescript);
454 printf(_(" start sector: (from/to) %ju / %ju\n"), (uintmax_t) from, (uintmax_t) to);
455 printf(_(" sectors: %ju\n"), (uintmax_t) nsectors);
456 printf(_(" step size: %zu bytes\n"), step_bytes);
457 putchar('\n');
458 fflush(stdout);
459
460 if (isatty(fileno(stdout)))
461 progress = 1;
462 }
463
464 if (sf->interactive) {
465 int yes = 0;
466 fdisk_ask_yesno(sf->cxt, _("Do you want to move partition data?"), &yes);
467 if (!yes) {
468 fdisk_info(sf->cxt, _("Leaving."));
469 return 0;
470 }
471 }
472
473 if (typescript) {
474 f = fopen(typescript, "w");
475 if (!f) {
476 fdisk_warn(sf->cxt, _("cannot open %s"), typescript);
477 goto fail;
478 }
479
480 /* don't translate */
481 fprintf(f, "# sfdisk: " PACKAGE_STRING "\n");
482 fprintf(f, "# Disk: %s\n", devname);
483 fprintf(f, "# Partition: %zu\n", partno + 1);
484 fprintf(f, "# Operation: move data\n");
485 fprintf(f, "# Original start offset (sectors/bytes): %ju/%ju\n",
486 (uintmax_t)from, (uintmax_t)from * ss);
487 fprintf(f, "# New start offset (sectors/bytes): %ju/%ju\n",
488 (uintmax_t)to, (uintmax_t)to * ss);
489 fprintf(f, "# Area size (sectors/bytes): %ju/%ju\n",
490 (uintmax_t)nsectors, (uintmax_t)nsectors * ss);
491 fprintf(f, "# Sector size: %zu\n", ss);
492 fprintf(f, "# Step size (in bytes): %zu\n", step_bytes);
493 fprintf(f, "# Steps: %ju\n", (uintmax_t)(nsectors / step));
494 fprintf(f, "#\n");
495 fprintf(f, "# <step>: <from> <to> (step offsets in bytes)\n");
496 }
497
498 src = (backward ? from + nsectors : from) * ss;
499 dst = (backward ? to + nsectors : to) * ss;
500 buf = xmalloc(step_bytes);
501
502 DBG(MISC, ul_debug(" initial: src=%ju dst=%ju", src, dst));
503
504 gettimeofday(&prev_time, NULL);
505 prev = 0;
506
507 for (cc = 1, i = 0; i < nsectors; i += step, cc++) {
508 ssize_t rc;
509
510 if (backward)
511 src -= step_bytes, dst -= step_bytes;
512
513 DBG(MISC, ul_debug("#%05zu: src=%ju dst=%ju", cc, src, dst));
514
515 if (!sf->noact) {
516 /* read source */
517 if (lseek(fd, src, SEEK_SET) == (off_t) -1)
518 goto fail;
519 rc = read(fd, buf, step_bytes);
520 if (rc < 0 || rc != (ssize_t) step_bytes)
521 goto fail;
522
523 /* write target */
524 if (lseek(fd, dst, SEEK_SET) == (off_t) -1)
525 goto fail;
526 rc = write(fd, buf, step_bytes);
527 if (rc < 0 || rc != (ssize_t) step_bytes)
528 goto fail;
529 if (sf->movefsync)
530 fsync(fd);
531 }
532
533 /* write log */
534 if (f)
535 fprintf(f, "%05zu: %12ju %12ju\n", cc, src, dst);
536
537 if (progress && i % 10 == 0) {
538 unsigned int elapsed = 0; /* usec */
539 struct timeval cur_time;
540
541 gettimeofday(&cur_time, NULL);
542 if (cur_time.tv_sec - prev_time.tv_sec > 1) {
543 elapsed = ((cur_time.tv_sec - prev_time.tv_sec) * 1000000) +
544 (cur_time.tv_usec - prev_time.tv_usec);
545
546 bytes_per_sec = ((i - prev) * ss) / elapsed; /* per usec */
547 bytes_per_sec *= 1000000; /* per sec */
548
549 prev_time = cur_time;
550 prev = i;
551 }
552
553 if (bytes_per_sec)
554 fprintf(stdout, _("Moved %ju from %ju sectors (%.3f%%, %.1f MiB/s)."),
555 i + 1, nsectors,
556 100.0 / ((double) nsectors/(i+1)),
557 (double) (bytes_per_sec / (1024 * 1024)));
558 else
559 fprintf(stdout, _("Moved %ju from %ju sectors (%.3f%%)."),
560 i + 1, nsectors,
561 100.0 / ((double) nsectors/(i+1)));
562 fflush(stdout);
563 fputc('\r', stdout);
564
565 }
566
567 if (!backward)
568 src += step_bytes, dst += step_bytes;
569 }
570
571 if (progress) {
572 int x = get_terminal_width(80);
573 for (; x > 0; x--)
574 fputc(' ', stdout);
575 fflush(stdout);
576 fputc('\r', stdout);
577 fprintf(stdout, _("Moved %ju from %ju sectors (%.3f%%)."),
578 i, nsectors,
579 100.0 / ((double) nsectors/(i+1)));
580 fputc('\n', stdout);
581 }
582 if (f)
583 fclose(f);
584 free(buf);
585 free(devname);
586 free(typescript);
587
588 if (sf->noact)
589 fdisk_info(sf->cxt, _("Your data has not been moved (--no-act)."));
590
591 return 0;
592 fail:
593 errsv = -errno;
594 warn(_("%s: failed to move data"), devname);
595 if (f)
596 fclose(f);
597 free(buf);
598 free(devname);
599 free(typescript);
600
601 return errsv;
602 }
603
604 static int write_changes(struct sfdisk *sf)
605 {
606 int rc = 0;
607
608 if (sf->noact)
609 fdisk_info(sf->cxt, _("The partition table is unchanged (--no-act)."));
610 else
611 rc = fdisk_write_disklabel(sf->cxt);
612
613 if (rc == 0 && sf->movedata && sf->orig_pa)
614 rc = move_partition_data(sf, sf->partno, sf->orig_pa);
615
616 if (!sf->noact && !rc) {
617 fdisk_info(sf->cxt, _("\nThe partition table has been altered."));
618 if (!sf->notell) {
619 /* Let's wait a little bit. It's possible that our
620 * system is still busy with a previous re-read
621 * ioctl (on sfdisk start) or with another task
622 * related to the write to the device.
623 */
624 xusleep(250000);
625 fdisk_reread_partition_table(sf->cxt);
626 }
627 }
628
629 if (!rc)
630 rc = fdisk_deassign_device(sf->cxt,
631 sf->noact || sf->notell); /* no-sync */
632 return rc;
633 }
634
635 /*
636 * sfdisk --list [<device ..]
637 */
638 static int command_list_partitions(struct sfdisk *sf, int argc, char **argv)
639 {
640 int fail = 0;
641 fdisk_enable_listonly(sf->cxt, 1);
642
643 if (argc) {
644 int i, ct = 0;
645
646 for (i = 0; i < argc; i++) {
647 if (ct)
648 fputs("\n\n", stdout);
649 if (print_device_pt(sf->cxt, argv[i], 1, sf->verify) != 0)
650 fail++;
651 ct++;
652 }
653 } else
654 print_all_devices_pt(sf->cxt, sf->verify);
655
656 return fail;
657 }
658
659 /*
660 * sfdisk --list-free [<device ..]
661 */
662 static int command_list_freespace(struct sfdisk *sf, int argc, char **argv)
663 {
664 int fail = 0;
665 fdisk_enable_listonly(sf->cxt, 1);
666
667 if (argc) {
668 int i, ct = 0;
669
670 for (i = 0; i < argc; i++) {
671 if (ct)
672 fputs("\n\n", stdout);
673 if (print_device_freespace(sf->cxt, argv[i], 1) != 0)
674 fail++;
675 ct++;
676 }
677 } else
678 print_all_devices_freespace(sf->cxt);
679
680 return fail;
681 }
682
683 /*
684 * sfdisk --list-types
685 */
686 static int command_list_types(struct sfdisk *sf)
687 {
688 const struct fdisk_parttype *t;
689 struct fdisk_label *lb;
690 const char *name;
691 size_t i = 0;
692 int codes;
693
694 assert(sf);
695 assert(sf->cxt);
696
697 name = sf->label ? sf->label : "dos";
698 lb = fdisk_get_label(sf->cxt, name);
699 if (!lb)
700 errx(EXIT_FAILURE, _("unsupported label '%s'"), name);
701
702 codes = fdisk_label_has_code_parttypes(lb);
703 fputs(_("Id Name\n\n"), stdout);
704
705 while ((t = fdisk_label_get_parttype(lb, i++))) {
706 if (codes)
707 printf("%2x %s\n", fdisk_parttype_get_code(t),
708 fdisk_parttype_get_name(t));
709 else
710 printf("%s %s\n", fdisk_parttype_get_string(t),
711 fdisk_parttype_get_name(t));
712 }
713
714 return 0;
715 }
716
717 static int verify_device(struct sfdisk *sf, const char *devname)
718 {
719 int rc = 1;
720
721 fdisk_enable_listonly(sf->cxt, 1);
722
723 if (fdisk_assign_device(sf->cxt, devname, 1)) {
724 warn(_("cannot open %s"), devname);
725 return 1;
726 }
727
728 color_scheme_enable("header", UL_COLOR_BOLD);
729 fdisk_info(sf->cxt, "%s:", devname);
730 color_disable();
731
732 if (!fdisk_has_label(sf->cxt))
733 fdisk_info(sf->cxt, _("unrecognized partition table type"));
734 else
735 rc = fdisk_verify_disklabel(sf->cxt);
736
737 fdisk_deassign_device(sf->cxt, 1);
738 return rc;
739 }
740
741 /*
742 * sfdisk --verify [<device ..]
743 */
744 static int command_verify(struct sfdisk *sf, int argc, char **argv)
745 {
746 int nfails = 0, ct = 0;
747
748 if (argc) {
749 int i;
750 for (i = 0; i < argc; i++) {
751 if (i)
752 fdisk_info(sf->cxt, " ");
753 if (verify_device(sf, argv[i]) < 0)
754 nfails++;
755 }
756 } else {
757 FILE *f = NULL;
758 char *dev;
759
760 while ((dev = next_proc_partition(&f))) {
761 if (ct)
762 fdisk_info(sf->cxt, " ");
763 if (verify_device(sf, dev) < 0)
764 nfails++;
765 free(dev);
766 ct++;
767 }
768 }
769
770 return nfails;
771 }
772
773 static int get_size(const char *dev, int silent, uintmax_t *sz)
774 {
775 int fd, rc = 0;
776
777 fd = open(dev, O_RDONLY);
778 if (fd < 0) {
779 if (!silent)
780 warn(_("cannot open %s"), dev);
781 return -errno;
782 }
783
784 if (blkdev_get_sectors(fd, (unsigned long long *) sz) == -1) {
785 if (!silent)
786 warn(_("Cannot get size of %s"), dev);
787 rc = -errno;
788 }
789
790 close(fd);
791 return rc;
792 }
793
794 /*
795 * sfdisk --show-size [<device ..]
796 *
797 * (silly, but just for backward compatibility)
798 */
799 static int command_show_size(struct sfdisk *sf __attribute__((__unused__)),
800 int argc, char **argv)
801 {
802 uintmax_t sz;
803
804 if (argc) {
805 int i;
806 for (i = 0; i < argc; i++) {
807 if (get_size(argv[i], 0, &sz) == 0)
808 printf("%ju\n", sz / 2);
809 }
810 } else {
811 FILE *f = NULL;
812 uintmax_t total = 0;
813 char *dev;
814
815 while ((dev = next_proc_partition(&f))) {
816 if (get_size(dev, 1, &sz) == 0) {
817 printf("%s: %9ju\n", dev, sz / 2);
818 total += sz / 2;
819 }
820 free(dev);
821 }
822 if (total)
823 printf(_("total: %ju blocks\n"), total);
824 }
825
826 return 0;
827 }
828
829 static int print_geom(struct sfdisk *sf, const char *devname)
830 {
831 fdisk_enable_listonly(sf->cxt, 1);
832
833 if (fdisk_assign_device(sf->cxt, devname, 1)) {
834 warn(_("cannot open %s"), devname);
835 return 1;
836 }
837
838 fdisk_info(sf->cxt, "%s: %ju cylinders, %ju heads, %ju sectors/track",
839 devname,
840 (uintmax_t) fdisk_get_geom_cylinders(sf->cxt),
841 (uintmax_t) fdisk_get_geom_heads(sf->cxt),
842 (uintmax_t) fdisk_get_geom_sectors(sf->cxt));
843
844 fdisk_deassign_device(sf->cxt, 1);
845 return 0;
846 }
847
848 /*
849 * sfdisk --show-geometry [<device ..]
850 */
851 static int command_show_geometry(struct sfdisk *sf, int argc, char **argv)
852 {
853 int nfails = 0;
854
855 if (argc) {
856 int i;
857 for (i = 0; i < argc; i++) {
858 if (print_geom(sf, argv[i]) < 0)
859 nfails++;
860 }
861 } else {
862 FILE *f = NULL;
863 char *dev;
864
865 while ((dev = next_proc_partition(&f))) {
866 if (print_geom(sf, dev) < 0)
867 nfails++;
868 free(dev);
869 }
870 }
871
872 return nfails;
873 }
874
875 /*
876 * sfdisk --activate <device> [<partno> ...]
877 */
878 static int command_activate(struct sfdisk *sf, int argc, char **argv)
879 {
880 int rc, nparts, i, listonly;
881 struct fdisk_partition *pa = NULL;
882 const char *devname = NULL;
883
884 if (argc < 1)
885 errx(EXIT_FAILURE, _("no disk device specified"));
886 devname = argv[0];
887
888 /* --activate <device> */
889 listonly = argc == 1;
890
891 rc = fdisk_assign_device(sf->cxt, devname, listonly);
892 if (rc)
893 err(EXIT_FAILURE, _("cannot open %s"), devname);
894
895 if (fdisk_is_label(sf->cxt, GPT)) {
896 if (fdisk_gpt_is_hybrid(sf->cxt))
897 errx(EXIT_FAILURE, _("toggle boot flags is unsupported for Hybrid GPT/MBR"));
898
899 /* Switch from GPT to PMBR */
900 sf->cxt = fdisk_new_nested_context(sf->cxt, "dos");
901 if (!sf->cxt)
902 err(EXIT_FAILURE, _("cannot switch to PMBR"));
903 fdisk_info(sf->cxt, _("Activation is unsupported for GPT -- entering nested PMBR."));
904
905 } else if (!fdisk_is_label(sf->cxt, DOS))
906 errx(EXIT_FAILURE, _("toggle boot flags is supported for MBR or PMBR only"));
907
908 if (!listonly && sf->backup)
909 backup_partition_table(sf, devname);
910
911 nparts = fdisk_get_npartitions(sf->cxt);
912 for (i = 0; i < nparts; i++) {
913 char *data = NULL;
914
915 /* note that fdisk_get_partition() reuses the @pa pointer, you
916 * don't have to (re)allocate it */
917 if (fdisk_get_partition(sf->cxt, i, &pa) != 0)
918 continue;
919
920 /* sfdisk --activate list bootable partitions */
921 if (listonly) {
922 if (!fdisk_partition_is_bootable(pa))
923 continue;
924 if (fdisk_partition_to_string(pa, sf->cxt,
925 FDISK_FIELD_DEVICE, &data) == 0) {
926 printf("%s\n", data);
927 free(data);
928 }
929
930 /* deactivate all active partitions */
931 } else if (fdisk_partition_is_bootable(pa))
932 fdisk_toggle_partition_flag(sf->cxt, i, DOS_FLAG_ACTIVE);
933 }
934
935 /* sfdisk --activate <partno> [..] */
936 for (i = 1; i < argc; i++) {
937 int n;
938
939 if (i == 1 && strcmp(argv[1], "-") == 0)
940 break;
941 n = strtou32_or_err(argv[i], _("failed to parse partition number"));
942
943 rc = fdisk_toggle_partition_flag(sf->cxt, n - 1, DOS_FLAG_ACTIVE);
944 if (rc)
945 errx(EXIT_FAILURE,
946 _("%s: partition %d: failed to toggle bootable flag"),
947 devname, i + 1);
948 }
949
950 fdisk_unref_partition(pa);
951
952 if (listonly)
953 rc = fdisk_deassign_device(sf->cxt, 1);
954 else
955 rc = write_changes(sf);
956 return rc;
957 }
958
959 /*
960 * sfdisk --delete <device> [<partno> ...]
961 */
962 static int command_delete(struct sfdisk *sf, int argc, char **argv)
963 {
964 size_t i;
965 const char *devname = NULL;
966
967 if (argc < 1)
968 errx(EXIT_FAILURE, _("no disk device specified"));
969 devname = argv[0];
970
971 if (fdisk_assign_device(sf->cxt, devname, 0) != 0)
972 err(EXIT_FAILURE, _("cannot open %s"), devname);
973
974 if (sf->backup)
975 backup_partition_table(sf, devname);
976
977 /* delete all */
978 if (argc == 1) {
979 size_t nparts = fdisk_get_npartitions(sf->cxt);
980 for (i = 0; i < nparts; i++) {
981 if (fdisk_is_partition_used(sf->cxt, i) &&
982 fdisk_delete_partition(sf->cxt, i) != 0)
983 errx(EXIT_FAILURE, _("%s: partition %zu: failed to delete"), devname, i + 1);
984 }
985 /* delete specified */
986 } else {
987 for (i = 1; i < (size_t) argc; i++) {
988 size_t n = strtou32_or_err(argv[i], _("failed to parse partition number"));
989
990 if (fdisk_delete_partition(sf->cxt, n - 1) != 0)
991 errx(EXIT_FAILURE, _("%s: partition %zu: failed to delete"), devname, n);
992 }
993 }
994
995 return write_changes(sf);
996 }
997
998 /*
999 * sfdisk --reorder <device>
1000 */
1001 static int command_reorder(struct sfdisk *sf, int argc, char **argv)
1002 {
1003 const char *devname = NULL;
1004 int rc;
1005
1006 if (argc)
1007 devname = argv[0];
1008 if (!devname)
1009 errx(EXIT_FAILURE, _("no disk device specified"));
1010
1011 rc = fdisk_assign_device(sf->cxt, devname, 0); /* read-write */
1012 if (rc)
1013 err(EXIT_FAILURE, _("cannot open %s"), devname);
1014
1015 if (sf->backup)
1016 backup_partition_table(sf, devname);
1017
1018 if (fdisk_reorder_partitions(sf->cxt) == 1) /* unchanged */
1019 rc = fdisk_deassign_device(sf->cxt, 1);
1020 else
1021 rc = write_changes(sf);
1022
1023 return rc;
1024 }
1025
1026
1027 /*
1028 * sfdisk --dump <device>
1029 */
1030 static int command_dump(struct sfdisk *sf, int argc, char **argv)
1031 {
1032 const char *devname = NULL;
1033 struct fdisk_script *dp;
1034 int rc;
1035
1036 if (argc)
1037 devname = argv[0];
1038 if (!devname)
1039 errx(EXIT_FAILURE, _("no disk device specified"));
1040
1041 rc = fdisk_assign_device(sf->cxt, devname, 1); /* read-only */
1042 if (rc)
1043 err(EXIT_FAILURE, _("cannot open %s"), devname);
1044
1045 if (!fdisk_has_label(sf->cxt))
1046 errx(EXIT_FAILURE, _("%s: does not contain a recognized partition table"), devname);
1047
1048 dp = fdisk_new_script(sf->cxt);
1049 if (!dp)
1050 err(EXIT_FAILURE, _("failed to allocate dump struct"));
1051
1052 rc = fdisk_script_read_context(dp, NULL);
1053 if (rc)
1054 errx(EXIT_FAILURE, _("%s: failed to dump partition table"), devname);
1055
1056 if (sf->json)
1057 fdisk_script_enable_json(dp, 1);
1058 fdisk_script_write_file(dp, stdout);
1059
1060 fdisk_unref_script(dp);
1061 fdisk_deassign_device(sf->cxt, 1); /* no-sync() */
1062 return 0;
1063 }
1064
1065 static void assign_device_partition(struct sfdisk *sf,
1066 const char *devname,
1067 size_t partno,
1068 int rdonly)
1069 {
1070 int rc;
1071 size_t n;
1072 struct fdisk_label *lb = NULL;
1073
1074 assert(sf);
1075 assert(devname);
1076
1077 /* read-only when a new <type> undefined */
1078 rc = fdisk_assign_device(sf->cxt, devname, rdonly);
1079 if (rc)
1080 err(EXIT_FAILURE, _("cannot open %s"), devname);
1081
1082 lb = fdisk_get_label(sf->cxt, NULL);
1083 if (!lb)
1084 errx(EXIT_FAILURE, _("%s: no partition table found"), devname);
1085
1086 n = fdisk_get_npartitions(sf->cxt);
1087 if (partno > n)
1088 errx(EXIT_FAILURE, _("%s: partition %zu: partition table contains "
1089 "only %zu partitions"), devname, partno, n);
1090 if (!fdisk_is_partition_used(sf->cxt, partno - 1))
1091 errx(EXIT_FAILURE, _("%s: partition %zu: partition is unused"),
1092 devname, partno);
1093 }
1094
1095 /*
1096 * sfdisk --part-type <device> <partno> [<type>]
1097 */
1098 static int command_parttype(struct sfdisk *sf, int argc, char **argv)
1099 {
1100 size_t partno;
1101 struct fdisk_parttype *type = NULL;
1102 struct fdisk_label *lb;
1103 const char *devname = NULL, *typestr = NULL;
1104
1105 if (!argc)
1106 errx(EXIT_FAILURE, _("no disk device specified"));
1107 devname = argv[0];
1108
1109 if (argc < 2)
1110 errx(EXIT_FAILURE, _("no partition number specified"));
1111 partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
1112
1113 if (argc == 3)
1114 typestr = argv[2];
1115 else if (argc > 3)
1116 errx(EXIT_FAILURE, _("unexpected arguments"));
1117
1118 /* read-only when a new <type> undefined */
1119 assign_device_partition(sf, devname, partno, !typestr);
1120
1121 lb = fdisk_get_label(sf->cxt, NULL);
1122
1123 /* print partition type */
1124 if (!typestr) {
1125 const struct fdisk_parttype *t = NULL;
1126 struct fdisk_partition *pa = NULL;
1127
1128 if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
1129 t = fdisk_partition_get_type(pa);
1130 if (!t)
1131 errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition type"),
1132 devname, partno);
1133
1134 if (fdisk_label_has_code_parttypes(lb))
1135 printf("%2x\n", fdisk_parttype_get_code(t));
1136 else
1137 printf("%s\n", fdisk_parttype_get_string(t));
1138
1139 fdisk_unref_partition(pa);
1140 fdisk_deassign_device(sf->cxt, 1);
1141 return 0;
1142 }
1143
1144 if (sf->backup)
1145 backup_partition_table(sf, devname);
1146
1147 /* parse <type> and apply to PT */
1148 type = fdisk_label_parse_parttype(lb, typestr);
1149 if (!type)
1150 errx(EXIT_FAILURE, _("failed to parse %s partition type '%s'"),
1151 fdisk_label_get_name(lb), typestr);
1152
1153 else if (fdisk_set_partition_type(sf->cxt, partno - 1, type) != 0)
1154 errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition type"),
1155 devname, partno);
1156 fdisk_unref_parttype(type);
1157 return write_changes(sf);
1158 }
1159
1160 /*
1161 * sfdisk --part-uuid <device> <partno> [<uuid>]
1162 */
1163 static int command_partuuid(struct sfdisk *sf, int argc, char **argv)
1164 {
1165 size_t partno;
1166 struct fdisk_partition *pa = NULL;
1167 const char *devname = NULL, *uuid = NULL;
1168
1169 if (!argc)
1170 errx(EXIT_FAILURE, _("no disk device specified"));
1171 devname = argv[0];
1172
1173 if (argc < 2)
1174 errx(EXIT_FAILURE, _("no partition number specified"));
1175 partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
1176
1177 if (argc == 3)
1178 uuid = argv[2];
1179 else if (argc > 3)
1180 errx(EXIT_FAILURE, _("unexpected arguments"));
1181
1182 /* read-only if uuid not given */
1183 assign_device_partition(sf, devname, partno, !uuid);
1184
1185 /* print partition uuid */
1186 if (!uuid) {
1187 const char *str = NULL;
1188
1189 if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
1190 str = fdisk_partition_get_uuid(pa);
1191 if (!str)
1192 errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition UUID"),
1193 devname, partno);
1194 printf("%s\n", str);
1195 fdisk_unref_partition(pa);
1196 fdisk_deassign_device(sf->cxt, 1);
1197 return 0;
1198 }
1199
1200 if (sf->backup)
1201 backup_partition_table(sf, devname);
1202
1203 pa = fdisk_new_partition();
1204 if (!pa)
1205 err(EXIT_FAILURE, _("failed to allocate partition object"));
1206
1207 if (fdisk_partition_set_uuid(pa, uuid) != 0 ||
1208 fdisk_set_partition(sf->cxt, partno - 1, pa) != 0)
1209 errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition UUID"),
1210 devname, partno);
1211 fdisk_unref_partition(pa);
1212 return write_changes(sf);
1213 }
1214
1215 /*
1216 * sfdisk --part-label <device> <partno> [<label>]
1217 */
1218 static int command_partlabel(struct sfdisk *sf, int argc, char **argv)
1219 {
1220 size_t partno;
1221 struct fdisk_partition *pa = NULL;
1222 const char *devname = NULL, *name = NULL;
1223
1224 if (!argc)
1225 errx(EXIT_FAILURE, _("no disk device specified"));
1226 devname = argv[0];
1227
1228 if (argc < 2)
1229 errx(EXIT_FAILURE, _("no partition number specified"));
1230 partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
1231
1232 if (argc == 3)
1233 name = argv[2];
1234 else if (argc > 3)
1235 errx(EXIT_FAILURE, _("unexpected arguments"));
1236
1237 /* read-only if name not given */
1238 assign_device_partition(sf, devname, partno, !name);
1239
1240 /* print partition name */
1241 if (!name) {
1242 const char *str = NULL;
1243
1244 if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
1245 str = fdisk_partition_get_name(pa);
1246 if (!str)
1247 errx(EXIT_FAILURE, _("%s: partition %zu: failed to get partition name"),
1248 devname, partno);
1249 printf("%s\n", str);
1250 fdisk_unref_partition(pa);
1251 fdisk_deassign_device(sf->cxt, 1);
1252 return 0;
1253 }
1254
1255 if (sf->backup)
1256 backup_partition_table(sf, devname);
1257
1258 pa = fdisk_new_partition();
1259 if (!pa)
1260 err(EXIT_FAILURE, _("failed to allocate partition object"));
1261
1262 if (fdisk_partition_set_name(pa, name) != 0 ||
1263 fdisk_set_partition(sf->cxt, partno - 1, pa) != 0)
1264 errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition name"),
1265 devname, partno);
1266
1267 fdisk_unref_partition(pa);
1268 return write_changes(sf);
1269 }
1270
1271 /*
1272 * sfdisk --part-attrs <device> <partno> [<attrs>]
1273 */
1274 static int command_partattrs(struct sfdisk *sf, int argc, char **argv)
1275 {
1276 size_t partno;
1277 struct fdisk_partition *pa = NULL;
1278 const char *devname = NULL, *attrs = NULL;
1279
1280 if (!argc)
1281 errx(EXIT_FAILURE, _("no disk device specified"));
1282 devname = argv[0];
1283
1284 if (argc < 2)
1285 errx(EXIT_FAILURE, _("no partition number specified"));
1286 partno = strtou32_or_err(argv[1], _("failed to parse partition number"));
1287
1288 if (argc == 3)
1289 attrs = argv[2];
1290 else if (argc > 3)
1291 errx(EXIT_FAILURE, _("unexpected arguments"));
1292
1293 /* read-only if name not given */
1294 assign_device_partition(sf, devname, partno, !attrs);
1295
1296 /* print partition name */
1297 if (!attrs) {
1298 const char *str = NULL;
1299
1300 if (fdisk_get_partition(sf->cxt, partno - 1, &pa) == 0)
1301 str = fdisk_partition_get_attrs(pa);
1302 if (str)
1303 printf("%s\n", str);
1304 fdisk_unref_partition(pa);
1305 fdisk_deassign_device(sf->cxt, 1);
1306 return 0;
1307 }
1308
1309 if (sf->backup)
1310 backup_partition_table(sf, devname);
1311
1312 pa = fdisk_new_partition();
1313 if (!pa)
1314 err(EXIT_FAILURE, _("failed to allocate partition object"));
1315
1316 if (fdisk_partition_set_attrs(pa, attrs) != 0 ||
1317 fdisk_set_partition(sf->cxt, partno - 1, pa) != 0)
1318 errx(EXIT_FAILURE, _("%s: partition %zu: failed to set partition attributes"),
1319 devname, partno);
1320
1321 fdisk_unref_partition(pa);
1322 return write_changes(sf);
1323 }
1324
1325 static void sfdisk_print_partition(struct sfdisk *sf, size_t n)
1326 {
1327 struct fdisk_partition *pa = NULL;
1328 char *data;
1329
1330 assert(sf);
1331
1332 if (sf->quiet)
1333 return;
1334 if (fdisk_get_partition(sf->cxt, n, &pa) != 0)
1335 return;
1336
1337 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_DEVICE, &data);
1338 printf("%12s : ", data);
1339
1340 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_START, &data);
1341 printf("%12s ", data);
1342
1343 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_END, &data);
1344 printf("%12s ", data);
1345
1346 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_SIZE, &data);
1347 printf("(%s) ", data);
1348
1349 fdisk_partition_to_string(pa, sf->cxt, FDISK_FIELD_TYPE, &data);
1350 printf("%s\n", data);
1351
1352 fdisk_unref_partition(pa);
1353 }
1354
1355 static void command_fdisk_help(void)
1356 {
1357 fputs(_("\nHelp:\n"), stdout);
1358
1359 fputc('\n', stdout);
1360 color_scheme_enable("help-title", UL_COLOR_BOLD);
1361 fputs(_(" Commands:\n"), stdout);
1362 color_disable();
1363 fputs(_(" write write table to disk and exit\n"), stdout);
1364 fputs(_(" quit show new situation and wait for user's feedback before write\n"), stdout);
1365 fputs(_(" abort exit sfdisk shell\n"), stdout);
1366 fputs(_(" print display the partition table\n"), stdout);
1367 fputs(_(" help show this help text\n"), stdout);
1368 fputc('\n', stdout);
1369 fputs(_(" Ctrl-D the same as 'quit'\n"), stdout);
1370
1371 fputc('\n', stdout);
1372 color_scheme_enable("help-title", UL_COLOR_BOLD);
1373 fputs(_(" Input format:\n"), stdout);
1374 color_disable();
1375 fputs(_(" <start>, <size>, <type>, <bootable>\n"), stdout);
1376
1377 fputc('\n', stdout);
1378 fputs(_(" <start> Beginning of the partition in sectors, or bytes if\n"
1379 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1380 " The default is the first free space.\n"), stdout);
1381
1382 fputc('\n', stdout);
1383 fputs(_(" <size> Size of the partition in sectors, or bytes if\n"
1384 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1385 " The default is all available space.\n"), stdout);
1386
1387 fputc('\n', stdout);
1388 fputs(_(" <type> The partition type. Default is a Linux data partition.\n"), stdout);
1389 fputs(_(" MBR: hex or L,S,E,X,U,R,V shortcuts.\n"), stdout);
1390 fputs(_(" GPT: UUID or L,S,H,U,R,V shortcuts.\n"), stdout);
1391
1392 fputc('\n', stdout);
1393 fputs(_(" <bootable> Use '*' to mark an MBR partition as bootable.\n"), stdout);
1394
1395 fputc('\n', stdout);
1396 color_scheme_enable("help-title", UL_COLOR_BOLD);
1397 fputs(_(" Example:\n"), stdout);
1398 color_disable();
1399 fputs(_(" , 4G Creates a 4GiB partition at default start offset.\n"), stdout);
1400 fputc('\n', stdout);
1401 }
1402
1403 enum {
1404 SFDISK_DONE_NONE = 0,
1405 SFDISK_DONE_EOF,
1406 SFDISK_DONE_ABORT,
1407 SFDISK_DONE_WRITE,
1408 SFDISK_DONE_ASK
1409 };
1410
1411 /* returns: 0 on success, <0 on error, 1 successfully stop sfdisk */
1412 static int loop_control_commands(struct sfdisk *sf,
1413 struct fdisk_script *dp,
1414 char *buf)
1415 {
1416 const char *p = skip_blank(buf);
1417 int rc = SFDISK_DONE_NONE;
1418
1419 if (strcmp(p, "print") == 0)
1420 list_disklabel(sf->cxt);
1421 else if (strcmp(p, "help") == 0)
1422 command_fdisk_help();
1423 else if (strcmp(p, "quit") == 0)
1424 rc = SFDISK_DONE_ASK;
1425 else if (strcmp(p, "write") == 0)
1426 rc = SFDISK_DONE_WRITE;
1427 else if (strcmp(p, "abort") == 0)
1428 rc = SFDISK_DONE_ABORT;
1429 else {
1430 if (sf->interactive)
1431 fdisk_warnx(sf->cxt, _("unsupported command"));
1432 else {
1433 fdisk_warnx(sf->cxt, _("line %d: unsupported command"),
1434 fdisk_script_get_nlines(dp));
1435 rc = -EINVAL;
1436 }
1437 }
1438 return rc;
1439 }
1440
1441 static int has_container(struct sfdisk *sf)
1442 {
1443 size_t i, nparts;
1444 struct fdisk_partition *pa = NULL;
1445
1446 if (sf->container)
1447 return sf->container;
1448
1449 nparts = fdisk_get_npartitions(sf->cxt);
1450 for (i = 0; i < nparts; i++) {
1451 if (fdisk_get_partition(sf->cxt, i, &pa) != 0)
1452 continue;
1453 if (fdisk_partition_is_container(pa)) {
1454 sf->container = 1;
1455 break;
1456 }
1457 }
1458
1459 fdisk_unref_partition(pa);
1460 return sf->container;
1461 }
1462
1463 static size_t last_pt_partno(struct sfdisk *sf)
1464 {
1465 size_t i, nparts, partno = 0;
1466 struct fdisk_partition *pa = NULL;
1467
1468
1469 nparts = fdisk_get_npartitions(sf->cxt);
1470 for (i = 0; i < nparts; i++) {
1471 size_t x;
1472
1473 if (fdisk_get_partition(sf->cxt, i, &pa) != 0 ||
1474 !fdisk_partition_is_used(pa))
1475 continue;
1476 x = fdisk_partition_get_partno(pa);
1477 if (x > partno)
1478 partno = x;
1479 }
1480
1481 fdisk_unref_partition(pa);
1482 return partno;
1483 }
1484
1485 #ifdef HAVE_LIBREADLINE
1486 static char *sfdisk_fgets(struct fdisk_script *dp,
1487 char *buf, size_t bufsz, FILE *f)
1488 {
1489 struct sfdisk *sf = (struct sfdisk *) fdisk_script_get_userdata(dp);
1490
1491 assert(dp);
1492 assert(buf);
1493 assert(bufsz > 2);
1494
1495 if (sf->interactive) {
1496 char *p = readline(sf->prompt);
1497 size_t len;
1498
1499 if (!p)
1500 return NULL;
1501 len = strlen(p);
1502 if (len > bufsz - 2)
1503 len = bufsz - 2;
1504
1505 memcpy(buf, p, len);
1506 buf[len] = '\n'; /* append \n to be compatible with libc fgetc() */
1507 buf[len + 1] = '\0';
1508 free(p);
1509 fflush(stdout);
1510 return buf;
1511 }
1512 return fgets(buf, bufsz, f);
1513 }
1514 #endif
1515
1516 static int ignore_partition(struct fdisk_partition *pa)
1517 {
1518 /* incomplete partition setting */
1519 if (!fdisk_partition_has_start(pa) && !fdisk_partition_start_is_default(pa))
1520 return 1;
1521 if (!fdisk_partition_has_size(pa) && !fdisk_partition_end_is_default(pa))
1522 return 1;
1523
1524 /* probably dump from old sfdisk with start=0 size=0 */
1525 if (fdisk_partition_has_start(pa) && fdisk_partition_get_start(pa) == 0 &&
1526 fdisk_partition_has_size(pa) && fdisk_partition_get_size(pa) == 0)
1527 return 1;
1528
1529 return 0;
1530 }
1531
1532 static void follow_wipe_mode(struct sfdisk *sf)
1533 {
1534 int dowipe = sf->wipemode == WIPEMODE_ALWAYS ? 1 : 0;
1535
1536 if (sf->interactive && sf->wipemode == WIPEMODE_AUTO)
1537 dowipe = 1; /* do it in interactive mode */
1538
1539 if (fdisk_is_ptcollision(sf->cxt) && sf->wipemode != WIPEMODE_NEVER)
1540 dowipe = 1; /* always wipe old PT */
1541
1542 fdisk_enable_wipe(sf->cxt, dowipe);
1543 if (sf->quiet)
1544 return;
1545
1546 if (dowipe) {
1547 if (!fdisk_is_ptcollision(sf->cxt)) {
1548 fdisk_warnx(sf->cxt, _(
1549 "The device contains '%s' signature and it will be removed by a write command. "
1550 "See sfdisk(8) man page and --wipe option for more details."),
1551 fdisk_get_collision(sf->cxt));
1552 fputc('\n', stdout);
1553 }
1554 } else {
1555 fdisk_warnx(sf->cxt, _(
1556 "The device contains '%s' signature and it may remain on the device. "
1557 "It is recommended to wipe the device with wipefs(8) or "
1558 "sfdisk --wipe, in order to avoid possible collisions."),
1559 fdisk_get_collision(sf->cxt));
1560 fputc('\n', stderr);
1561 }
1562 }
1563
1564 static int wipe_partition(struct sfdisk *sf, size_t partno)
1565 {
1566 int rc, yes = 0;
1567 char *fstype = NULL;
1568 struct fdisk_partition *tmp = NULL;
1569
1570 DBG(MISC, ul_debug("checking for signature"));
1571
1572 rc = fdisk_get_partition(sf->cxt, partno, &tmp);
1573 if (rc)
1574 goto done;
1575
1576 rc = fdisk_partition_to_string(tmp, sf->cxt, FDISK_FIELD_FSTYPE, &fstype);
1577 if (rc || fstype == NULL)
1578 goto done;
1579
1580 fdisk_warnx(sf->cxt, _("Partition #%zu contains a %s signature."), partno + 1, fstype);
1581
1582 if (sf->pwipemode == WIPEMODE_AUTO && isatty(STDIN_FILENO))
1583 fdisk_ask_yesno(sf->cxt, _("Do you want to remove the signature?"), &yes);
1584 else if (sf->pwipemode == WIPEMODE_ALWAYS)
1585 yes = 1;
1586
1587 if (yes) {
1588 fdisk_info(sf->cxt, _("The signature will be removed by a write command."));
1589 rc = fdisk_wipe_partition(sf->cxt, partno, TRUE);
1590 }
1591 done:
1592 fdisk_unref_partition(tmp);
1593 free(fstype);
1594 DBG(MISC, ul_debug("partition wipe check end [rc=%d]", rc));
1595 return rc;
1596 }
1597
1598 static void refresh_prompt_buffer(struct sfdisk *sf, const char *devname,
1599 size_t next_partno, int created)
1600 {
1601 if (created) {
1602 char *partname = fdisk_partname(devname, next_partno + 1);
1603 if (!partname)
1604 err(EXIT_FAILURE, _("failed to allocate partition name"));
1605
1606 if (!sf->prompt || !startswith(sf->prompt, partname)) {
1607 free(sf->prompt);
1608 xasprintf(&sf->prompt,"%s: ", partname);
1609 }
1610 free(partname);
1611 } else if (!sf->prompt || !startswith(sf->prompt, SFDISK_PROMPT)) {
1612 free(sf->prompt);
1613 sf->prompt = xstrdup(SFDISK_PROMPT);
1614 }
1615 }
1616
1617 /*
1618 * sfdisk <device> [[-N] <partno>]
1619 *
1620 * Note that the option -N is there for backward compatibility only.
1621 */
1622 static int command_fdisk(struct sfdisk *sf, int argc, char **argv)
1623 {
1624 int rc = 0, partno = sf->partno, created = 0, unused = 0;
1625 struct fdisk_script *dp;
1626 struct fdisk_table *tb = NULL;
1627 const char *devname = NULL, *label;
1628 char buf[BUFSIZ];
1629 size_t next_partno = (size_t) -1;
1630
1631 if (argc)
1632 devname = argv[0];
1633 if (partno < 0 && argc > 1)
1634 partno = strtou32_or_err(argv[1],
1635 _("failed to parse partition number"));
1636 if (!devname)
1637 errx(EXIT_FAILURE, _("no disk device specified"));
1638
1639 rc = fdisk_assign_device(sf->cxt, devname, 0);
1640 if (rc)
1641 err(EXIT_FAILURE, _("cannot open %s"), devname);
1642
1643 dp = fdisk_new_script(sf->cxt);
1644 if (!dp)
1645 err(EXIT_FAILURE, _("failed to allocate script handler"));
1646 fdisk_set_script(sf->cxt, dp);
1647 #ifdef HAVE_LIBREADLINE
1648 fdisk_script_set_fgets(dp, sfdisk_fgets);
1649 #endif
1650 fdisk_script_set_userdata(dp, (void *) sf);
1651
1652 /*
1653 * Don't create a new disklabel when [-N] <partno> specified. In this
1654 * case reuse already specified disklabel. Let's check that the disk
1655 * really contains the partition.
1656 */
1657 if (partno >= 0) {
1658 size_t n;
1659
1660 if (!fdisk_has_label(sf->cxt))
1661 errx(EXIT_FAILURE, _("%s: cannot modify partition %d: "
1662 "no partition table was found"),
1663 devname, partno + 1);
1664 n = fdisk_get_npartitions(sf->cxt);
1665 if ((size_t) partno > n)
1666 errx(EXIT_FAILURE, _("%s: cannot modify partition %d: "
1667 "partition table contains only %zu "
1668 "partitions"),
1669 devname, partno + 1, n);
1670
1671 if (!fdisk_is_partition_used(sf->cxt, partno)) {
1672 fdisk_warnx(sf->cxt, _("warning: %s: partition %d is not defined yet"),
1673 devname, partno + 1);
1674 unused = 1;
1675 }
1676 created = 1;
1677 next_partno = partno;
1678
1679 if (sf->movedata)
1680 sf->orig_pa = get_partition(sf->cxt, partno);
1681 }
1682
1683 if (sf->append) {
1684 created = 1;
1685 next_partno = last_pt_partno(sf) + 1;
1686 }
1687
1688 if (!sf->quiet && sf->interactive) {
1689 color_scheme_enable("welcome", UL_COLOR_GREEN);
1690 fdisk_info(sf->cxt, _("\nWelcome to sfdisk (%s)."), PACKAGE_STRING);
1691 color_disable();
1692 fdisk_info(sf->cxt, _("Changes will remain in memory only, until you decide to write them.\n"
1693 "Be careful before using the write command.\n"));
1694 }
1695
1696 if (!sf->noact && !sf->noreread) {
1697 if (!sf->quiet)
1698 fputs(_("Checking that no-one is using this disk right now ..."), stdout);
1699 if (fdisk_device_is_used(sf->cxt)) {
1700 if (!sf->quiet)
1701 fputs(_(" FAILED\n\n"), stdout);
1702
1703 fdisk_warnx(sf->cxt, _(
1704 "This disk is currently in use - repartitioning is probably a bad idea.\n"
1705 "Umount all file systems, and swapoff all swap partitions on this disk.\n"
1706 "Use the --no-reread flag to suppress this check.\n"));
1707
1708 if (!sf->force)
1709 errx(EXIT_FAILURE, _("Use the --force flag to overrule all checks."));
1710 } else if (!sf->quiet)
1711 fputs(_(" OK\n\n"), stdout);
1712 }
1713
1714 if (fdisk_get_collision(sf->cxt))
1715 follow_wipe_mode(sf);
1716
1717 if (sf->backup)
1718 backup_partition_table(sf, devname);
1719
1720 if (!sf->quiet) {
1721 list_disk_geometry(sf->cxt);
1722 if (fdisk_has_label(sf->cxt)) {
1723 fdisk_info(sf->cxt, _("\nOld situation:"));
1724 list_disklabel(sf->cxt);
1725 }
1726 }
1727
1728 if (sf->label)
1729 label = sf->label;
1730 else if (fdisk_has_label(sf->cxt))
1731 label = fdisk_label_get_name(fdisk_get_label(sf->cxt, NULL));
1732 else
1733 label = "dos"; /* just for backward compatibility */
1734
1735 if (fdisk_script_set_header(dp, "label", label) != 0)
1736 errx(EXIT_FAILURE, _("failed to set script header"));
1737
1738 if (!sf->quiet && sf->interactive) {
1739 if (!fdisk_has_label(sf->cxt) && !sf->label)
1740 fdisk_info(sf->cxt,
1741 _("\nsfdisk is going to create a new '%s' disk label.\n"
1742 "Use 'label: <name>' before you define a first partition\n"
1743 "to override the default."), label);
1744 fdisk_info(sf->cxt, _("\nType 'help' to get more information.\n"));
1745 } else if (!sf->quiet)
1746 fputc('\n', stdout);
1747
1748 tb = fdisk_script_get_table(dp);
1749 assert(tb);
1750
1751 do {
1752 size_t nparts;
1753
1754 DBG(PARSE, ul_debug("<---next-line--->"));
1755 if (next_partno == (size_t) -1)
1756 next_partno = fdisk_table_get_nents(tb);
1757
1758 if (created
1759 && partno < 0
1760 && next_partno == fdisk_get_npartitions(sf->cxt)
1761 && !has_container(sf)) {
1762 fdisk_info(sf->cxt, _("All partitions used."));
1763 rc = SFDISK_DONE_ASK;
1764 break;
1765 }
1766
1767 refresh_prompt_buffer(sf, devname, next_partno, created);
1768
1769
1770 if (sf->prompt && (sf->interactive || !sf->quiet)) {
1771 #ifndef HAVE_LIBREADLINE
1772 fputs(sf->prompt, stdout);
1773 #else
1774 if (!sf->interactive)
1775 fputs(sf->prompt, stdout);
1776 #endif
1777 }
1778
1779 rc = fdisk_script_read_line(dp, stdin, buf, sizeof(buf));
1780 if (rc < 0) {
1781 DBG(PARSE, ul_debug("script parsing failed, trying sfdisk specific commands"));
1782 buf[sizeof(buf) - 1] = '\0';
1783 rc = loop_control_commands(sf, dp, buf);
1784 if (rc)
1785 break;
1786 continue;
1787 } else if (rc == 1) {
1788 rc = SFDISK_DONE_EOF;
1789 if (!sf->quiet)
1790 fputs(_("Done.\n"), stdout);
1791 break;
1792 }
1793
1794 nparts = fdisk_table_get_nents(tb);
1795 if (nparts) {
1796 size_t cur_partno = (size_t) -1;
1797 struct fdisk_partition *pa = fdisk_table_get_partition(tb, nparts - 1);
1798
1799 assert(pa);
1800
1801 if (ignore_partition(pa)) {
1802 fdisk_info(sf->cxt, _("Ignoring partition."));
1803 next_partno++;
1804 continue;
1805 }
1806 if (!created) { /* create a new disklabel */
1807 rc = fdisk_apply_script_headers(sf->cxt, dp);
1808 created = !rc;
1809 if (rc)
1810 fdisk_warnx(sf->cxt, _(
1811 "Failed to apply script headers, "
1812 "disk label not created."));
1813
1814 if (rc == 0 && fdisk_get_collision(sf->cxt))
1815 follow_wipe_mode(sf);
1816 }
1817 if (!rc && partno >= 0) { /* -N <partno>, modify partition */
1818 rc = fdisk_set_partition(sf->cxt, partno, pa);
1819 rc = rc == 0 ? SFDISK_DONE_ASK : SFDISK_DONE_ABORT;
1820 break;
1821 } else if (!rc) { /* add partition */
1822 if (!sf->interactive && !sf->quiet &&
1823 (!sf->prompt || startswith(sf->prompt, SFDISK_PROMPT))) {
1824 refresh_prompt_buffer(sf, devname, next_partno, created);
1825 fputs(sf->prompt, stdout);
1826 }
1827 rc = fdisk_add_partition(sf->cxt, pa, &cur_partno);
1828 if (rc) {
1829 errno = -rc;
1830 fdisk_warn(sf->cxt, _("Failed to add #%d partition"), next_partno + 1);
1831 }
1832 }
1833
1834 /* wipe partition on success
1835 *
1836 * Note that unused=1 means -N <partno> for unused,
1837 * otherwise we wipe only newly created partitions.
1838 */
1839 if (rc == 0 && (unused || partno < 0)) {
1840 rc = wipe_partition(sf, unused ? (size_t) partno : cur_partno);
1841 if (rc)
1842 errno = -rc;
1843 }
1844
1845 if (!rc) {
1846 /* success print result */
1847 if (sf->interactive)
1848 sfdisk_print_partition(sf, cur_partno);
1849 next_partno = cur_partno + 1;
1850 } else if (pa) /* error, drop partition from script */
1851 fdisk_table_remove_partition(tb, pa);
1852 } else
1853 fdisk_info(sf->cxt, _("Script header accepted."));
1854
1855 if (rc && !sf->interactive) {
1856 rc = SFDISK_DONE_ABORT;
1857 break;
1858 }
1859 } while (1);
1860
1861 /* create empty disk label if label, but no partition specified */
1862 if ((rc == SFDISK_DONE_EOF || rc == SFDISK_DONE_WRITE) && created == 0
1863 && fdisk_script_has_force_label(dp) == 1
1864 && fdisk_table_get_nents(tb) == 0
1865 && fdisk_script_get_header(dp, "label")) {
1866
1867 int xrc = fdisk_apply_script_headers(sf->cxt, dp);
1868 created = !xrc;
1869 if (xrc) {
1870 fdisk_warnx(sf->cxt, _(
1871 "Failed to apply script headers, "
1872 "disk label not created."));
1873 rc = SFDISK_DONE_ABORT;
1874 }
1875 }
1876
1877 if (!sf->quiet && rc != SFDISK_DONE_ABORT) {
1878 fdisk_info(sf->cxt, _("\nNew situation:"));
1879 list_disk_identifier(sf->cxt);
1880 list_disklabel(sf->cxt);
1881 }
1882
1883 switch (rc) {
1884 case SFDISK_DONE_ASK:
1885 case SFDISK_DONE_EOF:
1886 if (sf->interactive) {
1887 int yes = 0;
1888 fdisk_ask_yesno(sf->cxt, _("Do you want to write this to disk?"), &yes);
1889 if (!yes) {
1890 fdisk_info(sf->cxt, _("Leaving."));
1891 rc = 0;
1892 break;
1893 }
1894 }
1895 /* fallthrough */
1896 case SFDISK_DONE_WRITE:
1897 rc = write_changes(sf);
1898 break;
1899 case SFDISK_DONE_ABORT:
1900 default: /* rc < 0 on error */
1901 fdisk_info(sf->cxt, _("Leaving.\n"));
1902 break;
1903 }
1904
1905 fdisk_unref_script(dp);
1906 return rc;
1907 }
1908
1909 static void __attribute__((__noreturn__)) usage(void)
1910 {
1911 FILE *out = stdout;
1912 fputs(USAGE_HEADER, out);
1913
1914 fprintf(out,
1915 _(" %1$s [options] <dev> [[-N] <part>]\n"
1916 " %1$s [options] <command>\n"), program_invocation_short_name);
1917
1918 fputs(USAGE_SEPARATOR, out);
1919 fputs(_("Display or manipulate a disk partition table.\n"), out);
1920
1921 fputs(USAGE_COMMANDS, out);
1922 fputs(_(" -A, --activate <dev> [<part> ...] list or set bootable (P)MBR partitions\n"), out);
1923 fputs(_(" -d, --dump <dev> dump partition table (usable for later input)\n"), out);
1924 fputs(_(" -J, --json <dev> dump partition table in JSON format\n"), out);
1925 fputs(_(" -g, --show-geometry [<dev> ...] list geometry of all or specified devices\n"), out);
1926 fputs(_(" -l, --list [<dev> ...] list partitions of each device\n"), out);
1927 fputs(_(" -F, --list-free [<dev> ...] list unpartitioned free areas of each device\n"), out);
1928 fputs(_(" -r, --reorder <dev> fix partitions order (by start offset)\n"), out);
1929 fputs(_(" -s, --show-size [<dev> ...] list sizes of all or specified devices\n"), out);
1930 fputs(_(" -T, --list-types print the recognized types (see -X)\n"), out);
1931 fputs(_(" -V, --verify [<dev> ...] test whether partitions seem correct\n"), out);
1932 fputs(_(" --delete <dev> [<part> ...] delete all or specified partitions\n"), out);
1933
1934 fputs(USAGE_SEPARATOR, out);
1935 fputs(_(" --part-label <dev> <part> [<str>] print or change partition label\n"), out);
1936 fputs(_(" --part-type <dev> <part> [<type>] print or change partition type\n"), out);
1937 fputs(_(" --part-uuid <dev> <part> [<uuid>] print or change partition uuid\n"), out);
1938 fputs(_(" --part-attrs <dev> <part> [<str>] print or change partition attributes\n"), out);
1939
1940 fputs(USAGE_SEPARATOR, out);
1941 fputs(_(" <dev> device (usually disk) path\n"), out);
1942 fputs(_(" <part> partition number\n"), out);
1943 fputs(_(" <type> partition type, GUID for GPT, hex for MBR\n"), out);
1944
1945 fputs(USAGE_OPTIONS, out);
1946 fputs(_(" -a, --append append partitions to existing partition table\n"), out);
1947 fputs(_(" -b, --backup backup partition table sectors (see -O)\n"), out);
1948 fputs(_(" --bytes print SIZE in bytes rather than in human readable format\n"), out);
1949 fputs(_(" --move-data[=<typescript>] move partition data after relocation (requires -N)\n"), out);
1950 fputs(_(" --move-use-fsync use fsync after each write when move data\n"), out);
1951 fputs(_(" -f, --force disable all consistency checking\n"), out);
1952 fprintf(out,
1953 _(" --color[=<when>] colorize output (%s, %s or %s)\n"), "auto", "always", "never");
1954 fprintf(out,
1955 " %s\n", USAGE_COLORS_DEFAULT);
1956 fputs(_(" -N, --partno <num> specify partition number\n"), out);
1957 fputs(_(" -n, --no-act do everything except write to device\n"), out);
1958 fputs(_(" --no-reread do not check whether the device is in use\n"), out);
1959 fputs(_(" --no-tell-kernel do not tell kernel about changes\n"), out);
1960 fputs(_(" -O, --backup-file <path> override default backup file name\n"), out);
1961 fputs(_(" -o, --output <list> output columns\n"), out);
1962 fputs(_(" -q, --quiet suppress extra info messages\n"), out);
1963 fprintf(out,
1964 _(" -w, --wipe <mode> wipe signatures (%s, %s or %s)\n"), "auto", "always", "never");
1965 fprintf(out,
1966 _(" -W, --wipe-partitions <mode> wipe signatures from new partitions (%s, %s or %s)\n"), "auto", "always", "never");
1967 fputs(_(" -X, --label <name> specify label type (dos, gpt, ...)\n"), out);
1968 fputs(_(" -Y, --label-nested <name> specify nested label type (dos, bsd)\n"), out);
1969 fputs(USAGE_SEPARATOR, out);
1970 fputs(_(" -G, --show-pt-geometry deprecated, alias to --show-geometry\n"), out);
1971 fputs(_(" -L, --Linux deprecated, only for backward compatibility\n"), out);
1972 fputs(_(" -u, --unit S deprecated, only sector unit is supported\n"), out);
1973
1974 fputs(USAGE_SEPARATOR, out);
1975 printf( " -h, --help %s\n", USAGE_OPTSTR_HELP);
1976 printf( " -v, --version %s\n", USAGE_OPTSTR_VERSION);
1977
1978 list_available_columns(out);
1979
1980 printf(USAGE_MAN_TAIL("sfdisk(8)"));
1981 exit(EXIT_SUCCESS);
1982 }
1983
1984
1985 int main(int argc, char *argv[])
1986 {
1987 const char *outarg = NULL;
1988 int rc = -EINVAL, c, longidx = -1, bytes = 0;
1989 int colormode = UL_COLORMODE_UNDEF;
1990 struct sfdisk _sf = {
1991 .partno = -1,
1992 .wipemode = WIPEMODE_AUTO,
1993 .pwipemode = WIPEMODE_AUTO,
1994 .interactive = isatty(STDIN_FILENO) ? 1 : 0,
1995 }, *sf = &_sf;
1996
1997 enum {
1998 OPT_CHANGE_ID = CHAR_MAX + 1,
1999 OPT_PRINT_ID,
2000 OPT_ID,
2001 OPT_NOREREAD,
2002 OPT_PARTUUID,
2003 OPT_PARTLABEL,
2004 OPT_PARTTYPE,
2005 OPT_PARTATTRS,
2006 OPT_BYTES,
2007 OPT_COLOR,
2008 OPT_MOVEDATA,
2009 OPT_MOVEFSYNC,
2010 OPT_DELETE,
2011 OPT_NOTELL
2012 };
2013
2014 static const struct option longopts[] = {
2015 { "activate",no_argument, NULL, 'A' },
2016 { "append", no_argument, NULL, 'a' },
2017 { "backup", no_argument, NULL, 'b' },
2018 { "backup-file", required_argument, NULL, 'O' },
2019 { "bytes", no_argument, NULL, OPT_BYTES },
2020 { "color", optional_argument, NULL, OPT_COLOR },
2021 { "delete", no_argument, NULL, OPT_DELETE },
2022 { "dump", no_argument, NULL, 'd' },
2023 { "help", no_argument, NULL, 'h' },
2024 { "force", no_argument, NULL, 'f' },
2025 { "json", no_argument, NULL, 'J' },
2026 { "label", required_argument, NULL, 'X' },
2027 { "label-nested", required_argument, NULL, 'Y' },
2028 { "list", no_argument, NULL, 'l' },
2029 { "list-free", no_argument, NULL, 'F' },
2030 { "list-types", no_argument, NULL, 'T' },
2031 { "no-act", no_argument, NULL, 'n' },
2032 { "no-reread", no_argument, NULL, OPT_NOREREAD },
2033 { "no-tell-kernel", no_argument, NULL, OPT_NOTELL },
2034 { "move-data", optional_argument, NULL, OPT_MOVEDATA },
2035 { "move-use-fsync", no_argument, NULL, OPT_MOVEFSYNC },
2036 { "output", required_argument, NULL, 'o' },
2037 { "partno", required_argument, NULL, 'N' },
2038 { "reorder", no_argument, NULL, 'r' },
2039 { "show-geometry", no_argument, NULL, 'g' },
2040 { "quiet", no_argument, NULL, 'q' },
2041 { "verify", no_argument, NULL, 'V' },
2042 { "version", no_argument, NULL, 'v' },
2043 { "wipe", required_argument, NULL, 'w' },
2044 { "wipe-partitions", required_argument, NULL, 'W' },
2045
2046 { "part-uuid", no_argument, NULL, OPT_PARTUUID },
2047 { "part-label", no_argument, NULL, OPT_PARTLABEL },
2048 { "part-type", no_argument, NULL, OPT_PARTTYPE },
2049 { "part-attrs", no_argument, NULL, OPT_PARTATTRS },
2050
2051 { "show-pt-geometry", no_argument, NULL, 'G' }, /* deprecated */
2052 { "unit", required_argument, NULL, 'u' }, /* deprecated */
2053 { "Linux", no_argument, NULL, 'L' }, /* deprecated */
2054 { "show-size", no_argument, NULL, 's' }, /* deprecated */
2055
2056 { "change-id",no_argument, NULL, OPT_CHANGE_ID }, /* deprecated */
2057 { "id", no_argument, NULL, 'c' }, /* deprecated */
2058 { "print-id",no_argument, NULL, OPT_PRINT_ID }, /* deprecated */
2059
2060 { NULL, 0, NULL, 0 },
2061 };
2062 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
2063 { 'F','J','d'}, /* --list-free --json --dump */
2064 { 's','u'}, /* --show-size --unit */
2065 { 0 }
2066 };
2067 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
2068
2069
2070 setlocale(LC_ALL, "");
2071 bindtextdomain(PACKAGE, LOCALEDIR);
2072 textdomain(PACKAGE);
2073 close_stdout_atexit();
2074
2075 while ((c = getopt_long(argc, argv, "aAbcdfFgGhJlLo:O:nN:qrsTu:vVX:Y:w:W:",
2076 longopts, &longidx)) != -1) {
2077
2078 err_exclusive_options(c, longopts, excl, excl_st);
2079
2080 switch(c) {
2081 case 'A':
2082 sf->act = ACT_ACTIVATE;
2083 break;
2084 case 'a':
2085 sf->append = 1;
2086 break;
2087 case 'b':
2088 sf->backup = 1;
2089 break;
2090 case OPT_CHANGE_ID:
2091 case OPT_PRINT_ID:
2092 case OPT_ID:
2093 warnx(_("%s is deprecated in favour of --part-type"),
2094 longopts[longidx].name);
2095 sf->act = ACT_PARTTYPE;
2096 break;
2097 case 'c':
2098 warnx(_("--id is deprecated in favour of --part-type"));
2099 sf->act = ACT_PARTTYPE;
2100 break;
2101 case 'J':
2102 sf->json = 1;
2103 /* fallthrough */
2104 case 'd':
2105 sf->act = ACT_DUMP;
2106 break;
2107 case 'F':
2108 sf->act = ACT_LIST_FREE;
2109 break;
2110 case 'f':
2111 sf->force = 1;
2112 break;
2113 case 'G':
2114 warnx(_("--show-pt-geometry is no more implemented. Using --show-geometry."));
2115 /* fallthrough */
2116 case 'g':
2117 sf->act = ACT_SHOW_GEOM;
2118 break;
2119 case 'h':
2120 usage();
2121 break;
2122 case 'l':
2123 sf->act = ACT_LIST;
2124 break;
2125 case 'L':
2126 warnx(_("--Linux option is unnecessary and deprecated"));
2127 break;
2128 case 'o':
2129 outarg = optarg;
2130 break;
2131 case 'O':
2132 sf->backup = 1;
2133 sf->backup_file = optarg;
2134 break;
2135 case 'n':
2136 sf->noact = 1;
2137 break;
2138 case 'N':
2139 sf->partno = strtou32_or_err(optarg, _("failed to parse partition number")) - 1;
2140 break;
2141 case 'q':
2142 sf->quiet = 1;
2143 break;
2144 case 'r':
2145 sf->act = ACT_REORDER;
2146 break;
2147 case 's':
2148 sf->act = ACT_SHOW_SIZE;
2149 break;
2150 case 'T':
2151 sf->act = ACT_LIST_TYPES;
2152 break;
2153 case 'u':
2154 if (*optarg != 'S')
2155 errx(EXIT_FAILURE, _("unsupported unit '%c'"), *optarg);
2156 break;
2157 case 'v':
2158 print_version(EXIT_SUCCESS);
2159 case 'V':
2160 sf->verify = 1;
2161 break;
2162 case 'w':
2163 sf->wipemode = wipemode_from_string(optarg);
2164 if (sf->wipemode < 0)
2165 errx(EXIT_FAILURE, _("unsupported wipe mode"));
2166 break;
2167 case 'W':
2168 sf->pwipemode = wipemode_from_string(optarg);
2169 if (sf->pwipemode < 0)
2170 errx(EXIT_FAILURE, _("unsupported wipe mode"));
2171 break;
2172 case 'X':
2173 sf->label = optarg;
2174 break;
2175 case 'Y':
2176 sf->label_nested = optarg;
2177 break;
2178
2179 case OPT_PARTUUID:
2180 sf->act = ACT_PARTUUID;
2181 break;
2182 case OPT_PARTTYPE:
2183 sf->act = ACT_PARTTYPE;
2184 break;
2185 case OPT_PARTLABEL:
2186 sf->act = ACT_PARTLABEL;
2187 break;
2188 case OPT_PARTATTRS:
2189 sf->act = ACT_PARTATTRS;
2190 break;
2191 case OPT_NOREREAD:
2192 sf->noreread = 1;
2193 break;
2194 case OPT_BYTES:
2195 bytes = 1;
2196 break;
2197 case OPT_COLOR:
2198 colormode = UL_COLORMODE_AUTO;
2199 if (optarg)
2200 colormode = colormode_or_err(optarg,
2201 _("unsupported color mode"));
2202 break;
2203 case OPT_MOVEDATA:
2204 sf->movedata = 1;
2205 sf->move_typescript = optarg;
2206 break;
2207 case OPT_MOVEFSYNC:
2208 sf->movefsync = 1;
2209 break;
2210 case OPT_DELETE:
2211 sf->act = ACT_DELETE;
2212 break;
2213 case OPT_NOTELL:
2214 sf->notell = 1;
2215 break;
2216 default:
2217 errtryhelp(EXIT_FAILURE);
2218 }
2219 }
2220
2221 colors_init(colormode, "sfdisk");
2222
2223 sfdisk_init(sf);
2224 if (bytes)
2225 fdisk_set_size_unit(sf->cxt, FDISK_SIZEUNIT_BYTES);
2226
2227 if (outarg)
2228 init_fields(NULL, outarg, NULL);
2229
2230 if (sf->verify && !sf->act)
2231 sf->act = ACT_VERIFY; /* --verify make be used with --list too */
2232 else if (!sf->act)
2233 sf->act = ACT_FDISK; /* default */
2234
2235 if (sf->movedata && !(sf->act == ACT_FDISK && sf->partno >= 0))
2236 errx(EXIT_FAILURE, _("--movedata requires -N"));
2237
2238 switch (sf->act) {
2239 case ACT_ACTIVATE:
2240 rc = command_activate(sf, argc - optind, argv + optind);
2241 break;
2242
2243 case ACT_DELETE:
2244 rc = command_delete(sf, argc - optind, argv + optind);
2245 break;
2246
2247 case ACT_LIST:
2248 rc = command_list_partitions(sf, argc - optind, argv + optind);
2249 break;
2250
2251 case ACT_LIST_TYPES:
2252 rc = command_list_types(sf);
2253 break;
2254
2255 case ACT_LIST_FREE:
2256 rc = command_list_freespace(sf, argc - optind, argv + optind);
2257 break;
2258
2259 case ACT_FDISK:
2260 rc = command_fdisk(sf, argc - optind, argv + optind);
2261 break;
2262
2263 case ACT_DUMP:
2264 rc = command_dump(sf, argc - optind, argv + optind);
2265 break;
2266
2267 case ACT_SHOW_SIZE:
2268 rc = command_show_size(sf, argc - optind, argv + optind);
2269 break;
2270
2271 case ACT_SHOW_GEOM:
2272 rc = command_show_geometry(sf, argc - optind, argv + optind);
2273 break;
2274
2275 case ACT_VERIFY:
2276 rc = command_verify(sf, argc - optind, argv + optind);
2277 break;
2278
2279 case ACT_PARTTYPE:
2280 rc = command_parttype(sf, argc - optind, argv + optind);
2281 break;
2282
2283 case ACT_PARTUUID:
2284 rc = command_partuuid(sf, argc - optind, argv + optind);
2285 break;
2286
2287 case ACT_PARTLABEL:
2288 rc = command_partlabel(sf, argc - optind, argv + optind);
2289 break;
2290
2291 case ACT_PARTATTRS:
2292 rc = command_partattrs(sf, argc - optind, argv + optind);
2293 break;
2294
2295 case ACT_REORDER:
2296 rc = command_reorder(sf, argc - optind, argv + optind);
2297 break;
2298 }
2299
2300 sfdisk_deinit(sf);
2301
2302 DBG(MISC, ul_debug("bye! [rc=%d]", rc));
2303 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
2304 }
2305