summaryrefslogtreecommitdiffstats
path: root/disk-utils/fdisk-menu.c
blob: a6f43515fb4dd03b28528d21df80d2c754930fc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>

#include "c.h"
#include "rpmatch.h"
#include "fdisk.h"
#include "pt-sun.h"
#include "pt-mbr.h"

struct menu_entry {
	const char	key;			/* command key */
	const char	*title;			/* help string */
	unsigned int	normal : 1,		/* normal mode */
			expert : 1,		/* expert mode */
			hidden : 1;		/* be sensitive for this key,
						   but don't print it in help */

	enum fdisk_labeltype	label;		/* only for this label */
	int                     exclude;    /* all labels except these */
	enum fdisk_labeltype	parent;		/* for nested PT */
};

#define IS_MENU_SEP(e)	((e)->key == '-')
#define IS_MENU_HID(e)	((e)->hidden)

struct menu {
	enum fdisk_labeltype	label;		/* only for this label */
	int                     exclude;    /* all labels except these */

	unsigned int		nonested : 1;	/* don't make this menu active in nested PT */

	int (*callback)(struct fdisk_context **,
			const struct menu *,
			const struct menu_entry *);

	struct menu_entry	entries[];	/* NULL terminated array */
};

struct menu_context {
	size_t		menu_idx;		/* the current menu */
	size_t		entry_idx;		/* index with in the current menu */
};

#define MENU_CXT_EMPTY	{ 0, 0 }
#define DECLARE_MENU_CB(x) \
	static int x(struct fdisk_context **, \
		     const struct menu *, \
		     const struct menu_entry *)

DECLARE_MENU_CB(gpt_menu_cb);
DECLARE_MENU_CB(sun_menu_cb);
DECLARE_MENU_CB(sgi_menu_cb);
DECLARE_MENU_CB(geo_menu_cb);
DECLARE_MENU_CB(dos_menu_cb);
DECLARE_MENU_CB(bsd_menu_cb);
DECLARE_MENU_CB(createlabel_menu_cb);
DECLARE_MENU_CB(generic_menu_cb);

/*
 * Menu entry macros:
 *	MENU_X*    expert mode only
 *      MENU_B*    both -- expert + normal mode
 *
 *      *_E  exclude this label
 *      *_H  hidden
 *      *_L  only for this label
 */

/* separator */
#define MENU_SEP(t)		{ .title = t, .key = '-', .normal = 1 }
#define MENU_XSEP(t)		{ .title = t, .key = '-', .expert = 1 }
#define MENU_BSEP(t)		{ .title = t, .key = '-', .expert = 1, .normal = 1 }

/* entry */
#define MENU_ENT(k, t)		{ .title = t, .key = k, .normal = 1 }
#define MENU_ENT_E(k, t, l)	{ .title = t, .key = k, .normal = 1, .exclude = l }
#define MENU_ENT_L(k, t, l)	{ .title = t, .key = k, .normal = 1, .label = l }

#define MENU_XENT(k, t)		{ .title = t, .key = k, .expert = 1 }
#define MENU_XENT_H(k, t)	{ .title = t, .key = k, .expert = 1, .hidden = 1 }

#define MENU_BENT(k, t)		{ .title = t, .key = k, .expert = 1, .normal = 1 }
#define MENU_BENT_E(k, t, l)	{ .title = t, .key = k, .expert = 1, .normal = 1, .exclude = l }

#define MENU_ENT_NEST(k, t, l, p)	{ .title = t, .key = k, .normal = 1, .label = l, .parent = p }
#define MENU_XENT_NEST(k, t, l, p)	{ .title = t, .key = k, .expert = 1, .label = l, .parent = p }
#define MENU_BENT_NEST(k, t, l, p)	{ .title = t, .key = k, .expert = 1, .normal = 1, .label = l, .parent = p }

/* Generic menu */
static const struct menu menu_generic = {
	.callback	= generic_menu_cb,
	.entries	= {
		MENU_BSEP(N_("Generic")),
		MENU_ENT  ('d', N_("delete a partition")),
		MENU_ENT  ('F', N_("list free unpartitioned space")),
		MENU_ENT  ('l', N_("list known partition types")),
		MENU_ENT  ('n', N_("add a new partition")),
		MENU_BENT ('p', N_("print the partition table")),
		MENU_ENT  ('t', N_("change a partition type")),
		MENU_BENT_E('v', N_("verify the partition table"), FDISK_DISKLABEL_BSD),
		MENU_ENT  ('i', N_("print information about a partition")),

		MENU_XENT('d', N_("print the raw data of the first sector from the device")),
		MENU_XENT('D', N_("print the raw data of the disklabel from the device")),
		MENU_XENT('f', N_("fix partitions order")),

		MENU_SEP(N_("Misc")),
		MENU_BENT ('m', N_("print this menu")),
		MENU_ENT_E('u', N_("change display/entry units"), FDISK_DISKLABEL_GPT),
		MENU_ENT_E('x', N_("extra functionality (experts only)"), FDISK_DISKLABEL_BSD),

		MENU_SEP(N_("Script")),
		MENU_ENT  ('I', N_("load disk layout from sfdisk script file")),
		MENU_ENT  ('O', N_("dump disk layout to sfdisk script file")),

		MENU_BSEP(N_("Save & Exit")),
		MENU_ENT_E('w', N_("write table to disk and exit"), FDISK_DISKLABEL_BSD),
		MENU_ENT_L('w', N_("write table to disk"), FDISK_DISKLABEL_BSD),
		MENU_BENT ('q', N_("quit without saving changes")),
		MENU_XENT ('r', N_("return to main menu")),

		MENU_ENT_NEST('r', N_("return from BSD to DOS"), FDISK_DISKLABEL_BSD, FDISK_DISKLABEL_DOS),

		{ 0, NULL }
	}
};

static const struct menu menu_createlabel = {
	.callback = createlabel_menu_cb,
	.exclude = FDISK_DISKLABEL_BSD,
	.nonested = 1,
	.entries = {
		MENU_SEP(N_("Create a new label")),
		MENU_ENT('g', N_("create a new empty GPT partition table")),
		MENU_ENT('G', N_("create a new empty SGI (IRIX) partition table")),
		MENU_ENT('o', N_("create a new empty DOS partition table")),
		MENU_ENT('s', N_("create a new empty Sun partition table")),

		/* backward compatibility -- be sensitive to 'g', but don't
		 * print it in the expert menu */
		MENU_XENT_H('g', N_("create an IRIX (SGI) partition table")),
		{ 0, NULL }
	}
};

static const struct menu menu_geo = {
	.callback = geo_menu_cb,
	.exclude = FDISK_DISKLABEL_GPT | FDISK_DISKLABEL_BSD,
	.entries = {
		MENU_XSEP(N_("Geometry (for the current label)")),
		MENU_XENT('c', N_("change number of cylinders")),
		MENU_XENT('h', N_("change number of heads")),
		MENU_XENT('s', N_("change number of sectors/track")),
		{ 0, NULL }
	}
};

static const struct menu menu_gpt = {
	.callback = gpt_menu_cb,
	.label = FDISK_DISKLABEL_GPT,
	.entries = {
		MENU_BSEP(N_("GPT")),
		MENU_XENT('i', N_("change disk GUID")),
		MENU_XENT('n', N_("change partition name")),
		MENU_XENT('u', N_("change partition UUID")),
		MENU_XENT('l', N_("change table length")),
		MENU_BENT('M', N_("enter protective/hybrid MBR")),

		MENU_XSEP(""),
		MENU_XENT('A', N_("toggle the legacy BIOS bootable flag")),
		MENU_XENT('B', N_("toggle the no block IO protocol flag")),
		MENU_XENT('R', N_("toggle the required partition flag")),
		MENU_XENT('S', N_("toggle the GUID specific bits")),

		{ 0, NULL }
	}
};

static const struct menu menu_sun = {
	.callback = sun_menu_cb,
	.label = FDISK_DISKLABEL_SUN,
	.entries = {
		MENU_BSEP(N_("Sun")),
		MENU_ENT('a', N_("toggle the read-only flag")),
		MENU_ENT('c', N_("toggle the mountable flag")),

		MENU_XENT('a', N_("change number of alternate cylinders")),
		MENU_XENT('e', N_("change number of extra sectors per cylinder")),
		MENU_XENT('i', N_("change interleave factor")),
		MENU_XENT('o', N_("change rotation speed (rpm)")),
		MENU_XENT('y', N_("change number of physical cylinders")),
		{ 0, NULL }
	}
};

static const struct menu menu_sgi = {
	.callback = sgi_menu_cb,
	.label = FDISK_DISKLABEL_SGI,
	.entries = {
		MENU_SEP(N_("SGI")),
		MENU_ENT('a', N_("select bootable partition")),
		MENU_ENT('b', N_("edit bootfile entry")),
		MENU_ENT('c', N_("select sgi swap partition")),
		MENU_ENT('i', N_("create SGI info")),
		{ 0, NULL }
	}
};

static const struct menu menu_dos = {
	.callback = dos_menu_cb,
	.label = FDISK_DISKLABEL_DOS,
	.entries = {
		MENU_BSEP(N_("DOS (MBR)")),
		MENU_ENT('a', N_("toggle a bootable flag")),
		MENU_ENT('b', N_("edit nested BSD disklabel")),
		MENU_ENT('c', N_("toggle the dos compatibility flag")),

		MENU_XENT('b', N_("move beginning of data in a partition")),
		MENU_XENT('i', N_("change the disk identifier")),

		MENU_BENT_NEST('M', N_("return from protective/hybrid MBR to GPT"),
					FDISK_DISKLABEL_DOS, FDISK_DISKLABEL_GPT),
		{ 0, NULL }
	}
};

static const struct menu menu_bsd = {
	.callback = bsd_menu_cb,
	.label = FDISK_DISKLABEL_BSD,
	.entries = {
		MENU_SEP(N_("BSD")),
		MENU_ENT('e', N_("edit drive data")),
		MENU_ENT('i', N_("install bootstrap")),
		MENU_ENT('s', N_("show complete disklabel")),
		MENU_ENT('x', N_("link BSD partition to non-BSD partition")),
		{ 0, NULL }
	}
};

static const struct menu *menus[] = {
	&menu_gpt,
	&menu_sun,
	&menu_sgi,
	&menu_dos,
	&menu_bsd,
	&menu_geo,
	&menu_generic,
	&menu_createlabel,
};

static const struct menu_entry *next_menu_entry(
			struct fdisk_context *cxt,
			struct menu_context *mc)
{
	struct fdisk_label *lb = fdisk_get_label(cxt, NULL);
	struct fdisk_context *parent = fdisk_get_parent(cxt);
	unsigned int type = 0, pr_type = 0;

	assert(cxt);

	if (lb)
		type = fdisk_label_get_type(lb);
	if (parent)
		pr_type = fdisk_label_get_type(fdisk_get_label(parent, NULL));

	while (mc->menu_idx < ARRAY_SIZE(menus)) {
		const struct menu *m = menus[mc->menu_idx];
		const struct menu_entry *e = &(m->entries[mc->entry_idx]);

		/*
		 * whole-menu filter
		 */

		/* no more entries */
		if (e->title == NULL ||
		/* menu wanted for specified labels only */
		    (m->label && lb && !(m->label & type)) ||
		/* unwanted for nested PT */
		    (m->nonested && parent) ||
		/* menu excluded for specified labels */
		    (m->exclude && lb && (m->exclude & type))) {
			mc->menu_idx++;
			mc->entry_idx = 0;
			continue;
		}

		/*
		 * per entry filter
		 */

		/* excluded for the current label */
		if ((e->exclude && lb && e->exclude & type) ||
		/* entry wanted for specified labels only */
		    (e->label && lb && !(e->label & type)) ||
		/* exclude non-expert entries in expect mode */
		    (e->expert == 0 && fdisk_is_details(cxt)) ||
		/* nested only */
		    (e->parent && (!parent || pr_type != e->parent)) ||
		/* exclude non-normal entries in normal mode */
		    (e->normal == 0 && !fdisk_is_details(cxt))) {
			mc->entry_idx++;
			continue;
		}
		mc->entry_idx++;
		return e;

	}
	return NULL;
}

/* returns @menu and menu entry for then @key */
static const struct menu_entry *get_fdisk_menu_entry(
		struct fdisk_context *cxt,
		int key,
		const struct menu **menu)
{
	struct menu_context mc = MENU_CXT_EMPTY;
	const struct menu_entry *e;

	while ((e = next_menu_entry(cxt, &mc))) {
		if (IS_MENU_SEP(e) || e->key != key)
			continue;

		if (menu)
			*menu = menus[mc.menu_idx];
		return e;
	}

	return NULL;
}

static int menu_detect_collisions(struct fdisk_context *cxt)
{
	struct menu_context mc = MENU_CXT_EMPTY;
	const struct menu_entry *e, *r;

	while ((e = next_menu_entry(cxt, &mc))) {
		if (IS_MENU_SEP(e))
			continue;

		r = get_fdisk_menu_entry(cxt, e->key, NULL);
		if (!r) {
			DBG(MENU, ul_debug("warning: not found "
					"entry for %c", e->key));
			return -1;
		}
		if (r != e) {
			DBG(MENU, ul_debug("warning: duplicate key '%c'",
						e->key));
			DBG(MENU, ul_debug("       : %s", e->title));
			DBG(MENU, ul_debug("       : %s", r->title));
			abort();
		}
	}

	return 0;
}

static int print_fdisk_menu(struct fdisk_context *cxt)
{
	struct menu_context mc = MENU_CXT_EMPTY;
	const struct menu_entry *e;

	ON_DBG(MENU, menu_detect_collisions(cxt));

	if (fdisk_is_details(cxt))
		printf(_("\nHelp (expert commands):\n"));
	else
		printf(_("\nHelp:\n"));

	while ((e = next_menu_entry(cxt, &mc))) {
		if (IS_MENU_HID(e))
			continue;	/* hidden entry */
		if (IS_MENU_SEP(e) && (!e->title || !*e->title))
			printf("\n");
		else if (IS_MENU_SEP(e)) {
			color_scheme_enable("help-title", UL_COLOR_BOLD);
			printf("\n  %s\n", _(e->title));
			color_disable();
		} else
			printf("   %c   %s\n", e->key, _(e->title));
	}
	fputc('\n', stdout);

	if (fdisk_get_parent(cxt)) {
		struct fdisk_label *l = fdisk_get_label(cxt, NULL),
				   *p = fdisk_get_label(fdisk_get_parent(cxt), NULL);

		fdisk_info(cxt, _("You're editing nested '%s' partition table, "
				  "primary partition table is '%s'."),
				fdisk_label_get_name(l),
				fdisk_label_get_name(p));
	}

	return 0;
}

/* Asks for command, verify the key and perform the command or
 * returns the command key if no callback for the command is
 * implemented.
 *
 * Note that this function might exchange the context pointer to
 * switch to another (nested) context.
 *
 * Returns: <0 on error
 *           0 on success (the command performed)
 *          >0 if no callback (then returns the key)
 */
int process_fdisk_menu(struct fdisk_context **cxt0)
{
	struct fdisk_context *cxt = *cxt0;
	const struct menu_entry *ent;
	const struct menu *menu;
	int key, rc;
	const char *prompt;
	char buf[BUFSIZ];

	if (fdisk_is_details(cxt))
		prompt = _("Expert command (m for help): ");
	else
		prompt = _("Command (m for help): ");

	fputc('\n',stdout);
	rc = get_user_reply(prompt, buf, sizeof(buf));

	if (rc == -ECANCELED) {
		/* Map ^C and ^D in main menu to 'q' */
		if (is_interactive
		    && fdisk_label_is_changed(fdisk_get_label(cxt, NULL))) {
			rc = get_user_reply(
				_("\nDo you really want to quit? "),
				buf, sizeof(buf));
			if (rc || !rpmatch(buf))
				return 0;
		}
		key = 'q';
	} else if (rc) {
		return rc;
	} else
		key = buf[0];

	ent = get_fdisk_menu_entry(cxt, key, &menu);
	if (!ent) {
		fdisk_warnx(cxt, _("%c: unknown command"), key);
		return -EINVAL;
	}

	DBG(MENU, ul_debug("selected: key=%c, entry='%s'",
				key, ent->title));

	/* menu has implemented callback, use it */
	if (menu->callback)
		rc = menu->callback(cxt0, menu, ent);
	else {
		DBG(MENU, ul_debug("no callback for key '%c'", key));
		rc = -EINVAL;
	}

	DBG(MENU, ul_debug("process menu done [rc=%d]", rc));
	return rc;
}

static int script_read(struct fdisk_context *cxt)
{
	struct fdisk_script *sc = NULL;
	char *filename = NULL;
	int rc;

	rc = fdisk_ask_string(cxt, _("Enter script file name"), &filename);
	if (rc)
		return rc;

	errno = 0;
	sc = fdisk_new_script_from_file(cxt, filename);
	if (!sc && errno)
		fdisk_warn(cxt, _("Cannot open %s"), filename);
	else if (!sc)
		fdisk_warnx(cxt, _("Failed to parse script file %s"), filename);
	else if (fdisk_apply_script(cxt, sc) != 0) {
		fdisk_warnx(cxt, _("Failed to apply script %s"), filename);
		fdisk_warnx(cxt, _("Resetting fdisk!"));
		rc = fdisk_reassign_device(cxt);
                if (rc == 0 && !fdisk_has_label(cxt)) {
                        fdisk_info(cxt, _("Device does not contain a recognized partition table."));
                        fdisk_create_disklabel(cxt, NULL);
		}
	} else
		fdisk_info(cxt, _("Script successfully applied."));

	fdisk_unref_script(sc);
	free(filename);
	return rc;
}

static int script_write(struct fdisk_context *cxt)
{
	struct fdisk_script *sc = NULL;
	char *filename = NULL;
	FILE *f = NULL;
	int rc;

	rc = fdisk_ask_string(cxt, _("Enter script file name"), &filename);
	if (rc)
		return rc;

	sc = fdisk_new_script(cxt);
	if (!sc) {
		fdisk_warn(cxt, _("Failed to allocate script handler"));
		goto done;
	}

	rc = fdisk_script_read_context(sc, NULL);
	if (rc) {
		fdisk_warnx(cxt, _("Failed to transform disk layout into script"));
		goto done;
	}

	f = fopen(filename, "w");
	if (!f) {
		fdisk_warn(cxt, _("Cannot open %s"), filename);
		goto done;
	}

	rc = fdisk_script_write_file(sc, f);
	if (rc)
		fdisk_warn(cxt, _("Failed to write script %s"), filename);
	else
		fdisk_info(cxt, _("Script successfully saved."));
done:
	if (f)
		fclose(f);
	fdisk_unref_script(sc);
	free(filename);
	return rc;
}

static int ask_for_wipe(struct fdisk_context *cxt, size_t partno)
{
	struct fdisk_partition *tmp = NULL;
	char *fstype = NULL;
	int rc, yes = 0;

	rc = fdisk_get_partition(cxt, partno, &tmp);
	if (rc)
		goto done;

	rc = fdisk_partition_to_string(tmp, cxt, FDISK_FIELD_FSTYPE, &fstype);
	if (rc || fstype == NULL)
		goto done;

	fdisk_warnx(cxt, _("Partition #%zu contains a %s signature."), partno + 1, fstype);

	if (pwipemode == WIPEMODE_AUTO && isatty(STDIN_FILENO))
		fdisk_ask_yesno(cxt, _("Do you want to remove the signature?"), &yes);
	else if (pwipemode == WIPEMODE_ALWAYS)
		yes = 1;

	if (yes) {
		fdisk_info(cxt, _("The signature will be removed by a write command."));
		rc = fdisk_wipe_partition(cxt, partno, TRUE);
	}
done:
	fdisk_unref_partition(tmp);
	free(fstype);
	return rc;
}

/*
 * Basic fdisk actions
 */
static int generic_menu_cb(struct fdisk_context **cxt0,
		       const struct menu *menu __attribute__((__unused__)),
		       const struct menu_entry *ent)
{
	struct fdisk_context *cxt = *cxt0;
	int rc = 0;
	size_t n;

	/* actions shared between expert and normal mode */
	switch (ent->key) {
	case 'p':
		list_disk_geometry(cxt);
		list_disklabel(cxt);
		break;
	case 'w':
		if (fdisk_is_readonly(cxt)) {
			fdisk_warnx(cxt, _("Device is open in read-only mode."));
			break;
		}
		rc = fdisk_write_disklabel(cxt);
		if (rc)
			err(EXIT_FAILURE, _("failed to write disklabel"));
		if (fdisk_get_parent(cxt))
			break; /* nested PT, don't leave */
		fdisk_info(cxt, _("The partition table has been altered."));

		if (device_is_used)
			rc = fdisk_reread_changes(cxt, original_layout);
		else
			rc = fdisk_reread_partition_table(cxt);
		if (!rc)
			rc = fdisk_deassign_device(cxt, 0);
		/* fallthrough */
	case 'q':
		fdisk_unref_context(cxt);
		fputc('\n', stdout);
		exit(rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
	case 'm':
		rc = print_fdisk_menu(cxt);
		break;
	case 'v':
		rc = fdisk_verify_disklabel(cxt);
		break;
	case 'i':
		rc = print_partition_info(cxt);
		break;
	case 'F':
		list_freespace(cxt);
		break;
	}

	/* expert mode */
	if (ent->expert) {
		switch (ent->key) {
		case 'd':
			dump_firstsector(cxt);
			break;
		case 'D':
			dump_disklabel(cxt);
			break;
		case 'f':
			rc = fdisk_reorder_partitions(cxt);
			if (rc)
				fdisk_warnx(cxt, _("Failed to fix partitions order."));
			else
				fdisk_info(cxt, _("Partitions order fixed."));
			break;
		case 'r':
			rc = fdisk_enable_details(cxt, 0);
			break;
		}
		return rc;
	}

	/* normal mode */
	switch (ent->key) {
	case 'd':
		rc = fdisk_ask_partnum(cxt, &n, FALSE);
		if (rc)
			break; /* no partitions yet (or ENOMEM, ...) */

		rc = fdisk_delete_partition(cxt, n);
		if (rc)
			fdisk_warnx(cxt, _("Could not delete partition %zu"), n + 1);
		else
			fdisk_info(cxt, _("Partition %zu has been deleted."), n + 1);
		break;
	case 'I':
		script_read(cxt);
		break;
	case 'O':
		script_write(cxt);
		break;
	case 'l':
		list_partition_types(cxt);
		break;
	case 'n':
	{
		size_t partno;
		rc = fdisk_add_partition(cxt, NULL, &partno);
		if (!rc)
			rc = ask_for_wipe(cxt, partno);
		break;
	}
	case 't':
		change_partition_type(cxt);
		break;
	case 'u':
		fdisk_set_unit(cxt,
			fdisk_use_cylinders(cxt) ? "sectors" :
							   "cylinders");
		if (fdisk_use_cylinders(cxt))
			fdisk_info(cxt, _("Changing display/entry units to cylinders (DEPRECATED!)."));
		else
			fdisk_info(cxt, _("Changing display/entry units to sectors."));
		break;
	case 'x':
		fdisk_enable_details(cxt, 1);
		break;
	case 'r':
		/* return from nested BSD to DOS */
		if (fdisk_get_parent(cxt)) {
			*cxt0 = fdisk_get_parent(cxt);

			fdisk_info(cxt, _("Leaving nested disklabel."));
			fdisk_unref_context(cxt);
		}
		break;
	}

	return rc;
}


/*
 * This is fdisk frontend for GPT specific libfdisk functions that
 * are not exported by generic libfdisk API.
 */
static int gpt_menu_cb(struct fdisk_context **cxt0,
		       const struct menu *menu __attribute__((__unused__)),
		       const struct menu_entry *ent)
{
	struct fdisk_context *cxt = *cxt0;
	struct fdisk_context *mbr;
	struct fdisk_partition *pa = NULL;
	size_t n;
	int rc = 0;
	uintmax_t length = 0;

	assert(cxt);
	assert(ent);
	assert(fdisk_is_label(cxt, GPT));

	DBG(MENU, ul_debug("enter GPT menu"));

	if (ent->expert) {
		switch (ent->key) {
		case 'i':
			return fdisk_set_disklabel_id(cxt);
		case 'l':
	                rc =  fdisk_ask_number(cxt, 1, fdisk_get_npartitions(cxt),
	                                ~(uint32_t)0, _("New maximum entries"), &length);
			if (rc)
				return rc;
			return fdisk_gpt_set_npartitions(cxt, (uint32_t) length);
		case 'M':
			mbr = fdisk_new_nested_context(cxt, "dos");
			if (!mbr)
				return -ENOMEM;
			*cxt0 = cxt = mbr;
			if (fdisk_is_details(cxt))
				fdisk_enable_details(cxt, 1);	/* keep us in expert mode */
			fdisk_info(cxt, _("Entering protective/hybrid MBR disklabel."));
			return 0;
		}

		/* actions where is necessary partnum */
		rc = fdisk_ask_partnum(cxt, &n, FALSE);
		if (rc)
			return rc;

		switch(ent->key) {
		case 'u':
			pa = fdisk_new_partition();	/* new template */
			if (!pa)
				rc = -ENOMEM;
			else {
				char *str = NULL;
				rc = fdisk_ask_string(cxt, _("New UUID (in 8-4-4-4-12 format)"), &str);
				if (!rc)
					rc = fdisk_partition_set_uuid(pa, str);
				if (!rc)
					rc = fdisk_set_partition(cxt, n, pa);
				free(str);
				fdisk_unref_partition(pa);
			}
			break;
		case 'n':
			pa = fdisk_new_partition();	/* new template */
			if (!pa)
				rc = -ENOMEM;
			else {
				char *str = NULL;
				rc = fdisk_ask_string(cxt, _("New name"), &str);
				if (!rc)
					rc = fdisk_partition_set_name(pa, str);
				if (!rc)
					rc = fdisk_set_partition(cxt, n, pa);
				free(str);
				fdisk_unref_partition(pa);
			}
			break;
		case 'A':
			rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_LEGACYBOOT);
			break;
		case 'B':
			rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_NOBLOCK);
			break;
		case 'R':
			rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_REQUIRED);
			break;
		case 'S':
			rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_GUIDSPECIFIC);
			break;
		}
	}

	return rc;
}


/*
 * This is fdisk frontend for MBR specific libfdisk functions that
 * are not exported by generic libfdisk API.
 */
static int dos_menu_cb(struct fdisk_context **cxt0,
		       const struct menu *menu __attribute__((__unused__)),
		       const struct menu_entry *ent)
{
	struct fdisk_context *cxt = *cxt0;
	int rc = 0;

	DBG(MENU, ul_debug("enter DOS menu"));

	if (!ent->expert) {
		switch (ent->key) {
		case 'a':
		{
			size_t n;
			rc = fdisk_ask_partnum(cxt, &n, FALSE);
			if (!rc)
				rc = fdisk_toggle_partition_flag(cxt, n, DOS_FLAG_ACTIVE);
			break;
		}
		case 'b':
		{
			struct fdisk_context *bsd
					= fdisk_new_nested_context(cxt, "bsd");
			if (!bsd)
				return -ENOMEM;
			if (!fdisk_has_label(bsd))
				rc = fdisk_create_disklabel(bsd, "bsd");
			if (rc)
				fdisk_unref_context(bsd);
			else {
				*cxt0 = cxt = bsd;
				fdisk_info(cxt,	_("Entering nested BSD disklabel."));
			}
			break;
		}
		case 'c':
			toggle_dos_compatibility_flag(cxt);
			break;
		}
		return rc;
	}

	/* expert mode */
	switch (ent->key) {
	case 'b':
	{
		size_t n;
		rc = fdisk_ask_partnum(cxt, &n, FALSE);
		if (!rc)
			rc = fdisk_dos_move_begin(cxt, n);
		break;
	}
	case 'i':
		rc = fdisk_set_disklabel_id(cxt);
		break;
	case 'M':
		/* return from nested MBR to GPT */
		if (fdisk_get_parent(cxt)) {
			*cxt0 = fdisk_get_parent(cxt);

			fdisk_info(cxt, _("Leaving nested disklabel."));
			fdisk_unref_context(cxt);
		}
		break;
	}
	return rc;
}

static int sun_menu_cb(struct fdisk_context **cxt0,
		       const struct menu *menu __attribute__((__unused__)),
		       const struct menu_entry *ent)
{
	struct fdisk_context *cxt = *cxt0;
	int rc = 0;

	DBG(MENU, ul_debug("enter SUN menu"));

	assert(cxt);
	assert(ent);
	assert(fdisk_is_label(cxt, SUN));

	DBG(MENU, ul_debug("enter SUN menu"));

	/* normal mode */
	if (!ent->expert) {
		size_t n;

		rc = fdisk_ask_partnum(cxt, &n, FALSE);
		if (rc)
			return rc;
		switch (ent->key) {
		case 'a':
			rc = fdisk_toggle_partition_flag(cxt, n, SUN_FLAG_RONLY);
			break;
		case 'c':
			rc = fdisk_toggle_partition_flag(cxt, n, SUN_FLAG_UNMNT);
			break;
		}
		return rc;
	}

	/* expert mode */
	switch (ent->key) {
	case 'a':
		rc = fdisk_sun_set_alt_cyl(cxt);
		break;
	case 'e':
		rc = fdisk_sun_set_xcyl(cxt);
		break;
	case 'i':
		rc = fdisk_sun_set_ilfact(cxt);
		break;
	case 'o':
		rc = fdisk_sun_set_rspeed(cxt);
		break;
	case 'y':
		rc = fdisk_sun_set_pcylcount(cxt);
		break;
	}
	return rc;
}

static int sgi_menu_cb(struct fdisk_context **cxt0,
		       const struct menu *menu __attribute__((__unused__)),
		       const struct menu_entry *ent)
{
	struct fdisk_context *cxt = *cxt0;
	int rc = -EINVAL;
	size_t n = 0;

	DBG(MENU, ul_debug("enter SGI menu"));

	assert(cxt);
	assert(ent);
	assert(fdisk_is_label(cxt, SGI));

	if (ent->expert)
		return rc;

	switch (ent->key) {
	case 'a':
		rc = fdisk_ask_partnum(cxt, &n, FALSE);
		if (!rc)
			rc = fdisk_toggle_partition_flag(cxt, n, SGI_FLAG_BOOT);
		break;
	case 'b':
		fdisk_sgi_set_bootfile(cxt);
		break;
	case 'c':
		rc = fdisk_ask_partnum(cxt, &n, FALSE);
		if (!rc)
			rc = fdisk_toggle_partition_flag(cxt, n, SGI_FLAG_SWAP);
		break;
	case 'i':
		rc = fdisk_sgi_create_info(cxt);
		break;
	}

	return rc;
}

/*
 * This is fdisk frontend for BSD specific libfdisk functions that
 * are not exported by generic libfdisk API.
 */
static int bsd_menu_cb(struct fdisk_context **cxt0,
		       const struct menu *menu __attribute__((__unused__)),
		       const struct menu_entry *ent)
{
	struct fdisk_context *cxt = *cxt0;
	int rc = 0, org;

	assert(cxt);
	assert(ent);
	assert(fdisk_is_label(cxt, BSD));

	DBG(MENU, ul_debug("enter BSD menu"));

	switch(ent->key) {
	case 'e':
		rc = fdisk_bsd_edit_disklabel(cxt);
		break;
	case 'i':
		rc = fdisk_bsd_write_bootstrap(cxt);
		break;
	case 's':
		org = fdisk_is_details(cxt);

		fdisk_enable_details(cxt, 1);
		list_disklabel(cxt);
		fdisk_enable_details(cxt, org);
		break;
	case 'x':
		rc = fdisk_bsd_link_partition(cxt);
		break;
	}
	return rc;
}

/* C/H/S commands
 *
 * The geometry setting from this dialog is not persistent and maybe reset by
 * fdisk_reset_device_properties() (for example when you create a new disk
 * label). Note that on command line specified -C/-H/-S setting is persistent
 * as it's based on fdisk_save_user_geometry().
 */
static int geo_menu_cb(struct fdisk_context **cxt0,
		       const struct menu *menu __attribute__((__unused__)),
		       const struct menu_entry *ent)
{
	struct fdisk_context *cxt = *cxt0;
	struct fdisk_label *lb = fdisk_get_label(cxt, NULL);
	int rc = -EINVAL;
	uintmax_t c = 0, h = 0, s = 0;
	fdisk_sector_t mi, ma;

	DBG(MENU, ul_debug("enter GEO menu"));

	assert(cxt);
	assert(ent);

	/* default */
	if (!lb)
		lb = fdisk_get_label(cxt, "dos");

	switch (ent->key) {
	case 'c':
		fdisk_label_get_geomrange_cylinders(lb, &mi, &ma);
		rc =  fdisk_ask_number(cxt, mi, fdisk_get_geom_cylinders(cxt),
				ma, _("Number of cylinders"), &c);
		break;
	case 'h':
	{
		unsigned int i, a;
		fdisk_label_get_geomrange_heads(lb, &i, &a);
		rc =  fdisk_ask_number(cxt, i, fdisk_get_geom_heads(cxt),
				a, _("Number of heads"), &h);
		break;
	}
	case 's':
		fdisk_label_get_geomrange_sectors(lb, &mi, &ma);
		rc =  fdisk_ask_number(cxt, mi, fdisk_get_geom_sectors(cxt),
				ma, _("Number of sectors"), &s);
		break;
	}

	if (!rc)
		fdisk_override_geometry(cxt, c, h, s);
	return rc;
}

static int createlabel_menu_cb(struct fdisk_context **cxt0,
		       const struct menu *menu __attribute__((__unused__)),
		       const struct menu_entry *ent)
{
	struct fdisk_context *cxt = *cxt0;
	int rc = -EINVAL;

	DBG(MENU, ul_debug("enter Create label menu"));

	assert(cxt);
	assert(ent);

	if (ent->expert) {
		switch (ent->key) {
		case 'g':
			/* Deprecated, use 'G' in main menu, just for backward
			 * compatibility only. */
			rc = fdisk_create_disklabel(cxt, "sgi");
			break;
		}
	} else {
		switch (ent->key) {
			case 'g':
				rc = fdisk_create_disklabel(cxt, "gpt");
				break;
			case 'G':
				rc = fdisk_create_disklabel(cxt, "sgi");
				break;
			case 'o':
				rc = fdisk_create_disklabel(cxt, "dos");
				break;
			case 's':
				rc = fdisk_create_disklabel(cxt, "sun");
				break;
		}
	}

	if (rc == 0 && fdisk_get_collision(cxt))
		follow_wipe_mode(cxt);

	return rc;
}