]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - disk-utils/fdisk-menu.c
Merge branch 'PR/libmount-exec-errors' of github.com:karelzak/util-linux-work
[thirdparty/util-linux.git] / disk-utils / fdisk-menu.c
index b099cc3c29913c184d54f8fb123d60fb7f88eea9..717554b0e3478d49e03a436086d61dbffa59c21d 100644 (file)
@@ -1,4 +1,13 @@
-
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
+ */
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -6,6 +15,7 @@
 #include <stdint.h>
 
 #include "c.h"
+#include "rpmatch.h"
 #include "fdisk.h"
 #include "pt-sun.h"
 #include "pt-mbr.h"
@@ -86,10 +96,10 @@ 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")),
@@ -101,6 +111,8 @@ struct menu menu_generic = {
                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_ENT  ('e', N_("resize a partition")),
+               MENU_ENT  ('T', N_("discard (trim) sectors")),
 
                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,13 +133,15 @@ struct menu menu_generic = {
                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),
+               MENU_ENT_NEST('r', N_("return from BSD to DOS (MBR)"), 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,
@@ -135,7 +149,7 @@ struct menu menu_createlabel = {
                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('o', N_("create a new empty MBR (DOS) partition table")),
                MENU_ENT('s', N_("create a new empty Sun partition table")),
 
                /* backward compatibility -- be sensitive to 'g', but don't
@@ -145,11 +159,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")),
@@ -157,15 +171,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")),
@@ -177,7 +192,7 @@ struct menu menu_gpt = {
        }
 };
 
-struct menu menu_sun = {
+static const struct menu menu_sun = {
        .callback = sun_menu_cb,
        .label = FDISK_DISKLABEL_SUN,
        .entries = {
@@ -194,7 +209,7 @@ struct menu menu_sun = {
        }
 };
 
-struct menu menu_sgi = {
+static const struct menu menu_sgi = {
        .callback = sgi_menu_cb,
        .label = FDISK_DISKLABEL_SGI,
        .entries = {
@@ -207,7 +222,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,15 +232,16 @@ 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 partitions C/H/S values")),
                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 = {
@@ -275,7 +291,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 */
@@ -292,7 +308,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 */
@@ -414,7 +430,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): ");
@@ -422,11 +438,26 @@ 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))) {
+                       /* TRANSLATORS: these yes no questions use rpmatch(),
+                        * and should be translated.  */
+                       rc = get_user_reply(
+                               _("\nAll unwritten changes will be lost, do you really want to quit? (y/n)"),
+                               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);
@@ -464,9 +495,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."));
+                        rc = fdisk_create_disklabel(cxt, NULL);
+               }
+       } else
                fdisk_info(cxt, _("Script successfully applied."));
 
        fdisk_unref_script(sc);
@@ -572,10 +609,15 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
                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 */
@@ -608,10 +650,6 @@ 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);
@@ -624,8 +662,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
@@ -648,6 +688,9 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
                        rc = ask_for_wipe(cxt, partno);
                break;
        }
+       case 'e':
+               resize_partition(cxt);
+               break;
        case 't':
                change_partition_type(cxt);
                break;
@@ -664,7 +707,7 @@ 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);
 
@@ -672,6 +715,10 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
                        fdisk_unref_context(cxt);
                }
                break;
+       case 'T':
+               /* discard (trim) */
+               discard_sectors(cxt);
+               break;
        }
 
        return rc;
@@ -680,7 +727,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__)),
@@ -691,6 +738,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);
@@ -702,12 +750,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;
                }
@@ -769,7 +824,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__)),
@@ -827,7 +882,7 @@ 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);
 
@@ -835,6 +890,13 @@ static int dos_menu_cb(struct fdisk_context **cxt0,
                        fdisk_unref_context(cxt);
                }
                break;
+       case 'F':
+               rc = fdisk_dos_fix_chs(cxt);
+               if (rc)
+                       fdisk_info(cxt, _("C/H/S values fixed."));
+               else
+                       fdisk_info(cxt, _("Nothing to do. C/H/S values are correct already."));
+               break;
        }
        return rc;
 }
@@ -934,7 +996,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__)),
@@ -970,32 +1032,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;
        }
 
@@ -1009,6 +1089,7 @@ static int createlabel_menu_cb(struct fdisk_context **cxt0,
                       const struct menu_entry *ent)
 {
        struct fdisk_context *cxt = *cxt0;
+       const char *wanted = NULL;
        int rc = -EINVAL;
 
        DBG(MENU, ul_debug("enter Create label menu"));
@@ -1021,25 +1102,35 @@ static int createlabel_menu_cb(struct fdisk_context **cxt0,
                case 'g':
                        /* Deprecated, use 'G' in main menu, just for backward
                         * compatibility only. */
-                       rc = fdisk_create_disklabel(cxt, "sgi");
+                       wanted = "sgi";
                        break;
                }
-               return rc;
+       } else {
+               switch (ent->key) {
+                       case 'g':
+                               wanted = "gpt";
+                               break;
+                       case 'G':
+                               wanted = "sgi";
+                               break;
+                       case 'o':
+                               wanted = "dos";
+                               break;
+                       case 's':
+                               wanted = "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 (wanted) {
+               rc = fdisk_create_disklabel(cxt, wanted);
+               if (rc) {
+                       errno = -rc;
+                       fdisk_warn(cxt, _("Failed to create '%s' disk label"), wanted);
+               }
        }
+       if (rc == 0 && fdisk_get_collision(cxt))
+               follow_wipe_mode(cxt);
+
        return rc;
 }