]> git.ipfire.org Git - thirdparty/util-linux.git/blame - disk-utils/fdisk-menu.c
Merge branch 'bash-completion-fsck-find' of https://github.com/kevinoid/util-linux
[thirdparty/util-linux.git] / disk-utils / fdisk-menu.c
CommitLineData
161b0d1a
KZ
1
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <ctype.h>
6#include <stdint.h>
7
8#include "c.h"
ebfab541 9#include "rpmatch.h"
161b0d1a 10#include "fdisk.h"
9f280903 11#include "pt-sun.h"
f02fecd1
KZ
12#include "pt-mbr.h"
13
161b0d1a 14struct menu_entry {
7e937b77
KZ
15 const char key; /* command key */
16 const char *title; /* help string */
17 unsigned int normal : 1, /* normal mode */
18 expert : 1, /* expert mode */
19 hidden : 1; /* be sensitive for this key,
20 but don't print it in help */
161b0d1a 21
7e937b77 22 enum fdisk_labeltype label; /* only for this label */
58c87bd0 23 int exclude; /* all labels except these */
f92e1810 24 enum fdisk_labeltype parent; /* for nested PT */
161b0d1a
KZ
25};
26
27#define IS_MENU_SEP(e) ((e)->key == '-')
28#define IS_MENU_HID(e) ((e)->hidden)
29
30struct menu {
31 enum fdisk_labeltype label; /* only for this label */
58c87bd0 32 int exclude; /* all labels except these */
161b0d1a 33
f92e1810
KZ
34 unsigned int nonested : 1; /* don't make this menu active in nested PT */
35
a47fec81 36 int (*callback)(struct fdisk_context **,
a410f8df
KZ
37 const struct menu *,
38 const struct menu_entry *);
161b0d1a
KZ
39
40 struct menu_entry entries[]; /* NULL terminated array */
41};
42
43struct menu_context {
44 size_t menu_idx; /* the current menu */
45 size_t entry_idx; /* index with in the current menu */
46};
47
48#define MENU_CXT_EMPTY { 0, 0 }
8e40a677 49#define DECLARE_MENU_CB(x) \
a47fec81 50 static int x(struct fdisk_context **, \
8e40a677
KZ
51 const struct menu *, \
52 const struct menu_entry *)
161b0d1a 53
8e40a677 54DECLARE_MENU_CB(gpt_menu_cb);
9f280903 55DECLARE_MENU_CB(sun_menu_cb);
aae727f2 56DECLARE_MENU_CB(sgi_menu_cb);
8e40a677 57DECLARE_MENU_CB(geo_menu_cb);
f02fecd1 58DECLARE_MENU_CB(dos_menu_cb);
b529ea2a 59DECLARE_MENU_CB(bsd_menu_cb);
b3ac22ef 60DECLARE_MENU_CB(createlabel_menu_cb);
89309968 61DECLARE_MENU_CB(generic_menu_cb);
b9e94cd7 62
161b0d1a
KZ
63/*
64 * Menu entry macros:
65 * MENU_X* expert mode only
66 * MENU_B* both -- expert + normal mode
67 *
7e937b77
KZ
68 * *_E exclude this label
69 * *_H hidden
70 * *_L only for this label
161b0d1a
KZ
71 */
72
73/* separator */
74#define MENU_SEP(t) { .title = t, .key = '-', .normal = 1 }
75#define MENU_XSEP(t) { .title = t, .key = '-', .expert = 1 }
76#define MENU_BSEP(t) { .title = t, .key = '-', .expert = 1, .normal = 1 }
77
78/* entry */
79#define MENU_ENT(k, t) { .title = t, .key = k, .normal = 1 }
80#define MENU_ENT_E(k, t, l) { .title = t, .key = k, .normal = 1, .exclude = l }
7e937b77 81#define MENU_ENT_L(k, t, l) { .title = t, .key = k, .normal = 1, .label = l }
161b0d1a
KZ
82
83#define MENU_XENT(k, t) { .title = t, .key = k, .expert = 1 }
84#define MENU_XENT_H(k, t) { .title = t, .key = k, .expert = 1, .hidden = 1 }
85
86#define MENU_BENT(k, t) { .title = t, .key = k, .expert = 1, .normal = 1 }
27ddd4f1 87#define MENU_BENT_E(k, t, l) { .title = t, .key = k, .expert = 1, .normal = 1, .exclude = l }
161b0d1a 88
f92e1810
KZ
89#define MENU_ENT_NEST(k, t, l, p) { .title = t, .key = k, .normal = 1, .label = l, .parent = p }
90#define MENU_XENT_NEST(k, t, l, p) { .title = t, .key = k, .expert = 1, .label = l, .parent = p }
161b0d1a
KZ
91
92/* Generic menu */
2ba641e5 93static const struct menu menu_generic = {
89309968 94 .callback = generic_menu_cb,
161b0d1a 95 .entries = {
6ae5e1e0 96 MENU_BSEP(N_("Generic")),
161b0d1a 97 MENU_ENT ('d', N_("delete a partition")),
0efd951c 98 MENU_ENT ('F', N_("list free unpartitioned space")),
161b0d1a
KZ
99 MENU_ENT ('l', N_("list known partition types")),
100 MENU_ENT ('n', N_("add a new partition")),
101 MENU_BENT ('p', N_("print the partition table")),
6ae5e1e0 102 MENU_ENT ('t', N_("change a partition type")),
27ddd4f1 103 MENU_BENT_E('v', N_("verify the partition table"), FDISK_DISKLABEL_BSD),
3e3b51b3 104 MENU_ENT ('i', N_("print information about a partition")),
161b0d1a 105
e916600f
KZ
106 MENU_XENT('d', N_("print the raw data of the first sector from the device")),
107 MENU_XENT('D', N_("print the raw data of the disklabel from the device")),
dd7ba604 108 MENU_XENT('f', N_("fix partitions order")),
6ae5e1e0 109
161b0d1a
KZ
110 MENU_SEP(N_("Misc")),
111 MENU_BENT ('m', N_("print this menu")),
112 MENU_ENT_E('u', N_("change display/entry units"), FDISK_DISKLABEL_GPT),
22ddf547 113 MENU_ENT_E('x', N_("extra functionality (experts only)"), FDISK_DISKLABEL_BSD),
161b0d1a 114
a30e4ef4 115 MENU_SEP(N_("Script")),
5cb9918d
KZ
116 MENU_ENT ('I', N_("load disk layout from sfdisk script file")),
117 MENU_ENT ('O', N_("dump disk layout to sfdisk script file")),
a30e4ef4 118
2a1a67df 119 MENU_BSEP(N_("Save & Exit")),
22ddf547
KZ
120 MENU_ENT_E('w', N_("write table to disk and exit"), FDISK_DISKLABEL_BSD),
121 MENU_ENT_L('w', N_("write table to disk"), FDISK_DISKLABEL_BSD),
161b0d1a
KZ
122 MENU_BENT ('q', N_("quit without saving changes")),
123 MENU_XENT ('r', N_("return to main menu")),
f92e1810
KZ
124
125 MENU_ENT_NEST('r', N_("return from BSD to DOS"), FDISK_DISKLABEL_BSD, FDISK_DISKLABEL_DOS),
161b0d1a
KZ
126
127 { 0, NULL }
128 }
129};
130
2ba641e5 131static const struct menu menu_createlabel = {
b3ac22ef 132 .callback = createlabel_menu_cb,
22ddf547 133 .exclude = FDISK_DISKLABEL_BSD,
f92e1810 134 .nonested = 1,
161b0d1a
KZ
135 .entries = {
136 MENU_SEP(N_("Create a new label")),
137 MENU_ENT('g', N_("create a new empty GPT partition table")),
138 MENU_ENT('G', N_("create a new empty SGI (IRIX) partition table")),
139 MENU_ENT('o', N_("create a new empty DOS partition table")),
140 MENU_ENT('s', N_("create a new empty Sun partition table")),
141
142 /* backward compatibility -- be sensitive to 'g', but don't
143 * print it in the expert menu */
144 MENU_XENT_H('g', N_("create an IRIX (SGI) partition table")),
145 { 0, NULL }
146 }
147};
148
2ba641e5 149static const struct menu menu_geo = {
8e40a677 150 .callback = geo_menu_cb,
22ddf547 151 .exclude = FDISK_DISKLABEL_GPT | FDISK_DISKLABEL_BSD,
be9ba6f3
KZ
152 .entries = {
153 MENU_XSEP(N_("Geometry")),
154 MENU_XENT('c', N_("change number of cylinders")),
155 MENU_XENT('h', N_("change number of heads")),
156 MENU_XENT('s', N_("change number of sectors/track")),
157 { 0, NULL }
158 }
159};
160
2ba641e5 161static const struct menu menu_gpt = {
b9e94cd7 162 .callback = gpt_menu_cb,
161b0d1a
KZ
163 .label = FDISK_DISKLABEL_GPT,
164 .entries = {
165 MENU_XSEP(N_("GPT")),
35b1f0a4 166 MENU_XENT('i', N_("change disk GUID")),
161b0d1a 167 MENU_XENT('n', N_("change partition name")),
35b1f0a4 168 MENU_XENT('u', N_("change partition UUID")),
304cf949 169 MENU_XENT('l', N_("change table length")),
f92e1810 170 MENU_XENT('M', N_("enter protective/hybrid MBR")),
f362d863
KZ
171
172 MENU_XSEP(""),
173 MENU_XENT('A', N_("toggle the legacy BIOS bootable flag")),
174 MENU_XENT('B', N_("toggle the no block IO protocol flag")),
175 MENU_XENT('R', N_("toggle the required partition flag")),
176 MENU_XENT('S', N_("toggle the GUID specific bits")),
177
161b0d1a
KZ
178 { 0, NULL }
179 }
180};
181
2ba641e5 182static const struct menu menu_sun = {
9f280903 183 .callback = sun_menu_cb,
2a1a67df
KZ
184 .label = FDISK_DISKLABEL_SUN,
185 .entries = {
186 MENU_BSEP(N_("Sun")),
aaa43826 187 MENU_ENT('a', N_("toggle the read-only flag")),
2a1a67df
KZ
188 MENU_ENT('c', N_("toggle the mountable flag")),
189
190 MENU_XENT('a', N_("change number of alternate cylinders")),
2a1a67df 191 MENU_XENT('e', N_("change number of extra sectors per cylinder")),
2a1a67df
KZ
192 MENU_XENT('i', N_("change interleave factor")),
193 MENU_XENT('o', N_("change rotation speed (rpm)")),
2a1a67df
KZ
194 MENU_XENT('y', N_("change number of physical cylinders")),
195 { 0, NULL }
196 }
197};
198
2ba641e5 199static const struct menu menu_sgi = {
aae727f2 200 .callback = sgi_menu_cb,
6ae5e1e0
KZ
201 .label = FDISK_DISKLABEL_SGI,
202 .entries = {
203 MENU_SEP(N_("SGI")),
204 MENU_ENT('a', N_("select bootable partition")),
205 MENU_ENT('b', N_("edit bootfile entry")),
206 MENU_ENT('c', N_("select sgi swap partition")),
aae727f2 207 MENU_ENT('i', N_("create SGI info")),
6ae5e1e0
KZ
208 { 0, NULL }
209 }
210};
211
2ba641e5 212static const struct menu menu_dos = {
f02fecd1 213 .callback = dos_menu_cb,
6ae5e1e0
KZ
214 .label = FDISK_DISKLABEL_DOS,
215 .entries = {
216 MENU_BSEP(N_("DOS (MBR)")),
217 MENU_ENT('a', N_("toggle a bootable flag")),
218 MENU_ENT('b', N_("edit nested BSD disklabel")),
219 MENU_ENT('c', N_("toggle the dos compatibility flag")),
220
221 MENU_XENT('b', N_("move beginning of data in a partition")),
6ae5e1e0 222 MENU_XENT('i', N_("change the disk identifier")),
f92e1810
KZ
223
224 MENU_XENT_NEST('M', N_("return from protective/hybrid MBR to GPT"),
225 FDISK_DISKLABEL_DOS, FDISK_DISKLABEL_GPT),
6ae5e1e0
KZ
226 { 0, NULL }
227 }
228};
229
2ba641e5 230static const struct menu menu_bsd = {
b529ea2a 231 .callback = bsd_menu_cb,
22ddf547 232 .label = FDISK_DISKLABEL_BSD,
6ae5e1e0
KZ
233 .entries = {
234 MENU_SEP(N_("BSD")),
235 MENU_ENT('e', N_("edit drive data")),
236 MENU_ENT('i', N_("install bootstrap")),
237 MENU_ENT('s', N_("show complete disklabel")),
6ae5e1e0 238 MENU_ENT('x', N_("link BSD partition to non-BSD partition")),
6ae5e1e0
KZ
239 { 0, NULL }
240 }
241};
242
161b0d1a 243static const struct menu *menus[] = {
6ae5e1e0
KZ
244 &menu_gpt,
245 &menu_sun,
246 &menu_sgi,
247 &menu_dos,
248 &menu_bsd,
be9ba6f3 249 &menu_geo,
161b0d1a
KZ
250 &menu_generic,
251 &menu_createlabel,
161b0d1a
KZ
252};
253
254static const struct menu_entry *next_menu_entry(
255 struct fdisk_context *cxt,
256 struct menu_context *mc)
257{
8eccde20
KZ
258 struct fdisk_label *lb = fdisk_get_label(cxt, NULL);
259 struct fdisk_context *parent = fdisk_get_parent(cxt);
260 unsigned int type = 0, pr_type = 0;
261
262 assert(cxt);
263
68ddb136
KZ
264 if (lb)
265 type = fdisk_label_get_type(lb);
8eccde20
KZ
266 if (parent)
267 pr_type = fdisk_label_get_type(fdisk_get_label(parent, NULL));
268
161b0d1a
KZ
269 while (mc->menu_idx < ARRAY_SIZE(menus)) {
270 const struct menu *m = menus[mc->menu_idx];
271 const struct menu_entry *e = &(m->entries[mc->entry_idx]);
272
f92e1810
KZ
273 /*
274 * whole-menu filter
275 */
276
be9ba6f3 277 /* no more entries */
161b0d1a 278 if (e->title == NULL ||
be9ba6f3 279 /* menu wanted for specified labels only */
8eccde20 280 (m->label && lb && !(m->label & type)) ||
f92e1810 281 /* unwanted for nested PT */
8eccde20 282 (m->nonested && parent) ||
be9ba6f3 283 /* menu excluded for specified labels */
8eccde20 284 (m->exclude && lb && (m->exclude & type))) {
161b0d1a
KZ
285 mc->menu_idx++;
286 mc->entry_idx = 0;
287 continue;
288 }
289
f92e1810
KZ
290 /*
291 * per entry filter
292 */
293
7e937b77 294 /* excluded for the current label */
8eccde20 295 if ((e->exclude && lb && e->exclude & type) ||
7e937b77 296 /* entry wanted for specified labels only */
8eccde20 297 (e->label && lb && !(e->label & type)) ||
161b0d1a 298 /* exclude non-expert entries in expect mode */
6a632136 299 (e->expert == 0 && fdisk_is_details(cxt)) ||
f92e1810 300 /* nested only */
8eccde20 301 (e->parent && (!parent || pr_type != e->parent)) ||
161b0d1a 302 /* exclude non-normal entries in normal mode */
6a632136 303 (e->normal == 0 && !fdisk_is_details(cxt))) {
161b0d1a
KZ
304 mc->entry_idx++;
305 continue;
306 }
307 mc->entry_idx++;
308 return e;
309
310 }
311 return NULL;
312}
313
3b4f9f16
KZ
314/* returns @menu and menu entry for then @key */
315static const struct menu_entry *get_fdisk_menu_entry(
316 struct fdisk_context *cxt,
317 int key,
318 const struct menu **menu)
319{
320 struct menu_context mc = MENU_CXT_EMPTY;
321 const struct menu_entry *e;
322
323 while ((e = next_menu_entry(cxt, &mc))) {
324 if (IS_MENU_SEP(e) || e->key != key)
325 continue;
326
327 if (menu)
328 *menu = menus[mc.menu_idx];
329 return e;
330 }
331
332 return NULL;
333}
334
335static int menu_detect_collisions(struct fdisk_context *cxt)
336{
337 struct menu_context mc = MENU_CXT_EMPTY;
338 const struct menu_entry *e, *r;
339
340 while ((e = next_menu_entry(cxt, &mc))) {
341 if (IS_MENU_SEP(e))
342 continue;
343
344 r = get_fdisk_menu_entry(cxt, e->key, NULL);
345 if (!r) {
e6d0c4c1 346 DBG(MENU, ul_debug("warning: not found "
3b4f9f16
KZ
347 "entry for %c", e->key));
348 return -1;
349 }
350 if (r != e) {
e6d0c4c1 351 DBG(MENU, ul_debug("warning: duplicate key '%c'",
3b4f9f16 352 e->key));
e6d0c4c1
KZ
353 DBG(MENU, ul_debug(" : %s", e->title));
354 DBG(MENU, ul_debug(" : %s", r->title));
3b4f9f16
KZ
355 abort();
356 }
357 }
358
359 return 0;
360}
361
1f75d1ce 362static int print_fdisk_menu(struct fdisk_context *cxt)
161b0d1a
KZ
363{
364 struct menu_context mc = MENU_CXT_EMPTY;
365 const struct menu_entry *e;
366
e6d0c4c1 367 ON_DBG(MENU, menu_detect_collisions(cxt));
3b4f9f16 368
6a632136 369 if (fdisk_is_details(cxt))
39f01b7b 370 printf(_("\nHelp (expert commands):\n"));
161b0d1a 371 else
39f01b7b 372 printf(_("\nHelp:\n"));
161b0d1a
KZ
373
374 while ((e = next_menu_entry(cxt, &mc))) {
375 if (IS_MENU_HID(e))
376 continue; /* hidden entry */
f362d863
KZ
377 if (IS_MENU_SEP(e) && (!e->title || !*e->title))
378 printf("\n");
379 else if (IS_MENU_SEP(e)) {
496c979a 380 color_scheme_enable("help-title", UL_COLOR_BOLD);
161b0d1a 381 printf("\n %s\n", _(e->title));
80a1712f
KZ
382 color_disable();
383 } else
161b0d1a
KZ
384 printf(" %c %s\n", e->key, _(e->title));
385 }
386 fputc('\n', stdout);
387
8eccde20
KZ
388 if (fdisk_get_parent(cxt)) {
389 struct fdisk_label *l = fdisk_get_label(cxt, NULL),
390 *p = fdisk_get_label(fdisk_get_parent(cxt), NULL);
391
f92e1810
KZ
392 fdisk_info(cxt, _("You're editing nested '%s' partition table, "
393 "primary partition table is '%s'."),
8eccde20
KZ
394 fdisk_label_get_name(l),
395 fdisk_label_get_name(p));
396 }
f92e1810 397
161b0d1a
KZ
398 return 0;
399}
400
a410f8df
KZ
401/* Asks for command, verify the key and perform the command or
402 * returns the command key if no callback for the command is
403 * implemented.
404 *
a47fec81
KZ
405 * Note that this function might exchange the context pointer to
406 * switch to another (nested) context.
407 *
a410f8df
KZ
408 * Returns: <0 on error
409 * 0 on success (the command performed)
410 * >0 if no callback (then returns the key)
411 */
a47fec81 412int process_fdisk_menu(struct fdisk_context **cxt0)
a410f8df 413{
a47fec81 414 struct fdisk_context *cxt = *cxt0;
a410f8df
KZ
415 const struct menu_entry *ent;
416 const struct menu *menu;
417 int key, rc;
418 const char *prompt;
419 char buf[BUFSIZ];
420
6a632136 421 if (fdisk_is_details(cxt))
a410f8df
KZ
422 prompt = _("Expert command (m for help): ");
423 else
424 prompt = _("Command (m for help): ");
425
426 fputc('\n',stdout);
c1154128
KZ
427 rc = get_user_reply(prompt, buf, sizeof(buf));
428
429 if (rc == -ECANCELED) {
430 /* Map ^C and ^D in main menu to 'q' */
431 if (is_interactive
432 && fdisk_label_is_changed(fdisk_get_label(cxt, NULL))) {
433 rc = get_user_reply(
434 _("\nDo you really want to quit? "),
435 buf, sizeof(buf));
436 if (rc || !rpmatch(buf))
437 return 0;
438 }
439 key = 'q';
440 } else if (rc) {
a410f8df 441 return rc;
c1154128
KZ
442 } else
443 key = buf[0];
a410f8df 444
a410f8df
KZ
445 ent = get_fdisk_menu_entry(cxt, key, &menu);
446 if (!ent) {
447 fdisk_warnx(cxt, _("%c: unknown command"), key);
448 return -EINVAL;
449 }
450
e6d0c4c1 451 DBG(MENU, ul_debug("selected: key=%c, entry='%s'",
a410f8df 452 key, ent->title));
a410f8df
KZ
453
454 /* menu has implemented callback, use it */
27ddd4f1 455 if (menu->callback)
a47fec81
KZ
456 rc = menu->callback(cxt0, menu, ent);
457 else {
e6d0c4c1 458 DBG(MENU, ul_debug("no callback for key '%c'", key));
27ddd4f1 459 rc = -EINVAL;
a47fec81 460 }
a410f8df 461
e6d0c4c1 462 DBG(MENU, ul_debug("process menu done [rc=%d]", rc));
a47fec81 463 return rc;
a410f8df
KZ
464}
465
a30e4ef4
KZ
466static int script_read(struct fdisk_context *cxt)
467{
468 struct fdisk_script *sc = NULL;
469 char *filename = NULL;
470 int rc;
471
472 rc = fdisk_ask_string(cxt, _("Enter script file name"), &filename);
473 if (rc)
474 return rc;
475
476 errno = 0;
477 sc = fdisk_new_script_from_file(cxt, filename);
478 if (!sc && errno)
54fefa07 479 fdisk_warn(cxt, _("Cannot open %s"), filename);
a30e4ef4
KZ
480 else if (!sc)
481 fdisk_warnx(cxt, _("Failed to parse script file %s"), filename);
dda4743b 482 else if (fdisk_apply_script(cxt, sc) != 0) {
a30e4ef4 483 fdisk_warnx(cxt, _("Failed to apply script %s"), filename);
dda4743b
KZ
484 fdisk_warnx(cxt, _("Resetting fdisk!"));
485 rc = fdisk_reassign_device(cxt);
486 if (rc == 0 && !fdisk_has_label(cxt)) {
487 fdisk_info(cxt, _("Device does not contain a recognized partition table."));
488 fdisk_create_disklabel(cxt, NULL);
489 }
490 } else
a30e4ef4
KZ
491 fdisk_info(cxt, _("Script successfully applied."));
492
493 fdisk_unref_script(sc);
494 free(filename);
495 return rc;
496}
497
498static int script_write(struct fdisk_context *cxt)
499{
500 struct fdisk_script *sc = NULL;
501 char *filename = NULL;
502 FILE *f = NULL;
503 int rc;
504
505 rc = fdisk_ask_string(cxt, _("Enter script file name"), &filename);
506 if (rc)
507 return rc;
508
509 sc = fdisk_new_script(cxt);
510 if (!sc) {
511 fdisk_warn(cxt, _("Failed to allocate script handler"));
512 goto done;
513 }
514
515 rc = fdisk_script_read_context(sc, NULL);
516 if (rc) {
54fefa07 517 fdisk_warnx(cxt, _("Failed to transform disk layout into script"));
a30e4ef4
KZ
518 goto done;
519 }
520
521 f = fopen(filename, "w");
522 if (!f) {
54fefa07 523 fdisk_warn(cxt, _("Cannot open %s"), filename);
a30e4ef4
KZ
524 goto done;
525 }
526
527 rc = fdisk_script_write_file(sc, f);
528 if (rc)
529 fdisk_warn(cxt, _("Failed to write script %s"), filename);
530 else
531 fdisk_info(cxt, _("Script successfully saved."));
532done:
533 if (f)
534 fclose(f);
535 fdisk_unref_script(sc);
536 free(filename);
537 return rc;
538}
539
ba465623
KZ
540static int ask_for_wipe(struct fdisk_context *cxt, size_t partno)
541{
542 struct fdisk_partition *tmp = NULL;
543 char *fstype = NULL;
544 int rc, yes = 0;
545
546 rc = fdisk_get_partition(cxt, partno, &tmp);
547 if (rc)
548 goto done;
549
550 rc = fdisk_partition_to_string(tmp, cxt, FDISK_FIELD_FSTYPE, &fstype);
551 if (rc || fstype == NULL)
552 goto done;
553
554 fdisk_warnx(cxt, _("Partition #%zu contains a %s signature."), partno + 1, fstype);
555
556 if (pwipemode == WIPEMODE_AUTO && isatty(STDIN_FILENO))
557 fdisk_ask_yesno(cxt, _("Do you want to remove the signature?"), &yes);
558 else if (pwipemode == WIPEMODE_ALWAYS)
559 yes = 1;
560
561 if (yes) {
562 fdisk_info(cxt, _("The signature will be removed by a write command."));
563 rc = fdisk_wipe_partition(cxt, partno, TRUE);
564 }
565done:
566 fdisk_unref_partition(tmp);
567 free(fstype);
568 return rc;
569}
570
89309968
KZ
571/*
572 * Basic fdisk actions
573 */
574static int generic_menu_cb(struct fdisk_context **cxt0,
575 const struct menu *menu __attribute__((__unused__)),
576 const struct menu_entry *ent)
577{
578 struct fdisk_context *cxt = *cxt0;
579 int rc = 0;
27ddd4f1 580 size_t n;
89309968 581
27ddd4f1 582 /* actions shared between expert and normal mode */
89309968 583 switch (ent->key) {
89309968
KZ
584 case 'p':
585 list_disk_geometry(cxt);
9f670072 586 list_disklabel(cxt);
89309968 587 break;
df4bfa97 588 case 'w':
6a632136 589 if (fdisk_is_readonly(cxt)) {
62eea9ce 590 fdisk_warnx(cxt, _("Device is open in read-only mode."));
e146ae4e
KZ
591 break;
592 }
df4bfa97
KZ
593 rc = fdisk_write_disklabel(cxt);
594 if (rc)
fd56121a 595 err(EXIT_FAILURE, _("failed to write disklabel"));
8eccde20 596 if (fdisk_get_parent(cxt))
df4bfa97
KZ
597 break; /* nested PT, don't leave */
598 fdisk_info(cxt, _("The partition table has been altered."));
fadd8e08
KZ
599
600 if (device_is_used)
601 rc = fdisk_reread_changes(cxt, original_layout);
602 else
603 rc = fdisk_reread_partition_table(cxt);
a57639e1 604 if (!rc)
6a632136 605 rc = fdisk_deassign_device(cxt, 0);
a57639e1 606 /* fallthrough */
89309968 607 case 'q':
c7119037 608 fdisk_unref_context(cxt);
a57639e1
KZ
609 fputc('\n', stdout);
610 exit(rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
27ddd4f1
KZ
611 case 'm':
612 rc = print_fdisk_menu(cxt);
613 break;
89309968
KZ
614 case 'v':
615 rc = fdisk_verify_disklabel(cxt);
616 break;
3e3b51b3
JLB
617 case 'i':
618 rc = print_partition_info(cxt);
619 break;
0efd951c
KZ
620 case 'F':
621 list_freespace(cxt);
622 break;
27ddd4f1
KZ
623 }
624
625 /* expert mode */
626 if (ent->expert) {
627 switch (ent->key) {
628 case 'd':
e916600f
KZ
629 dump_firstsector(cxt);
630 break;
631 case 'D':
632 dump_disklabel(cxt);
27ddd4f1 633 break;
dd7ba604
KZ
634 case 'f':
635 rc = fdisk_reorder_partitions(cxt);
4afb337e 636 if (rc)
056e622c 637 fdisk_warnx(cxt, _("Failed to fix partitions order."));
4afb337e
KZ
638 else
639 fdisk_info(cxt, _("Partitions order fixed."));
dd7ba604 640 break;
27ddd4f1 641 case 'r':
6a632136 642 rc = fdisk_enable_details(cxt, 0);
27ddd4f1
KZ
643 break;
644 }
645 return rc;
646 }
647
648 /* normal mode */
649 switch (ent->key) {
650 case 'd':
651 rc = fdisk_ask_partnum(cxt, &n, FALSE);
652 if (!rc)
653 rc = fdisk_delete_partition(cxt, n);
654 if (rc)
48d4d931 655 fdisk_warnx(cxt, _("Could not delete partition %zu"), n + 1);
27ddd4f1 656 else
48d4d931 657 fdisk_info(cxt, _("Partition %zu has been deleted."), n + 1);
27ddd4f1 658 break;
a30e4ef4
KZ
659 case 'I':
660 script_read(cxt);
661 break;
662 case 'O':
663 script_write(cxt);
664 break;
27ddd4f1
KZ
665 case 'l':
666 list_partition_types(cxt);
667 break;
668 case 'n':
ba465623
KZ
669 {
670 size_t partno;
671 rc = fdisk_add_partition(cxt, NULL, &partno);
672 if (!rc)
673 rc = ask_for_wipe(cxt, partno);
27ddd4f1 674 break;
ba465623 675 }
27ddd4f1
KZ
676 case 't':
677 change_partition_type(cxt);
678 break;
679 case 'u':
6a632136
KZ
680 fdisk_set_unit(cxt,
681 fdisk_use_cylinders(cxt) ? "sectors" :
27ddd4f1 682 "cylinders");
6a632136 683 if (fdisk_use_cylinders(cxt))
27ddd4f1
KZ
684 fdisk_info(cxt, _("Changing display/entry units to cylinders (DEPRECATED!)."));
685 else
686 fdisk_info(cxt, _("Changing display/entry units to sectors."));
687 break;
688 case 'x':
6a632136 689 fdisk_enable_details(cxt, 1);
27ddd4f1
KZ
690 break;
691 case 'r':
f92e1810 692 /* return from nested BSD to DOS */
8eccde20
KZ
693 if (fdisk_get_parent(cxt)) {
694 *cxt0 = fdisk_get_parent(cxt);
27ddd4f1 695
fd56121a 696 fdisk_info(cxt, _("Leaving nested disklabel."));
c7119037 697 fdisk_unref_context(cxt);
27ddd4f1
KZ
698 }
699 break;
89309968 700 }
27ddd4f1 701
89309968
KZ
702 return rc;
703}
704
8e40a677 705
b9e94cd7
KZ
706/*
707 * This is fdisk frontend for GPT specific libfdisk functions that
9e930041 708 * are not exported by generic libfdisk API.
b9e94cd7 709 */
a47fec81 710static int gpt_menu_cb(struct fdisk_context **cxt0,
b9e94cd7
KZ
711 const struct menu *menu __attribute__((__unused__)),
712 const struct menu_entry *ent)
713{
a47fec81 714 struct fdisk_context *cxt = *cxt0;
f92e1810 715 struct fdisk_context *mbr;
6936c081 716 struct fdisk_partition *pa = NULL;
b9e94cd7 717 size_t n;
f92e1810 718 int rc = 0;
e4015b34 719 uintmax_t length = 0;
b9e94cd7
KZ
720
721 assert(cxt);
722 assert(ent);
aa36c2cf 723 assert(fdisk_is_label(cxt, GPT));
b9e94cd7 724
e6d0c4c1 725 DBG(MENU, ul_debug("enter GPT menu"));
b9e94cd7 726
f92e1810
KZ
727 if (ent->expert) {
728 switch (ent->key) {
729 case 'i':
730 return fdisk_set_disklabel_id(cxt);
304cf949
SP
731 case 'l':
732 rc = fdisk_ask_number(cxt, 1, fdisk_get_npartitions(cxt),
733 ~(uint32_t)0, _("New maximum entries"), &length);
734 if (rc)
735 return rc;
e4015b34 736 return fdisk_gpt_set_npartitions(cxt, (uint32_t) length);
f92e1810
KZ
737 case 'M':
738 mbr = fdisk_new_nested_context(cxt, "dos");
739 if (!mbr)
740 return -ENOMEM;
741 *cxt0 = cxt = mbr;
6a632136 742 fdisk_enable_details(cxt, 1); /* keep us in expert mode */
0477369a 743 fdisk_info(cxt, _("Entering protective/hybrid MBR disklabel."));
f92e1810
KZ
744 return 0;
745 }
35b1f0a4 746
f92e1810
KZ
747 /* actions where is necessary partnum */
748 rc = fdisk_ask_partnum(cxt, &n, FALSE);
749 if (rc)
750 return rc;
b9e94cd7 751
f92e1810
KZ
752 switch(ent->key) {
753 case 'u':
6936c081
KZ
754 pa = fdisk_new_partition(); /* new template */
755 if (!pa)
756 rc = -ENOMEM;
757 else {
758 char *str = NULL;
759 rc = fdisk_ask_string(cxt, _("New UUID (in 8-4-4-4-12 format)"), &str);
760 if (!rc)
761 rc = fdisk_partition_set_uuid(pa, str);
762 if (!rc)
763 rc = fdisk_set_partition(cxt, n, pa);
764 free(str);
765 fdisk_unref_partition(pa);
766 }
f92e1810
KZ
767 break;
768 case 'n':
6936c081
KZ
769 pa = fdisk_new_partition(); /* new template */
770 if (!pa)
771 rc = -ENOMEM;
772 else {
773 char *str = NULL;
774 rc = fdisk_ask_string(cxt, _("New name"), &str);
775 if (!rc)
776 rc = fdisk_partition_set_name(pa, str);
777 if (!rc)
778 rc = fdisk_set_partition(cxt, n, pa);
779 free(str);
780 fdisk_unref_partition(pa);
781 }
f92e1810 782 break;
f362d863 783 case 'A':
a1ef792f 784 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_LEGACYBOOT);
f362d863
KZ
785 break;
786 case 'B':
a1ef792f 787 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_NOBLOCK);
f362d863
KZ
788 break;
789 case 'R':
a1ef792f 790 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_REQUIRED);
f362d863
KZ
791 break;
792 case 'S':
a1ef792f 793 rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_GUIDSPECIFIC);
f362d863 794 break;
f92e1810 795 }
b9e94cd7 796 }
6936c081 797
b9e94cd7
KZ
798 return rc;
799}
800
f02fecd1
KZ
801
802/*
803 * This is fdisk frontend for MBR specific libfdisk functions that
9e930041 804 * are not exported by generic libfdisk API.
f02fecd1 805 */
a47fec81 806static int dos_menu_cb(struct fdisk_context **cxt0,
f02fecd1
KZ
807 const struct menu *menu __attribute__((__unused__)),
808 const struct menu_entry *ent)
809{
a47fec81 810 struct fdisk_context *cxt = *cxt0;
f02fecd1
KZ
811 int rc = 0;
812
e6d0c4c1 813 DBG(MENU, ul_debug("enter DOS menu"));
a47fec81 814
f02fecd1
KZ
815 if (!ent->expert) {
816 switch (ent->key) {
817 case 'a':
818 {
819 size_t n;
820 rc = fdisk_ask_partnum(cxt, &n, FALSE);
821 if (!rc)
a1ef792f 822 rc = fdisk_toggle_partition_flag(cxt, n, DOS_FLAG_ACTIVE);
f02fecd1
KZ
823 break;
824 }
825 case 'b':
826 {
827 struct fdisk_context *bsd
828 = fdisk_new_nested_context(cxt, "bsd");
818d7924
KZ
829 if (!bsd)
830 return -ENOMEM;
aa36c2cf 831 if (!fdisk_has_label(bsd))
818d7924 832 rc = fdisk_create_disklabel(bsd, "bsd");
a47fec81 833 if (rc)
c7119037 834 fdisk_unref_context(bsd);
27ddd4f1 835 else {
a47fec81 836 *cxt0 = cxt = bsd;
0477369a 837 fdisk_info(cxt, _("Entering nested BSD disklabel."));
27ddd4f1 838 }
f02fecd1
KZ
839 break;
840 }
841 case 'c':
842 toggle_dos_compatibility_flag(cxt);
843 break;
844 }
845 return rc;
846 }
847
848 /* expert mode */
849 switch (ent->key) {
850 case 'b':
851 {
852 size_t n;
853 rc = fdisk_ask_partnum(cxt, &n, FALSE);
854 if (!rc)
f8ad3899 855 rc = fdisk_dos_move_begin(cxt, n);
f02fecd1
KZ
856 break;
857 }
f02fecd1
KZ
858 case 'i':
859 rc = fdisk_set_disklabel_id(cxt);
860 break;
f92e1810
KZ
861 case 'M':
862 /* return from nested MBR to GPT */
8eccde20
KZ
863 if (fdisk_get_parent(cxt)) {
864 *cxt0 = fdisk_get_parent(cxt);
f92e1810
KZ
865
866 fdisk_info(cxt, _("Leaving nested disklabel."));
c7119037 867 fdisk_unref_context(cxt);
f92e1810
KZ
868 }
869 break;
f02fecd1
KZ
870 }
871 return rc;
872}
873
a47fec81 874static int sun_menu_cb(struct fdisk_context **cxt0,
9f280903
KZ
875 const struct menu *menu __attribute__((__unused__)),
876 const struct menu_entry *ent)
877{
a47fec81 878 struct fdisk_context *cxt = *cxt0;
9f280903
KZ
879 int rc = 0;
880
e6d0c4c1 881 DBG(MENU, ul_debug("enter SUN menu"));
a47fec81 882
9f280903
KZ
883 assert(cxt);
884 assert(ent);
aa36c2cf 885 assert(fdisk_is_label(cxt, SUN));
9f280903 886
e6d0c4c1 887 DBG(MENU, ul_debug("enter SUN menu"));
9f280903
KZ
888
889 /* normal mode */
890 if (!ent->expert) {
891 size_t n;
892
893 rc = fdisk_ask_partnum(cxt, &n, FALSE);
894 if (rc)
895 return rc;
896 switch (ent->key) {
897 case 'a':
a1ef792f 898 rc = fdisk_toggle_partition_flag(cxt, n, SUN_FLAG_RONLY);
9f280903
KZ
899 break;
900 case 'c':
a1ef792f 901 rc = fdisk_toggle_partition_flag(cxt, n, SUN_FLAG_UNMNT);
9f280903
KZ
902 break;
903 }
904 return rc;
905 }
906
907 /* expert mode */
908 switch (ent->key) {
909 case 'a':
910 rc = fdisk_sun_set_alt_cyl(cxt);
911 break;
912 case 'e':
913 rc = fdisk_sun_set_xcyl(cxt);
914 break;
915 case 'i':
916 rc = fdisk_sun_set_ilfact(cxt);
917 break;
918 case 'o':
919 rc = fdisk_sun_set_rspeed(cxt);
920 break;
921 case 'y':
922 rc = fdisk_sun_set_pcylcount(cxt);
923 break;
924 }
925 return rc;
926}
927
aae727f2
KZ
928static int sgi_menu_cb(struct fdisk_context **cxt0,
929 const struct menu *menu __attribute__((__unused__)),
930 const struct menu_entry *ent)
931{
932 struct fdisk_context *cxt = *cxt0;
933 int rc = -EINVAL;
934 size_t n = 0;
935
e6d0c4c1 936 DBG(MENU, ul_debug("enter SGI menu"));
aae727f2
KZ
937
938 assert(cxt);
939 assert(ent);
aa36c2cf 940 assert(fdisk_is_label(cxt, SGI));
aae727f2
KZ
941
942 if (ent->expert)
943 return rc;
944
945 switch (ent->key) {
946 case 'a':
947 rc = fdisk_ask_partnum(cxt, &n, FALSE);
948 if (!rc)
a1ef792f 949 rc = fdisk_toggle_partition_flag(cxt, n, SGI_FLAG_BOOT);
aae727f2
KZ
950 break;
951 case 'b':
ac84272d 952 fdisk_sgi_set_bootfile(cxt);
aae727f2
KZ
953 break;
954 case 'c':
955 rc = fdisk_ask_partnum(cxt, &n, FALSE);
956 if (!rc)
a1ef792f 957 rc = fdisk_toggle_partition_flag(cxt, n, SGI_FLAG_SWAP);
aae727f2
KZ
958 break;
959 case 'i':
ac84272d 960 rc = fdisk_sgi_create_info(cxt);
aae727f2
KZ
961 break;
962 }
963
964 return rc;
965}
966
b529ea2a
KZ
967/*
968 * This is fdisk frontend for BSD specific libfdisk functions that
9e930041 969 * are not exported by generic libfdisk API.
b529ea2a
KZ
970 */
971static int bsd_menu_cb(struct fdisk_context **cxt0,
972 const struct menu *menu __attribute__((__unused__)),
973 const struct menu_entry *ent)
974{
975 struct fdisk_context *cxt = *cxt0;
e563f055 976 int rc = 0, org;
b529ea2a
KZ
977
978 assert(cxt);
979 assert(ent);
aa36c2cf 980 assert(fdisk_is_label(cxt, BSD));
b529ea2a 981
e6d0c4c1 982 DBG(MENU, ul_debug("enter BSD menu"));
b529ea2a
KZ
983
984 switch(ent->key) {
985 case 'e':
986 rc = fdisk_bsd_edit_disklabel(cxt);
987 break;
988 case 'i':
989 rc = fdisk_bsd_write_bootstrap(cxt);
990 break;
991 case 's':
6a632136 992 org = fdisk_is_details(cxt);
e563f055 993
6a632136 994 fdisk_enable_details(cxt, 1);
9f670072 995 list_disklabel(cxt);
6a632136 996 fdisk_enable_details(cxt, org);
b529ea2a
KZ
997 break;
998 case 'x':
999 rc = fdisk_bsd_link_partition(cxt);
1000 break;
1001 }
1002 return rc;
1003}
1004
8e40a677 1005/* C/H/S commands */
a47fec81 1006static int geo_menu_cb(struct fdisk_context **cxt0,
8e40a677
KZ
1007 const struct menu *menu __attribute__((__unused__)),
1008 const struct menu_entry *ent)
1009{
a47fec81 1010 struct fdisk_context *cxt = *cxt0;
8e40a677
KZ
1011 int rc = -EINVAL;
1012 uintmax_t c = 0, h = 0, s = 0;
1013
e6d0c4c1 1014 DBG(MENU, ul_debug("enter GEO menu"));
a47fec81 1015
8e40a677
KZ
1016 assert(cxt);
1017 assert(ent);
1018
1019 switch (ent->key) {
1020 case 'c':
8eccde20 1021 rc = fdisk_ask_number(cxt, 1, fdisk_get_geom_cylinders(cxt),
8e40a677
KZ
1022 1048576, _("Number of cylinders"), &c);
1023 break;
1024 case 'h':
8eccde20 1025 rc = fdisk_ask_number(cxt, 1, fdisk_get_geom_heads(cxt),
8e40a677
KZ
1026 256, _("Number of heads"), &h);
1027 break;
1028 case 's':
8eccde20 1029 rc = fdisk_ask_number(cxt, 1, fdisk_get_geom_sectors(cxt),
8e40a677
KZ
1030 63, _("Number of sectors"), &s);
1031 break;
1032 }
1033
1034 if (!rc)
1035 fdisk_override_geometry(cxt, c, h, s);
1036 return rc;
1037}
1038
b3ac22ef
KZ
1039static int createlabel_menu_cb(struct fdisk_context **cxt0,
1040 const struct menu *menu __attribute__((__unused__)),
1041 const struct menu_entry *ent)
1042{
1043 struct fdisk_context *cxt = *cxt0;
1044 int rc = -EINVAL;
1045
e6d0c4c1 1046 DBG(MENU, ul_debug("enter Create label menu"));
b3ac22ef
KZ
1047
1048 assert(cxt);
1049 assert(ent);
1050
aae727f2
KZ
1051 if (ent->expert) {
1052 switch (ent->key) {
1053 case 'g':
1054 /* Deprecated, use 'G' in main menu, just for backward
1055 * compatibility only. */
1056 rc = fdisk_create_disklabel(cxt, "sgi");
1057 break;
1058 }
5635d195
KZ
1059 } else {
1060 switch (ent->key) {
1061 case 'g':
1062 rc = fdisk_create_disklabel(cxt, "gpt");
1063 break;
1064 case 'G':
1065 rc = fdisk_create_disklabel(cxt, "sgi");
1066 break;
1067 case 'o':
1068 rc = fdisk_create_disklabel(cxt, "dos");
1069 break;
1070 case 's':
1071 rc = fdisk_create_disklabel(cxt, "sun");
1072 break;
1073 }
aae727f2
KZ
1074 }
1075
5635d195
KZ
1076 if (rc == 0 && fdisk_get_collision(cxt))
1077 follow_wipe_mode(cxt);
1078
b3ac22ef
KZ
1079 return rc;
1080}