]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
sfdisk add --backup-pt-sectors
authorKarel Zak <kzak@redhat.com>
Thu, 9 Dec 2021 11:07:16 +0000 (12:07 +0100)
committerKarel Zak <kzak@redhat.com>
Thu, 9 Dec 2021 11:07:16 +0000 (12:07 +0100)
The current option --backup is usable only when used with others sfdisk
commands. The new command --backup-pt-sectors create backup and exit.

Signed-off-by: Karel Zak <kzak@redhat.com>
bash-completion/sfdisk
disk-utils/sfdisk.8.adoc
disk-utils/sfdisk.c

index 8aa051626d6aa0b46691ea05c9930a9aab44cf15..b075ccc911602faab687da18a65288d7ce183d5a 100644 (file)
@@ -71,6 +71,7 @@ _sfdisk_module()
                                --part-attrs
                                --append
                                --backup
+                               --backup-pt-sectors
                                --bytes
                                --move-data
                                --force
index f45db414aa995397f27866e557304a3536497b22..ad095775350d54bd52b716c42b4bde87ee37f86d 100644 (file)
@@ -67,6 +67,9 @@ The activation command is supported for MBR and PMBR only. If a GPT label is det
 +
 If no _partition-number_ is specified, then list the partitions with an enabled flag.
 
+*--backup-pt-sectors* _device_::
+Back up the current partition table sectors in binary format and exit. See the *BACKING UP THE PARTITION TABLE* section.
+
 *--delete* _device_ [__partition-number__...]::
 Delete all or the specified partitions.
 
@@ -344,31 +347,40 @@ creates empty GPT partition table. Note that the *--append* disables this featur
 
 It is recommended to save the layout of your devices. *sfdisk* supports two ways.
 
-Use the *--dump* option to save a description of the device layout to a text file. The dump format is suitable for later *sfdisk* input. For example:
+=== Dump in sfdisk compatible format
 
+Use the *--dump* command to save a description of the device layout to a text file.
+The dump format is suitable for later *sfdisk* input. For example:
 ____
 *sfdisk --dump /dev/sda > sda.dump*
 ____
 
 This can later be restored by:
-
 ____
 *sfdisk /dev/sda < sda.dump*
 ____
 
-If you want to do a full (binary) backup of all sectors where the partition table is stored, then use the *--backup* option. It writes the sectors to _~/sfdisk-<device>-<offset>.bak_ files. The default name of the backup file can be changed with the *--backup-file* option. The backup files contain only raw data from the _device_. *sfdisk* writes backup files immediately after startup, this operation is independent, and the backup is done although the device is not modified later.
-For example:
+=== Full binary backup
+
+If you want to do a full binary backup of all sectors where the partition table is stored, then use the *--backup-pt-sectors* command. It writes the sectors to _~/sfdisk-<device>-<offset>.bak_ files. The default name of the backup file can be changed with the *--backup-file* option. The backup files contain only raw data from the _device_. For example:
 
 ____
-*sfdisk --backup /dev/sda*
+*sfdisk --backup-pt-sectors /dev/sda*
 ____
 
 The GPT header can later be restored by:
 
 ____
-dd if=~/sfdisk-sda-0x00000200.bak of=/dev/sda seek=$\((0x00000200)) bs=1 conv=notrunc
+*dd if=~/sfdisk-sda-0x00000200.bak of=/dev/sda seek=$\((0x00000200)) bs=1 conv=notrunc*
+____
+
+
+It's also possible to use the *--backup* option to create the same backup immediately after startup for other sfdisk commands. For example, backup partition table before deleting all partitions from partition table:
+____
+*sfdisk --backup --delete /dev/sda*
 ____
 
+
 The same concept of backup files is used by *wipefs*(8).
 
 Note that *sfdisk* since version 2.26 no longer provides the *-I* option to restore sectors. *dd*(1) provides all necessary functionality.
index 65256c8a51d725f479a0e02646963a43917b9408..d46cdd2449704d42ad2643d820a4d493ff1559b6 100644 (file)
@@ -88,7 +88,8 @@ enum {
        ACT_PARTLABEL,
        ACT_PARTATTRS,
        ACT_DISKID,
-       ACT_DELETE
+       ACT_DELETE,
+       ACT_BACKUP_SECTORS,
 };
 
 struct sfdisk {
@@ -1075,6 +1076,29 @@ static int command_dump(struct sfdisk *sf, int argc, char **argv)
        return 0;
 }
 
+/*
+ * sfdisk --backup-pt-sectors <device>
+ */
+static int command_backup_sectors(struct sfdisk *sf, int argc, char **argv)
+{
+       const char *devname = NULL;
+
+       if (argc)
+               devname = argv[0];
+       if (!devname)
+               errx(EXIT_FAILURE, _("no disk device specified"));
+
+       assign_device(sf, devname, 1);  /* read-only */
+
+       if (!fdisk_has_label(sf->cxt))
+               errx(EXIT_FAILURE, _("%s: does not contain a recognized partition table"), devname);
+
+       backup_partition_table(sf, devname);
+
+       fdisk_deassign_device(sf->cxt, 1);              /* no-sync() */
+       return 0;
+}
+
 static void assign_device_partition(struct sfdisk *sf,
                                const char *devname,
                                size_t partno,
@@ -2022,6 +2046,7 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_(" -A, --activate <dev> [<part> ...] list or set bootable (P)MBR partitions\n"), out);
        fputs(_(" -d, --dump <dev>                  dump partition table (usable for later input)\n"), out);
        fputs(_(" -J, --json <dev>                  dump partition table in JSON format\n"), out);
+       fputs(_(" -B, --backup-pt-sectors <dev>     binary partition table backup (see -b and -O)\n"), out);
        fputs(_(" -g, --show-geometry [<dev> ...]   list geometry of all or specified devices\n"), out);
        fputs(_(" -l, --list [<dev> ...]            list partitions of each device\n"), out);
        fputs(_(" -F, --list-free [<dev> ...]       list unpartitioned free areas of each device\n"), out);
@@ -2124,6 +2149,7 @@ int main(int argc, char *argv[])
        static const struct option longopts[] = {
                { "activate",no_argument,       NULL, 'A' },
                { "append",  no_argument,       NULL, 'a' },
+               { "backup-pt-sectors", no_argument,   NULL, 'B' },
                { "backup",  no_argument,       NULL, 'b' },
                { "backup-file", required_argument, NULL, 'O' },
                { "bytes",   no_argument,       NULL, OPT_BYTES },
@@ -2188,7 +2214,7 @@ int main(int argc, char *argv[])
        textdomain(PACKAGE);
        close_stdout_atexit();
 
-       while ((c = getopt_long(argc, argv, "aAbcdfFgGhJlLo:O:nN:qrsTu:vVX:Y:w:W:",
+       while ((c = getopt_long(argc, argv, "aAbBcdfFgGhJlLo:O:nN:qrsTu:vVX:Y:w:W:",
                                        longopts, &longidx)) != -1) {
 
                err_exclusive_options(c, longopts, excl, excl_st);
@@ -2203,6 +2229,9 @@ int main(int argc, char *argv[])
                case 'b':
                        sf->backup = 1;
                        break;
+               case 'B':
+                       sf->act = ACT_BACKUP_SECTORS;
+                       break;
                case OPT_CHANGE_ID:
                case OPT_PRINT_ID:
                case OPT_ID:
@@ -2370,6 +2399,10 @@ int main(int argc, char *argv[])
                rc = command_activate(sf, argc - optind, argv + optind);
                break;
 
+       case ACT_BACKUP_SECTORS:
+               rc = command_backup_sectors(sf, argc - optind, argv + optind);
+               break;
+
        case ACT_DELETE:
                rc = command_delete(sf, argc - optind, argv + optind);
                break;