]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - disk-utils/fdisk-menu.c
wipefs: add --lock and LOCK_BLOCK_DEVICE
[thirdparty/util-linux.git] / disk-utils / fdisk-menu.c
index 64e7ba1446593e37c97870e49cb8ff530b847459..51ae7b5e6e912eeefeab203bd83c76ae530232b0 100644 (file)
@@ -6,6 +6,7 @@
 #include <stdint.h>
 
 #include "c.h"
+#include "rpmatch.h"
 #include "fdisk.h"
 #include "pt-sun.h"
 #include "pt-mbr.h"
@@ -19,7 +20,7 @@ struct menu_entry {
                                                   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 */
 };
 
@@ -28,7 +29,7 @@ struct menu_entry {
 
 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 */
 
@@ -86,19 +87,21 @@ DECLARE_MENU_CB(generic_menu_cb);
 #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_H(k, t, l, p)   { .title = t, .key = k, .expert = 1, .normal = 1, .label = l, .parent = p, .hidden = 1 }
 
 /* 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")),
                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")),
@@ -121,11 +124,13 @@ struct menu menu_generic = {
 
                MENU_ENT_NEST('r', N_("return from BSD to DOS"), FDISK_DISKLABEL_BSD, FDISK_DISKLABEL_DOS),
 
+               MENU_ENT_NEST('r', N_("return from protective/hybrid MBR to GPT"), FDISK_DISKLABEL_DOS, FDISK_DISKLABEL_GPT),
+
                { 0, NULL }
        }
 };
 
-struct menu menu_createlabel = {
+static const struct menu menu_createlabel = {
        .callback = createlabel_menu_cb,
        .exclude = FDISK_DISKLABEL_BSD,
        .nonested = 1,
@@ -143,11 +148,11 @@ struct menu menu_createlabel = {
        }
 };
 
-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")),
@@ -155,15 +160,16 @@ struct menu menu_geo = {
        }
 };
 
-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")),
@@ -175,7 +181,7 @@ struct menu menu_gpt = {
        }
 };
 
-struct menu menu_sun = {
+static const struct menu menu_sun = {
        .callback = sun_menu_cb,
        .label = FDISK_DISKLABEL_SUN,
        .entries = {
@@ -192,7 +198,7 @@ struct menu menu_sun = {
        }
 };
 
-struct menu menu_sgi = {
+static const struct menu menu_sgi = {
        .callback = sgi_menu_cb,
        .label = FDISK_DISKLABEL_SGI,
        .entries = {
@@ -205,7 +211,7 @@ struct menu menu_sgi = {
        }
 };
 
-struct menu menu_dos = {
+static const struct menu menu_dos = {
        .callback = dos_menu_cb,
        .label = FDISK_DISKLABEL_DOS,
        .entries = {
@@ -217,13 +223,13 @@ struct menu menu_dos = {
                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"),
-                                       FDISK_DISKLABEL_DOS, FDISK_DISKLABEL_GPT),
+               MENU_BENT_NEST_H('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 = {
@@ -257,7 +263,8 @@ static const struct menu_entry *next_menu_entry(
 
        assert(cxt);
 
-       type = fdisk_label_get_type(lb);
+       if (lb)
+               type = fdisk_label_get_type(lb);
        if (parent)
                pr_type = fdisk_label_get_type(fdisk_get_label(parent, NULL));
 
@@ -272,7 +279,7 @@ static const struct menu_entry *next_menu_entry(
                /* no more entries */
                if (e->title == NULL ||
                /* menu wanted for specified labels only */
-                   (m->label && lb && !(m->label & type)) ||
+                   (m->label && (!lb || !(m->label & type))) ||
                /* unwanted for nested PT */
                    (m->nonested && parent) ||
                /* menu excluded for specified labels */
@@ -289,7 +296,7 @@ static const struct menu_entry *next_menu_entry(
                /* excluded for the current label */
                if ((e->exclude && lb && e->exclude & type) ||
                /* entry wanted for specified labels only */
-                   (e->label && lb && !(e->label & type)) ||
+                   (e->label && (!lb || !(e->label & type))) ||
                /* exclude non-expert entries in expect mode */
                    (e->expert == 0 && fdisk_is_details(cxt)) ||
                /* nested only */
@@ -411,7 +418,7 @@ int process_fdisk_menu(struct fdisk_context **cxt0)
        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): ");
@@ -419,18 +426,30 @@ int process_fdisk_menu(struct fdisk_context **cxt0)
                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(
+                               _("\nAll unwritten changes will be lost, do 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));
 
@@ -462,9 +481,15 @@ static int script_read(struct fdisk_context *cxt)
                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);
@@ -514,6 +539,37 @@ done:
        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
  */
@@ -533,16 +589,21 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
                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 (rc)
                        err(EXIT_FAILURE, _("failed to write disklabel"));
+
+               fdisk_info(cxt, _("The partition table has been altered."));
                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 */
@@ -556,6 +617,12 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
        case 'v':
                rc = fdisk_verify_disklabel(cxt);
                break;
+       case 'i':
+               rc = print_partition_info(cxt);
+               break;
+       case 'F':
+               list_freespace(cxt);
+               break;
        }
 
        /* expert mode */
@@ -569,6 +636,10 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
                        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);
@@ -581,8 +652,10 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
        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
@@ -598,8 +671,13 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
                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;
@@ -616,13 +694,12 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
                fdisk_enable_details(cxt, 1);
                break;
        case 'r':
-               /* return from nested BSD to DOS */
+               /* return from nested BSD to DOS or MBR to GPT */
                if (fdisk_get_parent(cxt)) {
                        *cxt0 = fdisk_get_parent(cxt);
 
                        fdisk_info(cxt, _("Leaving nested disklabel."));
                        fdisk_unref_context(cxt);
-                       cxt = *cxt0;
                }
                break;
        }
@@ -633,7 +710,7 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
 
 /*
  * 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__)),
@@ -644,6 +721,7 @@ static int gpt_menu_cb(struct fdisk_context **cxt0,
        struct fdisk_partition *pa = NULL;
        size_t n;
        int rc = 0;
+       uintmax_t length = 0;
 
        assert(cxt);
        assert(ent);
@@ -655,12 +733,19 @@ static int gpt_menu_cb(struct fdisk_context **cxt0,
                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;
                }
@@ -722,7 +807,7 @@ static int gpt_menu_cb(struct fdisk_context **cxt0,
 
 /*
  * 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__)),
@@ -780,13 +865,12 @@ static int dos_menu_cb(struct fdisk_context **cxt0,
                rc = fdisk_set_disklabel_id(cxt);
                break;
        case 'M':
-               /* return from nested MBR to GPT */
+               /* return from nested MBR to GPT (backward compatibility only) */
                if (fdisk_get_parent(cxt)) {
                        *cxt0 = fdisk_get_parent(cxt);
 
                        fdisk_info(cxt, _("Leaving nested disklabel."));
                        fdisk_unref_context(cxt);
-                       cxt = *cxt0;
                }
                break;
        }
@@ -888,7 +972,7 @@ static int sgi_menu_cb(struct fdisk_context **cxt0,
 
 /*
  * 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__)),
@@ -924,32 +1008,50 @@ static int bsd_menu_cb(struct fdisk_context **cxt0,
        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;
        }
 
@@ -978,22 +1080,25 @@ static int createlabel_menu_cb(struct fdisk_context **cxt0,
                        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;
 }