]>
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 while (mc
->menu_idx
< ARRAY_SIZE(menus
)) {
251 const struct menu
*m
= menus
[mc
->menu_idx
];
252 const struct menu_entry
*e
= &(m
->entries
[mc
->entry_idx
]);
258 /* no more entries */
259 if (e
->title
== NULL
||
260 /* menu wanted for specified labels only */
261 (m
->label
&& cxt
->label
&& !(m
->label
& cxt
->label
->id
)) ||
262 /* unwanted for nested PT */
263 (m
->nonested
&& cxt
->parent
) ||
264 /* menu excluded for specified labels */
265 (m
->exclude
&& cxt
->label
&& (m
->exclude
& cxt
->label
->id
))) {
275 /* excluded for the current label */
276 if ((e
->exclude
&& cxt
->label
&& e
->exclude
& cxt
->label
->id
) ||
277 /* entry wanted for specified labels only */
278 (e
->label
&& cxt
->label
&& !(e
->label
& cxt
->label
->id
)) ||
279 /* exclude non-expert entries in expect mode */
280 (e
->expert
== 0 && fdisk_context_display_details(cxt
)) ||
282 (e
->parent
&& (!cxt
->parent
|| cxt
->parent
->label
->id
!= e
->parent
)) ||
283 /* exclude non-normal entries in normal mode */
284 (e
->normal
== 0 && !fdisk_context_display_details(cxt
))) {
296 /* returns @menu and menu entry for then @key */
297 static const struct menu_entry
*get_fdisk_menu_entry(
298 struct fdisk_context
*cxt
,
300 const struct menu
**menu
)
302 struct menu_context mc
= MENU_CXT_EMPTY
;
303 const struct menu_entry
*e
;
305 while ((e
= next_menu_entry(cxt
, &mc
))) {
306 if (IS_MENU_SEP(e
) || e
->key
!= key
)
310 *menu
= menus
[mc
.menu_idx
];
317 static int menu_detect_collisions(struct fdisk_context
*cxt
)
319 struct menu_context mc
= MENU_CXT_EMPTY
;
320 const struct menu_entry
*e
, *r
;
322 while ((e
= next_menu_entry(cxt
, &mc
))) {
326 r
= get_fdisk_menu_entry(cxt
, e
->key
, NULL
);
328 DBG(FRONTEND
, ul_debug("warning: not found "
329 "entry for %c", e
->key
));
333 DBG(FRONTEND
, ul_debug("warning: duplicate key '%c'",
335 DBG(FRONTEND
, ul_debug(" : %s", e
->title
));
336 DBG(FRONTEND
, ul_debug(" : %s", r
->title
));
344 static int print_fdisk_menu(struct fdisk_context
*cxt
)
346 struct menu_context mc
= MENU_CXT_EMPTY
;
347 const struct menu_entry
*e
;
349 ON_DBG(FRONTEND
, menu_detect_collisions(cxt
));
351 if (fdisk_context_display_details(cxt
))
352 printf(_("\nHelp (expert commands):\n"));
354 printf(_("\nHelp:\n"));
356 while ((e
= next_menu_entry(cxt
, &mc
))) {
358 continue; /* hidden entry */
359 if (IS_MENU_SEP(e
) && (!e
->title
|| !*e
->title
))
361 else if (IS_MENU_SEP(e
)) {
362 color_scheme_enable("help-title", UL_COLOR_BOLD
);
363 printf("\n %s\n", _(e
->title
));
366 printf(" %c %s\n", e
->key
, _(e
->title
));
371 fdisk_info(cxt
, _("You're editing nested '%s' partition table, "
372 "primary partition table is '%s'."),
374 cxt
->parent
->label
->name
);
379 /* Asks for command, verify the key and perform the command or
380 * returns the command key if no callback for the command is
383 * Note that this function might exchange the context pointer to
384 * switch to another (nested) context.
386 * Returns: <0 on error
387 * 0 on success (the command performed)
388 * >0 if no callback (then returns the key)
390 int process_fdisk_menu(struct fdisk_context
**cxt0
)
392 struct fdisk_context
*cxt
= *cxt0
;
393 const struct menu_entry
*ent
;
394 const struct menu
*menu
;
399 if (fdisk_context_display_details(cxt
))
400 prompt
= _("Expert command (m for help): ");
402 prompt
= _("Command (m for help): ");
405 rc
= get_user_reply(cxt
, prompt
, buf
, sizeof(buf
));
410 ent
= get_fdisk_menu_entry(cxt
, key
, &menu
);
412 fdisk_warnx(cxt
, _("%c: unknown command"), key
);
417 DBG(FRONTEND
, ul_debug("selected: key=%c, entry='%s'",
420 /* menu has implemented callback, use it */
422 rc
= menu
->callback(cxt0
, menu
, ent
);
424 DBG(FRONTEND
, ul_debug("no callback for key '%c'", key
));
428 DBG(FRONTEND
, ul_debug("process menu done [rc=%d]", rc
));
433 * Basic fdisk actions
435 static int generic_menu_cb(struct fdisk_context
**cxt0
,
436 const struct menu
*menu
__attribute__((__unused__
)),
437 const struct menu_entry
*ent
)
439 struct fdisk_context
*cxt
= *cxt0
;
443 /* actions shared between expert and normal mode */
446 list_disk_geometry(cxt
);
450 if (fdisk_context_is_readonly(cxt
)) {
451 fdisk_warnx(cxt
, _("Device open in read-only mode."));
454 rc
= fdisk_write_disklabel(cxt
);
456 err(EXIT_FAILURE
, _("failed to write disklabel"));
458 break; /* nested PT, don't leave */
459 fdisk_info(cxt
, _("The partition table has been altered."));
460 rc
= fdisk_reread_partition_table(cxt
);
462 rc
= fdisk_context_deassign_device(cxt
);
465 fdisk_free_context(cxt
);
467 exit(rc
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
469 rc
= print_fdisk_menu(cxt
);
472 rc
= fdisk_verify_disklabel(cxt
);
480 dump_firstsector(cxt
);
486 rc
= fdisk_reorder_partitions(cxt
);
489 rc
= fdisk_context_enable_details(cxt
, 0);
498 rc
= fdisk_ask_partnum(cxt
, &n
, FALSE
);
500 rc
= fdisk_delete_partition(cxt
, n
);
502 fdisk_warnx(cxt
, _("Could not delete partition %zu"), n
+ 1);
504 fdisk_info(cxt
, _("Partition %zu has been deleted."), n
+ 1);
507 list_partition_types(cxt
);
510 rc
= fdisk_add_partition(cxt
, NULL
);
513 change_partition_type(cxt
);
516 fdisk_context_set_unit(cxt
,
517 fdisk_context_use_cylinders(cxt
) ? "sectors" :
519 if (fdisk_context_use_cylinders(cxt
))
520 fdisk_info(cxt
, _("Changing display/entry units to cylinders (DEPRECATED!)."));
522 fdisk_info(cxt
, _("Changing display/entry units to sectors."));
525 fdisk_context_enable_details(cxt
, 1);
528 /* return from nested BSD to DOS */
532 fdisk_info(cxt
, _("Leaving nested disklabel."));
533 fdisk_free_context(cxt
);
544 * This is fdisk frontend for GPT specific libfdisk functions that
545 * are not expported by generic libfdisk API.
547 static int gpt_menu_cb(struct fdisk_context
**cxt0
,
548 const struct menu
*menu
__attribute__((__unused__
)),
549 const struct menu_entry
*ent
)
551 struct fdisk_context
*cxt
= *cxt0
;
552 struct fdisk_context
*mbr
;
558 assert(fdisk_is_disklabel(cxt
, GPT
));
560 DBG(FRONTEND
, ul_debug("enter GPT menu"));
565 return fdisk_set_disklabel_id(cxt
);
567 mbr
= fdisk_new_nested_context(cxt
, "dos");
571 fdisk_context_enable_details(cxt
, 1); /* keep us in expert mode */
572 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
573 _("Entering protective/hybrid MBR disklabel."));
577 /* actions where is necessary partnum */
578 rc
= fdisk_ask_partnum(cxt
, &n
, FALSE
);
584 rc
= fdisk_gpt_partition_set_uuid(cxt
, n
);
587 rc
= fdisk_gpt_partition_set_name(cxt
, n
);
590 rc
= fdisk_partition_toggle_flag(cxt
, n
, GPT_FLAG_LEGACYBOOT
);
593 rc
= fdisk_partition_toggle_flag(cxt
, n
, GPT_FLAG_NOBLOCK
);
596 rc
= fdisk_partition_toggle_flag(cxt
, n
, GPT_FLAG_REQUIRED
);
599 rc
= fdisk_partition_toggle_flag(cxt
, n
, GPT_FLAG_GUIDSPECIFIC
);
608 * This is fdisk frontend for MBR specific libfdisk functions that
609 * are not expported by generic libfdisk API.
611 static int dos_menu_cb(struct fdisk_context
**cxt0
,
612 const struct menu
*menu
__attribute__((__unused__
)),
613 const struct menu_entry
*ent
)
615 struct fdisk_context
*cxt
= *cxt0
;
618 DBG(FRONTEND
, ul_debug("enter DOS menu"));
625 rc
= fdisk_ask_partnum(cxt
, &n
, FALSE
);
627 rc
= fdisk_partition_toggle_flag(cxt
, n
, DOS_FLAG_ACTIVE
);
632 struct fdisk_context
*bsd
633 = fdisk_new_nested_context(cxt
, "bsd");
636 if (!fdisk_dev_has_disklabel(bsd
))
637 rc
= fdisk_create_disklabel(bsd
, "bsd");
639 fdisk_free_context(bsd
);
642 fdisk_sinfo(cxt
, FDISK_INFO_SUCCESS
,
643 _("Entering nested BSD disklabel."));
648 toggle_dos_compatibility_flag(cxt
);
659 rc
= fdisk_ask_partnum(cxt
, &n
, FALSE
);
661 rc
= fdisk_dos_move_begin(cxt
, n
);
665 rc
= fdisk_set_disklabel_id(cxt
);
668 /* return from nested MBR to GPT */
672 fdisk_info(cxt
, _("Leaving nested disklabel."));
673 fdisk_free_context(cxt
);
681 static int sun_menu_cb(struct fdisk_context
**cxt0
,
682 const struct menu
*menu
__attribute__((__unused__
)),
683 const struct menu_entry
*ent
)
685 struct fdisk_context
*cxt
= *cxt0
;
688 DBG(FRONTEND
, ul_debug("enter SUN menu"));
692 assert(fdisk_is_disklabel(cxt
, SUN
));
694 DBG(FRONTEND
, ul_debug("enter SUN menu"));
700 rc
= fdisk_ask_partnum(cxt
, &n
, FALSE
);
705 rc
= fdisk_partition_toggle_flag(cxt
, n
, SUN_FLAG_RONLY
);
708 rc
= fdisk_partition_toggle_flag(cxt
, n
, SUN_FLAG_UNMNT
);
717 rc
= fdisk_sun_set_alt_cyl(cxt
);
720 rc
= fdisk_sun_set_xcyl(cxt
);
723 rc
= fdisk_sun_set_ilfact(cxt
);
726 rc
= fdisk_sun_set_rspeed(cxt
);
729 rc
= fdisk_sun_set_pcylcount(cxt
);
735 static int sgi_menu_cb(struct fdisk_context
**cxt0
,
736 const struct menu
*menu
__attribute__((__unused__
)),
737 const struct menu_entry
*ent
)
739 struct fdisk_context
*cxt
= *cxt0
;
743 DBG(FRONTEND
, ul_debug("enter SGI menu"));
747 assert(fdisk_is_disklabel(cxt
, SGI
));
754 rc
= fdisk_ask_partnum(cxt
, &n
, FALSE
);
756 rc
= fdisk_partition_toggle_flag(cxt
, n
, SGI_FLAG_BOOT
);
759 fdisk_sgi_set_bootfile(cxt
);
762 rc
= fdisk_ask_partnum(cxt
, &n
, FALSE
);
764 rc
= fdisk_partition_toggle_flag(cxt
, n
, SGI_FLAG_SWAP
);
767 rc
= fdisk_sgi_create_info(cxt
);
775 * This is fdisk frontend for BSD specific libfdisk functions that
776 * are not expported by generic libfdisk API.
778 static int bsd_menu_cb(struct fdisk_context
**cxt0
,
779 const struct menu
*menu
__attribute__((__unused__
)),
780 const struct menu_entry
*ent
)
782 struct fdisk_context
*cxt
= *cxt0
;
787 assert(fdisk_is_disklabel(cxt
, BSD
));
789 DBG(FRONTEND
, ul_debug("enter BSD menu"));
793 rc
= fdisk_bsd_edit_disklabel(cxt
);
796 rc
= fdisk_bsd_write_bootstrap(cxt
);
799 org
= fdisk_context_display_details(cxt
);
801 fdisk_context_enable_details(cxt
, 1);
803 fdisk_context_enable_details(cxt
, org
);
806 rc
= fdisk_bsd_link_partition(cxt
);
813 static int geo_menu_cb(struct fdisk_context
**cxt0
,
814 const struct menu
*menu
__attribute__((__unused__
)),
815 const struct menu_entry
*ent
)
817 struct fdisk_context
*cxt
= *cxt0
;
819 uintmax_t c
= 0, h
= 0, s
= 0;
821 DBG(FRONTEND
, ul_debug("enter GEO menu"));
828 rc
= fdisk_ask_number(cxt
, 1, cxt
->geom
.cylinders
,
829 1048576, _("Number of cylinders"), &c
);
832 rc
= fdisk_ask_number(cxt
, 1, cxt
->geom
.heads
,
833 256, _("Number of heads"), &h
);
836 rc
= fdisk_ask_number(cxt
, 1, cxt
->geom
.sectors
,
837 63, _("Number of sectors"), &s
);
842 fdisk_override_geometry(cxt
, c
, h
, s
);
846 static int createlabel_menu_cb(struct fdisk_context
**cxt0
,
847 const struct menu
*menu
__attribute__((__unused__
)),
848 const struct menu_entry
*ent
)
850 struct fdisk_context
*cxt
= *cxt0
;
853 DBG(FRONTEND
, ul_debug("enter Create label menu"));
861 /* Deprecated, use 'G' in main menu, just for backward
862 * compatibility only. */
863 rc
= fdisk_create_disklabel(cxt
, "sgi");
871 fdisk_create_disklabel(cxt
, "gpt");
874 fdisk_create_disklabel(cxt
, "sgi");
877 fdisk_create_disklabel(cxt
, "dos");
880 fdisk_create_disklabel(cxt
, "sun");