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)
96 int partno
; /* -N <partno>, default -1 */
97 int wipemode
; /* remove foreign signatures from disk */
98 int pwipemode
; /* remove foreign signatures from partitions */
99 const char *lockmode
; /* as specified by --lock */
100 const char *label
; /* --label <label> */
101 const char *label_nested
; /* --label-nested <label> */
102 const char *backup_file
; /* -O <path> */
103 const char *move_typescript
; /* --movedata <typescript> */
106 struct fdisk_context
*cxt
; /* libfdisk context */
107 struct fdisk_partition
*orig_pa
; /* -N <partno> before the change */
109 unsigned int verify
: 1, /* call fdisk_verify_disklabel() */
110 quiet
: 1, /* suppress extra messages */
111 interactive
: 1, /* running on tty */
112 noreread
: 1, /* don't check device is in use */
113 force
: 1, /* do also stupid things */
114 backup
: 1, /* backup sectors before write PT */
115 container
: 1, /* PT contains container (MBR extended) partitions */
116 unused
: 1, /* PT contains unused partition */
117 append
: 1, /* don't create new PT, append partitions only */
118 json
: 1, /* JSON dump */
119 movedata
: 1, /* move data after resize */
120 movefsync
: 1, /* use fsync() after each write() */
121 notell
: 1, /* don't tell kernel aout new PT */
122 noact
: 1; /* do not write to device */
125 #define SFDISK_PROMPT ">>> "
127 static void sfdiskprog_init_debug(void)
129 __UL_INIT_DEBUG_FROM_ENV(sfdisk
, SFDISKPROG_DEBUG_
, 0, SFDISK_DEBUG
);
133 static int get_user_reply(const char *prompt
, char *buf
, size_t bufsz
)
138 #ifdef HAVE_LIBREADLINE
139 if (isatty(STDIN_FILENO
)) {
140 p
= readline(prompt
);
143 xstrncpy(buf
, p
, bufsz
);
148 fputs(prompt
, stdout
);
151 if (!fgets(buf
, bufsz
, stdin
))
155 for (p
= buf
; *p
&& !isgraph(*p
); p
++); /* get first non-blank */
158 memmove(buf
, p
, p
- buf
); /* remove blank space */
160 if (sz
&& *(buf
+ sz
- 1) == '\n')
161 *(buf
+ sz
- 1) = '\0';
163 DBG(ASK
, ul_debug("user's reply: >>>%s<<<", buf
));
167 static int ask_callback(struct fdisk_context
*cxt
__attribute__((__unused__
)),
168 struct fdisk_ask
*ask
,
171 struct sfdisk
*sf
= (struct sfdisk
*) data
;
176 switch(fdisk_ask_get_type(ask
)) {
177 case FDISK_ASKTYPE_INFO
:
180 fputs(fdisk_ask_print_get_mesg(ask
), stdout
);
183 case FDISK_ASKTYPE_WARNX
:
185 color_scheme_fenable("warn", UL_COLOR_RED
, stderr
);
186 fputs(fdisk_ask_print_get_mesg(ask
), stderr
);
187 color_fdisable(stderr
);
190 case FDISK_ASKTYPE_WARN
:
192 color_scheme_fenable("warn", UL_COLOR_RED
, stderr
);
193 fputs(fdisk_ask_print_get_mesg(ask
), stderr
);
194 errno
= fdisk_ask_print_get_errno(ask
);
195 fprintf(stderr
, ": %m\n");
196 color_fdisable(stderr
);
198 case FDISK_ASKTYPE_YESNO
:
200 char buf
[BUFSIZ
] = { '\0' };
204 fputs(fdisk_ask_get_query(ask
), stdout
);
205 rc
= get_user_reply(_(" [Y]es/[N]o: "), buf
, sizeof(buf
));
209 if (x
== RPMATCH_YES
|| x
== RPMATCH_NO
) {
210 fdisk_ask_yesno_set_result(ask
, x
);
214 DBG(ASK
, ul_debug("yes-no ask: reply '%s' [rc=%d]", buf
, rc
));
223 static void sfdisk_init(struct sfdisk
*sf
)
227 sfdiskprog_init_debug();
229 sf
->cxt
= fdisk_new_context();
231 err(EXIT_FAILURE
, _("failed to allocate libfdisk context"));
232 fdisk_set_ask(sf
->cxt
, ask_callback
, (void *) sf
);
233 fdisk_enable_bootbits_protection(sf
->cxt
, 1);
235 if (sf
->label_nested
) {
236 struct fdisk_context
*x
= fdisk_new_nested_context(sf
->cxt
,
239 err(EXIT_FAILURE
, _("failed to allocate nested libfdisk context"));
240 /* the original context is available by fdisk_get_parent() */
245 static int sfdisk_deinit(struct sfdisk
*sf
)
247 struct fdisk_context
*parent
;
252 parent
= fdisk_get_parent(sf
->cxt
);
254 fdisk_unref_context(sf
->cxt
);
258 fdisk_unref_context(sf
->cxt
);
261 memset(sf
, 0, sizeof(*sf
));
265 static struct fdisk_partition
*get_partition(struct fdisk_context
*cxt
, size_t partno
)
267 struct fdisk_table
*tb
= NULL
;
268 struct fdisk_partition
*pa
;
270 if (fdisk_get_partitions(cxt
, &tb
) != 0)
273 pa
= fdisk_table_get_partition_by_partno(tb
, partno
);
275 fdisk_ref_partition(pa
);
276 fdisk_unref_table(tb
);
280 static void backup_sectors(struct sfdisk
*sf
,
284 uint64_t offset
, size_t size
)
289 devfd
= fdisk_get_devfd(sf
->cxt
);
292 xasprintf(&fname
, "%s0x%08"PRIx64
".bak", tpl
, offset
);
294 fd
= open(fname
, O_CREAT
| O_WRONLY
, S_IRUSR
| S_IWUSR
);
298 if (lseek(devfd
, (off_t
) offset
, SEEK_SET
) == (off_t
) -1) {
299 fdisk_warn(sf
->cxt
, _("cannot seek %s"), devname
);
302 unsigned char *buf
= xmalloc(size
);
304 if (read_all(devfd
, (char *) buf
, size
) != (ssize_t
) size
) {
305 fdisk_warn(sf
->cxt
, _("cannot read %s"), devname
);
309 if (write_all(fd
, buf
, size
) != 0) {
310 fdisk_warn(sf
->cxt
, _("cannot write %s"), fname
);
317 fdisk_info(sf
->cxt
, _("%12s (offset %5ju, size %5ju): %s"),
318 name
, (uintmax_t) offset
, (uintmax_t) size
, fname
);
323 errx(EXIT_FAILURE
, _("%s: failed to create a backup"), devname
);
326 static char *mk_backup_filename_tpl(const char *filename
, const char *devname
, const char *suffix
)
329 char *name
, *buf
= xstrdup(devname
);
331 name
= basename(buf
);
333 if (!filename
|| strcmp(filename
, "@default") == 0) {
334 const char *home
= getenv ("HOME");
336 errx(EXIT_FAILURE
, _("failed to create a backup file, $HOME undefined"));
337 xasprintf(&tpl
, "%s/sfdisk-%s%s", home
, name
, suffix
);
339 xasprintf(&tpl
, "%s-%s%s", filename
, name
, suffix
);
346 static void backup_partition_table(struct sfdisk
*sf
, const char *devname
)
356 if (!fdisk_has_label(sf
->cxt
))
359 tpl
= mk_backup_filename_tpl(sf
->backup_file
, devname
, "-");
361 color_scheme_enable("header", UL_COLOR_BOLD
);
362 fdisk_info(sf
->cxt
, _("Backup files:"));
365 while (fdisk_locate_disklabel(sf
->cxt
, i
++, &name
, &offset
, &size
) == 0 && size
)
366 backup_sectors(sf
, tpl
, name
, devname
, offset
, size
);
373 static int assign_device(struct sfdisk
*sf
, const char *devname
, int rdonly
)
375 struct fdisk_context
*cxt
= sf
->cxt
;
377 if (fdisk_assign_device(cxt
, devname
, rdonly
) != 0)
378 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
380 if (!fdisk_is_readonly(cxt
)) {
381 if (blkdev_lock(fdisk_get_devfd(cxt
), devname
, sf
->lockmode
) != 0) {
382 fdisk_deassign_device(cxt
, 1);
386 backup_partition_table(sf
, devname
);
392 static int move_partition_data(struct sfdisk
*sf
, size_t partno
, struct fdisk_partition
*orig_pa
)
394 struct fdisk_partition
*pa
= get_partition(sf
->cxt
, partno
);
395 char *devname
= NULL
, *typescript
= NULL
, *buf
= NULL
;
397 int ok
= 0, fd
, backward
= 0;
398 fdisk_sector_t nsectors
, from
, to
, step
, i
, prev
;
399 size_t io
, ss
, step_bytes
, cc
, ioerr
= 0;
400 uintmax_t src
, dst
, nbytes
;
401 int progress
= 0, rc
= 0;
402 struct timeval prev_time
;
403 uint64_t bytes_per_sec
= 0;
405 assert(sf
->movedata
);
408 warnx(_("failed to read new partition from device; ignoring --move-data"));
409 else if (!fdisk_partition_has_size(pa
))
410 warnx(_("failed to get size of the new partition; ignoring --move-data"));
411 else if (!fdisk_partition_has_start(pa
))
412 warnx(_("failed to get start of the new partition; ignoring --move-data"));
413 else if (!fdisk_partition_has_size(orig_pa
))
414 warnx(_("failed to get size of the old partition; ignoring --move-data"));
415 else if (!fdisk_partition_has_start(orig_pa
))
416 warnx(_("failed to get start of the old partition; ignoring --move-data"));
417 else if (fdisk_partition_get_start(pa
) == fdisk_partition_get_start(orig_pa
))
418 warnx(_("start of the partition has not been moved; ignoring --move-data"));
419 else if (fdisk_partition_get_size(orig_pa
) < fdisk_partition_get_size(pa
))
420 warnx(_("new partition is smaller than original; ignoring --move-data"));
426 DBG(MISC
, ul_debug("moving data"));
428 fd
= fdisk_get_devfd(sf
->cxt
);
430 /* set move direction and overlay */
431 nsectors
= fdisk_partition_get_size(orig_pa
);
432 from
= fdisk_partition_get_start(orig_pa
);
433 to
= fdisk_partition_get_start(pa
);
436 if ((to
>= from
&& from
+ nsectors
>= to
) ||
437 (from
>= to
&& to
+ nsectors
>= from
)) {
438 /* source and target overlay, check if we need to copy
439 * backwardly from end of the source */
440 DBG(MISC
, ul_debug("overlay between source and target"));
441 backward
= from
< to
;
442 DBG(MISC
, ul_debug(" copy order: %s", backward
? "backward" : "forward"));
445 /* set optimal step size -- nearest to 1MiB aligned to optimal I/O */
446 io
= fdisk_get_optimal_iosize(sf
->cxt
);
447 ss
= fdisk_get_sector_size(sf
->cxt
);
450 if (io
< 1024 * 1024)
451 step_bytes
= ((1024 * 1024) + io
/2) / io
* io
;
455 step
= step_bytes
/ ss
;
456 nbytes
= nsectors
* ss
;
458 DBG(MISC
, ul_debug(" step: %ju (%zu bytes)", (uintmax_t)step
, step_bytes
));
460 #if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
462 posix_fadvise(fd
, from
* ss
, nsectors
* ss
, POSIX_FADV_SEQUENTIAL
);
464 devname
= fdisk_partname(fdisk_get_devname(sf
->cxt
), partno
+1);
465 if (sf
->move_typescript
)
466 typescript
= mk_backup_filename_tpl(sf
->move_typescript
, devname
, ".move");
469 fdisk_info(sf
->cxt
,"");
470 color_scheme_enable("header", UL_COLOR_BOLD
);
471 fdisk_info(sf
->cxt
, sf
->noact
? _("Data move: (--no-act)") : _("Data move:"));
474 fdisk_info(sf
->cxt
, _(" typescript file: %s"), typescript
);
475 printf(_(" start sector: (from/to) %ju / %ju\n"), (uintmax_t) from
, (uintmax_t) to
);
476 printf(_(" sectors: %ju\n"), (uintmax_t) nsectors
);
477 printf(_(" step size: %zu bytes\n"), step_bytes
);
481 if (isatty(fileno(stdout
)))
485 if (sf
->interactive
) {
487 fdisk_ask_yesno(sf
->cxt
, _("Do you want to move partition data?"), &yes
);
489 fdisk_info(sf
->cxt
, _("Leaving."));
495 f
= fopen(typescript
, "w");
498 fdisk_warn(sf
->cxt
, _("cannot open %s"), typescript
);
502 /* don't translate */
503 fprintf(f
, "# sfdisk: " PACKAGE_STRING
"\n");
504 fprintf(f
, "# Disk: %s\n", devname
);
505 fprintf(f
, "# Partition: %zu\n", partno
+ 1);
506 fprintf(f
, "# Operation: move data\n");
507 fprintf(f
, "# Sector size: %zu\n", ss
);
508 fprintf(f
, "# Original start offset (sectors/bytes): %ju/%ju\n",
509 (uintmax_t)from
, (uintmax_t)from
* ss
);
510 fprintf(f
, "# New start offset (sectors/bytes): %ju/%ju\n",
511 (uintmax_t)to
, (uintmax_t)to
* ss
);
512 fprintf(f
, "# Area size (sectors/bytes): %ju/%ju\n",
513 (uintmax_t)nsectors
, (uintmax_t)nsectors
* ss
);
514 fprintf(f
, "# Step size (sectors/bytes): %zu/%zu\n", step
, step_bytes
);
515 fprintf(f
, "# Steps: %ju\n", ((uintmax_t) nsectors
/ step
) + 1);
517 fprintf(f
, "# <step>: <from> <to> (step offsets in bytes)\n");
520 src
= (backward
? from
+ nsectors
: from
) * ss
;
521 dst
= (backward
? to
+ nsectors
: to
) * ss
;
522 buf
= xmalloc(step_bytes
);
524 DBG(MISC
, ul_debug(" initial: src=%ju dst=%ju", src
, dst
));
526 gettimeofday(&prev_time
, NULL
);
529 for (cc
= 1, i
= 0; i
< nsectors
&& nbytes
> 0; i
+= step
, cc
++) {
531 src
-= step_bytes
, dst
-= step_bytes
;
533 DBG(MISC
, ul_debug("#%05zu: src=%ju dst=%ju", cc
, src
, dst
));
535 if (nbytes
< step_bytes
) {
536 DBG(MISC
, ul_debug(" aligning step from %ju to %ju",
537 step_bytes
, nbytes
));
540 nbytes
-= step_bytes
;
544 if (lseek(fd
, src
, SEEK_SET
) == (off_t
) -1 ||
545 read_all(fd
, buf
, step_bytes
) != (ssize_t
) step_bytes
) {
547 fprintf(f
, "%05zu: read error %12ju %12ju\n", cc
, src
, dst
);
549 _("cannot read at offset: %zu; continue"), src
);
555 if (lseek(fd
, dst
, SEEK_SET
) == (off_t
) -1 ||
556 write_all(fd
, buf
, step_bytes
) != 0) {
558 fprintf(f
, "%05zu: write error %12ju %12ju\n", cc
, src
, dst
);
560 _("cannot write at offset: %zu; continue"), dst
);
570 fprintf(f
, "%05zu: %12ju %12ju\n", cc
, src
, dst
);
572 if (progress
&& i
% 10 == 0) {
573 unsigned int elapsed
= 0; /* usec */
574 struct timeval cur_time
;
576 gettimeofday(&cur_time
, NULL
);
577 if (cur_time
.tv_sec
- prev_time
.tv_sec
> 1) {
578 elapsed
= ((cur_time
.tv_sec
- prev_time
.tv_sec
) * 1000000) +
579 (cur_time
.tv_usec
- prev_time
.tv_usec
);
581 bytes_per_sec
= ((i
- prev
) * ss
) / elapsed
; /* per usec */
582 bytes_per_sec
*= 1000000; /* per sec */
584 prev_time
= cur_time
;
589 fprintf(stdout
, _("Moved %ju from %ju sectors (%.3f%%, %.1f MiB/s)."),
591 100.0 / ((double) nsectors
/(i
+1)),
592 (double) (bytes_per_sec
/ (1024 * 1024)));
594 fprintf(stdout
, _("Moved %ju from %ju sectors (%.3f%%)."),
596 100.0 / ((double) nsectors
/(i
+1)));
603 src
+= step_bytes
, dst
+= step_bytes
;
607 int x
= get_terminal_width(80);
612 fprintf(stdout
, _("Moved %ju from %ju sectors (%.0f%%)."),
614 100.0 / ((double) nsectors
/(i
+1)));
626 fdisk_info(sf
->cxt
, _("Your data has not been moved (--no-act)."));
628 fdisk_info(sf
->cxt
, _("%zu I/O errors detected!"), ioerr
);
631 warn(_("%s: failed to move data"), devname
);
636 static int write_changes(struct sfdisk
*sf
)
641 fdisk_info(sf
->cxt
, _("The partition table is unchanged (--no-act)."));
643 rc
= fdisk_write_disklabel(sf
->cxt
);
645 if (rc
== 0 && sf
->movedata
&& sf
->orig_pa
)
646 rc
= move_partition_data(sf
, sf
->partno
, sf
->orig_pa
);
648 if (!sf
->noact
&& !rc
) {
649 fdisk_info(sf
->cxt
, _("\nThe partition table has been altered."));
651 /* Let's wait a little bit. It's possible that our
652 * system is still busy with a previous re-read
653 * ioctl (on sfdisk start) or with another task
654 * related to the write to the device.
657 fdisk_reread_partition_table(sf
->cxt
);
662 rc
= fdisk_deassign_device(sf
->cxt
,
663 sf
->noact
|| sf
->notell
); /* no-sync */
668 * sfdisk --list [<device ..]
670 static int command_list_partitions(struct sfdisk
*sf
, int argc
, char **argv
)
673 fdisk_enable_listonly(sf
->cxt
, 1);
678 for (i
= 0; i
< argc
; i
++)
679 if (print_device_pt(sf
->cxt
, argv
[i
], 1, sf
->verify
, i
) != 0)
682 print_all_devices_pt(sf
->cxt
, sf
->verify
);
688 * sfdisk --list-free [<device ..]
690 static int command_list_freespace(struct sfdisk
*sf
, int argc
, char **argv
)
693 fdisk_enable_listonly(sf
->cxt
, 1);
698 for (i
= 0; i
< argc
; i
++)
699 if (print_device_freespace(sf
->cxt
, argv
[i
], 1, i
) != 0)
702 print_all_devices_freespace(sf
->cxt
);
708 * sfdisk --list-types
710 static int command_list_types(struct sfdisk
*sf
)
712 const struct fdisk_parttype
*t
;
713 struct fdisk_label
*lb
;
721 name
= sf
->label
? sf
->label
: "dos";
722 lb
= fdisk_get_label(sf
->cxt
, name
);
724 errx(EXIT_FAILURE
, _("unsupported label '%s'"), name
);
726 codes
= fdisk_label_has_code_parttypes(lb
);
727 fputs(_("Id Name\n\n"), stdout
);
729 while ((t
= fdisk_label_get_parttype(lb
, i
++))) {
731 printf("%2x %s\n", fdisk_parttype_get_code(t
),
732 fdisk_parttype_get_name(t
));
734 printf("%s %s\n", fdisk_parttype_get_string(t
),
735 fdisk_parttype_get_name(t
));
741 static int verify_device(struct sfdisk
*sf
, const char *devname
)
745 fdisk_enable_listonly(sf
->cxt
, 1);
747 assign_device(sf
, devname
, 1);
749 color_scheme_enable("header", UL_COLOR_BOLD
);
750 fdisk_info(sf
->cxt
, "%s:", devname
);
753 if (!fdisk_has_label(sf
->cxt
))
754 fdisk_info(sf
->cxt
, _("unrecognized partition table type"));
756 rc
= fdisk_verify_disklabel(sf
->cxt
);
758 fdisk_deassign_device(sf
->cxt
, 1);
763 * sfdisk --verify [<device ..]
765 static int command_verify(struct sfdisk
*sf
, int argc
, char **argv
)
767 int nfails
= 0, ct
= 0;
771 for (i
= 0; i
< argc
; i
++) {
773 fdisk_info(sf
->cxt
, " ");
774 if (verify_device(sf
, argv
[i
]) < 0)
781 while ((dev
= next_proc_partition(&f
))) {
783 fdisk_info(sf
->cxt
, " ");
784 if (verify_device(sf
, dev
) < 0)
794 static int get_size(const char *dev
, int silent
, uintmax_t *sz
)
798 fd
= open(dev
, O_RDONLY
);
801 warn(_("cannot open %s"), dev
);
805 if (blkdev_get_sectors(fd
, (unsigned long long *) sz
) == -1) {
807 warn(_("Cannot get size of %s"), dev
);
816 * sfdisk --show-size [<device ..]
818 * (silly, but just for backward compatibility)
820 static int command_show_size(struct sfdisk
*sf
__attribute__((__unused__
)),
821 int argc
, char **argv
)
827 for (i
= 0; i
< argc
; i
++) {
828 if (get_size(argv
[i
], 0, &sz
) == 0)
829 printf("%ju\n", sz
/ 2);
836 while ((dev
= next_proc_partition(&f
))) {
837 if (get_size(dev
, 1, &sz
) == 0) {
838 printf("%s: %9ju\n", dev
, sz
/ 2);
844 printf(_("total: %ju blocks\n"), total
);
850 static int print_geom(struct sfdisk
*sf
, const char *devname
)
852 fdisk_enable_listonly(sf
->cxt
, 1);
854 assign_device(sf
, devname
, 1);
856 fdisk_info(sf
->cxt
, "%s: %ju cylinders, %ju heads, %ju sectors/track",
858 (uintmax_t) fdisk_get_geom_cylinders(sf
->cxt
),
859 (uintmax_t) fdisk_get_geom_heads(sf
->cxt
),
860 (uintmax_t) fdisk_get_geom_sectors(sf
->cxt
));
862 fdisk_deassign_device(sf
->cxt
, 1);
867 * sfdisk --show-geometry [<device ..]
869 static int command_show_geometry(struct sfdisk
*sf
, int argc
, char **argv
)
875 for (i
= 0; i
< argc
; i
++) {
876 if (print_geom(sf
, argv
[i
]) < 0)
883 while ((dev
= next_proc_partition(&f
))) {
884 if (print_geom(sf
, dev
) < 0)
894 * sfdisk --activate <device> [<partno> ...]
896 static int command_activate(struct sfdisk
*sf
, int argc
, char **argv
)
898 int rc
, nparts
, i
, listonly
;
899 struct fdisk_partition
*pa
= NULL
;
900 const char *devname
= NULL
;
903 errx(EXIT_FAILURE
, _("no disk device specified"));
906 /* --activate <device> */
907 listonly
= argc
== 1;
909 assign_device(sf
, devname
, listonly
);
911 if (fdisk_is_label(sf
->cxt
, GPT
)) {
912 if (fdisk_gpt_is_hybrid(sf
->cxt
))
913 errx(EXIT_FAILURE
, _("toggle boot flags is unsupported for Hybrid GPT/MBR"));
915 /* Switch from GPT to PMBR */
916 sf
->cxt
= fdisk_new_nested_context(sf
->cxt
, "dos");
918 err(EXIT_FAILURE
, _("cannot switch to PMBR"));
919 fdisk_info(sf
->cxt
, _("Activation is unsupported for GPT -- entering nested PMBR."));
921 } else if (!fdisk_is_label(sf
->cxt
, DOS
))
922 errx(EXIT_FAILURE
, _("toggle boot flags is supported for MBR or PMBR only"));
924 nparts
= fdisk_get_npartitions(sf
->cxt
);
925 for (i
= 0; i
< nparts
; i
++) {
928 /* note that fdisk_get_partition() reuses the @pa pointer, you
929 * don't have to (re)allocate it */
930 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0)
933 /* sfdisk --activate list bootable partitions */
935 if (!fdisk_partition_is_bootable(pa
))
937 if (fdisk_partition_to_string(pa
, sf
->cxt
,
938 FDISK_FIELD_DEVICE
, &data
) == 0) {
939 printf("%s\n", data
);
943 /* deactivate all active partitions */
944 } else if (fdisk_partition_is_bootable(pa
))
945 fdisk_toggle_partition_flag(sf
->cxt
, i
, DOS_FLAG_ACTIVE
);
948 /* sfdisk --activate <partno> [..] */
949 for (i
= 1; i
< argc
; i
++) {
952 if (i
== 1 && strcmp(argv
[1], "-") == 0)
954 n
= strtou32_or_err(argv
[i
], _("failed to parse partition number"));
956 rc
= fdisk_toggle_partition_flag(sf
->cxt
, n
- 1, DOS_FLAG_ACTIVE
);
959 _("%s: partition %d: failed to toggle bootable flag"),
963 fdisk_unref_partition(pa
);
966 rc
= fdisk_deassign_device(sf
->cxt
, 1);
968 rc
= write_changes(sf
);
973 * sfdisk --delete <device> [<partno> ...]
975 static int command_delete(struct sfdisk
*sf
, int argc
, char **argv
)
978 const char *devname
= NULL
;
981 errx(EXIT_FAILURE
, _("no disk device specified"));
984 assign_device(sf
, devname
, 0);
988 size_t nparts
= fdisk_get_npartitions(sf
->cxt
);
989 for (i
= 0; i
< nparts
; i
++) {
990 if (fdisk_is_partition_used(sf
->cxt
, i
) &&
991 fdisk_delete_partition(sf
->cxt
, i
) != 0)
992 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to delete"), devname
, i
+ 1);
994 /* delete specified */
996 for (i
= 1; i
< (size_t) argc
; i
++) {
997 size_t n
= strtou32_or_err(argv
[i
], _("failed to parse partition number"));
999 if (fdisk_delete_partition(sf
->cxt
, n
- 1) != 0)
1000 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to delete"), devname
, n
);
1004 return write_changes(sf
);
1008 * sfdisk --reorder <device>
1010 static int command_reorder(struct sfdisk
*sf
, int argc
, char **argv
)
1012 const char *devname
= NULL
;
1018 errx(EXIT_FAILURE
, _("no disk device specified"));
1020 assign_device(sf
, devname
, 0); /* read-write */
1022 if (fdisk_reorder_partitions(sf
->cxt
) == 1) /* unchanged */
1023 rc
= fdisk_deassign_device(sf
->cxt
, 1);
1025 rc
= write_changes(sf
);
1032 * sfdisk --dump <device>
1034 static int command_dump(struct sfdisk
*sf
, int argc
, char **argv
)
1036 const char *devname
= NULL
;
1037 struct fdisk_script
*dp
;
1043 errx(EXIT_FAILURE
, _("no disk device specified"));
1045 assign_device(sf
, devname
, 1); /* read-only */
1047 if (!fdisk_has_label(sf
->cxt
))
1048 errx(EXIT_FAILURE
, _("%s: does not contain a recognized partition table"), devname
);
1050 dp
= fdisk_new_script(sf
->cxt
);
1052 err(EXIT_FAILURE
, _("failed to allocate dump struct"));
1054 rc
= fdisk_script_read_context(dp
, NULL
);
1056 errx(EXIT_FAILURE
, _("%s: failed to dump partition table"), devname
);
1059 fdisk_script_enable_json(dp
, 1);
1060 fdisk_script_write_file(dp
, stdout
);
1062 fdisk_unref_script(dp
);
1063 fdisk_deassign_device(sf
->cxt
, 1); /* no-sync() */
1067 static void assign_device_partition(struct sfdisk
*sf
,
1068 const char *devname
,
1074 struct fdisk_label
*lb
= NULL
;
1079 /* read-only when a new <type> undefined */
1080 rc
= fdisk_assign_device(sf
->cxt
, devname
, rdonly
);
1082 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
1084 if (!fdisk_is_readonly(sf
->cxt
)
1085 && blkdev_lock(fdisk_get_devfd(sf
->cxt
), devname
, sf
->lockmode
) != 0) {
1086 fdisk_deassign_device(sf
->cxt
, 1);
1089 lb
= fdisk_get_label(sf
->cxt
, NULL
);
1091 errx(EXIT_FAILURE
, _("%s: no partition table found"), devname
);
1093 n
= fdisk_get_npartitions(sf
->cxt
);
1095 errx(EXIT_FAILURE
, _("%s: partition %zu: partition table contains "
1096 "only %zu partitions"), devname
, partno
, n
);
1097 if (!fdisk_is_partition_used(sf
->cxt
, partno
- 1))
1098 errx(EXIT_FAILURE
, _("%s: partition %zu: partition is unused"),
1103 * sfdisk --part-type <device> <partno> [<type>]
1105 static int command_parttype(struct sfdisk
*sf
, int argc
, char **argv
)
1108 struct fdisk_parttype
*type
= NULL
;
1109 struct fdisk_label
*lb
;
1110 const char *devname
= NULL
, *typestr
= NULL
;
1113 errx(EXIT_FAILURE
, _("no disk device specified"));
1117 errx(EXIT_FAILURE
, _("no partition number specified"));
1118 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1123 errx(EXIT_FAILURE
, _("unexpected arguments"));
1125 /* read-only when a new <type> undefined */
1126 assign_device_partition(sf
, devname
, partno
, !typestr
);
1128 lb
= fdisk_get_label(sf
->cxt
, NULL
);
1130 /* print partition type */
1132 const struct fdisk_parttype
*t
= NULL
;
1133 struct fdisk_partition
*pa
= NULL
;
1135 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1136 t
= fdisk_partition_get_type(pa
);
1138 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition type"),
1141 if (fdisk_label_has_code_parttypes(lb
))
1142 printf("%2x\n", fdisk_parttype_get_code(t
));
1144 printf("%s\n", fdisk_parttype_get_string(t
));
1146 fdisk_unref_partition(pa
);
1147 fdisk_deassign_device(sf
->cxt
, 1);
1152 backup_partition_table(sf
, devname
);
1154 /* parse <type> and apply to PT */
1155 type
= fdisk_label_advparse_parttype(lb
, typestr
,
1156 FDISK_PARTTYPE_PARSE_DATA
1157 | FDISK_PARTTYPE_PARSE_ALIAS
1158 | FDISK_PARTTYPE_PARSE_SHORTCUT
);
1160 errx(EXIT_FAILURE
, _("failed to parse %s partition type '%s'"),
1161 fdisk_label_get_name(lb
), typestr
);
1163 else if (fdisk_set_partition_type(sf
->cxt
, partno
- 1, type
) != 0)
1164 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition type"),
1166 fdisk_unref_parttype(type
);
1167 return write_changes(sf
);
1171 * sfdisk --part-uuid <device> <partno> [<uuid>]
1173 static int command_partuuid(struct sfdisk
*sf
, int argc
, char **argv
)
1176 struct fdisk_partition
*pa
= NULL
;
1177 const char *devname
= NULL
, *uuid
= NULL
;
1180 errx(EXIT_FAILURE
, _("no disk device specified"));
1184 errx(EXIT_FAILURE
, _("no partition number specified"));
1185 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1190 errx(EXIT_FAILURE
, _("unexpected arguments"));
1192 /* read-only if uuid not given */
1193 assign_device_partition(sf
, devname
, partno
, !uuid
);
1195 /* print partition uuid */
1197 const char *str
= NULL
;
1199 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1200 str
= fdisk_partition_get_uuid(pa
);
1202 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition UUID"),
1204 printf("%s\n", str
);
1205 fdisk_unref_partition(pa
);
1206 fdisk_deassign_device(sf
->cxt
, 1);
1211 backup_partition_table(sf
, devname
);
1213 pa
= fdisk_new_partition();
1215 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1217 if (fdisk_partition_set_uuid(pa
, uuid
) != 0 ||
1218 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1219 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition UUID"),
1221 fdisk_unref_partition(pa
);
1222 return write_changes(sf
);
1226 * sfdisk --part-label <device> <partno> [<label>]
1228 static int command_partlabel(struct sfdisk
*sf
, int argc
, char **argv
)
1231 struct fdisk_partition
*pa
= NULL
;
1232 const char *devname
= NULL
, *name
= NULL
;
1235 errx(EXIT_FAILURE
, _("no disk device specified"));
1239 errx(EXIT_FAILURE
, _("no partition number specified"));
1240 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1245 errx(EXIT_FAILURE
, _("unexpected arguments"));
1247 /* read-only if name not given */
1248 assign_device_partition(sf
, devname
, partno
, !name
);
1250 /* print partition name */
1252 const char *str
= NULL
;
1254 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1255 str
= fdisk_partition_get_name(pa
);
1257 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition name"),
1259 printf("%s\n", str
);
1260 fdisk_unref_partition(pa
);
1261 fdisk_deassign_device(sf
->cxt
, 1);
1266 backup_partition_table(sf
, devname
);
1268 pa
= fdisk_new_partition();
1270 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1272 if (fdisk_partition_set_name(pa
, name
) != 0 ||
1273 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1274 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition name"),
1277 fdisk_unref_partition(pa
);
1278 return write_changes(sf
);
1282 * sfdisk --part-attrs <device> <partno> [<attrs>]
1284 static int command_partattrs(struct sfdisk
*sf
, int argc
, char **argv
)
1287 struct fdisk_partition
*pa
= NULL
;
1288 const char *devname
= NULL
, *attrs
= NULL
;
1291 errx(EXIT_FAILURE
, _("no disk device specified"));
1295 errx(EXIT_FAILURE
, _("no partition number specified"));
1296 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1301 errx(EXIT_FAILURE
, _("unexpected arguments"));
1303 /* read-only if name not given */
1304 assign_device_partition(sf
, devname
, partno
, !attrs
);
1306 /* print partition name */
1308 const char *str
= NULL
;
1310 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1311 str
= fdisk_partition_get_attrs(pa
);
1313 printf("%s\n", str
);
1314 fdisk_unref_partition(pa
);
1315 fdisk_deassign_device(sf
->cxt
, 1);
1320 backup_partition_table(sf
, devname
);
1322 pa
= fdisk_new_partition();
1324 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1326 if (fdisk_partition_set_attrs(pa
, attrs
) != 0 ||
1327 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1328 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition attributes"),
1331 fdisk_unref_partition(pa
);
1332 return write_changes(sf
);
1336 * sfdisk --disk-id <device> [<str>]
1338 static int command_diskid(struct sfdisk
*sf
, int argc
, char **argv
)
1340 const char *devname
= NULL
;
1344 errx(EXIT_FAILURE
, _("no disk device specified"));
1350 errx(EXIT_FAILURE
, _("unexpected arguments"));
1352 assign_device(sf
, devname
, !str
);
1356 fdisk_get_disklabel_id(sf
->cxt
, &str
);
1358 printf("%s\n", str
);
1360 fdisk_deassign_device(sf
->cxt
, 1);
1364 if (fdisk_set_disklabel_id_from_string(sf
->cxt
, str
) != 0)
1365 errx(EXIT_FAILURE
, _("%s: failed to set disklabel ID"), devname
);
1367 return write_changes(sf
);
1371 * sfdisk --relocate <mode> <device>
1373 static int command_relocate(struct sfdisk
*sf
, int argc
, char **argv
)
1375 const char *devname
= NULL
;
1376 const char *oper
= NULL
;
1377 struct fdisk_label
*lb
;
1380 errx(EXIT_FAILURE
, _("no relocate operation specified"));
1382 errx(EXIT_FAILURE
, _("no disk device specified"));
1384 errx(EXIT_FAILURE
, _("unexpected arguments"));
1388 lb
= fdisk_get_label(sf
->cxt
, "gpt");
1390 if (strcmp(oper
, "gpt-bak-mini") == 0)
1391 fdisk_gpt_enable_minimize(lb
, 1);
1393 else if (strcmp(oper
, "gpt-bak-std") != 0)
1394 errx(EXIT_FAILURE
, _("unsupported relocation operation"));
1396 assign_device(sf
, devname
, 0);
1398 fdisk_label_set_changed(lb
, 1);
1400 return write_changes(sf
);
1403 static void sfdisk_print_partition(struct sfdisk
*sf
, size_t n
)
1405 struct fdisk_partition
*pa
= NULL
;
1412 if (fdisk_get_partition(sf
->cxt
, n
, &pa
) != 0)
1415 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_DEVICE
, &data
);
1416 printf("%12s : ", data
);
1418 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_START
, &data
);
1419 printf("%12s ", data
);
1421 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_END
, &data
);
1422 printf("%12s ", data
);
1424 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_SIZE
, &data
);
1425 printf("(%s) ", data
);
1427 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_TYPE
, &data
);
1428 printf("%s\n", data
);
1430 fdisk_unref_partition(pa
);
1433 static void command_fdisk_help(void)
1435 fputs(_("\nHelp:\n"), stdout
);
1437 fputc('\n', stdout
);
1438 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1439 fputs(_(" Commands:\n"), stdout
);
1441 fputs(_(" write write table to disk and exit\n"), stdout
);
1442 fputs(_(" quit show new situation and wait for user's feedback before write\n"), stdout
);
1443 fputs(_(" abort exit sfdisk shell\n"), stdout
);
1444 fputs(_(" print display the partition table\n"), stdout
);
1445 fputs(_(" help show this help text\n"), stdout
);
1446 fputc('\n', stdout
);
1447 fputs(_(" Ctrl-D the same as 'quit'\n"), stdout
);
1449 fputc('\n', stdout
);
1450 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1451 fputs(_(" Input format:\n"), stdout
);
1453 fputs(_(" <start>, <size>, <type>, <bootable>\n"), stdout
);
1455 fputc('\n', stdout
);
1456 fputs(_(" <start> Beginning of the partition in sectors, or bytes if\n"
1457 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1458 " The default is the first free space.\n"), stdout
);
1460 fputc('\n', stdout
);
1461 fputs(_(" <size> Size of the partition in sectors, or bytes if\n"
1462 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1463 " The default is all available space.\n"), stdout
);
1465 fputc('\n', stdout
);
1466 fputs(_(" <type> The partition type. Default is a Linux data partition.\n"), stdout
);
1467 fputs(_(" MBR: hex or L,S,Ex,X,U,R,V shortcuts.\n"), stdout
);
1468 fputs(_(" GPT: UUID or L,S,H,U,R,V shortcuts.\n"), stdout
);
1470 fputc('\n', stdout
);
1471 fputs(_(" <bootable> Use '*' to mark an MBR partition as bootable.\n"), stdout
);
1473 fputc('\n', stdout
);
1474 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1475 fputs(_(" Example:\n"), stdout
);
1477 fputs(_(" , 4G Creates a 4GiB partition at default start offset.\n"), stdout
);
1478 fputc('\n', stdout
);
1482 SFDISK_DONE_NONE
= 0,
1489 /* returns: 0 on success, <0 on error, 1 successfully stop sfdisk */
1490 static int loop_control_commands(struct sfdisk
*sf
,
1491 struct fdisk_script
*dp
,
1494 const char *p
= skip_blank(buf
);
1495 int rc
= SFDISK_DONE_NONE
;
1497 if (strcmp(p
, "print") == 0)
1498 list_disklabel(sf
->cxt
);
1499 else if (strcmp(p
, "help") == 0)
1500 command_fdisk_help();
1501 else if (strcmp(p
, "quit") == 0)
1502 rc
= SFDISK_DONE_ASK
;
1503 else if (strcmp(p
, "write") == 0)
1504 rc
= SFDISK_DONE_WRITE
;
1505 else if (strcmp(p
, "abort") == 0)
1506 rc
= SFDISK_DONE_ABORT
;
1508 if (sf
->interactive
)
1509 fdisk_warnx(sf
->cxt
, _("unsupported command"));
1511 fdisk_warnx(sf
->cxt
, _("line %d: unsupported command"),
1512 fdisk_script_get_nlines(dp
));
1519 static int has_container_or_unused(struct sfdisk
*sf
)
1522 struct fdisk_partition
*pa
= NULL
;
1524 if (sf
->container
|| sf
->unused
)
1527 nparts
= fdisk_get_npartitions(sf
->cxt
);
1528 for (i
= 0; i
< nparts
; i
++) {
1530 if (!fdisk_is_partition_used(sf
->cxt
, i
)) {
1534 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0)
1536 if (fdisk_partition_is_container(pa
))
1540 fdisk_unref_partition(pa
);
1541 return sf
->container
|| sf
->unused
;
1544 static size_t last_pt_partno(struct sfdisk
*sf
)
1546 size_t i
, nparts
, partno
= 0;
1547 struct fdisk_partition
*pa
= NULL
;
1550 nparts
= fdisk_get_npartitions(sf
->cxt
);
1551 for (i
= 0; i
< nparts
; i
++) {
1554 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0 ||
1555 !fdisk_partition_is_used(pa
))
1557 x
= fdisk_partition_get_partno(pa
);
1562 fdisk_unref_partition(pa
);
1566 #ifdef HAVE_LIBREADLINE
1567 static char *sfdisk_fgets(struct fdisk_script
*dp
,
1568 char *buf
, size_t bufsz
, FILE *f
)
1570 struct sfdisk
*sf
= (struct sfdisk
*) fdisk_script_get_userdata(dp
);
1576 if (sf
->interactive
) {
1577 char *p
= readline(sf
->prompt
);
1583 if (len
> bufsz
- 2)
1586 memcpy(buf
, p
, len
);
1587 buf
[len
] = '\n'; /* append \n to be compatible with libc fgetc() */
1588 buf
[len
+ 1] = '\0';
1593 return fgets(buf
, bufsz
, f
);
1597 static int ignore_partition(struct fdisk_partition
*pa
)
1599 /* incomplete partition setting */
1600 if (!fdisk_partition_has_start(pa
) && !fdisk_partition_start_is_default(pa
))
1602 if (!fdisk_partition_has_size(pa
) && !fdisk_partition_end_is_default(pa
))
1605 /* probably dump from old sfdisk with start=0 size=0 */
1606 if (fdisk_partition_has_start(pa
) && fdisk_partition_get_start(pa
) == 0 &&
1607 fdisk_partition_has_size(pa
) && fdisk_partition_get_size(pa
) == 0)
1613 static void follow_wipe_mode(struct sfdisk
*sf
)
1615 int dowipe
= sf
->wipemode
== WIPEMODE_ALWAYS
? 1 : 0;
1617 if (sf
->interactive
&& sf
->wipemode
== WIPEMODE_AUTO
)
1618 dowipe
= 1; /* do it in interactive mode */
1620 if (fdisk_is_ptcollision(sf
->cxt
) && sf
->wipemode
!= WIPEMODE_NEVER
)
1621 dowipe
= 1; /* always wipe old PT */
1623 fdisk_enable_wipe(sf
->cxt
, dowipe
);
1628 if (!fdisk_is_ptcollision(sf
->cxt
)) {
1629 fdisk_warnx(sf
->cxt
, _(
1630 "The device contains '%s' signature and it will be removed by a write command. "
1631 "See sfdisk(8) man page and --wipe option for more details."),
1632 fdisk_get_collision(sf
->cxt
));
1633 fputc('\n', stdout
);
1636 fdisk_warnx(sf
->cxt
, _(
1637 "The device contains '%s' signature and it may remain on the device. "
1638 "It is recommended to wipe the device with wipefs(8) or "
1639 "sfdisk --wipe, in order to avoid possible collisions."),
1640 fdisk_get_collision(sf
->cxt
));
1641 fputc('\n', stderr
);
1645 static int wipe_partition(struct sfdisk
*sf
, size_t partno
)
1648 char *fstype
= NULL
;
1649 struct fdisk_partition
*tmp
= NULL
;
1651 DBG(MISC
, ul_debug("checking for signature"));
1653 rc
= fdisk_get_partition(sf
->cxt
, partno
, &tmp
);
1657 rc
= fdisk_partition_to_string(tmp
, sf
->cxt
, FDISK_FIELD_FSTYPE
, &fstype
);
1658 if (rc
|| fstype
== NULL
)
1661 fdisk_warnx(sf
->cxt
, _("Partition #%zu contains a %s signature."), partno
+ 1, fstype
);
1663 if (sf
->pwipemode
== WIPEMODE_AUTO
&& isatty(STDIN_FILENO
))
1664 fdisk_ask_yesno(sf
->cxt
, _("Do you want to remove the signature?"), &yes
);
1665 else if (sf
->pwipemode
== WIPEMODE_ALWAYS
)
1669 fdisk_info(sf
->cxt
, _("The signature will be removed by a write command."));
1670 rc
= fdisk_wipe_partition(sf
->cxt
, partno
, TRUE
);
1673 fdisk_unref_partition(tmp
);
1675 DBG(MISC
, ul_debug("partition wipe check end [rc=%d]", rc
));
1679 static void refresh_prompt_buffer(struct sfdisk
*sf
, const char *devname
,
1680 size_t next_partno
, int created
)
1683 char *partname
= fdisk_partname(devname
, next_partno
+ 1);
1685 err(EXIT_FAILURE
, _("failed to allocate partition name"));
1687 if (!sf
->prompt
|| !startswith(sf
->prompt
, partname
)) {
1689 xasprintf(&sf
->prompt
,"%s: ", partname
);
1692 } else if (!sf
->prompt
|| !startswith(sf
->prompt
, SFDISK_PROMPT
)) {
1694 sf
->prompt
= xstrdup(SFDISK_PROMPT
);
1699 * sfdisk <device> [[-N] <partno>]
1701 * Note that the option -N is there for backward compatibility only.
1703 static int command_fdisk(struct sfdisk
*sf
, int argc
, char **argv
)
1705 int rc
= 0, partno
= sf
->partno
, created
= 0, unused
= 0;
1706 struct fdisk_script
*dp
;
1707 struct fdisk_table
*tb
= NULL
;
1708 const char *devname
= NULL
, *label
;
1710 size_t next_partno
= (size_t) -1;
1714 if (partno
< 0 && argc
> 1)
1715 partno
= strtou32_or_err(argv
[1],
1716 _("failed to parse partition number"));
1718 errx(EXIT_FAILURE
, _("no disk device specified"));
1720 assign_device(sf
, devname
, 0);
1722 dp
= fdisk_new_script(sf
->cxt
);
1724 err(EXIT_FAILURE
, _("failed to allocate script handler"));
1725 fdisk_set_script(sf
->cxt
, dp
);
1726 #ifdef HAVE_LIBREADLINE
1727 fdisk_script_set_fgets(dp
, sfdisk_fgets
);
1729 fdisk_script_set_userdata(dp
, (void *) sf
);
1732 * Don't create a new disklabel when [-N] <partno> specified. In this
1733 * case reuse already specified disklabel. Let's check that the disk
1734 * really contains the partition.
1739 if (!fdisk_has_label(sf
->cxt
))
1740 errx(EXIT_FAILURE
, _("%s: cannot modify partition %d: "
1741 "no partition table was found"),
1742 devname
, partno
+ 1);
1743 n
= fdisk_get_npartitions(sf
->cxt
);
1744 if ((size_t) partno
> n
)
1745 errx(EXIT_FAILURE
, _("%s: cannot modify partition %d: "
1746 "partition table contains only %zu "
1748 devname
, partno
+ 1, n
);
1750 if (!fdisk_is_partition_used(sf
->cxt
, partno
)) {
1751 fdisk_warnx(sf
->cxt
, _("warning: %s: partition %d is not defined yet"),
1752 devname
, partno
+ 1);
1756 next_partno
= partno
;
1759 sf
->orig_pa
= get_partition(sf
->cxt
, partno
);
1764 next_partno
= last_pt_partno(sf
) + 1;
1767 if (!sf
->quiet
&& sf
->interactive
) {
1768 color_scheme_enable("welcome", UL_COLOR_GREEN
);
1769 fdisk_info(sf
->cxt
, _("\nWelcome to sfdisk (%s)."), PACKAGE_STRING
);
1771 fdisk_info(sf
->cxt
, _("Changes will remain in memory only, until you decide to write them.\n"
1772 "Be careful before using the write command.\n"));
1775 if (!sf
->noact
&& !sf
->noreread
) {
1777 fputs(_("Checking that no-one is using this disk right now ..."), stdout
);
1778 if (fdisk_device_is_used(sf
->cxt
)) {
1780 fputs(_(" FAILED\n\n"), stdout
);
1782 fdisk_warnx(sf
->cxt
, _(
1783 "This disk is currently in use - repartitioning is probably a bad idea.\n"
1784 "Umount all file systems, and swapoff all swap partitions on this disk.\n"
1785 "Use the --no-reread flag to suppress this check.\n"));
1788 errx(EXIT_FAILURE
, _("Use the --force flag to overrule all checks."));
1789 } else if (!sf
->quiet
)
1790 fputs(_(" OK\n\n"), stdout
);
1793 if (fdisk_get_collision(sf
->cxt
))
1794 follow_wipe_mode(sf
);
1797 list_disk_geometry(sf
->cxt
);
1798 if (fdisk_has_label(sf
->cxt
)) {
1799 fdisk_info(sf
->cxt
, _("\nOld situation:"));
1800 list_disklabel(sf
->cxt
);
1806 else if (fdisk_has_label(sf
->cxt
))
1807 label
= fdisk_label_get_name(fdisk_get_label(sf
->cxt
, NULL
));
1809 label
= "dos"; /* just for backward compatibility */
1811 if (fdisk_script_set_header(dp
, "label", label
) != 0)
1812 errx(EXIT_FAILURE
, _("failed to set script header"));
1814 if (!sf
->quiet
&& sf
->interactive
) {
1815 if (!fdisk_has_label(sf
->cxt
) && !sf
->label
)
1817 _("\nsfdisk is going to create a new '%s' disk label.\n"
1818 "Use 'label: <name>' before you define a first partition\n"
1819 "to override the default."), label
);
1820 fdisk_info(sf
->cxt
, _("\nType 'help' to get more information.\n"));
1821 } else if (!sf
->quiet
)
1822 fputc('\n', stdout
);
1824 tb
= fdisk_script_get_table(dp
);
1830 DBG(PARSE
, ul_debug("<---next-line--->"));
1831 if (next_partno
== (size_t) -1)
1832 next_partno
= fdisk_table_get_nents(tb
);
1836 && next_partno
== fdisk_get_npartitions(sf
->cxt
)
1837 && !has_container_or_unused(sf
)) {
1838 fdisk_info(sf
->cxt
, _("All partitions used."));
1839 rc
= SFDISK_DONE_ASK
;
1843 refresh_prompt_buffer(sf
, devname
, next_partno
, created
);
1846 if (sf
->prompt
&& (sf
->interactive
|| !sf
->quiet
)) {
1847 #ifndef HAVE_LIBREADLINE
1848 fputs(sf
->prompt
, stdout
);
1850 if (!sf
->interactive
)
1851 fputs(sf
->prompt
, stdout
);
1855 rc
= fdisk_script_read_line(dp
, stdin
, buf
, sizeof(buf
));
1856 if (rc
== -ENOTSUP
) {
1857 buf
[sizeof(buf
) - 1] = '\0';
1858 fdisk_warnx(sf
->cxt
, _("Unknown script header '%s' -- ignore."), buf
);
1863 DBG(PARSE
, ul_debug("script parsing failed, trying sfdisk specific commands"));
1864 buf
[sizeof(buf
) - 1] = '\0';
1865 rc
= loop_control_commands(sf
, dp
, buf
);
1872 rc
= SFDISK_DONE_EOF
;
1874 fputs(_("Done.\n"), stdout
);
1878 nparts
= fdisk_table_get_nents(tb
);
1880 size_t cur_partno
= (size_t) -1;
1881 struct fdisk_partition
*pa
= fdisk_table_get_partition(tb
, nparts
- 1);
1885 if (ignore_partition(pa
)) {
1886 fdisk_info(sf
->cxt
, _("Ignoring partition."));
1890 if (!created
) { /* create a new disklabel */
1891 rc
= fdisk_apply_script_headers(sf
->cxt
, dp
);
1894 fdisk_warnx(sf
->cxt
, _(
1895 "Failed to apply script headers, "
1896 "disk label not created."));
1898 if (rc
== 0 && fdisk_get_collision(sf
->cxt
))
1899 follow_wipe_mode(sf
);
1901 if (!rc
&& partno
>= 0) { /* -N <partno>, modify partition */
1902 rc
= fdisk_set_partition(sf
->cxt
, partno
, pa
);
1903 rc
= rc
== 0 ? SFDISK_DONE_ASK
: SFDISK_DONE_ABORT
;
1907 if (!rc
) { /* add partition */
1908 if (!sf
->interactive
&& !sf
->quiet
&&
1909 (!sf
->prompt
|| startswith(sf
->prompt
, SFDISK_PROMPT
))) {
1910 refresh_prompt_buffer(sf
, devname
, next_partno
, created
);
1911 fputs(sf
->prompt
, stdout
);
1913 rc
= fdisk_add_partition(sf
->cxt
, pa
, &cur_partno
);
1916 fdisk_warn(sf
->cxt
, _("Failed to add #%d partition"), next_partno
+ 1);
1920 /* wipe partition on success
1922 * Note that unused=1 means -N <partno> for unused,
1923 * otherwise we wipe only newly created partitions.
1925 if (rc
== 0 && (unused
|| partno
< 0)) {
1926 rc
= wipe_partition(sf
, unused
? (size_t) partno
: cur_partno
);
1932 /* success print result */
1933 if (sf
->interactive
)
1934 sfdisk_print_partition(sf
, cur_partno
);
1935 next_partno
= cur_partno
+ 1;
1936 } else if (pa
) /* error, drop partition from script */
1937 fdisk_table_remove_partition(tb
, pa
);
1939 fdisk_info(sf
->cxt
, _("Script header accepted."));
1941 if (rc
&& !sf
->interactive
) {
1942 rc
= SFDISK_DONE_ABORT
;
1947 /* create empty disk label if label, but no partition specified */
1948 if ((rc
== SFDISK_DONE_EOF
|| rc
== SFDISK_DONE_WRITE
) && created
== 0
1949 && fdisk_script_has_force_label(dp
) == 1
1950 && fdisk_table_get_nents(tb
) == 0
1951 && fdisk_script_get_header(dp
, "label")) {
1953 int xrc
= fdisk_apply_script_headers(sf
->cxt
, dp
);
1955 fdisk_warnx(sf
->cxt
, _(
1956 "Failed to apply script headers, "
1957 "disk label not created."));
1958 rc
= SFDISK_DONE_ABORT
;
1962 if (!sf
->quiet
&& rc
!= SFDISK_DONE_ABORT
) {
1963 fdisk_info(sf
->cxt
, _("\nNew situation:"));
1964 list_disk_identifier(sf
->cxt
);
1965 list_disklabel(sf
->cxt
);
1969 case SFDISK_DONE_ASK
:
1970 case SFDISK_DONE_EOF
:
1971 if (sf
->interactive
) {
1973 fdisk_ask_yesno(sf
->cxt
, _("Do you want to write this to disk?"), &yes
);
1975 fdisk_info(sf
->cxt
, _("Leaving."));
1981 case SFDISK_DONE_WRITE
:
1982 rc
= write_changes(sf
);
1984 case SFDISK_DONE_ABORT
:
1985 default: /* rc < 0 on error */
1986 fdisk_info(sf
->cxt
, _("Leaving.\n"));
1990 fdisk_set_script(sf
->cxt
, NULL
);
1991 fdisk_unref_script(dp
);
1995 static void __attribute__((__noreturn__
)) usage(void)
1998 fputs(USAGE_HEADER
, out
);
2001 _(" %1$s [options] <dev> [[-N] <part>]\n"
2002 " %1$s [options] <command>\n"), program_invocation_short_name
);
2004 fputs(USAGE_SEPARATOR
, out
);
2005 fputs(_("Display or manipulate a disk partition table.\n"), out
);
2007 fputs(USAGE_COMMANDS
, out
);
2008 fputs(_(" -A, --activate <dev> [<part> ...] list or set bootable (P)MBR partitions\n"), out
);
2009 fputs(_(" -d, --dump <dev> dump partition table (usable for later input)\n"), out
);
2010 fputs(_(" -J, --json <dev> dump partition table in JSON format\n"), out
);
2011 fputs(_(" -g, --show-geometry [<dev> ...] list geometry of all or specified devices\n"), out
);
2012 fputs(_(" -l, --list [<dev> ...] list partitions of each device\n"), out
);
2013 fputs(_(" -F, --list-free [<dev> ...] list unpartitioned free areas of each device\n"), out
);
2014 fputs(_(" -r, --reorder <dev> fix partitions order (by start offset)\n"), out
);
2015 fputs(_(" -s, --show-size [<dev> ...] list sizes of all or specified devices\n"), out
);
2016 fputs(_(" -T, --list-types print the recognized types (see -X)\n"), out
);
2017 fputs(_(" -V, --verify [<dev> ...] test whether partitions seem correct\n"), out
);
2018 fputs(_(" --delete <dev> [<part> ...] delete all or specified partitions\n"), out
);
2020 fputs(USAGE_SEPARATOR
, out
);
2021 fputs(_(" --part-label <dev> <part> [<str>] print or change partition label\n"), out
);
2022 fputs(_(" --part-type <dev> <part> [<type>] print or change partition type\n"), out
);
2023 fputs(_(" --part-uuid <dev> <part> [<uuid>] print or change partition uuid\n"), out
);
2024 fputs(_(" --part-attrs <dev> <part> [<str>] print or change partition attributes\n"), out
);
2026 fputs(USAGE_SEPARATOR
, out
);
2027 fputs(_(" --disk-id <dev> [<str>] print or change disk label ID (UUID)\n"), out
);
2028 fputs(_(" --relocate <oper> <dev> move partition header\n"), out
);
2030 fputs(USAGE_ARGUMENTS
, out
);
2031 fputs(_(" <dev> device (usually disk) path\n"), out
);
2032 fputs(_(" <part> partition number\n"), out
);
2033 fputs(_(" <type> partition type, GUID for GPT, hex for MBR\n"), out
);
2035 fputs(USAGE_OPTIONS
, out
);
2036 fputs(_(" -a, --append append partitions to existing partition table\n"), out
);
2037 fputs(_(" -b, --backup backup partition table sectors (see -O)\n"), out
);
2038 fputs(_(" --bytes print SIZE in bytes rather than in human readable format\n"), out
);
2039 fputs(_(" --move-data[=<typescript>] move partition data after relocation (requires -N)\n"), out
);
2040 fputs(_(" --move-use-fsync use fsync after each write when move data\n"), out
);
2041 fputs(_(" -f, --force disable all consistency checking\n"), out
);
2044 _(" --color[=<when>] colorize output (%s, %s or %s)\n"), "auto", "always", "never");
2046 " %s\n", USAGE_COLORS_DEFAULT
);
2048 _(" --lock[=<mode>] use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock");
2049 fputs(_(" -N, --partno <num> specify partition number\n"), out
);
2050 fputs(_(" -n, --no-act do everything except write to device\n"), out
);
2051 fputs(_(" --no-reread do not check whether the device is in use\n"), out
);
2052 fputs(_(" --no-tell-kernel do not tell kernel about changes\n"), out
);
2053 fputs(_(" -O, --backup-file <path> override default backup file name\n"), out
);
2054 fputs(_(" -o, --output <list> output columns\n"), out
);
2055 fputs(_(" -q, --quiet suppress extra info messages\n"), out
);
2057 _(" -w, --wipe <mode> wipe signatures (%s, %s or %s)\n"), "auto", "always", "never");
2059 _(" -W, --wipe-partitions <mode> wipe signatures from new partitions (%s, %s or %s)\n"), "auto", "always", "never");
2060 fputs(_(" -X, --label <name> specify label type (dos, gpt, ...)\n"), out
);
2061 fputs(_(" -Y, --label-nested <name> specify nested label type (dos, bsd)\n"), out
);
2062 fputs(USAGE_SEPARATOR
, out
);
2063 fputs(_(" -G, --show-pt-geometry deprecated, alias to --show-geometry\n"), out
);
2064 fputs(_(" -L, --Linux deprecated, only for backward compatibility\n"), out
);
2065 fputs(_(" -u, --unit S deprecated, only sector unit is supported\n"), out
);
2067 fputs(USAGE_SEPARATOR
, out
);
2068 printf( " -h, --help %s\n", USAGE_OPTSTR_HELP
);
2069 printf( " -v, --version %s\n", USAGE_OPTSTR_VERSION
);
2071 list_available_columns(out
);
2073 printf(USAGE_MAN_TAIL("sfdisk(8)"));
2078 int main(int argc
, char *argv
[])
2080 const char *outarg
= NULL
;
2081 int rc
= -EINVAL
, c
, longidx
= -1, bytes
= 0;
2082 int colormode
= UL_COLORMODE_UNDEF
;
2083 struct sfdisk _sf
= {
2085 .wipemode
= WIPEMODE_AUTO
,
2086 .pwipemode
= WIPEMODE_AUTO
,
2087 .interactive
= isatty(STDIN_FILENO
) ? 1 : 0,
2091 OPT_CHANGE_ID
= CHAR_MAX
+ 1,
2110 static const struct option longopts
[] = {
2111 { "activate",no_argument
, NULL
, 'A' },
2112 { "append", no_argument
, NULL
, 'a' },
2113 { "backup", no_argument
, NULL
, 'b' },
2114 { "backup-file", required_argument
, NULL
, 'O' },
2115 { "bytes", no_argument
, NULL
, OPT_BYTES
},
2116 { "color", optional_argument
, NULL
, OPT_COLOR
},
2117 { "lock", optional_argument
, NULL
, OPT_LOCK
},
2118 { "delete", no_argument
, NULL
, OPT_DELETE
},
2119 { "dump", no_argument
, NULL
, 'd' },
2120 { "help", no_argument
, NULL
, 'h' },
2121 { "force", no_argument
, NULL
, 'f' },
2122 { "json", no_argument
, NULL
, 'J' },
2123 { "label", required_argument
, NULL
, 'X' },
2124 { "label-nested", required_argument
, NULL
, 'Y' },
2125 { "list", no_argument
, NULL
, 'l' },
2126 { "list-free", no_argument
, NULL
, 'F' },
2127 { "list-types", no_argument
, NULL
, 'T' },
2128 { "no-act", no_argument
, NULL
, 'n' },
2129 { "no-reread", no_argument
, NULL
, OPT_NOREREAD
},
2130 { "no-tell-kernel", no_argument
, NULL
, OPT_NOTELL
},
2131 { "move-data", optional_argument
, NULL
, OPT_MOVEDATA
},
2132 { "move-use-fsync", no_argument
, NULL
, OPT_MOVEFSYNC
},
2133 { "output", required_argument
, NULL
, 'o' },
2134 { "partno", required_argument
, NULL
, 'N' },
2135 { "reorder", no_argument
, NULL
, 'r' },
2136 { "show-geometry", no_argument
, NULL
, 'g' },
2137 { "quiet", no_argument
, NULL
, 'q' },
2138 { "verify", no_argument
, NULL
, 'V' },
2139 { "version", no_argument
, NULL
, 'v' },
2140 { "wipe", required_argument
, NULL
, 'w' },
2141 { "wipe-partitions", required_argument
, NULL
, 'W' },
2143 { "relocate", no_argument
, NULL
, OPT_RELOCATE
},
2145 { "part-uuid", no_argument
, NULL
, OPT_PARTUUID
},
2146 { "part-label", no_argument
, NULL
, OPT_PARTLABEL
},
2147 { "part-type", no_argument
, NULL
, OPT_PARTTYPE
},
2148 { "part-attrs", no_argument
, NULL
, OPT_PARTATTRS
},
2150 { "disk-id", no_argument
, NULL
, OPT_DISKID
},
2152 { "show-pt-geometry", no_argument
, NULL
, 'G' }, /* deprecated */
2153 { "unit", required_argument
, NULL
, 'u' }, /* deprecated */
2154 { "Linux", no_argument
, NULL
, 'L' }, /* deprecated */
2155 { "show-size", no_argument
, NULL
, 's' }, /* deprecated */
2157 { "change-id",no_argument
, NULL
, OPT_CHANGE_ID
}, /* deprecated */
2158 { "id", no_argument
, NULL
, 'c' }, /* deprecated */
2159 { "print-id",no_argument
, NULL
, OPT_PRINT_ID
}, /* deprecated */
2161 { NULL
, 0, NULL
, 0 },
2163 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
2164 { 'F','J','d'}, /* --list-free --json --dump */
2165 { 's','u'}, /* --show-size --unit */
2168 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
2171 setlocale(LC_ALL
, "");
2172 bindtextdomain(PACKAGE
, LOCALEDIR
);
2173 textdomain(PACKAGE
);
2174 close_stdout_atexit();
2176 while ((c
= getopt_long(argc
, argv
, "aAbcdfFgGhJlLo:O:nN:qrsTu:vVX:Y:w:W:",
2177 longopts
, &longidx
)) != -1) {
2179 err_exclusive_options(c
, longopts
, excl
, excl_st
);
2183 sf
->act
= ACT_ACTIVATE
;
2194 warnx(_("%s is deprecated in favour of --part-type"),
2195 longopts
[longidx
].name
);
2196 sf
->act
= ACT_PARTTYPE
;
2199 warnx(_("--id is deprecated in favour of --part-type"));
2200 sf
->act
= ACT_PARTTYPE
;
2209 sf
->act
= ACT_LIST_FREE
;
2215 warnx(_("--show-pt-geometry is no more implemented. Using --show-geometry."));
2218 sf
->act
= ACT_SHOW_GEOM
;
2227 warnx(_("--Linux option is unnecessary and deprecated"));
2234 sf
->backup_file
= optarg
;
2240 sf
->partno
= strtou32_or_err(optarg
, _("failed to parse partition number")) - 1;
2246 sf
->act
= ACT_REORDER
;
2249 sf
->act
= ACT_SHOW_SIZE
;
2252 sf
->act
= ACT_LIST_TYPES
;
2256 errx(EXIT_FAILURE
, _("unsupported unit '%c'"), *optarg
);
2259 print_version(EXIT_SUCCESS
);
2264 sf
->wipemode
= wipemode_from_string(optarg
);
2265 if (sf
->wipemode
< 0)
2266 errx(EXIT_FAILURE
, _("unsupported wipe mode"));
2269 sf
->pwipemode
= wipemode_from_string(optarg
);
2270 if (sf
->pwipemode
< 0)
2271 errx(EXIT_FAILURE
, _("unsupported wipe mode"));
2277 sf
->label_nested
= optarg
;
2281 sf
->act
= ACT_PARTUUID
;
2284 sf
->act
= ACT_PARTTYPE
;
2287 sf
->act
= ACT_PARTLABEL
;
2290 sf
->act
= ACT_PARTATTRS
;
2293 sf
->act
= ACT_DISKID
;
2302 colormode
= UL_COLORMODE_AUTO
;
2304 colormode
= colormode_or_err(optarg
,
2305 _("unsupported color mode"));
2309 sf
->move_typescript
= optarg
;
2315 sf
->act
= ACT_DELETE
;
2321 sf
->act
= ACT_RELOCATE
;
2328 sf
->lockmode
= optarg
;
2332 errtryhelp(EXIT_FAILURE
);
2336 colors_init(colormode
, "sfdisk");
2340 fdisk_set_size_unit(sf
->cxt
, FDISK_SIZEUNIT_BYTES
);
2343 init_fields(NULL
, outarg
, NULL
);
2345 if (sf
->verify
&& !sf
->act
)
2346 sf
->act
= ACT_VERIFY
; /* --verify make be used with --list too */
2348 sf
->act
= ACT_FDISK
; /* default */
2350 if (sf
->movedata
&& !(sf
->act
== ACT_FDISK
&& sf
->partno
>= 0))
2351 errx(EXIT_FAILURE
, _("--movedata requires -N"));
2355 rc
= command_activate(sf
, argc
- optind
, argv
+ optind
);
2359 rc
= command_delete(sf
, argc
- optind
, argv
+ optind
);
2363 rc
= command_list_partitions(sf
, argc
- optind
, argv
+ optind
);
2366 case ACT_LIST_TYPES
:
2367 rc
= command_list_types(sf
);
2371 rc
= command_list_freespace(sf
, argc
- optind
, argv
+ optind
);
2375 rc
= command_fdisk(sf
, argc
- optind
, argv
+ optind
);
2379 rc
= command_dump(sf
, argc
- optind
, argv
+ optind
);
2383 rc
= command_show_size(sf
, argc
- optind
, argv
+ optind
);
2387 rc
= command_show_geometry(sf
, argc
- optind
, argv
+ optind
);
2391 rc
= command_verify(sf
, argc
- optind
, argv
+ optind
);
2395 rc
= command_parttype(sf
, argc
- optind
, argv
+ optind
);
2399 rc
= command_partuuid(sf
, argc
- optind
, argv
+ optind
);
2403 rc
= command_partlabel(sf
, argc
- optind
, argv
+ optind
);
2407 rc
= command_partattrs(sf
, argc
- optind
, argv
+ optind
);
2411 rc
= command_diskid(sf
, argc
- optind
, argv
+ optind
);
2415 rc
= command_reorder(sf
, argc
- optind
, argv
+ optind
);
2419 rc
= command_relocate(sf
, argc
- optind
, argv
+ optind
);
2425 DBG(MISC
, ul_debug("bye! [rc=%d]", rc
));
2426 return rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;