[parted-devel] [PATCH] Add autofix: Automatically "Fix" in script mode
Cristian Klein
cristian at kleinlabs.eu
Sat Nov 21 20:00:02 GMT 2020
Use-case: VMs are booted from images that are smaller than their virtual
disk. This means that -- almost by definition -- the secondary GPT
header will be "misplaced", i.e., not at the end of the virtual disk.
Without this patch, parted cannot be used for custom/exotic partitioning
when the VM boots (e.g., in cloud-init's `bootcmd`). Specifically, it
will fail as follows:
```
$ sudo parted --script /dev/vda "mkpart 2 10GB -1"
Warning: Not all of the space available to /dev/vda appears to be used,
you can fix the GPT to use all of the space (an extra 500 blocks) or continue with the current setting?
Error: Unable to satisfy all constraints on the partition.
```
This happens because, in script mode, exceptions are usually not
resolved.
This patch adds `--autofix`. This allows exceptions to be automatically
resolved with Fix. As a result, the following command will work:
```
$ sudo parted autofix --script /dev/vda "mkpart 2 10GB -1"
```
---
parted/parted.c | 4 ++++
parted/ui.c | 6 ++++++
parted/ui.h | 1 +
3 files changed, 11 insertions(+)
diff --git a/parted/parted.c b/parted/parted.c
index dbd38d0..36d1201 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -115,6 +115,7 @@ static struct option const options[] = {
{"list", 0, NULL, 'l'},
{"machine", 0, NULL, 'm'},
{"script", 0, NULL, 's'},
+ {"autofix", 0, NULL, 'f'},
{"version", 0, NULL, 'v'},
{"align", required_argument, NULL, 'a'},
{"-pretend-input-tty", 0, NULL, PRETEND_INPUT_TTY},
@@ -126,12 +127,14 @@ static const char *const options_help [][2] = {
{"list", N_("lists partition layout on all block devices")},
{"machine", N_("displays machine parseable output")},
{"script", N_("never prompts for user intervention")},
+ {"autofix", N_("in script mode, fix instead of abort when asked")},
{"version", N_("displays the version")},
{"align=[none|cyl|min|opt]", N_("alignment for new partitions")},
{NULL, NULL}
};
int opt_script_mode = 0;
+int opt_autofix_mode = 0;
int pretend_input_tty = 0;
int opt_machine_mode = 0;
int disk_is_modified = 0;
@@ -2200,6 +2203,7 @@ while (1)
case 'l': list = 1; break;
case 'm': opt_machine_mode = 1; break;
case 's': opt_script_mode = 1; break;
+ case 'f': opt_autofix_mode = 1; break;
case 'v': version = 1; break;
case 'a':
alignment = XARGMATCH ("--align", optarg,
diff --git a/parted/ui.c b/parted/ui.c
index 7b5374d..2e43896 100644
--- a/parted/ui.c
+++ b/parted/ui.c
@@ -643,6 +643,12 @@ exception_handler (PedException* ex)
if (!option_get_next (ex->options, opt))
return opt;
+ /* script-mode and autofix? */
+ int autofix_is_an_option = (ex->options & PED_EXCEPTION_FIX);
+ if (opt_script_mode && opt_autofix_mode && autofix_is_an_option) {
+ return PED_EXCEPTION_FIX;
+ }
+
/* script-mode: don't handle the exception */
if (opt_script_mode || (!isatty (0) && !pretend_input_tty))
return PED_EXCEPTION_UNHANDLED;
diff --git a/parted/ui.h b/parted/ui.h
index 6192ee5..ff754a2 100644
--- a/parted/ui.h
+++ b/parted/ui.h
@@ -88,6 +88,7 @@ extern void print_using_dev (PedDevice* dev);
/* in parted.c */
extern int opt_script_mode;
+extern int opt_autofix_mode;
extern int pretend_input_tty;
extern void print_options_help ();
--
2.25.1
More information about the parted-devel
mailing list