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