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