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>
46 #include "closestream.h"
54 #include "fdisk-list.h"
57 * sfdisk debug stuff (see fdisk.h and include/debug.h)
59 static UL_DEBUG_DEFINE_MASK(sfdisk
);
60 UL_DEBUG_DEFINE_MASKNAMES(sfdisk
) = UL_DEBUG_EMPTY_MASKNAMES
;
62 #define SFDISKPROG_DEBUG_INIT (1 << 1)
63 #define SFDISKPROG_DEBUG_PARSE (1 << 2)
64 #define SFDISKPROG_DEBUG_MISC (1 << 3)
65 #define SFDISKPROG_DEBUG_ASK (1 << 4)
66 #define SFDISKPROG_DEBUG_ALL 0xFFFF
68 #define DBG(m, x) __UL_DBG(sfdisk, SFDISKPROG_DEBUG_, m, x)
69 #define ON_DBG(m, x) __UL_DBG_CALL(sfdisk, SFDISKPROG_DEBUG_, m, x)
92 int partno
; /* -N <partno>, default -1 */
93 int wipemode
; /* remove foreign signatures from disk */
94 int pwipemode
; /* remove foreign signatures from partitions */
95 const char *label
; /* --label <label> */
96 const char *label_nested
; /* --label-nested <label> */
97 const char *backup_file
; /* -O <path> */
98 const char *move_typescript
; /* --movedata <typescript> */
101 struct fdisk_context
*cxt
; /* libfdisk context */
102 struct fdisk_partition
*orig_pa
; /* -N <partno> before the change */
104 unsigned int verify
: 1, /* call fdisk_verify_disklabel() */
105 quiet
: 1, /* suppress extra messages */
106 interactive
: 1, /* running on tty */
107 noreread
: 1, /* don't check device is in use */
108 force
: 1, /* do also stupid things */
109 backup
: 1, /* backup sectors before write PT */
110 container
: 1, /* PT contains container (MBR extended) partitions */
111 append
: 1, /* don't create new PT, append partitions only */
112 json
: 1, /* JSON dump */
113 movedata
: 1, /* move data after resize */
114 notell
: 1, /* don't tell kernel aout new PT */
115 noact
: 1; /* do not write to device */
118 #define SFDISK_PROMPT ">>> "
120 static void sfdiskprog_init_debug(void)
122 __UL_INIT_DEBUG_FROM_ENV(sfdisk
, SFDISKPROG_DEBUG_
, 0, SFDISK_DEBUG
);
126 static int get_user_reply(const char *prompt
, char *buf
, size_t bufsz
)
131 #ifdef HAVE_LIBREADLINE
132 if (isatty(STDIN_FILENO
)) {
133 p
= readline(prompt
);
136 xstrncpy(buf
, p
, bufsz
);
141 fputs(prompt
, stdout
);
144 if (!fgets(buf
, bufsz
, stdin
))
148 for (p
= buf
; *p
&& !isgraph(*p
); p
++); /* get first non-blank */
151 memmove(buf
, p
, p
- buf
); /* remove blank space */
153 if (sz
&& *(buf
+ sz
- 1) == '\n')
154 *(buf
+ sz
- 1) = '\0';
156 DBG(ASK
, ul_debug("user's reply: >>>%s<<<", buf
));
160 static int ask_callback(struct fdisk_context
*cxt
__attribute__((__unused__
)),
161 struct fdisk_ask
*ask
,
164 struct sfdisk
*sf
= (struct sfdisk
*) data
;
169 switch(fdisk_ask_get_type(ask
)) {
170 case FDISK_ASKTYPE_INFO
:
173 fputs(fdisk_ask_print_get_mesg(ask
), stdout
);
176 case FDISK_ASKTYPE_WARNX
:
178 color_scheme_fenable("warn", UL_COLOR_RED
, stderr
);
179 fputs(fdisk_ask_print_get_mesg(ask
), stderr
);
180 color_fdisable(stderr
);
183 case FDISK_ASKTYPE_WARN
:
185 color_scheme_fenable("warn", UL_COLOR_RED
, stderr
);
186 fputs(fdisk_ask_print_get_mesg(ask
), stderr
);
187 errno
= fdisk_ask_print_get_errno(ask
);
188 fprintf(stderr
, ": %m\n");
189 color_fdisable(stderr
);
191 case FDISK_ASKTYPE_YESNO
:
193 char buf
[BUFSIZ
] = { '\0' };
197 fputs(fdisk_ask_get_query(ask
), stdout
);
198 rc
= get_user_reply(_(" [Y]es/[N]o: "), buf
, sizeof(buf
));
202 if (x
== RPMATCH_YES
|| x
== RPMATCH_NO
) {
203 fdisk_ask_yesno_set_result(ask
, x
);
207 DBG(ASK
, ul_debug("yes-no ask: reply '%s' [rc=%d]", buf
, rc
));
216 static void sfdisk_init(struct sfdisk
*sf
)
220 sfdiskprog_init_debug();
222 sf
->cxt
= fdisk_new_context();
224 err(EXIT_FAILURE
, _("failed to allocate libfdisk context"));
225 fdisk_set_ask(sf
->cxt
, ask_callback
, (void *) sf
);
226 fdisk_enable_bootbits_protection(sf
->cxt
, 1);
228 if (sf
->label_nested
) {
229 struct fdisk_context
*x
= fdisk_new_nested_context(sf
->cxt
,
232 err(EXIT_FAILURE
, _("failed to allocate nested libfdisk context"));
233 /* the original context is available by fdisk_get_parent() */
238 static int sfdisk_deinit(struct sfdisk
*sf
)
240 struct fdisk_context
*parent
;
245 parent
= fdisk_get_parent(sf
->cxt
);
247 fdisk_unref_context(sf
->cxt
);
251 fdisk_unref_context(sf
->cxt
);
254 memset(sf
, 0, sizeof(*sf
));
258 static struct fdisk_partition
*get_partition(struct fdisk_context
*cxt
, size_t partno
)
260 struct fdisk_table
*tb
= NULL
;
261 struct fdisk_partition
*pa
;
263 if (fdisk_get_partitions(cxt
, &tb
) != 0)
266 pa
= fdisk_table_get_partition_by_partno(tb
, partno
);
268 fdisk_ref_partition(pa
);
269 fdisk_unref_table(tb
);
273 static void backup_sectors(struct sfdisk
*sf
,
277 uint64_t offset
, size_t size
)
282 devfd
= fdisk_get_devfd(sf
->cxt
);
285 xasprintf(&fname
, "%s0x%08"PRIx64
".bak", tpl
, offset
);
287 fd
= open(fname
, O_CREAT
| O_WRONLY
, S_IRUSR
| S_IWUSR
);
291 if (lseek(devfd
, (off_t
) offset
, SEEK_SET
) == (off_t
) -1) {
292 fdisk_warn(sf
->cxt
, _("cannot seek %s"), devname
);
295 unsigned char *buf
= xmalloc(size
);
297 if (read_all(devfd
, (char *) buf
, size
) != (ssize_t
) size
) {
298 fdisk_warn(sf
->cxt
, _("cannot read %s"), devname
);
302 if (write_all(fd
, buf
, size
) != 0) {
303 fdisk_warn(sf
->cxt
, _("cannot write %s"), fname
);
310 fdisk_info(sf
->cxt
, _("%12s (offset %5ju, size %5ju): %s"),
311 name
, (uintmax_t) offset
, (uintmax_t) size
, fname
);
316 errx(EXIT_FAILURE
, _("%s: failed to create a backup"), devname
);
319 static char *mk_backup_filename_tpl(const char *filename
, const char *devname
, const char *suffix
)
322 char *name
, *buf
= xstrdup(devname
);
324 name
= basename(buf
);
327 const char *home
= getenv ("HOME");
329 errx(EXIT_FAILURE
, _("failed to create a backup file, $HOME undefined"));
330 xasprintf(&tpl
, "%s/sfdisk-%s%s", home
, name
, suffix
);
332 xasprintf(&tpl
, "%s-%s%s", filename
, name
, suffix
);
339 static void backup_partition_table(struct sfdisk
*sf
, const char *devname
)
349 if (!fdisk_has_label(sf
->cxt
))
352 tpl
= mk_backup_filename_tpl(sf
->backup_file
, devname
, "-");
354 color_scheme_enable("header", UL_COLOR_BOLD
);
355 fdisk_info(sf
->cxt
, _("Backup files:"));
358 while (fdisk_locate_disklabel(sf
->cxt
, i
++, &name
, &offset
, &size
) == 0 && size
)
359 backup_sectors(sf
, tpl
, name
, devname
, offset
, size
);
366 static int move_partition_data(struct sfdisk
*sf
, size_t partno
, struct fdisk_partition
*orig_pa
)
368 struct fdisk_partition
*pa
= get_partition(sf
->cxt
, partno
);
369 char *devname
= NULL
, *typescript
= NULL
, *buf
= NULL
;
371 int ok
= 0, fd
, backward
= 0;
372 fdisk_sector_t nsectors
, from
, to
, step
, i
;
373 size_t ss
, step_bytes
, cc
;
377 assert(sf
->movedata
);
380 warnx(_("failed to read new partition from device; ignoring --move-data"));
381 else if (!fdisk_partition_has_size(pa
))
382 warnx(_("failed to get size of the new partition; ignoring --move-data"));
383 else if (!fdisk_partition_has_start(pa
))
384 warnx(_("failed to get start of the new partition; ignoring --move-data"));
385 else if (!fdisk_partition_has_size(orig_pa
))
386 warnx(_("failed to get size of the old partition; ignoring --move-data"));
387 else if (!fdisk_partition_has_start(orig_pa
))
388 warnx(_("failed to get start of the old partition; ignoring --move-data"));
389 else if (fdisk_partition_get_start(pa
) == fdisk_partition_get_start(orig_pa
))
390 warnx(_("start of the partition has not been moved; ignoring --move-data"));
391 else if (fdisk_partition_get_size(orig_pa
) < fdisk_partition_get_size(pa
))
392 warnx(_("new partition is smaller than original; ignoring --move-data"));
398 DBG(MISC
, ul_debug("moving data"));
400 fd
= fdisk_get_devfd(sf
->cxt
);
402 ss
= fdisk_get_sector_size(sf
->cxt
);
403 nsectors
= fdisk_partition_get_size(orig_pa
);
404 from
= fdisk_partition_get_start(orig_pa
);
405 to
= fdisk_partition_get_start(pa
);
407 if ((to
>= from
&& from
+ nsectors
>= to
) ||
408 (from
>= to
&& to
+ nsectors
>= from
)) {
409 /* source and target overlay, check if we need to copy
410 * backwardly from end of the source */
411 DBG(MISC
, ul_debug("overlay between source and target"));
412 backward
= from
< to
;
413 DBG(MISC
, ul_debug(" copy order: %s", backward
? "backward" : "forward"));
415 step
= from
> to
? from
- to
: to
- from
;
421 /* make step usable for malloc() */
422 if (step
* ss
> (getpagesize() * 256U))
423 step
= (getpagesize() * 256) / ss
;
425 /* align the step (note that nsectors does not have to be power of 2) */
426 while (nsectors
% step
)
429 step_bytes
= step
* ss
;
430 DBG(MISC
, ul_debug(" step: %ju (%zu bytes)", (uintmax_t)step
, step_bytes
));
432 #if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
434 posix_fadvise(fd
, from
* ss
, nsectors
* ss
, POSIX_FADV_SEQUENTIAL
);
436 devname
= fdisk_partname(fdisk_get_devname(sf
->cxt
), partno
+1);
437 typescript
= mk_backup_filename_tpl(sf
->move_typescript
, devname
, ".move");
440 fdisk_info(sf
->cxt
,"");
441 color_scheme_enable("header", UL_COLOR_BOLD
);
442 fdisk_info(sf
->cxt
, _("Data move:"));
444 fdisk_info(sf
->cxt
, _(" typescript file: %s"), typescript
);
445 printf(_(" old start: %ju, new start: %ju (move %ju sectors)\n"),
446 (uintmax_t) from
, (uintmax_t) to
, (uintmax_t) nsectors
);
450 if (sf
->interactive
) {
452 fdisk_ask_yesno(sf
->cxt
, _("Do you want to move partition data?"), &yes
);
454 fdisk_info(sf
->cxt
, _("Leaving."));
459 f
= fopen(typescript
, "w");
463 /* don't translate */
464 fprintf(f
, "# sfdisk: " PACKAGE_STRING
"\n");
465 fprintf(f
, "# Disk: %s\n", devname
);
466 fprintf(f
, "# Partition: %zu\n", partno
+ 1);
467 fprintf(f
, "# Operation: move data\n");
468 fprintf(f
, "# Original start offset (sectors/bytes): %ju/%ju\n",
469 (uintmax_t)from
, (uintmax_t)from
* ss
);
470 fprintf(f
, "# New start offset (sectors/bytes): %ju/%ju\n",
471 (uintmax_t)to
, (uintmax_t)to
* ss
);
472 fprintf(f
, "# Area size (sectors/bytes): %ju/%ju\n",
473 (uintmax_t)nsectors
, (uintmax_t)nsectors
* ss
);
474 fprintf(f
, "# Sector size: %zu\n", ss
);
475 fprintf(f
, "# Step size (in bytes): %zu\n", step_bytes
);
476 fprintf(f
, "# Steps: %ju\n", (uintmax_t)(nsectors
/ step
));
478 fprintf(f
, "# <step>: <from> <to> (step offsets in bytes)\n");
480 src
= (backward
? from
+ nsectors
: from
) * ss
;
481 dst
= (backward
? to
+ nsectors
: to
) * ss
;
482 buf
= xmalloc(step_bytes
);
484 DBG(MISC
, ul_debug(" initial: src=%ju dst=%ju", src
, dst
));
486 for (cc
= 1, i
= 0; i
< nsectors
; i
+= step
, cc
++) {
490 src
-= step_bytes
, dst
-= step_bytes
;
492 DBG(MISC
, ul_debug("#%05zu: src=%ju dst=%ju", cc
, src
, dst
));
495 if (lseek(fd
, src
, SEEK_SET
) == (off_t
) -1)
497 rc
= read(fd
, buf
, step_bytes
);
498 if (rc
< 0 || rc
!= (ssize_t
) step_bytes
)
502 if (lseek(fd
, dst
, SEEK_SET
) == (off_t
) -1)
504 rc
= write(fd
, buf
, step_bytes
);
505 if (rc
< 0 || rc
!= (ssize_t
) step_bytes
)
510 fprintf(f
, "%05zu: %12ju %12ju\n", cc
, src
, dst
);
512 #if defined(POSIX_FADV_DONTNEED) && defined(HAVE_POSIX_FADVISE)
513 posix_fadvise(fd
, src
, step_bytes
, POSIX_FADV_DONTNEED
);
516 src
+= step_bytes
, dst
+= step_bytes
;
527 warn(_("%s: failed to move data"), devname
);
537 static int write_changes(struct sfdisk
*sf
)
542 fdisk_info(sf
->cxt
, _("The partition table is unchanged (--no-act)."));
544 rc
= fdisk_write_disklabel(sf
->cxt
);
545 if (rc
== 0 && sf
->movedata
&& sf
->orig_pa
)
546 rc
= move_partition_data(sf
, sf
->partno
, sf
->orig_pa
);
548 fdisk_info(sf
->cxt
, _("\nThe partition table has been altered."));
550 /* Let's wait a little bit. It's possible that our
551 * system is still busy with a previous re-read
552 * ioctl (on sfdisk start) or with another task
553 * related to the write to the device.
556 fdisk_reread_partition_table(sf
->cxt
);
561 rc
= fdisk_deassign_device(sf
->cxt
,
562 sf
->noact
|| sf
->notell
); /* no-sync */
567 * sfdisk --list [<device ..]
569 static int command_list_partitions(struct sfdisk
*sf
, int argc
, char **argv
)
572 fdisk_enable_listonly(sf
->cxt
, 1);
577 for (i
= 0; i
< argc
; i
++) {
579 fputs("\n\n", stdout
);
580 if (print_device_pt(sf
->cxt
, argv
[i
], 1, sf
->verify
) != 0)
585 print_all_devices_pt(sf
->cxt
, sf
->verify
);
591 * sfdisk --list-free [<device ..]
593 static int command_list_freespace(struct sfdisk
*sf
, int argc
, char **argv
)
596 fdisk_enable_listonly(sf
->cxt
, 1);
601 for (i
= 0; i
< argc
; i
++) {
603 fputs("\n\n", stdout
);
604 if (print_device_freespace(sf
->cxt
, argv
[i
], 1) != 0)
609 print_all_devices_freespace(sf
->cxt
);
615 * sfdisk --list-types
617 static int command_list_types(struct sfdisk
*sf
)
619 const struct fdisk_parttype
*t
;
620 struct fdisk_label
*lb
;
628 name
= sf
->label
? sf
->label
: "dos";
629 lb
= fdisk_get_label(sf
->cxt
, name
);
631 errx(EXIT_FAILURE
, _("unsupported label '%s'"), name
);
633 codes
= fdisk_label_has_code_parttypes(lb
);
634 fputs(_("Id Name\n\n"), stdout
);
636 while ((t
= fdisk_label_get_parttype(lb
, i
++))) {
638 printf("%2x %s\n", fdisk_parttype_get_code(t
),
639 fdisk_parttype_get_name(t
));
641 printf("%s %s\n", fdisk_parttype_get_string(t
),
642 fdisk_parttype_get_name(t
));
648 static int verify_device(struct sfdisk
*sf
, const char *devname
)
652 fdisk_enable_listonly(sf
->cxt
, 1);
654 if (fdisk_assign_device(sf
->cxt
, devname
, 1)) {
655 warn(_("cannot open %s"), devname
);
659 color_scheme_enable("header", UL_COLOR_BOLD
);
660 fdisk_info(sf
->cxt
, "%s:", devname
);
663 if (!fdisk_has_label(sf
->cxt
))
664 fdisk_info(sf
->cxt
, _("unrecognized partition table type"));
666 rc
= fdisk_verify_disklabel(sf
->cxt
);
668 fdisk_deassign_device(sf
->cxt
, 1);
673 * sfdisk --verify [<device ..]
675 static int command_verify(struct sfdisk
*sf
, int argc
, char **argv
)
677 int nfails
= 0, ct
= 0;
681 for (i
= 0; i
< argc
; i
++) {
683 fdisk_info(sf
->cxt
, " ");
684 if (verify_device(sf
, argv
[i
]) < 0)
691 while ((dev
= next_proc_partition(&f
))) {
693 fdisk_info(sf
->cxt
, " ");
694 if (verify_device(sf
, dev
) < 0)
704 static int get_size(const char *dev
, int silent
, uintmax_t *sz
)
708 fd
= open(dev
, O_RDONLY
);
711 warn(_("cannot open %s"), dev
);
715 if (blkdev_get_sectors(fd
, (unsigned long long *) sz
) == -1) {
717 warn(_("Cannot get size of %s"), dev
);
726 * sfdisk --show-size [<device ..]
728 * (silly, but just for backward compatibility)
730 static int command_show_size(struct sfdisk
*sf
__attribute__((__unused__
)),
731 int argc
, char **argv
)
737 for (i
= 0; i
< argc
; i
++) {
738 if (get_size(argv
[i
], 0, &sz
) == 0)
739 printf("%ju\n", sz
/ 2);
746 while ((dev
= next_proc_partition(&f
))) {
747 if (get_size(dev
, 1, &sz
) == 0) {
748 printf("%s: %9ju\n", dev
, sz
/ 2);
754 printf(_("total: %ju blocks\n"), total
);
760 static int print_geom(struct sfdisk
*sf
, const char *devname
)
762 fdisk_enable_listonly(sf
->cxt
, 1);
764 if (fdisk_assign_device(sf
->cxt
, devname
, 1)) {
765 warn(_("cannot open %s"), devname
);
769 fdisk_info(sf
->cxt
, "%s: %ju cylinders, %ju heads, %ju sectors/track",
771 (uintmax_t) fdisk_get_geom_cylinders(sf
->cxt
),
772 (uintmax_t) fdisk_get_geom_heads(sf
->cxt
),
773 (uintmax_t) fdisk_get_geom_sectors(sf
->cxt
));
775 fdisk_deassign_device(sf
->cxt
, 1);
780 * sfdisk --show-geometry [<device ..]
782 static int command_show_geometry(struct sfdisk
*sf
, int argc
, char **argv
)
788 for (i
= 0; i
< argc
; i
++) {
789 if (print_geom(sf
, argv
[i
]) < 0)
796 while ((dev
= next_proc_partition(&f
))) {
797 if (print_geom(sf
, dev
) < 0)
807 * sfdisk --activate <device> [<partno> ...]
809 static int command_activate(struct sfdisk
*sf
, int argc
, char **argv
)
811 int rc
, nparts
, i
, listonly
;
812 struct fdisk_partition
*pa
= NULL
;
813 const char *devname
= NULL
;
816 errx(EXIT_FAILURE
, _("no disk device specified"));
819 /* --activate <device> */
820 listonly
= argc
== 1;
822 rc
= fdisk_assign_device(sf
->cxt
, devname
, listonly
);
824 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
826 if (fdisk_is_label(sf
->cxt
, GPT
)) {
827 if (fdisk_gpt_is_hybrid(sf
->cxt
))
828 errx(EXIT_FAILURE
, _("toggle boot flags is unsupported for Hybrid GPT/MBR"));
830 /* Switch from GPT to PMBR */
831 sf
->cxt
= fdisk_new_nested_context(sf
->cxt
, "dos");
833 err(EXIT_FAILURE
, _("cannot switch to PMBR"));
834 fdisk_info(sf
->cxt
, _("Activation is unsupported for GPT -- entering nested PMBR."));
836 } else if (!fdisk_is_label(sf
->cxt
, DOS
))
837 errx(EXIT_FAILURE
, _("toggle boot flags is supported for MBR or PMBR only"));
839 if (!listonly
&& sf
->backup
)
840 backup_partition_table(sf
, devname
);
842 nparts
= fdisk_get_npartitions(sf
->cxt
);
843 for (i
= 0; i
< nparts
; i
++) {
846 /* note that fdisk_get_partition() reuses the @pa pointer, you
847 * don't have to (re)allocate it */
848 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0)
851 /* sfdisk --activate list bootable partitions */
853 if (!fdisk_partition_is_bootable(pa
))
855 if (fdisk_partition_to_string(pa
, sf
->cxt
,
856 FDISK_FIELD_DEVICE
, &data
) == 0) {
857 printf("%s\n", data
);
861 /* deactivate all active partitions */
862 } else if (fdisk_partition_is_bootable(pa
))
863 fdisk_toggle_partition_flag(sf
->cxt
, i
, DOS_FLAG_ACTIVE
);
866 /* sfdisk --activate <partno> [..] */
867 for (i
= 1; i
< argc
; i
++) {
870 if (i
== 1 && strcmp(argv
[1], "-") == 0)
872 n
= strtou32_or_err(argv
[i
], _("failed to parse partition number"));
874 rc
= fdisk_toggle_partition_flag(sf
->cxt
, n
- 1, DOS_FLAG_ACTIVE
);
877 _("%s: partition %d: failed to toggle bootable flag"),
881 fdisk_unref_partition(pa
);
884 rc
= fdisk_deassign_device(sf
->cxt
, 1);
886 rc
= write_changes(sf
);
891 * sfdisk --delete <device> [<partno> ...]
893 static int command_delete(struct sfdisk
*sf
, int argc
, char **argv
)
896 const char *devname
= NULL
;
899 errx(EXIT_FAILURE
, _("no disk device specified"));
902 if (fdisk_assign_device(sf
->cxt
, devname
, 0) != 0)
903 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
906 backup_partition_table(sf
, devname
);
910 size_t nparts
= fdisk_get_npartitions(sf
->cxt
);
911 for (i
= 0; i
< nparts
; i
++) {
912 if (fdisk_is_partition_used(sf
->cxt
, i
) &&
913 fdisk_delete_partition(sf
->cxt
, i
) != 0)
914 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to delete"), devname
, i
+ 1);
916 /* delete specified */
918 for (i
= 1; i
< (size_t) argc
; i
++) {
919 size_t n
= strtou32_or_err(argv
[i
], _("failed to parse partition number"));
921 if (fdisk_delete_partition(sf
->cxt
, n
- 1) != 0)
922 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to delete"), devname
, n
);
926 return write_changes(sf
);
930 * sfdisk --reorder <device>
932 static int command_reorder(struct sfdisk
*sf
, int argc
, char **argv
)
934 const char *devname
= NULL
;
940 errx(EXIT_FAILURE
, _("no disk device specified"));
942 rc
= fdisk_assign_device(sf
->cxt
, devname
, 0); /* read-write */
944 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
947 backup_partition_table(sf
, devname
);
949 if (fdisk_reorder_partitions(sf
->cxt
) == 1) /* unchanged */
950 rc
= fdisk_deassign_device(sf
->cxt
, 1);
952 rc
= write_changes(sf
);
959 * sfdisk --dump <device>
961 static int command_dump(struct sfdisk
*sf
, int argc
, char **argv
)
963 const char *devname
= NULL
;
964 struct fdisk_script
*dp
;
970 errx(EXIT_FAILURE
, _("no disk device specified"));
972 rc
= fdisk_assign_device(sf
->cxt
, devname
, 1); /* read-only */
974 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
976 if (!fdisk_has_label(sf
->cxt
))
977 errx(EXIT_FAILURE
, _("%s: does not contain a recognized partition table"), devname
);
979 dp
= fdisk_new_script(sf
->cxt
);
981 err(EXIT_FAILURE
, _("failed to allocate dump struct"));
983 rc
= fdisk_script_read_context(dp
, NULL
);
985 errx(EXIT_FAILURE
, _("%s: failed to dump partition table"), devname
);
988 fdisk_script_enable_json(dp
, 1);
989 fdisk_script_write_file(dp
, stdout
);
991 fdisk_unref_script(dp
);
992 fdisk_deassign_device(sf
->cxt
, 1); /* no-sync() */
996 static void assign_device_partition(struct sfdisk
*sf
,
1003 struct fdisk_label
*lb
= NULL
;
1008 /* read-only when a new <type> undefined */
1009 rc
= fdisk_assign_device(sf
->cxt
, devname
, rdonly
);
1011 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
1013 lb
= fdisk_get_label(sf
->cxt
, NULL
);
1015 errx(EXIT_FAILURE
, _("%s: no partition table found"), devname
);
1017 n
= fdisk_get_npartitions(sf
->cxt
);
1019 errx(EXIT_FAILURE
, _("%s: partition %zu: partition table contains "
1020 "only %zu partitions"), devname
, partno
, n
);
1021 if (!fdisk_is_partition_used(sf
->cxt
, partno
- 1))
1022 errx(EXIT_FAILURE
, _("%s: partition %zu: partition is unused"),
1027 * sfdisk --part-type <device> <partno> [<type>]
1029 static int command_parttype(struct sfdisk
*sf
, int argc
, char **argv
)
1032 struct fdisk_parttype
*type
= NULL
;
1033 struct fdisk_label
*lb
;
1034 const char *devname
= NULL
, *typestr
= NULL
;
1037 errx(EXIT_FAILURE
, _("no disk device specified"));
1041 errx(EXIT_FAILURE
, _("no partition number specified"));
1042 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1047 errx(EXIT_FAILURE
, _("unexpected arguments"));
1049 /* read-only when a new <type> undefined */
1050 assign_device_partition(sf
, devname
, partno
, !typestr
);
1052 lb
= fdisk_get_label(sf
->cxt
, NULL
);
1054 /* print partition type */
1056 const struct fdisk_parttype
*t
= NULL
;
1057 struct fdisk_partition
*pa
= NULL
;
1059 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1060 t
= fdisk_partition_get_type(pa
);
1062 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition type"),
1065 if (fdisk_label_has_code_parttypes(lb
))
1066 printf("%2x\n", fdisk_parttype_get_code(t
));
1068 printf("%s\n", fdisk_parttype_get_string(t
));
1070 fdisk_unref_partition(pa
);
1071 fdisk_deassign_device(sf
->cxt
, 1);
1076 backup_partition_table(sf
, devname
);
1078 /* parse <type> and apply to PT */
1079 type
= fdisk_label_parse_parttype(lb
, typestr
);
1081 errx(EXIT_FAILURE
, _("failed to parse %s partition type '%s'"),
1082 fdisk_label_get_name(lb
), typestr
);
1084 else if (fdisk_set_partition_type(sf
->cxt
, partno
- 1, type
) != 0)
1085 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition type"),
1087 fdisk_unref_parttype(type
);
1088 return write_changes(sf
);
1092 * sfdisk --part-uuid <device> <partno> [<uuid>]
1094 static int command_partuuid(struct sfdisk
*sf
, int argc
, char **argv
)
1097 struct fdisk_partition
*pa
= NULL
;
1098 const char *devname
= NULL
, *uuid
= NULL
;
1101 errx(EXIT_FAILURE
, _("no disk device specified"));
1105 errx(EXIT_FAILURE
, _("no partition number specified"));
1106 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1111 errx(EXIT_FAILURE
, _("unexpected arguments"));
1113 /* read-only if uuid not given */
1114 assign_device_partition(sf
, devname
, partno
, !uuid
);
1116 /* print partition uuid */
1118 const char *str
= NULL
;
1120 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1121 str
= fdisk_partition_get_uuid(pa
);
1123 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition UUID"),
1125 printf("%s\n", str
);
1126 fdisk_unref_partition(pa
);
1127 fdisk_deassign_device(sf
->cxt
, 1);
1132 backup_partition_table(sf
, devname
);
1134 pa
= fdisk_new_partition();
1136 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1138 if (fdisk_partition_set_uuid(pa
, uuid
) != 0 ||
1139 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1140 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition UUID"),
1142 fdisk_unref_partition(pa
);
1143 return write_changes(sf
);
1147 * sfdisk --part-label <device> <partno> [<label>]
1149 static int command_partlabel(struct sfdisk
*sf
, int argc
, char **argv
)
1152 struct fdisk_partition
*pa
= NULL
;
1153 const char *devname
= NULL
, *name
= NULL
;
1156 errx(EXIT_FAILURE
, _("no disk device specified"));
1160 errx(EXIT_FAILURE
, _("no partition number specified"));
1161 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1166 errx(EXIT_FAILURE
, _("unexpected arguments"));
1168 /* read-only if name not given */
1169 assign_device_partition(sf
, devname
, partno
, !name
);
1171 /* print partition name */
1173 const char *str
= NULL
;
1175 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1176 str
= fdisk_partition_get_name(pa
);
1178 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition name"),
1180 printf("%s\n", str
);
1181 fdisk_unref_partition(pa
);
1182 fdisk_deassign_device(sf
->cxt
, 1);
1187 backup_partition_table(sf
, devname
);
1189 pa
= fdisk_new_partition();
1191 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1193 if (fdisk_partition_set_name(pa
, name
) != 0 ||
1194 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1195 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition name"),
1198 fdisk_unref_partition(pa
);
1199 return write_changes(sf
);
1203 * sfdisk --part-attrs <device> <partno> [<attrs>]
1205 static int command_partattrs(struct sfdisk
*sf
, int argc
, char **argv
)
1208 struct fdisk_partition
*pa
= NULL
;
1209 const char *devname
= NULL
, *attrs
= NULL
;
1212 errx(EXIT_FAILURE
, _("no disk device specified"));
1216 errx(EXIT_FAILURE
, _("no partition number specified"));
1217 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1222 errx(EXIT_FAILURE
, _("unexpected arguments"));
1224 /* read-only if name not given */
1225 assign_device_partition(sf
, devname
, partno
, !attrs
);
1227 /* print partition name */
1229 const char *str
= NULL
;
1231 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1232 str
= fdisk_partition_get_attrs(pa
);
1234 printf("%s\n", str
);
1235 fdisk_unref_partition(pa
);
1236 fdisk_deassign_device(sf
->cxt
, 1);
1241 backup_partition_table(sf
, devname
);
1243 pa
= fdisk_new_partition();
1245 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1247 if (fdisk_partition_set_attrs(pa
, attrs
) != 0 ||
1248 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1249 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition attributes"),
1252 fdisk_unref_partition(pa
);
1253 return write_changes(sf
);
1256 static void sfdisk_print_partition(struct sfdisk
*sf
, size_t n
)
1258 struct fdisk_partition
*pa
= NULL
;
1265 if (fdisk_get_partition(sf
->cxt
, n
, &pa
) != 0)
1268 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_DEVICE
, &data
);
1269 printf("%12s : ", data
);
1271 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_START
, &data
);
1272 printf("%12s ", data
);
1274 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_END
, &data
);
1275 printf("%12s ", data
);
1277 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_SIZE
, &data
);
1278 printf("(%s) ", data
);
1280 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_TYPE
, &data
);
1281 printf("%s\n", data
);
1283 fdisk_unref_partition(pa
);
1286 static void command_fdisk_help(void)
1288 fputs(_("\nHelp:\n"), stdout
);
1290 fputc('\n', stdout
);
1291 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1292 fputs(_(" Commands:\n"), stdout
);
1294 fputs(_(" write write table to disk and exit\n"), stdout
);
1295 fputs(_(" quit show new situation and wait for user's feedback before write\n"), stdout
);
1296 fputs(_(" abort exit sfdisk shell\n"), stdout
);
1297 fputs(_(" print display the partition table\n"), stdout
);
1298 fputs(_(" help show this help text\n"), stdout
);
1299 fputc('\n', stdout
);
1300 fputs(_(" Ctrl-D the same as 'quit'\n"), stdout
);
1302 fputc('\n', stdout
);
1303 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1304 fputs(_(" Input format:\n"), stdout
);
1306 fputs(_(" <start>, <size>, <type>, <bootable>\n"), stdout
);
1308 fputc('\n', stdout
);
1309 fputs(_(" <start> Beginning of the partition in sectors, or bytes if\n"
1310 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1311 " The default is the first free space.\n"), stdout
);
1313 fputc('\n', stdout
);
1314 fputs(_(" <size> Size of the partition in sectors, or bytes if\n"
1315 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1316 " The default is all available space.\n"), stdout
);
1318 fputc('\n', stdout
);
1319 fputs(_(" <type> The partition type. Default is a Linux data partition.\n"), stdout
);
1320 fputs(_(" MBR: hex or L,S,E,X,U,R,V shortcuts.\n"), stdout
);
1321 fputs(_(" GPT: UUID or L,S,H,U,R,V shortcuts.\n"), stdout
);
1323 fputc('\n', stdout
);
1324 fputs(_(" <bootable> Use '*' to mark an MBR partition as bootable.\n"), stdout
);
1326 fputc('\n', stdout
);
1327 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1328 fputs(_(" Example:\n"), stdout
);
1330 fputs(_(" , 4G Creates a 4GiB partition at default start offset.\n"), stdout
);
1331 fputc('\n', stdout
);
1335 SFDISK_DONE_NONE
= 0,
1342 /* returns: 0 on success, <0 on error, 1 successfully stop sfdisk */
1343 static int loop_control_commands(struct sfdisk
*sf
,
1344 struct fdisk_script
*dp
,
1347 const char *p
= skip_blank(buf
);
1348 int rc
= SFDISK_DONE_NONE
;
1350 if (strcmp(p
, "print") == 0)
1351 list_disklabel(sf
->cxt
);
1352 else if (strcmp(p
, "help") == 0)
1353 command_fdisk_help();
1354 else if (strcmp(p
, "quit") == 0)
1355 rc
= SFDISK_DONE_ASK
;
1356 else if (strcmp(p
, "write") == 0)
1357 rc
= SFDISK_DONE_WRITE
;
1358 else if (strcmp(p
, "abort") == 0)
1359 rc
= SFDISK_DONE_ABORT
;
1361 if (sf
->interactive
)
1362 fdisk_warnx(sf
->cxt
, _("unsupported command"));
1364 fdisk_warnx(sf
->cxt
, _("line %d: unsupported command"),
1365 fdisk_script_get_nlines(dp
));
1372 static int has_container(struct sfdisk
*sf
)
1375 struct fdisk_partition
*pa
= NULL
;
1378 return sf
->container
;
1380 nparts
= fdisk_get_npartitions(sf
->cxt
);
1381 for (i
= 0; i
< nparts
; i
++) {
1382 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0)
1384 if (fdisk_partition_is_container(pa
)) {
1390 fdisk_unref_partition(pa
);
1391 return sf
->container
;
1394 static size_t last_pt_partno(struct sfdisk
*sf
)
1396 size_t i
, nparts
, partno
= 0;
1397 struct fdisk_partition
*pa
= NULL
;
1400 nparts
= fdisk_get_npartitions(sf
->cxt
);
1401 for (i
= 0; i
< nparts
; i
++) {
1404 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0 ||
1405 !fdisk_partition_is_used(pa
))
1407 x
= fdisk_partition_get_partno(pa
);
1412 fdisk_unref_partition(pa
);
1416 #ifdef HAVE_LIBREADLINE
1417 static char *sfdisk_fgets(struct fdisk_script
*dp
,
1418 char *buf
, size_t bufsz
, FILE *f
)
1420 struct sfdisk
*sf
= (struct sfdisk
*) fdisk_script_get_userdata(dp
);
1426 if (sf
->interactive
) {
1427 char *p
= readline(sf
->prompt
);
1433 if (len
> bufsz
- 2)
1436 memcpy(buf
, p
, len
);
1437 buf
[len
] = '\n'; /* append \n to be compatible with libc fgetc() */
1438 buf
[len
+ 1] = '\0';
1443 return fgets(buf
, bufsz
, f
);
1447 static int ignore_partition(struct fdisk_partition
*pa
)
1449 /* incomplete partition setting */
1450 if (!fdisk_partition_has_start(pa
) && !fdisk_partition_start_is_default(pa
))
1452 if (!fdisk_partition_has_size(pa
) && !fdisk_partition_end_is_default(pa
))
1455 /* probably dump from old sfdisk with start=0 size=0 */
1456 if (fdisk_partition_has_start(pa
) && fdisk_partition_get_start(pa
) == 0 &&
1457 fdisk_partition_has_size(pa
) && fdisk_partition_get_size(pa
) == 0)
1463 static void follow_wipe_mode(struct sfdisk
*sf
)
1465 int dowipe
= sf
->wipemode
== WIPEMODE_ALWAYS
? 1 : 0;
1467 if (sf
->interactive
&& sf
->wipemode
== WIPEMODE_AUTO
)
1468 dowipe
= 1; /* do it in interactive mode */
1470 if (fdisk_is_ptcollision(sf
->cxt
) && sf
->wipemode
!= WIPEMODE_NEVER
)
1471 dowipe
= 1; /* always wipe old PT */
1473 fdisk_enable_wipe(sf
->cxt
, dowipe
);
1478 if (!fdisk_is_ptcollision(sf
->cxt
)) {
1479 fdisk_info(sf
->cxt
, _("The old %s signature will be removed by a write command."),
1480 fdisk_get_collision(sf
->cxt
));
1481 fputc('\n', stderr
);
1484 fdisk_warnx(sf
->cxt
, _(
1485 "The old %s signature may remain on the device. "
1486 "It is recommended to wipe the device with wipefs(8) or "
1487 "sfdisk --wipe, in order to avoid possible collisions."),
1488 fdisk_get_collision(sf
->cxt
));
1489 fputc('\n', stderr
);
1493 static int wipe_partition(struct sfdisk
*sf
, size_t partno
)
1496 char *fstype
= NULL
;
1497 struct fdisk_partition
*tmp
= NULL
;
1499 DBG(MISC
, ul_debug("checking for signature"));
1501 rc
= fdisk_get_partition(sf
->cxt
, partno
, &tmp
);
1505 rc
= fdisk_partition_to_string(tmp
, sf
->cxt
, FDISK_FIELD_FSTYPE
, &fstype
);
1506 if (rc
|| fstype
== NULL
)
1509 fdisk_warnx(sf
->cxt
, _("Partition #%zu contains a %s signature."), partno
+ 1, fstype
);
1511 if (sf
->pwipemode
== WIPEMODE_AUTO
&& isatty(STDIN_FILENO
))
1512 fdisk_ask_yesno(sf
->cxt
, _("Do you want to remove the signature?"), &yes
);
1513 else if (sf
->pwipemode
== WIPEMODE_ALWAYS
)
1517 fdisk_info(sf
->cxt
, _("The signature will be removed by a write command."));
1518 rc
= fdisk_wipe_partition(sf
->cxt
, partno
, TRUE
);
1521 fdisk_unref_partition(tmp
);
1523 DBG(MISC
, ul_debug("partition wipe check end [rc=%d]", rc
));
1527 static void refresh_prompt_buffer(struct sfdisk
*sf
, const char *devname
,
1528 size_t next_partno
, int created
)
1531 char *partname
= fdisk_partname(devname
, next_partno
+ 1);
1533 err(EXIT_FAILURE
, _("failed to allocate partition name"));
1535 if (!sf
->prompt
|| !startswith(sf
->prompt
, partname
)) {
1537 xasprintf(&sf
->prompt
,"%s: ", partname
);
1540 } else if (!sf
->prompt
|| !startswith(sf
->prompt
, SFDISK_PROMPT
)) {
1542 sf
->prompt
= xstrdup(SFDISK_PROMPT
);
1547 * sfdisk <device> [[-N] <partno>]
1549 * Note that the option -N is there for backward compatibility only.
1551 static int command_fdisk(struct sfdisk
*sf
, int argc
, char **argv
)
1553 int rc
= 0, partno
= sf
->partno
, created
= 0, unused
= 0;
1554 struct fdisk_script
*dp
;
1555 struct fdisk_table
*tb
= NULL
;
1556 const char *devname
= NULL
, *label
;
1558 size_t next_partno
= (size_t) -1;
1562 if (partno
< 0 && argc
> 1)
1563 partno
= strtou32_or_err(argv
[1],
1564 _("failed to parse partition number"));
1566 errx(EXIT_FAILURE
, _("no disk device specified"));
1568 rc
= fdisk_assign_device(sf
->cxt
, devname
, 0);
1570 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
1572 dp
= fdisk_new_script(sf
->cxt
);
1574 err(EXIT_FAILURE
, _("failed to allocate script handler"));
1575 fdisk_set_script(sf
->cxt
, dp
);
1576 #ifdef HAVE_LIBREADLINE
1577 fdisk_script_set_fgets(dp
, sfdisk_fgets
);
1579 fdisk_script_set_userdata(dp
, (void *) sf
);
1582 * Don't create a new disklabel when [-N] <partno> specified. In this
1583 * case reuse already specified disklabel. Let's check that the disk
1584 * really contains the partition.
1589 if (!fdisk_has_label(sf
->cxt
))
1590 errx(EXIT_FAILURE
, _("%s: cannot modify partition %d: "
1591 "no partition table was found"),
1592 devname
, partno
+ 1);
1593 n
= fdisk_get_npartitions(sf
->cxt
);
1594 if ((size_t) partno
> n
)
1595 errx(EXIT_FAILURE
, _("%s: cannot modify partition %d: "
1596 "partition table contains only %zu "
1598 devname
, partno
+ 1, n
);
1600 if (!fdisk_is_partition_used(sf
->cxt
, partno
)) {
1601 fdisk_warnx(sf
->cxt
, _("warning: %s: partition %d is not defined yet"),
1602 devname
, partno
+ 1);
1606 next_partno
= partno
;
1609 sf
->orig_pa
= get_partition(sf
->cxt
, partno
);
1614 next_partno
= last_pt_partno(sf
) + 1;
1617 if (!sf
->quiet
&& sf
->interactive
) {
1618 color_scheme_enable("welcome", UL_COLOR_GREEN
);
1619 fdisk_info(sf
->cxt
, _("\nWelcome to sfdisk (%s)."), PACKAGE_STRING
);
1621 fdisk_info(sf
->cxt
, _("Changes will remain in memory only, until you decide to write them.\n"
1622 "Be careful before using the write command.\n"));
1625 if (!sf
->noact
&& !sf
->noreread
) {
1627 fputs(_("Checking that no-one is using this disk right now ..."), stdout
);
1628 if (fdisk_device_is_used(sf
->cxt
)) {
1630 fputs(_(" FAILED\n\n"), stdout
);
1632 fdisk_warnx(sf
->cxt
, _(
1633 "This disk is currently in use - repartitioning is probably a bad idea.\n"
1634 "Umount all file systems, and swapoff all swap partitions on this disk.\n"
1635 "Use the --no-reread flag to suppress this check.\n"));
1638 errx(EXIT_FAILURE
, _("Use the --force flag to overrule all checks."));
1639 } else if (!sf
->quiet
)
1640 fputs(_(" OK\n\n"), stdout
);
1643 if (fdisk_get_collision(sf
->cxt
))
1644 follow_wipe_mode(sf
);
1647 backup_partition_table(sf
, devname
);
1650 list_disk_geometry(sf
->cxt
);
1651 if (fdisk_has_label(sf
->cxt
)) {
1652 fdisk_info(sf
->cxt
, _("\nOld situation:"));
1653 list_disklabel(sf
->cxt
);
1659 else if (fdisk_has_label(sf
->cxt
))
1660 label
= fdisk_label_get_name(fdisk_get_label(sf
->cxt
, NULL
));
1662 label
= "dos"; /* just for backward compatibility */
1664 fdisk_script_set_header(dp
, "label", label
);
1667 if (!sf
->quiet
&& sf
->interactive
) {
1668 if (!fdisk_has_label(sf
->cxt
) && !sf
->label
)
1670 _("\nsfdisk is going to create a new '%s' disk label.\n"
1671 "Use 'label: <name>' before you define a first partition\n"
1672 "to override the default."), label
);
1673 fdisk_info(sf
->cxt
, _("\nType 'help' to get more information.\n"));
1674 } else if (!sf
->quiet
)
1675 fputc('\n', stdout
);
1677 tb
= fdisk_script_get_table(dp
);
1683 DBG(PARSE
, ul_debug("<---next-line--->"));
1684 if (next_partno
== (size_t) -1)
1685 next_partno
= fdisk_table_get_nents(tb
);
1689 && next_partno
== fdisk_get_npartitions(sf
->cxt
)
1690 && !has_container(sf
)) {
1691 fdisk_info(sf
->cxt
, _("All partitions used."));
1692 rc
= SFDISK_DONE_ASK
;
1696 refresh_prompt_buffer(sf
, devname
, next_partno
, created
);
1699 if (sf
->prompt
&& (sf
->interactive
|| !sf
->quiet
)) {
1700 #ifndef HAVE_LIBREADLINE
1701 fputs(sf
->prompt
, stdout
);
1703 if (!sf
->interactive
)
1704 fputs(sf
->prompt
, stdout
);
1708 rc
= fdisk_script_read_line(dp
, stdin
, buf
, sizeof(buf
));
1710 DBG(PARSE
, ul_debug("script parsing failed, trying sfdisk specific commands"));
1711 buf
[sizeof(buf
) - 1] = '\0';
1712 rc
= loop_control_commands(sf
, dp
, buf
);
1716 } else if (rc
== 1) {
1717 rc
= SFDISK_DONE_EOF
;
1719 fputs(_("Done.\n"), stdout
);
1723 nparts
= fdisk_table_get_nents(tb
);
1725 size_t cur_partno
= (size_t) -1;
1726 struct fdisk_partition
*pa
= fdisk_table_get_partition(tb
, nparts
- 1);
1730 if (ignore_partition(pa
)) {
1731 fdisk_info(sf
->cxt
, _("Ignoring partition."));
1735 if (!created
) { /* create a new disklabel */
1736 rc
= fdisk_apply_script_headers(sf
->cxt
, dp
);
1739 fdisk_warnx(sf
->cxt
, _(
1740 "Failed to apply script headers, "
1741 "disk label not created."));
1743 if (rc
== 0 && fdisk_get_collision(sf
->cxt
))
1744 follow_wipe_mode(sf
);
1746 if (!rc
&& partno
>= 0) { /* -N <partno>, modify partition */
1747 rc
= fdisk_set_partition(sf
->cxt
, partno
, pa
);
1748 rc
= rc
== 0 ? SFDISK_DONE_ASK
: SFDISK_DONE_ABORT
;
1750 } else if (!rc
) { /* add partition */
1751 if (!sf
->interactive
&& !sf
->quiet
&&
1752 (!sf
->prompt
|| startswith(sf
->prompt
, SFDISK_PROMPT
))) {
1753 refresh_prompt_buffer(sf
, devname
, next_partno
, created
);
1754 fputs(sf
->prompt
, stdout
);
1756 rc
= fdisk_add_partition(sf
->cxt
, pa
, &cur_partno
);
1759 fdisk_warn(sf
->cxt
, _("Failed to add #%d partition"), next_partno
+ 1);
1763 /* wipe partition on success
1765 * Note that unused=1 means -N <partno> for unused,
1766 * otherwise we wipe only newly created partitions.
1768 if (rc
== 0 && (unused
|| partno
< 0)) {
1769 rc
= wipe_partition(sf
, unused
? (size_t) partno
: cur_partno
);
1775 /* success print result */
1776 if (sf
->interactive
)
1777 sfdisk_print_partition(sf
, cur_partno
);
1778 next_partno
= cur_partno
+ 1;
1779 } else if (pa
) /* error, drop partition from script */
1780 fdisk_table_remove_partition(tb
, pa
);
1782 fdisk_info(sf
->cxt
, _("Script header accepted."));
1784 if (rc
&& !sf
->interactive
) {
1785 rc
= SFDISK_DONE_ABORT
;
1790 /* create empty disk label if label, but no partition specified */
1791 if ((rc
== SFDISK_DONE_EOF
|| rc
== SFDISK_DONE_WRITE
) && created
== 0
1792 && fdisk_script_has_force_label(dp
) == 1
1793 && fdisk_table_get_nents(tb
) == 0
1794 && fdisk_script_get_header(dp
, "label")) {
1796 int xrc
= fdisk_apply_script_headers(sf
->cxt
, dp
);
1799 fdisk_warnx(sf
->cxt
, _(
1800 "Failed to apply script headers, "
1801 "disk label not created."));
1802 rc
= SFDISK_DONE_ABORT
;
1806 if (!sf
->quiet
&& rc
!= SFDISK_DONE_ABORT
) {
1807 fdisk_info(sf
->cxt
, _("\nNew situation:"));
1808 list_disk_identifier(sf
->cxt
);
1809 list_disklabel(sf
->cxt
);
1813 case SFDISK_DONE_ASK
:
1814 case SFDISK_DONE_EOF
:
1815 if (sf
->interactive
) {
1817 fdisk_ask_yesno(sf
->cxt
, _("Do you want to write this to disk?"), &yes
);
1819 fdisk_info(sf
->cxt
, _("Leaving."));
1825 case SFDISK_DONE_WRITE
:
1826 rc
= write_changes(sf
);
1828 case SFDISK_DONE_ABORT
:
1829 default: /* rc < 0 on error */
1830 fdisk_info(sf
->cxt
, _("Leaving.\n"));
1834 fdisk_unref_script(dp
);
1838 static void __attribute__((__noreturn__
)) usage(void)
1841 fputs(USAGE_HEADER
, out
);
1844 _(" %1$s [options] <dev> [[-N] <part>]\n"
1845 " %1$s [options] <command>\n"), program_invocation_short_name
);
1847 fputs(USAGE_SEPARATOR
, out
);
1848 fputs(_("Display or manipulate a disk partition table.\n"), out
);
1850 fputs(USAGE_COMMANDS
, out
);
1851 fputs(_(" -A, --activate <dev> [<part> ...] list or set bootable (P)MBR partitions\n"), out
);
1852 fputs(_(" -d, --dump <dev> dump partition table (usable for later input)\n"), out
);
1853 fputs(_(" -J, --json <dev> dump partition table in JSON format\n"), out
);
1854 fputs(_(" -g, --show-geometry [<dev> ...] list geometry of all or specified devices\n"), out
);
1855 fputs(_(" -l, --list [<dev> ...] list partitions of each device\n"), out
);
1856 fputs(_(" -F, --list-free [<dev> ...] list unpartitioned free areas of each device\n"), out
);
1857 fputs(_(" -r, --reorder <dev> fix partitions order (by start offset)\n"), out
);
1858 fputs(_(" -s, --show-size [<dev> ...] list sizes of all or specified devices\n"), out
);
1859 fputs(_(" -T, --list-types print the recognized types (see -X)\n"), out
);
1860 fputs(_(" -V, --verify [<dev> ...] test whether partitions seem correct\n"), out
);
1861 fputs(_(" --delete <dev> [<part> ...] delete all or specified partitions\n"), out
);
1863 fputs(USAGE_SEPARATOR
, out
);
1864 fputs(_(" --part-label <dev> <part> [<str>] print or change partition label\n"), out
);
1865 fputs(_(" --part-type <dev> <part> [<type>] print or change partition type\n"), out
);
1866 fputs(_(" --part-uuid <dev> <part> [<uuid>] print or change partition uuid\n"), out
);
1867 fputs(_(" --part-attrs <dev> <part> [<str>] print or change partition attributes\n"), out
);
1869 fputs(USAGE_SEPARATOR
, out
);
1870 fputs(_(" <dev> device (usually disk) path\n"), out
);
1871 fputs(_(" <part> partition number\n"), out
);
1872 fputs(_(" <type> partition type, GUID for GPT, hex for MBR\n"), out
);
1874 fputs(USAGE_OPTIONS
, out
);
1875 fputs(_(" -a, --append append partitions to existing partition table\n"), out
);
1876 fputs(_(" -b, --backup backup partition table sectors (see -O)\n"), out
);
1877 fputs(_(" --bytes print SIZE in bytes rather than in human readable format\n"), out
);
1878 fputs(_(" --move-data[=<typescript>] move partition data after relocation (requires -N)\n"), out
);
1879 fputs(_(" -f, --force disable all consistency checking\n"), out
);
1880 fputs(_(" --color[=<when>] colorize output (auto, always or never)\n"), out
);
1882 " %s\n", USAGE_COLORS_DEFAULT
);
1883 fputs(_(" -N, --partno <num> specify partition number\n"), out
);
1884 fputs(_(" -n, --no-act do everything except write to device\n"), out
);
1885 fputs(_(" --no-reread do not check whether the device is in use\n"), out
);
1886 fputs(_(" --no-tell-kernel do not tell kernel about changes\n"), out
);
1887 fputs(_(" -O, --backup-file <path> override default backup file name\n"), out
);
1888 fputs(_(" -o, --output <list> output columns\n"), out
);
1889 fputs(_(" -q, --quiet suppress extra info messages\n"), out
);
1890 fputs(_(" -w, --wipe <mode> wipe signatures (auto, always or never)\n"), out
);
1891 fputs(_(" -W, --wipe-partitions <mode> wipe signatures from new partitions (auto, always or never)\n"), out
);
1892 fputs(_(" -X, --label <name> specify label type (dos, gpt, ...)\n"), out
);
1893 fputs(_(" -Y, --label-nested <name> specify nested label type (dos, bsd)\n"), out
);
1894 fputs(USAGE_SEPARATOR
, out
);
1895 fputs(_(" -G, --show-pt-geometry deprecated, alias to --show-geometry\n"), out
);
1896 fputs(_(" -L, --Linux deprecated, only for backward compatibility\n"), out
);
1897 fputs(_(" -u, --unit S deprecated, only sector unit is supported\n"), out
);
1899 fputs(USAGE_SEPARATOR
, out
);
1900 printf( " -h, --help %s\n", USAGE_OPTSTR_HELP
);
1901 printf( " -v, --version %s\n", USAGE_OPTSTR_VERSION
);
1903 list_available_columns(out
);
1905 printf(USAGE_MAN_TAIL("sfdisk(8)"));
1910 int main(int argc
, char *argv
[])
1912 const char *outarg
= NULL
;
1913 int rc
= -EINVAL
, c
, longidx
= -1, bytes
= 0;
1914 int colormode
= UL_COLORMODE_UNDEF
;
1915 struct sfdisk _sf
= {
1917 .wipemode
= WIPEMODE_AUTO
,
1918 .pwipemode
= WIPEMODE_AUTO
,
1919 .interactive
= isatty(STDIN_FILENO
) ? 1 : 0,
1923 OPT_CHANGE_ID
= CHAR_MAX
+ 1,
1938 static const struct option longopts
[] = {
1939 { "activate",no_argument
, NULL
, 'A' },
1940 { "append", no_argument
, NULL
, 'a' },
1941 { "backup", no_argument
, NULL
, 'b' },
1942 { "backup-file", required_argument
, NULL
, 'O' },
1943 { "bytes", no_argument
, NULL
, OPT_BYTES
},
1944 { "color", optional_argument
, NULL
, OPT_COLOR
},
1945 { "delete", no_argument
, NULL
, OPT_DELETE
},
1946 { "dump", no_argument
, NULL
, 'd' },
1947 { "help", no_argument
, NULL
, 'h' },
1948 { "force", no_argument
, NULL
, 'f' },
1949 { "json", no_argument
, NULL
, 'J' },
1950 { "label", required_argument
, NULL
, 'X' },
1951 { "label-nested", required_argument
, NULL
, 'Y' },
1952 { "list", no_argument
, NULL
, 'l' },
1953 { "list-free", no_argument
, NULL
, 'F' },
1954 { "list-types", no_argument
, NULL
, 'T' },
1955 { "no-act", no_argument
, NULL
, 'n' },
1956 { "no-reread", no_argument
, NULL
, OPT_NOREREAD
},
1957 { "no-tell-kernel", no_argument
, NULL
, OPT_NOTELL
},
1958 { "move-data", optional_argument
, NULL
, OPT_MOVEDATA
},
1959 { "output", required_argument
, NULL
, 'o' },
1960 { "partno", required_argument
, NULL
, 'N' },
1961 { "reorder", no_argument
, NULL
, 'r' },
1962 { "show-geometry", no_argument
, NULL
, 'g' },
1963 { "quiet", no_argument
, NULL
, 'q' },
1964 { "verify", no_argument
, NULL
, 'V' },
1965 { "version", no_argument
, NULL
, 'v' },
1966 { "wipe", required_argument
, NULL
, 'w' },
1967 { "wipe-partitions", required_argument
, NULL
, 'W' },
1969 { "part-uuid", no_argument
, NULL
, OPT_PARTUUID
},
1970 { "part-label", no_argument
, NULL
, OPT_PARTLABEL
},
1971 { "part-type", no_argument
, NULL
, OPT_PARTTYPE
},
1972 { "part-attrs", no_argument
, NULL
, OPT_PARTATTRS
},
1974 { "show-pt-geometry", no_argument
, NULL
, 'G' }, /* deprecated */
1975 { "unit", required_argument
, NULL
, 'u' }, /* deprecated */
1976 { "Linux", no_argument
, NULL
, 'L' }, /* deprecated */
1977 { "show-size", no_argument
, NULL
, 's' }, /* deprecated */
1979 { "change-id",no_argument
, NULL
, OPT_CHANGE_ID
}, /* deprecated */
1980 { "id", no_argument
, NULL
, 'c' }, /* deprecated */
1981 { "print-id",no_argument
, NULL
, OPT_PRINT_ID
}, /* deprecated */
1983 { NULL
, 0, NULL
, 0 },
1985 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
1986 { 's','u'}, /* --show-size --unit */
1989 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1992 setlocale(LC_ALL
, "");
1993 bindtextdomain(PACKAGE
, LOCALEDIR
);
1994 textdomain(PACKAGE
);
1995 close_stdout_atexit();
1997 while ((c
= getopt_long(argc
, argv
, "aAbcdfFgGhJlLo:O:nN:qrsTu:vVX:Y:w:W:",
1998 longopts
, &longidx
)) != -1) {
2000 err_exclusive_options(c
, longopts
, excl
, excl_st
);
2004 sf
->act
= ACT_ACTIVATE
;
2015 warnx(_("%s is deprecated in favour of --part-type"),
2016 longopts
[longidx
].name
);
2017 sf
->act
= ACT_PARTTYPE
;
2020 warnx(_("--id is deprecated in favour of --part-type"));
2021 sf
->act
= ACT_PARTTYPE
;
2030 sf
->act
= ACT_LIST_FREE
;
2036 warnx(_("--show-pt-geometry is no more implemented. Using --show-geometry."));
2039 sf
->act
= ACT_SHOW_GEOM
;
2048 warnx(_("--Linux option is unnecessary and deprecated"));
2055 sf
->backup_file
= optarg
;
2061 sf
->partno
= strtou32_or_err(optarg
, _("failed to parse partition number")) - 1;
2067 sf
->act
= ACT_REORDER
;
2070 sf
->act
= ACT_SHOW_SIZE
;
2073 sf
->act
= ACT_LIST_TYPES
;
2077 errx(EXIT_FAILURE
, _("unsupported unit '%c'"), *optarg
);
2080 print_version(EXIT_SUCCESS
);
2085 sf
->wipemode
= wipemode_from_string(optarg
);
2086 if (sf
->wipemode
< 0)
2087 errx(EXIT_FAILURE
, _("unsupported wipe mode"));
2090 sf
->pwipemode
= wipemode_from_string(optarg
);
2091 if (sf
->pwipemode
< 0)
2092 errx(EXIT_FAILURE
, _("unsupported wipe mode"));
2098 sf
->label_nested
= optarg
;
2102 sf
->act
= ACT_PARTUUID
;
2105 sf
->act
= ACT_PARTTYPE
;
2108 sf
->act
= ACT_PARTLABEL
;
2111 sf
->act
= ACT_PARTATTRS
;
2120 colormode
= UL_COLORMODE_AUTO
;
2122 colormode
= colormode_or_err(optarg
,
2123 _("unsupported color mode"));
2127 sf
->move_typescript
= optarg
;
2130 sf
->act
= ACT_DELETE
;
2136 errtryhelp(EXIT_FAILURE
);
2140 colors_init(colormode
, "sfdisk");
2144 fdisk_set_size_unit(sf
->cxt
, FDISK_SIZEUNIT_BYTES
);
2147 init_fields(NULL
, outarg
, NULL
);
2149 if (sf
->verify
&& !sf
->act
)
2150 sf
->act
= ACT_VERIFY
; /* --verify make be used with --list too */
2152 sf
->act
= ACT_FDISK
; /* default */
2154 if (sf
->movedata
&& !(sf
->act
== ACT_FDISK
&& sf
->partno
>= 0))
2155 errx(EXIT_FAILURE
, _("--movedata requires -N"));
2159 rc
= command_activate(sf
, argc
- optind
, argv
+ optind
);
2163 rc
= command_delete(sf
, argc
- optind
, argv
+ optind
);
2167 rc
= command_list_partitions(sf
, argc
- optind
, argv
+ optind
);
2170 case ACT_LIST_TYPES
:
2171 rc
= command_list_types(sf
);
2175 rc
= command_list_freespace(sf
, argc
- optind
, argv
+ optind
);
2179 rc
= command_fdisk(sf
, argc
- optind
, argv
+ optind
);
2183 rc
= command_dump(sf
, argc
- optind
, argv
+ optind
);
2187 rc
= command_show_size(sf
, argc
- optind
, argv
+ optind
);
2191 rc
= command_show_geometry(sf
, argc
- optind
, argv
+ optind
);
2195 rc
= command_verify(sf
, argc
- optind
, argv
+ optind
);
2199 rc
= command_parttype(sf
, argc
- optind
, argv
+ optind
);
2203 rc
= command_partuuid(sf
, argc
- optind
, argv
+ optind
);
2207 rc
= command_partlabel(sf
, argc
- optind
, argv
+ optind
);
2211 rc
= command_partattrs(sf
, argc
- optind
, argv
+ optind
);
2215 rc
= command_reorder(sf
, argc
- optind
, argv
+ optind
);
2221 DBG(MISC
, ul_debug("bye! [rc=%d]", rc
));
2222 return rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;