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)
94 int partno
; /* -N <partno>, default -1 */
95 int wipemode
; /* remove foreign signatures from disk */
96 int pwipemode
; /* remove foreign signatures from partitions */
97 const char *label
; /* --label <label> */
98 const char *label_nested
; /* --label-nested <label> */
99 const char *backup_file
; /* -O <path> */
100 const char *move_typescript
; /* --movedata <typescript> */
103 struct fdisk_context
*cxt
; /* libfdisk context */
104 struct fdisk_partition
*orig_pa
; /* -N <partno> before the change */
106 unsigned int verify
: 1, /* call fdisk_verify_disklabel() */
107 quiet
: 1, /* suppress extra messages */
108 interactive
: 1, /* running on tty */
109 noreread
: 1, /* don't check device is in use */
110 force
: 1, /* do also stupid things */
111 backup
: 1, /* backup sectors before write PT */
112 container
: 1, /* PT contains container (MBR extended) partitions */
113 append
: 1, /* don't create new PT, append partitions only */
114 json
: 1, /* JSON dump */
115 movedata
: 1, /* move data after resize */
116 movefsync
: 1, /* use fsync() after each write() */
117 notell
: 1, /* don't tell kernel aout new PT */
118 noact
: 1; /* do not write to device */
121 #define SFDISK_PROMPT ">>> "
123 static void sfdiskprog_init_debug(void)
125 __UL_INIT_DEBUG_FROM_ENV(sfdisk
, SFDISKPROG_DEBUG_
, 0, SFDISK_DEBUG
);
129 static int get_user_reply(const char *prompt
, char *buf
, size_t bufsz
)
134 #ifdef HAVE_LIBREADLINE
135 if (isatty(STDIN_FILENO
)) {
136 p
= readline(prompt
);
139 xstrncpy(buf
, p
, bufsz
);
144 fputs(prompt
, stdout
);
147 if (!fgets(buf
, bufsz
, stdin
))
151 for (p
= buf
; *p
&& !isgraph(*p
); p
++); /* get first non-blank */
154 memmove(buf
, p
, p
- buf
); /* remove blank space */
156 if (sz
&& *(buf
+ sz
- 1) == '\n')
157 *(buf
+ sz
- 1) = '\0';
159 DBG(ASK
, ul_debug("user's reply: >>>%s<<<", buf
));
163 static int ask_callback(struct fdisk_context
*cxt
__attribute__((__unused__
)),
164 struct fdisk_ask
*ask
,
167 struct sfdisk
*sf
= (struct sfdisk
*) data
;
172 switch(fdisk_ask_get_type(ask
)) {
173 case FDISK_ASKTYPE_INFO
:
176 fputs(fdisk_ask_print_get_mesg(ask
), stdout
);
179 case FDISK_ASKTYPE_WARNX
:
181 color_scheme_fenable("warn", UL_COLOR_RED
, stderr
);
182 fputs(fdisk_ask_print_get_mesg(ask
), stderr
);
183 color_fdisable(stderr
);
186 case FDISK_ASKTYPE_WARN
:
188 color_scheme_fenable("warn", UL_COLOR_RED
, stderr
);
189 fputs(fdisk_ask_print_get_mesg(ask
), stderr
);
190 errno
= fdisk_ask_print_get_errno(ask
);
191 fprintf(stderr
, ": %m\n");
192 color_fdisable(stderr
);
194 case FDISK_ASKTYPE_YESNO
:
196 char buf
[BUFSIZ
] = { '\0' };
200 fputs(fdisk_ask_get_query(ask
), stdout
);
201 rc
= get_user_reply(_(" [Y]es/[N]o: "), buf
, sizeof(buf
));
205 if (x
== RPMATCH_YES
|| x
== RPMATCH_NO
) {
206 fdisk_ask_yesno_set_result(ask
, x
);
210 DBG(ASK
, ul_debug("yes-no ask: reply '%s' [rc=%d]", buf
, rc
));
219 static void sfdisk_init(struct sfdisk
*sf
)
223 sfdiskprog_init_debug();
225 sf
->cxt
= fdisk_new_context();
227 err(EXIT_FAILURE
, _("failed to allocate libfdisk context"));
228 fdisk_set_ask(sf
->cxt
, ask_callback
, (void *) sf
);
229 fdisk_enable_bootbits_protection(sf
->cxt
, 1);
231 if (sf
->label_nested
) {
232 struct fdisk_context
*x
= fdisk_new_nested_context(sf
->cxt
,
235 err(EXIT_FAILURE
, _("failed to allocate nested libfdisk context"));
236 /* the original context is available by fdisk_get_parent() */
241 static int sfdisk_deinit(struct sfdisk
*sf
)
243 struct fdisk_context
*parent
;
248 parent
= fdisk_get_parent(sf
->cxt
);
250 fdisk_unref_context(sf
->cxt
);
254 fdisk_unref_context(sf
->cxt
);
257 memset(sf
, 0, sizeof(*sf
));
261 static struct fdisk_partition
*get_partition(struct fdisk_context
*cxt
, size_t partno
)
263 struct fdisk_table
*tb
= NULL
;
264 struct fdisk_partition
*pa
;
266 if (fdisk_get_partitions(cxt
, &tb
) != 0)
269 pa
= fdisk_table_get_partition_by_partno(tb
, partno
);
271 fdisk_ref_partition(pa
);
272 fdisk_unref_table(tb
);
276 static void backup_sectors(struct sfdisk
*sf
,
280 uint64_t offset
, size_t size
)
285 devfd
= fdisk_get_devfd(sf
->cxt
);
288 xasprintf(&fname
, "%s0x%08"PRIx64
".bak", tpl
, offset
);
290 fd
= open(fname
, O_CREAT
| O_WRONLY
, S_IRUSR
| S_IWUSR
);
294 if (lseek(devfd
, (off_t
) offset
, SEEK_SET
) == (off_t
) -1) {
295 fdisk_warn(sf
->cxt
, _("cannot seek %s"), devname
);
298 unsigned char *buf
= xmalloc(size
);
300 if (read_all(devfd
, (char *) buf
, size
) != (ssize_t
) size
) {
301 fdisk_warn(sf
->cxt
, _("cannot read %s"), devname
);
305 if (write_all(fd
, buf
, size
) != 0) {
306 fdisk_warn(sf
->cxt
, _("cannot write %s"), fname
);
313 fdisk_info(sf
->cxt
, _("%12s (offset %5ju, size %5ju): %s"),
314 name
, (uintmax_t) offset
, (uintmax_t) size
, fname
);
319 errx(EXIT_FAILURE
, _("%s: failed to create a backup"), devname
);
322 static char *mk_backup_filename_tpl(const char *filename
, const char *devname
, const char *suffix
)
325 char *name
, *buf
= xstrdup(devname
);
327 name
= basename(buf
);
329 if (!filename
|| strcmp(filename
, "@default") == 0) {
330 const char *home
= getenv ("HOME");
332 errx(EXIT_FAILURE
, _("failed to create a backup file, $HOME undefined"));
333 xasprintf(&tpl
, "%s/sfdisk-%s%s", home
, name
, suffix
);
335 xasprintf(&tpl
, "%s-%s%s", filename
, name
, suffix
);
342 static void backup_partition_table(struct sfdisk
*sf
, const char *devname
)
352 if (!fdisk_has_label(sf
->cxt
))
355 tpl
= mk_backup_filename_tpl(sf
->backup_file
, devname
, "-");
357 color_scheme_enable("header", UL_COLOR_BOLD
);
358 fdisk_info(sf
->cxt
, _("Backup files:"));
361 while (fdisk_locate_disklabel(sf
->cxt
, i
++, &name
, &offset
, &size
) == 0 && size
)
362 backup_sectors(sf
, tpl
, name
, devname
, offset
, size
);
369 static int move_partition_data(struct sfdisk
*sf
, size_t partno
, struct fdisk_partition
*orig_pa
)
371 struct fdisk_partition
*pa
= get_partition(sf
->cxt
, partno
);
372 char *devname
= NULL
, *typescript
= NULL
, *buf
= NULL
;
374 int ok
= 0, fd
, backward
= 0;
375 fdisk_sector_t nsectors
, from
, to
, step
, i
, prev
;
376 size_t io
, ss
, step_bytes
, cc
;
378 int errsv
, progress
= 0;
379 struct timeval prev_time
;
380 uint64_t bytes_per_sec
= 0;
382 assert(sf
->movedata
);
385 warnx(_("failed to read new partition from device; ignoring --move-data"));
386 else if (!fdisk_partition_has_size(pa
))
387 warnx(_("failed to get size of the new partition; ignoring --move-data"));
388 else if (!fdisk_partition_has_start(pa
))
389 warnx(_("failed to get start of the new partition; ignoring --move-data"));
390 else if (!fdisk_partition_has_size(orig_pa
))
391 warnx(_("failed to get size of the old partition; ignoring --move-data"));
392 else if (!fdisk_partition_has_start(orig_pa
))
393 warnx(_("failed to get start of the old partition; ignoring --move-data"));
394 else if (fdisk_partition_get_start(pa
) == fdisk_partition_get_start(orig_pa
))
395 warnx(_("start of the partition has not been moved; ignoring --move-data"));
396 else if (fdisk_partition_get_size(orig_pa
) < fdisk_partition_get_size(pa
))
397 warnx(_("new partition is smaller than original; ignoring --move-data"));
403 DBG(MISC
, ul_debug("moving data"));
405 fd
= fdisk_get_devfd(sf
->cxt
);
407 /* set move direction and overlay */
408 nsectors
= fdisk_partition_get_size(orig_pa
);
409 from
= fdisk_partition_get_start(orig_pa
);
410 to
= fdisk_partition_get_start(pa
);
412 if ((to
>= from
&& from
+ nsectors
>= to
) ||
413 (from
>= to
&& to
+ nsectors
>= from
)) {
414 /* source and target overlay, check if we need to copy
415 * backwardly from end of the source */
416 DBG(MISC
, ul_debug("overlay between source and target"));
417 backward
= from
< to
;
418 DBG(MISC
, ul_debug(" copy order: %s", backward
? "backward" : "forward"));
421 /* set optimal step size -- nearest to 1MiB aligned to optimal I/O */
422 io
= fdisk_get_optimal_iosize(sf
->cxt
);
423 ss
= fdisk_get_sector_size(sf
->cxt
);
426 if (io
< 1024 * 1024)
427 step_bytes
= ((1024 * 1024) + io
/2) / io
* io
;
431 step
= step_bytes
/ ss
;
433 /* align the step (note that nsectors does not have to be power of 2) */
434 while (nsectors
% step
)
437 DBG(MISC
, ul_debug(" step: %ju (%zu bytes)", (uintmax_t)step
, step_bytes
));
439 #if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
441 posix_fadvise(fd
, from
* ss
, nsectors
* ss
, POSIX_FADV_SEQUENTIAL
);
443 devname
= fdisk_partname(fdisk_get_devname(sf
->cxt
), partno
+1);
444 if (sf
->move_typescript
)
445 typescript
= mk_backup_filename_tpl(sf
->move_typescript
, devname
, ".move");
448 fdisk_info(sf
->cxt
,"");
449 color_scheme_enable("header", UL_COLOR_BOLD
);
450 fdisk_info(sf
->cxt
, sf
->noact
? _("Data move: (--no-act)") : _("Data move:"));
453 fdisk_info(sf
->cxt
, _(" typescript file: %s"), typescript
);
454 printf(_(" start sector: (from/to) %ju / %ju\n"), (uintmax_t) from
, (uintmax_t) to
);
455 printf(_(" sectors: %ju\n"), (uintmax_t) nsectors
);
456 printf(_(" step size: %zu bytes\n"), step_bytes
);
460 if (isatty(fileno(stdout
)))
464 if (sf
->interactive
) {
466 fdisk_ask_yesno(sf
->cxt
, _("Do you want to move partition data?"), &yes
);
468 fdisk_info(sf
->cxt
, _("Leaving."));
474 f
= fopen(typescript
, "w");
476 fdisk_warn(sf
->cxt
, _("cannot open %s"), typescript
);
480 /* don't translate */
481 fprintf(f
, "# sfdisk: " PACKAGE_STRING
"\n");
482 fprintf(f
, "# Disk: %s\n", devname
);
483 fprintf(f
, "# Partition: %zu\n", partno
+ 1);
484 fprintf(f
, "# Operation: move data\n");
485 fprintf(f
, "# Original start offset (sectors/bytes): %ju/%ju\n",
486 (uintmax_t)from
, (uintmax_t)from
* ss
);
487 fprintf(f
, "# New start offset (sectors/bytes): %ju/%ju\n",
488 (uintmax_t)to
, (uintmax_t)to
* ss
);
489 fprintf(f
, "# Area size (sectors/bytes): %ju/%ju\n",
490 (uintmax_t)nsectors
, (uintmax_t)nsectors
* ss
);
491 fprintf(f
, "# Sector size: %zu\n", ss
);
492 fprintf(f
, "# Step size (in bytes): %zu\n", step_bytes
);
493 fprintf(f
, "# Steps: %ju\n", (uintmax_t)(nsectors
/ step
));
495 fprintf(f
, "# <step>: <from> <to> (step offsets in bytes)\n");
498 src
= (backward
? from
+ nsectors
: from
) * ss
;
499 dst
= (backward
? to
+ nsectors
: to
) * ss
;
500 buf
= xmalloc(step_bytes
);
502 DBG(MISC
, ul_debug(" initial: src=%ju dst=%ju", src
, dst
));
504 gettimeofday(&prev_time
, NULL
);
507 for (cc
= 1, i
= 0; i
< nsectors
; i
+= step
, cc
++) {
511 src
-= step_bytes
, dst
-= step_bytes
;
513 DBG(MISC
, ul_debug("#%05zu: src=%ju dst=%ju", cc
, src
, dst
));
517 if (lseek(fd
, src
, SEEK_SET
) == (off_t
) -1)
519 rc
= read(fd
, buf
, step_bytes
);
520 if (rc
< 0 || rc
!= (ssize_t
) step_bytes
)
524 if (lseek(fd
, dst
, SEEK_SET
) == (off_t
) -1)
526 rc
= write(fd
, buf
, step_bytes
);
527 if (rc
< 0 || rc
!= (ssize_t
) step_bytes
)
535 fprintf(f
, "%05zu: %12ju %12ju\n", cc
, src
, dst
);
537 if (progress
&& i
% 10 == 0) {
538 unsigned int elapsed
= 0; /* usec */
539 struct timeval cur_time
;
541 gettimeofday(&cur_time
, NULL
);
542 if (cur_time
.tv_sec
- prev_time
.tv_sec
> 1) {
543 elapsed
= ((cur_time
.tv_sec
- prev_time
.tv_sec
) * 1000000) +
544 (cur_time
.tv_usec
- prev_time
.tv_usec
);
546 bytes_per_sec
= ((i
- prev
) * ss
) / elapsed
; /* per usec */
547 bytes_per_sec
*= 1000000; /* per sec */
549 prev_time
= cur_time
;
554 fprintf(stdout
, _("Moved %ju from %ju sectors (%.3f%%, %.1f MiB/s)."),
556 100.0 / ((double) nsectors
/(i
+1)),
557 (double) (bytes_per_sec
/ (1024 * 1024)));
559 fprintf(stdout
, _("Moved %ju from %ju sectors (%.3f%%)."),
561 100.0 / ((double) nsectors
/(i
+1)));
568 src
+= step_bytes
, dst
+= step_bytes
;
572 int x
= get_terminal_width(80);
577 fprintf(stdout
, _("Moved %ju from %ju sectors (%.3f%%)."),
579 100.0 / ((double) nsectors
/(i
+1)));
589 fdisk_info(sf
->cxt
, _("Your data has not been moved (--no-act)."));
594 warn(_("%s: failed to move data"), devname
);
604 static int write_changes(struct sfdisk
*sf
)
609 fdisk_info(sf
->cxt
, _("The partition table is unchanged (--no-act)."));
611 rc
= fdisk_write_disklabel(sf
->cxt
);
613 if (rc
== 0 && sf
->movedata
&& sf
->orig_pa
)
614 rc
= move_partition_data(sf
, sf
->partno
, sf
->orig_pa
);
616 if (!sf
->noact
&& !rc
) {
617 fdisk_info(sf
->cxt
, _("\nThe partition table has been altered."));
619 /* Let's wait a little bit. It's possible that our
620 * system is still busy with a previous re-read
621 * ioctl (on sfdisk start) or with another task
622 * related to the write to the device.
625 fdisk_reread_partition_table(sf
->cxt
);
630 rc
= fdisk_deassign_device(sf
->cxt
,
631 sf
->noact
|| sf
->notell
); /* no-sync */
636 * sfdisk --list [<device ..]
638 static int command_list_partitions(struct sfdisk
*sf
, int argc
, char **argv
)
641 fdisk_enable_listonly(sf
->cxt
, 1);
646 for (i
= 0; i
< argc
; i
++) {
648 fputs("\n\n", stdout
);
649 if (print_device_pt(sf
->cxt
, argv
[i
], 1, sf
->verify
) != 0)
654 print_all_devices_pt(sf
->cxt
, sf
->verify
);
660 * sfdisk --list-free [<device ..]
662 static int command_list_freespace(struct sfdisk
*sf
, int argc
, char **argv
)
665 fdisk_enable_listonly(sf
->cxt
, 1);
670 for (i
= 0; i
< argc
; i
++) {
672 fputs("\n\n", stdout
);
673 if (print_device_freespace(sf
->cxt
, argv
[i
], 1) != 0)
678 print_all_devices_freespace(sf
->cxt
);
684 * sfdisk --list-types
686 static int command_list_types(struct sfdisk
*sf
)
688 const struct fdisk_parttype
*t
;
689 struct fdisk_label
*lb
;
697 name
= sf
->label
? sf
->label
: "dos";
698 lb
= fdisk_get_label(sf
->cxt
, name
);
700 errx(EXIT_FAILURE
, _("unsupported label '%s'"), name
);
702 codes
= fdisk_label_has_code_parttypes(lb
);
703 fputs(_("Id Name\n\n"), stdout
);
705 while ((t
= fdisk_label_get_parttype(lb
, i
++))) {
707 printf("%2x %s\n", fdisk_parttype_get_code(t
),
708 fdisk_parttype_get_name(t
));
710 printf("%s %s\n", fdisk_parttype_get_string(t
),
711 fdisk_parttype_get_name(t
));
717 static int verify_device(struct sfdisk
*sf
, const char *devname
)
721 fdisk_enable_listonly(sf
->cxt
, 1);
723 if (fdisk_assign_device(sf
->cxt
, devname
, 1)) {
724 warn(_("cannot open %s"), devname
);
728 color_scheme_enable("header", UL_COLOR_BOLD
);
729 fdisk_info(sf
->cxt
, "%s:", devname
);
732 if (!fdisk_has_label(sf
->cxt
))
733 fdisk_info(sf
->cxt
, _("unrecognized partition table type"));
735 rc
= fdisk_verify_disklabel(sf
->cxt
);
737 fdisk_deassign_device(sf
->cxt
, 1);
742 * sfdisk --verify [<device ..]
744 static int command_verify(struct sfdisk
*sf
, int argc
, char **argv
)
746 int nfails
= 0, ct
= 0;
750 for (i
= 0; i
< argc
; i
++) {
752 fdisk_info(sf
->cxt
, " ");
753 if (verify_device(sf
, argv
[i
]) < 0)
760 while ((dev
= next_proc_partition(&f
))) {
762 fdisk_info(sf
->cxt
, " ");
763 if (verify_device(sf
, dev
) < 0)
773 static int get_size(const char *dev
, int silent
, uintmax_t *sz
)
777 fd
= open(dev
, O_RDONLY
);
780 warn(_("cannot open %s"), dev
);
784 if (blkdev_get_sectors(fd
, (unsigned long long *) sz
) == -1) {
786 warn(_("Cannot get size of %s"), dev
);
795 * sfdisk --show-size [<device ..]
797 * (silly, but just for backward compatibility)
799 static int command_show_size(struct sfdisk
*sf
__attribute__((__unused__
)),
800 int argc
, char **argv
)
806 for (i
= 0; i
< argc
; i
++) {
807 if (get_size(argv
[i
], 0, &sz
) == 0)
808 printf("%ju\n", sz
/ 2);
815 while ((dev
= next_proc_partition(&f
))) {
816 if (get_size(dev
, 1, &sz
) == 0) {
817 printf("%s: %9ju\n", dev
, sz
/ 2);
823 printf(_("total: %ju blocks\n"), total
);
829 static int print_geom(struct sfdisk
*sf
, const char *devname
)
831 fdisk_enable_listonly(sf
->cxt
, 1);
833 if (fdisk_assign_device(sf
->cxt
, devname
, 1)) {
834 warn(_("cannot open %s"), devname
);
838 fdisk_info(sf
->cxt
, "%s: %ju cylinders, %ju heads, %ju sectors/track",
840 (uintmax_t) fdisk_get_geom_cylinders(sf
->cxt
),
841 (uintmax_t) fdisk_get_geom_heads(sf
->cxt
),
842 (uintmax_t) fdisk_get_geom_sectors(sf
->cxt
));
844 fdisk_deassign_device(sf
->cxt
, 1);
849 * sfdisk --show-geometry [<device ..]
851 static int command_show_geometry(struct sfdisk
*sf
, int argc
, char **argv
)
857 for (i
= 0; i
< argc
; i
++) {
858 if (print_geom(sf
, argv
[i
]) < 0)
865 while ((dev
= next_proc_partition(&f
))) {
866 if (print_geom(sf
, dev
) < 0)
876 * sfdisk --activate <device> [<partno> ...]
878 static int command_activate(struct sfdisk
*sf
, int argc
, char **argv
)
880 int rc
, nparts
, i
, listonly
;
881 struct fdisk_partition
*pa
= NULL
;
882 const char *devname
= NULL
;
885 errx(EXIT_FAILURE
, _("no disk device specified"));
888 /* --activate <device> */
889 listonly
= argc
== 1;
891 rc
= fdisk_assign_device(sf
->cxt
, devname
, listonly
);
893 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
895 if (fdisk_is_label(sf
->cxt
, GPT
)) {
896 if (fdisk_gpt_is_hybrid(sf
->cxt
))
897 errx(EXIT_FAILURE
, _("toggle boot flags is unsupported for Hybrid GPT/MBR"));
899 /* Switch from GPT to PMBR */
900 sf
->cxt
= fdisk_new_nested_context(sf
->cxt
, "dos");
902 err(EXIT_FAILURE
, _("cannot switch to PMBR"));
903 fdisk_info(sf
->cxt
, _("Activation is unsupported for GPT -- entering nested PMBR."));
905 } else if (!fdisk_is_label(sf
->cxt
, DOS
))
906 errx(EXIT_FAILURE
, _("toggle boot flags is supported for MBR or PMBR only"));
908 if (!listonly
&& sf
->backup
)
909 backup_partition_table(sf
, devname
);
911 nparts
= fdisk_get_npartitions(sf
->cxt
);
912 for (i
= 0; i
< nparts
; i
++) {
915 /* note that fdisk_get_partition() reuses the @pa pointer, you
916 * don't have to (re)allocate it */
917 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0)
920 /* sfdisk --activate list bootable partitions */
922 if (!fdisk_partition_is_bootable(pa
))
924 if (fdisk_partition_to_string(pa
, sf
->cxt
,
925 FDISK_FIELD_DEVICE
, &data
) == 0) {
926 printf("%s\n", data
);
930 /* deactivate all active partitions */
931 } else if (fdisk_partition_is_bootable(pa
))
932 fdisk_toggle_partition_flag(sf
->cxt
, i
, DOS_FLAG_ACTIVE
);
935 /* sfdisk --activate <partno> [..] */
936 for (i
= 1; i
< argc
; i
++) {
939 if (i
== 1 && strcmp(argv
[1], "-") == 0)
941 n
= strtou32_or_err(argv
[i
], _("failed to parse partition number"));
943 rc
= fdisk_toggle_partition_flag(sf
->cxt
, n
- 1, DOS_FLAG_ACTIVE
);
946 _("%s: partition %d: failed to toggle bootable flag"),
950 fdisk_unref_partition(pa
);
953 rc
= fdisk_deassign_device(sf
->cxt
, 1);
955 rc
= write_changes(sf
);
960 * sfdisk --delete <device> [<partno> ...]
962 static int command_delete(struct sfdisk
*sf
, int argc
, char **argv
)
965 const char *devname
= NULL
;
968 errx(EXIT_FAILURE
, _("no disk device specified"));
971 if (fdisk_assign_device(sf
->cxt
, devname
, 0) != 0)
972 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
975 backup_partition_table(sf
, devname
);
979 size_t nparts
= fdisk_get_npartitions(sf
->cxt
);
980 for (i
= 0; i
< nparts
; i
++) {
981 if (fdisk_is_partition_used(sf
->cxt
, i
) &&
982 fdisk_delete_partition(sf
->cxt
, i
) != 0)
983 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to delete"), devname
, i
+ 1);
985 /* delete specified */
987 for (i
= 1; i
< (size_t) argc
; i
++) {
988 size_t n
= strtou32_or_err(argv
[i
], _("failed to parse partition number"));
990 if (fdisk_delete_partition(sf
->cxt
, n
- 1) != 0)
991 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to delete"), devname
, n
);
995 return write_changes(sf
);
999 * sfdisk --reorder <device>
1001 static int command_reorder(struct sfdisk
*sf
, int argc
, char **argv
)
1003 const char *devname
= NULL
;
1009 errx(EXIT_FAILURE
, _("no disk device specified"));
1011 rc
= fdisk_assign_device(sf
->cxt
, devname
, 0); /* read-write */
1013 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
1016 backup_partition_table(sf
, devname
);
1018 if (fdisk_reorder_partitions(sf
->cxt
) == 1) /* unchanged */
1019 rc
= fdisk_deassign_device(sf
->cxt
, 1);
1021 rc
= write_changes(sf
);
1028 * sfdisk --dump <device>
1030 static int command_dump(struct sfdisk
*sf
, int argc
, char **argv
)
1032 const char *devname
= NULL
;
1033 struct fdisk_script
*dp
;
1039 errx(EXIT_FAILURE
, _("no disk device specified"));
1041 rc
= fdisk_assign_device(sf
->cxt
, devname
, 1); /* read-only */
1043 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
1045 if (!fdisk_has_label(sf
->cxt
))
1046 errx(EXIT_FAILURE
, _("%s: does not contain a recognized partition table"), devname
);
1048 dp
= fdisk_new_script(sf
->cxt
);
1050 err(EXIT_FAILURE
, _("failed to allocate dump struct"));
1052 rc
= fdisk_script_read_context(dp
, NULL
);
1054 errx(EXIT_FAILURE
, _("%s: failed to dump partition table"), devname
);
1057 fdisk_script_enable_json(dp
, 1);
1058 fdisk_script_write_file(dp
, stdout
);
1060 fdisk_unref_script(dp
);
1061 fdisk_deassign_device(sf
->cxt
, 1); /* no-sync() */
1065 static void assign_device_partition(struct sfdisk
*sf
,
1066 const char *devname
,
1072 struct fdisk_label
*lb
= NULL
;
1077 /* read-only when a new <type> undefined */
1078 rc
= fdisk_assign_device(sf
->cxt
, devname
, rdonly
);
1080 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
1082 lb
= fdisk_get_label(sf
->cxt
, NULL
);
1084 errx(EXIT_FAILURE
, _("%s: no partition table found"), devname
);
1086 n
= fdisk_get_npartitions(sf
->cxt
);
1088 errx(EXIT_FAILURE
, _("%s: partition %zu: partition table contains "
1089 "only %zu partitions"), devname
, partno
, n
);
1090 if (!fdisk_is_partition_used(sf
->cxt
, partno
- 1))
1091 errx(EXIT_FAILURE
, _("%s: partition %zu: partition is unused"),
1096 * sfdisk --part-type <device> <partno> [<type>]
1098 static int command_parttype(struct sfdisk
*sf
, int argc
, char **argv
)
1101 struct fdisk_parttype
*type
= NULL
;
1102 struct fdisk_label
*lb
;
1103 const char *devname
= NULL
, *typestr
= NULL
;
1106 errx(EXIT_FAILURE
, _("no disk device specified"));
1110 errx(EXIT_FAILURE
, _("no partition number specified"));
1111 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1116 errx(EXIT_FAILURE
, _("unexpected arguments"));
1118 /* read-only when a new <type> undefined */
1119 assign_device_partition(sf
, devname
, partno
, !typestr
);
1121 lb
= fdisk_get_label(sf
->cxt
, NULL
);
1123 /* print partition type */
1125 const struct fdisk_parttype
*t
= NULL
;
1126 struct fdisk_partition
*pa
= NULL
;
1128 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1129 t
= fdisk_partition_get_type(pa
);
1131 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition type"),
1134 if (fdisk_label_has_code_parttypes(lb
))
1135 printf("%2x\n", fdisk_parttype_get_code(t
));
1137 printf("%s\n", fdisk_parttype_get_string(t
));
1139 fdisk_unref_partition(pa
);
1140 fdisk_deassign_device(sf
->cxt
, 1);
1145 backup_partition_table(sf
, devname
);
1147 /* parse <type> and apply to PT */
1148 type
= fdisk_label_parse_parttype(lb
, typestr
);
1150 errx(EXIT_FAILURE
, _("failed to parse %s partition type '%s'"),
1151 fdisk_label_get_name(lb
), typestr
);
1153 else if (fdisk_set_partition_type(sf
->cxt
, partno
- 1, type
) != 0)
1154 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition type"),
1156 fdisk_unref_parttype(type
);
1157 return write_changes(sf
);
1161 * sfdisk --part-uuid <device> <partno> [<uuid>]
1163 static int command_partuuid(struct sfdisk
*sf
, int argc
, char **argv
)
1166 struct fdisk_partition
*pa
= NULL
;
1167 const char *devname
= NULL
, *uuid
= NULL
;
1170 errx(EXIT_FAILURE
, _("no disk device specified"));
1174 errx(EXIT_FAILURE
, _("no partition number specified"));
1175 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1180 errx(EXIT_FAILURE
, _("unexpected arguments"));
1182 /* read-only if uuid not given */
1183 assign_device_partition(sf
, devname
, partno
, !uuid
);
1185 /* print partition uuid */
1187 const char *str
= NULL
;
1189 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1190 str
= fdisk_partition_get_uuid(pa
);
1192 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition UUID"),
1194 printf("%s\n", str
);
1195 fdisk_unref_partition(pa
);
1196 fdisk_deassign_device(sf
->cxt
, 1);
1201 backup_partition_table(sf
, devname
);
1203 pa
= fdisk_new_partition();
1205 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1207 if (fdisk_partition_set_uuid(pa
, uuid
) != 0 ||
1208 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1209 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition UUID"),
1211 fdisk_unref_partition(pa
);
1212 return write_changes(sf
);
1216 * sfdisk --part-label <device> <partno> [<label>]
1218 static int command_partlabel(struct sfdisk
*sf
, int argc
, char **argv
)
1221 struct fdisk_partition
*pa
= NULL
;
1222 const char *devname
= NULL
, *name
= NULL
;
1225 errx(EXIT_FAILURE
, _("no disk device specified"));
1229 errx(EXIT_FAILURE
, _("no partition number specified"));
1230 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1235 errx(EXIT_FAILURE
, _("unexpected arguments"));
1237 /* read-only if name not given */
1238 assign_device_partition(sf
, devname
, partno
, !name
);
1240 /* print partition name */
1242 const char *str
= NULL
;
1244 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1245 str
= fdisk_partition_get_name(pa
);
1247 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition name"),
1249 printf("%s\n", str
);
1250 fdisk_unref_partition(pa
);
1251 fdisk_deassign_device(sf
->cxt
, 1);
1256 backup_partition_table(sf
, devname
);
1258 pa
= fdisk_new_partition();
1260 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1262 if (fdisk_partition_set_name(pa
, name
) != 0 ||
1263 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1264 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition name"),
1267 fdisk_unref_partition(pa
);
1268 return write_changes(sf
);
1272 * sfdisk --part-attrs <device> <partno> [<attrs>]
1274 static int command_partattrs(struct sfdisk
*sf
, int argc
, char **argv
)
1277 struct fdisk_partition
*pa
= NULL
;
1278 const char *devname
= NULL
, *attrs
= NULL
;
1281 errx(EXIT_FAILURE
, _("no disk device specified"));
1285 errx(EXIT_FAILURE
, _("no partition number specified"));
1286 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1291 errx(EXIT_FAILURE
, _("unexpected arguments"));
1293 /* read-only if name not given */
1294 assign_device_partition(sf
, devname
, partno
, !attrs
);
1296 /* print partition name */
1298 const char *str
= NULL
;
1300 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1301 str
= fdisk_partition_get_attrs(pa
);
1303 printf("%s\n", str
);
1304 fdisk_unref_partition(pa
);
1305 fdisk_deassign_device(sf
->cxt
, 1);
1310 backup_partition_table(sf
, devname
);
1312 pa
= fdisk_new_partition();
1314 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1316 if (fdisk_partition_set_attrs(pa
, attrs
) != 0 ||
1317 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1318 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition attributes"),
1321 fdisk_unref_partition(pa
);
1322 return write_changes(sf
);
1325 static void sfdisk_print_partition(struct sfdisk
*sf
, size_t n
)
1327 struct fdisk_partition
*pa
= NULL
;
1334 if (fdisk_get_partition(sf
->cxt
, n
, &pa
) != 0)
1337 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_DEVICE
, &data
);
1338 printf("%12s : ", data
);
1340 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_START
, &data
);
1341 printf("%12s ", data
);
1343 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_END
, &data
);
1344 printf("%12s ", data
);
1346 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_SIZE
, &data
);
1347 printf("(%s) ", data
);
1349 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_TYPE
, &data
);
1350 printf("%s\n", data
);
1352 fdisk_unref_partition(pa
);
1355 static void command_fdisk_help(void)
1357 fputs(_("\nHelp:\n"), stdout
);
1359 fputc('\n', stdout
);
1360 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1361 fputs(_(" Commands:\n"), stdout
);
1363 fputs(_(" write write table to disk and exit\n"), stdout
);
1364 fputs(_(" quit show new situation and wait for user's feedback before write\n"), stdout
);
1365 fputs(_(" abort exit sfdisk shell\n"), stdout
);
1366 fputs(_(" print display the partition table\n"), stdout
);
1367 fputs(_(" help show this help text\n"), stdout
);
1368 fputc('\n', stdout
);
1369 fputs(_(" Ctrl-D the same as 'quit'\n"), stdout
);
1371 fputc('\n', stdout
);
1372 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1373 fputs(_(" Input format:\n"), stdout
);
1375 fputs(_(" <start>, <size>, <type>, <bootable>\n"), stdout
);
1377 fputc('\n', stdout
);
1378 fputs(_(" <start> Beginning of the partition in sectors, or bytes if\n"
1379 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1380 " The default is the first free space.\n"), stdout
);
1382 fputc('\n', stdout
);
1383 fputs(_(" <size> Size of the partition in sectors, or bytes if\n"
1384 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1385 " The default is all available space.\n"), stdout
);
1387 fputc('\n', stdout
);
1388 fputs(_(" <type> The partition type. Default is a Linux data partition.\n"), stdout
);
1389 fputs(_(" MBR: hex or L,S,E,X,U,R,V shortcuts.\n"), stdout
);
1390 fputs(_(" GPT: UUID or L,S,H,U,R,V shortcuts.\n"), stdout
);
1392 fputc('\n', stdout
);
1393 fputs(_(" <bootable> Use '*' to mark an MBR partition as bootable.\n"), stdout
);
1395 fputc('\n', stdout
);
1396 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1397 fputs(_(" Example:\n"), stdout
);
1399 fputs(_(" , 4G Creates a 4GiB partition at default start offset.\n"), stdout
);
1400 fputc('\n', stdout
);
1404 SFDISK_DONE_NONE
= 0,
1411 /* returns: 0 on success, <0 on error, 1 successfully stop sfdisk */
1412 static int loop_control_commands(struct sfdisk
*sf
,
1413 struct fdisk_script
*dp
,
1416 const char *p
= skip_blank(buf
);
1417 int rc
= SFDISK_DONE_NONE
;
1419 if (strcmp(p
, "print") == 0)
1420 list_disklabel(sf
->cxt
);
1421 else if (strcmp(p
, "help") == 0)
1422 command_fdisk_help();
1423 else if (strcmp(p
, "quit") == 0)
1424 rc
= SFDISK_DONE_ASK
;
1425 else if (strcmp(p
, "write") == 0)
1426 rc
= SFDISK_DONE_WRITE
;
1427 else if (strcmp(p
, "abort") == 0)
1428 rc
= SFDISK_DONE_ABORT
;
1430 if (sf
->interactive
)
1431 fdisk_warnx(sf
->cxt
, _("unsupported command"));
1433 fdisk_warnx(sf
->cxt
, _("line %d: unsupported command"),
1434 fdisk_script_get_nlines(dp
));
1441 static int has_container(struct sfdisk
*sf
)
1444 struct fdisk_partition
*pa
= NULL
;
1447 return sf
->container
;
1449 nparts
= fdisk_get_npartitions(sf
->cxt
);
1450 for (i
= 0; i
< nparts
; i
++) {
1451 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0)
1453 if (fdisk_partition_is_container(pa
)) {
1459 fdisk_unref_partition(pa
);
1460 return sf
->container
;
1463 static size_t last_pt_partno(struct sfdisk
*sf
)
1465 size_t i
, nparts
, partno
= 0;
1466 struct fdisk_partition
*pa
= NULL
;
1469 nparts
= fdisk_get_npartitions(sf
->cxt
);
1470 for (i
= 0; i
< nparts
; i
++) {
1473 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0 ||
1474 !fdisk_partition_is_used(pa
))
1476 x
= fdisk_partition_get_partno(pa
);
1481 fdisk_unref_partition(pa
);
1485 #ifdef HAVE_LIBREADLINE
1486 static char *sfdisk_fgets(struct fdisk_script
*dp
,
1487 char *buf
, size_t bufsz
, FILE *f
)
1489 struct sfdisk
*sf
= (struct sfdisk
*) fdisk_script_get_userdata(dp
);
1495 if (sf
->interactive
) {
1496 char *p
= readline(sf
->prompt
);
1502 if (len
> bufsz
- 2)
1505 memcpy(buf
, p
, len
);
1506 buf
[len
] = '\n'; /* append \n to be compatible with libc fgetc() */
1507 buf
[len
+ 1] = '\0';
1512 return fgets(buf
, bufsz
, f
);
1516 static int ignore_partition(struct fdisk_partition
*pa
)
1518 /* incomplete partition setting */
1519 if (!fdisk_partition_has_start(pa
) && !fdisk_partition_start_is_default(pa
))
1521 if (!fdisk_partition_has_size(pa
) && !fdisk_partition_end_is_default(pa
))
1524 /* probably dump from old sfdisk with start=0 size=0 */
1525 if (fdisk_partition_has_start(pa
) && fdisk_partition_get_start(pa
) == 0 &&
1526 fdisk_partition_has_size(pa
) && fdisk_partition_get_size(pa
) == 0)
1532 static void follow_wipe_mode(struct sfdisk
*sf
)
1534 int dowipe
= sf
->wipemode
== WIPEMODE_ALWAYS
? 1 : 0;
1536 if (sf
->interactive
&& sf
->wipemode
== WIPEMODE_AUTO
)
1537 dowipe
= 1; /* do it in interactive mode */
1539 if (fdisk_is_ptcollision(sf
->cxt
) && sf
->wipemode
!= WIPEMODE_NEVER
)
1540 dowipe
= 1; /* always wipe old PT */
1542 fdisk_enable_wipe(sf
->cxt
, dowipe
);
1547 if (!fdisk_is_ptcollision(sf
->cxt
)) {
1548 fdisk_warnx(sf
->cxt
, _(
1549 "The device contains '%s' signature and it will be removed by a write command. "
1550 "See sfdisk(8) man page and --wipe option for more details."),
1551 fdisk_get_collision(sf
->cxt
));
1552 fputc('\n', stdout
);
1555 fdisk_warnx(sf
->cxt
, _(
1556 "The device contains '%s' signature and it may remain on the device. "
1557 "It is recommended to wipe the device with wipefs(8) or "
1558 "sfdisk --wipe, in order to avoid possible collisions."),
1559 fdisk_get_collision(sf
->cxt
));
1560 fputc('\n', stderr
);
1564 static int wipe_partition(struct sfdisk
*sf
, size_t partno
)
1567 char *fstype
= NULL
;
1568 struct fdisk_partition
*tmp
= NULL
;
1570 DBG(MISC
, ul_debug("checking for signature"));
1572 rc
= fdisk_get_partition(sf
->cxt
, partno
, &tmp
);
1576 rc
= fdisk_partition_to_string(tmp
, sf
->cxt
, FDISK_FIELD_FSTYPE
, &fstype
);
1577 if (rc
|| fstype
== NULL
)
1580 fdisk_warnx(sf
->cxt
, _("Partition #%zu contains a %s signature."), partno
+ 1, fstype
);
1582 if (sf
->pwipemode
== WIPEMODE_AUTO
&& isatty(STDIN_FILENO
))
1583 fdisk_ask_yesno(sf
->cxt
, _("Do you want to remove the signature?"), &yes
);
1584 else if (sf
->pwipemode
== WIPEMODE_ALWAYS
)
1588 fdisk_info(sf
->cxt
, _("The signature will be removed by a write command."));
1589 rc
= fdisk_wipe_partition(sf
->cxt
, partno
, TRUE
);
1592 fdisk_unref_partition(tmp
);
1594 DBG(MISC
, ul_debug("partition wipe check end [rc=%d]", rc
));
1598 static void refresh_prompt_buffer(struct sfdisk
*sf
, const char *devname
,
1599 size_t next_partno
, int created
)
1602 char *partname
= fdisk_partname(devname
, next_partno
+ 1);
1604 err(EXIT_FAILURE
, _("failed to allocate partition name"));
1606 if (!sf
->prompt
|| !startswith(sf
->prompt
, partname
)) {
1608 xasprintf(&sf
->prompt
,"%s: ", partname
);
1611 } else if (!sf
->prompt
|| !startswith(sf
->prompt
, SFDISK_PROMPT
)) {
1613 sf
->prompt
= xstrdup(SFDISK_PROMPT
);
1618 * sfdisk <device> [[-N] <partno>]
1620 * Note that the option -N is there for backward compatibility only.
1622 static int command_fdisk(struct sfdisk
*sf
, int argc
, char **argv
)
1624 int rc
= 0, partno
= sf
->partno
, created
= 0, unused
= 0;
1625 struct fdisk_script
*dp
;
1626 struct fdisk_table
*tb
= NULL
;
1627 const char *devname
= NULL
, *label
;
1629 size_t next_partno
= (size_t) -1;
1633 if (partno
< 0 && argc
> 1)
1634 partno
= strtou32_or_err(argv
[1],
1635 _("failed to parse partition number"));
1637 errx(EXIT_FAILURE
, _("no disk device specified"));
1639 rc
= fdisk_assign_device(sf
->cxt
, devname
, 0);
1641 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
1643 dp
= fdisk_new_script(sf
->cxt
);
1645 err(EXIT_FAILURE
, _("failed to allocate script handler"));
1646 fdisk_set_script(sf
->cxt
, dp
);
1647 #ifdef HAVE_LIBREADLINE
1648 fdisk_script_set_fgets(dp
, sfdisk_fgets
);
1650 fdisk_script_set_userdata(dp
, (void *) sf
);
1653 * Don't create a new disklabel when [-N] <partno> specified. In this
1654 * case reuse already specified disklabel. Let's check that the disk
1655 * really contains the partition.
1660 if (!fdisk_has_label(sf
->cxt
))
1661 errx(EXIT_FAILURE
, _("%s: cannot modify partition %d: "
1662 "no partition table was found"),
1663 devname
, partno
+ 1);
1664 n
= fdisk_get_npartitions(sf
->cxt
);
1665 if ((size_t) partno
> n
)
1666 errx(EXIT_FAILURE
, _("%s: cannot modify partition %d: "
1667 "partition table contains only %zu "
1669 devname
, partno
+ 1, n
);
1671 if (!fdisk_is_partition_used(sf
->cxt
, partno
)) {
1672 fdisk_warnx(sf
->cxt
, _("warning: %s: partition %d is not defined yet"),
1673 devname
, partno
+ 1);
1677 next_partno
= partno
;
1680 sf
->orig_pa
= get_partition(sf
->cxt
, partno
);
1685 next_partno
= last_pt_partno(sf
) + 1;
1688 if (!sf
->quiet
&& sf
->interactive
) {
1689 color_scheme_enable("welcome", UL_COLOR_GREEN
);
1690 fdisk_info(sf
->cxt
, _("\nWelcome to sfdisk (%s)."), PACKAGE_STRING
);
1692 fdisk_info(sf
->cxt
, _("Changes will remain in memory only, until you decide to write them.\n"
1693 "Be careful before using the write command.\n"));
1696 if (!sf
->noact
&& !sf
->noreread
) {
1698 fputs(_("Checking that no-one is using this disk right now ..."), stdout
);
1699 if (fdisk_device_is_used(sf
->cxt
)) {
1701 fputs(_(" FAILED\n\n"), stdout
);
1703 fdisk_warnx(sf
->cxt
, _(
1704 "This disk is currently in use - repartitioning is probably a bad idea.\n"
1705 "Umount all file systems, and swapoff all swap partitions on this disk.\n"
1706 "Use the --no-reread flag to suppress this check.\n"));
1709 errx(EXIT_FAILURE
, _("Use the --force flag to overrule all checks."));
1710 } else if (!sf
->quiet
)
1711 fputs(_(" OK\n\n"), stdout
);
1714 if (fdisk_get_collision(sf
->cxt
))
1715 follow_wipe_mode(sf
);
1718 backup_partition_table(sf
, devname
);
1721 list_disk_geometry(sf
->cxt
);
1722 if (fdisk_has_label(sf
->cxt
)) {
1723 fdisk_info(sf
->cxt
, _("\nOld situation:"));
1724 list_disklabel(sf
->cxt
);
1730 else if (fdisk_has_label(sf
->cxt
))
1731 label
= fdisk_label_get_name(fdisk_get_label(sf
->cxt
, NULL
));
1733 label
= "dos"; /* just for backward compatibility */
1735 if (fdisk_script_set_header(dp
, "label", label
) != 0)
1736 errx(EXIT_FAILURE
, _("failed to set script header"));
1738 if (!sf
->quiet
&& sf
->interactive
) {
1739 if (!fdisk_has_label(sf
->cxt
) && !sf
->label
)
1741 _("\nsfdisk is going to create a new '%s' disk label.\n"
1742 "Use 'label: <name>' before you define a first partition\n"
1743 "to override the default."), label
);
1744 fdisk_info(sf
->cxt
, _("\nType 'help' to get more information.\n"));
1745 } else if (!sf
->quiet
)
1746 fputc('\n', stdout
);
1748 tb
= fdisk_script_get_table(dp
);
1754 DBG(PARSE
, ul_debug("<---next-line--->"));
1755 if (next_partno
== (size_t) -1)
1756 next_partno
= fdisk_table_get_nents(tb
);
1760 && next_partno
== fdisk_get_npartitions(sf
->cxt
)
1761 && !has_container(sf
)) {
1762 fdisk_info(sf
->cxt
, _("All partitions used."));
1763 rc
= SFDISK_DONE_ASK
;
1767 refresh_prompt_buffer(sf
, devname
, next_partno
, created
);
1770 if (sf
->prompt
&& (sf
->interactive
|| !sf
->quiet
)) {
1771 #ifndef HAVE_LIBREADLINE
1772 fputs(sf
->prompt
, stdout
);
1774 if (!sf
->interactive
)
1775 fputs(sf
->prompt
, stdout
);
1779 rc
= fdisk_script_read_line(dp
, stdin
, buf
, sizeof(buf
));
1781 DBG(PARSE
, ul_debug("script parsing failed, trying sfdisk specific commands"));
1782 buf
[sizeof(buf
) - 1] = '\0';
1783 rc
= loop_control_commands(sf
, dp
, buf
);
1787 } else if (rc
== 1) {
1788 rc
= SFDISK_DONE_EOF
;
1790 fputs(_("Done.\n"), stdout
);
1794 nparts
= fdisk_table_get_nents(tb
);
1796 size_t cur_partno
= (size_t) -1;
1797 struct fdisk_partition
*pa
= fdisk_table_get_partition(tb
, nparts
- 1);
1801 if (ignore_partition(pa
)) {
1802 fdisk_info(sf
->cxt
, _("Ignoring partition."));
1806 if (!created
) { /* create a new disklabel */
1807 rc
= fdisk_apply_script_headers(sf
->cxt
, dp
);
1810 fdisk_warnx(sf
->cxt
, _(
1811 "Failed to apply script headers, "
1812 "disk label not created."));
1814 if (rc
== 0 && fdisk_get_collision(sf
->cxt
))
1815 follow_wipe_mode(sf
);
1817 if (!rc
&& partno
>= 0) { /* -N <partno>, modify partition */
1818 rc
= fdisk_set_partition(sf
->cxt
, partno
, pa
);
1819 rc
= rc
== 0 ? SFDISK_DONE_ASK
: SFDISK_DONE_ABORT
;
1821 } else if (!rc
) { /* add partition */
1822 if (!sf
->interactive
&& !sf
->quiet
&&
1823 (!sf
->prompt
|| startswith(sf
->prompt
, SFDISK_PROMPT
))) {
1824 refresh_prompt_buffer(sf
, devname
, next_partno
, created
);
1825 fputs(sf
->prompt
, stdout
);
1827 rc
= fdisk_add_partition(sf
->cxt
, pa
, &cur_partno
);
1830 fdisk_warn(sf
->cxt
, _("Failed to add #%d partition"), next_partno
+ 1);
1834 /* wipe partition on success
1836 * Note that unused=1 means -N <partno> for unused,
1837 * otherwise we wipe only newly created partitions.
1839 if (rc
== 0 && (unused
|| partno
< 0)) {
1840 rc
= wipe_partition(sf
, unused
? (size_t) partno
: cur_partno
);
1846 /* success print result */
1847 if (sf
->interactive
)
1848 sfdisk_print_partition(sf
, cur_partno
);
1849 next_partno
= cur_partno
+ 1;
1850 } else if (pa
) /* error, drop partition from script */
1851 fdisk_table_remove_partition(tb
, pa
);
1853 fdisk_info(sf
->cxt
, _("Script header accepted."));
1855 if (rc
&& !sf
->interactive
) {
1856 rc
= SFDISK_DONE_ABORT
;
1861 /* create empty disk label if label, but no partition specified */
1862 if ((rc
== SFDISK_DONE_EOF
|| rc
== SFDISK_DONE_WRITE
) && created
== 0
1863 && fdisk_script_has_force_label(dp
) == 1
1864 && fdisk_table_get_nents(tb
) == 0
1865 && fdisk_script_get_header(dp
, "label")) {
1867 int xrc
= fdisk_apply_script_headers(sf
->cxt
, dp
);
1870 fdisk_warnx(sf
->cxt
, _(
1871 "Failed to apply script headers, "
1872 "disk label not created."));
1873 rc
= SFDISK_DONE_ABORT
;
1877 if (!sf
->quiet
&& rc
!= SFDISK_DONE_ABORT
) {
1878 fdisk_info(sf
->cxt
, _("\nNew situation:"));
1879 list_disk_identifier(sf
->cxt
);
1880 list_disklabel(sf
->cxt
);
1884 case SFDISK_DONE_ASK
:
1885 case SFDISK_DONE_EOF
:
1886 if (sf
->interactive
) {
1888 fdisk_ask_yesno(sf
->cxt
, _("Do you want to write this to disk?"), &yes
);
1890 fdisk_info(sf
->cxt
, _("Leaving."));
1896 case SFDISK_DONE_WRITE
:
1897 rc
= write_changes(sf
);
1899 case SFDISK_DONE_ABORT
:
1900 default: /* rc < 0 on error */
1901 fdisk_info(sf
->cxt
, _("Leaving.\n"));
1905 fdisk_unref_script(dp
);
1909 static void __attribute__((__noreturn__
)) usage(void)
1912 fputs(USAGE_HEADER
, out
);
1915 _(" %1$s [options] <dev> [[-N] <part>]\n"
1916 " %1$s [options] <command>\n"), program_invocation_short_name
);
1918 fputs(USAGE_SEPARATOR
, out
);
1919 fputs(_("Display or manipulate a disk partition table.\n"), out
);
1921 fputs(USAGE_COMMANDS
, out
);
1922 fputs(_(" -A, --activate <dev> [<part> ...] list or set bootable (P)MBR partitions\n"), out
);
1923 fputs(_(" -d, --dump <dev> dump partition table (usable for later input)\n"), out
);
1924 fputs(_(" -J, --json <dev> dump partition table in JSON format\n"), out
);
1925 fputs(_(" -g, --show-geometry [<dev> ...] list geometry of all or specified devices\n"), out
);
1926 fputs(_(" -l, --list [<dev> ...] list partitions of each device\n"), out
);
1927 fputs(_(" -F, --list-free [<dev> ...] list unpartitioned free areas of each device\n"), out
);
1928 fputs(_(" -r, --reorder <dev> fix partitions order (by start offset)\n"), out
);
1929 fputs(_(" -s, --show-size [<dev> ...] list sizes of all or specified devices\n"), out
);
1930 fputs(_(" -T, --list-types print the recognized types (see -X)\n"), out
);
1931 fputs(_(" -V, --verify [<dev> ...] test whether partitions seem correct\n"), out
);
1932 fputs(_(" --delete <dev> [<part> ...] delete all or specified partitions\n"), out
);
1934 fputs(USAGE_SEPARATOR
, out
);
1935 fputs(_(" --part-label <dev> <part> [<str>] print or change partition label\n"), out
);
1936 fputs(_(" --part-type <dev> <part> [<type>] print or change partition type\n"), out
);
1937 fputs(_(" --part-uuid <dev> <part> [<uuid>] print or change partition uuid\n"), out
);
1938 fputs(_(" --part-attrs <dev> <part> [<str>] print or change partition attributes\n"), out
);
1940 fputs(USAGE_SEPARATOR
, out
);
1941 fputs(_(" <dev> device (usually disk) path\n"), out
);
1942 fputs(_(" <part> partition number\n"), out
);
1943 fputs(_(" <type> partition type, GUID for GPT, hex for MBR\n"), out
);
1945 fputs(USAGE_OPTIONS
, out
);
1946 fputs(_(" -a, --append append partitions to existing partition table\n"), out
);
1947 fputs(_(" -b, --backup backup partition table sectors (see -O)\n"), out
);
1948 fputs(_(" --bytes print SIZE in bytes rather than in human readable format\n"), out
);
1949 fputs(_(" --move-data[=<typescript>] move partition data after relocation (requires -N)\n"), out
);
1950 fputs(_(" --move-use-fsync use fsync after each write when move data\n"), out
);
1951 fputs(_(" -f, --force disable all consistency checking\n"), out
);
1953 _(" --color[=<when>] colorize output (%s, %s or %s)\n"), "auto", "always", "never");
1955 " %s\n", USAGE_COLORS_DEFAULT
);
1956 fputs(_(" -N, --partno <num> specify partition number\n"), out
);
1957 fputs(_(" -n, --no-act do everything except write to device\n"), out
);
1958 fputs(_(" --no-reread do not check whether the device is in use\n"), out
);
1959 fputs(_(" --no-tell-kernel do not tell kernel about changes\n"), out
);
1960 fputs(_(" -O, --backup-file <path> override default backup file name\n"), out
);
1961 fputs(_(" -o, --output <list> output columns\n"), out
);
1962 fputs(_(" -q, --quiet suppress extra info messages\n"), out
);
1964 _(" -w, --wipe <mode> wipe signatures (%s, %s or %s)\n"), "auto", "always", "never");
1966 _(" -W, --wipe-partitions <mode> wipe signatures from new partitions (%s, %s or %s)\n"), "auto", "always", "never");
1967 fputs(_(" -X, --label <name> specify label type (dos, gpt, ...)\n"), out
);
1968 fputs(_(" -Y, --label-nested <name> specify nested label type (dos, bsd)\n"), out
);
1969 fputs(USAGE_SEPARATOR
, out
);
1970 fputs(_(" -G, --show-pt-geometry deprecated, alias to --show-geometry\n"), out
);
1971 fputs(_(" -L, --Linux deprecated, only for backward compatibility\n"), out
);
1972 fputs(_(" -u, --unit S deprecated, only sector unit is supported\n"), out
);
1974 fputs(USAGE_SEPARATOR
, out
);
1975 printf( " -h, --help %s\n", USAGE_OPTSTR_HELP
);
1976 printf( " -v, --version %s\n", USAGE_OPTSTR_VERSION
);
1978 list_available_columns(out
);
1980 printf(USAGE_MAN_TAIL("sfdisk(8)"));
1985 int main(int argc
, char *argv
[])
1987 const char *outarg
= NULL
;
1988 int rc
= -EINVAL
, c
, longidx
= -1, bytes
= 0;
1989 int colormode
= UL_COLORMODE_UNDEF
;
1990 struct sfdisk _sf
= {
1992 .wipemode
= WIPEMODE_AUTO
,
1993 .pwipemode
= WIPEMODE_AUTO
,
1994 .interactive
= isatty(STDIN_FILENO
) ? 1 : 0,
1998 OPT_CHANGE_ID
= CHAR_MAX
+ 1,
2014 static const struct option longopts
[] = {
2015 { "activate",no_argument
, NULL
, 'A' },
2016 { "append", no_argument
, NULL
, 'a' },
2017 { "backup", no_argument
, NULL
, 'b' },
2018 { "backup-file", required_argument
, NULL
, 'O' },
2019 { "bytes", no_argument
, NULL
, OPT_BYTES
},
2020 { "color", optional_argument
, NULL
, OPT_COLOR
},
2021 { "delete", no_argument
, NULL
, OPT_DELETE
},
2022 { "dump", no_argument
, NULL
, 'd' },
2023 { "help", no_argument
, NULL
, 'h' },
2024 { "force", no_argument
, NULL
, 'f' },
2025 { "json", no_argument
, NULL
, 'J' },
2026 { "label", required_argument
, NULL
, 'X' },
2027 { "label-nested", required_argument
, NULL
, 'Y' },
2028 { "list", no_argument
, NULL
, 'l' },
2029 { "list-free", no_argument
, NULL
, 'F' },
2030 { "list-types", no_argument
, NULL
, 'T' },
2031 { "no-act", no_argument
, NULL
, 'n' },
2032 { "no-reread", no_argument
, NULL
, OPT_NOREREAD
},
2033 { "no-tell-kernel", no_argument
, NULL
, OPT_NOTELL
},
2034 { "move-data", optional_argument
, NULL
, OPT_MOVEDATA
},
2035 { "move-use-fsync", no_argument
, NULL
, OPT_MOVEFSYNC
},
2036 { "output", required_argument
, NULL
, 'o' },
2037 { "partno", required_argument
, NULL
, 'N' },
2038 { "reorder", no_argument
, NULL
, 'r' },
2039 { "show-geometry", no_argument
, NULL
, 'g' },
2040 { "quiet", no_argument
, NULL
, 'q' },
2041 { "verify", no_argument
, NULL
, 'V' },
2042 { "version", no_argument
, NULL
, 'v' },
2043 { "wipe", required_argument
, NULL
, 'w' },
2044 { "wipe-partitions", required_argument
, NULL
, 'W' },
2046 { "part-uuid", no_argument
, NULL
, OPT_PARTUUID
},
2047 { "part-label", no_argument
, NULL
, OPT_PARTLABEL
},
2048 { "part-type", no_argument
, NULL
, OPT_PARTTYPE
},
2049 { "part-attrs", no_argument
, NULL
, OPT_PARTATTRS
},
2051 { "show-pt-geometry", no_argument
, NULL
, 'G' }, /* deprecated */
2052 { "unit", required_argument
, NULL
, 'u' }, /* deprecated */
2053 { "Linux", no_argument
, NULL
, 'L' }, /* deprecated */
2054 { "show-size", no_argument
, NULL
, 's' }, /* deprecated */
2056 { "change-id",no_argument
, NULL
, OPT_CHANGE_ID
}, /* deprecated */
2057 { "id", no_argument
, NULL
, 'c' }, /* deprecated */
2058 { "print-id",no_argument
, NULL
, OPT_PRINT_ID
}, /* deprecated */
2060 { NULL
, 0, NULL
, 0 },
2062 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
2063 { 'F','J','d'}, /* --list-free --json --dump */
2064 { 's','u'}, /* --show-size --unit */
2067 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
2070 setlocale(LC_ALL
, "");
2071 bindtextdomain(PACKAGE
, LOCALEDIR
);
2072 textdomain(PACKAGE
);
2073 close_stdout_atexit();
2075 while ((c
= getopt_long(argc
, argv
, "aAbcdfFgGhJlLo:O:nN:qrsTu:vVX:Y:w:W:",
2076 longopts
, &longidx
)) != -1) {
2078 err_exclusive_options(c
, longopts
, excl
, excl_st
);
2082 sf
->act
= ACT_ACTIVATE
;
2093 warnx(_("%s is deprecated in favour of --part-type"),
2094 longopts
[longidx
].name
);
2095 sf
->act
= ACT_PARTTYPE
;
2098 warnx(_("--id is deprecated in favour of --part-type"));
2099 sf
->act
= ACT_PARTTYPE
;
2108 sf
->act
= ACT_LIST_FREE
;
2114 warnx(_("--show-pt-geometry is no more implemented. Using --show-geometry."));
2117 sf
->act
= ACT_SHOW_GEOM
;
2126 warnx(_("--Linux option is unnecessary and deprecated"));
2133 sf
->backup_file
= optarg
;
2139 sf
->partno
= strtou32_or_err(optarg
, _("failed to parse partition number")) - 1;
2145 sf
->act
= ACT_REORDER
;
2148 sf
->act
= ACT_SHOW_SIZE
;
2151 sf
->act
= ACT_LIST_TYPES
;
2155 errx(EXIT_FAILURE
, _("unsupported unit '%c'"), *optarg
);
2158 print_version(EXIT_SUCCESS
);
2163 sf
->wipemode
= wipemode_from_string(optarg
);
2164 if (sf
->wipemode
< 0)
2165 errx(EXIT_FAILURE
, _("unsupported wipe mode"));
2168 sf
->pwipemode
= wipemode_from_string(optarg
);
2169 if (sf
->pwipemode
< 0)
2170 errx(EXIT_FAILURE
, _("unsupported wipe mode"));
2176 sf
->label_nested
= optarg
;
2180 sf
->act
= ACT_PARTUUID
;
2183 sf
->act
= ACT_PARTTYPE
;
2186 sf
->act
= ACT_PARTLABEL
;
2189 sf
->act
= ACT_PARTATTRS
;
2198 colormode
= UL_COLORMODE_AUTO
;
2200 colormode
= colormode_or_err(optarg
,
2201 _("unsupported color mode"));
2205 sf
->move_typescript
= optarg
;
2211 sf
->act
= ACT_DELETE
;
2217 errtryhelp(EXIT_FAILURE
);
2221 colors_init(colormode
, "sfdisk");
2225 fdisk_set_size_unit(sf
->cxt
, FDISK_SIZEUNIT_BYTES
);
2228 init_fields(NULL
, outarg
, NULL
);
2230 if (sf
->verify
&& !sf
->act
)
2231 sf
->act
= ACT_VERIFY
; /* --verify make be used with --list too */
2233 sf
->act
= ACT_FDISK
; /* default */
2235 if (sf
->movedata
&& !(sf
->act
== ACT_FDISK
&& sf
->partno
>= 0))
2236 errx(EXIT_FAILURE
, _("--movedata requires -N"));
2240 rc
= command_activate(sf
, argc
- optind
, argv
+ optind
);
2244 rc
= command_delete(sf
, argc
- optind
, argv
+ optind
);
2248 rc
= command_list_partitions(sf
, argc
- optind
, argv
+ optind
);
2251 case ACT_LIST_TYPES
:
2252 rc
= command_list_types(sf
);
2256 rc
= command_list_freespace(sf
, argc
- optind
, argv
+ optind
);
2260 rc
= command_fdisk(sf
, argc
- optind
, argv
+ optind
);
2264 rc
= command_dump(sf
, argc
- optind
, argv
+ optind
);
2268 rc
= command_show_size(sf
, argc
- optind
, argv
+ optind
);
2272 rc
= command_show_geometry(sf
, argc
- optind
, argv
+ optind
);
2276 rc
= command_verify(sf
, argc
- optind
, argv
+ optind
);
2280 rc
= command_parttype(sf
, argc
- optind
, argv
+ optind
);
2284 rc
= command_partuuid(sf
, argc
- optind
, argv
+ optind
);
2288 rc
= command_partlabel(sf
, argc
- optind
, argv
+ optind
);
2292 rc
= command_partattrs(sf
, argc
- optind
, argv
+ optind
);
2296 rc
= command_reorder(sf
, argc
- optind
, argv
+ optind
);
2302 DBG(MISC
, ul_debug("bye! [rc=%d]", rc
));
2303 return rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;