]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/fdisk-menu.c
14 const char key
; /* command key */
15 const char *title
; /* help string */
16 unsigned int normal
: 1, /* normal mode */
17 expert
: 1, /* expert mode */
18 hidden
: 1; /* be sensitive for this key,
19 but don't print it in help */
21 enum fdisk_labeltype label
; /* only for this label */
22 enum fdisk_labeltype exclude
; /* all labels except this */
23 enum fdisk_labeltype parent
; /* for nested PT */
26 #define IS_MENU_SEP(e) ((e)->key == '-')
27 #define IS_MENU_HID(e) ((e)->hidden)
30 enum fdisk_labeltype label
; /* only for this label */
31 enum fdisk_labeltype exclude
; /* all labels except this */
33 unsigned int nonested
: 1; /* don't make this menu active in nested PT */
35 int (*callback
)(struct fdisk_context
**,
37 const struct menu_entry
*);
39 struct menu_entry entries
[]; /* NULL terminated array */
43 size_t menu_idx
; /* the current menu */
44 size_t entry_idx
; /* index with in the current menu */
47 #define MENU_CXT_EMPTY { 0, 0 }
48 #define DECLARE_MENU_CB(x) \
49 static int x(struct fdisk_context **, \
50 const struct menu *, \
51 const struct menu_entry *)
53 DECLARE_MENU_CB(gpt_menu_cb
);
54 DECLARE_MENU_CB(sun_menu_cb
);
55 DECLARE_MENU_CB(sgi_menu_cb
);
56 DECLARE_MENU_CB(geo_menu_cb
);
57 DECLARE_MENU_CB(dos_menu_cb
);
58 DECLARE_MENU_CB(bsd_menu_cb
);
59 DECLARE_MENU_CB(createlabel_menu_cb
);
60 DECLARE_MENU_CB(generic_menu_cb
);
64 * MENU_X* expert mode only
65 * MENU_B* both -- expert + normal mode
67 * *_E exclude this label
69 * *_L only for this label
73 #define MENU_SEP(t) { .title = t, .key = '-', .normal = 1 }
74 #define MENU_XSEP(t) { .title = t, .key = '-', .expert = 1 }
75 #define MENU_BSEP(t) { .title = t, .key = '-', .expert = 1, .normal = 1 }
78 #define MENU_ENT(k, t) { .title = t, .key = k, .normal = 1 }
79 #define MENU_ENT_E(k, t, l) { .title = t, .key = k, .normal = 1, .exclude = l }
80 #define MENU_ENT_L(k, t, l) { .title = t, .key = k, .normal = 1, .label = l }
82 #define MENU_XENT(k, t) { .title = t, .key = k, .expert = 1 }
83 #define MENU_XENT_H(k, t) { .title = t, .key = k, .expert = 1, .hidden = 1 }
85 #define MENU_BENT(k, t) { .title = t, .key = k, .expert = 1, .normal = 1 }
86 #define MENU_BENT_E(k, t, l) { .title = t, .key = k, .expert = 1, .normal = 1, .exclude = l }
88 #define MENU_ENT_NEST(k, t, l, p) { .title = t, .key = k, .normal = 1, .label = l, .parent = p }
89 #define MENU_XENT_NEST(k, t, l, p) { .title = t, .key = k, .expert = 1, .label = l, .parent = p }
92 struct menu menu_generic
= {
93 .callback
= generic_menu_cb
,
95 MENU_BSEP(N_("Generic")),
96 MENU_ENT ('d', N_("delete a partition")),
97 MENU_ENT ('l', N_("list known partition types")),
98 MENU_ENT ('n', N_("add a new partition")),
99 MENU_BENT ('p', N_("print the partition table")),
100 MENU_ENT ('t', N_("change a partition type")),
101 MENU_BENT_E('v', N_("verify the partition table"), FDISK_DISKLABEL_BSD
),
103 MENU_XENT('d', N_("print the raw data of the first sector from the device")),
104 MENU_XENT('D', N_("print the raw data of the disklabel from the device")),
105 MENU_XENT('f', N_("fix partitions order")),
107 MENU_SEP(N_("Misc")),
108 MENU_BENT ('m', N_("print this menu")),
109 MENU_ENT_E('u', N_("change display/entry units"), FDISK_DISKLABEL_GPT
),
110 MENU_ENT_E('x', N_("extra functionality (experts only)"), FDISK_DISKLABEL_BSD
),
112 MENU_BSEP(N_("Save & Exit")),
113 MENU_ENT_E('w', N_("write table to disk and exit"), FDISK_DISKLABEL_BSD
),
114 MENU_ENT_L('w', N_("write table to disk"), FDISK_DISKLABEL_BSD
),
115 MENU_BENT ('q', N_("quit without saving changes")),
116 MENU_XENT ('r', N_("return to main menu")),
118 MENU_ENT_NEST('r', N_("return from BSD to DOS"), FDISK_DISKLABEL_BSD
, FDISK_DISKLABEL_DOS
),
124 struct menu menu_createlabel
= {
125 .callback
= createlabel_menu_cb
,
126 .exclude
= FDISK_DISKLABEL_BSD
,
129 MENU_SEP(N_("Create a new label")),
130 MENU_ENT('g', N_("create a new empty GPT partition table")),
131 MENU_ENT('G', N_("create a new empty SGI (IRIX) partition table")),
132 MENU_ENT('o', N_("create a new empty DOS partition table")),
133 MENU_ENT('s', N_("create a new empty Sun partition table")),
135 /* backward compatibility -- be sensitive to 'g', but don't
136 * print it in the expert menu */
137 MENU_XENT_H('g', N_("create an IRIX (SGI) partition table")),
142 struct menu menu_geo
= {
143 .callback
= geo_menu_cb
,
144 .exclude
= FDISK_DISKLABEL_GPT
| FDISK_DISKLABEL_BSD
,
146 MENU_XSEP(N_("Geometry")),
147 MENU_XENT('c', N_("change number of cylinders")),
148 MENU_XENT('h', N_("change number of heads")),
149 MENU_XENT('s', N_("change number of sectors/track")),
154 struct menu menu_gpt
= {
155 .callback
= gpt_menu_cb
,
156 .label
= FDISK_DISKLABEL_GPT
,
158 MENU_XSEP(N_("GPT")),
159 MENU_XENT('i', N_("change disk GUID")),
160 MENU_XENT('n', N_("change partition name")),
161 MENU_XENT('u', N_("change partition UUID")),
162 MENU_XENT('M', N_("enter protective/hybrid MBR")),
165 MENU_XENT('A', N_("toggle the legacy BIOS bootable flag")),
166 MENU_XENT('B', N_("toggle the no block IO protocol flag")),
167 MENU_XENT('R', N_("toggle the required partition flag")),
168 MENU_XENT('S', N_("toggle the GUID specific bits")),
174 struct menu menu_sun
= {
175 .callback
= sun_menu_cb
,
176 .label
= FDISK_DISKLABEL_SUN
,
178 MENU_BSEP(N_("Sun")),
179 MENU_ENT('a', N_("toggle the read-only flag")),
180 MENU_ENT('c', N_("toggle the mountable flag")),
182 MENU_XENT('a', N_("change number of alternate cylinders")),
183 MENU_XENT('e', N_("change number of extra sectors per cylinder")),
184 MENU_XENT('i', N_("change interleave factor")),
185 MENU_XENT('o', N_("change rotation speed (rpm)")),
186 MENU_XENT('y', N_("change number of physical cylinders")),
191 struct menu menu_sgi
= {
192 .callback
= sgi_menu_cb
,
193 .label
= FDISK_DISKLABEL_SGI
,
196 MENU_ENT('a', N_("select bootable partition")),
197 MENU_ENT('b', N_("edit bootfile entry")),
198 MENU_ENT('c', N_("select sgi swap partition")),
199 MENU_ENT('i', N_("create SGI info")),
204 struct menu menu_dos
= {
205 .callback
= dos_menu_cb
,
206 .label
= FDISK_DISKLABEL_DOS
,
208 MENU_BSEP(N_("DOS (MBR)")),
209 MENU_ENT('a', N_("toggle a bootable flag")),
210 MENU_ENT('b', N_("edit nested BSD disklabel")),
211 MENU_ENT('c', N_("toggle the dos compatibility flag")),
213 MENU_XENT('b', N_("move beginning of data in a partition")),
214 MENU_XENT('i', N_("change the disk identifier")),
216 MENU_XENT_NEST('M', N_("return from protective/hybrid MBR to GPT"),
217 FDISK_DISKLABEL_DOS
, FDISK_DISKLABEL_GPT
),
222 struct menu menu_bsd
= {
223 .callback
= bsd_menu_cb
,
224 .label
= FDISK_DISKLABEL_BSD
,
227 MENU_ENT('e', N_("edit drive data")),
228 MENU_ENT('i', N_("install bootstrap")),
229 MENU_ENT('s', N_("show complete disklabel")),
230 MENU_ENT('x', N_("link BSD partition to non-BSD partition")),
235 static const struct menu
*menus
[] = {
246 static const struct menu_entry
*next_menu_entry(
247 struct fdisk_context
*cxt
,
248 struct menu_context
*mc
)
250 struct fdisk_label
*lb
= fdisk_get_label(cxt
, NULL
);
251 struct fdisk_context
*parent
= fdisk_get_parent(cxt
);
252 unsigned int type
= 0, pr_type
= 0;
256 type
= fdisk_label_get_type(lb
);
258 pr_type
= fdisk_label_get_type(fdisk_get_label(parent
, NULL
));
260 while (mc
->menu_idx
< ARRAY_SIZE(menus
)) {
261 const struct menu
*m
= menus
[mc
->menu_idx
];
262 const struct menu_entry
*e
= &(m
->entries
[mc
->entry_idx
]);
268 /* no more entries */
269 if (e
->title
== NULL
||
270 /* menu wanted for specified labels only */
271 (m
->label
&& lb
&& !(m
->label
& type
)) ||
272 /* unwanted for nested PT */
273 (m
->nonested
&& parent
) ||
274 /* menu excluded for specified labels */
275 (m
->exclude
&& lb
&& (m
->exclude
& type
))) {
285 /* excluded for the current label */
286 if ((e
->exclude
&& lb
&& e
->exclude
& type
) ||
287 /* entry wanted for specified labels only */
288 (e
->label
&& lb
&& !(e
->label
& type
)) ||
289 /* exclude non-expert entries in expect mode */
290 (e
->expert
== 0 && fdisk_is_details(cxt
)) ||
292 (e
->parent
&& (!parent
|| pr_type
!= e
->parent
)) ||
293 /* exclude non-normal entries in normal mode */
294 (e
->normal
== 0 && !fdisk_is_details(cxt
))) {
305 /* returns @menu and menu entry for then @key */
306 static const struct menu_entry
*get_fdisk_menu_entry(
307 struct fdisk_context
*cxt
,
309 const struct menu
**menu
)
311 struct menu_context mc
= MENU_CXT_EMPTY
;
312 const struct menu_entry
*e
;
314 while ((e
= next_menu_entry(cxt
, &mc
))) {
315 if (IS_MENU_SEP(e
) || e
->key
!= key
)
319 *menu
= menus
[mc
.menu_idx
];
326 static int menu_detect_collisions(struct fdisk_context
*cxt
)
328 struct menu_context mc
= MENU_CXT_EMPTY
;
329 const struct menu_entry
*e
, *r
;
331 while ((e
= next_menu_entry(cxt
, &mc
))) {
335 r
= get_fdisk_menu_entry(cxt
, e
->key
, NULL
);
337 DBG(MENU
, ul_debug("warning: not found "
338 "entry for %c", e
->key
));
342 DBG(MENU
, ul_debug("warning: duplicate key '%c'",
344 DBG(MENU
, ul_debug(" : %s", e
->title
));
345 DBG(MENU
, ul_debug(" : %s", r
->title
));
353 static int print_fdisk_menu(struct fdisk_context
*cxt
)
355 struct menu_context mc
= MENU_CXT_EMPTY
;
356 const struct menu_entry
*e
;
358 ON_DBG(MENU
, menu_detect_collisions(cxt
));
360 if (fdisk_is_details(cxt
))
361 printf(_("\nHelp (expert commands):\n"));
363 printf(_("\nHelp:\n"));
365 while ((e
= next_menu_entry(cxt
, &mc
))) {
367 continue; /* hidden entry */
368 if (IS_MENU_SEP(e
) && (!e
->title
|| !*e
->title
))
370 else if (IS_MENU_SEP(e
)) {
371 color_scheme_enable("help-title", UL_COLOR_BOLD
);
372 printf("\n %s\n", _(e
->title
));
375 printf(" %c %s\n", e
->key
, _(e
->title
));
379 if (fdisk_get_parent(cxt
)) {
380 struct fdisk_label
*l
= fdisk_get_label(cxt
, NULL
),
381 *p
= fdisk_get_label(fdisk_get_parent(cxt
), NULL
);
383 fdisk_info(cxt
, _("You're editing nested '%s' partition table, "
384 "primary partition table is '%s'."),
385 fdisk_label_get_name(l
),
386 fdisk_label_get_name(p
));
392 /* Asks for command, verify the key and perform the command or
393 * returns the command key if no callback for the command is
396 * Note that this function might exchange the context pointer to
397 * switch to another (nested) context.
399 * Returns: <0 on error
400 * 0 on success (the command performed)
401 * >0 if no callback (then returns the key)
403 int process_fdisk_menu(struct fdisk_context
**cxt0
)
405 struct fdisk_context
*cxt
= *cxt0
;
406 const struct menu_entry
*ent
;
407 const struct menu
*menu
;
412 if (fdisk_is_details(cxt
))
413 prompt
= _("Expert command (m for help): ");
415 prompt
= _("Command (m for help): ");
418 rc
= get_user_reply(cxt
, prompt
, buf
, sizeof(buf
));
423 ent
= get_fdisk_menu_entry(cxt
, key
, &menu
);
425 fdisk_warnx(cxt
, _("%c: unknown command"), key
);
430 DBG(MENU
, ul_debug("selected: key=%c, entry='%s'",
433 /* menu has implemented callback, use it */
435 rc
= menu
->callback(cxt0
, menu
, ent
);
437 DBG(MENU
, ul_debug("no callback for key '%c'", key
));
441 DBG(MENU
, ul_debug("process menu done [rc=%d]", rc
));
446 * Basic fdisk actions
448 static int generic_menu_cb(struct fdisk_context
**cxt0
,
449 const struct menu
*menu
__attribute__((__unused__
)),
450 const struct menu_entry
*ent
)
452 struct fdisk_context
*cxt
= *cxt0
;
456 /* actions shared between expert and normal mode */
459 list_disk_geometry(cxt
);
463 if (fdisk_is_readonly(cxt
)) {
464 fdisk_warnx(cxt
, _("Device open in read-only mode."));
467 rc
= fdisk_write_disklabel(cxt
);
469 err(EXIT_FAILURE
, _("failed to write disklabel"));
470 if (fdisk_get_parent(cxt
))
471 break; /* nested PT, don't leave */
472 fdisk_info(cxt
, _("The partition table has been altered."));
473 rc
= fdisk_reread_partition_table(cxt
);
475 rc
= fdisk_deassign_device(cxt
, 0);
478 fdisk_unref_context(cxt
);
480 exit(rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
482 rc
= print_fdisk_menu(cxt
);
485 rc
= fdisk_verify_disklabel(cxt
);
493 dump_firstsector(cxt
);
499 rc
= fdisk_reorder_partitions(cxt
);
502 rc
= fdisk_enable_details(cxt
, 0);
511 rc
= fdisk_ask_partnum(cxt
, &n
, FALSE
);
513 rc
= fdisk_delete_partition(cxt
, n
);
515 fdisk_warnx(cxt
, _("Could not delete partition %zu"), n
+ 1);
517 fdisk_info(cxt
, _("Partition %zu has been deleted."), n
+ 1);
520 list_partition_types(cxt
);
523 rc
= fdisk_add_partition(cxt
, NULL
, NULL
);
526 change_partition_type(cxt
);
530 fdisk_use_cylinders(cxt
) ? "sectors" :
532 if (fdisk_use_cylinders(cxt
))
533 fdisk_info(cxt
, _("Changing display/entry units to cylinders (DEPRECATED!)."));
535 fdisk_info(cxt
, _("Changing display/entry units to sectors."));
538 fdisk_enable_details(cxt
, 1);
541 /* return from nested BSD to DOS */
542 if (fdisk_get_parent(cxt
)) {
543 *cxt0
= fdisk_get_parent(cxt
);
545 fdisk_info(cxt
, _("Leaving nested disklabel."));
546 fdisk_unref_context(cxt
);
557 * This is fdisk frontend for GPT specific libfdisk functions that
558 * are not expported by generic libfdisk API.
560 static int gpt_menu_cb(struct fdisk_context
**cxt0
,
561 const struct menu
*menu
__attribute__((__unused__
)),
562 const struct menu_entry
*ent
)
564 struct fdisk_context
*cxt
= *cxt0
;
565 struct fdisk_context
*mbr
;
566 struct fdisk_partition
*pa
= NULL
;
572 assert(fdisk_is_label(cxt
, GPT
));
574 DBG(MENU
, ul_debug("enter GPT menu"));
579 return fdisk_set_disklabel_id(cxt
);
581 mbr
= fdisk_new_nested_context(cxt
, "dos");
585 fdisk_enable_details(cxt
, 1); /* keep us in expert mode */
586 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
587 _("Entering protective/hybrid MBR disklabel."));
591 /* actions where is necessary partnum */
592 rc
= fdisk_ask_partnum(cxt
, &n
, FALSE
);
598 pa
= fdisk_new_partition(); /* new template */
603 rc
= fdisk_ask_string(cxt
, _("New UUID (in 8-4-4-4-12 format)"), &str
);
605 rc
= fdisk_partition_set_uuid(pa
, str
);
607 rc
= fdisk_set_partition(cxt
, n
, pa
);
609 fdisk_unref_partition(pa
);
613 pa
= fdisk_new_partition(); /* new template */
618 rc
= fdisk_ask_string(cxt
, _("New name"), &str
);
620 rc
= fdisk_partition_set_name(pa
, str
);
622 rc
= fdisk_set_partition(cxt
, n
, pa
);
624 fdisk_unref_partition(pa
);
628 rc
= fdisk_partition_toggle_flag(cxt
, n
, GPT_FLAG_LEGACYBOOT
);
631 rc
= fdisk_partition_toggle_flag(cxt
, n
, GPT_FLAG_NOBLOCK
);
634 rc
= fdisk_partition_toggle_flag(cxt
, n
, GPT_FLAG_REQUIRED
);
637 rc
= fdisk_partition_toggle_flag(cxt
, n
, GPT_FLAG_GUIDSPECIFIC
);
647 * This is fdisk frontend for MBR specific libfdisk functions that
648 * are not expported by generic libfdisk API.
650 static int dos_menu_cb(struct fdisk_context
**cxt0
,
651 const struct menu
*menu
__attribute__((__unused__
)),
652 const struct menu_entry
*ent
)
654 struct fdisk_context
*cxt
= *cxt0
;
657 DBG(MENU
, ul_debug("enter DOS menu"));
664 rc
= fdisk_ask_partnum(cxt
, &n
, FALSE
);
666 rc
= fdisk_partition_toggle_flag(cxt
, n
, DOS_FLAG_ACTIVE
);
671 struct fdisk_context
*bsd
672 = fdisk_new_nested_context(cxt
, "bsd");
675 if (!fdisk_has_label(bsd
))
676 rc
= fdisk_create_disklabel(bsd
, "bsd");
678 fdisk_unref_context(bsd
);
681 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
682 _("Entering nested BSD disklabel."));
687 toggle_dos_compatibility_flag(cxt
);
698 rc
= fdisk_ask_partnum(cxt
, &n
, FALSE
);
700 rc
= fdisk_dos_move_begin(cxt
, n
);
704 rc
= fdisk_set_disklabel_id(cxt
);
707 /* return from nested MBR to GPT */
708 if (fdisk_get_parent(cxt
)) {
709 *cxt0
= fdisk_get_parent(cxt
);
711 fdisk_info(cxt
, _("Leaving nested disklabel."));
712 fdisk_unref_context(cxt
);
720 static int sun_menu_cb(struct fdisk_context
**cxt0
,
721 const struct menu
*menu
__attribute__((__unused__
)),
722 const struct menu_entry
*ent
)
724 struct fdisk_context
*cxt
= *cxt0
;
727 DBG(MENU
, ul_debug("enter SUN menu"));
731 assert(fdisk_is_label(cxt
, SUN
));
733 DBG(MENU
, ul_debug("enter SUN menu"));
739 rc
= fdisk_ask_partnum(cxt
, &n
, FALSE
);
744 rc
= fdisk_partition_toggle_flag(cxt
, n
, SUN_FLAG_RONLY
);
747 rc
= fdisk_partition_toggle_flag(cxt
, n
, SUN_FLAG_UNMNT
);
756 rc
= fdisk_sun_set_alt_cyl(cxt
);
759 rc
= fdisk_sun_set_xcyl(cxt
);
762 rc
= fdisk_sun_set_ilfact(cxt
);
765 rc
= fdisk_sun_set_rspeed(cxt
);
768 rc
= fdisk_sun_set_pcylcount(cxt
);
774 static int sgi_menu_cb(struct fdisk_context
**cxt0
,
775 const struct menu
*menu
__attribute__((__unused__
)),
776 const struct menu_entry
*ent
)
778 struct fdisk_context
*cxt
= *cxt0
;
782 DBG(MENU
, ul_debug("enter SGI menu"));
786 assert(fdisk_is_label(cxt
, SGI
));
793 rc
= fdisk_ask_partnum(cxt
, &n
, FALSE
);
795 rc
= fdisk_partition_toggle_flag(cxt
, n
, SGI_FLAG_BOOT
);
798 fdisk_sgi_set_bootfile(cxt
);
801 rc
= fdisk_ask_partnum(cxt
, &n
, FALSE
);
803 rc
= fdisk_partition_toggle_flag(cxt
, n
, SGI_FLAG_SWAP
);
806 rc
= fdisk_sgi_create_info(cxt
);
814 * This is fdisk frontend for BSD specific libfdisk functions that
815 * are not expported by generic libfdisk API.
817 static int bsd_menu_cb(struct fdisk_context
**cxt0
,
818 const struct menu
*menu
__attribute__((__unused__
)),
819 const struct menu_entry
*ent
)
821 struct fdisk_context
*cxt
= *cxt0
;
826 assert(fdisk_is_label(cxt
, BSD
));
828 DBG(MENU
, ul_debug("enter BSD menu"));
832 rc
= fdisk_bsd_edit_disklabel(cxt
);
835 rc
= fdisk_bsd_write_bootstrap(cxt
);
838 org
= fdisk_is_details(cxt
);
840 fdisk_enable_details(cxt
, 1);
842 fdisk_enable_details(cxt
, org
);
845 rc
= fdisk_bsd_link_partition(cxt
);
852 static int geo_menu_cb(struct fdisk_context
**cxt0
,
853 const struct menu
*menu
__attribute__((__unused__
)),
854 const struct menu_entry
*ent
)
856 struct fdisk_context
*cxt
= *cxt0
;
858 uintmax_t c
= 0, h
= 0, s
= 0;
860 DBG(MENU
, ul_debug("enter GEO menu"));
867 rc
= fdisk_ask_number(cxt
, 1, fdisk_get_geom_cylinders(cxt
),
868 1048576, _("Number of cylinders"), &c
);
871 rc
= fdisk_ask_number(cxt
, 1, fdisk_get_geom_heads(cxt
),
872 256, _("Number of heads"), &h
);
875 rc
= fdisk_ask_number(cxt
, 1, fdisk_get_geom_sectors(cxt
),
876 63, _("Number of sectors"), &s
);
881 fdisk_override_geometry(cxt
, c
, h
, s
);
885 static int createlabel_menu_cb(struct fdisk_context
**cxt0
,
886 const struct menu
*menu
__attribute__((__unused__
)),
887 const struct menu_entry
*ent
)
889 struct fdisk_context
*cxt
= *cxt0
;
892 DBG(MENU
, ul_debug("enter Create label menu"));
900 /* Deprecated, use 'G' in main menu, just for backward
901 * compatibility only. */
902 rc
= fdisk_create_disklabel(cxt
, "sgi");
910 fdisk_create_disklabel(cxt
, "gpt");
913 fdisk_create_disklabel(cxt
, "sgi");
916 fdisk_create_disklabel(cxt
, "dos");
919 fdisk_create_disklabel(cxt
, "sun");