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 dp
= fdisk_new_script(sf
->cxt
);
955 err(EXIT_FAILURE
, _("failed to allocate dump struct"));
957 rc
= fdisk_script_read_context(dp
, NULL
);
959 err(EXIT_FAILURE
, _("failed to dump partition table"));
962 fdisk_script_enable_json(dp
, 1);
963 fdisk_script_write_file(dp
, stdout
);
965 fdisk_unref_script(dp
);
966 fdisk_deassign_device(sf
->cxt
, 1); /* no-sync() */
970 static void assign_device_partition(struct sfdisk
*sf
,
977 struct fdisk_label
*lb
= NULL
;
982 /* read-only when a new <type> undefined */
983 rc
= fdisk_assign_device(sf
->cxt
, devname
, rdonly
);
985 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
987 lb
= fdisk_get_label(sf
->cxt
, NULL
);
989 errx(EXIT_FAILURE
, _("%s: no partition table found"), devname
);
991 n
= fdisk_get_npartitions(sf
->cxt
);
993 errx(EXIT_FAILURE
, _("%s: partition %zu: partition table contains "
994 "only %zu partitions"), devname
, partno
, n
);
995 if (!fdisk_is_partition_used(sf
->cxt
, partno
- 1))
996 errx(EXIT_FAILURE
, _("%s: partition %zu: partition is unused"),
1001 * sfdisk --part-type <device> <partno> [<type>]
1003 static int command_parttype(struct sfdisk
*sf
, int argc
, char **argv
)
1006 struct fdisk_parttype
*type
= NULL
;
1007 struct fdisk_label
*lb
;
1008 const char *devname
= NULL
, *typestr
= NULL
;
1011 errx(EXIT_FAILURE
, _("no disk device specified"));
1015 errx(EXIT_FAILURE
, _("no partition number specified"));
1016 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1021 errx(EXIT_FAILURE
, _("unexpected arguments"));
1023 /* read-only when a new <type> undefined */
1024 assign_device_partition(sf
, devname
, partno
, !typestr
);
1026 lb
= fdisk_get_label(sf
->cxt
, NULL
);
1028 /* print partition type */
1030 const struct fdisk_parttype
*t
= NULL
;
1031 struct fdisk_partition
*pa
= NULL
;
1033 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1034 t
= fdisk_partition_get_type(pa
);
1036 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition type"),
1039 if (fdisk_label_has_code_parttypes(lb
))
1040 printf("%2x\n", fdisk_parttype_get_code(t
));
1042 printf("%s\n", fdisk_parttype_get_string(t
));
1044 fdisk_unref_partition(pa
);
1045 fdisk_deassign_device(sf
->cxt
, 1);
1050 backup_partition_table(sf
, devname
);
1052 /* parse <type> and apply to PT */
1053 type
= fdisk_label_parse_parttype(lb
, typestr
);
1055 errx(EXIT_FAILURE
, _("failed to parse %s partition type '%s'"),
1056 fdisk_label_get_name(lb
), typestr
);
1058 else if (fdisk_set_partition_type(sf
->cxt
, partno
- 1, type
) != 0)
1059 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition type"),
1061 fdisk_unref_parttype(type
);
1062 return write_changes(sf
);
1066 * sfdisk --part-uuid <device> <partno> [<uuid>]
1068 static int command_partuuid(struct sfdisk
*sf
, int argc
, char **argv
)
1071 struct fdisk_partition
*pa
= NULL
;
1072 const char *devname
= NULL
, *uuid
= NULL
;
1075 errx(EXIT_FAILURE
, _("no disk device specified"));
1079 errx(EXIT_FAILURE
, _("no partition number specified"));
1080 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1085 errx(EXIT_FAILURE
, _("unexpected arguments"));
1087 /* read-only if uuid not given */
1088 assign_device_partition(sf
, devname
, partno
, !uuid
);
1090 /* print partition uuid */
1092 const char *str
= NULL
;
1094 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1095 str
= fdisk_partition_get_uuid(pa
);
1097 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition UUID"),
1099 printf("%s\n", str
);
1100 fdisk_unref_partition(pa
);
1101 fdisk_deassign_device(sf
->cxt
, 1);
1106 backup_partition_table(sf
, devname
);
1108 pa
= fdisk_new_partition();
1110 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1112 if (fdisk_partition_set_uuid(pa
, uuid
) != 0 ||
1113 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1114 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition UUID"),
1116 fdisk_unref_partition(pa
);
1117 return write_changes(sf
);
1121 * sfdisk --part-label <device> <partno> [<label>]
1123 static int command_partlabel(struct sfdisk
*sf
, int argc
, char **argv
)
1126 struct fdisk_partition
*pa
= NULL
;
1127 const char *devname
= NULL
, *name
= NULL
;
1130 errx(EXIT_FAILURE
, _("no disk device specified"));
1134 errx(EXIT_FAILURE
, _("no partition number specified"));
1135 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1140 errx(EXIT_FAILURE
, _("unexpected arguments"));
1142 /* read-only if name not given */
1143 assign_device_partition(sf
, devname
, partno
, !name
);
1145 /* print partition name */
1147 const char *str
= NULL
;
1149 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1150 str
= fdisk_partition_get_name(pa
);
1152 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to get partition name"),
1154 printf("%s\n", str
);
1155 fdisk_unref_partition(pa
);
1156 fdisk_deassign_device(sf
->cxt
, 1);
1161 backup_partition_table(sf
, devname
);
1163 pa
= fdisk_new_partition();
1165 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1167 if (fdisk_partition_set_name(pa
, name
) != 0 ||
1168 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1169 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition name"),
1172 fdisk_unref_partition(pa
);
1173 return write_changes(sf
);
1177 * sfdisk --part-attrs <device> <partno> [<attrs>]
1179 static int command_partattrs(struct sfdisk
*sf
, int argc
, char **argv
)
1182 struct fdisk_partition
*pa
= NULL
;
1183 const char *devname
= NULL
, *attrs
= NULL
;
1186 errx(EXIT_FAILURE
, _("no disk device specified"));
1190 errx(EXIT_FAILURE
, _("no partition number specified"));
1191 partno
= strtou32_or_err(argv
[1], _("failed to parse partition number"));
1196 errx(EXIT_FAILURE
, _("unexpected arguments"));
1198 /* read-only if name not given */
1199 assign_device_partition(sf
, devname
, partno
, !attrs
);
1201 /* print partition name */
1203 const char *str
= NULL
;
1205 if (fdisk_get_partition(sf
->cxt
, partno
- 1, &pa
) == 0)
1206 str
= fdisk_partition_get_attrs(pa
);
1208 printf("%s\n", str
);
1209 fdisk_unref_partition(pa
);
1210 fdisk_deassign_device(sf
->cxt
, 1);
1215 backup_partition_table(sf
, devname
);
1217 pa
= fdisk_new_partition();
1219 err(EXIT_FAILURE
, _("failed to allocate partition object"));
1221 if (fdisk_partition_set_attrs(pa
, attrs
) != 0 ||
1222 fdisk_set_partition(sf
->cxt
, partno
- 1, pa
) != 0)
1223 errx(EXIT_FAILURE
, _("%s: partition %zu: failed to set partition attributes"),
1226 fdisk_unref_partition(pa
);
1227 return write_changes(sf
);
1230 static void sfdisk_print_partition(struct sfdisk
*sf
, size_t n
)
1232 struct fdisk_partition
*pa
= NULL
;
1239 if (fdisk_get_partition(sf
->cxt
, n
, &pa
) != 0)
1242 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_DEVICE
, &data
);
1243 printf("%12s : ", data
);
1245 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_START
, &data
);
1246 printf("%12s ", data
);
1248 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_END
, &data
);
1249 printf("%12s ", data
);
1251 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_SIZE
, &data
);
1252 printf("(%s) ", data
);
1254 fdisk_partition_to_string(pa
, sf
->cxt
, FDISK_FIELD_TYPE
, &data
);
1255 printf("%s\n", data
);
1257 fdisk_unref_partition(pa
);
1260 static void command_fdisk_help(void)
1262 fputs(_("\nHelp:\n"), stdout
);
1264 fputc('\n', stdout
);
1265 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1266 fputs(_(" Commands:\n"), stdout
);
1268 fputs(_(" write write table to disk and exit\n"), stdout
);
1269 fputs(_(" quit show new situation and wait for user's feedback before write\n"), stdout
);
1270 fputs(_(" abort exit sfdisk shell\n"), stdout
);
1271 fputs(_(" print display the partition table\n"), stdout
);
1272 fputs(_(" help show this help text\n"), stdout
);
1273 fputc('\n', stdout
);
1274 fputs(_(" Ctrl-D the same as 'quit'\n"), stdout
);
1276 fputc('\n', stdout
);
1277 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1278 fputs(_(" Input format:\n"), stdout
);
1280 fputs(_(" <start>, <size>, <type>, <bootable>\n"), stdout
);
1282 fputc('\n', stdout
);
1283 fputs(_(" <start> Beginning of the partition in sectors, or bytes if\n"
1284 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1285 " The default is the first free space.\n"), stdout
);
1287 fputc('\n', stdout
);
1288 fputs(_(" <size> Size of the partition in sectors, or bytes if\n"
1289 " specified in the format <number>{K,M,G,T,P,E,Z,Y}.\n"
1290 " The default is all available space.\n"), stdout
);
1292 fputc('\n', stdout
);
1293 fputs(_(" <type> The partition type. Default is a Linux data partition.\n"), stdout
);
1294 fputs(_(" MBR: hex or L,S,E,X shortcuts.\n"), stdout
);
1295 fputs(_(" GPT: UUID or L,S,H shortcuts.\n"), stdout
);
1297 fputc('\n', stdout
);
1298 fputs(_(" <bootable> Use '*' to mark an MBR partition as bootable.\n"), stdout
);
1300 fputc('\n', stdout
);
1301 color_scheme_enable("help-title", UL_COLOR_BOLD
);
1302 fputs(_(" Example:\n"), stdout
);
1304 fputs(_(" , 4G Creates a 4GiB partition at default start offset.\n"), stdout
);
1305 fputc('\n', stdout
);
1309 SFDISK_DONE_NONE
= 0,
1316 /* returns: 0 on success, <0 on error, 1 successfully stop sfdisk */
1317 static int loop_control_commands(struct sfdisk
*sf
,
1318 struct fdisk_script
*dp
,
1321 const char *p
= skip_blank(buf
);
1322 int rc
= SFDISK_DONE_NONE
;
1324 if (strcmp(p
, "print") == 0)
1325 list_disklabel(sf
->cxt
);
1326 else if (strcmp(p
, "help") == 0)
1327 command_fdisk_help();
1328 else if (strcmp(p
, "quit") == 0)
1329 rc
= SFDISK_DONE_ASK
;
1330 else if (strcmp(p
, "write") == 0)
1331 rc
= SFDISK_DONE_WRITE
;
1332 else if (strcmp(p
, "abort") == 0)
1333 rc
= SFDISK_DONE_ABORT
;
1335 if (sf
->interactive
)
1336 fdisk_warnx(sf
->cxt
, _("unsupported command"));
1338 fdisk_warnx(sf
->cxt
, _("line %d: unsupported command"),
1339 fdisk_script_get_nlines(dp
));
1346 static int has_container(struct sfdisk
*sf
)
1349 struct fdisk_partition
*pa
= NULL
;
1352 return sf
->container
;
1354 nparts
= fdisk_get_npartitions(sf
->cxt
);
1355 for (i
= 0; i
< nparts
; i
++) {
1356 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0)
1358 if (fdisk_partition_is_container(pa
)) {
1364 fdisk_unref_partition(pa
);
1365 return sf
->container
;
1368 static size_t last_pt_partno(struct sfdisk
*sf
)
1370 size_t i
, nparts
, partno
= 0;
1371 struct fdisk_partition
*pa
= NULL
;
1374 nparts
= fdisk_get_npartitions(sf
->cxt
);
1375 for (i
= 0; i
< nparts
; i
++) {
1378 if (fdisk_get_partition(sf
->cxt
, i
, &pa
) != 0 ||
1379 !fdisk_partition_is_used(pa
))
1381 x
= fdisk_partition_get_partno(pa
);
1386 fdisk_unref_partition(pa
);
1390 static int is_device_used(struct sfdisk
*sf
)
1399 fd
= fdisk_get_devfd(sf
->cxt
);
1403 if (fstat(fd
, &st
) == 0 && S_ISBLK(st
.st_mode
)
1404 && major(st
.st_rdev
) != LOOPDEV_MAJOR
)
1405 return ioctl(fd
, BLKRRPART
) != 0;
1410 #ifdef HAVE_LIBREADLINE
1411 static char *sfdisk_fgets(struct fdisk_script
*dp
,
1412 char *buf
, size_t bufsz
, FILE *f
)
1414 struct sfdisk
*sf
= (struct sfdisk
*) fdisk_script_get_userdata(dp
);
1420 if (sf
->interactive
) {
1421 char *p
= readline(sf
->prompt
);
1427 if (len
> bufsz
- 2)
1430 memcpy(buf
, p
, len
);
1431 buf
[len
] = '\n'; /* append \n to be compatible with libc fgetc() */
1432 buf
[len
+ 1] = '\0';
1437 return fgets(buf
, bufsz
, f
);
1441 static int ignore_partition(struct fdisk_partition
*pa
)
1443 /* incomplete partition setting */
1444 if (!fdisk_partition_has_start(pa
) && !fdisk_partition_start_is_default(pa
))
1446 if (!fdisk_partition_has_size(pa
) && !fdisk_partition_end_is_default(pa
))
1449 /* probably dump from old sfdisk with start=0 size=0 */
1450 if (fdisk_partition_has_start(pa
) && fdisk_partition_get_start(pa
) == 0 &&
1451 fdisk_partition_has_size(pa
) && fdisk_partition_get_size(pa
) == 0)
1457 static int wipe_partition(struct sfdisk
*sf
, size_t partno
)
1460 char *fstype
= NULL
;;
1461 struct fdisk_partition
*tmp
= NULL
;
1463 DBG(MISC
, ul_debug("checking for signature"));
1465 rc
= fdisk_get_partition(sf
->cxt
, partno
, &tmp
);
1469 rc
= fdisk_partition_to_string(tmp
, sf
->cxt
, FDISK_FIELD_FSTYPE
, &fstype
);
1470 if (rc
|| fstype
== NULL
)
1473 fdisk_warnx(sf
->cxt
, _("Partition #%zu contains a %s signature."), partno
+ 1, fstype
);
1475 if (sf
->pwipemode
== WIPEMODE_AUTO
&& isatty(STDIN_FILENO
))
1476 fdisk_ask_yesno(sf
->cxt
, _("Do you want to remove the signature?"), &yes
);
1477 else if (sf
->pwipemode
== WIPEMODE_ALWAYS
)
1481 fdisk_info(sf
->cxt
, _("The signature will be removed by a write command."));
1482 rc
= fdisk_wipe_partition(sf
->cxt
, partno
, TRUE
);
1485 fdisk_unref_partition(tmp
);
1487 DBG(MISC
, ul_debug("partition wipe check end [rc=%d]", rc
));
1491 static void refresh_prompt_buffer(struct sfdisk
*sf
, const char *devname
,
1492 size_t next_partno
, int created
)
1495 char *partname
= fdisk_partname(devname
, next_partno
+ 1);
1497 err(EXIT_FAILURE
, _("failed to allocate partition name"));
1499 if (!sf
->prompt
|| !startswith(sf
->prompt
, partname
)) {
1501 xasprintf(&sf
->prompt
,"%s: ", partname
);
1504 } else if (!sf
->prompt
|| !startswith(sf
->prompt
, SFDISK_PROMPT
)) {
1506 sf
->prompt
= xstrdup(SFDISK_PROMPT
);
1511 * sfdisk <device> [[-N] <partno>]
1513 * Note that the option -N is there for backward compatibility only.
1515 static int command_fdisk(struct sfdisk
*sf
, int argc
, char **argv
)
1517 int rc
= 0, partno
= sf
->partno
, created
= 0, unused
= 0;
1518 struct fdisk_script
*dp
;
1519 struct fdisk_table
*tb
= NULL
;
1520 const char *devname
= NULL
, *label
;
1522 size_t next_partno
= (size_t) -1;
1526 if (partno
< 0 && argc
> 1)
1527 partno
= strtou32_or_err(argv
[1],
1528 _("failed to parse partition number"));
1530 errx(EXIT_FAILURE
, _("no disk device specified"));
1532 rc
= fdisk_assign_device(sf
->cxt
, devname
, 0);
1534 err(EXIT_FAILURE
, _("cannot open %s"), devname
);
1536 dp
= fdisk_new_script(sf
->cxt
);
1538 err(EXIT_FAILURE
, _("failed to allocate script handler"));
1539 fdisk_set_script(sf
->cxt
, dp
);
1540 #ifdef HAVE_LIBREADLINE
1541 fdisk_script_set_fgets(dp
, sfdisk_fgets
);
1543 fdisk_script_set_userdata(dp
, (void *) sf
);
1546 * Don't create a new disklabel when [-N] <partno> specified. In this
1547 * case reuse already specified disklabel. Let's check that the disk
1548 * really contains the partition.
1553 if (!fdisk_has_label(sf
->cxt
))
1554 errx(EXIT_FAILURE
, _("%s: cannot modify partition %d: "
1555 "no partition table was found"),
1556 devname
, partno
+ 1);
1557 n
= fdisk_get_npartitions(sf
->cxt
);
1558 if ((size_t) partno
> n
)
1559 errx(EXIT_FAILURE
, _("%s: cannot modify partition %d: "
1560 "partition table contains only %zu "
1562 devname
, partno
+ 1, n
);
1564 if (!fdisk_is_partition_used(sf
->cxt
, partno
)) {
1565 fdisk_warnx(sf
->cxt
, _("warning: %s: partition %d is not defined yet"),
1566 devname
, partno
+ 1);
1570 next_partno
= partno
;
1573 sf
->orig_pa
= get_partition(sf
->cxt
, partno
);
1578 next_partno
= last_pt_partno(sf
) + 1;
1581 if (!sf
->quiet
&& sf
->interactive
) {
1582 color_scheme_enable("welcome", UL_COLOR_GREEN
);
1583 fdisk_info(sf
->cxt
, _("\nWelcome to sfdisk (%s)."), PACKAGE_STRING
);
1585 fdisk_info(sf
->cxt
, _("Changes will remain in memory only, until you decide to write them.\n"
1586 "Be careful before using the write command.\n"));
1589 if (!sf
->noact
&& !sf
->noreread
) {
1591 fputs(_("Checking that no-one is using this disk right now ..."), stdout
);
1592 if (is_device_used(sf
)) {
1594 fputs(_(" FAILED\n\n"), stdout
);
1596 fdisk_warnx(sf
->cxt
, _(
1597 "This disk is currently in use - repartitioning is probably a bad idea.\n"
1598 "Umount all file systems, and swapoff all swap partitions on this disk.\n"
1599 "Use the --no-reread flag to suppress this check.\n"));
1602 errx(EXIT_FAILURE
, _("Use the --force flag to overrule all checks."));
1603 } else if (!sf
->quiet
)
1604 fputs(_(" OK\n\n"), stdout
);
1607 if (fdisk_get_collision(sf
->cxt
)) {
1608 int dowipe
= sf
->wipemode
== WIPEMODE_ALWAYS
? 1 : 0;
1610 fdisk_warnx(sf
->cxt
, _("Device %s already contains a %s signature."),
1611 devname
, fdisk_get_collision(sf
->cxt
));
1613 if (sf
->interactive
&& sf
->wipemode
== WIPEMODE_AUTO
)
1614 dowipe
= 1; /* do it in interactive mode */
1616 fdisk_enable_wipe(sf
->cxt
, dowipe
);
1618 fdisk_warnx(sf
->cxt
, _(
1619 "The signature will be removed by a write command."));
1621 fdisk_warnx(sf
->cxt
, _(
1622 "It is strongly recommended to wipe the device with "
1623 "wipefs(8), in order to avoid possible collisions."));
1624 fputc('\n', stderr
);
1628 backup_partition_table(sf
, devname
);
1631 list_disk_geometry(sf
->cxt
);
1632 if (fdisk_has_label(sf
->cxt
)) {
1633 fdisk_info(sf
->cxt
, _("\nOld situation:"));
1634 list_disklabel(sf
->cxt
);
1640 else if (fdisk_has_label(sf
->cxt
))
1641 label
= fdisk_label_get_name(fdisk_get_label(sf
->cxt
, NULL
));
1643 label
= "dos"; /* just for backward compatibility */
1645 fdisk_script_set_header(dp
, "label", label
);
1648 if (!sf
->quiet
&& sf
->interactive
) {
1649 if (!fdisk_has_label(sf
->cxt
) && !sf
->label
)
1651 _("\nsfdisk is going to create a new '%s' disk label.\n"
1652 "Use 'label: <name>' before you define a first partition\n"
1653 "to override the default."), label
);
1654 fdisk_info(sf
->cxt
, _("\nType 'help' to get more information.\n"));
1655 } else if (!sf
->quiet
)
1656 fputc('\n', stdout
);
1658 tb
= fdisk_script_get_table(dp
);
1664 DBG(PARSE
, ul_debug("<---next-line--->"));
1665 if (next_partno
== (size_t) -1)
1666 next_partno
= fdisk_table_get_nents(tb
);
1670 && next_partno
== fdisk_get_npartitions(sf
->cxt
)
1671 && !has_container(sf
)) {
1672 fdisk_info(sf
->cxt
, _("All partitions used."));
1673 rc
= SFDISK_DONE_ASK
;
1677 refresh_prompt_buffer(sf
, devname
, next_partno
, created
);
1678 if (sf
->prompt
&& (sf
->interactive
|| !sf
->quiet
)) {
1679 #ifndef HAVE_LIBREADLINE
1680 fputs(sf
->prompt
, stdout
);
1682 if (!sf
->interactive
)
1683 fputs(sf
->prompt
, stdout
);
1687 rc
= fdisk_script_read_line(dp
, stdin
, buf
, sizeof(buf
));
1689 DBG(PARSE
, ul_debug("script parsing failed, trying sfdisk specific commands"));
1690 buf
[sizeof(buf
) - 1] = '\0';
1691 rc
= loop_control_commands(sf
, dp
, buf
);
1695 } else if (rc
== 1) {
1696 rc
= SFDISK_DONE_EOF
;
1697 fputs(_("Done.\n"), stdout
);
1701 nparts
= fdisk_table_get_nents(tb
);
1704 struct fdisk_partition
*pa
= fdisk_table_get_partition(tb
, nparts
- 1);
1708 if (ignore_partition(pa
)) {
1709 fdisk_info(sf
->cxt
, _("Ignoring partition."));
1713 if (!created
) { /* create a new disklabel */
1714 rc
= fdisk_apply_script_headers(sf
->cxt
, dp
);
1717 fdisk_warnx(sf
->cxt
, _(
1718 "Failed to apply script headers, "
1719 "disk label not created."));
1721 if (!rc
&& partno
>= 0) { /* -N <partno>, modify partition */
1722 rc
= fdisk_set_partition(sf
->cxt
, partno
, pa
);
1723 rc
= rc
== 0 ? SFDISK_DONE_ASK
: SFDISK_DONE_ABORT
;
1725 } else if (!rc
) { /* add partition */
1726 if (!sf
->interactive
&&
1727 (!sf
->prompt
|| startswith(sf
->prompt
, SFDISK_PROMPT
))) {
1728 refresh_prompt_buffer(sf
, devname
, next_partno
, created
);
1729 fputs(sf
->prompt
, stdout
);
1731 rc
= fdisk_add_partition(sf
->cxt
, pa
, &cur_partno
);
1734 fdisk_warn(sf
->cxt
, _("Failed to add partition"));
1739 /* wipe partition on success
1741 * Note that unused=1 means -N <partno> for unused,
1742 * otherwise we wipe only newly created partitions.
1744 if (rc
== 0 && (unused
|| partno
< 0)) {
1745 rc
= wipe_partition(sf
, unused
? (size_t) partno
: cur_partno
);
1751 /* success print result */
1752 if (sf
->interactive
)
1753 sfdisk_print_partition(sf
, cur_partno
);
1754 next_partno
= cur_partno
+ 1;
1755 } else if (pa
) /* error, drop partition from script */
1756 fdisk_table_remove_partition(tb
, pa
);
1758 fdisk_info(sf
->cxt
, _("Script header accepted."));
1760 if (rc
&& !sf
->interactive
) {
1761 rc
= SFDISK_DONE_ABORT
;
1766 if (!sf
->quiet
&& rc
!= SFDISK_DONE_ABORT
) {
1767 fdisk_info(sf
->cxt
, _("\nNew situation:"));
1768 list_disklabel(sf
->cxt
);
1772 case SFDISK_DONE_ASK
:
1773 case SFDISK_DONE_EOF
:
1774 if (sf
->interactive
) {
1776 fdisk_ask_yesno(sf
->cxt
, _("Do you want to write this to disk?"), &yes
);
1778 fdisk_info(sf
->cxt
, _("Leaving."));
1783 case SFDISK_DONE_WRITE
:
1784 rc
= write_changes(sf
);
1786 case SFDISK_DONE_ABORT
:
1787 default: /* rc < 0 on error */
1788 fdisk_info(sf
->cxt
, _("Leaving.\n"));
1792 fdisk_unref_script(dp
);
1796 static void __attribute__ ((__noreturn__
)) usage(FILE *out
)
1798 fputs(USAGE_HEADER
, out
);
1801 _(" %1$s [options] <dev> [[-N] <part>]\n"
1802 " %1$s [options] <command>\n"), program_invocation_short_name
);
1804 fputs(USAGE_SEPARATOR
, out
);
1805 fputs(_("Display or manipulate a disk partition table.\n"), out
);
1807 fputs(_("\nCommands:\n"), out
);
1808 fputs(_(" -A, --activate <dev> [<part> ...] list or set bootable MBR partitions\n"), out
);
1809 fputs(_(" -d, --dump <dev> dump partition table (usable for later input)\n"), out
);
1810 fputs(_(" -J, --json <dev> dump partition table in JSON format\n"), out
);
1811 fputs(_(" -g, --show-geometry [<dev> ...] list geometry of all or specified devices\n"), out
);
1812 fputs(_(" -l, --list [<dev> ...] list partitions of each device\n"), out
);
1813 fputs(_(" -F, --list-free [<dev> ...] list unpartitioned free areas of each device\n"), out
);
1814 fputs(_(" -r, --reorder <dev> fix partitions order (by start offset)\n"), out
);
1815 fputs(_(" -s, --show-size [<dev> ...] list sizes of all or specified devices\n"), out
);
1816 fputs(_(" -T, --list-types print the recognized types (see -X)\n"), out
);
1817 fputs(_(" -V, --verify [<dev> ...] test whether partitions seem correct\n"), out
);
1818 fputs(_(" --delete <dev> [<part> ...] delete all or specified partitions\n"), out
);
1820 fputs(USAGE_SEPARATOR
, out
);
1821 fputs(_(" --part-label <dev> <part> [<str>] print or change partition label\n"), out
);
1822 fputs(_(" --part-type <dev> <part> [<type>] print or change partition type\n"), out
);
1823 fputs(_(" --part-uuid <dev> <part> [<uuid>] print or change partition uuid\n"), out
);
1824 fputs(_(" --part-attrs <dev> <part> [<str>] print or change partition attributes\n"), out
);
1826 fputs(USAGE_SEPARATOR
, out
);
1827 fputs(_(" <dev> device (usually disk) path\n"), out
);
1828 fputs(_(" <part> partition number\n"), out
);
1829 fputs(_(" <type> partition type, GUID for GPT, hex for MBR\n"), out
);
1831 fputs(USAGE_OPTIONS
, out
);
1832 fputs(_(" -a, --append append partitions to existing partition table\n"), out
);
1833 fputs(_(" -b, --backup backup partition table sectors (see -O)\n"), out
);
1834 fputs(_(" --bytes print SIZE in bytes rather than in human readable format\n"), out
);
1835 fputs(_(" --move-data[=<typescript>] move partition data after relocation (requires -N)\n"), out
);
1836 fputs(_(" -f, --force disable all consistency checking\n"), out
);
1837 fputs(_(" --color[=<when>] colorize output (auto, always or never)\n"), out
);
1839 " %s\n", USAGE_COLORS_DEFAULT
);
1840 fputs(_(" -N, --partno <num> specify partition number\n"), out
);
1841 fputs(_(" -n, --no-act do everything except write to device\n"), out
);
1842 fputs(_(" --no-reread do not check whether the device is in use\n"), out
);
1843 fputs(_(" --no-tell-kernel do not tell kernel about changes\n"), out
);
1844 fputs(_(" -O, --backup-file <path> override default backup file name\n"), out
);
1845 fputs(_(" -o, --output <list> output columns\n"), out
);
1846 fputs(_(" -q, --quiet suppress extra info messages\n"), out
);
1847 fputs(_(" -w, --wipe <mode> wipe signatures (auto, always or never)\n"), out
);
1848 fputs(_(" -W, --wipe-partitions <mode> wipe signatures from new partitions (auto, always or never)\n"), out
);
1849 fputs(_(" -X, --label <name> specify label type (dos, gpt, ...)\n"), out
);
1850 fputs(_(" -Y, --label-nested <name> specify nested label type (dos, bsd)\n"), out
);
1851 fputs(USAGE_SEPARATOR
, out
);
1852 fputs(_(" -G, --show-pt-geometry deprecated, alias to --show-geometry\n"), out
);
1853 fputs(_(" -L, --Linux deprecated, only for backward compatibility\n"), out
);
1854 fputs(_(" -u, --unit S deprecated, only sector unit is supported\n"), out
);
1856 fputs(USAGE_SEPARATOR
, out
);
1857 fputs(USAGE_HELP
, out
);
1858 fputs(_(" -v, --version output version information and exit\n"), out
);
1860 list_available_columns(out
);
1862 fprintf(out
, USAGE_MAN_TAIL("sfdisk(8)"));
1863 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
1867 int main(int argc
, char *argv
[])
1869 const char *outarg
= NULL
;
1870 int rc
= -EINVAL
, c
, longidx
= -1, bytes
= 0;
1871 int colormode
= UL_COLORMODE_UNDEF
;
1872 struct sfdisk _sf
= {
1874 .wipemode
= WIPEMODE_AUTO
,
1875 .pwipemode
= WIPEMODE_AUTO
,
1876 .interactive
= isatty(STDIN_FILENO
) ? 1 : 0,
1880 OPT_CHANGE_ID
= CHAR_MAX
+ 1,
1895 static const struct option longopts
[] = {
1896 { "activate",no_argument
, NULL
, 'A' },
1897 { "append", no_argument
, NULL
, 'a' },
1898 { "backup", no_argument
, NULL
, 'b' },
1899 { "backup-file", required_argument
, NULL
, 'O' },
1900 { "bytes", no_argument
, NULL
, OPT_BYTES
},
1901 { "color", optional_argument
, NULL
, OPT_COLOR
},
1902 { "delete", no_argument
, NULL
, OPT_DELETE
},
1903 { "dump", no_argument
, NULL
, 'd' },
1904 { "help", no_argument
, NULL
, 'h' },
1905 { "force", no_argument
, NULL
, 'f' },
1906 { "json", no_argument
, NULL
, 'J' },
1907 { "label", required_argument
, NULL
, 'X' },
1908 { "label-nested", required_argument
, NULL
, 'Y' },
1909 { "list", no_argument
, NULL
, 'l' },
1910 { "list-free", no_argument
, NULL
, 'F' },
1911 { "list-types", no_argument
, NULL
, 'T' },
1912 { "no-act", no_argument
, NULL
, 'n' },
1913 { "no-reread", no_argument
, NULL
, OPT_NOREREAD
},
1914 { "no-tell-kernel", no_argument
, NULL
, OPT_NOTELL
},
1915 { "move-data", optional_argument
, NULL
, OPT_MOVEDATA
},
1916 { "output", required_argument
, NULL
, 'o' },
1917 { "partno", required_argument
, NULL
, 'N' },
1918 { "reorder", no_argument
, NULL
, 'r' },
1919 { "show-size", no_argument
, NULL
, 's' },
1920 { "show-geometry", no_argument
, NULL
, 'g' },
1921 { "quiet", no_argument
, NULL
, 'q' },
1922 { "verify", no_argument
, NULL
, 'V' },
1923 { "version", no_argument
, NULL
, 'v' },
1924 { "wipe", required_argument
, NULL
, 'w' },
1925 { "wipe-partitions", required_argument
, NULL
, 'W' },
1927 { "part-uuid", no_argument
, NULL
, OPT_PARTUUID
},
1928 { "part-label", no_argument
, NULL
, OPT_PARTLABEL
},
1929 { "part-type", no_argument
, NULL
, OPT_PARTTYPE
},
1930 { "part-attrs", no_argument
, NULL
, OPT_PARTATTRS
},
1932 { "show-pt-geometry", no_argument
, NULL
, 'G' }, /* deprecated */
1933 { "unit", required_argument
, NULL
, 'u' },
1934 { "Linux", no_argument
, NULL
, 'L' }, /* deprecated */
1936 { "change-id",no_argument
, NULL
, OPT_CHANGE_ID
}, /* deprecated */
1937 { "id", no_argument
, NULL
, 'c' }, /* deprecated */
1938 { "print-id",no_argument
, NULL
, OPT_PRINT_ID
}, /* deprecated */
1943 setlocale(LC_ALL
, "");
1944 bindtextdomain(PACKAGE
, LOCALEDIR
);
1945 textdomain(PACKAGE
);
1946 atexit(close_stdout
);
1948 while ((c
= getopt_long(argc
, argv
, "aAbcdfFgGhJlLo:O:nN:qrsTu:vVX:Y:w:W:",
1949 longopts
, &longidx
)) != -1) {
1952 sf
->act
= ACT_ACTIVATE
;
1963 warnx(_("%s is deprecated in favour of --part-type"),
1964 longopts
[longidx
].name
);
1965 sf
->act
= ACT_PARTTYPE
;
1968 warnx(_("--id is deprecated in favour of --part-type"));
1969 sf
->act
= ACT_PARTTYPE
;
1978 sf
->act
= ACT_LIST_FREE
;
1984 warnx(_("--show-pt-geometry is no more implemented. Using --show-geometry."));
1986 sf
->act
= ACT_SHOW_GEOM
;
1995 warnx(_("--Linux option is unnecessary and deprecated"));
2002 sf
->backup_file
= optarg
;
2008 sf
->partno
= strtou32_or_err(optarg
, _("failed to parse partition number")) - 1;
2014 sf
->act
= ACT_REORDER
;
2017 sf
->act
= ACT_SHOW_SIZE
;
2020 sf
->act
= ACT_LIST_TYPES
;
2024 errx(EXIT_FAILURE
, _("unsupported unit '%c'"), *optarg
);
2027 printf(_("%s from %s\n"), program_invocation_short_name
,
2029 return EXIT_SUCCESS
;
2034 sf
->wipemode
= wipemode_from_string(optarg
);
2035 if (sf
->wipemode
< 0)
2036 errx(EXIT_FAILURE
, _("unsupported wipe mode"));
2039 sf
->pwipemode
= wipemode_from_string(optarg
);
2040 if (sf
->pwipemode
< 0)
2041 errx(EXIT_FAILURE
, _("unsupported wipe mode"));
2047 sf
->label_nested
= optarg
;
2051 sf
->act
= ACT_PARTUUID
;
2054 sf
->act
= ACT_PARTTYPE
;
2057 sf
->act
= ACT_PARTLABEL
;
2060 sf
->act
= ACT_PARTATTRS
;
2069 colormode
= UL_COLORMODE_AUTO
;
2071 colormode
= colormode_or_err(optarg
,
2072 _("unsupported color mode"));
2076 sf
->move_typescript
= optarg
;
2079 sf
->act
= ACT_DELETE
;
2089 colors_init(colormode
, "sfdisk");
2093 fdisk_set_size_unit(sf
->cxt
, FDISK_SIZEUNIT_BYTES
);
2096 init_fields(NULL
, outarg
, NULL
);
2098 if (sf
->verify
&& !sf
->act
)
2099 sf
->act
= ACT_VERIFY
; /* --verify make be used with --list too */
2101 sf
->act
= ACT_FDISK
; /* default */
2103 if (sf
->movedata
&& !(sf
->act
== ACT_FDISK
&& sf
->partno
>= 0))
2104 errx(EXIT_FAILURE
, _("--movedata requires -N"));
2108 rc
= command_activate(sf
, argc
- optind
, argv
+ optind
);
2112 rc
= command_delete(sf
, argc
- optind
, argv
+ optind
);
2116 rc
= command_list_partitions(sf
, argc
- optind
, argv
+ optind
);
2119 case ACT_LIST_TYPES
:
2120 rc
= command_list_types(sf
);
2124 rc
= command_list_freespace(sf
, argc
- optind
, argv
+ optind
);
2128 rc
= command_fdisk(sf
, argc
- optind
, argv
+ optind
);
2132 rc
= command_dump(sf
, argc
- optind
, argv
+ optind
);
2136 rc
= command_show_size(sf
, argc
- optind
, argv
+ optind
);
2140 rc
= command_show_geometry(sf
, argc
- optind
, argv
+ optind
);
2144 rc
= command_verify(sf
, argc
- optind
, argv
+ optind
);
2148 rc
= command_parttype(sf
, argc
- optind
, argv
+ optind
);
2152 rc
= command_partuuid(sf
, argc
- optind
, argv
+ optind
);
2156 rc
= command_partlabel(sf
, argc
- optind
, argv
+ optind
);
2160 rc
= command_partattrs(sf
, argc
- optind
, argv
+ optind
);
2164 rc
= command_reorder(sf
, argc
- optind
, argv
+ optind
);
2170 DBG(MISC
, ul_debug("bye! [rc=%d]", rc
));
2171 return rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;