2 * Copyright (C) 1995 Andries E. Brouwer (aeb@cwi.nl)
3 * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
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.
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.
20 * Karel Zak wrote new sfdisk based on libfdisk from util-linux
34 #include <libsmartcols.h>
35 #ifdef HAVE_LIBREADLINE
36 # define _FUNCTION_DEF
37 # include <readline/readline.h>
47 #include "closestream.h"
56 #include "fdisk-list.h"
59 * sfdisk debug stuff (see fdisk.h and include/debug.h)
61 static UL_DEBUG_DEFINE_MASK(sfdisk
);
62 UL_DEBUG_DEFINE_MASKNAMES(sfdisk
) = UL_DEBUG_EMPTY_MASKNAMES
;
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
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)
97 int partno
; /* -N <partno>, default -1 */
98 int wipemode
; /* remove foreign signatures from disk */
99 int pwipemode
; /* remove foreign signatures from partitions */
100 const char *lockmode
; /* as specified by --lock */
101 const char *label
; /* --label <label> */
102 const char *label_nested
; /* --label-nested <label> */
103 const char *backup_file
; /* -O <path> */
104 const char *move_typescript
; /* --movedata <typescript> */
107 struct fdisk_context
*cxt
; /* libfdisk context */
108 struct fdisk_partition
*orig_pa
; /* -N <partno> before the change */
110 unsigned int verify
: 1, /* call fdisk_verify_disklabel() */
111 quiet
: 1, /* suppress extra messages */
112 interactive
: 1, /* running on tty */
113 noreread
: 1, /* don't check device is in use */
114 force
: 1, /* do also stupid things */
115 backup
: 1, /* backup sectors before write PT */
116 container
: 1, /* PT contains container (MBR extended) partitions */
117 unused
: 1, /* PT contains unused partition */
118 append
: 1, /* don't create new PT, append partitions only */
119 json
: 1, /* JSON dump */
120 movedata
: 1, /* move data after resize */
121 movefsync
: 1, /* use fsync() after each write() */
122 notell
: 1, /* don't tell kernel aout new PT */
123 noact
: 1; /* do not write to device */
126 #define SFDISK_PROMPT ">>> "
128 static void sfdiskprog_init_debug(void)
130 __UL_INIT_DEBUG_FROM_ENV(sfdisk
, SFDISKPROG_DEBUG_
, 0, SFDISK_DEBUG
);
134 static int get_user_reply(const char *prompt
, char *buf
, size_t bufsz
)
139 #ifdef HAVE_LIBREADLINE
140 if (isatty(STDIN_FILENO
)) {
141 p
= readline(prompt
);
144 xstrncpy(buf
, p
, bufsz
);
149 fputs(prompt
, stdout
);
152 if (!fgets(buf
, bufsz
, stdin
))
156 for (p
= buf
; *p
&& !isgraph(*p
); p
++); /* get first non-blank */
159 memmove(buf
, p
, p
- buf
); /* remove blank space */
161 if (sz
&& *(buf
+ sz
- 1) == '\n')
162 *(buf
+ sz
- 1) = '\0';
164 DBG(ASK
, ul_debug("user's reply: >>>%s<<<", buf
));
168 static int ask_callback(struct fdisk_context
*cxt
__attribute__((__unused__
)),
169 struct fdisk_ask
*ask
,
172 struct sfdisk
*sf
= (struct sfdisk
*) data
;
177 switch(fdisk_ask_get_type(ask
)) {
178 case FDISK_ASKTYPE_INFO
:
181 fputs(fdisk_ask_print_get_mesg(ask
), stdout
);
184 case FDISK_ASKTYPE_WARNX
:
186 color_scheme_fenable("warn", UL_COLOR_RED
, stderr
);
187 fputs(fdisk_ask_print_get_mesg(ask
), stderr
);
188 color_fdisable(stderr
);
191 case FDISK_ASKTYPE_WARN
:
193 color_scheme_fenable("warn", UL_COLOR_RED
, stderr
);
194 fputs(fdisk_ask_print_get_mesg(ask
), stderr
);
195 errno
= fdisk_ask_print_get_errno(ask
);
196 fprintf(stderr
, ": %m\n");
197 color_fdisable(stderr
);
199 case FDISK_ASKTYPE_YESNO
:
201 char buf
[BUFSIZ
] = { '\0' };
205 fputs(fdisk_ask_get_query(ask
), stdout
);
206 rc
= get_user_reply(_(" [Y]es/[N]o: "), buf
, sizeof(buf
));
210 if (x
== RPMATCH_YES
|| x
== RPMATCH_NO
) {
211 fdisk_ask_yesno_set_result(ask
, x
);
215 DBG(ASK
, ul_debug("yes-no ask: reply '%s' [rc=%d]", buf
, rc
));
224 static void sfdisk_init(struct sfdisk
*sf
)
228 sfdiskprog_init_debug();
230 sf
->cxt
= fdisk_new_context();
232 err(EXIT_FAILURE
, _("failed to allocate libfdisk context"));
233 fdisk_set_ask(sf
->cxt
, ask_callback
, (void *) sf
);
235 if (sf
->wipemode
!= WIPEMODE_ALWAYS
)
236 fdisk_enable_bootbits_protection(sf
->cxt
, 1);
238 if (sf
->label_nested
) {
239 struct fdisk_context
*x
= fdisk_new_nested_context(sf
->cxt
,
242 err(EXIT_FAILURE
, _("failed to allocate nested libfdisk context"));
243 /* the original context is available by fdisk_get_parent() */
248 static int sfdisk_deinit(struct sfdisk
*sf
)
250 struct fdisk_context
*parent
;
255 parent
= fdisk_get_parent(sf
->cxt
);
257 fdisk_unref_context(sf
->cxt
);
261 fdisk_unref_context(sf
->cxt
);
264 memset(sf
, 0, sizeof(*sf
));
268 static struct fdisk_partition
*get_partition(struct fdisk_context
*cxt
, size_t partno
)
270 struct fdisk_table
*tb
= NULL
;
271 struct fdisk_partition
*pa
;
273 if (fdisk_get_partitions(cxt
, &tb
) != 0)
276 pa
= fdisk_table_get_partition_by_partno(tb
, partno
);
278 fdisk_ref_partition(pa
);
279 fdisk_unref_table(tb
);
283 static void backup_sectors(struct sfdisk
*sf
,
287 uint64_t offset
, size_t size
)
292 devfd
= fdisk_get_devfd(sf
->cxt
);
295 xasprintf(&fname
, "%s0x%08"PRIx64
".bak", tpl
, offset
);
297 fd
= open(fname
, O_CREAT
| O_WRONLY
, S_IRUSR
| S_IWUSR
);
301 if (lseek(devfd
, (off_t
) offset
, SEEK_SET
) == (off_t
) -1) {
302 fdisk_warn(sf
->cxt
, _("cannot seek %s"), devname
);
305 unsigned char *buf
= xmalloc(size
);
307 if (read_all(devfd
, (char *) buf
, size
) != (ssize_t
) size
) {
308 fdisk_warn(sf
->cxt
, _("cannot read %s"), devname
);
312 if (write_all(fd
, buf
, size
) != 0) {
313 fdisk_warn(sf
->cxt
, _("cannot write %s"), fname
);
320 fdisk_info(sf
->cxt
, _("%12s (offset %5ju, size %5ju): %s"),
321 name
, (uintmax_t) offset
, (uintmax_t) size
, fname
);
326 errx(EXIT_FAILURE
, _("%s: failed to create a backup"), devname
);
329 static char *mk_backup_filename_tpl(const char *filename
, const char *devname
, const char *suffix
)
332 char *name
, *buf
= xstrdup(devname
);
334 name
= basename(buf
);
336 if (!filename
|| strcmp(filename
, "@default") == 0) {
337 const char *home
= getenv ("HOME");
339 errx(EXIT_FAILURE
, _("failed to create a backup file, $HOME undefined"));
340 xasprintf(&tpl
, "%s/sfdisk-%s%s", home
, name
, suffix
);
342 xasprintf(&tpl
, "%s-%s%s", filename
, name
, suffix
);
349 static void backup_partition_table(struct sfdisk
*sf
, const char *devname
)
359 if (!fdisk_has_label(sf
->cxt
))
362 tpl
= mk_backup_filename_tpl(sf
->backup_file
, devname
, "-");
364 color_scheme_enable("header", UL_COLOR_BOLD
);
365 fdisk_info(sf
->cxt
, _("Backup files:"));
368 while (fdisk_locate_disklabel(sf
->cxt
, i
++, &name
, &offset
, &size
) == 0 && size
)
369 backup_sectors(sf
, tpl
, name
, devname
, offset
, size
);
376 static int assign_device(struct sfdisk
*sf
, const char *devname
, int rdonly
)
378 struct fdisk_context
*cxt
= sf
->cxt
;
380 if (fdisk_assign_device(cxt
, devname
, rdonly
) != 0)
381 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
383 if (!fdisk_is_readonly(cxt
)) {
384 if (blkdev_lock(fdisk_get_devfd(cxt
), devname
, sf
->lockmode
) != 0) {
385 fdisk_deassign_device(cxt
, 1);
389 backup_partition_table(sf
, devname
);
395 static int move_partition_data(struct sfdisk
*sf
, size_t partno
, struct fdisk_partition
*orig_pa
)
397 struct fdisk_partition
*pa
= get_partition(sf
->cxt
, partno
);
398 char *devname
= NULL
, *typescript
= NULL
, *buf
= NULL
;
400 int ok
= 0, fd
, backward
= 0;
401 fdisk_sector_t nsectors
, from
, to
, step
, i
, prev
;
402 size_t io
, ss
, step_bytes
, cc
, ioerr
= 0;
403 uintmax_t src
, dst
, nbytes
;
404 int progress
= 0, rc
= 0;
405 struct timeval prev_time
;
406 uint64_t bytes_per_sec
= 0;
408 assert(sf
->movedata
);
411 warnx(_("failed to read new partition from device; ignoring --move-data"));
412 else if (!fdisk_partition_has_size(pa
))
413 warnx(_("failed to get size of the new partition; ignoring --move-data"));
414 else if (!fdisk_partition_has_start(pa
))
415 warnx(_("failed to get start of the new partition; ignoring --move-data"));
416 else if (!fdisk_partition_has_size(orig_pa
))
417 warnx(_("failed to get size of the old partition; ignoring --move-data"));
418 else if (!fdisk_partition_has_start(orig_pa
))
419 warnx(_("failed to get start of the old partition; ignoring --move-data"));
420 else if (fdisk_partition_get_start(pa
) == fdisk_partition_get_start(orig_pa
))
421 warnx(_("start of the partition has not been moved; ignoring --move-data"));
422 else if (fdisk_partition_get_size(orig_pa
) > fdisk_partition_get_size(pa
))
423 warnx(_("new partition is smaller than original; ignoring --move-data"));
429 DBG(MISC
, ul_debug("moving data"));
431 fd
= fdisk_get_devfd(sf
->cxt
);
433 /* set move direction and overlay */
434 nsectors
= fdisk_partition_get_size(orig_pa
);
435 from
= fdisk_partition_get_start(orig_pa
);
436 to
= fdisk_partition_get_start(pa
);
438 if ((to
>= from
&& from
+ nsectors
>= to
) ||
439 (from
>= to
&& to
+ nsectors
>= from
)) {
440 /* source and target overlay, check if we need to copy
441 * backwardly from end of the source */
442 DBG(MISC
, ul_debug("overlay between source and target"));
443 backward
= from
< to
;
444 DBG(MISC
, ul_debug(" copy order: %s", backward
? "backward" : "forward"));
447 /* set optimal step size -- nearest to 1MiB aligned to optimal I/O */
448 io
= fdisk_get_optimal_iosize(sf
->cxt
);
449 ss
= fdisk_get_sector_size(sf
->cxt
);
452 if (io
< 1024 * 1024)
453 step_bytes
= ((1024 * 1024) + io
/2) / io
* io
;
457 step
= step_bytes
/ ss
;
458 nbytes
= nsectors
* ss
;
460 DBG(MISC
, ul_debug(" step: %ju (%zu bytes)", (uintmax_t)step
, step_bytes
));
462 #if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
464 ignore_result( posix_fadvise(fd
, from
* ss
,
465 nsectors
* ss
, POSIX_FADV_SEQUENTIAL
) );
467 devname
= fdisk_partname(fdisk_get_devname(sf
->cxt
), partno
+1);
468 if (sf
->move_typescript
)
469 typescript
= mk_backup_filename_tpl(sf
->move_typescript
, devname
, ".move");
472 fdisk_info(sf
->cxt
, "%s", "");
473 color_scheme_enable("header", UL_COLOR_BOLD
);
474 fdisk_info(sf
->cxt
, sf
->noact
? _("Data move: (--no-act)") : _("Data move:"));
477 fdisk_info(sf
->cxt
, _(" typescript file: %s"), typescript
);
478 printf(_(" start sector: (from/to) %ju / %ju\n"), (uintmax_t) from
, (uintmax_t) to
);
479 printf(_(" sectors: %ju\n"), (uintmax_t) nsectors
);
480 printf(_(" step size: %zu bytes\n"), step_bytes
);
484 if (isatty(fileno(stdout
)))
488 if (sf
->interactive
) {
490 fdisk_ask_yesno(sf
->cxt
, _("Do you want to move partition data?"), &yes
);
492 fdisk_info(sf
->cxt
, _("Leaving."));
499 f
= fopen(typescript
, "w");
502 fdisk_warn(sf
->cxt
, _("cannot open %s"), typescript
);
506 /* don't translate */
507 fprintf(f
, "# sfdisk: " PACKAGE_STRING
"\n");
508 fprintf(f
, "# Disk: %s\n", devname
);
509 fprintf(f
, "# Partition: %zu\n", partno
+ 1);
510 fprintf(f
, "# Operation: move data\n");
511 fprintf(f
, "# Sector size: %zu\n", ss
);
512 fprintf(f
, "# Original start offset (sectors/bytes): %ju/%ju\n",
513 (uintmax_t)from
, (uintmax_t)from
* ss
);
514 fprintf(f
, "# New start offset (sectors/bytes): %ju/%ju\n",
515 (uintmax_t)to
, (uintmax_t)to
* ss
);
516 fprintf(f
, "# Area size (sectors/bytes): %ju/%ju\n",
517 (uintmax_t)nsectors
, (uintmax_t)nsectors
* ss
);
518 fprintf(f
, "# Step size (sectors/bytes): %" PRIu64
"/%zu\n", step
, step_bytes
);
519 fprintf(f
, "# Steps: %ju\n", ((uintmax_t) nsectors
/ step
) + 1);
521 fprintf(f
, "# <step>: <from> <to> (step offsets in bytes)\n");
524 src
= (backward
? from
+ nsectors
: from
) * ss
;
525 dst
= (backward
? to
+ nsectors
: to
) * ss
;
526 buf
= xmalloc(step_bytes
);
528 DBG(MISC
, ul_debug(" initial: src=%ju dst=%ju", src
, dst
));
530 gettimeofday(&prev_time
, NULL
);
533 for (cc
= 1, i
= 0; i
< nsectors
&& nbytes
> 0; i
+= step
, cc
++) {
535 if (nbytes
< step_bytes
) {
536 DBG(MISC
, ul_debug("aligning step #%05zu from %zu to %ju",
537 cc
, step_bytes
, nbytes
));
540 nbytes
-= step_bytes
;
543 src
-= step_bytes
, dst
-= step_bytes
;
545 DBG(MISC
, ul_debug("#%05zu: src=%ju dst=%ju", cc
, src
, dst
));
549 if (lseek(fd
, src
, SEEK_SET
) == (off_t
) -1 ||
550 read_all(fd
, buf
, step_bytes
) != (ssize_t
) step_bytes
) {
552 fprintf(f
, "%05zu: read error %12ju %12ju\n", cc
, src
, dst
);
554 _("cannot read at offset: %ju; continue"), src
);
560 if (lseek(fd
, dst
, SEEK_SET
) == (off_t
) -1 ||
561 write_all(fd
, buf
, step_bytes
) != 0) {
563 fprintf(f
, "%05zu: write error %12ju %12ju\n", cc
, src
, dst
);
565 _("cannot write at offset: %ju; continue"), dst
);
569 if (sf
->movefsync
&& fsync(fd
) != 0)
571 _("cannot fsync at offset: %ju; continue"), dst
);
576 fprintf(f
, "%05zu: %12ju %12ju\n", cc
, src
, dst
);
578 if (progress
&& i
% 10 == 0) {
579 unsigned int elapsed
= 0; /* usec */
580 struct timeval cur_time
;
582 gettimeofday(&cur_time
, NULL
);
583 if (cur_time
.tv_sec
- prev_time
.tv_sec
> 1) {
584 elapsed
= ((cur_time
.tv_sec
- prev_time
.tv_sec
) * 1000000) +
585 (cur_time
.tv_usec
- prev_time
.tv_usec
);
587 bytes_per_sec
= ((i
- prev
) * ss
) / elapsed
; /* per usec */
588 bytes_per_sec
*= 1000000; /* per sec */
590 prev_time
= cur_time
;
595 fprintf(stdout
, _("Moved %ju from %ju sectors (%.3f%%, %.1f MiB/s)."),
597 100.0 / ((double) nsectors
/(i
+1)),
598 (double) (bytes_per_sec
/ (1024 * 1024)));
600 fprintf(stdout
, _("Moved %ju from %ju sectors (%.3f%%)."),
602 100.0 / ((double) nsectors
/(i
+1)));
609 src
+= step_bytes
, dst
+= step_bytes
;
612 if (progress
&& nsectors
) {
613 int x
= get_terminal_width(80);
620 /* see for() above; @i has to be greater than @nsectors
621 * on success due to i += step */
624 fprintf(stdout
, _("Moved %ju from %ju sectors (%.0f%%)."),
626 100.0 / ((double) nsectors
/(i
+1)));
637 fdisk_info(sf
->cxt
, _("Your data has not been moved (--no-act)."));
639 fdisk_info(sf
->cxt
, _("%zu I/O errors detected!"), ioerr
);
642 warn(_("%s: failed to move data"), devname
);
649 static int write_changes(struct sfdisk
*sf
)
654 fdisk_info(sf
->cxt
, _("The partition table is unchanged (--no-act)."));
656 rc
= fdisk_write_disklabel(sf
->cxt
);
658 if (rc
== 0 && sf
->movedata
&& sf
->orig_pa
)
659 rc
= move_partition_data(sf
, sf
->partno
, sf
->orig_pa
);
661 if (!sf
->noact
&& !rc
) {
662 fdisk_info(sf
->cxt
, _("\nThe partition table has been altered."));
664 /* Let's wait a little bit. It's possible that our
665 * system is still busy with a previous re-read
666 * ioctl (on sfdisk start) or with another task
667 * related to the write to the device.
670 fdisk_reread_partition_table(sf
->cxt
);
675 rc
= fdisk_deassign_device(sf
->cxt
,
676 sf
->noact
|| sf
->notell
); /* no-sync */
681 * sfdisk --list [<device ..]
683 static int command_list_partitions(struct sfdisk
*sf
, int argc
, char **argv
)
686 fdisk_enable_listonly(sf
->cxt
, 1);
691 for (i
= 0; i
< argc
; i
++)
692 if (print_device_pt(sf
->cxt
, argv
[i
], 1, sf
->verify
, i
) != 0)
695 print_all_devices_pt(sf
->cxt
, sf
->verify
);
701 * sfdisk --list-free [<device ..]
703 static int command_list_freespace(struct sfdisk
*sf
, int argc
, char **argv
)
706 fdisk_enable_listonly(sf
->cxt
, 1);
711 for (i
= 0; i
< argc
; i
++)
712 if (print_device_freespace(sf
->cxt
, argv
[i
], 1, i
) != 0)
715 print_all_devices_freespace(sf
->cxt
);
721 * sfdisk --list-types
723 static int command_list_types(struct sfdisk
*sf
)
725 const struct fdisk_parttype
*t
;
726 struct fdisk_label
*lb
;
734 name
= sf
->label
? sf
->label
: "dos";
735 lb
= fdisk_get_label(sf
->cxt
, name
);
737 errx(EXIT_FAILURE
, _("unsupported label '%s'"), name
);
739 codes
= fdisk_label_has_code_parttypes(lb
);
740 fputs(_("Id Name\n\n"), stdout
);
742 while ((t
= fdisk_label_get_parttype(lb
, i
++))) {
744 printf("%2x %s\n", fdisk_parttype_get_code(t
),
745 fdisk_parttype_get_name(t
));
747 printf("%s %s\n", fdisk_parttype_get_string(t
),
748 fdisk_parttype_get_name(t
));
754 static int verify_device(struct sfdisk
*sf
, const char *devname
)
758 fdisk_enable_listonly(sf
->cxt
, 1);
760 assign_device(sf
, devname
, 1);
762 color_scheme_enable("header", UL_COLOR_BOLD
);
763 fdisk_info(sf
->cxt
, "%s:", devname
);
766 if (!fdisk_has_label(sf
->cxt
))
767 fdisk_info(sf
->cxt
, _("unrecognized partition table type"));
769 rc
= fdisk_verify_disklabel(sf
->cxt
);
771 fdisk_deassign_device(sf
->cxt
, 1);
776 * sfdisk --verify [<device ..]
778 static int command_verify(struct sfdisk
*sf
, int argc
, char **argv
)
780 int nfails
= 0, ct
= 0;
784 for (i
= 0; i
< argc
; i
++) {
786 fdisk_info(sf
->cxt
, " ");
787 if (verify_device(sf
, argv
[i
]) < 0)
794 while ((dev
= next_proc_partition(&f
))) {
796 fdisk_info(sf
->cxt
, " ");
797 if (verify_device(sf
, dev
) < 0)
807 static int get_size(const char *dev
, int silent
, uintmax_t *sz
)
811 fd
= open(dev
, O_RDONLY
);
814 warn(_("cannot open %s"), dev
);
818 if (blkdev_get_sectors(fd
, (unsigned long long *) sz
) == -1) {
820 warn(_("Cannot get size of %s"), dev
);
829 * sfdisk --show-size [<device ..]
831 * (silly, but just for backward compatibility)
833 static int command_show_size(struct sfdisk
*sf
__attribute__((__unused__
)),
834 int argc
, char **argv
)
840 for (i
= 0; i
< argc
; i
++) {
841 if (get_size(argv
[i
], 0, &sz
) == 0)
842 printf("%ju\n", sz
/ 2);
849 while ((dev
= next_proc_partition(&f
))) {
850 if (get_size(dev
, 1, &sz
) == 0) {
851 printf("%s: %9ju\n", dev
, sz
/ 2);
857 printf(_("total: %ju blocks\n"), total
);
863 static int print_geom(struct sfdisk
*sf
, const char *devname
)
865 fdisk_enable_listonly(sf
->cxt
, 1);
867 assign_device(sf
, devname
, 1);
869 fdisk_info(sf
->cxt
, "%s: %ju cylinders, %ju heads, %ju sectors/track",
871 (uintmax_t) fdisk_get_geom_cylinders(sf
->cxt
),
872 (uintmax_t) fdisk_get_geom_heads(sf
->cxt
),
873 (uintmax_t) fdisk_get_geom_sectors(sf
->cxt
));
875 fdisk_deassign_device(sf
->cxt
, 1);
880 * sfdisk --show-geometry [<device ..]
882 static int command_show_geometry(struct sfdisk
*sf
, int argc
, char **argv
)
888 for (i
= 0; i
< argc
; i
++) {
889 if (print_geom(sf
, argv
[i
]) < 0)
896 while ((dev
= next_proc_partition(&f
))) {
897 if (print_geom(sf
, dev
) < 0)
907 * sfdisk --activate <device> [<partno> ...]
909 static int command_activate(struct sfdisk
*sf
, int argc
, char **argv
)
911 int rc
, nparts
, i
, listonly
;
912 struct fdisk_partition
*pa
= NULL
;
913 const char *devname
= NULL
;
916 errx(EXIT_FAILURE
, _("no disk device specified"));
919 /* --activate <device> */
920 listonly
= argc
== 1;
922 assign_device(sf
, devname
, listonly
);
924 if (fdisk_is_label(sf
->cxt
, GPT
)) {
925 if (fdisk_gpt_is_hybrid(sf
->cxt
))
926 errx(EXIT_FAILURE
, _("toggle boot flags is unsupported for Hybrid GPT/MBR"));
928 /* Switch from GPT to PMBR */
929 sf
->cxt
= fdisk_new_nested_context(sf
->cxt
, "dos");
931 err(EXIT_FAILURE
, _("cannot switch to PMBR"));
932 fdisk_info(sf
->cxt
, _("Activation is unsupported for GPT -- entering nested PMBR."));
934 } else if (!fdisk_is_label(sf
->cxt
, DOS
))
935 errx(EXIT_FAILURE
, _("toggle boot flags is supported for MBR or PMBR only"));
937 nparts
= fdisk_get_npartitions(sf
->cxt
);
938 for (i
= 0; i
< nparts
; i
++) {
941 /* note that fdisk_get_partition() reuses the @pa pointer, you
942 * don't have to (re)allocate it */
943 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0)
946 /* sfdisk --activate list bootable partitions */
948 if (!fdisk_partition_is_bootable(pa
))
950 if (fdisk_partition_to_string(pa
, sf
->cxt
,
951 FDISK_FIELD_DEVICE
, &data
) == 0) {
952 printf("%s\n", data
);
956 /* deactivate all active partitions */
957 } else if (fdisk_partition_is_bootable(pa
))
958 fdisk_toggle_partition_flag(sf
->cxt
, i
, DOS_FLAG_ACTIVE
);
961 /* sfdisk --activate <partno> [..] */
962 for (i
= 1; i
< argc
; i
++) {
965 if (i
== 1 && strcmp(argv
[1], "-") == 0)
967 n
= strtou32_or_err(argv
[i
], _("failed to parse partition number"));
969 rc
= fdisk_toggle_partition_flag(sf
->cxt
, n
- 1, DOS_FLAG_ACTIVE
);
972 _("%s: partition %d: failed to toggle bootable flag"),
976 fdisk_unref_partition(pa
);
979 rc
= fdisk_deassign_device(sf
->cxt
, 1);
981 rc
= write_changes(sf
);
986 * sfdisk --delete <device> [<partno> ...]
988 static int command_delete(struct sfdisk
*sf
, int argc
, char **argv
)
991 const char *devname
= NULL
;
994 errx(EXIT_FAILURE
, _("no disk device specified"));
997 assign_device(sf
, devname
, 0);
1001 size_t nparts
= fdisk_get_npartitions(sf
->cxt
);
1002 for (i
= 0; i
< nparts
; i
++) {
1003 if (fdisk_is_partition_used(sf
->cxt
, i
) &&
1004 fdisk_delete_partition(sf
->cxt
, i
) != 0)
1005 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to delete"), devname
, i
+ 1);
1007 /* delete specified */
1009 for (i
= 1; i
< (size_t) argc
; i
++) {
1010 size_t n
= strtou32_or_err(argv
[i
], _("failed to parse partition number"));
1012 if (fdisk_delete_partition(sf
->cxt
, n
- 1) != 0)
1013 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to delete"), devname
, n
);
1017 return write_changes(sf
);
1021 * sfdisk --reorder <device>
1023 static int command_reorder(struct sfdisk
*sf
, int argc
, char **argv
)
1025 const char *devname
= NULL
;
1031 errx(EXIT_FAILURE
, _("no disk device specified"));
1033 assign_device(sf
, devname
, 0); /* read-write */
1035 if (fdisk_reorder_partitions(sf
->cxt
) == 1) /* unchanged */
1036 rc
= fdisk_deassign_device(sf
->cxt
, 1);
1038 rc
= write_changes(sf
);
1045 * sfdisk --dump <device>
1047 static int command_dump(struct sfdisk
*sf
, int argc
, char **argv
)
1049 const char *devname
= NULL
;
1050 struct fdisk_script
*dp
;
1056 errx(EXIT_FAILURE
, _("no disk device specified"));
1058 assign_device(sf
, devname
, 1); /* read-only */
1060 if (!fdisk_has_label(sf
->cxt
))
1061 errx(EXIT_FAILURE
, _("%s: does not contain a recognized partition table"), devname
);
1063 dp
= fdisk_new_script(sf
->cxt
);
1065 err(EXIT_FAILURE
, _("failed to allocate dump struct"));
1067 rc
= fdisk_script_read_context(dp
, NULL
);
1069 errx(EXIT_FAILURE
, _("%s: failed to dump partition table"), devname
);
1072 fdisk_script_enable_json(dp
, 1);
1073 fdisk_script_write_file(dp
, stdout
);
1075 fdisk_unref_script(dp
);
1076 fdisk_deassign_device(sf
->cxt
, 1); /* no-sync() */
1081 * sfdisk --backup-pt-sectors <device>
1083 static int command_backup_sectors(struct sfdisk
*sf
, int argc
, char **argv
)
1085 const char *devname
= NULL
;
1090 errx(EXIT_FAILURE
, _("no disk device specified"));
1092 assign_device(sf
, devname
, 1); /* read-only */
1094 if (!fdisk_has_label(sf
->cxt
))
1095 errx(EXIT_FAILURE
, _("%s: does not contain a recognized partition table"), devname
);
1097 backup_partition_table(sf
, devname
);
1099 fdisk_deassign_device(sf
->cxt
, 1); /* no-sync() */
1103 static void assign_device_partition(struct sfdisk
*sf
,
1104 const char *devname
,
1110 struct fdisk_label
*lb
= NULL
;
1115 /* read-only when a new <type> undefined */
1116 rc
= fdisk_assign_device(sf
->cxt
, devname
, rdonly
);
1118 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
1120 if (!fdisk_is_readonly(sf
->cxt
)
1121 && blkdev_lock(fdisk_get_devfd(sf
->cxt
), devname
, sf
->lockmode
) != 0) {
1122 fdisk_deassign_device(sf
->cxt
, 1);
1125 lb
= fdisk_get_label(sf
->cxt
, NULL
);
1127 errx(EXIT_FAILURE
, _("%s: no partition table found"), devname
);
1129 n
= fdisk_get_npartitions(sf
->cxt
);
1131 errx(EXIT_FAILURE
, _("%s: partition %zu: partition table contains "
1132 "only %zu partitions"), devname
, partno
, n
);
1133 if (!fdisk_is_partition_used(sf
->cxt
, partno
- 1))
1134 errx(EXIT_FAILURE
, _("%s: partition %zu: partition is unused"),
1139 * sfdisk --part-type <device> <partno> [<type>]
1141 static int command_parttype(struct sfdisk
*sf
, int argc
, char **argv
)
1144 struct fdisk_parttype
*type
= NULL
;
1145 struct fdisk_label
*lb
;
1146 const char *devname
= NULL
, *typestr
= NULL
;
1149 errx(EXIT_FAILURE
, _("no disk device specified"));
1153 errx(EXIT_FAILURE
, _("no partition number specified"));
1154 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1159 errx(EXIT_FAILURE
, _("unexpected arguments"));
1161 /* read-only when a new <type> undefined */
1162 assign_device_partition(sf
, devname
, partno
, !typestr
);
1164 lb
= fdisk_get_label(sf
->cxt
, NULL
);
1166 /* print partition type */
1168 const struct fdisk_parttype
*t
= NULL
;
1169 struct fdisk_partition
*pa
= NULL
;
1171 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1172 t
= fdisk_partition_get_type(pa
);
1174 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition type"),
1177 if (fdisk_label_has_code_parttypes(lb
))
1178 printf("%2x\n", fdisk_parttype_get_code(t
));
1180 printf("%s\n", fdisk_parttype_get_string(t
));
1182 fdisk_unref_partition(pa
);
1183 fdisk_deassign_device(sf
->cxt
, 1);
1188 backup_partition_table(sf
, devname
);
1190 /* parse <type> and apply to PT */
1191 type
= fdisk_label_advparse_parttype(lb
, typestr
,
1192 FDISK_PARTTYPE_PARSE_DATA
1193 | FDISK_PARTTYPE_PARSE_ALIAS
1194 | FDISK_PARTTYPE_PARSE_NAME
1195 | FDISK_PARTTYPE_PARSE_SHORTCUT
);
1197 errx(EXIT_FAILURE
, _("failed to parse %s partition type '%s'"),
1198 fdisk_label_get_name(lb
), typestr
);
1200 else if (fdisk_set_partition_type(sf
->cxt
, partno
- 1, type
) != 0)
1201 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition type"),
1203 fdisk_unref_parttype(type
);
1204 return write_changes(sf
);
1208 * sfdisk --part-uuid <device> <partno> [<uuid>]
1210 static int command_partuuid(struct sfdisk
*sf
, int argc
, char **argv
)
1213 struct fdisk_partition
*pa
= NULL
;
1214 const char *devname
= NULL
, *uuid
= NULL
;
1217 errx(EXIT_FAILURE
, _("no disk device specified"));
1221 errx(EXIT_FAILURE
, _("no partition number specified"));
1222 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1227 errx(EXIT_FAILURE
, _("unexpected arguments"));
1229 /* read-only if uuid not given */
1230 assign_device_partition(sf
, devname
, partno
, !uuid
);
1232 /* print partition uuid */
1234 const char *str
= NULL
;
1236 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1237 str
= fdisk_partition_get_uuid(pa
);
1239 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition UUID"),
1241 printf("%s\n", str
);
1242 fdisk_unref_partition(pa
);
1243 fdisk_deassign_device(sf
->cxt
, 1);
1248 backup_partition_table(sf
, devname
);
1250 pa
= fdisk_new_partition();
1252 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1254 if (fdisk_partition_set_uuid(pa
, uuid
) != 0 ||
1255 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1256 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition UUID"),
1258 fdisk_unref_partition(pa
);
1259 return write_changes(sf
);
1263 * sfdisk --part-label <device> <partno> [<label>]
1265 static int command_partlabel(struct sfdisk
*sf
, int argc
, char **argv
)
1268 struct fdisk_partition
*pa
= NULL
;
1269 const char *devname
= NULL
, *name
= NULL
;
1272 errx(EXIT_FAILURE
, _("no disk device specified"));
1276 errx(EXIT_FAILURE
, _("no partition number specified"));
1277 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1282 errx(EXIT_FAILURE
, _("unexpected arguments"));
1284 /* read-only if name not given */
1285 assign_device_partition(sf
, devname
, partno
, !name
);
1287 /* print partition name */
1289 const char *str
= NULL
;
1291 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1292 str
= fdisk_partition_get_name(pa
);
1294 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition name"),
1296 printf("%s\n", str
);
1297 fdisk_unref_partition(pa
);
1298 fdisk_deassign_device(sf
->cxt
, 1);
1303 backup_partition_table(sf
, devname
);
1305 pa
= fdisk_new_partition();
1307 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1309 if (fdisk_partition_set_name(pa
, name
) != 0 ||
1310 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1311 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition name"),
1314 fdisk_unref_partition(pa
);
1315 return write_changes(sf
);
1319 * sfdisk --part-attrs <device> <partno> [<attrs>]
1321 static int command_partattrs(struct sfdisk
*sf
, int argc
, char **argv
)
1324 struct fdisk_partition
*pa
= NULL
;
1325 const char *devname
= NULL
, *attrs
= NULL
;
1328 errx(EXIT_FAILURE
, _("no disk device specified"));
1332 errx(EXIT_FAILURE
, _("no partition number specified"));
1333 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1338 errx(EXIT_FAILURE
, _("unexpected arguments"));
1340 /* read-only if name not given */
1341 assign_device_partition(sf
, devname
, partno
, !attrs
);
1343 /* print partition name */
1345 const char *str
= NULL
;
1347 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1348 str
= fdisk_partition_get_attrs(pa
);
1350 printf("%s\n", str
);
1351 fdisk_unref_partition(pa
);
1352 fdisk_deassign_device(sf
->cxt
, 1);
1357 backup_partition_table(sf
, devname
);
1359 pa
= fdisk_new_partition();
1361 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1363 if (fdisk_partition_set_attrs(pa
, attrs
) != 0 ||
1364 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1365 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition attributes"),
1368 fdisk_unref_partition(pa
);
1369 return write_changes(sf
);
1373 * sfdisk --disk-id <device> [<str>]
1375 static int command_diskid(struct sfdisk
*sf
, int argc
, char **argv
)
1377 const char *devname
= NULL
;
1381 errx(EXIT_FAILURE
, _("no disk device specified"));
1387 errx(EXIT_FAILURE
, _("unexpected arguments"));
1389 assign_device(sf
, devname
, !str
);
1393 fdisk_get_disklabel_id(sf
->cxt
, &str
);
1395 printf("%s\n", str
);
1397 fdisk_deassign_device(sf
->cxt
, 1);
1401 if (fdisk_set_disklabel_id_from_string(sf
->cxt
, str
) != 0)
1402 errx(EXIT_FAILURE
, _("%s: failed to set disklabel ID"), devname
);
1404 return write_changes(sf
);
1408 * sfdisk --relocate <mode> <device>
1410 static int command_relocate(struct sfdisk
*sf
, int argc
, char **argv
)
1412 const char *devname
= NULL
;
1413 const char *oper
= NULL
;
1414 struct fdisk_label
*lb
;
1417 errx(EXIT_FAILURE
, _("no relocate operation specified"));
1419 errx(EXIT_FAILURE
, _("no disk device specified"));
1421 errx(EXIT_FAILURE
, _("unexpected arguments"));
1425 lb
= fdisk_get_label(sf
->cxt
, "gpt");
1427 if (strcmp(oper
, "gpt-bak-mini") == 0)
1428 fdisk_gpt_enable_minimize(lb
, 1);
1430 else if (strcmp(oper
, "gpt-bak-std") != 0)
1431 errx(EXIT_FAILURE
, _("unsupported relocation operation"));
1433 assign_device(sf
, devname
, 0);
1435 fdisk_label_set_changed(lb
, 1);
1437 return write_changes(sf
);
1440 static void sfdisk_print_partition(struct sfdisk
*sf
, size_t n
)
1442 struct fdisk_partition
*pa
= NULL
;
1449 if (fdisk_get_partition(sf
->cxt
, n
, &pa
) != 0)
1452 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_DEVICE
, &data
);
1453 printf("%12s : ", data
);
1455 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_START
, &data
);
1456 printf("%12s ", data
);
1458 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_END
, &data
);
1459 printf("%12s ", data
);
1461 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_SIZE
, &data
);
1462 printf("(%s) ", data
);
1464 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_TYPE
, &data
);
1465 printf("%s\n", data
);
1467 fdisk_unref_partition(pa
);
1470 static void command_fdisk_help(void)
1472 fputs(_("\nHelp:\n"), stdout
);
1474 fputc('\n', stdout
);
1475 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1476 fputs(_(" Commands:\n"), stdout
);
1478 fputs(_(" write write table to disk and exit\n"), stdout
);
1479 fputs(_(" quit show new situation and wait for user's feedback before write\n"), stdout
);
1480 fputs(_(" abort exit sfdisk shell\n"), stdout
);
1481 fputs(_(" print display the partition table\n"), stdout
);
1482 fputs(_(" help show this help text\n"), stdout
);
1483 fputc('\n', stdout
);
1484 fputs(_(" Ctrl-D the same as 'quit'\n"), stdout
);
1486 fputc('\n', stdout
);
1487 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1488 fputs(_(" Input format:\n"), stdout
);
1490 fputs(_(" <start>, <size>, <type>, <bootable>\n"), stdout
);
1492 fputc('\n', stdout
);
1493 fputs(_(" <start> Beginning of the partition in sectors, or bytes if\n"
1494 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1495 " The default is the first free space.\n"), stdout
);
1497 fputc('\n', stdout
);
1498 fputs(_(" <size> Size of the partition in sectors, or bytes if\n"
1499 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1500 " The default is all available space.\n"), stdout
);
1502 fputc('\n', stdout
);
1503 fputs(_(" <type> The partition type. Default is a Linux data partition.\n"), stdout
);
1504 fputs(_(" MBR: hex or L,S,Ex,X,U,R,V shortcuts.\n"), stdout
);
1505 fputs(_(" GPT: UUID or L,S,H,U,R,V shortcuts.\n"), stdout
);
1507 fputc('\n', stdout
);
1508 fputs(_(" <bootable> Use '*' to mark an MBR partition as bootable.\n"), stdout
);
1510 fputc('\n', stdout
);
1511 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1512 fputs(_(" Example:\n"), stdout
);
1514 fputs(_(" , 4G Creates a 4GiB partition at default start offset.\n"), stdout
);
1515 fputc('\n', stdout
);
1519 SFDISK_DONE_NONE
= 0,
1526 /* returns: 0 on success, <0 on error, 1 successfully stop sfdisk */
1527 static int loop_control_commands(struct sfdisk
*sf
,
1528 struct fdisk_script
*dp
,
1531 const char *p
= skip_blank(buf
);
1532 int rc
= SFDISK_DONE_NONE
;
1534 if (strcmp(p
, "print") == 0)
1535 list_disklabel(sf
->cxt
);
1536 else if (strcmp(p
, "help") == 0)
1537 command_fdisk_help();
1538 else if (strcmp(p
, "quit") == 0)
1539 rc
= SFDISK_DONE_ASK
;
1540 else if (strcmp(p
, "write") == 0)
1541 rc
= SFDISK_DONE_WRITE
;
1542 else if (strcmp(p
, "abort") == 0)
1543 rc
= SFDISK_DONE_ABORT
;
1545 if (sf
->interactive
)
1546 fdisk_warnx(sf
->cxt
, _("unsupported command"));
1548 fdisk_warnx(sf
->cxt
, _("line %d: unsupported command"),
1549 fdisk_script_get_nlines(dp
));
1556 static int has_container_or_unused(struct sfdisk
*sf
)
1559 struct fdisk_partition
*pa
= NULL
;
1561 if (sf
->container
|| sf
->unused
)
1564 nparts
= fdisk_get_npartitions(sf
->cxt
);
1565 for (i
= 0; i
< nparts
; i
++) {
1567 if (!fdisk_is_partition_used(sf
->cxt
, i
)) {
1571 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0)
1573 if (fdisk_partition_is_container(pa
))
1577 fdisk_unref_partition(pa
);
1578 return sf
->container
|| sf
->unused
;
1581 static size_t last_pt_partno(struct sfdisk
*sf
)
1583 size_t i
, nparts
, partno
= 0;
1584 struct fdisk_partition
*pa
= NULL
;
1587 nparts
= fdisk_get_npartitions(sf
->cxt
);
1588 for (i
= 0; i
< nparts
; i
++) {
1591 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0 ||
1592 !fdisk_partition_is_used(pa
))
1594 x
= fdisk_partition_get_partno(pa
);
1599 fdisk_unref_partition(pa
);
1603 #ifdef HAVE_LIBREADLINE
1604 static char *sfdisk_fgets(struct fdisk_script
*dp
,
1605 char *buf
, size_t bufsz
, FILE *f
)
1607 struct sfdisk
*sf
= (struct sfdisk
*) fdisk_script_get_userdata(dp
);
1613 if (sf
->interactive
) {
1614 char *p
= readline(sf
->prompt
);
1620 if (len
> bufsz
- 2)
1623 memcpy(buf
, p
, len
);
1624 buf
[len
] = '\n'; /* append \n to be compatible with libc fgetc() */
1625 buf
[len
+ 1] = '\0';
1630 return fgets(buf
, bufsz
, f
);
1634 static int ignore_partition(struct fdisk_partition
*pa
)
1636 /* incomplete partition setting */
1637 if (!fdisk_partition_has_start(pa
) && !fdisk_partition_start_is_default(pa
))
1639 if (!fdisk_partition_has_size(pa
) && !fdisk_partition_end_is_default(pa
))
1642 /* probably dump from old sfdisk with start=0 size=0 */
1643 if (fdisk_partition_has_start(pa
) && fdisk_partition_get_start(pa
) == 0 &&
1644 fdisk_partition_has_size(pa
) && fdisk_partition_get_size(pa
) == 0)
1650 static void follow_wipe_mode(struct sfdisk
*sf
)
1652 int dowipe
= sf
->wipemode
== WIPEMODE_ALWAYS
? 1 : 0;
1654 if (sf
->interactive
&& sf
->wipemode
== WIPEMODE_AUTO
)
1655 dowipe
= 1; /* do it in interactive mode */
1657 if (fdisk_is_ptcollision(sf
->cxt
) && sf
->wipemode
!= WIPEMODE_NEVER
)
1658 dowipe
= 1; /* always wipe old PT */
1660 fdisk_enable_wipe(sf
->cxt
, dowipe
);
1665 if (!fdisk_is_ptcollision(sf
->cxt
)) {
1666 fdisk_warnx(sf
->cxt
, _(
1667 "The device contains '%s' signature and it may be removed by a write command. "
1668 "See sfdisk(8) man page and --wipe option for more details."),
1669 fdisk_get_collision(sf
->cxt
));
1670 fputc('\n', stdout
);
1673 fdisk_warnx(sf
->cxt
, _(
1674 "The device contains '%s' signature and it may remain on the device. "
1675 "It is recommended to wipe the device with wipefs(8) or "
1676 "sfdisk --wipe, in order to avoid possible collisions."),
1677 fdisk_get_collision(sf
->cxt
));
1678 fputc('\n', stderr
);
1682 static int wipe_partition(struct sfdisk
*sf
, size_t partno
)
1685 char *fstype
= NULL
;
1686 struct fdisk_partition
*tmp
= NULL
;
1688 DBG(MISC
, ul_debug("checking for signature"));
1690 rc
= fdisk_get_partition(sf
->cxt
, partno
, &tmp
);
1694 rc
= fdisk_partition_to_string(tmp
, sf
->cxt
, FDISK_FIELD_FSTYPE
, &fstype
);
1695 if (rc
|| fstype
== NULL
)
1698 fdisk_warnx(sf
->cxt
, _("Partition #%zu contains a %s signature."), partno
+ 1, fstype
);
1700 if (sf
->pwipemode
== WIPEMODE_AUTO
&& isatty(STDIN_FILENO
))
1701 fdisk_ask_yesno(sf
->cxt
, _("Do you want to remove the signature?"), &yes
);
1702 else if (sf
->pwipemode
== WIPEMODE_ALWAYS
)
1706 fdisk_info(sf
->cxt
, _("The signature will be removed by a write command."));
1707 rc
= fdisk_wipe_partition(sf
->cxt
, partno
, TRUE
);
1710 fdisk_unref_partition(tmp
);
1712 DBG(MISC
, ul_debug("partition wipe check end [rc=%d]", rc
));
1716 static void refresh_prompt_buffer(struct sfdisk
*sf
, const char *devname
,
1717 size_t next_partno
, int created
)
1720 char *partname
= fdisk_partname(devname
, next_partno
+ 1);
1722 err(EXIT_FAILURE
, _("failed to allocate partition name"));
1724 if (!sf
->prompt
|| !startswith(sf
->prompt
, partname
)) {
1726 xasprintf(&sf
->prompt
,"%s: ", partname
);
1729 } else if (!sf
->prompt
|| !startswith(sf
->prompt
, SFDISK_PROMPT
)) {
1731 sf
->prompt
= xstrdup(SFDISK_PROMPT
);
1736 * sfdisk <device> [[-N] <partno>]
1738 * Note that the option -N is there for backward compatibility only.
1740 static int command_fdisk(struct sfdisk
*sf
, int argc
, char **argv
)
1742 int rc
= 0, partno
= sf
->partno
, created
= 0, unused
= 0, ignored
= 0;
1743 struct fdisk_script
*dp
;
1744 struct fdisk_table
*tb
= NULL
;
1745 const char *devname
= NULL
, *label
;
1747 size_t next_partno
= (size_t) -1;
1751 if (partno
< 0 && argc
> 1)
1752 partno
= strtou32_or_err(argv
[1],
1753 _("failed to parse partition number"));
1755 errx(EXIT_FAILURE
, _("no disk device specified"));
1757 assign_device(sf
, devname
, 0);
1759 dp
= fdisk_new_script(sf
->cxt
);
1761 err(EXIT_FAILURE
, _("failed to allocate script handler"));
1762 fdisk_set_script(sf
->cxt
, dp
);
1763 #ifdef HAVE_LIBREADLINE
1764 fdisk_script_set_fgets(dp
, sfdisk_fgets
);
1766 fdisk_script_set_userdata(dp
, (void *) sf
);
1769 * Don't create a new disklabel when [-N] <partno> specified. In this
1770 * case reuse already specified disklabel. Let's check that the disk
1771 * really contains the partition.
1776 if (!fdisk_has_label(sf
->cxt
))
1777 errx(EXIT_FAILURE
, _("%s: cannot modify partition %d: "
1778 "no partition table was found"),
1779 devname
, partno
+ 1);
1780 n
= fdisk_get_npartitions(sf
->cxt
);
1781 if ((size_t) partno
> n
)
1782 errx(EXIT_FAILURE
, _("%s: cannot modify partition %d: "
1783 "partition table contains only %zu "
1785 devname
, partno
+ 1, n
);
1787 if (!fdisk_is_partition_used(sf
->cxt
, partno
)) {
1788 fdisk_warnx(sf
->cxt
, _("warning: %s: partition %d is not defined yet"),
1789 devname
, partno
+ 1);
1793 next_partno
= partno
;
1796 sf
->orig_pa
= get_partition(sf
->cxt
, partno
);
1801 next_partno
= last_pt_partno(sf
) + 1;
1804 if (!sf
->quiet
&& sf
->interactive
) {
1805 color_scheme_enable("welcome", UL_COLOR_GREEN
);
1806 fdisk_info(sf
->cxt
, _("\nWelcome to sfdisk (%s)."), PACKAGE_STRING
);
1808 fdisk_info(sf
->cxt
, _("Changes will remain in memory only, until you decide to write them.\n"
1809 "Be careful before using the write command.\n"));
1812 if (!sf
->noact
&& !sf
->noreread
) {
1814 fputs(_("Checking that no-one is using this disk right now ..."), stdout
);
1815 if (fdisk_device_is_used(sf
->cxt
)) {
1817 fputs(_(" FAILED\n\n"), stdout
);
1819 fdisk_warnx(sf
->cxt
, _(
1820 "This disk is currently in use - repartitioning is probably a bad idea.\n"
1821 "Umount all file systems, and swapoff all swap partitions on this disk.\n"
1822 "Use the --no-reread flag to suppress this check.\n"));
1825 errx(EXIT_FAILURE
, _("Use the --force flag to overrule all checks."));
1826 } else if (!sf
->quiet
)
1827 fputs(_(" OK\n\n"), stdout
);
1830 if (fdisk_get_collision(sf
->cxt
))
1831 follow_wipe_mode(sf
);
1834 list_disk_geometry(sf
->cxt
);
1835 if (fdisk_has_label(sf
->cxt
)) {
1836 fdisk_info(sf
->cxt
, _("\nOld situation:"));
1837 list_disklabel(sf
->cxt
);
1843 else if (fdisk_has_label(sf
->cxt
))
1844 label
= fdisk_label_get_name(fdisk_get_label(sf
->cxt
, NULL
));
1846 label
= "dos"; /* just for backward compatibility */
1848 if (fdisk_script_set_header(dp
, "label", label
) != 0)
1849 errx(EXIT_FAILURE
, _("failed to set script header"));
1851 if (!sf
->quiet
&& sf
->interactive
) {
1852 if (!fdisk_has_label(sf
->cxt
) && !sf
->label
)
1854 _("\nsfdisk is going to create a new '%s' disk label.\n"
1855 "Use 'label: <name>' before you define a first partition\n"
1856 "to override the default."), label
);
1857 fdisk_info(sf
->cxt
, _("\nType 'help' to get more information.\n"));
1858 } else if (!sf
->quiet
)
1859 fputc('\n', stdout
);
1861 tb
= fdisk_script_get_table(dp
);
1867 DBG(PARSE
, ul_debug("<---next-line--->"));
1868 if (next_partno
== (size_t) -1)
1869 next_partno
= fdisk_table_get_nents(tb
);
1873 && next_partno
== fdisk_get_npartitions(sf
->cxt
)
1874 && !has_container_or_unused(sf
)) {
1875 fdisk_info(sf
->cxt
, _("All partitions used."));
1876 rc
= SFDISK_DONE_ASK
;
1880 refresh_prompt_buffer(sf
, devname
, next_partno
, created
);
1883 if (sf
->prompt
&& (sf
->interactive
|| !sf
->quiet
)) {
1884 #ifndef HAVE_LIBREADLINE
1885 fputs(sf
->prompt
, stdout
);
1887 if (!sf
->interactive
)
1888 fputs(sf
->prompt
, stdout
);
1892 rc
= fdisk_script_read_line(dp
, stdin
, buf
, sizeof(buf
));
1893 if (rc
== -ENOTSUP
) {
1894 buf
[sizeof(buf
) - 1] = '\0';
1895 fdisk_warnx(sf
->cxt
, _("Unknown script header '%s' -- ignore."), buf
);
1900 DBG(PARSE
, ul_debug("script parsing failed, trying sfdisk specific commands"));
1901 buf
[sizeof(buf
) - 1] = '\0';
1902 rc
= loop_control_commands(sf
, dp
, buf
);
1909 rc
= SFDISK_DONE_EOF
;
1911 fputs(_("Done.\n"), stdout
);
1915 nparts
= fdisk_table_get_nents(tb
);
1917 size_t cur_partno
= (size_t) -1;
1918 struct fdisk_partition
*pa
= fdisk_table_get_partition(tb
, nparts
- 1);
1922 if (ignore_partition(pa
)) {
1923 fdisk_info(sf
->cxt
, _("Ignoring partition."));
1928 if (!created
) { /* create a new disklabel */
1929 rc
= fdisk_apply_script_headers(sf
->cxt
, dp
);
1933 fdisk_warn(sf
->cxt
, _(
1934 "Failed to apply script headers, disk label not created"));
1937 if (rc
== 0 && fdisk_get_collision(sf
->cxt
))
1938 follow_wipe_mode(sf
);
1940 if (!rc
&& partno
>= 0) { /* -N <partno>, modify partition */
1941 rc
= fdisk_set_partition(sf
->cxt
, partno
, pa
);
1942 rc
= rc
== 0 ? SFDISK_DONE_ASK
: SFDISK_DONE_ABORT
;
1946 if (!rc
) { /* add partition */
1947 if (!sf
->interactive
&& !sf
->quiet
&&
1948 (!sf
->prompt
|| startswith(sf
->prompt
, SFDISK_PROMPT
))) {
1949 refresh_prompt_buffer(sf
, devname
, next_partno
, created
);
1950 fputs(sf
->prompt
, stdout
);
1952 rc
= fdisk_add_partition(sf
->cxt
, pa
, &cur_partno
);
1955 fdisk_warn(sf
->cxt
, _("Failed to add #%zu partition"), next_partno
+ 1);
1959 /* wipe partition on success
1961 * Note that unused=1 means -N <partno> for unused,
1962 * otherwise we wipe only newly created partitions.
1964 if (rc
== 0 && (unused
|| partno
< 0)) {
1965 rc
= wipe_partition(sf
, unused
? (size_t) partno
: cur_partno
);
1971 /* success print result */
1972 if (sf
->interactive
)
1973 sfdisk_print_partition(sf
, cur_partno
);
1974 next_partno
= cur_partno
+ 1;
1975 } else if (pa
) /* error, drop partition from script */
1976 fdisk_table_remove_partition(tb
, pa
);
1978 fdisk_info(sf
->cxt
, _("Script header accepted."));
1980 if (rc
&& !sf
->interactive
) {
1981 rc
= SFDISK_DONE_ABORT
;
1986 /* create empty disk label if label, but no partition specified */
1987 if ((rc
== SFDISK_DONE_EOF
|| rc
== SFDISK_DONE_WRITE
) && created
== 0
1988 && fdisk_script_has_force_label(dp
) == 1
1989 && fdisk_table_get_nents(tb
) == (size_t) ignored
1990 && fdisk_script_get_header(dp
, "label")) {
1992 int xrc
= fdisk_apply_script_headers(sf
->cxt
, dp
);
1994 fdisk_warnx(sf
->cxt
, _(
1995 "Failed to apply script headers, "
1996 "disk label not created."));
1997 rc
= SFDISK_DONE_ABORT
;
2001 if (!sf
->quiet
&& rc
!= SFDISK_DONE_ABORT
) {
2002 fdisk_info(sf
->cxt
, _("\nNew situation:"));
2003 list_disk_identifier(sf
->cxt
);
2004 list_disklabel(sf
->cxt
);
2008 case SFDISK_DONE_ASK
:
2009 case SFDISK_DONE_EOF
:
2010 if (sf
->interactive
) {
2012 fdisk_ask_yesno(sf
->cxt
, _("Do you want to write this to disk?"), &yes
);
2014 fdisk_info(sf
->cxt
, _("Leaving."));
2020 case SFDISK_DONE_WRITE
:
2021 rc
= write_changes(sf
);
2023 case SFDISK_DONE_ABORT
:
2024 default: /* rc < 0 on error */
2025 fdisk_info(sf
->cxt
, _("Leaving.\n"));
2029 fdisk_set_script(sf
->cxt
, NULL
);
2030 fdisk_unref_script(dp
);
2034 static void __attribute__((__noreturn__
)) usage(void)
2037 fputs(USAGE_HEADER
, out
);
2040 _(" %1$s [options] <dev> [[-N] <part>]\n"
2041 " %1$s [options] <command>\n"), program_invocation_short_name
);
2043 fputs(USAGE_SEPARATOR
, out
);
2044 fputs(_("Display or manipulate a disk partition table.\n"), out
);
2046 fputs(USAGE_COMMANDS
, out
);
2047 fputs(_(" -A, --activate <dev> [<part> ...] list or set bootable (P)MBR partitions\n"), out
);
2048 fputs(_(" -d, --dump <dev> dump partition table (usable for later input)\n"), out
);
2049 fputs(_(" -J, --json <dev> dump partition table in JSON format\n"), out
);
2050 fputs(_(" -B, --backup-pt-sectors <dev> binary partition table backup (see -b and -O)\n"), out
);
2051 fputs(_(" -g, --show-geometry [<dev> ...] list geometry of all or specified devices\n"), out
);
2052 fputs(_(" -l, --list [<dev> ...] list partitions of each device\n"), out
);
2053 fputs(_(" -F, --list-free [<dev> ...] list unpartitioned free areas of each device\n"), out
);
2054 fputs(_(" -r, --reorder <dev> fix partitions order (by start offset)\n"), out
);
2055 fputs(_(" -s, --show-size [<dev> ...] list sizes of all or specified devices\n"), out
);
2056 fputs(_(" -T, --list-types print the recognized types (see -X)\n"), out
);
2057 fputs(_(" -V, --verify [<dev> ...] test whether partitions seem correct\n"), out
);
2058 fputs(_(" --delete <dev> [<part> ...] delete all or specified partitions\n"), out
);
2060 fputs(USAGE_SEPARATOR
, out
);
2061 fputs(_(" --part-label <dev> <part> [<str>] print or change partition label\n"), out
);
2062 fputs(_(" --part-type <dev> <part> [<type>] print or change partition type\n"), out
);
2063 fputs(_(" --part-uuid <dev> <part> [<uuid>] print or change partition uuid\n"), out
);
2064 fputs(_(" --part-attrs <dev> <part> [<str>] print or change partition attributes\n"), out
);
2066 fputs(USAGE_SEPARATOR
, out
);
2067 fputs(_(" --disk-id <dev> [<str>] print or change disk label ID (UUID)\n"), out
);
2068 fputs(_(" --relocate <oper> <dev> move partition header\n"), out
);
2070 fputs(USAGE_ARGUMENTS
, out
);
2071 fputs(_(" <dev> device (usually disk) path\n"), out
);
2072 fputs(_(" <part> partition number\n"), out
);
2073 fputs(_(" <type> partition type, GUID for GPT, hex for MBR\n"), out
);
2075 fputs(USAGE_OPTIONS
, out
);
2076 fputs(_(" -a, --append append partitions to existing partition table\n"), out
);
2077 fputs(_(" -b, --backup backup partition table sectors (see -O)\n"), out
);
2078 fputs(_(" --bytes print SIZE in bytes rather than in human readable format\n"), out
);
2079 fputs(_(" --move-data[=<typescript>] move partition data after relocation (requires -N)\n"), out
);
2080 fputs(_(" --move-use-fsync use fsync after each write when move data\n"), out
);
2081 fputs(_(" -f, --force disable all consistency checking\n"), out
);
2084 _(" --color[=<when>] colorize output (%s, %s or %s)\n"), "auto", "always", "never");
2086 " %s\n", USAGE_COLORS_DEFAULT
);
2088 _(" --lock[=<mode>] use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock");
2089 fputs(_(" -N, --partno <num> specify partition number\n"), out
);
2090 fputs(_(" -n, --no-act do everything except write to device\n"), out
);
2091 fputs(_(" --no-reread do not check whether the device is in use\n"), out
);
2092 fputs(_(" --no-tell-kernel do not tell kernel about changes\n"), out
);
2093 fputs(_(" -O, --backup-file <path> override default backup file name\n"), out
);
2094 fputs(_(" -o, --output <list> output columns\n"), out
);
2095 fputs(_(" -q, --quiet suppress extra info messages\n"), out
);
2097 _(" -w, --wipe <mode> wipe signatures (%s, %s or %s)\n"), "auto", "always", "never");
2099 _(" -W, --wipe-partitions <mode> wipe signatures from new partitions (%s, %s or %s)\n"), "auto", "always", "never");
2100 fputs(_(" -X, --label <name> specify label type (dos, gpt, ...)\n"), out
);
2101 fputs(_(" -Y, --label-nested <name> specify nested label type (dos, bsd)\n"), out
);
2102 fputs(USAGE_SEPARATOR
, out
);
2103 fputs(_(" -G, --show-pt-geometry deprecated, alias to --show-geometry\n"), out
);
2104 fputs(_(" -L, --Linux deprecated, only for backward compatibility\n"), out
);
2105 fputs(_(" -u, --unit S deprecated, only sector unit is supported\n"), out
);
2107 fputs(USAGE_SEPARATOR
, out
);
2108 fprintf(out
, " -h, --help %s\n", USAGE_OPTSTR_HELP
);
2109 fprintf(out
, " -v, --version %s\n", USAGE_OPTSTR_VERSION
);
2111 list_available_columns(out
);
2113 fprintf(out
, USAGE_MAN_TAIL("sfdisk(8)"));
2118 int main(int argc
, char *argv
[])
2120 const char *outarg
= NULL
;
2121 int rc
= -EINVAL
, c
, longidx
= -1, bytes
= 0;
2122 int colormode
= UL_COLORMODE_UNDEF
;
2123 struct sfdisk _sf
= {
2125 .wipemode
= WIPEMODE_AUTO
,
2126 .pwipemode
= WIPEMODE_AUTO
,
2127 .interactive
= isatty(STDIN_FILENO
) ? 1 : 0,
2131 OPT_CHANGE_ID
= CHAR_MAX
+ 1,
2150 static const struct option longopts
[] = {
2151 { "activate",no_argument
, NULL
, 'A' },
2152 { "append", no_argument
, NULL
, 'a' },
2153 { "backup-pt-sectors", no_argument
, NULL
, 'B' },
2154 { "backup", no_argument
, NULL
, 'b' },
2155 { "backup-file", required_argument
, NULL
, 'O' },
2156 { "bytes", no_argument
, NULL
, OPT_BYTES
},
2157 { "color", optional_argument
, NULL
, OPT_COLOR
},
2158 { "lock", optional_argument
, NULL
, OPT_LOCK
},
2159 { "delete", no_argument
, NULL
, OPT_DELETE
},
2160 { "dump", no_argument
, NULL
, 'd' },
2161 { "help", no_argument
, NULL
, 'h' },
2162 { "force", no_argument
, NULL
, 'f' },
2163 { "json", no_argument
, NULL
, 'J' },
2164 { "label", required_argument
, NULL
, 'X' },
2165 { "label-nested", required_argument
, NULL
, 'Y' },
2166 { "list", no_argument
, NULL
, 'l' },
2167 { "list-free", no_argument
, NULL
, 'F' },
2168 { "list-types", no_argument
, NULL
, 'T' },
2169 { "no-act", no_argument
, NULL
, 'n' },
2170 { "no-reread", no_argument
, NULL
, OPT_NOREREAD
},
2171 { "no-tell-kernel", no_argument
, NULL
, OPT_NOTELL
},
2172 { "move-data", optional_argument
, NULL
, OPT_MOVEDATA
},
2173 { "move-use-fsync", no_argument
, NULL
, OPT_MOVEFSYNC
},
2174 { "output", required_argument
, NULL
, 'o' },
2175 { "partno", required_argument
, NULL
, 'N' },
2176 { "reorder", no_argument
, NULL
, 'r' },
2177 { "show-geometry", no_argument
, NULL
, 'g' },
2178 { "quiet", no_argument
, NULL
, 'q' },
2179 { "verify", no_argument
, NULL
, 'V' },
2180 { "version", no_argument
, NULL
, 'v' },
2181 { "wipe", required_argument
, NULL
, 'w' },
2182 { "wipe-partitions", required_argument
, NULL
, 'W' },
2184 { "relocate", no_argument
, NULL
, OPT_RELOCATE
},
2186 { "part-uuid", no_argument
, NULL
, OPT_PARTUUID
},
2187 { "part-label", no_argument
, NULL
, OPT_PARTLABEL
},
2188 { "part-type", no_argument
, NULL
, OPT_PARTTYPE
},
2189 { "part-attrs", no_argument
, NULL
, OPT_PARTATTRS
},
2191 { "disk-id", no_argument
, NULL
, OPT_DISKID
},
2193 { "show-pt-geometry", no_argument
, NULL
, 'G' }, /* deprecated */
2194 { "unit", required_argument
, NULL
, 'u' }, /* deprecated */
2195 { "Linux", no_argument
, NULL
, 'L' }, /* deprecated */
2196 { "show-size", no_argument
, NULL
, 's' }, /* deprecated */
2198 { "change-id",no_argument
, NULL
, OPT_CHANGE_ID
}, /* deprecated */
2199 { "id", no_argument
, NULL
, 'c' }, /* deprecated */
2200 { "print-id",no_argument
, NULL
, OPT_PRINT_ID
}, /* deprecated */
2202 { NULL
, 0, NULL
, 0 },
2204 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
2205 { 'F','d'}, /* --list-free --dump */
2206 { 'F','J'}, /* --list-free --json */
2207 { 's','u'}, /* --show-size --unit */
2210 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
2213 setlocale(LC_ALL
, "");
2214 bindtextdomain(PACKAGE
, LOCALEDIR
);
2215 textdomain(PACKAGE
);
2216 close_stdout_atexit();
2218 while ((c
= getopt_long(argc
, argv
, "aAbBcdfFgGhJlLo:O:nN:qrsTu:vVX:Y:w:W:",
2219 longopts
, &longidx
)) != -1) {
2221 err_exclusive_options(c
, longopts
, excl
, excl_st
);
2225 sf
->act
= ACT_ACTIVATE
;
2234 sf
->act
= ACT_BACKUP_SECTORS
;
2239 warnx(_("%s is deprecated in favour of --part-type"),
2240 longopts
[longidx
].name
);
2241 sf
->act
= ACT_PARTTYPE
;
2244 warnx(_("--id is deprecated in favour of --part-type"));
2245 sf
->act
= ACT_PARTTYPE
;
2254 sf
->act
= ACT_LIST_FREE
;
2260 warnx(_("--show-pt-geometry is no more implemented. Using --show-geometry."));
2263 sf
->act
= ACT_SHOW_GEOM
;
2272 warnx(_("--Linux option is unnecessary and deprecated"));
2279 sf
->backup_file
= optarg
;
2285 sf
->partno
= strtou32_or_err(optarg
, _("failed to parse partition number")) - 1;
2291 sf
->act
= ACT_REORDER
;
2294 sf
->act
= ACT_SHOW_SIZE
;
2297 sf
->act
= ACT_LIST_TYPES
;
2301 errx(EXIT_FAILURE
, _("unsupported unit '%c'"), *optarg
);
2304 print_version(EXIT_SUCCESS
);
2309 sf
->wipemode
= wipemode_from_string(optarg
);
2310 if (sf
->wipemode
< 0)
2311 errx(EXIT_FAILURE
, _("unsupported wipe mode"));
2314 sf
->pwipemode
= wipemode_from_string(optarg
);
2315 if (sf
->pwipemode
< 0)
2316 errx(EXIT_FAILURE
, _("unsupported wipe mode"));
2322 sf
->label_nested
= optarg
;
2326 sf
->act
= ACT_PARTUUID
;
2329 sf
->act
= ACT_PARTTYPE
;
2332 sf
->act
= ACT_PARTLABEL
;
2335 sf
->act
= ACT_PARTATTRS
;
2338 sf
->act
= ACT_DISKID
;
2347 colormode
= UL_COLORMODE_AUTO
;
2349 colormode
= colormode_or_err(optarg
,
2350 _("unsupported color mode"));
2354 sf
->move_typescript
= optarg
;
2360 sf
->act
= ACT_DELETE
;
2366 sf
->act
= ACT_RELOCATE
;
2373 sf
->lockmode
= optarg
;
2377 errtryhelp(EXIT_FAILURE
);
2381 colors_init(colormode
, "sfdisk");
2385 fdisk_set_size_unit(sf
->cxt
, FDISK_SIZEUNIT_BYTES
);
2388 init_fields(NULL
, outarg
, NULL
);
2390 if (sf
->verify
&& !sf
->act
)
2391 sf
->act
= ACT_VERIFY
; /* --verify make be used with --list too */
2393 sf
->act
= ACT_FDISK
; /* default */
2395 if (sf
->movedata
&& !(sf
->act
== ACT_FDISK
&& sf
->partno
>= 0))
2396 errx(EXIT_FAILURE
, _("--movedata requires -N"));
2400 rc
= command_activate(sf
, argc
- optind
, argv
+ optind
);
2403 case ACT_BACKUP_SECTORS
:
2404 rc
= command_backup_sectors(sf
, argc
- optind
, argv
+ optind
);
2408 rc
= command_delete(sf
, argc
- optind
, argv
+ optind
);
2412 rc
= command_list_partitions(sf
, argc
- optind
, argv
+ optind
);
2415 case ACT_LIST_TYPES
:
2416 rc
= command_list_types(sf
);
2420 rc
= command_list_freespace(sf
, argc
- optind
, argv
+ optind
);
2424 rc
= command_fdisk(sf
, argc
- optind
, argv
+ optind
);
2428 rc
= command_dump(sf
, argc
- optind
, argv
+ optind
);
2432 rc
= command_show_size(sf
, argc
- optind
, argv
+ optind
);
2436 rc
= command_show_geometry(sf
, argc
- optind
, argv
+ optind
);
2440 rc
= command_verify(sf
, argc
- optind
, argv
+ optind
);
2444 rc
= command_parttype(sf
, argc
- optind
, argv
+ optind
);
2448 rc
= command_partuuid(sf
, argc
- optind
, argv
+ optind
);
2452 rc
= command_partlabel(sf
, argc
- optind
, argv
+ optind
);
2456 rc
= command_partattrs(sf
, argc
- optind
, argv
+ optind
);
2460 rc
= command_diskid(sf
, argc
- optind
, argv
+ optind
);
2464 rc
= command_reorder(sf
, argc
- optind
, argv
+ optind
);
2468 rc
= command_relocate(sf
, argc
- optind
, argv
+ optind
);
2474 DBG(MISC
, ul_debug("bye! [rc=%d]", rc
));
2475 return rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;