#include <stdint.h>
#include "c.h"
+#include "rpmatch.h"
#include "fdisk.h"
#include "pt-sun.h"
#include "pt-mbr.h"
but don't print it in help */
enum fdisk_labeltype label; /* only for this label */
- enum fdisk_labeltype exclude; /* all labels except this */
+ int exclude; /* all labels except these */
enum fdisk_labeltype parent; /* for nested PT */
};
struct menu {
enum fdisk_labeltype label; /* only for this label */
- enum fdisk_labeltype exclude; /* all labels except this */
+ int exclude; /* all labels except these */
unsigned int nonested : 1; /* don't make this menu active in nested PT */
#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 */
-struct menu menu_generic = {
+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")),
}
};
-struct menu menu_createlabel = {
+static const struct menu menu_createlabel = {
.callback = createlabel_menu_cb,
.exclude = FDISK_DISKLABEL_BSD,
.nonested = 1,
}
};
-struct menu menu_geo = {
+static const struct menu menu_geo = {
.callback = geo_menu_cb,
.exclude = FDISK_DISKLABEL_GPT | FDISK_DISKLABEL_BSD,
.entries = {
- MENU_XSEP(N_("Geometry")),
+ 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")),
}
};
-struct menu menu_gpt = {
+static const struct menu menu_gpt = {
.callback = gpt_menu_cb,
.label = FDISK_DISKLABEL_GPT,
.entries = {
- MENU_XSEP(N_("GPT")),
+ 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('M', N_("enter protective/hybrid MBR")),
+ 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")),
}
};
-struct menu menu_sun = {
+static const struct menu menu_sun = {
.callback = sun_menu_cb,
.label = FDISK_DISKLABEL_SUN,
.entries = {
}
};
-struct menu menu_sgi = {
+static const struct menu menu_sgi = {
.callback = sgi_menu_cb,
.label = FDISK_DISKLABEL_SGI,
.entries = {
}
};
-struct menu menu_dos = {
+static const struct menu menu_dos = {
.callback = dos_menu_cb,
.label = FDISK_DISKLABEL_DOS,
.entries = {
MENU_XENT('b', N_("move beginning of data in a partition")),
MENU_XENT('i', N_("change the disk identifier")),
- MENU_XENT_NEST('M', N_("return from protective/hybrid MBR to GPT"),
+ MENU_BENT_NEST('M', N_("return from protective/hybrid MBR to GPT"),
FDISK_DISKLABEL_DOS, FDISK_DISKLABEL_GPT),
{ 0, NULL }
}
};
-struct menu menu_bsd = {
+static const struct menu menu_bsd = {
.callback = bsd_menu_cb,
.label = FDISK_DISKLABEL_BSD,
.entries = {
const struct menu *menu;
int key, rc;
const char *prompt;
- char buf[BUFSIZ];
+ char buf[BUFSIZ] = { '\0' };
if (fdisk_is_details(cxt))
prompt = _("Expert command (m for help): ");
prompt = _("Command (m for help): ");
fputc('\n',stdout);
- rc = get_user_reply(cxt, prompt, buf, sizeof(buf));
- if (rc)
+ 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];
- key = buf[0];
ent = get_fdisk_menu_entry(cxt, key, &menu);
if (!ent) {
fdisk_warnx(cxt, _("%c: unknown command"), key);
return -EINVAL;
}
- rc = 0;
DBG(MENU, ul_debug("selected: key=%c, entry='%s'",
key, ent->title));
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)
+ else if (fdisk_apply_script(cxt, sc) != 0) {
fdisk_warnx(cxt, _("Failed to apply script %s"), filename);
- else
+ 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);
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
*/
break;
case 'w':
if (fdisk_is_readonly(cxt)) {
- fdisk_warnx(cxt, _("Device open in read-only mode."));
+ fdisk_warnx(cxt, _("Device is open in read-only mode."));
break;
}
rc = fdisk_write_disklabel(cxt);
if (fdisk_get_parent(cxt))
break; /* nested PT, don't leave */
fdisk_info(cxt, _("The partition table has been altered."));
- rc = fdisk_reread_partition_table(cxt);
+
+ 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 'i':
rc = print_partition_info(cxt);
break;
+ case 'F':
+ list_freespace(cxt);
+ break;
}
/* expert mode */
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);
switch (ent->key) {
case 'd':
rc = fdisk_ask_partnum(cxt, &n, FALSE);
- if (!rc)
- rc = fdisk_delete_partition(cxt, n);
+ 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
list_partition_types(cxt);
break;
case 'n':
- rc = fdisk_add_partition(cxt, NULL, NULL);
+ {
+ 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;
fdisk_info(cxt, _("Leaving nested disklabel."));
fdisk_unref_context(cxt);
- cxt = *cxt0;
}
break;
}
/*
* This is fdisk frontend for GPT specific libfdisk functions that
- * are not expported by generic libfdisk API.
+ * are not exported by generic libfdisk API.
*/
static int gpt_menu_cb(struct fdisk_context **cxt0,
const struct menu *menu __attribute__((__unused__)),
struct fdisk_partition *pa = NULL;
size_t n;
int rc = 0;
+ uintmax_t length = 0;
assert(cxt);
assert(ent);
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;
- fdisk_enable_details(cxt, 1); /* keep us in expert mode */
+ 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;
}
/*
* This is fdisk frontend for MBR specific libfdisk functions that
- * are not expported by generic libfdisk API.
+ * are not exported by generic libfdisk API.
*/
static int dos_menu_cb(struct fdisk_context **cxt0,
const struct menu *menu __attribute__((__unused__)),
fdisk_info(cxt, _("Leaving nested disklabel."));
fdisk_unref_context(cxt);
- cxt = *cxt0;
}
break;
}
/*
* This is fdisk frontend for BSD specific libfdisk functions that
- * are not expported by generic libfdisk API.
+ * are not exported by generic libfdisk API.
*/
static int bsd_menu_cb(struct fdisk_context **cxt0,
const struct menu *menu __attribute__((__unused__)),
return rc;
}
-/* C/H/S commands */
+/* 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':
- rc = fdisk_ask_number(cxt, 1, fdisk_get_geom_cylinders(cxt),
- 1048576, _("Number of cylinders"), &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':
- rc = fdisk_ask_number(cxt, 1, fdisk_get_geom_heads(cxt),
- 256, _("Number of heads"), &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':
- rc = fdisk_ask_number(cxt, 1, fdisk_get_geom_sectors(cxt),
- 63, _("Number of sectors"), &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;
}
rc = fdisk_create_disklabel(cxt, "sgi");
break;
}
- return rc;
+ } 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;
+ }
}
- switch (ent->key) {
- case 'g':
- fdisk_create_disklabel(cxt, "gpt");
- break;
- case 'G':
- fdisk_create_disklabel(cxt, "sgi");
- break;
- case 'o':
- fdisk_create_disklabel(cxt, "dos");
- break;
- case 's':
- fdisk_create_disklabel(cxt, "sun");
- break;
- }
+ if (rc == 0 && fdisk_get_collision(cxt))
+ follow_wipe_mode(cxt);
+
return rc;
}