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