diff options
author | Karel Zak | 2014-11-12 11:15:06 +0100 |
---|---|---|
committer | Karel Zak | 2014-11-12 11:15:06 +0100 |
commit | a30e4ef423c8c57d4510d6353f3b00080a7290e8 (patch) | |
tree | 9370cc8c6871874bb7029b3ba4407946105441e6 /disk-utils/fdisk-menu.c | |
parent | libfdisk: fix script parser, add debug messages (diff) | |
download | kernel-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/fdisk-menu.c')
-rw-r--r-- | disk-utils/fdisk-menu.c | 78 |
1 files changed, 78 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; |