SFDISK(8) | System Administration | SFDISK(8) |
NAME
sfdisk - display or manipulate a disk partition table
SYNOPSIS
sfdisk [options] device [-N partition-number]
sfdisk [options] command
DESCRIPTION
sfdisk is a script-oriented tool for partitioning any block device. It runs in interactive mode if executed on a terminal (stdin refers to a terminal).
Since version 2.26 sfdisk supports MBR (DOS), GPT, SUN and SGI disk labels, but no longer provides any functionality for CHS (Cylinder-Head-Sector) addressing. CHS has never been important for Linux, and this addressing concept does not make any sense for new devices.
sfdisk protects the first disk sector when create a new disk label. The option --wipe always disables this protection. Note that fdisk(8) and cfdisk(8) completely erase this area by default.
sfdisk (since version 2.26) aligns the start and end of partitions to block-device I/O limits when relative sizes are specified, when the default values are used or when multiplicative suffixes (e.g., MiB) are used for sizes. It is possible that partition size will be optimized (reduced or enlarged) due to alignment if the start offset is specified exactly in sectors and partition size relative or by multiplicative suffixes.
The recommended way is not to specify start offsets at all and specify partition size in MiB, GiB (or so). In this case sfdisk aligns all partitions to block-device I/O limits (or when I/O limits are too small then to megabyte boundary to keep disk layout portable). If this default behaviour is unwanted (usually for very small partitions) then specify offsets and sizes in sectors. In this case sfdisk entirely follows specified numbers without any optimization.
sfdisk does not create the standard system partitions for SGI and SUN disk labels like fdisk(8) does. It is necessary to explicitly create all partitions including whole-disk system partitions.
sfdisk uses BLKRRPART (reread partition table) ioctl to make sure that the device is not used by system or other tools (see also --no-reread). It’s possible that this feature or another sfdisk activity races with systemd-udevd(8). The recommended way how to avoid possible collisions is to use --lock option. The exclusive lock will cause systemd-udevd to skip the event handling on the device.
The sfdisk prompt is only a hint for users and a displayed partition number does not mean that the same partition table entry will be created (if -N not specified), especially for tables with gaps.
COMMANDS
The commands are mutually exclusive.
[-N partition-number] device
If the option -N is specified, then the changes are applied to the partition addressed by partition-number. The unspecified fields of the partition are not modified.
Note that it’s possible to address an unused partition with -N. For example, an MBR always contains 4 partitions, but the number of used partitions may be smaller. In this case sfdisk follows the default values from the partition table and does not use built-in defaults for the unused partition given with -N. See also --append.
-A, --activate device [partition-number...]
The activation command is supported for MBR and PMBR only. If a GPT label is detected, then sfdisk prints warning and automatically enters PMBR.
If no partition-number is specified, then list the partitions with an enabled flag.
--backup-pt-sectors device
--delete device [partition-number...]
-d, --dump device
-g, --show-geometry [device...]
-J, --json device
-l, --list [device...]
-F, --list-free [device...]
--part-attrs device partition-number [attributes]
Bit 0 (RequiredPartition)
Bit 1 (NoBlockIOProtocol)
Bit 2 (LegacyBIOSBootable)
Bits 3-47
Bits 48-63
--part-label device partition-number [label]
--part-type device partition-number [type]
The type argument is hexadecimal for MBR, GUID for GPT, type alias (e.g. "linux") or type shortcut (e.g. 'L'). For backward compatibility the options -c and --id have the same meaning as this one.
--part-uuid device partition-number [uuid]
--disk-id device [id]
-r, --reorder device
-s, --show-size [device...]
-T, --list-types
-V, --verify [device...]
--relocate oper device
gpt-bak-std
gpt-bak-mini
OPTIONS
-a, --append
Note that unused partition maybe be re-used in this case although it is not the last partition in the partition table. See also -N to specify entry in the partition table.
-b, --backup
--color[=when]
-f, --force
--Linux
--lock[=mode]
-n, --no-act
--no-reread
--no-tell-kernel
-O, --backup-file path
--move-data[=path]
The optional path specifies log file name. The log file contains information about all read/write operations on the partition data. The word "@default" as a path forces sfdisk to use ~/sfdisk-<devname>.move for the log. The log is optional since v2.35.
Note that this operation is risky and not atomic. Don’t forget to backup your data!
See also --move-use-fsync.
In the example below, the first command creates a 100MiB free area before the first partition and moves the data it contains (e.g., a filesystem), the next command creates a new partition from the free space (at offset 2048), and the last command reorders partitions to match disk order (the original sdc1 will become sdc2).
echo '+100M,' | sfdisk --move-data /dev/sdc -N 1
echo '2048,' | sfdisk /dev/sdc --append
sfdisk /dev/sdc --reorder
--move-use-fsync
-o, --output list
The default list of columns may be extended if list is specified in the format +list (e.g., -o +UUID).
-q, --quiet
-u, --unit S
-X, --label type
-Y, --label-nested type
-w, --wipe when
-W, --wipe-partitions when
-v, --version
-h, --help
INPUT FORMATS
sfdisk supports two input formats and generic header lines.
Header lines
The optional header lines specify generic information that apply to the partition table. The header-line format is:
<name>: <value>
The currently recognized headers are:
unit
label
label-id
first-lba
last-lba
table-length
grain
sector-size
Note that it is only possible to use header lines before the first partition is specified in the input.
Unnamed-fields format
start size type bootable
where each line fills one partition descriptor.
Fields are separated by whitespace, comma (recommended) or semicolon possibly followed by whitespace; initial and trailing whitespace is ignored. Numbers can be octal, decimal or hexadecimal; decimal is the default. When a field is absent, empty or specified as '-' a default value is used. But when the -N option (change a single partition) is given, the default for each field is its previous value.
The default value of start is the first non-assigned sector aligned according to device I/O limits. The default start offset for the first partition is 1 MiB. If the offset is followed by the multiplicative suffixes (KiB, MiB, GiB, TiB, PiB, EiB, ZiB and YiB), then the number is interpreted as offset in bytes. Since v2.38 when the -N option (change a single partition) is given, a '+' can be used to enlarge partition by move start of the partition if there is a free space before the partition.
The default value of size indicates "as much as possible"; i.e., until the next partition or end-of-device. A numerical argument is by default interpreted as a number of sectors, however if the size is followed by one of the multiplicative suffixes (KiB, MiB, GiB, TiB, PiB, EiB, ZiB and YiB) then the number is interpreted as the size of the partition in bytes and it is then aligned according to the device I/O limits. A '+' can be used instead of a number to enlarge the partition as much as possible. Note '+' is equivalent to the default behaviour for a new partition; existing partitions will be resized as required.
The partition type is given in hex for MBR (DOS) where 0x prefix is optional; a GUID string for GPT; a shortcut or an alias. It’s recommended to use two letters for MBR hex codes to avoid collision between deprecated shortcut 'E' and '0E' MBR hex code. For backward compatibility sfdisk tries to interpret type as a shortcut as a first possibility in partitioning scripts although on other places (e.g. --part-type command) it tries shortcuts as the last possibility.
Since v2.36 libfdisk supports partition type aliases as extension to shortcuts. The alias is a simple human readable word (e.g. "linux").
Since v2.37 libfdisk supports partition type names on input, ignoring the case of the characters and all non-alphanumeric and non-digit characters in the name (e.g. "Linux /usr x86" is the same as "linux usr-x86").
Supported shortcuts and aliases:
L - alias 'linux'
S - alias 'swap'
Ex - alias 'extended'
H - alias 'home'
U - alias 'uefi'
R - alias 'raid'
V - alias 'lvm'
The default type value is linux.
The shortcut 'X' for Linux extended partition (85) is deprecated in favour of 'Ex'.
bootable is specified as [*|-], with as default not-bootable. The value of this field is irrelevant for Linux - when Linux runs it has been booted already - but it might play a role for certain boot loaders and for other operating systems.
Named-fields format
This format is more readable, robust, extensible and allows specifying additional information (e.g., a UUID). It is recommended to use this format to keep your scripts more readable.
[device :] name[=value], ...
The device field is optional. sfdisk extracts the partition number from the device name. It allows specifying the partitions in random order. This functionality is mostly used by --dump. Don’t use it if you are not sure.
The value can be between quotation marks (e.g., name="This is partition name"). The fields start= and size= support '+' and '-' in the same way as Unnamed-fields format.
The currently supported fields are:
start=number
size=number
bootable
attrs=string
uuid=string
name=string
type=code
EMPTY DISK LABEL
sfdisk does not create partition table without partitions by default. The lines with partitions are expected in the script by default. The empty partition table has to be explicitly requested by "label: <name>" script header line without any partitions lines. For example:
echo 'label: gpt' | sfdisk /dev/sdb
creates empty GPT partition table. Note that the --append disables this feature.
BACKING UP THE PARTITION TABLE
It is recommended to save the layout of your devices. sfdisk supports two ways.
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
Note that sfdisk completely restores partition types and partition UUIDs. This could potentially become problematic if you duplicate the same layout to different disks, as it may result in duplicate UUIDs within your system.
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-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
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.
COLORS
The output colorization is implemented by terminal-colors.d(5) functionality. Implicit coloring can be disabled by an empty file
/etc/terminal-colors.d/sfdisk.disable
for the sfdisk command or for all tools by
/etc/terminal-colors.d/disable
The user-specific $XDG_CONFIG_HOME/terminal-colors.d or $HOME/.config/terminal-colors.d overrides the global setting.
Note that the output colorization may be enabled by default, and in this case terminal-colors.d directories do not have to exist yet.
The logical color names supported by sfdisk are:
header
warn
welcome
ENVIRONMENT
SFDISK_DEBUG=all
LIBFDISK_DEBUG=all
LIBBLKID_DEBUG=all
LIBSMARTCOLS_DEBUG=all
LOCK_BLOCK_DEVICE=<mode>
NOTES
Since version 2.26 sfdisk no longer provides the -R or --re-read option to force the kernel to reread the partition table. Use blockdev --rereadpt instead.
Since version 2.26 sfdisk does not provide the --DOS, --IBM, --DOS-extended, --unhide, --show-extended, --cylinders, --heads, --sectors, --inside-outer, --not-inside-outer options.
EXAMPLES
sfdisk --list --label-nested=mbr /dev/sda
echo -e ',10M,L\n,10M,L\n,+,\n' | sfdisk /dev/sdc
echo -e 'size=10M, type=L\n size=10M, type=L\n size=+\n' | sfdisk /dev/sdc
echo -e 'type=swap' | sfdisk -N 3 /dev/sdc
sfdisk --part-type /dev/sdc 3 swap
sfdisk --delete /dev/sdc 2
echo "+,+" | sfdisk -N 3 --move-data /dev/sdc
AUTHORS
Karel Zak <kzak@redhat.com>
The current sfdisk implementation is based on the original sfdisk from Andries E. Brouwer.
SEE ALSO
REPORTING BUGS
For bug reports, use the issue tracker at https://github.com/util-linux/util-linux/issues.
AVAILABILITY
The sfdisk command is part of the util-linux package which can be downloaded from Linux Kernel Archive https://www.kernel.org/pub/linux/utils/util-linux/.
2024-07-04 | util-linux 2.40.2 |