]> 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 bdaa7347552aa0bbe5541c5128ebc278fca70a70..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,28 +87,35 @@ 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")),
+               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),
@@ -116,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,
@@ -138,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")),
@@ -150,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")),
@@ -170,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 = {
@@ -187,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 = {
@@ -200,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 = {
@@ -210,16 +221,15 @@ struct menu menu_dos = {
                MENU_ENT('c', N_("toggle the dos compatibility flag")),
 
                MENU_XENT('b', N_("move beginning of data in a partition")),
-               MENU_XENT('f', N_("fix partition order")),
                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 = {
@@ -247,6 +257,17 @@ 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]);
@@ -258,11 +279,11 @@ static const struct menu_entry *next_menu_entry(
                /* no more entries */
                if (e->title == NULL ||
                /* menu wanted for specified labels only */
-                   (m->label && cxt->label && !(m->label & cxt->label->id)) ||
+                   (m->label && (!lb || !(m->label & type))) ||
                /* unwanted for nested PT */
-                   (m->nonested && cxt->parent) ||
+                   (m->nonested && parent) ||
                /* menu excluded for specified labels */
-                   (m->exclude && cxt->label && (m->exclude & cxt->label->id))) {
+                   (m->exclude && lb && (m->exclude & type))) {
                        mc->menu_idx++;
                        mc->entry_idx = 0;
                        continue;
@@ -273,16 +294,15 @@ static const struct menu_entry *next_menu_entry(
                 */
 
                /* excluded for the current label */
-               if ((e->exclude && cxt->label && e->exclude & cxt->label->id) ||
+               if ((e->exclude && lb && e->exclude & type) ||
                /* entry wanted for specified labels only */
-                   (e->label && cxt->label && !(e->label & cxt->label->id)) ||
+                   (e->label && (!lb || !(e->label & type))) ||
                /* exclude non-expert entries in expect mode */
-                   (e->expert == 0 && fdisk_context_display_details(cxt)) ||
+                   (e->expert == 0 && fdisk_is_details(cxt)) ||
                /* nested only */
-                   (e->parent && (!cxt->parent || cxt->parent->label->id != e->parent)) ||
+                   (e->parent && (!parent || pr_type != e->parent)) ||
                /* exclude non-normal entries in normal mode */
-                   (e->normal == 0 && !fdisk_context_display_details(cxt))) {
-
+                   (e->normal == 0 && !fdisk_is_details(cxt))) {
                        mc->entry_idx++;
                        continue;
                }
@@ -325,15 +345,15 @@ static int menu_detect_collisions(struct fdisk_context *cxt)
 
                r = get_fdisk_menu_entry(cxt, e->key, NULL);
                if (!r) {
-                       DBG(FRONTEND, ul_debug("warning: not found "
+                       DBG(MENU, ul_debug("warning: not found "
                                        "entry for %c", e->key));
                        return -1;
                }
                if (r != e) {
-                       DBG(FRONTEND, ul_debug("warning: duplicate key '%c'",
+                       DBG(MENU, ul_debug("warning: duplicate key '%c'",
                                                e->key));
-                       DBG(FRONTEND, ul_debug("       : %s", e->title));
-                       DBG(FRONTEND, ul_debug("       : %s", r->title));
+                       DBG(MENU, ul_debug("       : %s", e->title));
+                       DBG(MENU, ul_debug("       : %s", r->title));
                        abort();
                }
        }
@@ -346,9 +366,9 @@ static int print_fdisk_menu(struct fdisk_context *cxt)
        struct menu_context mc = MENU_CXT_EMPTY;
        const struct menu_entry *e;
 
-       ON_DBG(FRONTEND, menu_detect_collisions(cxt));
+       ON_DBG(MENU, menu_detect_collisions(cxt));
 
-       if (fdisk_context_display_details(cxt))
+       if (fdisk_is_details(cxt))
                printf(_("\nHelp (expert commands):\n"));
        else
                printf(_("\nHelp:\n"));
@@ -359,7 +379,7 @@ static int print_fdisk_menu(struct fdisk_context *cxt)
                if (IS_MENU_SEP(e) && (!e->title || !*e->title))
                        printf("\n");
                else if (IS_MENU_SEP(e)) {
-                       color_enable(UL_COLOR_BOLD);
+                       color_scheme_enable("help-title", UL_COLOR_BOLD);
                        printf("\n  %s\n", _(e->title));
                        color_disable();
                } else
@@ -367,11 +387,15 @@ static int print_fdisk_menu(struct fdisk_context *cxt)
        }
        fputc('\n', stdout);
 
-       if (cxt->parent)
+       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'."),
-                               cxt->label->name,
-                               cxt->parent->label->name);
+                               fdisk_label_get_name(l),
+                               fdisk_label_get_name(p));
+       }
 
        return 0;
 }
@@ -394,38 +418,155 @@ 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_context_display_details(cxt))
+       if (fdisk_is_details(cxt))
                prompt = _("Expert command (m for help): ");
        else
                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(FRONTEND, ul_debug("selected: key=%c, entry='%s'",
+       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(FRONTEND, ul_debug("no callback for key '%c'", key));
+               DBG(MENU, ul_debug("no callback for key '%c'", key));
                rc = -EINVAL;
        }
 
-       DBG(FRONTEND, ul_debug("process menu done [rc=%d]", rc));
+       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;
 }
 
@@ -447,22 +588,27 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
                list_disklabel(cxt);
                break;
        case 'w':
-               if (fdisk_context_is_readonly(cxt)) {
-                       fdisk_warnx(cxt, _("Device open in read-only mode."));
+               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 (cxt->parent)
-                       break; /* nested PT, don't leave */
+
                fdisk_info(cxt, _("The partition table has been altered."));
-               rc = fdisk_reread_partition_table(cxt);
+               if (fdisk_get_parent(cxt))
+                       break; /* nested PT, don't leave */
+
+               if (device_is_used)
+                       rc = fdisk_reread_changes(cxt, original_layout);
+               else
+                       rc = fdisk_reread_partition_table(cxt);
                if (!rc)
-                       rc = fdisk_context_deassign_device(cxt);
+                       rc = fdisk_deassign_device(cxt, 0);
                /* fallthrough */
        case 'q':
-               fdisk_free_context(cxt);
+               fdisk_unref_context(cxt);
                fputc('\n', stdout);
                exit(rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
        case 'm':
@@ -471,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 */
@@ -482,8 +634,15 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
                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_context_enable_details(cxt, 0);
+                       rc = fdisk_enable_details(cxt, 0);
                        break;
                }
                return rc;
@@ -493,42 +652,54 @@ 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
                        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':
-               rc = fdisk_add_partition(cxt, 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;
        case 'u':
-               fdisk_context_set_unit(cxt,
-                       fdisk_context_use_cylinders(cxt) ? "sectors" :
+               fdisk_set_unit(cxt,
+                       fdisk_use_cylinders(cxt) ? "sectors" :
                                                           "cylinders");
-               if (fdisk_context_use_cylinders(cxt))
+               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_context_enable_details(cxt, 1);
+               fdisk_enable_details(cxt, 1);
                break;
        case 'r':
-               /* return from nested BSD to DOS */
-               if (cxt->parent) {
-                       *cxt0 = cxt->parent;
+               /* 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_free_context(cxt);
-                       cxt = *cxt0;
+                       fdisk_unref_context(cxt);
                }
                break;
        }
@@ -539,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__)),
@@ -547,27 +718,35 @@ static int gpt_menu_cb(struct fdisk_context **cxt0,
 {
        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_disklabel(cxt, GPT));
+       assert(fdisk_is_label(cxt, GPT));
 
-       DBG(FRONTEND, ul_debug("enter GPT menu"));
+       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;
-                       fdisk_context_enable_details(cxt, 1);   /* keep us in expert mode */
-                       fdisk_sinfo(cxt, FDISK_INFO_SUCCESS,
-                                       _("Entering protective/hybrid MBR disklabel."));
+                       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;
                }
 
@@ -578,32 +757,57 @@ static int gpt_menu_cb(struct fdisk_context **cxt0,
 
                switch(ent->key) {
                case 'u':
-                       rc = fdisk_gpt_partition_set_uuid(cxt, n);
+                       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':
-                       rc = fdisk_gpt_partition_set_name(cxt, 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_partition_toggle_flag(cxt, n, GPT_FLAG_LEGACYBOOT);
+                       rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_LEGACYBOOT);
                        break;
                case 'B':
-                       rc = fdisk_partition_toggle_flag(cxt, n, GPT_FLAG_NOBLOCK);
+                       rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_NOBLOCK);
                        break;
                case 'R':
-                       rc = fdisk_partition_toggle_flag(cxt, n, GPT_FLAG_REQUIRED);
+                       rc = fdisk_toggle_partition_flag(cxt, n, GPT_FLAG_REQUIRED);
                        break;
                case 'S':
-                       rc = fdisk_partition_toggle_flag(cxt, n, GPT_FLAG_GUIDSPECIFIC);
+                       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 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__)),
@@ -612,7 +816,7 @@ static int dos_menu_cb(struct fdisk_context **cxt0,
        struct fdisk_context *cxt = *cxt0;
        int rc = 0;
 
-       DBG(FRONTEND, ul_debug("enter DOS menu"));
+       DBG(MENU, ul_debug("enter DOS menu"));
 
        if (!ent->expert) {
                switch (ent->key) {
@@ -621,7 +825,7 @@ static int dos_menu_cb(struct fdisk_context **cxt0,
                        size_t n;
                        rc = fdisk_ask_partnum(cxt, &n, FALSE);
                        if (!rc)
-                               rc = fdisk_partition_toggle_flag(cxt, n, DOS_FLAG_ACTIVE);
+                               rc = fdisk_toggle_partition_flag(cxt, n, DOS_FLAG_ACTIVE);
                        break;
                }
                case 'b':
@@ -630,14 +834,13 @@ static int dos_menu_cb(struct fdisk_context **cxt0,
                                        = fdisk_new_nested_context(cxt, "bsd");
                        if (!bsd)
                                return -ENOMEM;
-                       if (!fdisk_dev_has_disklabel(bsd))
+                       if (!fdisk_has_label(bsd))
                                rc = fdisk_create_disklabel(bsd, "bsd");
                        if (rc)
-                               fdisk_free_context(bsd);
+                               fdisk_unref_context(bsd);
                        else {
                                *cxt0 = cxt = bsd;
-                               fdisk_sinfo(cxt, FDISK_INFO_SUCCESS,
-                                               _("Entering nested BSD disklabel."));
+                               fdisk_info(cxt, _("Entering nested BSD disklabel."));
                        }
                        break;
                }
@@ -658,20 +861,16 @@ static int dos_menu_cb(struct fdisk_context **cxt0,
                        rc = fdisk_dos_move_begin(cxt, n);
                break;
        }
-       case 'f':
-               rc = fdisk_dos_fix_order(cxt);
-               break;
        case 'i':
                rc = fdisk_set_disklabel_id(cxt);
                break;
        case 'M':
-               /* return from nested MBR to GPT */
-               if (cxt->parent) {
-                       *cxt0 = cxt->parent;
+               /* 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_free_context(cxt);
-                       cxt = *cxt0;
+                       fdisk_unref_context(cxt);
                }
                break;
        }
@@ -685,13 +884,13 @@ static int sun_menu_cb(struct fdisk_context **cxt0,
        struct fdisk_context *cxt = *cxt0;
        int rc = 0;
 
-       DBG(FRONTEND, ul_debug("enter SUN menu"));
+       DBG(MENU, ul_debug("enter SUN menu"));
 
        assert(cxt);
        assert(ent);
-       assert(fdisk_is_disklabel(cxt, SUN));
+       assert(fdisk_is_label(cxt, SUN));
 
-       DBG(FRONTEND, ul_debug("enter SUN menu"));
+       DBG(MENU, ul_debug("enter SUN menu"));
 
        /* normal mode */
        if (!ent->expert) {
@@ -702,10 +901,10 @@ static int sun_menu_cb(struct fdisk_context **cxt0,
                        return rc;
                switch (ent->key) {
                case 'a':
-                       rc = fdisk_partition_toggle_flag(cxt, n, SUN_FLAG_RONLY);
+                       rc = fdisk_toggle_partition_flag(cxt, n, SUN_FLAG_RONLY);
                        break;
                case 'c':
-                       rc = fdisk_partition_toggle_flag(cxt, n, SUN_FLAG_UNMNT);
+                       rc = fdisk_toggle_partition_flag(cxt, n, SUN_FLAG_UNMNT);
                        break;
                }
                return rc;
@@ -740,11 +939,11 @@ static int sgi_menu_cb(struct fdisk_context **cxt0,
        int rc = -EINVAL;
        size_t n = 0;
 
-       DBG(FRONTEND, ul_debug("enter SGI menu"));
+       DBG(MENU, ul_debug("enter SGI menu"));
 
        assert(cxt);
        assert(ent);
-       assert(fdisk_is_disklabel(cxt, SGI));
+       assert(fdisk_is_label(cxt, SGI));
 
        if (ent->expert)
                return rc;
@@ -753,7 +952,7 @@ static int sgi_menu_cb(struct fdisk_context **cxt0,
        case 'a':
                rc = fdisk_ask_partnum(cxt, &n, FALSE);
                if (!rc)
-                       rc = fdisk_partition_toggle_flag(cxt, n, SGI_FLAG_BOOT);
+                       rc = fdisk_toggle_partition_flag(cxt, n, SGI_FLAG_BOOT);
                break;
        case 'b':
                fdisk_sgi_set_bootfile(cxt);
@@ -761,7 +960,7 @@ static int sgi_menu_cb(struct fdisk_context **cxt0,
        case 'c':
                rc = fdisk_ask_partnum(cxt, &n, FALSE);
                if (!rc)
-                       rc = fdisk_partition_toggle_flag(cxt, n, SGI_FLAG_SWAP);
+                       rc = fdisk_toggle_partition_flag(cxt, n, SGI_FLAG_SWAP);
                break;
        case 'i':
                rc = fdisk_sgi_create_info(cxt);
@@ -773,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__)),
@@ -784,9 +983,9 @@ static int bsd_menu_cb(struct fdisk_context **cxt0,
 
        assert(cxt);
        assert(ent);
-       assert(fdisk_is_disklabel(cxt, BSD));
+       assert(fdisk_is_label(cxt, BSD));
 
-       DBG(FRONTEND, ul_debug("enter BSD menu"));
+       DBG(MENU, ul_debug("enter BSD menu"));
 
        switch(ent->key) {
        case 'e':
@@ -796,11 +995,11 @@ static int bsd_menu_cb(struct fdisk_context **cxt0,
                rc = fdisk_bsd_write_bootstrap(cxt);
                break;
        case 's':
-               org = fdisk_context_display_details(cxt);
+               org = fdisk_is_details(cxt);
 
-               fdisk_context_enable_details(cxt, 1);
+               fdisk_enable_details(cxt, 1);
                list_disklabel(cxt);
-               fdisk_context_enable_details(cxt, org);
+               fdisk_enable_details(cxt, org);
                break;
        case 'x':
                rc = fdisk_bsd_link_partition(cxt);
@@ -809,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(FRONTEND, ul_debug("enter GEO menu"));
+       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, cxt->geom.cylinders,
-                               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, cxt->geom.heads,
-                               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, cxt->geom.sectors,
-                               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;
        }
 
@@ -850,7 +1067,7 @@ static int createlabel_menu_cb(struct fdisk_context **cxt0,
        struct fdisk_context *cxt = *cxt0;
        int rc = -EINVAL;
 
-       DBG(FRONTEND, ul_debug("enter Create label menu"));
+       DBG(MENU, ul_debug("enter Create label menu"));
 
        assert(cxt);
        assert(ent);
@@ -863,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;
 }