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 # include <readline/readline.h>
45 #include "closestream.h"
53 #include "fdisk-list.h"
56 * sfdisk debug stuff (see fdisk.h and include/debug.h)
58 UL_DEBUG_DEFINE_MASK(sfdisk
);
59 UL_DEBUG_DEFINE_MASKNAMES(sfdisk
) = UL_DEBUG_EMPTY_MASKNAMES
;
61 #define SFDISKPROG_DEBUG_INIT (1 << 1)
62 #define SFDISKPROG_DEBUG_PARSE (1 << 2)
63 #define SFDISKPROG_DEBUG_MISC (1 << 3)
64 #define SFDISKPROG_DEBUG_ASK (1 << 4)
65 #define SFDISKPROG_DEBUG_ALL 0xFFFF
67 #define DBG(m, x) __UL_DBG(sfdisk, SFDISKPROG_DEBUG_, m, x)
68 #define ON_DBG(m, x) __UL_DBG_CALL(sfdisk, SFDISKPROG_DEBUG_, m, x)
91 int partno
; /* -N <partno>, default -1 */
92 int wipemode
; /* remove foreign signatures from disk */
93 int pwipemode
; /* remove foreign signatures from partitions */
94 const char *label
; /* --label <label> */
95 const char *label_nested
; /* --label-nested <label> */
96 const char *backup_file
; /* -O <path> */
97 const char *move_typescript
; /* --movedata <typescript> */
100 struct fdisk_context
*cxt
; /* libfdisk context */
101 struct fdisk_partition
*orig_pa
; /* -N <partno> before the change */
103 unsigned int verify
: 1, /* call fdisk_verify_disklabel() */
104 quiet
: 1, /* suppress extra messages */
105 interactive
: 1, /* running on tty */
106 noreread
: 1, /* don't check device is in use */
107 force
: 1, /* do also stupid things */
108 backup
: 1, /* backup sectors before write PT */
109 container
: 1, /* PT contains container (MBR extended) partitions */
110 append
: 1, /* don't create new PT, append partitions only */
111 json
: 1, /* JSON dump */
112 movedata
: 1, /* move data after resize */
113 notell
: 1, /* don't tell kernel aout new PT */
114 noact
: 1; /* do not write to device */
117 #define SFDISK_PROMPT ">>> "
119 static void sfdiskprog_init_debug(void)
121 __UL_INIT_DEBUG(sfdisk
, SFDISKPROG_DEBUG_
, 0, SFDISK_DEBUG
);
125 static int get_user_reply(const char *prompt
, char *buf
, size_t bufsz
)
130 #ifdef HAVE_LIBREADLINE
131 if (isatty(STDIN_FILENO
)) {
132 p
= readline(prompt
);
135 memcpy(buf
, p
, bufsz
);
140 fputs(prompt
, stdout
);
143 if (!fgets(buf
, bufsz
, stdin
))
147 for (p
= buf
; *p
&& !isgraph(*p
); p
++); /* get first non-blank */
150 memmove(buf
, p
, p
- buf
); /* remove blank space */
152 if (sz
&& *(buf
+ sz
- 1) == '\n')
153 *(buf
+ sz
- 1) = '\0';
155 DBG(ASK
, ul_debug("user's reply: >>>%s<<<", buf
));
159 static int ask_callback(struct fdisk_context
*cxt
__attribute__((__unused__
)),
160 struct fdisk_ask
*ask
,
163 struct sfdisk
*sf
= (struct sfdisk
*) data
;
168 switch(fdisk_ask_get_type(ask
)) {
169 case FDISK_ASKTYPE_INFO
:
172 fputs(fdisk_ask_print_get_mesg(ask
), stdout
);
175 case FDISK_ASKTYPE_WARNX
:
177 color_scheme_fenable("warn", UL_COLOR_RED
, stderr
);
178 fputs(fdisk_ask_print_get_mesg(ask
), stderr
);
179 color_fdisable(stderr
);
182 case FDISK_ASKTYPE_WARN
:
184 color_scheme_fenable("warn", UL_COLOR_RED
, stderr
);
185 fputs(fdisk_ask_print_get_mesg(ask
), stderr
);
186 errno
= fdisk_ask_print_get_errno(ask
);
187 fprintf(stderr
, ": %m\n");
188 color_fdisable(stderr
);
190 case FDISK_ASKTYPE_YESNO
:
196 fputs(fdisk_ask_get_query(ask
), stdout
);
197 rc
= get_user_reply(_(" [Y]es/[N]o: "), buf
, sizeof(buf
));
201 if (x
== RPMATCH_YES
|| x
== RPMATCH_NO
) {
202 fdisk_ask_yesno_set_result(ask
, x
);
206 DBG(ASK
, ul_debug("yes-no ask: reply '%s' [rc=%d]", buf
, rc
));
215 static void sfdisk_init(struct sfdisk
*sf
)
219 sfdiskprog_init_debug();
221 sf
->cxt
= fdisk_new_context();
223 err(EXIT_FAILURE
, _("failed to allocate libfdisk context"));
224 fdisk_set_ask(sf
->cxt
, ask_callback
, (void *) sf
);
225 fdisk_enable_bootbits_protection(sf
->cxt
, 1);
227 if (sf
->label_nested
) {
228 struct fdisk_context
*x
= fdisk_new_nested_context(sf
->cxt
,
231 err(EXIT_FAILURE
, _("failed to allocate nested libfdisk context"));
232 /* the original context is available by fdisk_get_parent() */
237 static int sfdisk_deinit(struct sfdisk
*sf
)
239 struct fdisk_context
*parent
;
244 parent
= fdisk_get_parent(sf
->cxt
);
246 fdisk_unref_context(sf
->cxt
);
250 fdisk_unref_context(sf
->cxt
);
253 memset(sf
, 0, sizeof(*sf
));
257 static struct fdisk_partition
*get_partition(struct fdisk_context
*cxt
, size_t partno
)
259 struct fdisk_table
*tb
= NULL
;
260 struct fdisk_partition
*pa
;
262 if (fdisk_get_partitions(cxt
, &tb
) != 0)
265 pa
= fdisk_table_get_partition_by_partno(tb
, partno
);
267 fdisk_ref_partition(pa
);
268 fdisk_unref_table(tb
);
272 static void backup_sectors(struct sfdisk
*sf
,
276 uint64_t offset
, size_t size
)
281 devfd
= fdisk_get_devfd(sf
->cxt
);
284 xasprintf(&fname
, "%s0x%08"PRIx64
".bak", tpl
, offset
);
286 fd
= open(fname
, O_CREAT
| O_WRONLY
, S_IRUSR
| S_IWUSR
);
290 if (lseek(devfd
, (off_t
) offset
, SEEK_SET
) == (off_t
) -1) {
291 fdisk_warn(sf
->cxt
, _("cannot seek %s"), devname
);
294 unsigned char *buf
= xmalloc(size
);
296 if (read_all(devfd
, (char *) buf
, size
) != (ssize_t
) size
) {
297 fdisk_warn(sf
->cxt
, _("cannot read %s"), devname
);
301 if (write_all(fd
, buf
, size
) != 0) {
302 fdisk_warn(sf
->cxt
, _("cannot write %s"), fname
);
309 fdisk_info(sf
->cxt
, _("%12s (offset %5ju, size %5ju): %s"),
310 name
, (uintmax_t) offset
, (uintmax_t) size
, fname
);
315 errx(EXIT_FAILURE
, _("%s: failed to create a backup"), devname
);
318 static char *mk_backup_filename_tpl(const char *filename
, const char *devname
, const char *suffix
)
321 char *name
, *buf
= xstrdup(devname
);
323 name
= basename(buf
);
326 const char *home
= getenv ("HOME");
328 errx(EXIT_FAILURE
, _("failed to create a backup file, $HOME undefined"));
329 xasprintf(&tpl
, "%s/sfdisk-%s%s", home
, name
, suffix
);
331 xasprintf(&tpl
, "%s-%s%s", filename
, name
, suffix
);
338 static void backup_partition_table(struct sfdisk
*sf
, const char *devname
)
348 if (!fdisk_has_label(sf
->cxt
))
351 tpl
= mk_backup_filename_tpl(sf
->backup_file
, devname
, "-");
353 color_scheme_enable("header", UL_COLOR_BOLD
);
354 fdisk_info(sf
->cxt
, _("Backup files:"));
357 while (fdisk_locate_disklabel(sf
->cxt
, i
++, &name
, &offset
, &size
) == 0 && size
)
358 backup_sectors(sf
, tpl
, name
, devname
, offset
, size
);
365 static int move_partition_data(struct sfdisk
*sf
, size_t partno
, struct fdisk_partition
*orig_pa
)
367 struct fdisk_partition
*pa
= get_partition(sf
->cxt
, partno
);
368 char *devname
= NULL
, *typescript
= NULL
, *buf
= NULL
;
370 int ok
= 0, fd
, backward
= 0;
371 fdisk_sector_t nsectors
, from
, to
, step
, i
;
372 size_t ss
, step_bytes
, cc
;
376 assert(sf
->movedata
);
379 warnx(_("failed to read new partition from device; ignoring --move-data"));
380 else if (!fdisk_partition_has_size(pa
))
381 warnx(_("failed to get size of the new partition; ignoring --move-data"));
382 else if (!fdisk_partition_has_start(pa
))
383 warnx(_("failed to get start of the new partition; ignoring --move-data"));
384 else if (!fdisk_partition_has_size(orig_pa
))
385 warnx(_("failed to get size of the old partition; ignoring --move-data"));
386 else if (!fdisk_partition_has_start(orig_pa
))
387 warnx(_("failed to get start of the old partition; ignoring --move-data"));
388 else if (fdisk_partition_get_start(pa
) == fdisk_partition_get_start(orig_pa
))
389 warnx(_("start of the partition has not been moved; ignoring --move-data"));
390 else if (fdisk_partition_get_size(orig_pa
) < fdisk_partition_get_size(pa
))
391 warnx(_("new partition is smaller than original; ignoring --move-data"));
397 DBG(MISC
, ul_debug("moving data"));
399 fd
= fdisk_get_devfd(sf
->cxt
);
401 ss
= fdisk_get_sector_size(sf
->cxt
);
402 nsectors
= fdisk_partition_get_size(orig_pa
);
403 from
= fdisk_partition_get_start(orig_pa
);
404 to
= fdisk_partition_get_start(pa
);
406 if ((to
>= from
&& from
+ nsectors
>= to
) ||
407 (from
>= to
&& to
+ nsectors
>= from
)) {
408 /* source and target overlay, check if we need to copy
409 * backwardly from end of the source */
410 DBG(MISC
, ul_debug("overlay between source and target"));
411 backward
= from
< to
;
412 DBG(MISC
, ul_debug(" copy order: %s", backward
? "backward" : "forward"));
414 step
= from
> to
? from
- to
: to
- from
;
420 /* make step usable for malloc() */
421 if (step
* ss
> (getpagesize() * 256U))
422 step
= (getpagesize() * 256) / ss
;
424 /* align the step (note that nsectors does not have to be power of 2) */
425 while (nsectors
% step
)
428 step_bytes
= step
* ss
;
429 DBG(MISC
, ul_debug(" step: %ju (%zu bytes)", (uintmax_t)step
, step_bytes
));
431 #if defined(POSIX_FADV_SEQUENTIAL) && defined(HAVE_POSIX_FADVISE)
433 posix_fadvise(fd
, from
* ss
, nsectors
* ss
, POSIX_FADV_SEQUENTIAL
);
435 devname
= fdisk_partname(fdisk_get_devname(sf
->cxt
), partno
+1);
436 typescript
= mk_backup_filename_tpl(sf
->move_typescript
, devname
, ".move");
439 fdisk_info(sf
->cxt
,"");
440 color_scheme_enable("header", UL_COLOR_BOLD
);
441 fdisk_info(sf
->cxt
, _("Data move:"));
443 fdisk_info(sf
->cxt
, _(" typescript file: %s"), typescript
);
444 printf(_(" old start: %ju, new start: %ju (move %ju sectors)\n"),
445 (uintmax_t) from
, (uintmax_t) to
, (uintmax_t) nsectors
);
449 if (sf
->interactive
) {
451 fdisk_ask_yesno(sf
->cxt
, _("Do you want to move partition data?"), &yes
);
453 fdisk_info(sf
->cxt
, _("Leaving."));
458 f
= fopen(typescript
, "w");
462 /* don't translate */
463 fprintf(f
, "# sfdisk: " PACKAGE_STRING
"\n");
464 fprintf(f
, "# Disk: %s\n", devname
);
465 fprintf(f
, "# Partition: %zu\n", partno
+ 1);
466 fprintf(f
, "# Operation: move data\n");
467 fprintf(f
, "# Original start offset (sectors/bytes): %ju/%ju\n",
468 (uintmax_t)from
, (uintmax_t)from
* ss
);
469 fprintf(f
, "# New start offset (sectors/bytes): %ju/%ju\n",
470 (uintmax_t)to
, (uintmax_t)to
* ss
);
471 fprintf(f
, "# Area size (sectors/bytes): %ju/%ju\n",
472 (uintmax_t)nsectors
, (uintmax_t)nsectors
* ss
);
473 fprintf(f
, "# Sector size: %zu\n", ss
);
474 fprintf(f
, "# Step size (in bytes): %zu\n", step_bytes
);
475 fprintf(f
, "# Steps: %ju\n", (uintmax_t)(nsectors
/ step
));
477 fprintf(f
, "# <step>: <from> <to> (step offsets in bytes)\n");
479 src
= (backward
? from
+ nsectors
: from
) * ss
;
480 dst
= (backward
? to
+ nsectors
: to
) * ss
;
481 buf
= xmalloc(step_bytes
);
483 DBG(MISC
, ul_debug(" initial: src=%ju dst=%ju", src
, dst
));
485 for (cc
= 1, i
= 0; i
< nsectors
; i
+= step
, cc
++) {
489 src
-= step_bytes
, dst
-= step_bytes
;
491 DBG(MISC
, ul_debug("#%05zu: src=%ju dst=%ju", cc
, src
, dst
));
494 if (lseek(fd
, src
, SEEK_SET
) == (off_t
) -1)
496 rc
= read(fd
, buf
, step_bytes
);
497 if (rc
< 0 || rc
!= (ssize_t
) step_bytes
)
501 if (lseek(fd
, dst
, SEEK_SET
) == (off_t
) -1)
503 rc
= write(fd
, buf
, step_bytes
);
504 if (rc
< 0 || rc
!= (ssize_t
) step_bytes
)
509 fprintf(f
, "%05zu: %12ju %12ju\n", cc
, src
, dst
);
511 #if defined(POSIX_FADV_DONTNEED) && defined(HAVE_POSIX_FADVISE)
512 posix_fadvise(fd
, src
, step_bytes
, POSIX_FADV_DONTNEED
);
515 src
+= step_bytes
, dst
+= step_bytes
;
526 warn(_("%s: failed to move data"), devname
);
536 static int write_changes(struct sfdisk
*sf
)
541 fdisk_info(sf
->cxt
, _("The partition table is unchanged (--no-act)."));
543 rc
= fdisk_write_disklabel(sf
->cxt
);
544 if (rc
== 0 && sf
->movedata
&& sf
->orig_pa
)
545 rc
= move_partition_data(sf
, sf
->partno
, sf
->orig_pa
);
547 fdisk_info(sf
->cxt
, _("\nThe partition table has been altered."));
549 fdisk_reread_partition_table(sf
->cxt
);
553 rc
= fdisk_deassign_device(sf
->cxt
,
554 sf
->noact
|| sf
->notell
); /* no-sync */
559 * sfdisk --list [<device ..]
561 static int command_list_partitions(struct sfdisk
*sf
, int argc
, char **argv
)
564 fdisk_enable_listonly(sf
->cxt
, 1);
569 for (i
= 0; i
< argc
; i
++) {
571 fputs("\n\n", stdout
);
572 if (print_device_pt(sf
->cxt
, argv
[i
], 1, sf
->verify
) != 0)
577 print_all_devices_pt(sf
->cxt
, sf
->verify
);
583 * sfdisk --list-free [<device ..]
585 static int command_list_freespace(struct sfdisk
*sf
, int argc
, char **argv
)
588 fdisk_enable_listonly(sf
->cxt
, 1);
593 for (i
= 0; i
< argc
; i
++) {
595 fputs("\n\n", stdout
);
596 if (print_device_freespace(sf
->cxt
, argv
[i
], 1) != 0)
601 print_all_devices_freespace(sf
->cxt
);
607 * sfdisk --list-types
609 static int command_list_types(struct sfdisk
*sf
)
611 const struct fdisk_parttype
*t
;
612 struct fdisk_label
*lb
;
620 name
= sf
->label
? sf
->label
: "dos";
621 lb
= fdisk_get_label(sf
->cxt
, name
);
623 errx(EXIT_FAILURE
, _("unsupported label '%s'"), name
);
625 codes
= fdisk_label_has_code_parttypes(lb
);
626 fputs(_("Id Name\n\n"), stdout
);
628 while ((t
= fdisk_label_get_parttype(lb
, i
++))) {
630 printf("%2x %s\n", fdisk_parttype_get_code(t
),
631 fdisk_parttype_get_name(t
));
633 printf("%s %s\n", fdisk_parttype_get_string(t
),
634 fdisk_parttype_get_name(t
));
640 static int verify_device(struct sfdisk
*sf
, const char *devname
)
644 fdisk_enable_listonly(sf
->cxt
, 1);
646 if (fdisk_assign_device(sf
->cxt
, devname
, 1)) {
647 warn(_("cannot open %s"), devname
);
651 color_scheme_enable("header", UL_COLOR_BOLD
);
652 fdisk_info(sf
->cxt
, "%s:", devname
);
655 if (!fdisk_has_label(sf
->cxt
))
656 fdisk_info(sf
->cxt
, _("unrecognized partition table type"));
658 rc
= fdisk_verify_disklabel(sf
->cxt
);
660 fdisk_deassign_device(sf
->cxt
, 1);
665 * sfdisk --verify [<device ..]
667 static int command_verify(struct sfdisk
*sf
, int argc
, char **argv
)
669 int nfails
= 0, ct
= 0;
673 for (i
= 0; i
< argc
; i
++) {
675 fdisk_info(sf
->cxt
, " ");
676 if (verify_device(sf
, argv
[i
]) < 0)
683 while ((dev
= next_proc_partition(&f
))) {
685 fdisk_info(sf
->cxt
, " ");
686 if (verify_device(sf
, dev
) < 0)
696 static int get_size(const char *dev
, int silent
, uintmax_t *sz
)
700 fd
= open(dev
, O_RDONLY
);
703 warn(_("cannot open %s"), dev
);
707 if (blkdev_get_sectors(fd
, (unsigned long long *) sz
) == -1) {
709 warn(_("Cannot get size of %s"), dev
);
718 * sfdisk --show-size [<device ..]
720 * (silly, but just for backward compatibility)
722 static int command_show_size(struct sfdisk
*sf
__attribute__((__unused__
)),
723 int argc
, char **argv
)
729 for (i
= 0; i
< argc
; i
++) {
730 if (get_size(argv
[i
], 0, &sz
) == 0)
731 printf("%ju\n", sz
/ 2);
738 while ((dev
= next_proc_partition(&f
))) {
739 if (get_size(dev
, 1, &sz
) == 0) {
740 printf("%s: %9ju\n", dev
, sz
/ 2);
746 printf(_("total: %ju blocks\n"), total
);
752 static int print_geom(struct sfdisk
*sf
, const char *devname
)
754 fdisk_enable_listonly(sf
->cxt
, 1);
756 if (fdisk_assign_device(sf
->cxt
, devname
, 1)) {
757 warn(_("cannot open %s"), devname
);
761 fdisk_info(sf
->cxt
, "%s: %ju cylinders, %ju heads, %ju sectors/track",
763 (uintmax_t) fdisk_get_geom_cylinders(sf
->cxt
),
764 (uintmax_t) fdisk_get_geom_heads(sf
->cxt
),
765 (uintmax_t) fdisk_get_geom_sectors(sf
->cxt
));
767 fdisk_deassign_device(sf
->cxt
, 1);
772 * sfdisk --show-geometry [<device ..]
774 static int command_show_geometry(struct sfdisk
*sf
, int argc
, char **argv
)
780 for (i
= 0; i
< argc
; i
++) {
781 if (print_geom(sf
, argv
[i
]) < 0)
788 while ((dev
= next_proc_partition(&f
))) {
789 if (print_geom(sf
, dev
) < 0)
799 * sfdisk --activate <device> [<partno> ...]
801 static int command_activate(struct sfdisk
*sf
, int argc
, char **argv
)
803 int rc
, nparts
, i
, listonly
;
804 struct fdisk_partition
*pa
= NULL
;
805 const char *devname
= NULL
;
808 errx(EXIT_FAILURE
, _("no disk device specified"));
811 /* --activate <device> */
812 listonly
= argc
== 1;
814 rc
= fdisk_assign_device(sf
->cxt
, devname
, listonly
);
816 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
818 if (!fdisk_is_label(sf
->cxt
, DOS
))
819 errx(EXIT_FAILURE
, _("toggle boot flags is supported for MBR only"));
821 if (!listonly
&& sf
->backup
)
822 backup_partition_table(sf
, devname
);
824 nparts
= fdisk_get_npartitions(sf
->cxt
);
825 for (i
= 0; i
< nparts
; i
++) {
828 /* note that fdisk_get_partition() reuses the @pa pointer, you
829 * don't have to (re)allocate it */
830 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0)
833 /* sfdisk --activate list bootable partitions */
835 if (!fdisk_partition_is_bootable(pa
))
837 if (fdisk_partition_to_string(pa
, sf
->cxt
,
838 FDISK_FIELD_DEVICE
, &data
) == 0) {
839 printf("%s\n", data
);
843 /* deactivate all active partitions */
844 } else if (fdisk_partition_is_bootable(pa
))
845 fdisk_toggle_partition_flag(sf
->cxt
, i
, DOS_FLAG_ACTIVE
);
848 /* sfdisk --activate <partno> [..] */
849 for (i
= 1; i
< argc
; i
++) {
850 int n
= strtou32_or_err(argv
[i
], _("failed to parse partition number"));
852 rc
= fdisk_toggle_partition_flag(sf
->cxt
, n
- 1, DOS_FLAG_ACTIVE
);
855 _("%s: partition %d: failed to toggle bootable flag"),
859 fdisk_unref_partition(pa
);
861 rc
= fdisk_deassign_device(sf
->cxt
, 1);
863 rc
= write_changes(sf
);
868 * sfdisk --delete <device> [<partno> ...]
870 static int command_delete(struct sfdisk
*sf
, int argc
, char **argv
)
873 const char *devname
= NULL
;
876 errx(EXIT_FAILURE
, _("no disk device specified"));
879 if (fdisk_assign_device(sf
->cxt
, devname
, 0) != 0)
880 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
883 backup_partition_table(sf
, devname
);
887 size_t nparts
= fdisk_get_npartitions(sf
->cxt
);
888 for (i
= 0; i
< nparts
; i
++) {
889 if (fdisk_is_partition_used(sf
->cxt
, i
) &&
890 fdisk_delete_partition(sf
->cxt
, i
) != 0)
891 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to delete"), devname
, i
+ 1);
893 /* delete specified */
895 for (i
= 1; i
< (size_t) argc
; i
++) {
896 size_t n
= strtou32_or_err(argv
[i
], _("failed to parse partition number"));
898 if (fdisk_delete_partition(sf
->cxt
, n
- 1) != 0)
899 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to delete"), devname
, n
);
903 return write_changes(sf
);
907 * sfdisk --reorder <device>
909 static int command_reorder(struct sfdisk
*sf
, int argc
, char **argv
)
911 const char *devname
= NULL
;
917 errx(EXIT_FAILURE
, _("no disk device specified"));
919 rc
= fdisk_assign_device(sf
->cxt
, devname
, 0); /* read-write */
921 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
924 backup_partition_table(sf
, devname
);
926 if (fdisk_reorder_partitions(sf
->cxt
) == 1) /* unchanged */
927 rc
= fdisk_deassign_device(sf
->cxt
, 1);
929 rc
= write_changes(sf
);
936 * sfdisk --dump <device>
938 static int command_dump(struct sfdisk
*sf
, int argc
, char **argv
)
940 const char *devname
= NULL
;
941 struct fdisk_script
*dp
;
947 errx(EXIT_FAILURE
, _("no disk device specified"));
949 rc
= fdisk_assign_device(sf
->cxt
, devname
, 1); /* read-only */
951 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
953 if (!fdisk_has_label(sf
->cxt
))
954 errx(EXIT_FAILURE
, _("%s: does not contain a recognized partition table"), devname
);
956 dp
= fdisk_new_script(sf
->cxt
);
958 err(EXIT_FAILURE
, _("failed to allocate dump struct"));
960 rc
= fdisk_script_read_context(dp
, NULL
);
962 errx(EXIT_FAILURE
, _("%s: failed to dump partition table"), devname
);
965 fdisk_script_enable_json(dp
, 1);
966 fdisk_script_write_file(dp
, stdout
);
968 fdisk_unref_script(dp
);
969 fdisk_deassign_device(sf
->cxt
, 1); /* no-sync() */
973 static void assign_device_partition(struct sfdisk
*sf
,
980 struct fdisk_label
*lb
= NULL
;
985 /* read-only when a new <type> undefined */
986 rc
= fdisk_assign_device(sf
->cxt
, devname
, rdonly
);
988 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
990 lb
= fdisk_get_label(sf
->cxt
, NULL
);
992 errx(EXIT_FAILURE
, _("%s: no partition table found"), devname
);
994 n
= fdisk_get_npartitions(sf
->cxt
);
996 errx(EXIT_FAILURE
, _("%s: partition %zu: partition table contains "
997 "only %zu partitions"), devname
, partno
, n
);
998 if (!fdisk_is_partition_used(sf
->cxt
, partno
- 1))
999 errx(EXIT_FAILURE
, _("%s: partition %zu: partition is unused"),
1004 * sfdisk --part-type <device> <partno> [<type>]
1006 static int command_parttype(struct sfdisk
*sf
, int argc
, char **argv
)
1009 struct fdisk_parttype
*type
= NULL
;
1010 struct fdisk_label
*lb
;
1011 const char *devname
= NULL
, *typestr
= NULL
;
1014 errx(EXIT_FAILURE
, _("no disk device specified"));
1018 errx(EXIT_FAILURE
, _("no partition number specified"));
1019 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1024 errx(EXIT_FAILURE
, _("unexpected arguments"));
1026 /* read-only when a new <type> undefined */
1027 assign_device_partition(sf
, devname
, partno
, !typestr
);
1029 lb
= fdisk_get_label(sf
->cxt
, NULL
);
1031 /* print partition type */
1033 const struct fdisk_parttype
*t
= NULL
;
1034 struct fdisk_partition
*pa
= NULL
;
1036 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1037 t
= fdisk_partition_get_type(pa
);
1039 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition type"),
1042 if (fdisk_label_has_code_parttypes(lb
))
1043 printf("%2x\n", fdisk_parttype_get_code(t
));
1045 printf("%s\n", fdisk_parttype_get_string(t
));
1047 fdisk_unref_partition(pa
);
1048 fdisk_deassign_device(sf
->cxt
, 1);
1053 backup_partition_table(sf
, devname
);
1055 /* parse <type> and apply to PT */
1056 type
= fdisk_label_parse_parttype(lb
, typestr
);
1058 errx(EXIT_FAILURE
, _("failed to parse %s partition type '%s'"),
1059 fdisk_label_get_name(lb
), typestr
);
1061 else if (fdisk_set_partition_type(sf
->cxt
, partno
- 1, type
) != 0)
1062 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition type"),
1064 fdisk_unref_parttype(type
);
1065 return write_changes(sf
);
1069 * sfdisk --part-uuid <device> <partno> [<uuid>]
1071 static int command_partuuid(struct sfdisk
*sf
, int argc
, char **argv
)
1074 struct fdisk_partition
*pa
= NULL
;
1075 const char *devname
= NULL
, *uuid
= NULL
;
1078 errx(EXIT_FAILURE
, _("no disk device specified"));
1082 errx(EXIT_FAILURE
, _("no partition number specified"));
1083 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1088 errx(EXIT_FAILURE
, _("unexpected arguments"));
1090 /* read-only if uuid not given */
1091 assign_device_partition(sf
, devname
, partno
, !uuid
);
1093 /* print partition uuid */
1095 const char *str
= NULL
;
1097 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1098 str
= fdisk_partition_get_uuid(pa
);
1100 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition UUID"),
1102 printf("%s\n", str
);
1103 fdisk_unref_partition(pa
);
1104 fdisk_deassign_device(sf
->cxt
, 1);
1109 backup_partition_table(sf
, devname
);
1111 pa
= fdisk_new_partition();
1113 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1115 if (fdisk_partition_set_uuid(pa
, uuid
) != 0 ||
1116 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1117 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition UUID"),
1119 fdisk_unref_partition(pa
);
1120 return write_changes(sf
);
1124 * sfdisk --part-label <device> <partno> [<label>]
1126 static int command_partlabel(struct sfdisk
*sf
, int argc
, char **argv
)
1129 struct fdisk_partition
*pa
= NULL
;
1130 const char *devname
= NULL
, *name
= NULL
;
1133 errx(EXIT_FAILURE
, _("no disk device specified"));
1137 errx(EXIT_FAILURE
, _("no partition number specified"));
1138 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1143 errx(EXIT_FAILURE
, _("unexpected arguments"));
1145 /* read-only if name not given */
1146 assign_device_partition(sf
, devname
, partno
, !name
);
1148 /* print partition name */
1150 const char *str
= NULL
;
1152 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1153 str
= fdisk_partition_get_name(pa
);
1155 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition name"),
1157 printf("%s\n", str
);
1158 fdisk_unref_partition(pa
);
1159 fdisk_deassign_device(sf
->cxt
, 1);
1164 backup_partition_table(sf
, devname
);
1166 pa
= fdisk_new_partition();
1168 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1170 if (fdisk_partition_set_name(pa
, name
) != 0 ||
1171 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1172 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition name"),
1175 fdisk_unref_partition(pa
);
1176 return write_changes(sf
);
1180 * sfdisk --part-attrs <device> <partno> [<attrs>]
1182 static int command_partattrs(struct sfdisk
*sf
, int argc
, char **argv
)
1185 struct fdisk_partition
*pa
= NULL
;
1186 const char *devname
= NULL
, *attrs
= NULL
;
1189 errx(EXIT_FAILURE
, _("no disk device specified"));
1193 errx(EXIT_FAILURE
, _("no partition number specified"));
1194 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1199 errx(EXIT_FAILURE
, _("unexpected arguments"));
1201 /* read-only if name not given */
1202 assign_device_partition(sf
, devname
, partno
, !attrs
);
1204 /* print partition name */
1206 const char *str
= NULL
;
1208 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1209 str
= fdisk_partition_get_attrs(pa
);
1211 printf("%s\n", str
);
1212 fdisk_unref_partition(pa
);
1213 fdisk_deassign_device(sf
->cxt
, 1);
1218 backup_partition_table(sf
, devname
);
1220 pa
= fdisk_new_partition();
1222 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1224 if (fdisk_partition_set_attrs(pa
, attrs
) != 0 ||
1225 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1226 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition attributes"),
1229 fdisk_unref_partition(pa
);
1230 return write_changes(sf
);
1233 static void sfdisk_print_partition(struct sfdisk
*sf
, size_t n
)
1235 struct fdisk_partition
*pa
= NULL
;
1242 if (fdisk_get_partition(sf
->cxt
, n
, &pa
) != 0)
1245 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_DEVICE
, &data
);
1246 printf("%12s : ", data
);
1248 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_START
, &data
);
1249 printf("%12s ", data
);
1251 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_END
, &data
);
1252 printf("%12s ", data
);
1254 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_SIZE
, &data
);
1255 printf("(%s) ", data
);
1257 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_TYPE
, &data
);
1258 printf("%s\n", data
);
1260 fdisk_unref_partition(pa
);
1263 static void command_fdisk_help(void)
1265 fputs(_("\nHelp:\n"), stdout
);
1267 fputc('\n', stdout
);
1268 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1269 fputs(_(" Commands:\n"), stdout
);
1271 fputs(_(" write write table to disk and exit\n"), stdout
);
1272 fputs(_(" quit show new situation and wait for user's feedback before write\n"), stdout
);
1273 fputs(_(" abort exit sfdisk shell\n"), stdout
);
1274 fputs(_(" print display the partition table\n"), stdout
);
1275 fputs(_(" help show this help text\n"), stdout
);
1276 fputc('\n', stdout
);
1277 fputs(_(" Ctrl-D the same as 'quit'\n"), stdout
);
1279 fputc('\n', stdout
);
1280 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1281 fputs(_(" Input format:\n"), stdout
);
1283 fputs(_(" <start>, <size>, <type>, <bootable>\n"), stdout
);
1285 fputc('\n', stdout
);
1286 fputs(_(" <start> Beginning of the partition in sectors, or bytes if\n"
1287 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1288 " The default is the first free space.\n"), stdout
);
1290 fputc('\n', stdout
);
1291 fputs(_(" <size> Size of the partition in sectors, or bytes if\n"
1292 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1293 " The default is all available space.\n"), stdout
);
1295 fputc('\n', stdout
);
1296 fputs(_(" <type> The partition type. Default is a Linux data partition.\n"), stdout
);
1297 fputs(_(" MBR: hex or L,S,E,X shortcuts.\n"), stdout
);
1298 fputs(_(" GPT: UUID or L,S,H shortcuts.\n"), stdout
);
1300 fputc('\n', stdout
);
1301 fputs(_(" <bootable> Use '*' to mark an MBR partition as bootable.\n"), stdout
);
1303 fputc('\n', stdout
);
1304 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1305 fputs(_(" Example:\n"), stdout
);
1307 fputs(_(" , 4G Creates a 4GiB partition at default start offset.\n"), stdout
);
1308 fputc('\n', stdout
);
1312 SFDISK_DONE_NONE
= 0,
1319 /* returns: 0 on success, <0 on error, 1 successfully stop sfdisk */
1320 static int loop_control_commands(struct sfdisk
*sf
,
1321 struct fdisk_script
*dp
,
1324 const char *p
= skip_blank(buf
);
1325 int rc
= SFDISK_DONE_NONE
;
1327 if (strcmp(p
, "print") == 0)
1328 list_disklabel(sf
->cxt
);
1329 else if (strcmp(p
, "help") == 0)
1330 command_fdisk_help();
1331 else if (strcmp(p
, "quit") == 0)
1332 rc
= SFDISK_DONE_ASK
;
1333 else if (strcmp(p
, "write") == 0)
1334 rc
= SFDISK_DONE_WRITE
;
1335 else if (strcmp(p
, "abort") == 0)
1336 rc
= SFDISK_DONE_ABORT
;
1338 if (sf
->interactive
)
1339 fdisk_warnx(sf
->cxt
, _("unsupported command"));
1341 fdisk_warnx(sf
->cxt
, _("line %d: unsupported command"),
1342 fdisk_script_get_nlines(dp
));
1349 static int has_container(struct sfdisk
*sf
)
1352 struct fdisk_partition
*pa
= NULL
;
1355 return sf
->container
;
1357 nparts
= fdisk_get_npartitions(sf
->cxt
);
1358 for (i
= 0; i
< nparts
; i
++) {
1359 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0)
1361 if (fdisk_partition_is_container(pa
)) {
1367 fdisk_unref_partition(pa
);
1368 return sf
->container
;
1371 static size_t last_pt_partno(struct sfdisk
*sf
)
1373 size_t i
, nparts
, partno
= 0;
1374 struct fdisk_partition
*pa
= NULL
;
1377 nparts
= fdisk_get_npartitions(sf
->cxt
);
1378 for (i
= 0; i
< nparts
; i
++) {
1381 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0 ||
1382 !fdisk_partition_is_used(pa
))
1384 x
= fdisk_partition_get_partno(pa
);
1389 fdisk_unref_partition(pa
);
1394 static int is_device_used(struct sfdisk
*sf
)
1402 fd
= fdisk_get_devfd(sf
->cxt
);
1406 if (fstat(fd
, &st
) == 0 && S_ISBLK(st
.st_mode
)
1407 && major(st
.st_rdev
) != LOOPDEV_MAJOR
)
1408 return ioctl(fd
, BLKRRPART
) != 0;
1412 static int is_device_used(struct sfdisk
*sf
__attribute__((__unused__
)))
1418 #ifdef HAVE_LIBREADLINE
1419 static char *sfdisk_fgets(struct fdisk_script
*dp
,
1420 char *buf
, size_t bufsz
, FILE *f
)
1422 struct sfdisk
*sf
= (struct sfdisk
*) fdisk_script_get_userdata(dp
);
1428 if (sf
->interactive
) {
1429 char *p
= readline(sf
->prompt
);
1435 if (len
> bufsz
- 2)
1438 memcpy(buf
, p
, len
);
1439 buf
[len
] = '\n'; /* append \n to be compatible with libc fgetc() */
1440 buf
[len
+ 1] = '\0';
1445 return fgets(buf
, bufsz
, f
);
1449 static int ignore_partition(struct fdisk_partition
*pa
)
1451 /* incomplete partition setting */
1452 if (!fdisk_partition_has_start(pa
) && !fdisk_partition_start_is_default(pa
))
1454 if (!fdisk_partition_has_size(pa
) && !fdisk_partition_end_is_default(pa
))
1457 /* probably dump from old sfdisk with start=0 size=0 */
1458 if (fdisk_partition_has_start(pa
) && fdisk_partition_get_start(pa
) == 0 &&
1459 fdisk_partition_has_size(pa
) && fdisk_partition_get_size(pa
) == 0)
1465 static int wipe_partition(struct sfdisk
*sf
, size_t partno
)
1468 char *fstype
= NULL
;;
1469 struct fdisk_partition
*tmp
= NULL
;
1471 DBG(MISC
, ul_debug("checking for signature"));
1473 rc
= fdisk_get_partition(sf
->cxt
, partno
, &tmp
);
1477 rc
= fdisk_partition_to_string(tmp
, sf
->cxt
, FDISK_FIELD_FSTYPE
, &fstype
);
1478 if (rc
|| fstype
== NULL
)
1481 fdisk_warnx(sf
->cxt
, _("Partition #%zu contains a %s signature."), partno
+ 1, fstype
);
1483 if (sf
->pwipemode
== WIPEMODE_AUTO
&& isatty(STDIN_FILENO
))
1484 fdisk_ask_yesno(sf
->cxt
, _("Do you want to remove the signature?"), &yes
);
1485 else if (sf
->pwipemode
== WIPEMODE_ALWAYS
)
1489 fdisk_info(sf
->cxt
, _("The signature will be removed by a write command."));
1490 rc
= fdisk_wipe_partition(sf
->cxt
, partno
, TRUE
);
1493 fdisk_unref_partition(tmp
);
1495 DBG(MISC
, ul_debug("partition wipe check end [rc=%d]", rc
));
1499 static void refresh_prompt_buffer(struct sfdisk
*sf
, const char *devname
,
1500 size_t next_partno
, int created
)
1503 char *partname
= fdisk_partname(devname
, next_partno
+ 1);
1505 err(EXIT_FAILURE
, _("failed to allocate partition name"));
1507 if (!sf
->prompt
|| !startswith(sf
->prompt
, partname
)) {
1509 xasprintf(&sf
->prompt
,"%s: ", partname
);
1512 } else if (!sf
->prompt
|| !startswith(sf
->prompt
, SFDISK_PROMPT
)) {
1514 sf
->prompt
= xstrdup(SFDISK_PROMPT
);
1519 * sfdisk <device> [[-N] <partno>]
1521 * Note that the option -N is there for backward compatibility only.
1523 static int command_fdisk(struct sfdisk
*sf
, int argc
, char **argv
)
1525 int rc
= 0, partno
= sf
->partno
, created
= 0, unused
= 0;
1526 struct fdisk_script
*dp
;
1527 struct fdisk_table
*tb
= NULL
;
1528 const char *devname
= NULL
, *label
;
1530 size_t next_partno
= (size_t) -1;
1534 if (partno
< 0 && argc
> 1)
1535 partno
= strtou32_or_err(argv
[1],
1536 _("failed to parse partition number"));
1538 errx(EXIT_FAILURE
, _("no disk device specified"));
1540 rc
= fdisk_assign_device(sf
->cxt
, devname
, 0);
1542 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
1544 dp
= fdisk_new_script(sf
->cxt
);
1546 err(EXIT_FAILURE
, _("failed to allocate script handler"));
1547 fdisk_set_script(sf
->cxt
, dp
);
1548 #ifdef HAVE_LIBREADLINE
1549 fdisk_script_set_fgets(dp
, sfdisk_fgets
);
1551 fdisk_script_set_userdata(dp
, (void *) sf
);
1554 * Don't create a new disklabel when [-N] <partno> specified. In this
1555 * case reuse already specified disklabel. Let's check that the disk
1556 * really contains the partition.
1561 if (!fdisk_has_label(sf
->cxt
))
1562 errx(EXIT_FAILURE
, _("%s: cannot modify partition %d: "
1563 "no partition table was found"),
1564 devname
, partno
+ 1);
1565 n
= fdisk_get_npartitions(sf
->cxt
);
1566 if ((size_t) partno
> n
)
1567 errx(EXIT_FAILURE
, _("%s: cannot modify partition %d: "
1568 "partition table contains only %zu "
1570 devname
, partno
+ 1, n
);
1572 if (!fdisk_is_partition_used(sf
->cxt
, partno
)) {
1573 fdisk_warnx(sf
->cxt
, _("warning: %s: partition %d is not defined yet"),
1574 devname
, partno
+ 1);
1578 next_partno
= partno
;
1581 sf
->orig_pa
= get_partition(sf
->cxt
, partno
);
1586 next_partno
= last_pt_partno(sf
) + 1;
1589 if (!sf
->quiet
&& sf
->interactive
) {
1590 color_scheme_enable("welcome", UL_COLOR_GREEN
);
1591 fdisk_info(sf
->cxt
, _("\nWelcome to sfdisk (%s)."), PACKAGE_STRING
);
1593 fdisk_info(sf
->cxt
, _("Changes will remain in memory only, until you decide to write them.\n"
1594 "Be careful before using the write command.\n"));
1597 if (!sf
->noact
&& !sf
->noreread
) {
1599 fputs(_("Checking that no-one is using this disk right now ..."), stdout
);
1600 if (is_device_used(sf
)) {
1602 fputs(_(" FAILED\n\n"), stdout
);
1604 fdisk_warnx(sf
->cxt
, _(
1605 "This disk is currently in use - repartitioning is probably a bad idea.\n"
1606 "Umount all file systems, and swapoff all swap partitions on this disk.\n"
1607 "Use the --no-reread flag to suppress this check.\n"));
1610 errx(EXIT_FAILURE
, _("Use the --force flag to overrule all checks."));
1611 } else if (!sf
->quiet
)
1612 fputs(_(" OK\n\n"), stdout
);
1615 if (fdisk_get_collision(sf
->cxt
)) {
1616 int dowipe
= sf
->wipemode
== WIPEMODE_ALWAYS
? 1 : 0;
1618 fdisk_warnx(sf
->cxt
, _("Device %s already contains a %s signature."),
1619 devname
, fdisk_get_collision(sf
->cxt
));
1621 if (sf
->interactive
&& sf
->wipemode
== WIPEMODE_AUTO
)
1622 dowipe
= 1; /* do it in interactive mode */
1624 fdisk_enable_wipe(sf
->cxt
, dowipe
);
1626 fdisk_warnx(sf
->cxt
, _(
1627 "The signature will be removed by a write command."));
1629 fdisk_warnx(sf
->cxt
, _(
1630 "It is strongly recommended to wipe the device with "
1631 "wipefs(8), in order to avoid possible collisions."));
1632 fputc('\n', stderr
);
1636 backup_partition_table(sf
, devname
);
1639 list_disk_geometry(sf
->cxt
);
1640 if (fdisk_has_label(sf
->cxt
)) {
1641 fdisk_info(sf
->cxt
, _("\nOld situation:"));
1642 list_disklabel(sf
->cxt
);
1648 else if (fdisk_has_label(sf
->cxt
))
1649 label
= fdisk_label_get_name(fdisk_get_label(sf
->cxt
, NULL
));
1651 label
= "dos"; /* just for backward compatibility */
1653 fdisk_script_set_header(dp
, "label", label
);
1656 if (!sf
->quiet
&& sf
->interactive
) {
1657 if (!fdisk_has_label(sf
->cxt
) && !sf
->label
)
1659 _("\nsfdisk is going to create a new '%s' disk label.\n"
1660 "Use 'label: <name>' before you define a first partition\n"
1661 "to override the default."), label
);
1662 fdisk_info(sf
->cxt
, _("\nType 'help' to get more information.\n"));
1663 } else if (!sf
->quiet
)
1664 fputc('\n', stdout
);
1666 tb
= fdisk_script_get_table(dp
);
1672 DBG(PARSE
, ul_debug("<---next-line--->"));
1673 if (next_partno
== (size_t) -1)
1674 next_partno
= fdisk_table_get_nents(tb
);
1678 && next_partno
== fdisk_get_npartitions(sf
->cxt
)
1679 && !has_container(sf
)) {
1680 fdisk_info(sf
->cxt
, _("All partitions used."));
1681 rc
= SFDISK_DONE_ASK
;
1685 refresh_prompt_buffer(sf
, devname
, next_partno
, created
);
1686 if (sf
->prompt
&& (sf
->interactive
|| !sf
->quiet
)) {
1687 #ifndef HAVE_LIBREADLINE
1688 fputs(sf
->prompt
, stdout
);
1690 if (!sf
->interactive
)
1691 fputs(sf
->prompt
, stdout
);
1695 rc
= fdisk_script_read_line(dp
, stdin
, buf
, sizeof(buf
));
1697 DBG(PARSE
, ul_debug("script parsing failed, trying sfdisk specific commands"));
1698 buf
[sizeof(buf
) - 1] = '\0';
1699 rc
= loop_control_commands(sf
, dp
, buf
);
1703 } else if (rc
== 1) {
1704 rc
= SFDISK_DONE_EOF
;
1705 fputs(_("Done.\n"), stdout
);
1709 nparts
= fdisk_table_get_nents(tb
);
1712 struct fdisk_partition
*pa
= fdisk_table_get_partition(tb
, nparts
- 1);
1716 if (ignore_partition(pa
)) {
1717 fdisk_info(sf
->cxt
, _("Ignoring partition."));
1721 if (!created
) { /* create a new disklabel */
1722 rc
= fdisk_apply_script_headers(sf
->cxt
, dp
);
1725 fdisk_warnx(sf
->cxt
, _(
1726 "Failed to apply script headers, "
1727 "disk label not created."));
1729 if (!rc
&& partno
>= 0) { /* -N <partno>, modify partition */
1730 rc
= fdisk_set_partition(sf
->cxt
, partno
, pa
);
1731 rc
= rc
== 0 ? SFDISK_DONE_ASK
: SFDISK_DONE_ABORT
;
1733 } else if (!rc
) { /* add partition */
1734 if (!sf
->interactive
&&
1735 (!sf
->prompt
|| startswith(sf
->prompt
, SFDISK_PROMPT
))) {
1736 refresh_prompt_buffer(sf
, devname
, next_partno
, created
);
1737 fputs(sf
->prompt
, stdout
);
1739 rc
= fdisk_add_partition(sf
->cxt
, pa
, &cur_partno
);
1742 fdisk_warn(sf
->cxt
, _("Failed to add partition"));
1747 /* wipe partition on success
1749 * Note that unused=1 means -N <partno> for unused,
1750 * otherwise we wipe only newly created partitions.
1752 if (rc
== 0 && (unused
|| partno
< 0)) {
1753 rc
= wipe_partition(sf
, unused
? (size_t) partno
: cur_partno
);
1759 /* success print result */
1760 if (sf
->interactive
)
1761 sfdisk_print_partition(sf
, cur_partno
);
1762 next_partno
= cur_partno
+ 1;
1763 } else if (pa
) /* error, drop partition from script */
1764 fdisk_table_remove_partition(tb
, pa
);
1766 fdisk_info(sf
->cxt
, _("Script header accepted."));
1768 if (rc
&& !sf
->interactive
) {
1769 rc
= SFDISK_DONE_ABORT
;
1774 /* create empty disk label if label, but no partition specified */
1775 if (rc
== SFDISK_DONE_EOF
&& created
== 0
1776 && fdisk_script_has_force_label(dp
) == 1
1777 && fdisk_table_get_nents(tb
) == 0
1778 && fdisk_script_get_header(dp
, "label")) {
1780 int xrc
= fdisk_apply_script_headers(sf
->cxt
, dp
);
1783 fdisk_warnx(sf
->cxt
, _(
1784 "Failed to apply script headers, "
1785 "disk label not created."));
1786 rc
= SFDISK_DONE_ABORT
;
1790 if (!sf
->quiet
&& rc
!= SFDISK_DONE_ABORT
) {
1791 fdisk_info(sf
->cxt
, _("\nNew situation:"));
1792 list_disk_identifier(sf
->cxt
);
1793 list_disklabel(sf
->cxt
);
1797 case SFDISK_DONE_ASK
:
1798 case SFDISK_DONE_EOF
:
1799 if (sf
->interactive
) {
1801 fdisk_ask_yesno(sf
->cxt
, _("Do you want to write this to disk?"), &yes
);
1803 fdisk_info(sf
->cxt
, _("Leaving."));
1808 case SFDISK_DONE_WRITE
:
1809 rc
= write_changes(sf
);
1811 case SFDISK_DONE_ABORT
:
1812 default: /* rc < 0 on error */
1813 fdisk_info(sf
->cxt
, _("Leaving.\n"));
1817 fdisk_unref_script(dp
);
1821 static void __attribute__ ((__noreturn__
)) usage(FILE *out
)
1823 fputs(USAGE_HEADER
, out
);
1826 _(" %1$s [options] <dev> [[-N] <part>]\n"
1827 " %1$s [options] <command>\n"), program_invocation_short_name
);
1829 fputs(USAGE_SEPARATOR
, out
);
1830 fputs(_("Display or manipulate a disk partition table.\n"), out
);
1832 fputs(_("\nCommands:\n"), out
);
1833 fputs(_(" -A, --activate <dev> [<part> ...] list or set bootable MBR partitions\n"), out
);
1834 fputs(_(" -d, --dump <dev> dump partition table (usable for later input)\n"), out
);
1835 fputs(_(" -J, --json <dev> dump partition table in JSON format\n"), out
);
1836 fputs(_(" -g, --show-geometry [<dev> ...] list geometry of all or specified devices\n"), out
);
1837 fputs(_(" -l, --list [<dev> ...] list partitions of each device\n"), out
);
1838 fputs(_(" -F, --list-free [<dev> ...] list unpartitioned free areas of each device\n"), out
);
1839 fputs(_(" -r, --reorder <dev> fix partitions order (by start offset)\n"), out
);
1840 fputs(_(" -s, --show-size [<dev> ...] list sizes of all or specified devices\n"), out
);
1841 fputs(_(" -T, --list-types print the recognized types (see -X)\n"), out
);
1842 fputs(_(" -V, --verify [<dev> ...] test whether partitions seem correct\n"), out
);
1843 fputs(_(" --delete <dev> [<part> ...] delete all or specified partitions\n"), out
);
1845 fputs(USAGE_SEPARATOR
, out
);
1846 fputs(_(" --part-label <dev> <part> [<str>] print or change partition label\n"), out
);
1847 fputs(_(" --part-type <dev> <part> [<type>] print or change partition type\n"), out
);
1848 fputs(_(" --part-uuid <dev> <part> [<uuid>] print or change partition uuid\n"), out
);
1849 fputs(_(" --part-attrs <dev> <part> [<str>] print or change partition attributes\n"), out
);
1851 fputs(USAGE_SEPARATOR
, out
);
1852 fputs(_(" <dev> device (usually disk) path\n"), out
);
1853 fputs(_(" <part> partition number\n"), out
);
1854 fputs(_(" <type> partition type, GUID for GPT, hex for MBR\n"), out
);
1856 fputs(USAGE_OPTIONS
, out
);
1857 fputs(_(" -a, --append append partitions to existing partition table\n"), out
);
1858 fputs(_(" -b, --backup backup partition table sectors (see -O)\n"), out
);
1859 fputs(_(" --bytes print SIZE in bytes rather than in human readable format\n"), out
);
1860 fputs(_(" --move-data[=<typescript>] move partition data after relocation (requires -N)\n"), out
);
1861 fputs(_(" -f, --force disable all consistency checking\n"), out
);
1862 fputs(_(" --color[=<when>] colorize output (auto, always or never)\n"), out
);
1864 " %s\n", USAGE_COLORS_DEFAULT
);
1865 fputs(_(" -N, --partno <num> specify partition number\n"), out
);
1866 fputs(_(" -n, --no-act do everything except write to device\n"), out
);
1867 fputs(_(" --no-reread do not check whether the device is in use\n"), out
);
1868 fputs(_(" --no-tell-kernel do not tell kernel about changes\n"), out
);
1869 fputs(_(" -O, --backup-file <path> override default backup file name\n"), out
);
1870 fputs(_(" -o, --output <list> output columns\n"), out
);
1871 fputs(_(" -q, --quiet suppress extra info messages\n"), out
);
1872 fputs(_(" -w, --wipe <mode> wipe signatures (auto, always or never)\n"), out
);
1873 fputs(_(" -W, --wipe-partitions <mode> wipe signatures from new partitions (auto, always or never)\n"), out
);
1874 fputs(_(" -X, --label <name> specify label type (dos, gpt, ...)\n"), out
);
1875 fputs(_(" -Y, --label-nested <name> specify nested label type (dos, bsd)\n"), out
);
1876 fputs(USAGE_SEPARATOR
, out
);
1877 fputs(_(" -G, --show-pt-geometry deprecated, alias to --show-geometry\n"), out
);
1878 fputs(_(" -L, --Linux deprecated, only for backward compatibility\n"), out
);
1879 fputs(_(" -u, --unit S deprecated, only sector unit is supported\n"), out
);
1881 fputs(USAGE_SEPARATOR
, out
);
1882 fputs(USAGE_HELP
, out
);
1883 fputs(_(" -v, --version output version information and exit\n"), out
);
1885 list_available_columns(out
);
1887 fprintf(out
, USAGE_MAN_TAIL("sfdisk(8)"));
1888 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
1892 int main(int argc
, char *argv
[])
1894 const char *outarg
= NULL
;
1895 int rc
= -EINVAL
, c
, longidx
= -1, bytes
= 0;
1896 int colormode
= UL_COLORMODE_UNDEF
;
1897 struct sfdisk _sf
= {
1899 .wipemode
= WIPEMODE_AUTO
,
1900 .pwipemode
= WIPEMODE_AUTO
,
1901 .interactive
= isatty(STDIN_FILENO
) ? 1 : 0,
1905 OPT_CHANGE_ID
= CHAR_MAX
+ 1,
1920 static const struct option longopts
[] = {
1921 { "activate",no_argument
, NULL
, 'A' },
1922 { "append", no_argument
, NULL
, 'a' },
1923 { "backup", no_argument
, NULL
, 'b' },
1924 { "backup-file", required_argument
, NULL
, 'O' },
1925 { "bytes", no_argument
, NULL
, OPT_BYTES
},
1926 { "color", optional_argument
, NULL
, OPT_COLOR
},
1927 { "delete", no_argument
, NULL
, OPT_DELETE
},
1928 { "dump", no_argument
, NULL
, 'd' },
1929 { "help", no_argument
, NULL
, 'h' },
1930 { "force", no_argument
, NULL
, 'f' },
1931 { "json", no_argument
, NULL
, 'J' },
1932 { "label", required_argument
, NULL
, 'X' },
1933 { "label-nested", required_argument
, NULL
, 'Y' },
1934 { "list", no_argument
, NULL
, 'l' },
1935 { "list-free", no_argument
, NULL
, 'F' },
1936 { "list-types", no_argument
, NULL
, 'T' },
1937 { "no-act", no_argument
, NULL
, 'n' },
1938 { "no-reread", no_argument
, NULL
, OPT_NOREREAD
},
1939 { "no-tell-kernel", no_argument
, NULL
, OPT_NOTELL
},
1940 { "move-data", optional_argument
, NULL
, OPT_MOVEDATA
},
1941 { "output", required_argument
, NULL
, 'o' },
1942 { "partno", required_argument
, NULL
, 'N' },
1943 { "reorder", no_argument
, NULL
, 'r' },
1944 { "show-size", no_argument
, NULL
, 's' },
1945 { "show-geometry", no_argument
, NULL
, 'g' },
1946 { "quiet", no_argument
, NULL
, 'q' },
1947 { "verify", no_argument
, NULL
, 'V' },
1948 { "version", no_argument
, NULL
, 'v' },
1949 { "wipe", required_argument
, NULL
, 'w' },
1950 { "wipe-partitions", required_argument
, NULL
, 'W' },
1952 { "part-uuid", no_argument
, NULL
, OPT_PARTUUID
},
1953 { "part-label", no_argument
, NULL
, OPT_PARTLABEL
},
1954 { "part-type", no_argument
, NULL
, OPT_PARTTYPE
},
1955 { "part-attrs", no_argument
, NULL
, OPT_PARTATTRS
},
1957 { "show-pt-geometry", no_argument
, NULL
, 'G' }, /* deprecated */
1958 { "unit", required_argument
, NULL
, 'u' },
1959 { "Linux", no_argument
, NULL
, 'L' }, /* deprecated */
1961 { "change-id",no_argument
, NULL
, OPT_CHANGE_ID
}, /* deprecated */
1962 { "id", no_argument
, NULL
, 'c' }, /* deprecated */
1963 { "print-id",no_argument
, NULL
, OPT_PRINT_ID
}, /* deprecated */
1968 setlocale(LC_ALL
, "");
1969 bindtextdomain(PACKAGE
, LOCALEDIR
);
1970 textdomain(PACKAGE
);
1971 atexit(close_stdout
);
1973 while ((c
= getopt_long(argc
, argv
, "aAbcdfFgGhJlLo:O:nN:qrsTu:vVX:Y:w:W:",
1974 longopts
, &longidx
)) != -1) {
1977 sf
->act
= ACT_ACTIVATE
;
1988 warnx(_("%s is deprecated in favour of --part-type"),
1989 longopts
[longidx
].name
);
1990 sf
->act
= ACT_PARTTYPE
;
1993 warnx(_("--id is deprecated in favour of --part-type"));
1994 sf
->act
= ACT_PARTTYPE
;
2003 sf
->act
= ACT_LIST_FREE
;
2009 warnx(_("--show-pt-geometry is no more implemented. Using --show-geometry."));
2011 sf
->act
= ACT_SHOW_GEOM
;
2020 warnx(_("--Linux option is unnecessary and deprecated"));
2027 sf
->backup_file
= optarg
;
2033 sf
->partno
= strtou32_or_err(optarg
, _("failed to parse partition number")) - 1;
2039 sf
->act
= ACT_REORDER
;
2042 sf
->act
= ACT_SHOW_SIZE
;
2045 sf
->act
= ACT_LIST_TYPES
;
2049 errx(EXIT_FAILURE
, _("unsupported unit '%c'"), *optarg
);
2052 printf(_("%s from %s\n"), program_invocation_short_name
,
2054 return EXIT_SUCCESS
;
2059 sf
->wipemode
= wipemode_from_string(optarg
);
2060 if (sf
->wipemode
< 0)
2061 errx(EXIT_FAILURE
, _("unsupported wipe mode"));
2064 sf
->pwipemode
= wipemode_from_string(optarg
);
2065 if (sf
->pwipemode
< 0)
2066 errx(EXIT_FAILURE
, _("unsupported wipe mode"));
2072 sf
->label_nested
= optarg
;
2076 sf
->act
= ACT_PARTUUID
;
2079 sf
->act
= ACT_PARTTYPE
;
2082 sf
->act
= ACT_PARTLABEL
;
2085 sf
->act
= ACT_PARTATTRS
;
2094 colormode
= UL_COLORMODE_AUTO
;
2096 colormode
= colormode_or_err(optarg
,
2097 _("unsupported color mode"));
2101 sf
->move_typescript
= optarg
;
2104 sf
->act
= ACT_DELETE
;
2110 errtryhelp(EXIT_FAILURE
);
2114 colors_init(colormode
, "sfdisk");
2118 fdisk_set_size_unit(sf
->cxt
, FDISK_SIZEUNIT_BYTES
);
2121 init_fields(NULL
, outarg
, NULL
);
2123 if (sf
->verify
&& !sf
->act
)
2124 sf
->act
= ACT_VERIFY
; /* --verify make be used with --list too */
2126 sf
->act
= ACT_FDISK
; /* default */
2128 if (sf
->movedata
&& !(sf
->act
== ACT_FDISK
&& sf
->partno
>= 0))
2129 errx(EXIT_FAILURE
, _("--movedata requires -N"));
2133 rc
= command_activate(sf
, argc
- optind
, argv
+ optind
);
2137 rc
= command_delete(sf
, argc
- optind
, argv
+ optind
);
2141 rc
= command_list_partitions(sf
, argc
- optind
, argv
+ optind
);
2144 case ACT_LIST_TYPES
:
2145 rc
= command_list_types(sf
);
2149 rc
= command_list_freespace(sf
, argc
- optind
, argv
+ optind
);
2153 rc
= command_fdisk(sf
, argc
- optind
, argv
+ optind
);
2157 rc
= command_dump(sf
, argc
- optind
, argv
+ optind
);
2161 rc
= command_show_size(sf
, argc
- optind
, argv
+ optind
);
2165 rc
= command_show_geometry(sf
, argc
- optind
, argv
+ optind
);
2169 rc
= command_verify(sf
, argc
- optind
, argv
+ optind
);
2173 rc
= command_parttype(sf
, argc
- optind
, argv
+ optind
);
2177 rc
= command_partuuid(sf
, argc
- optind
, argv
+ optind
);
2181 rc
= command_partlabel(sf
, argc
- optind
, argv
+ optind
);
2185 rc
= command_partattrs(sf
, argc
- optind
, argv
+ optind
);
2189 rc
= command_reorder(sf
, argc
- optind
, argv
+ optind
);
2195 DBG(MISC
, ul_debug("bye! [rc=%d]", rc
));
2196 return rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;