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