+
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.
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.
ACT_PARTLABEL,
ACT_PARTATTRS,
ACT_DISKID,
- ACT_DELETE
+ ACT_DELETE,
+ ACT_BACKUP_SECTORS,
};
struct sfdisk {
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,
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);
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 },
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);
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:
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;