2 * Copyright (C) 1992 A. V. Le Blanc (LeBlanc@mcc.ac.uk)
3 * Copyright (C) 2012 Davidlohr Bueso <dave@gnu.org>
5 * Copyright (C) 2007-2013 Karel Zak <kzak@redhat.com>
7 * This program is free software. You can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation: either version 1 or
10 * (at your option) any later version.
24 #include <libsmartcols.h>
25 #ifdef HAVE_LIBREADLINE
26 # define _FUNCTION_DEF
27 # include <readline/readline.h>
37 #include "pathnames.h"
38 #include "canonicalize.h"
40 #include "closestream.h"
45 #include "pt-sun.h" /* to toggle flags */
47 #ifdef HAVE_LINUX_COMPILER_H
48 # include <linux/compiler.h>
50 #ifdef HAVE_LINUX_BLKPG_H
51 # include <linux/blkpg.h>
54 int pwipemode
= WIPEMODE_AUTO
;
55 static int wipemode
= WIPEMODE_AUTO
;
58 * fdisk debug stuff (see fdisk.h and include/debug.h)
60 UL_DEBUG_DEFINE_MASK(fdisk
);
61 UL_DEBUG_DEFINE_MASKNAMES(fdisk
) = UL_DEBUG_EMPTY_MASKNAMES
;
63 static void fdiskprog_init_debug(void)
65 __UL_INIT_DEBUG(fdisk
, FDISKPROG_DEBUG_
, 0, FDISK_DEBUG
);
68 #ifdef HAVE_LIBREADLINE
69 static char *rl_fgets(char *s
, int n
, FILE *stream
, const char *prompt
)
73 rl_outstream
= stream
;
85 int get_user_reply(struct fdisk_context
*cxt
, const char *prompt
,
86 char *buf
, size_t bufsz
)
92 #ifdef HAVE_LIBREADLINE
93 if (isatty(STDIN_FILENO
)) {
94 if (!rl_fgets(buf
, bufsz
, stdout
, prompt
)) {
95 if (fdisk_label_is_changed(fdisk_get_label(cxt
, NULL
))) {
96 if (rl_fgets(buf
, bufsz
, stderr
,
97 _("\nDo you really want to quit? "))
101 fdisk_unref_context(cxt
);
109 fputs(prompt
, stdout
);
111 if (!fgets(buf
, bufsz
, stdin
)) {
112 if (fdisk_label_is_changed(fdisk_get_label(cxt
, NULL
))) {
113 fprintf(stderr
, _("\nDo you really want to quit? "));
115 if (fgets(buf
, bufsz
, stdin
) && !rpmatch(buf
))
118 fdisk_unref_context(cxt
);
125 for (p
= buf
; *p
&& !isgraph(*p
); p
++); /* get first non-blank */
128 memmove(buf
, p
, p
- buf
); /* remove blank space */
130 if (sz
&& *(buf
+ sz
- 1) == '\n')
131 *(buf
+ sz
- 1) = '\0';
133 DBG(ASK
, ul_debug("user's reply: >>>%s<<<", buf
));
137 static int ask_menu(struct fdisk_context
*cxt
, struct fdisk_ask
*ask
,
138 char *buf
, size_t bufsz
)
141 const char *q
= fdisk_ask_get_query(ask
);
142 int dft
= fdisk_ask_menu_get_default(ask
);
145 fputs(q
, stdout
); /* print header */
152 const char *name
, *desc
;
155 /* print menu items */
156 while (fdisk_ask_menu_get_item(ask
, i
++, &key
, &name
, &desc
) == 0)
157 fprintf(stdout
, " %c %s (%s)\n", key
, name
, desc
);
160 snprintf(prompt
, sizeof(prompt
), _("Select (default %c): "), dft
);
161 rc
= get_user_reply(cxt
, prompt
, buf
, bufsz
);
165 fdisk_info(cxt
, _("Using default response %c."), dft
);
172 while (fdisk_ask_menu_get_item(ask
, i
++, &key
, NULL
, NULL
) == 0) {
174 fdisk_ask_menu_set_result(ask
, c
);
175 return 0; /* success */
178 fdisk_warnx(cxt
, _("Value out of range."));
185 #define tochar(num) ((int) ('a' + num - 1))
186 static int ask_number(struct fdisk_context
*cxt
,
187 struct fdisk_ask
*ask
,
188 char *buf
, size_t bufsz
)
190 char prompt
[128] = { '\0' };
191 const char *q
= fdisk_ask_get_query(ask
);
192 const char *range
= fdisk_ask_number_get_range(ask
);
194 uint64_t dflt
= fdisk_ask_number_get_default(ask
),
195 low
= fdisk_ask_number_get_low(ask
),
196 high
= fdisk_ask_number_get_high(ask
);
197 int inchar
= fdisk_ask_number_inchars(ask
);
201 DBG(ASK
, ul_debug("asking for number "
202 "['%s', <%"PRIu64
",%"PRIu64
">, default=%"PRIu64
", range: %s]",
203 q
, low
, high
, dflt
, range
));
205 if (range
&& dflt
>= low
&& dflt
<= high
) {
207 snprintf(prompt
, sizeof(prompt
), _("%s (%s, default %c): "),
208 q
, range
, tochar(dflt
));
210 snprintf(prompt
, sizeof(prompt
), _("%s (%s, default %"PRIu64
"): "),
213 } else if (dflt
>= low
&& dflt
<= high
) {
215 snprintf(prompt
, sizeof(prompt
), _("%s (%c-%c, default %c): "),
216 q
, tochar(low
), tochar(high
), tochar(dflt
));
218 snprintf(prompt
, sizeof(prompt
),
219 _("%s (%"PRIu64
"-%"PRIu64
", default %"PRIu64
"): "),
222 snprintf(prompt
, sizeof(prompt
), _("%s (%c-%c): "),
223 q
, tochar(low
), tochar(high
));
225 snprintf(prompt
, sizeof(prompt
), _("%s (%"PRIu64
"-%"PRIu64
"): "),
229 int rc
= get_user_reply(cxt
, prompt
, buf
, bufsz
);
234 if (!*buf
&& dflt
>= low
&& dflt
<= high
)
235 return fdisk_ask_number_set_result(ask
, dflt
);
237 if (isdigit_string(buf
)) {
241 num
= strtoumax(buf
, &end
, 10);
242 if (errno
|| buf
== end
|| (end
&& *end
))
244 } else if (inchar
&& isalpha(*buf
)) {
245 num
= tolower(*buf
) - 'a' + 1;
249 if (rc
== 0 && num
>= low
&& num
<= high
)
250 return fdisk_ask_number_set_result(ask
, num
);
252 fdisk_warnx(cxt
, _("Value out of range."));
258 static int ask_offset(struct fdisk_context
*cxt
,
259 struct fdisk_ask
*ask
,
260 char *buf
, size_t bufsz
)
262 char prompt
[128] = { '\0' };
263 const char *q
= fdisk_ask_get_query(ask
);
264 const char *range
= fdisk_ask_number_get_range(ask
);
266 uint64_t dflt
= fdisk_ask_number_get_default(ask
),
267 low
= fdisk_ask_number_get_low(ask
),
268 high
= fdisk_ask_number_get_high(ask
),
269 base
= fdisk_ask_number_get_base(ask
);
273 DBG(ASK
, ul_debug("asking for offset ['%s', <%"PRIu64
",%"PRIu64
">, base=%"PRIu64
", default=%"PRIu64
", range: %s]",
274 q
, low
, high
, base
, dflt
, range
));
276 if (range
&& dflt
>= low
&& dflt
<= high
)
277 snprintf(prompt
, sizeof(prompt
), _("%s (%s, default %"PRIu64
"): "),
279 else if (dflt
>= low
&& dflt
<= high
)
280 snprintf(prompt
, sizeof(prompt
),
281 _("%s (%"PRIu64
"-%"PRIu64
", default %"PRIu64
"): "),
284 snprintf(prompt
, sizeof(prompt
), _("%s (%"PRIu64
"-%"PRIu64
"): "),
292 int rc
= get_user_reply(cxt
, prompt
, buf
, bufsz
);
295 if (!*buf
&& dflt
>= low
&& dflt
<= high
)
296 return fdisk_ask_number_set_result(ask
, dflt
);
299 if (*p
== '+' || *p
== '-') {
304 rc
= parse_size(p
, &num
, &pwr
);
307 DBG(ASK
, ul_debug("parsed size: %ju", num
));
309 /* +{size}{K,M,...} specified, the "num" is in bytes */
310 uint64_t unit
= fdisk_ask_number_get_unit(ask
);
311 num
+= unit
/2; /* round */
319 DBG(ASK
, ul_debug("final offset: %ju [sig: %c, power: %d, %s]",
321 sig
? "relative" : "absolute"));
322 if (num
>= low
&& num
<= high
) {
324 fdisk_ask_number_set_relative(ask
, 1);
325 return fdisk_ask_number_set_result(ask
, (uint64_t)num
);
327 fdisk_warnx(cxt
, _("Value out of range."));
333 static unsigned int info_count
;
335 static void fputs_info(struct fdisk_ask
*ask
, FILE *out
)
340 msg
= fdisk_ask_print_get_mesg(ask
);
350 int ask_callback(struct fdisk_context
*cxt
, struct fdisk_ask
*ask
,
351 void *data
__attribute__((__unused__
)))
359 if (fdisk_ask_get_type(ask
) != FDISK_ASKTYPE_INFO
)
362 switch(fdisk_ask_get_type(ask
)) {
363 case FDISK_ASKTYPE_MENU
:
364 return ask_menu(cxt
, ask
, buf
, sizeof(buf
));
365 case FDISK_ASKTYPE_NUMBER
:
366 return ask_number(cxt
, ask
, buf
, sizeof(buf
));
367 case FDISK_ASKTYPE_OFFSET
:
368 return ask_offset(cxt
, ask
, buf
, sizeof(buf
));
369 case FDISK_ASKTYPE_INFO
:
370 if (!fdisk_is_listonly(cxt
))
372 fputs_info(ask
, stdout
);
374 case FDISK_ASKTYPE_WARNX
:
376 color_scheme_fenable("warn", UL_COLOR_RED
, stderr
);
377 fputs(fdisk_ask_print_get_mesg(ask
), stderr
);
378 color_fdisable(stderr
);
381 case FDISK_ASKTYPE_WARN
:
383 color_scheme_fenable("warn", UL_COLOR_RED
, stderr
);
384 fputs(fdisk_ask_print_get_mesg(ask
), stderr
);
385 errno
= fdisk_ask_print_get_errno(ask
);
386 fprintf(stderr
, ": %m\n");
387 color_fdisable(stderr
);
389 case FDISK_ASKTYPE_YESNO
:
393 fputs(fdisk_ask_get_query(ask
), stdout
);
394 rc
= get_user_reply(cxt
, _(" [Y]es/[N]o: "), buf
, sizeof(buf
));
398 if (x
== RPMATCH_YES
|| x
== RPMATCH_NO
) {
399 fdisk_ask_yesno_set_result(ask
, x
);
403 DBG(ASK
, ul_debug("yes-no ask: reply '%s' [rc=%d]", buf
, rc
));
405 case FDISK_ASKTYPE_STRING
:
408 snprintf(prmt
, sizeof(prmt
), "%s: ", fdisk_ask_get_query(ask
));
410 rc
= get_user_reply(cxt
, prmt
, buf
, sizeof(buf
));
412 fdisk_ask_string_set_result(ask
, xstrdup(buf
));
413 DBG(ASK
, ul_debug("string ask: reply '%s' [rc=%d]", buf
, rc
));
417 warnx(_("internal error: unsupported dialog type %d"), fdisk_ask_get_type(ask
));
423 static struct fdisk_parttype
*ask_partition_type(struct fdisk_context
*cxt
)
426 struct fdisk_label
*lb
;
429 lb
= fdisk_get_label(cxt
, NULL
);
434 q
= fdisk_label_has_code_parttypes(lb
) ?
435 _("Partition type (type L to list all types): ") :
436 _("Hex code (type L to list all codes): ");
439 int rc
= get_user_reply(cxt
, q
, buf
, sizeof(buf
));
444 if (buf
[1] == '\0' && toupper(*buf
) == 'L')
445 list_partition_types(cxt
);
447 return fdisk_label_parse_parttype(lb
, buf
);
454 void list_partition_types(struct fdisk_context
*cxt
)
457 struct fdisk_label
*lb
;
460 lb
= fdisk_get_label(cxt
, NULL
);
463 ntypes
= fdisk_label_get_nparttypes(lb
);
467 if (fdisk_label_has_code_parttypes(lb
)) {
469 * Prints in 4 columns in format <hex> <name>
471 size_t last
[4], done
= 0, next
= 0, size
;
476 for (i
= 3; i
>= 0; i
--)
477 last
[3 - i
] = done
+= (size
+ i
- done
) / (i
+ 1);
481 #define NAME_WIDTH 15
482 char name
[NAME_WIDTH
* MB_LEN_MAX
];
483 size_t width
= NAME_WIDTH
;
484 const struct fdisk_parttype
*t
= fdisk_label_get_parttype(lb
, next
);
487 if (fdisk_parttype_get_name(t
)) {
488 printf("%c%2x ", i
? ' ' : '\n',
489 fdisk_parttype_get_code(t
));
490 ret
= mbsalign(_(fdisk_parttype_get_name(t
)),
492 &width
, MBS_ALIGN_LEFT
, 0);
494 if (ret
== (size_t)-1 || ret
>= sizeof(name
))
496 _(fdisk_parttype_get_name(t
)));
501 next
= last
[i
++] + done
;
502 if (i
> 3 || next
>= last
[i
]) {
506 } while (done
< last
[0]);
510 * Prints 1 column in format <idx> <name> <typestr>
516 for (i
= 0; i
< ntypes
; i
++) {
517 const struct fdisk_parttype
*t
= fdisk_label_get_parttype(lb
, i
);
518 printf("%3zu %-30s %s\n", i
+ 1,
519 fdisk_parttype_get_name(t
),
520 fdisk_parttype_get_string(t
));
528 void toggle_dos_compatibility_flag(struct fdisk_context
*cxt
)
530 struct fdisk_label
*lb
= fdisk_get_label(cxt
, "dos");
536 flag
= !fdisk_dos_is_compatible(lb
);
537 fdisk_info(cxt
, flag
?
538 _("DOS Compatibility flag is set (DEPRECATED!)") :
539 _("DOS Compatibility flag is not set"));
541 fdisk_dos_enable_compatible(lb
, flag
);
543 if (fdisk_is_label(cxt
, DOS
))
544 fdisk_reset_alignment(cxt
); /* reset the current label */
547 void change_partition_type(struct fdisk_context
*cxt
)
550 struct fdisk_parttype
*t
= NULL
;
551 struct fdisk_partition
*pa
= NULL
;
552 const char *old
= NULL
;
556 if (fdisk_ask_partnum(cxt
, &i
, FALSE
))
559 if (fdisk_get_partition(cxt
, i
, &pa
)) {
560 fdisk_warnx(cxt
, _("Partition %zu does not exist yet!"), i
+ 1);
564 t
= (struct fdisk_parttype
*) fdisk_partition_get_type(pa
);
565 old
= t
? fdisk_parttype_get_name(t
) : _("Unknown");
568 t
= ask_partition_type(cxt
);
571 if (fdisk_set_partition_type(cxt
, i
, t
) == 0)
573 _("Changed type of partition '%s' to '%s'."),
574 old
, t
? fdisk_parttype_get_name(t
) : _("Unknown"));
577 _("Type of partition %zu is unchanged: %s."),
580 fdisk_unref_partition(pa
);
581 fdisk_unref_parttype(t
);
584 int print_partition_info(struct fdisk_context
*cxt
)
586 struct fdisk_partition
*pa
= NULL
;
590 struct fdisk_label
*lb
= fdisk_get_label(cxt
, NULL
);
592 if ((rc
= fdisk_ask_partnum(cxt
, &i
, FALSE
)))
595 if ((rc
= fdisk_get_partition(cxt
, i
, &pa
))) {
596 fdisk_warnx(cxt
, _("Partition %zu does not exist yet!"), i
+ 1);
600 if ((rc
= fdisk_label_get_fields_ids_all(lb
, cxt
, &fields
, &nfields
)))
603 for (i
= 0; i
< nfields
; ++i
) {
606 const struct fdisk_field
*fd
= fdisk_label_get_field(lb
, id
);
611 rc
= fdisk_partition_to_string(pa
, cxt
, id
, &data
);
616 fdisk_info(cxt
, _("%15s: %s"), fdisk_field_get_name(fd
), data
);
621 fdisk_unref_partition(pa
);
626 static size_t skip_empty(const unsigned char *buf
, size_t i
, size_t sz
)
629 const unsigned char *p0
= buf
+ i
;
631 for (next
= i
+ 16; next
< sz
; next
+= 16) {
632 if (memcmp(p0
, buf
+ next
, 16) != 0)
636 return next
== i
+ 16 ? i
: next
;
639 static void dump_buffer(off_t base
, unsigned char *buf
, size_t sz
, int all
)
641 size_t i
, l
, next
= 0;
645 for (i
= 0, l
= 0; i
< sz
; i
++, l
++) {
647 if (all
== 0 && !next
)
648 next
= skip_empty(buf
, i
, sz
);
649 printf("%08jx ", (intmax_t)base
+ i
);
651 printf(" %02x", buf
[i
]);
652 if (l
== 7) /* words separator */
655 fputc('\n', stdout
); /* next line */
668 static void dump_blkdev(struct fdisk_context
*cxt
, const char *name
,
669 uint64_t offset
, size_t size
, int all
)
671 int fd
= fdisk_get_devfd(cxt
);
673 fdisk_info(cxt
, _("\n%s: offset = %"PRIu64
", size = %zu bytes."),
678 if (lseek(fd
, (off_t
) offset
, SEEK_SET
) == (off_t
) -1)
679 fdisk_warn(cxt
, _("cannot seek"));
681 unsigned char *buf
= xmalloc(size
);
683 if (read_all(fd
, (char *) buf
, size
) != (ssize_t
) size
)
684 fdisk_warn(cxt
, _("cannot read"));
686 dump_buffer(offset
, buf
, size
, all
);
691 void dump_firstsector(struct fdisk_context
*cxt
)
693 int all
= !isatty(STDOUT_FILENO
);
697 dump_blkdev(cxt
, _("First sector"), 0, fdisk_get_sector_size(cxt
), all
);
700 void dump_disklabel(struct fdisk_context
*cxt
)
702 int all
= !isatty(STDOUT_FILENO
);
704 const char *name
= NULL
;
710 while (fdisk_locate_disklabel(cxt
, i
++, &name
, &offset
, &size
) == 0 && size
)
711 dump_blkdev(cxt
, name
, offset
, size
, all
);
714 static fdisk_sector_t
get_dev_blocks(char *dev
)
719 if ((fd
= open(dev
, O_RDONLY
)) < 0)
720 err(EXIT_FAILURE
, _("cannot open %s"), dev
);
721 ret
= blkdev_get_sectors(fd
, (unsigned long long *) &size
);
724 err(EXIT_FAILURE
, _("BLKGETSIZE ioctl failed on %s"), dev
);
729 void follow_wipe_mode(struct fdisk_context
*cxt
)
731 int dowipe
= wipemode
== WIPEMODE_ALWAYS
? 1 : 0;
733 if (isatty(STDIN_FILENO
) && wipemode
== WIPEMODE_AUTO
)
734 dowipe
= 1; /* do it in interactive mode */
736 if (fdisk_is_ptcollision(cxt
) && wipemode
!= WIPEMODE_NEVER
)
737 dowipe
= 1; /* always remove old PT */
739 fdisk_enable_wipe(cxt
, dowipe
);
742 "The old %s signature will be removed by a write command."),
743 fdisk_get_collision(cxt
));
746 "The old %s signature may remain on the device. "
747 "It is recommended to wipe the device with wipefs(8) or "
748 "fdisk --wipe, in order to avoid possible collisions."),
749 fdisk_get_collision(cxt
));
752 static void __attribute__ ((__noreturn__
)) usage(FILE *out
)
754 fputs(USAGE_HEADER
, out
);
757 _(" %1$s [options] <disk> change partition table\n"
758 " %1$s [options] -l [<disk>] list partition table(s)\n"),
759 program_invocation_short_name
);
761 fputs(USAGE_SEPARATOR
, out
);
762 fputs(_("Display or manipulate a disk partition table.\n"), out
);
764 fputs(USAGE_OPTIONS
, out
);
765 fputs(_(" -b, --sector-size <size> physical and logical sector size\n"), out
);
766 fputs(_(" -B, --protect-boot don't erase bootbits when creating a new label\n"), out
);
767 fputs(_(" -c, --compatibility[=<mode>] mode is 'dos' or 'nondos' (default)\n"), out
);
768 fputs(_(" -L, --color[=<when>] colorize output (auto, always or never)\n"), out
);
770 " %s\n", USAGE_COLORS_DEFAULT
);
771 fputs(_(" -l, --list display partitions and exit\n"), out
);
772 fputs(_(" -o, --output <list> output columns\n"), out
);
773 fputs(_(" -t, --type <type> recognize specified partition table type only\n"), out
);
774 fputs(_(" -u, --units[=<unit>] display units: 'cylinders' or 'sectors' (default)\n"), out
);
775 fputs(_(" -s, --getsz display device size in 512-byte sectors [DEPRECATED]\n"), out
);
776 fputs(_(" --bytes print SIZE in bytes rather than in human readable format\n"), out
);
777 fputs(_(" -w, --wipe <mode> wipe signatures (auto, always or never)\n"), out
);
778 fputs(_(" -W, --wipe-partitions <mode> wipe signatures from new partitions (auto, always or never)\n"), out
);
780 fputs(USAGE_SEPARATOR
, out
);
781 fputs(_(" -C, --cylinders <number> specify the number of cylinders\n"), out
);
782 fputs(_(" -H, --heads <number> specify the number of heads\n"), out
);
783 fputs(_(" -S, --sectors <number> specify the number of sectors per track\n"), out
);
785 fputs(USAGE_SEPARATOR
, out
);
786 fputs(USAGE_HELP
, out
);
787 fputs(USAGE_VERSION
, out
);
789 list_available_columns(out
);
791 fprintf(out
, USAGE_MAN_TAIL("fdisk(8)"));
792 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
797 ACT_FDISK
= 0, /* default */
802 int main(int argc
, char **argv
)
804 int rc
, i
, c
, act
= ACT_FDISK
;
805 int colormode
= UL_COLORMODE_UNDEF
;
806 struct fdisk_context
*cxt
;
809 OPT_BYTES
= CHAR_MAX
+ 1
811 static const struct option longopts
[] = {
812 { "bytes", no_argument
, NULL
, OPT_BYTES
},
813 { "color", optional_argument
, NULL
, 'L' },
814 { "compatibility", optional_argument
, NULL
, 'c' },
815 { "cylinders", required_argument
, NULL
, 'C' },
816 { "heads", required_argument
, NULL
, 'H' },
817 { "sectors", required_argument
, NULL
, 'S' },
818 { "getsz", no_argument
, NULL
, 's' },
819 { "help", no_argument
, NULL
, 'h' },
820 { "list", no_argument
, NULL
, 'l' },
821 { "sector-size", required_argument
, NULL
, 'b' },
822 { "type", required_argument
, NULL
, 't' },
823 { "units", optional_argument
, NULL
, 'u' },
824 { "version", no_argument
, NULL
, 'V' },
825 { "output", no_argument
, NULL
, 'o' },
826 { "protect-boot", no_argument
, NULL
, 'B' },
827 { "wipe", required_argument
, NULL
, 'w' },
828 { "wipe-partitions",required_argument
, NULL
, 'W' },
832 setlocale(LC_ALL
, "");
833 bindtextdomain(PACKAGE
, LOCALEDIR
);
835 atexit(close_stdout
);
839 fdiskprog_init_debug();
841 cxt
= fdisk_new_context();
843 err(EXIT_FAILURE
, _("failed to allocate libfdisk context"));
845 fdisk_set_ask(cxt
, ask_callback
, NULL
);
847 while ((c
= getopt_long(argc
, argv
, "b:Bc::C:hH:lL::o:sS:t:u::vVw:W:",
848 longopts
, NULL
)) != -1) {
852 size_t sz
= strtou32_or_err(optarg
,
853 _("invalid sector size argument"));
854 if (sz
!= 512 && sz
!= 1024 && sz
!= 2048 && sz
!= 4096)
856 fdisk_save_user_sector_size(cxt
, sz
, sz
);
860 fdisk_enable_bootbits_protection(cxt
, 1);
863 fdisk_save_user_geometry(cxt
,
864 strtou32_or_err(optarg
,
865 _("invalid cylinders argument")),
870 /* this setting is independent on the current
871 * actively used label
873 char *p
= *optarg
== '=' ? optarg
+ 1 : optarg
;
874 struct fdisk_label
*lb
= fdisk_get_label(cxt
, "dos");
877 err(EXIT_FAILURE
, _("not found DOS label driver"));
878 if (strcmp(p
, "dos") == 0)
879 fdisk_dos_enable_compatible(lb
, TRUE
);
880 else if (strcmp(p
, "nondos") == 0)
881 fdisk_dos_enable_compatible(lb
, FALSE
);
883 warnx(_("unknown compatibility mode '%s'"), p
);
887 /* use default if no optarg specified */
890 fdisk_save_user_geometry(cxt
, 0,
891 strtou32_or_err(optarg
,
892 _("invalid heads argument")),
896 fdisk_save_user_geometry(cxt
, 0, 0,
897 strtou32_or_err(optarg
,
898 _("invalid sectors argument")));
904 colormode
= UL_COLORMODE_AUTO
;
906 colormode
= colormode_or_err(optarg
,
907 _("unsupported color mode"));
917 struct fdisk_label
*lb
= NULL
;
919 while (fdisk_next_label(cxt
, &lb
) == 0)
920 fdisk_label_set_disabled(lb
, 1);
922 lb
= fdisk_get_label(cxt
, optarg
);
924 errx(EXIT_FAILURE
, _("unsupported disklabel: %s"), optarg
);
925 fdisk_label_set_disabled(lb
, 0);
929 if (optarg
&& *optarg
== '=')
931 if (fdisk_set_unit(cxt
, optarg
) != 0)
934 case 'V': /* preferred for util-linux */
935 case 'v': /* for backward compatibility only */
936 printf(UTIL_LINUX_VERSION
);
939 wipemode
= wipemode_from_string(optarg
);
941 errx(EXIT_FAILURE
, _("unsupported wipe mode"));
944 pwipemode
= wipemode_from_string(optarg
);
946 errx(EXIT_FAILURE
, _("unsupported wipe mode"));
951 fdisk_set_size_unit(cxt
, FDISK_SIZEUNIT_BYTES
);
954 errtryhelp(EXIT_FAILURE
);
958 if (argc
-optind
!= 1 && fdisk_has_user_device_properties(cxt
))
959 warnx(_("The device properties (sector size and geometry) should"
960 " be used with one specified device only."));
962 colors_init(colormode
, "fdisk");
966 fdisk_enable_listonly(cxt
, 1);
967 init_fields(cxt
, outarg
, NULL
);
973 for (rc
= 0, k
= optind
; k
< argc
; k
++) {
975 fputs("\n\n", stdout
);
977 rc
+= print_device_pt(cxt
, argv
[k
], 1, 0);
983 print_all_devices_pt(cxt
, 0);
988 if (argc
- optind
<= 0)
991 for (i
= optind
; i
< argc
; i
++) {
992 uintmax_t blks
= get_dev_blocks(argv
[i
]);
994 if (argc
- optind
== 1)
995 printf("%ju\n", blks
);
997 printf("%s: %ju\n", argv
[i
], blks
);
1002 if (argc
-optind
!= 1)
1005 /* Here starts interactive mode, use fdisk_{warn,info,..} functions */
1006 color_scheme_enable("welcome", UL_COLOR_GREEN
);
1007 fdisk_info(cxt
, _("Welcome to fdisk (%s)."), PACKAGE_STRING
);
1009 fdisk_info(cxt
, _("Changes will remain in memory only, until you decide to write them.\n"
1010 "Be careful before using the write command.\n"));
1012 rc
= fdisk_assign_device(cxt
, argv
[optind
], 0);
1013 if (rc
== -EACCES
) {
1014 rc
= fdisk_assign_device(cxt
, argv
[optind
], 1);
1016 fdisk_warnx(cxt
, _("Device is open in read-only mode."));
1019 err(EXIT_FAILURE
, _("cannot open %s"), argv
[optind
]);
1023 if (fdisk_get_collision(cxt
))
1024 follow_wipe_mode(cxt
);
1026 if (!fdisk_has_label(cxt
)) {
1027 fdisk_info(cxt
, _("Device does not contain a recognized partition table."));
1028 fdisk_create_disklabel(cxt
, NULL
);
1030 } else if (fdisk_is_label(cxt
, GPT
) && fdisk_gpt_is_hybrid(cxt
))
1032 "A hybrid GPT was detected. You have to sync "
1033 "the hybrid MBR manually (expert command 'M')."));
1035 init_fields(cxt
, outarg
, NULL
); /* -o <columns> */
1038 process_fdisk_menu(&cxt
);
1041 fdisk_unref_context(cxt
);
1042 return EXIT_SUCCESS
;