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(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 memcpy(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
:
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 fdisk_reread_partition_table(sf
->cxt
);
554 rc
= fdisk_deassign_device(sf
->cxt
,
555 sf
->noact
|| sf
->notell
); /* no-sync */
560 * sfdisk --list [<device ..]
562 static int command_list_partitions(struct sfdisk
*sf
, int argc
, char **argv
)
565 fdisk_enable_listonly(sf
->cxt
, 1);
570 for (i
= 0; i
< argc
; i
++) {
572 fputs("\n\n", stdout
);
573 if (print_device_pt(sf
->cxt
, argv
[i
], 1, sf
->verify
) != 0)
578 print_all_devices_pt(sf
->cxt
, sf
->verify
);
584 * sfdisk --list-free [<device ..]
586 static int command_list_freespace(struct sfdisk
*sf
, int argc
, char **argv
)
589 fdisk_enable_listonly(sf
->cxt
, 1);
594 for (i
= 0; i
< argc
; i
++) {
596 fputs("\n\n", stdout
);
597 if (print_device_freespace(sf
->cxt
, argv
[i
], 1) != 0)
602 print_all_devices_freespace(sf
->cxt
);
608 * sfdisk --list-types
610 static int command_list_types(struct sfdisk
*sf
)
612 const struct fdisk_parttype
*t
;
613 struct fdisk_label
*lb
;
621 name
= sf
->label
? sf
->label
: "dos";
622 lb
= fdisk_get_label(sf
->cxt
, name
);
624 errx(EXIT_FAILURE
, _("unsupported label '%s'"), name
);
626 codes
= fdisk_label_has_code_parttypes(lb
);
627 fputs(_("Id Name\n\n"), stdout
);
629 while ((t
= fdisk_label_get_parttype(lb
, i
++))) {
631 printf("%2x %s\n", fdisk_parttype_get_code(t
),
632 fdisk_parttype_get_name(t
));
634 printf("%s %s\n", fdisk_parttype_get_string(t
),
635 fdisk_parttype_get_name(t
));
641 static int verify_device(struct sfdisk
*sf
, const char *devname
)
645 fdisk_enable_listonly(sf
->cxt
, 1);
647 if (fdisk_assign_device(sf
->cxt
, devname
, 1)) {
648 warn(_("cannot open %s"), devname
);
652 color_scheme_enable("header", UL_COLOR_BOLD
);
653 fdisk_info(sf
->cxt
, "%s:", devname
);
656 if (!fdisk_has_label(sf
->cxt
))
657 fdisk_info(sf
->cxt
, _("unrecognized partition table type"));
659 rc
= fdisk_verify_disklabel(sf
->cxt
);
661 fdisk_deassign_device(sf
->cxt
, 1);
666 * sfdisk --verify [<device ..]
668 static int command_verify(struct sfdisk
*sf
, int argc
, char **argv
)
670 int nfails
= 0, ct
= 0;
674 for (i
= 0; i
< argc
; i
++) {
676 fdisk_info(sf
->cxt
, " ");
677 if (verify_device(sf
, argv
[i
]) < 0)
684 while ((dev
= next_proc_partition(&f
))) {
686 fdisk_info(sf
->cxt
, " ");
687 if (verify_device(sf
, dev
) < 0)
697 static int get_size(const char *dev
, int silent
, uintmax_t *sz
)
701 fd
= open(dev
, O_RDONLY
);
704 warn(_("cannot open %s"), dev
);
708 if (blkdev_get_sectors(fd
, (unsigned long long *) sz
) == -1) {
710 warn(_("Cannot get size of %s"), dev
);
719 * sfdisk --show-size [<device ..]
721 * (silly, but just for backward compatibility)
723 static int command_show_size(struct sfdisk
*sf
__attribute__((__unused__
)),
724 int argc
, char **argv
)
730 for (i
= 0; i
< argc
; i
++) {
731 if (get_size(argv
[i
], 0, &sz
) == 0)
732 printf("%ju\n", sz
/ 2);
739 while ((dev
= next_proc_partition(&f
))) {
740 if (get_size(dev
, 1, &sz
) == 0) {
741 printf("%s: %9ju\n", dev
, sz
/ 2);
747 printf(_("total: %ju blocks\n"), total
);
753 static int print_geom(struct sfdisk
*sf
, const char *devname
)
755 fdisk_enable_listonly(sf
->cxt
, 1);
757 if (fdisk_assign_device(sf
->cxt
, devname
, 1)) {
758 warn(_("cannot open %s"), devname
);
762 fdisk_info(sf
->cxt
, "%s: %ju cylinders, %ju heads, %ju sectors/track",
764 (uintmax_t) fdisk_get_geom_cylinders(sf
->cxt
),
765 (uintmax_t) fdisk_get_geom_heads(sf
->cxt
),
766 (uintmax_t) fdisk_get_geom_sectors(sf
->cxt
));
768 fdisk_deassign_device(sf
->cxt
, 1);
773 * sfdisk --show-geometry [<device ..]
775 static int command_show_geometry(struct sfdisk
*sf
, int argc
, char **argv
)
781 for (i
= 0; i
< argc
; i
++) {
782 if (print_geom(sf
, argv
[i
]) < 0)
789 while ((dev
= next_proc_partition(&f
))) {
790 if (print_geom(sf
, dev
) < 0)
800 * sfdisk --activate <device> [<partno> ...]
802 static int command_activate(struct sfdisk
*sf
, int argc
, char **argv
)
804 int rc
, nparts
, i
, listonly
;
805 struct fdisk_partition
*pa
= NULL
;
806 const char *devname
= NULL
;
809 errx(EXIT_FAILURE
, _("no disk device specified"));
812 /* --activate <device> */
813 listonly
= argc
== 1;
815 rc
= fdisk_assign_device(sf
->cxt
, devname
, listonly
);
817 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
819 if (fdisk_is_label(sf
->cxt
, GPT
)) {
820 /* Switch from GPT to PMBR */
821 sf
->cxt
= fdisk_new_nested_context(sf
->cxt
, "dos");
823 err(EXIT_FAILURE
, _("cannot switch to PMBR"));
824 } else if (!fdisk_is_label(sf
->cxt
, DOS
))
825 errx(EXIT_FAILURE
, _("toggle boot flags is supported for MBR or PMBR only"));
827 if (!listonly
&& sf
->backup
)
828 backup_partition_table(sf
, devname
);
830 nparts
= fdisk_get_npartitions(sf
->cxt
);
831 for (i
= 0; i
< nparts
; i
++) {
834 /* note that fdisk_get_partition() reuses the @pa pointer, you
835 * don't have to (re)allocate it */
836 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0)
839 /* sfdisk --activate list bootable partitions */
841 if (!fdisk_partition_is_bootable(pa
))
843 if (fdisk_partition_to_string(pa
, sf
->cxt
,
844 FDISK_FIELD_DEVICE
, &data
) == 0) {
845 printf("%s\n", data
);
849 /* deactivate all active partitions */
850 } else if (fdisk_partition_is_bootable(pa
))
851 fdisk_toggle_partition_flag(sf
->cxt
, i
, DOS_FLAG_ACTIVE
);
854 /* sfdisk --activate <partno> [..] */
855 for (i
= 1; i
< argc
; i
++) {
858 if (i
== 1 && strcmp(argv
[1], "-") == 0)
860 n
= strtou32_or_err(argv
[i
], _("failed to parse partition number"));
862 rc
= fdisk_toggle_partition_flag(sf
->cxt
, n
- 1, DOS_FLAG_ACTIVE
);
865 _("%s: partition %d: failed to toggle bootable flag"),
869 fdisk_unref_partition(pa
);
872 rc
= fdisk_deassign_device(sf
->cxt
, 1);
874 rc
= write_changes(sf
);
879 * sfdisk --delete <device> [<partno> ...]
881 static int command_delete(struct sfdisk
*sf
, int argc
, char **argv
)
884 const char *devname
= NULL
;
887 errx(EXIT_FAILURE
, _("no disk device specified"));
890 if (fdisk_assign_device(sf
->cxt
, devname
, 0) != 0)
891 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
894 backup_partition_table(sf
, devname
);
898 size_t nparts
= fdisk_get_npartitions(sf
->cxt
);
899 for (i
= 0; i
< nparts
; i
++) {
900 if (fdisk_is_partition_used(sf
->cxt
, i
) &&
901 fdisk_delete_partition(sf
->cxt
, i
) != 0)
902 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to delete"), devname
, i
+ 1);
904 /* delete specified */
906 for (i
= 1; i
< (size_t) argc
; i
++) {
907 size_t n
= strtou32_or_err(argv
[i
], _("failed to parse partition number"));
909 if (fdisk_delete_partition(sf
->cxt
, n
- 1) != 0)
910 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to delete"), devname
, n
);
914 return write_changes(sf
);
918 * sfdisk --reorder <device>
920 static int command_reorder(struct sfdisk
*sf
, int argc
, char **argv
)
922 const char *devname
= NULL
;
928 errx(EXIT_FAILURE
, _("no disk device specified"));
930 rc
= fdisk_assign_device(sf
->cxt
, devname
, 0); /* read-write */
932 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
935 backup_partition_table(sf
, devname
);
937 if (fdisk_reorder_partitions(sf
->cxt
) == 1) /* unchanged */
938 rc
= fdisk_deassign_device(sf
->cxt
, 1);
940 rc
= write_changes(sf
);
947 * sfdisk --dump <device>
949 static int command_dump(struct sfdisk
*sf
, int argc
, char **argv
)
951 const char *devname
= NULL
;
952 struct fdisk_script
*dp
;
958 errx(EXIT_FAILURE
, _("no disk device specified"));
960 rc
= fdisk_assign_device(sf
->cxt
, devname
, 1); /* read-only */
962 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
964 if (!fdisk_has_label(sf
->cxt
))
965 errx(EXIT_FAILURE
, _("%s: does not contain a recognized partition table"), devname
);
967 dp
= fdisk_new_script(sf
->cxt
);
969 err(EXIT_FAILURE
, _("failed to allocate dump struct"));
971 rc
= fdisk_script_read_context(dp
, NULL
);
973 errx(EXIT_FAILURE
, _("%s: failed to dump partition table"), devname
);
976 fdisk_script_enable_json(dp
, 1);
977 fdisk_script_write_file(dp
, stdout
);
979 fdisk_unref_script(dp
);
980 fdisk_deassign_device(sf
->cxt
, 1); /* no-sync() */
984 static void assign_device_partition(struct sfdisk
*sf
,
991 struct fdisk_label
*lb
= NULL
;
996 /* read-only when a new <type> undefined */
997 rc
= fdisk_assign_device(sf
->cxt
, devname
, rdonly
);
999 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
1001 lb
= fdisk_get_label(sf
->cxt
, NULL
);
1003 errx(EXIT_FAILURE
, _("%s: no partition table found"), devname
);
1005 n
= fdisk_get_npartitions(sf
->cxt
);
1007 errx(EXIT_FAILURE
, _("%s: partition %zu: partition table contains "
1008 "only %zu partitions"), devname
, partno
, n
);
1009 if (!fdisk_is_partition_used(sf
->cxt
, partno
- 1))
1010 errx(EXIT_FAILURE
, _("%s: partition %zu: partition is unused"),
1015 * sfdisk --part-type <device> <partno> [<type>]
1017 static int command_parttype(struct sfdisk
*sf
, int argc
, char **argv
)
1020 struct fdisk_parttype
*type
= NULL
;
1021 struct fdisk_label
*lb
;
1022 const char *devname
= NULL
, *typestr
= NULL
;
1025 errx(EXIT_FAILURE
, _("no disk device specified"));
1029 errx(EXIT_FAILURE
, _("no partition number specified"));
1030 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1035 errx(EXIT_FAILURE
, _("unexpected arguments"));
1037 /* read-only when a new <type> undefined */
1038 assign_device_partition(sf
, devname
, partno
, !typestr
);
1040 lb
= fdisk_get_label(sf
->cxt
, NULL
);
1042 /* print partition type */
1044 const struct fdisk_parttype
*t
= NULL
;
1045 struct fdisk_partition
*pa
= NULL
;
1047 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1048 t
= fdisk_partition_get_type(pa
);
1050 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition type"),
1053 if (fdisk_label_has_code_parttypes(lb
))
1054 printf("%2x\n", fdisk_parttype_get_code(t
));
1056 printf("%s\n", fdisk_parttype_get_string(t
));
1058 fdisk_unref_partition(pa
);
1059 fdisk_deassign_device(sf
->cxt
, 1);
1064 backup_partition_table(sf
, devname
);
1066 /* parse <type> and apply to PT */
1067 type
= fdisk_label_parse_parttype(lb
, typestr
);
1069 errx(EXIT_FAILURE
, _("failed to parse %s partition type '%s'"),
1070 fdisk_label_get_name(lb
), typestr
);
1072 else if (fdisk_set_partition_type(sf
->cxt
, partno
- 1, type
) != 0)
1073 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition type"),
1075 fdisk_unref_parttype(type
);
1076 return write_changes(sf
);
1080 * sfdisk --part-uuid <device> <partno> [<uuid>]
1082 static int command_partuuid(struct sfdisk
*sf
, int argc
, char **argv
)
1085 struct fdisk_partition
*pa
= NULL
;
1086 const char *devname
= NULL
, *uuid
= NULL
;
1089 errx(EXIT_FAILURE
, _("no disk device specified"));
1093 errx(EXIT_FAILURE
, _("no partition number specified"));
1094 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1099 errx(EXIT_FAILURE
, _("unexpected arguments"));
1101 /* read-only if uuid not given */
1102 assign_device_partition(sf
, devname
, partno
, !uuid
);
1104 /* print partition uuid */
1106 const char *str
= NULL
;
1108 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1109 str
= fdisk_partition_get_uuid(pa
);
1111 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition UUID"),
1113 printf("%s\n", str
);
1114 fdisk_unref_partition(pa
);
1115 fdisk_deassign_device(sf
->cxt
, 1);
1120 backup_partition_table(sf
, devname
);
1122 pa
= fdisk_new_partition();
1124 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1126 if (fdisk_partition_set_uuid(pa
, uuid
) != 0 ||
1127 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1128 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition UUID"),
1130 fdisk_unref_partition(pa
);
1131 return write_changes(sf
);
1135 * sfdisk --part-label <device> <partno> [<label>]
1137 static int command_partlabel(struct sfdisk
*sf
, int argc
, char **argv
)
1140 struct fdisk_partition
*pa
= NULL
;
1141 const char *devname
= NULL
, *name
= NULL
;
1144 errx(EXIT_FAILURE
, _("no disk device specified"));
1148 errx(EXIT_FAILURE
, _("no partition number specified"));
1149 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1154 errx(EXIT_FAILURE
, _("unexpected arguments"));
1156 /* read-only if name not given */
1157 assign_device_partition(sf
, devname
, partno
, !name
);
1159 /* print partition name */
1161 const char *str
= NULL
;
1163 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1164 str
= fdisk_partition_get_name(pa
);
1166 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition name"),
1168 printf("%s\n", str
);
1169 fdisk_unref_partition(pa
);
1170 fdisk_deassign_device(sf
->cxt
, 1);
1175 backup_partition_table(sf
, devname
);
1177 pa
= fdisk_new_partition();
1179 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1181 if (fdisk_partition_set_name(pa
, name
) != 0 ||
1182 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1183 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition name"),
1186 fdisk_unref_partition(pa
);
1187 return write_changes(sf
);
1191 * sfdisk --part-attrs <device> <partno> [<attrs>]
1193 static int command_partattrs(struct sfdisk
*sf
, int argc
, char **argv
)
1196 struct fdisk_partition
*pa
= NULL
;
1197 const char *devname
= NULL
, *attrs
= NULL
;
1200 errx(EXIT_FAILURE
, _("no disk device specified"));
1204 errx(EXIT_FAILURE
, _("no partition number specified"));
1205 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1210 errx(EXIT_FAILURE
, _("unexpected arguments"));
1212 /* read-only if name not given */
1213 assign_device_partition(sf
, devname
, partno
, !attrs
);
1215 /* print partition name */
1217 const char *str
= NULL
;
1219 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1220 str
= fdisk_partition_get_attrs(pa
);
1222 printf("%s\n", str
);
1223 fdisk_unref_partition(pa
);
1224 fdisk_deassign_device(sf
->cxt
, 1);
1229 backup_partition_table(sf
, devname
);
1231 pa
= fdisk_new_partition();
1233 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1235 if (fdisk_partition_set_attrs(pa
, attrs
) != 0 ||
1236 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1237 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition attributes"),
1240 fdisk_unref_partition(pa
);
1241 return write_changes(sf
);
1244 static void sfdisk_print_partition(struct sfdisk
*sf
, size_t n
)
1246 struct fdisk_partition
*pa
= NULL
;
1253 if (fdisk_get_partition(sf
->cxt
, n
, &pa
) != 0)
1256 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_DEVICE
, &data
);
1257 printf("%12s : ", data
);
1259 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_START
, &data
);
1260 printf("%12s ", data
);
1262 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_END
, &data
);
1263 printf("%12s ", data
);
1265 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_SIZE
, &data
);
1266 printf("(%s) ", data
);
1268 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_TYPE
, &data
);
1269 printf("%s\n", data
);
1271 fdisk_unref_partition(pa
);
1274 static void command_fdisk_help(void)
1276 fputs(_("\nHelp:\n"), stdout
);
1278 fputc('\n', stdout
);
1279 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1280 fputs(_(" Commands:\n"), stdout
);
1282 fputs(_(" write write table to disk and exit\n"), stdout
);
1283 fputs(_(" quit show new situation and wait for user's feedback before write\n"), stdout
);
1284 fputs(_(" abort exit sfdisk shell\n"), stdout
);
1285 fputs(_(" print display the partition table\n"), stdout
);
1286 fputs(_(" help show this help text\n"), stdout
);
1287 fputc('\n', stdout
);
1288 fputs(_(" Ctrl-D the same as 'quit'\n"), stdout
);
1290 fputc('\n', stdout
);
1291 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1292 fputs(_(" Input format:\n"), stdout
);
1294 fputs(_(" <start>, <size>, <type>, <bootable>\n"), stdout
);
1296 fputc('\n', stdout
);
1297 fputs(_(" <start> Beginning of the partition in sectors, or bytes if\n"
1298 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1299 " The default is the first free space.\n"), stdout
);
1301 fputc('\n', stdout
);
1302 fputs(_(" <size> Size of the partition in sectors, or bytes if\n"
1303 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1304 " The default is all available space.\n"), stdout
);
1306 fputc('\n', stdout
);
1307 fputs(_(" <type> The partition type. Default is a Linux data partition.\n"), stdout
);
1308 fputs(_(" MBR: hex or L,S,E,X shortcuts.\n"), stdout
);
1309 fputs(_(" GPT: UUID or L,S,H shortcuts.\n"), stdout
);
1311 fputc('\n', stdout
);
1312 fputs(_(" <bootable> Use '*' to mark an MBR partition as bootable.\n"), stdout
);
1314 fputc('\n', stdout
);
1315 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1316 fputs(_(" Example:\n"), stdout
);
1318 fputs(_(" , 4G Creates a 4GiB partition at default start offset.\n"), stdout
);
1319 fputc('\n', stdout
);
1323 SFDISK_DONE_NONE
= 0,
1330 /* returns: 0 on success, <0 on error, 1 successfully stop sfdisk */
1331 static int loop_control_commands(struct sfdisk
*sf
,
1332 struct fdisk_script
*dp
,
1335 const char *p
= skip_blank(buf
);
1336 int rc
= SFDISK_DONE_NONE
;
1338 if (strcmp(p
, "print") == 0)
1339 list_disklabel(sf
->cxt
);
1340 else if (strcmp(p
, "help") == 0)
1341 command_fdisk_help();
1342 else if (strcmp(p
, "quit") == 0)
1343 rc
= SFDISK_DONE_ASK
;
1344 else if (strcmp(p
, "write") == 0)
1345 rc
= SFDISK_DONE_WRITE
;
1346 else if (strcmp(p
, "abort") == 0)
1347 rc
= SFDISK_DONE_ABORT
;
1349 if (sf
->interactive
)
1350 fdisk_warnx(sf
->cxt
, _("unsupported command"));
1352 fdisk_warnx(sf
->cxt
, _("line %d: unsupported command"),
1353 fdisk_script_get_nlines(dp
));
1360 static int has_container(struct sfdisk
*sf
)
1363 struct fdisk_partition
*pa
= NULL
;
1366 return sf
->container
;
1368 nparts
= fdisk_get_npartitions(sf
->cxt
);
1369 for (i
= 0; i
< nparts
; i
++) {
1370 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0)
1372 if (fdisk_partition_is_container(pa
)) {
1378 fdisk_unref_partition(pa
);
1379 return sf
->container
;
1382 static size_t last_pt_partno(struct sfdisk
*sf
)
1384 size_t i
, nparts
, partno
= 0;
1385 struct fdisk_partition
*pa
= NULL
;
1388 nparts
= fdisk_get_npartitions(sf
->cxt
);
1389 for (i
= 0; i
< nparts
; i
++) {
1392 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0 ||
1393 !fdisk_partition_is_used(pa
))
1395 x
= fdisk_partition_get_partno(pa
);
1400 fdisk_unref_partition(pa
);
1404 #ifdef HAVE_LIBREADLINE
1405 static char *sfdisk_fgets(struct fdisk_script
*dp
,
1406 char *buf
, size_t bufsz
, FILE *f
)
1408 struct sfdisk
*sf
= (struct sfdisk
*) fdisk_script_get_userdata(dp
);
1414 if (sf
->interactive
) {
1415 char *p
= readline(sf
->prompt
);
1421 if (len
> bufsz
- 2)
1424 memcpy(buf
, p
, len
);
1425 buf
[len
] = '\n'; /* append \n to be compatible with libc fgetc() */
1426 buf
[len
+ 1] = '\0';
1431 return fgets(buf
, bufsz
, f
);
1435 static int ignore_partition(struct fdisk_partition
*pa
)
1437 /* incomplete partition setting */
1438 if (!fdisk_partition_has_start(pa
) && !fdisk_partition_start_is_default(pa
))
1440 if (!fdisk_partition_has_size(pa
) && !fdisk_partition_end_is_default(pa
))
1443 /* probably dump from old sfdisk with start=0 size=0 */
1444 if (fdisk_partition_has_start(pa
) && fdisk_partition_get_start(pa
) == 0 &&
1445 fdisk_partition_has_size(pa
) && fdisk_partition_get_size(pa
) == 0)
1451 static void follow_wipe_mode(struct sfdisk
*sf
)
1453 int dowipe
= sf
->wipemode
== WIPEMODE_ALWAYS
? 1 : 0;
1455 if (sf
->interactive
&& sf
->wipemode
== WIPEMODE_AUTO
)
1456 dowipe
= 1; /* do it in interactive mode */
1458 if (fdisk_is_ptcollision(sf
->cxt
) && sf
->wipemode
!= WIPEMODE_NEVER
)
1459 dowipe
= 1; /* always wipe old PT */
1461 fdisk_enable_wipe(sf
->cxt
, dowipe
);
1466 if (!fdisk_is_ptcollision(sf
->cxt
)) {
1467 fdisk_info(sf
->cxt
, _("The old %s signature will be removed by a write command."),
1468 fdisk_get_collision(sf
->cxt
));
1469 fputc('\n', stderr
);
1472 fdisk_warnx(sf
->cxt
, _(
1473 "The old %s signature may remain on the device. "
1474 "It is recommended to wipe the device with wipefs(8) or "
1475 "sfdisk --wipe, in order to avoid possible collisions."),
1476 fdisk_get_collision(sf
->cxt
));
1477 fputc('\n', stderr
);
1481 static int wipe_partition(struct sfdisk
*sf
, size_t partno
)
1484 char *fstype
= NULL
;
1485 struct fdisk_partition
*tmp
= NULL
;
1487 DBG(MISC
, ul_debug("checking for signature"));
1489 rc
= fdisk_get_partition(sf
->cxt
, partno
, &tmp
);
1493 rc
= fdisk_partition_to_string(tmp
, sf
->cxt
, FDISK_FIELD_FSTYPE
, &fstype
);
1494 if (rc
|| fstype
== NULL
)
1497 fdisk_warnx(sf
->cxt
, _("Partition #%zu contains a %s signature."), partno
+ 1, fstype
);
1499 if (sf
->pwipemode
== WIPEMODE_AUTO
&& isatty(STDIN_FILENO
))
1500 fdisk_ask_yesno(sf
->cxt
, _("Do you want to remove the signature?"), &yes
);
1501 else if (sf
->pwipemode
== WIPEMODE_ALWAYS
)
1505 fdisk_info(sf
->cxt
, _("The signature will be removed by a write command."));
1506 rc
= fdisk_wipe_partition(sf
->cxt
, partno
, TRUE
);
1509 fdisk_unref_partition(tmp
);
1511 DBG(MISC
, ul_debug("partition wipe check end [rc=%d]", rc
));
1515 static void refresh_prompt_buffer(struct sfdisk
*sf
, const char *devname
,
1516 size_t next_partno
, int created
)
1519 char *partname
= fdisk_partname(devname
, next_partno
+ 1);
1521 err(EXIT_FAILURE
, _("failed to allocate partition name"));
1523 if (!sf
->prompt
|| !startswith(sf
->prompt
, partname
)) {
1525 xasprintf(&sf
->prompt
,"%s: ", partname
);
1528 } else if (!sf
->prompt
|| !startswith(sf
->prompt
, SFDISK_PROMPT
)) {
1530 sf
->prompt
= xstrdup(SFDISK_PROMPT
);
1535 * sfdisk <device> [[-N] <partno>]
1537 * Note that the option -N is there for backward compatibility only.
1539 static int command_fdisk(struct sfdisk
*sf
, int argc
, char **argv
)
1541 int rc
= 0, partno
= sf
->partno
, created
= 0, unused
= 0;
1542 struct fdisk_script
*dp
;
1543 struct fdisk_table
*tb
= NULL
;
1544 const char *devname
= NULL
, *label
;
1546 size_t next_partno
= (size_t) -1;
1550 if (partno
< 0 && argc
> 1)
1551 partno
= strtou32_or_err(argv
[1],
1552 _("failed to parse partition number"));
1554 errx(EXIT_FAILURE
, _("no disk device specified"));
1556 rc
= fdisk_assign_device(sf
->cxt
, devname
, 0);
1558 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
1560 dp
= fdisk_new_script(sf
->cxt
);
1562 err(EXIT_FAILURE
, _("failed to allocate script handler"));
1563 fdisk_set_script(sf
->cxt
, dp
);
1564 #ifdef HAVE_LIBREADLINE
1565 fdisk_script_set_fgets(dp
, sfdisk_fgets
);
1567 fdisk_script_set_userdata(dp
, (void *) sf
);
1570 * Don't create a new disklabel when [-N] <partno> specified. In this
1571 * case reuse already specified disklabel. Let's check that the disk
1572 * really contains the partition.
1577 if (!fdisk_has_label(sf
->cxt
))
1578 errx(EXIT_FAILURE
, _("%s: cannot modify partition %d: "
1579 "no partition table was found"),
1580 devname
, partno
+ 1);
1581 n
= fdisk_get_npartitions(sf
->cxt
);
1582 if ((size_t) partno
> n
)
1583 errx(EXIT_FAILURE
, _("%s: cannot modify partition %d: "
1584 "partition table contains only %zu "
1586 devname
, partno
+ 1, n
);
1588 if (!fdisk_is_partition_used(sf
->cxt
, partno
)) {
1589 fdisk_warnx(sf
->cxt
, _("warning: %s: partition %d is not defined yet"),
1590 devname
, partno
+ 1);
1594 next_partno
= partno
;
1597 sf
->orig_pa
= get_partition(sf
->cxt
, partno
);
1602 next_partno
= last_pt_partno(sf
) + 1;
1605 if (!sf
->quiet
&& sf
->interactive
) {
1606 color_scheme_enable("welcome", UL_COLOR_GREEN
);
1607 fdisk_info(sf
->cxt
, _("\nWelcome to sfdisk (%s)."), PACKAGE_STRING
);
1609 fdisk_info(sf
->cxt
, _("Changes will remain in memory only, until you decide to write them.\n"
1610 "Be careful before using the write command.\n"));
1613 if (!sf
->noact
&& !sf
->noreread
) {
1615 fputs(_("Checking that no-one is using this disk right now ..."), stdout
);
1616 if (fdisk_device_is_used(sf
->cxt
)) {
1618 fputs(_(" FAILED\n\n"), stdout
);
1620 fdisk_warnx(sf
->cxt
, _(
1621 "This disk is currently in use - repartitioning is probably a bad idea.\n"
1622 "Umount all file systems, and swapoff all swap partitions on this disk.\n"
1623 "Use the --no-reread flag to suppress this check.\n"));
1626 errx(EXIT_FAILURE
, _("Use the --force flag to overrule all checks."));
1627 } else if (!sf
->quiet
)
1628 fputs(_(" OK\n\n"), stdout
);
1631 if (fdisk_get_collision(sf
->cxt
))
1632 follow_wipe_mode(sf
);
1635 backup_partition_table(sf
, devname
);
1638 list_disk_geometry(sf
->cxt
);
1639 if (fdisk_has_label(sf
->cxt
)) {
1640 fdisk_info(sf
->cxt
, _("\nOld situation:"));
1641 list_disklabel(sf
->cxt
);
1647 else if (fdisk_has_label(sf
->cxt
))
1648 label
= fdisk_label_get_name(fdisk_get_label(sf
->cxt
, NULL
));
1650 label
= "dos"; /* just for backward compatibility */
1652 fdisk_script_set_header(dp
, "label", label
);
1655 if (!sf
->quiet
&& sf
->interactive
) {
1656 if (!fdisk_has_label(sf
->cxt
) && !sf
->label
)
1658 _("\nsfdisk is going to create a new '%s' disk label.\n"
1659 "Use 'label: <name>' before you define a first partition\n"
1660 "to override the default."), label
);
1661 fdisk_info(sf
->cxt
, _("\nType 'help' to get more information.\n"));
1662 } else if (!sf
->quiet
)
1663 fputc('\n', stdout
);
1665 tb
= fdisk_script_get_table(dp
);
1671 DBG(PARSE
, ul_debug("<---next-line--->"));
1672 if (next_partno
== (size_t) -1)
1673 next_partno
= fdisk_table_get_nents(tb
);
1677 && next_partno
== fdisk_get_npartitions(sf
->cxt
)
1678 && !has_container(sf
)) {
1679 fdisk_info(sf
->cxt
, _("All partitions used."));
1680 rc
= SFDISK_DONE_ASK
;
1684 refresh_prompt_buffer(sf
, devname
, next_partno
, created
);
1687 if (sf
->prompt
&& (sf
->interactive
|| !sf
->quiet
)) {
1688 #ifndef HAVE_LIBREADLINE
1689 fputs(sf
->prompt
, stdout
);
1691 if (!sf
->interactive
)
1692 fputs(sf
->prompt
, stdout
);
1696 rc
= fdisk_script_read_line(dp
, stdin
, buf
, sizeof(buf
));
1698 DBG(PARSE
, ul_debug("script parsing failed, trying sfdisk specific commands"));
1699 buf
[sizeof(buf
) - 1] = '\0';
1700 rc
= loop_control_commands(sf
, dp
, buf
);
1704 } else if (rc
== 1) {
1705 rc
= SFDISK_DONE_EOF
;
1707 fputs(_("Done.\n"), stdout
);
1711 nparts
= fdisk_table_get_nents(tb
);
1714 struct fdisk_partition
*pa
= fdisk_table_get_partition(tb
, nparts
- 1);
1718 if (ignore_partition(pa
)) {
1719 fdisk_info(sf
->cxt
, _("Ignoring partition."));
1723 if (!created
) { /* create a new disklabel */
1724 rc
= fdisk_apply_script_headers(sf
->cxt
, dp
);
1727 fdisk_warnx(sf
->cxt
, _(
1728 "Failed to apply script headers, "
1729 "disk label not created."));
1731 if (rc
== 0 && fdisk_get_collision(sf
->cxt
))
1732 follow_wipe_mode(sf
);
1734 if (!rc
&& partno
>= 0) { /* -N <partno>, modify partition */
1735 rc
= fdisk_set_partition(sf
->cxt
, partno
, pa
);
1736 rc
= rc
== 0 ? SFDISK_DONE_ASK
: SFDISK_DONE_ABORT
;
1738 } else if (!rc
) { /* add partition */
1739 if (!sf
->interactive
&& !sf
->quiet
&&
1740 (!sf
->prompt
|| startswith(sf
->prompt
, SFDISK_PROMPT
))) {
1741 refresh_prompt_buffer(sf
, devname
, next_partno
, created
);
1742 fputs(sf
->prompt
, stdout
);
1744 rc
= fdisk_add_partition(sf
->cxt
, pa
, &cur_partno
);
1747 fdisk_warn(sf
->cxt
, _("Failed to add #%d partition"), next_partno
+ 1);
1751 /* wipe partition on success
1753 * Note that unused=1 means -N <partno> for unused,
1754 * otherwise we wipe only newly created partitions.
1756 if (rc
== 0 && (unused
|| partno
< 0)) {
1757 rc
= wipe_partition(sf
, unused
? (size_t) partno
: cur_partno
);
1763 /* success print result */
1764 if (sf
->interactive
)
1765 sfdisk_print_partition(sf
, cur_partno
);
1766 next_partno
= cur_partno
+ 1;
1767 } else if (pa
) /* error, drop partition from script */
1768 fdisk_table_remove_partition(tb
, pa
);
1770 fdisk_info(sf
->cxt
, _("Script header accepted."));
1772 if (rc
&& !sf
->interactive
) {
1773 rc
= SFDISK_DONE_ABORT
;
1778 /* create empty disk label if label, but no partition specified */
1779 if ((rc
== SFDISK_DONE_EOF
|| rc
== SFDISK_DONE_WRITE
) && created
== 0
1780 && fdisk_script_has_force_label(dp
) == 1
1781 && fdisk_table_get_nents(tb
) == 0
1782 && fdisk_script_get_header(dp
, "label")) {
1784 int xrc
= fdisk_apply_script_headers(sf
->cxt
, dp
);
1787 fdisk_warnx(sf
->cxt
, _(
1788 "Failed to apply script headers, "
1789 "disk label not created."));
1790 rc
= SFDISK_DONE_ABORT
;
1794 if (!sf
->quiet
&& rc
!= SFDISK_DONE_ABORT
) {
1795 fdisk_info(sf
->cxt
, _("\nNew situation:"));
1796 list_disk_identifier(sf
->cxt
);
1797 list_disklabel(sf
->cxt
);
1801 case SFDISK_DONE_ASK
:
1802 case SFDISK_DONE_EOF
:
1803 if (sf
->interactive
) {
1805 fdisk_ask_yesno(sf
->cxt
, _("Do you want to write this to disk?"), &yes
);
1807 fdisk_info(sf
->cxt
, _("Leaving."));
1813 case SFDISK_DONE_WRITE
:
1814 rc
= write_changes(sf
);
1816 case SFDISK_DONE_ABORT
:
1817 default: /* rc < 0 on error */
1818 fdisk_info(sf
->cxt
, _("Leaving.\n"));
1822 fdisk_unref_script(dp
);
1826 static void __attribute__((__noreturn__
)) usage(void)
1829 fputs(USAGE_HEADER
, out
);
1832 _(" %1$s [options] <dev> [[-N] <part>]\n"
1833 " %1$s [options] <command>\n"), program_invocation_short_name
);
1835 fputs(USAGE_SEPARATOR
, out
);
1836 fputs(_("Display or manipulate a disk partition table.\n"), out
);
1838 fputs(USAGE_COMMANDS
, out
);
1839 fputs(_(" -A, --activate <dev> [<part> ...] list or set bootable (P)MBR partitions\n"), out
);
1840 fputs(_(" -d, --dump <dev> dump partition table (usable for later input)\n"), out
);
1841 fputs(_(" -J, --json <dev> dump partition table in JSON format\n"), out
);
1842 fputs(_(" -g, --show-geometry [<dev> ...] list geometry of all or specified devices\n"), out
);
1843 fputs(_(" -l, --list [<dev> ...] list partitions of each device\n"), out
);
1844 fputs(_(" -F, --list-free [<dev> ...] list unpartitioned free areas of each device\n"), out
);
1845 fputs(_(" -r, --reorder <dev> fix partitions order (by start offset)\n"), out
);
1846 fputs(_(" -s, --show-size [<dev> ...] list sizes of all or specified devices\n"), out
);
1847 fputs(_(" -T, --list-types print the recognized types (see -X)\n"), out
);
1848 fputs(_(" -V, --verify [<dev> ...] test whether partitions seem correct\n"), out
);
1849 fputs(_(" --delete <dev> [<part> ...] delete all or specified partitions\n"), out
);
1851 fputs(USAGE_SEPARATOR
, out
);
1852 fputs(_(" --part-label <dev> <part> [<str>] print or change partition label\n"), out
);
1853 fputs(_(" --part-type <dev> <part> [<type>] print or change partition type\n"), out
);
1854 fputs(_(" --part-uuid <dev> <part> [<uuid>] print or change partition uuid\n"), out
);
1855 fputs(_(" --part-attrs <dev> <part> [<str>] print or change partition attributes\n"), out
);
1857 fputs(USAGE_SEPARATOR
, out
);
1858 fputs(_(" <dev> device (usually disk) path\n"), out
);
1859 fputs(_(" <part> partition number\n"), out
);
1860 fputs(_(" <type> partition type, GUID for GPT, hex for MBR\n"), out
);
1862 fputs(USAGE_OPTIONS
, out
);
1863 fputs(_(" -a, --append append partitions to existing partition table\n"), out
);
1864 fputs(_(" -b, --backup backup partition table sectors (see -O)\n"), out
);
1865 fputs(_(" --bytes print SIZE in bytes rather than in human readable format\n"), out
);
1866 fputs(_(" --move-data[=<typescript>] move partition data after relocation (requires -N)\n"), out
);
1867 fputs(_(" -f, --force disable all consistency checking\n"), out
);
1868 fputs(_(" --color[=<when>] colorize output (auto, always or never)\n"), out
);
1870 " %s\n", USAGE_COLORS_DEFAULT
);
1871 fputs(_(" -N, --partno <num> specify partition number\n"), out
);
1872 fputs(_(" -n, --no-act do everything except write to device\n"), out
);
1873 fputs(_(" --no-reread do not check whether the device is in use\n"), out
);
1874 fputs(_(" --no-tell-kernel do not tell kernel about changes\n"), out
);
1875 fputs(_(" -O, --backup-file <path> override default backup file name\n"), out
);
1876 fputs(_(" -o, --output <list> output columns\n"), out
);
1877 fputs(_(" -q, --quiet suppress extra info messages\n"), out
);
1878 fputs(_(" -w, --wipe <mode> wipe signatures (auto, always or never)\n"), out
);
1879 fputs(_(" -W, --wipe-partitions <mode> wipe signatures from new partitions (auto, always or never)\n"), out
);
1880 fputs(_(" -X, --label <name> specify label type (dos, gpt, ...)\n"), out
);
1881 fputs(_(" -Y, --label-nested <name> specify nested label type (dos, bsd)\n"), out
);
1882 fputs(USAGE_SEPARATOR
, out
);
1883 fputs(_(" -G, --show-pt-geometry deprecated, alias to --show-geometry\n"), out
);
1884 fputs(_(" -L, --Linux deprecated, only for backward compatibility\n"), out
);
1885 fputs(_(" -u, --unit S deprecated, only sector unit is supported\n"), out
);
1887 fputs(USAGE_SEPARATOR
, out
);
1888 printf( " -h, --help %s\n", USAGE_OPTSTR_HELP
);
1889 printf( " -v, --version %s\n", USAGE_OPTSTR_VERSION
);
1891 list_available_columns(out
);
1893 printf(USAGE_MAN_TAIL("sfdisk(8)"));
1898 int main(int argc
, char *argv
[])
1900 const char *outarg
= NULL
;
1901 int rc
= -EINVAL
, c
, longidx
= -1, bytes
= 0;
1902 int colormode
= UL_COLORMODE_UNDEF
;
1903 struct sfdisk _sf
= {
1905 .wipemode
= WIPEMODE_AUTO
,
1906 .pwipemode
= WIPEMODE_AUTO
,
1907 .interactive
= isatty(STDIN_FILENO
) ? 1 : 0,
1911 OPT_CHANGE_ID
= CHAR_MAX
+ 1,
1926 static const struct option longopts
[] = {
1927 { "activate",no_argument
, NULL
, 'A' },
1928 { "append", no_argument
, NULL
, 'a' },
1929 { "backup", no_argument
, NULL
, 'b' },
1930 { "backup-file", required_argument
, NULL
, 'O' },
1931 { "bytes", no_argument
, NULL
, OPT_BYTES
},
1932 { "color", optional_argument
, NULL
, OPT_COLOR
},
1933 { "delete", no_argument
, NULL
, OPT_DELETE
},
1934 { "dump", no_argument
, NULL
, 'd' },
1935 { "help", no_argument
, NULL
, 'h' },
1936 { "force", no_argument
, NULL
, 'f' },
1937 { "json", no_argument
, NULL
, 'J' },
1938 { "label", required_argument
, NULL
, 'X' },
1939 { "label-nested", required_argument
, NULL
, 'Y' },
1940 { "list", no_argument
, NULL
, 'l' },
1941 { "list-free", no_argument
, NULL
, 'F' },
1942 { "list-types", no_argument
, NULL
, 'T' },
1943 { "no-act", no_argument
, NULL
, 'n' },
1944 { "no-reread", no_argument
, NULL
, OPT_NOREREAD
},
1945 { "no-tell-kernel", no_argument
, NULL
, OPT_NOTELL
},
1946 { "move-data", optional_argument
, NULL
, OPT_MOVEDATA
},
1947 { "output", required_argument
, NULL
, 'o' },
1948 { "partno", required_argument
, NULL
, 'N' },
1949 { "reorder", no_argument
, NULL
, 'r' },
1950 { "show-geometry", no_argument
, NULL
, 'g' },
1951 { "quiet", no_argument
, NULL
, 'q' },
1952 { "verify", no_argument
, NULL
, 'V' },
1953 { "version", no_argument
, NULL
, 'v' },
1954 { "wipe", required_argument
, NULL
, 'w' },
1955 { "wipe-partitions", required_argument
, NULL
, 'W' },
1957 { "part-uuid", no_argument
, NULL
, OPT_PARTUUID
},
1958 { "part-label", no_argument
, NULL
, OPT_PARTLABEL
},
1959 { "part-type", no_argument
, NULL
, OPT_PARTTYPE
},
1960 { "part-attrs", no_argument
, NULL
, OPT_PARTATTRS
},
1962 { "show-pt-geometry", no_argument
, NULL
, 'G' }, /* deprecated */
1963 { "unit", required_argument
, NULL
, 'u' }, /* deprecated */
1964 { "Linux", no_argument
, NULL
, 'L' }, /* deprecated */
1965 { "show-size", no_argument
, NULL
, 's' }, /* deprecated */
1967 { "change-id",no_argument
, NULL
, OPT_CHANGE_ID
}, /* deprecated */
1968 { "id", no_argument
, NULL
, 'c' }, /* deprecated */
1969 { "print-id",no_argument
, NULL
, OPT_PRINT_ID
}, /* deprecated */
1971 { NULL
, 0, NULL
, 0 },
1973 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
1974 { 's','u'}, /* --show-size --unit */
1977 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
1980 setlocale(LC_ALL
, "");
1981 bindtextdomain(PACKAGE
, LOCALEDIR
);
1982 textdomain(PACKAGE
);
1983 atexit(close_stdout
);
1985 while ((c
= getopt_long(argc
, argv
, "aAbcdfFgGhJlLo:O:nN:qrsTu:vVX:Y:w:W:",
1986 longopts
, &longidx
)) != -1) {
1988 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1992 sf
->act
= ACT_ACTIVATE
;
2003 warnx(_("%s is deprecated in favour of --part-type"),
2004 longopts
[longidx
].name
);
2005 sf
->act
= ACT_PARTTYPE
;
2008 warnx(_("--id is deprecated in favour of --part-type"));
2009 sf
->act
= ACT_PARTTYPE
;
2018 sf
->act
= ACT_LIST_FREE
;
2024 warnx(_("--show-pt-geometry is no more implemented. Using --show-geometry."));
2027 sf
->act
= ACT_SHOW_GEOM
;
2036 warnx(_("--Linux option is unnecessary and deprecated"));
2043 sf
->backup_file
= optarg
;
2049 sf
->partno
= strtou32_or_err(optarg
, _("failed to parse partition number")) - 1;
2055 sf
->act
= ACT_REORDER
;
2058 sf
->act
= ACT_SHOW_SIZE
;
2061 sf
->act
= ACT_LIST_TYPES
;
2065 errx(EXIT_FAILURE
, _("unsupported unit '%c'"), *optarg
);
2068 printf(_("%s from %s\n"), program_invocation_short_name
,
2070 return EXIT_SUCCESS
;
2075 sf
->wipemode
= wipemode_from_string(optarg
);
2076 if (sf
->wipemode
< 0)
2077 errx(EXIT_FAILURE
, _("unsupported wipe mode"));
2080 sf
->pwipemode
= wipemode_from_string(optarg
);
2081 if (sf
->pwipemode
< 0)
2082 errx(EXIT_FAILURE
, _("unsupported wipe mode"));
2088 sf
->label_nested
= optarg
;
2092 sf
->act
= ACT_PARTUUID
;
2095 sf
->act
= ACT_PARTTYPE
;
2098 sf
->act
= ACT_PARTLABEL
;
2101 sf
->act
= ACT_PARTATTRS
;
2110 colormode
= UL_COLORMODE_AUTO
;
2112 colormode
= colormode_or_err(optarg
,
2113 _("unsupported color mode"));
2117 sf
->move_typescript
= optarg
;
2120 sf
->act
= ACT_DELETE
;
2126 errtryhelp(EXIT_FAILURE
);
2130 colors_init(colormode
, "sfdisk");
2134 fdisk_set_size_unit(sf
->cxt
, FDISK_SIZEUNIT_BYTES
);
2137 init_fields(NULL
, outarg
, NULL
);
2139 if (sf
->verify
&& !sf
->act
)
2140 sf
->act
= ACT_VERIFY
; /* --verify make be used with --list too */
2142 sf
->act
= ACT_FDISK
; /* default */
2144 if (sf
->movedata
&& !(sf
->act
== ACT_FDISK
&& sf
->partno
>= 0))
2145 errx(EXIT_FAILURE
, _("--movedata requires -N"));
2149 rc
= command_activate(sf
, argc
- optind
, argv
+ optind
);
2153 rc
= command_delete(sf
, argc
- optind
, argv
+ optind
);
2157 rc
= command_list_partitions(sf
, argc
- optind
, argv
+ optind
);
2160 case ACT_LIST_TYPES
:
2161 rc
= command_list_types(sf
);
2165 rc
= command_list_freespace(sf
, argc
- optind
, argv
+ optind
);
2169 rc
= command_fdisk(sf
, argc
- optind
, argv
+ optind
);
2173 rc
= command_dump(sf
, argc
- optind
, argv
+ optind
);
2177 rc
= command_show_size(sf
, argc
- optind
, argv
+ optind
);
2181 rc
= command_show_geometry(sf
, argc
- optind
, argv
+ optind
);
2185 rc
= command_verify(sf
, argc
- optind
, argv
+ optind
);
2189 rc
= command_parttype(sf
, argc
- optind
, argv
+ optind
);
2193 rc
= command_partuuid(sf
, argc
- optind
, argv
+ optind
);
2197 rc
= command_partlabel(sf
, argc
- optind
, argv
+ optind
);
2201 rc
= command_partattrs(sf
, argc
- optind
, argv
+ optind
);
2205 rc
= command_reorder(sf
, argc
- optind
, argv
+ optind
);
2211 DBG(MISC
, ul_debug("bye! [rc=%d]", rc
));
2212 return rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;