diff options
| author | Karel Zak <kzak@redhat.com> | 2021-12-09 12:07:16 +0100 |
|---|---|---|
| committer | Karel Zak <kzak@redhat.com> | 2021-12-09 12:07:16 +0100 |
| commit | c4762c436b2ee637d7752597cec950598d7d4486 (patch) | |
| tree | 595da02bf9b3791a6eb16740f796a0083bea44b0 | |
| parent | 03fdf11833a7dc9ae9128be12e6fffc013a3855b (diff) | |
| download | util-linux-c4762c436b2ee637d7752597cec950598d7d4486.tar.gz | |
sfdisk add --backup-pt-sectors
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>
| -rw-r--r-- | bash-completion/sfdisk | 1 | ||||
| -rw-r--r-- | disk-utils/sfdisk.8.adoc | 24 | ||||
| -rw-r--r-- | disk-utils/sfdisk.c | 37 |
3 files changed, 54 insertions, 8 deletions
diff --git a/bash-completion/sfdisk b/bash-completion/sfdisk index 8aa051626d..b075ccc911 100644 --- a/bash-completion/sfdisk +++ b/bash-completion/sfdisk @@ -71,6 +71,7 @@ _sfdisk_module() --part-attrs --append --backup + --backup-pt-sectors --bytes --move-data --force diff --git a/disk-utils/sfdisk.8.adoc b/disk-utils/sfdisk.8.adoc index f45db414aa..ad09577535 100644 --- a/disk-utils/sfdisk.8.adoc +++ b/disk-utils/sfdisk.8.adoc @@ -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. diff --git a/disk-utils/sfdisk.c b/disk-utils/sfdisk.c index 65256c8a51..d46cdd2449 100644 --- a/disk-utils/sfdisk.c +++ b/disk-utils/sfdisk.c @@ -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; |
