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