summaryrefslogtreecommitdiffstats
path: root/disk-utils
diff options
context:
space:
mode:
authorKarel Zak2014-11-12 11:15:06 +0100
committerKarel Zak2014-11-12 11:15:06 +0100
commita30e4ef423c8c57d4510d6353f3b00080a7290e8 (patch)
tree9370cc8c6871874bb7029b3ba4407946105441e6 /disk-utils
parentlibfdisk: fix script parser, add debug messages (diff)
downloadkernel-qcow2-util-linux-a30e4ef423c8c57d4510d6353f3b00080a7290e8.tar.gz
kernel-qcow2-util-linux-a30e4ef423c8c57d4510d6353f3b00080a7290e8.tar.xz
kernel-qcow2-util-linux-a30e4ef423c8c57d4510d6353f3b00080a7290e8.zip
fdisk: add support for sfdisk scripts
New commands 'I' and 'O' allows to read and write sfdisk compatible scripts by fdisk. It means that you can save your work (partition table) and later use it (in fdisk, sfdisk or cfdisk) to create a new partition table. Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'disk-utils')
-rw-r--r--disk-utils/fdisk-menu.c78
-rw-r--r--disk-utils/fdisk.813
2 files changed, 91 insertions, 0 deletions
diff --git a/disk-utils/fdisk-menu.c b/disk-utils/fdisk-menu.c
index c973833a0..83a3f9406 100644
--- a/disk-utils/fdisk-menu.c
+++ b/disk-utils/fdisk-menu.c
@@ -109,6 +109,10 @@ struct menu menu_generic = {
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_("read disk layout from fdisk script file")),
+ MENU_ENT ('O', N_("write disk layout to fdisk 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),
@@ -442,6 +446,74 @@ int process_fdisk_menu(struct fdisk_context **cxt0)
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);
+ 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 read 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;
+}
+
/*
* Basic fdisk actions
*/
@@ -516,6 +588,12 @@ static int generic_menu_cb(struct fdisk_context **cxt0,
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;
diff --git a/disk-utils/fdisk.8 b/disk-utils/fdisk.8
index c998e2ca5..56caab16d 100644
--- a/disk-utils/fdisk.8
+++ b/disk-utils/fdisk.8
@@ -147,6 +147,19 @@ For backward compatibility fdisk also accepts the suffixes KB=1000,
MB=1000*1000, and so on for GB, TB, PB, EB, ZB and YB. These 10^N suffixes
are deprecated.
+.SH SCRIPT FILES
+.B fdisk
+allows to read (by 'I' command) sfdisk compatible script files. The script is
+applied to in-memory partition table, and then it is possible to modify the
+partition table before you write it to the device.
+.PP
+And vice-versa it is possible to write the current in-memory disk layout
+to the script file by command 'O'.
+.PP
+The script files are compatible between cfdisk, sfdisk, fdisk and another
+libfdisk applications. For more details see
+.BR sfdisk (8).
+
.SH DISK LABELS
.B GPT (GUID Partition Table)
.RS