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 # include <readline/readline.h>
36 #include "pathnames.h"
37 #include "canonicalize.h"
39 #include "closestream.h"
44 #include "pt-sun.h" /* to toggle flags */
46 #ifdef HAVE_LINUX_COMPILER_H
47 # include <linux/compiler.h>
49 #ifdef HAVE_LINUX_BLKPG_H
50 # include <linux/blkpg.h>
53 int pwipemode
= WIPEMODE_AUTO
;
56 * fdisk debug stuff (see fdisk.h and include/debug.h)
58 UL_DEBUG_DEFINE_MASK(fdisk
);
59 UL_DEBUG_DEFINE_MASKNAMES(fdisk
) = UL_DEBUG_EMPTY_MASKNAMES
;
61 static void fdiskprog_init_debug(void)
63 __UL_INIT_DEBUG(fdisk
, FDISKPROG_DEBUG_
, 0, FDISK_DEBUG
);
66 #ifdef HAVE_LIBREADLINE
67 static char *rl_fgets(char *s
, int n
, FILE *stream
, const char *prompt
)
71 rl_outstream
= stream
;
83 int get_user_reply(struct fdisk_context
*cxt
, const char *prompt
,
84 char *buf
, size_t bufsz
)
90 #ifdef HAVE_LIBREADLINE
91 if (isatty(STDIN_FILENO
)) {
92 if (!rl_fgets(buf
, bufsz
, stdout
, prompt
)) {
93 if (fdisk_label_is_changed(fdisk_get_label(cxt
, NULL
))) {
94 if (rl_fgets(buf
, bufsz
, stderr
,
95 _("\nDo you really want to quit? "))
99 fdisk_unref_context(cxt
);
107 fputs(prompt
, stdout
);
109 if (!fgets(buf
, bufsz
, stdin
)) {
110 if (fdisk_label_is_changed(fdisk_get_label(cxt
, NULL
))) {
111 fprintf(stderr
, _("\nDo you really want to quit? "));
113 if (fgets(buf
, bufsz
, stdin
) && !rpmatch(buf
))
116 fdisk_unref_context(cxt
);
123 for (p
= buf
; *p
&& !isgraph(*p
); p
++); /* get first non-blank */
126 memmove(buf
, p
, p
- buf
); /* remove blank space */
128 if (sz
&& *(buf
+ sz
- 1) == '\n')
129 *(buf
+ sz
- 1) = '\0';
131 DBG(ASK
, ul_debug("user's reply: >>>%s<<<", buf
));
135 static int ask_menu(struct fdisk_context
*cxt
, struct fdisk_ask
*ask
,
136 char *buf
, size_t bufsz
)
139 const char *q
= fdisk_ask_get_query(ask
);
140 int dft
= fdisk_ask_menu_get_default(ask
);
143 fputs(q
, stdout
); /* print header */
150 const char *name
, *desc
;
153 /* print menu items */
154 while (fdisk_ask_menu_get_item(ask
, i
++, &key
, &name
, &desc
) == 0)
155 fprintf(stdout
, " %c %s (%s)\n", key
, name
, desc
);
158 snprintf(prompt
, sizeof(prompt
), _("Select (default %c): "), dft
);
159 rc
= get_user_reply(cxt
, prompt
, buf
, bufsz
);
163 fdisk_info(cxt
, _("Using default response %c."), dft
);
170 while (fdisk_ask_menu_get_item(ask
, i
++, &key
, NULL
, NULL
) == 0) {
172 fdisk_ask_menu_set_result(ask
, c
);
173 return 0; /* success */
176 fdisk_warnx(cxt
, _("Value out of range."));
183 #define tochar(num) ((int) ('a' + num - 1))
184 static int ask_number(struct fdisk_context
*cxt
,
185 struct fdisk_ask
*ask
,
186 char *buf
, size_t bufsz
)
188 char prompt
[128] = { '\0' };
189 const char *q
= fdisk_ask_get_query(ask
);
190 const char *range
= fdisk_ask_number_get_range(ask
);
192 uint64_t dflt
= fdisk_ask_number_get_default(ask
),
193 low
= fdisk_ask_number_get_low(ask
),
194 high
= fdisk_ask_number_get_high(ask
);
195 int inchar
= fdisk_ask_number_inchars(ask
);
199 DBG(ASK
, ul_debug("asking for number "
200 "['%s', <%"PRIu64
",%"PRIu64
">, default=%"PRIu64
", range: %s]",
201 q
, low
, high
, dflt
, range
));
203 if (range
&& dflt
>= low
&& dflt
<= high
) {
205 snprintf(prompt
, sizeof(prompt
), _("%s (%s, default %c): "),
206 q
, range
, tochar(dflt
));
208 snprintf(prompt
, sizeof(prompt
), _("%s (%s, default %"PRIu64
"): "),
211 } else if (dflt
>= low
&& dflt
<= high
) {
213 snprintf(prompt
, sizeof(prompt
), _("%s (%c-%c, default %c): "),
214 q
, tochar(low
), tochar(high
), tochar(dflt
));
216 snprintf(prompt
, sizeof(prompt
),
217 _("%s (%"PRIu64
"-%"PRIu64
", default %"PRIu64
"): "),
220 snprintf(prompt
, sizeof(prompt
), _("%s (%c-%c): "),
221 q
, tochar(low
), tochar(high
));
223 snprintf(prompt
, sizeof(prompt
), _("%s (%"PRIu64
"-%"PRIu64
"): "),
227 int rc
= get_user_reply(cxt
, prompt
, buf
, bufsz
);
232 if (!*buf
&& dflt
>= low
&& dflt
<= high
)
233 return fdisk_ask_number_set_result(ask
, dflt
);
235 if (isdigit_string(buf
)) {
239 num
= strtoumax(buf
, &end
, 10);
240 if (errno
|| buf
== end
|| (end
&& *end
))
242 } else if (inchar
&& isalpha(*buf
)) {
243 num
= tolower(*buf
) - 'a' + 1;
247 if (rc
== 0 && num
>= low
&& num
<= high
)
248 return fdisk_ask_number_set_result(ask
, num
);
250 fdisk_warnx(cxt
, _("Value out of range."));
256 static int ask_offset(struct fdisk_context
*cxt
,
257 struct fdisk_ask
*ask
,
258 char *buf
, size_t bufsz
)
260 char prompt
[128] = { '\0' };
261 const char *q
= fdisk_ask_get_query(ask
);
262 const char *range
= fdisk_ask_number_get_range(ask
);
264 uint64_t dflt
= fdisk_ask_number_get_default(ask
),
265 low
= fdisk_ask_number_get_low(ask
),
266 high
= fdisk_ask_number_get_high(ask
),
267 base
= fdisk_ask_number_get_base(ask
);
271 DBG(ASK
, ul_debug("asking for offset ['%s', <%"PRIu64
",%"PRIu64
">, base=%"PRIu64
", default=%"PRIu64
", range: %s]",
272 q
, low
, high
, base
, dflt
, range
));
274 if (range
&& dflt
>= low
&& dflt
<= high
)
275 snprintf(prompt
, sizeof(prompt
), _("%s (%s, default %"PRIu64
"): "),
277 else if (dflt
>= low
&& dflt
<= high
)
278 snprintf(prompt
, sizeof(prompt
),
279 _("%s (%"PRIu64
"-%"PRIu64
", default %"PRIu64
"): "),
282 snprintf(prompt
, sizeof(prompt
), _("%s (%"PRIu64
"-%"PRIu64
"): "),
290 int rc
= get_user_reply(cxt
, prompt
, buf
, bufsz
);
293 if (!*buf
&& dflt
>= low
&& dflt
<= high
)
294 return fdisk_ask_number_set_result(ask
, dflt
);
297 if (*p
== '+' || *p
== '-') {
302 rc
= parse_size(p
, &num
, &pwr
);
305 DBG(ASK
, ul_debug("parsed size: %ju", num
));
307 /* +{size}{K,M,...} specified, the "num" is in bytes */
308 uint64_t unit
= fdisk_ask_number_get_unit(ask
);
309 num
+= unit
/2; /* round */
317 DBG(ASK
, ul_debug("final offset: %ju [sig: %c, power: %d, %s]",
319 sig
? "relative" : "absolute"));
320 if (num
>= low
&& num
<= high
) {
322 fdisk_ask_number_set_relative(ask
, 1);
323 return fdisk_ask_number_set_result(ask
, (uint64_t)num
);
325 fdisk_warnx(cxt
, _("Value out of range."));
331 static unsigned int info_count
;
333 static void fputs_info(struct fdisk_ask
*ask
, FILE *out
)
338 msg
= fdisk_ask_print_get_mesg(ask
);
348 int ask_callback(struct fdisk_context
*cxt
, struct fdisk_ask
*ask
,
349 void *data
__attribute__((__unused__
)))
357 if (fdisk_ask_get_type(ask
) != FDISK_ASKTYPE_INFO
)
360 switch(fdisk_ask_get_type(ask
)) {
361 case FDISK_ASKTYPE_MENU
:
362 return ask_menu(cxt
, ask
, buf
, sizeof(buf
));
363 case FDISK_ASKTYPE_NUMBER
:
364 return ask_number(cxt
, ask
, buf
, sizeof(buf
));
365 case FDISK_ASKTYPE_OFFSET
:
366 return ask_offset(cxt
, ask
, buf
, sizeof(buf
));
367 case FDISK_ASKTYPE_INFO
:
368 if (!fdisk_is_listonly(cxt
))
370 fputs_info(ask
, stdout
);
372 case FDISK_ASKTYPE_WARNX
:
374 color_scheme_fenable("warn", UL_COLOR_RED
, stderr
);
375 fputs(fdisk_ask_print_get_mesg(ask
), stderr
);
376 color_fdisable(stderr
);
379 case FDISK_ASKTYPE_WARN
:
381 color_scheme_fenable("warn", UL_COLOR_RED
, stderr
);
382 fputs(fdisk_ask_print_get_mesg(ask
), stderr
);
383 errno
= fdisk_ask_print_get_errno(ask
);
384 fprintf(stderr
, ": %m\n");
385 color_fdisable(stderr
);
387 case FDISK_ASKTYPE_YESNO
:
391 fputs(fdisk_ask_get_query(ask
), stdout
);
392 rc
= get_user_reply(cxt
, _(" [Y]es/[N]o: "), buf
, sizeof(buf
));
396 if (x
== RPMATCH_YES
|| x
== RPMATCH_NO
) {
397 fdisk_ask_yesno_set_result(ask
, x
);
401 DBG(ASK
, ul_debug("yes-no ask: reply '%s' [rc=%d]", buf
, rc
));
403 case FDISK_ASKTYPE_STRING
:
406 snprintf(prmt
, sizeof(prmt
), "%s: ", fdisk_ask_get_query(ask
));
408 rc
= get_user_reply(cxt
, prmt
, buf
, sizeof(buf
));
410 fdisk_ask_string_set_result(ask
, xstrdup(buf
));
411 DBG(ASK
, ul_debug("string ask: reply '%s' [rc=%d]", buf
, rc
));
415 warnx(_("internal error: unsupported dialog type %d"), fdisk_ask_get_type(ask
));
421 static struct fdisk_parttype
*ask_partition_type(struct fdisk_context
*cxt
)
424 struct fdisk_label
*lb
;
427 lb
= fdisk_get_label(cxt
, NULL
);
432 q
= fdisk_label_has_code_parttypes(lb
) ?
433 _("Partition type (type L to list all types): ") :
434 _("Hex code (type L to list all codes): ");
437 int rc
= get_user_reply(cxt
, q
, buf
, sizeof(buf
));
442 if (buf
[1] == '\0' && toupper(*buf
) == 'L')
443 list_partition_types(cxt
);
445 return fdisk_label_parse_parttype(lb
, buf
);
452 void list_partition_types(struct fdisk_context
*cxt
)
455 struct fdisk_label
*lb
;
458 lb
= fdisk_get_label(cxt
, NULL
);
461 ntypes
= fdisk_label_get_nparttypes(lb
);
465 if (fdisk_label_has_code_parttypes(lb
)) {
467 * Prints in 4 columns in format <hex> <name>
469 size_t last
[4], done
= 0, next
= 0, size
;
474 for (i
= 3; i
>= 0; i
--)
475 last
[3 - i
] = done
+= (size
+ i
- done
) / (i
+ 1);
479 #define NAME_WIDTH 15
480 char name
[NAME_WIDTH
* MB_LEN_MAX
];
481 size_t width
= NAME_WIDTH
;
482 const struct fdisk_parttype
*t
= fdisk_label_get_parttype(lb
, next
);
485 if (fdisk_parttype_get_name(t
)) {
486 printf("%c%2x ", i
? ' ' : '\n',
487 fdisk_parttype_get_code(t
));
488 ret
= mbsalign(_(fdisk_parttype_get_name(t
)),
490 &width
, MBS_ALIGN_LEFT
, 0);
492 if (ret
== (size_t)-1 || ret
>= sizeof(name
))
494 _(fdisk_parttype_get_name(t
)));
499 next
= last
[i
++] + done
;
500 if (i
> 3 || next
>= last
[i
]) {
504 } while (done
< last
[0]);
508 * Prints 1 column in format <idx> <name> <typestr>
514 for (i
= 0; i
< ntypes
; i
++) {
515 const struct fdisk_parttype
*t
= fdisk_label_get_parttype(lb
, i
);
516 printf("%3zu %-30s %s\n", i
+ 1,
517 fdisk_parttype_get_name(t
),
518 fdisk_parttype_get_string(t
));
526 void toggle_dos_compatibility_flag(struct fdisk_context
*cxt
)
528 struct fdisk_label
*lb
= fdisk_get_label(cxt
, "dos");
534 flag
= !fdisk_dos_is_compatible(lb
);
535 fdisk_info(cxt
, flag
?
536 _("DOS Compatibility flag is set (DEPRECATED!)") :
537 _("DOS Compatibility flag is not set"));
539 fdisk_dos_enable_compatible(lb
, flag
);
541 if (fdisk_is_label(cxt
, DOS
))
542 fdisk_reset_alignment(cxt
); /* reset the current label */
545 void change_partition_type(struct fdisk_context
*cxt
)
548 struct fdisk_parttype
*t
= NULL
;
549 struct fdisk_partition
*pa
= NULL
;
550 const char *old
= NULL
;
554 if (fdisk_ask_partnum(cxt
, &i
, FALSE
))
557 if (fdisk_get_partition(cxt
, i
, &pa
)) {
558 fdisk_warnx(cxt
, _("Partition %zu does not exist yet!"), i
+ 1);
562 t
= (struct fdisk_parttype
*) fdisk_partition_get_type(pa
);
563 old
= t
? fdisk_parttype_get_name(t
) : _("Unknown");
566 t
= ask_partition_type(cxt
);
569 if (fdisk_set_partition_type(cxt
, i
, t
) == 0)
571 _("Changed type of partition '%s' to '%s'."),
572 old
, t
? fdisk_parttype_get_name(t
) : _("Unknown"));
575 _("Type of partition %zu is unchanged: %s."),
578 fdisk_unref_partition(pa
);
579 fdisk_unref_parttype(t
);
582 int print_partition_info(struct fdisk_context
*cxt
)
584 struct fdisk_partition
*pa
= NULL
;
588 struct fdisk_label
*lb
= fdisk_get_label(cxt
, NULL
);
590 if ((rc
= fdisk_ask_partnum(cxt
, &i
, FALSE
)))
593 if ((rc
= fdisk_get_partition(cxt
, i
, &pa
))) {
594 fdisk_warnx(cxt
, _("Partition %zu does not exist yet!"), i
+ 1);
598 if ((rc
= fdisk_label_get_fields_ids_all(lb
, cxt
, &fields
, &nfields
)))
601 for (i
= 0; i
< nfields
; ++i
) {
604 const struct fdisk_field
*fd
= fdisk_label_get_field(lb
, id
);
609 rc
= fdisk_partition_to_string(pa
, cxt
, id
, &data
);
614 fdisk_info(cxt
, _("%15s: %s"), fdisk_field_get_name(fd
), data
);
619 fdisk_unref_partition(pa
);
624 static size_t skip_empty(const unsigned char *buf
, size_t i
, size_t sz
)
627 const unsigned char *p0
= buf
+ i
;
629 for (next
= i
+ 16; next
< sz
; next
+= 16) {
630 if (memcmp(p0
, buf
+ next
, 16) != 0)
634 return next
== i
+ 16 ? i
: next
;
637 static void dump_buffer(off_t base
, unsigned char *buf
, size_t sz
, int all
)
639 size_t i
, l
, next
= 0;
643 for (i
= 0, l
= 0; i
< sz
; i
++, l
++) {
645 if (all
== 0 && !next
)
646 next
= skip_empty(buf
, i
, sz
);
647 printf("%08jx ", (intmax_t)base
+ i
);
649 printf(" %02x", buf
[i
]);
650 if (l
== 7) /* words separator */
653 fputc('\n', stdout
); /* next line */
666 static void dump_blkdev(struct fdisk_context
*cxt
, const char *name
,
667 uint64_t offset
, size_t size
, int all
)
669 int fd
= fdisk_get_devfd(cxt
);
671 fdisk_info(cxt
, _("\n%s: offset = %"PRIu64
", size = %zu bytes."),
676 if (lseek(fd
, (off_t
) offset
, SEEK_SET
) == (off_t
) -1)
677 fdisk_warn(cxt
, _("cannot seek"));
679 unsigned char *buf
= xmalloc(size
);
681 if (read_all(fd
, (char *) buf
, size
) != (ssize_t
) size
)
682 fdisk_warn(cxt
, _("cannot read"));
684 dump_buffer(offset
, buf
, size
, all
);
689 void dump_firstsector(struct fdisk_context
*cxt
)
691 int all
= !isatty(STDOUT_FILENO
);
695 dump_blkdev(cxt
, _("First sector"), 0, fdisk_get_sector_size(cxt
), all
);
698 void dump_disklabel(struct fdisk_context
*cxt
)
700 int all
= !isatty(STDOUT_FILENO
);
702 const char *name
= NULL
;
708 while (fdisk_locate_disklabel(cxt
, i
++, &name
, &offset
, &size
) == 0 && size
)
709 dump_blkdev(cxt
, name
, offset
, size
, all
);
712 static fdisk_sector_t
get_dev_blocks(char *dev
)
717 if ((fd
= open(dev
, O_RDONLY
)) < 0)
718 err(EXIT_FAILURE
, _("cannot open %s"), dev
);
719 ret
= blkdev_get_sectors(fd
, (unsigned long long *) &size
);
722 err(EXIT_FAILURE
, _("BLKGETSIZE ioctl failed on %s"), dev
);
726 static void __attribute__ ((__noreturn__
)) usage(FILE *out
)
728 fputs(USAGE_HEADER
, out
);
731 _(" %1$s [options] <disk> change partition table\n"
732 " %1$s [options] -l [<disk>] list partition table(s)\n"),
733 program_invocation_short_name
);
735 fputs(USAGE_SEPARATOR
, out
);
736 fputs(_("Display or manipulate a disk partition table.\n"), out
);
738 fputs(USAGE_OPTIONS
, out
);
739 fputs(_(" -b, --sector-size <size> physical and logical sector size\n"), out
);
740 fputs(_(" -B, --protect-boot don't erase bootbits when creating a new label\n"), out
);
741 fputs(_(" -c, --compatibility[=<mode>] mode is 'dos' or 'nondos' (default)\n"), out
);
742 fputs(_(" -L, --color[=<when>] colorize output (auto, always or never)\n"), out
);
744 " %s\n", USAGE_COLORS_DEFAULT
);
745 fputs(_(" -l, --list display partitions and exit\n"), out
);
746 fputs(_(" -o, --output <list> output columns\n"), out
);
747 fputs(_(" -t, --type <type> recognize specified partition table type only\n"), out
);
748 fputs(_(" -u, --units[=<unit>] display units: 'cylinders' or 'sectors' (default)\n"), out
);
749 fputs(_(" -s, --getsz display device size in 512-byte sectors [DEPRECATED]\n"), out
);
750 fputs(_(" --bytes print SIZE in bytes rather than in human readable format\n"), out
);
751 fputs(_(" -w, --wipe <mode> wipe signatures (auto, always or never)\n"), out
);
752 fputs(_(" -W, --wipe-partitions <mode> wipe signatures from new partitions (auto, always or never)\n"), out
);
754 fputs(USAGE_SEPARATOR
, out
);
755 fputs(_(" -C, --cylinders <number> specify the number of cylinders\n"), out
);
756 fputs(_(" -H, --heads <number> specify the number of heads\n"), out
);
757 fputs(_(" -S, --sectors <number> specify the number of sectors per track\n"), out
);
759 fputs(USAGE_SEPARATOR
, out
);
760 fputs(USAGE_HELP
, out
);
761 fputs(USAGE_VERSION
, out
);
763 list_available_columns(out
);
765 fprintf(out
, USAGE_MAN_TAIL("fdisk(8)"));
766 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
771 ACT_FDISK
= 0, /* default */
776 int main(int argc
, char **argv
)
778 int rc
, i
, c
, act
= ACT_FDISK
;
779 int colormode
= UL_COLORMODE_UNDEF
;
780 int wipemode
= WIPEMODE_AUTO
;
781 struct fdisk_context
*cxt
;
784 OPT_BYTES
= CHAR_MAX
+ 1
786 static const struct option longopts
[] = {
787 { "bytes", no_argument
, NULL
, OPT_BYTES
},
788 { "color", optional_argument
, NULL
, 'L' },
789 { "compatibility", optional_argument
, NULL
, 'c' },
790 { "cylinders", required_argument
, NULL
, 'C' },
791 { "heads", required_argument
, NULL
, 'H' },
792 { "sectors", required_argument
, NULL
, 'S' },
793 { "getsz", no_argument
, NULL
, 's' },
794 { "help", no_argument
, NULL
, 'h' },
795 { "list", no_argument
, NULL
, 'l' },
796 { "sector-size", required_argument
, NULL
, 'b' },
797 { "type", required_argument
, NULL
, 't' },
798 { "units", optional_argument
, NULL
, 'u' },
799 { "version", no_argument
, NULL
, 'V' },
800 { "output", no_argument
, NULL
, 'o' },
801 { "protect-boot", no_argument
, NULL
, 'B' },
802 { "wipe", required_argument
, NULL
, 'w' },
803 { "wipe-partitions",required_argument
, NULL
, 'W' },
807 setlocale(LC_ALL
, "");
808 bindtextdomain(PACKAGE
, LOCALEDIR
);
810 atexit(close_stdout
);
814 fdiskprog_init_debug();
816 cxt
= fdisk_new_context();
818 err(EXIT_FAILURE
, _("failed to allocate libfdisk context"));
820 fdisk_set_ask(cxt
, ask_callback
, NULL
);
822 while ((c
= getopt_long(argc
, argv
, "b:Bc::C:hH:lL::o:sS:t:u::vVw:W:",
823 longopts
, NULL
)) != -1) {
827 size_t sz
= strtou32_or_err(optarg
,
828 _("invalid sector size argument"));
829 if (sz
!= 512 && sz
!= 1024 && sz
!= 2048 && sz
!= 4096)
831 fdisk_save_user_sector_size(cxt
, sz
, sz
);
835 fdisk_enable_bootbits_protection(cxt
, 1);
838 fdisk_save_user_geometry(cxt
,
839 strtou32_or_err(optarg
,
840 _("invalid cylinders argument")),
845 /* this setting is independent on the current
846 * actively used label
848 char *p
= *optarg
== '=' ? optarg
+ 1 : optarg
;
849 struct fdisk_label
*lb
= fdisk_get_label(cxt
, "dos");
852 err(EXIT_FAILURE
, _("not found DOS label driver"));
853 if (strcmp(p
, "dos") == 0)
854 fdisk_dos_enable_compatible(lb
, TRUE
);
855 else if (strcmp(p
, "nondos") == 0)
856 fdisk_dos_enable_compatible(lb
, FALSE
);
858 warnx(_("unknown compatibility mode '%s'"), p
);
862 /* use default if no optarg specified */
865 fdisk_save_user_geometry(cxt
, 0,
866 strtou32_or_err(optarg
,
867 _("invalid heads argument")),
871 fdisk_save_user_geometry(cxt
, 0, 0,
872 strtou32_or_err(optarg
,
873 _("invalid sectors argument")));
879 colormode
= UL_COLORMODE_AUTO
;
881 colormode
= colormode_or_err(optarg
,
882 _("unsupported color mode"));
892 struct fdisk_label
*lb
= NULL
;
894 while (fdisk_next_label(cxt
, &lb
) == 0)
895 fdisk_label_set_disabled(lb
, 1);
897 lb
= fdisk_get_label(cxt
, optarg
);
899 errx(EXIT_FAILURE
, _("unsupported disklabel: %s"), optarg
);
900 fdisk_label_set_disabled(lb
, 0);
904 if (optarg
&& *optarg
== '=')
906 if (fdisk_set_unit(cxt
, optarg
) != 0)
909 case 'V': /* preferred for util-linux */
910 case 'v': /* for backward compatibility only */
911 printf(UTIL_LINUX_VERSION
);
914 wipemode
= wipemode_from_string(optarg
);
916 errx(EXIT_FAILURE
, _("unsupported wipe mode"));
919 pwipemode
= wipemode_from_string(optarg
);
921 errx(EXIT_FAILURE
, _("unsupported wipe mode"));
926 fdisk_set_size_unit(cxt
, FDISK_SIZEUNIT_BYTES
);
929 errtryhelp(EXIT_FAILURE
);
933 if (argc
-optind
!= 1 && fdisk_has_user_device_properties(cxt
))
934 warnx(_("The device properties (sector size and geometry) should"
935 " be used with one specified device only."));
937 colors_init(colormode
, "fdisk");
941 fdisk_enable_listonly(cxt
, 1);
942 init_fields(cxt
, outarg
, NULL
);
948 for (rc
= 0, k
= optind
; k
< argc
; k
++) {
950 fputs("\n\n", stdout
);
952 rc
+= print_device_pt(cxt
, argv
[k
], 1, 0);
958 print_all_devices_pt(cxt
, 0);
963 if (argc
- optind
<= 0)
966 for (i
= optind
; i
< argc
; i
++) {
967 uintmax_t blks
= get_dev_blocks(argv
[i
]);
969 if (argc
- optind
== 1)
970 printf("%ju\n", blks
);
972 printf("%s: %ju\n", argv
[i
], blks
);
977 if (argc
-optind
!= 1)
980 /* Here starts interactive mode, use fdisk_{warn,info,..} functions */
981 color_scheme_enable("welcome", UL_COLOR_GREEN
);
982 fdisk_info(cxt
, _("Welcome to fdisk (%s)."), PACKAGE_STRING
);
984 fdisk_info(cxt
, _("Changes will remain in memory only, until you decide to write them.\n"
985 "Be careful before using the write command.\n"));
987 rc
= fdisk_assign_device(cxt
, argv
[optind
], 0);
989 rc
= fdisk_assign_device(cxt
, argv
[optind
], 1);
991 fdisk_warnx(cxt
, _("Device is open in read-only mode."));
994 err(EXIT_FAILURE
, _("cannot open %s"), argv
[optind
]);
998 if (fdisk_get_collision(cxt
)) {
999 int dowipe
= wipemode
== WIPEMODE_ALWAYS
? 1 : 0;
1001 fdisk_warnx(cxt
, _("Device %s already contains a %s signature."),
1002 argv
[optind
], fdisk_get_collision(cxt
));
1004 if (isatty(STDIN_FILENO
) && wipemode
== WIPEMODE_AUTO
)
1005 dowipe
= 1; /* do it in interactive mode */
1007 fdisk_enable_wipe(cxt
, dowipe
);
1010 "The signature will be removed by a write command."));
1013 "It is strongly recommended to wipe the device with "
1014 "wipefs(8), in order to avoid possible collisions."));
1016 if (!fdisk_has_label(cxt
)) {
1017 fdisk_info(cxt
, _("Device does not contain a recognized partition table."));
1018 fdisk_create_disklabel(cxt
, NULL
);
1020 } else if (fdisk_is_label(cxt
, GPT
) && fdisk_gpt_is_hybrid(cxt
))
1022 "A hybrid GPT was detected. You have to sync "
1023 "the hybrid MBR manually (expert command 'M')."));
1025 init_fields(cxt
, outarg
, NULL
); /* -o <columns> */
1028 process_fdisk_menu(&cxt
);
1031 fdisk_unref_context(cxt
);
1032 return EXIT_SUCCESS
;