LinuxCommandLibrary

sfdisk

Partition disks; create, modify, examine partition tables

TLDR

Back up the partition layout to a file

$ sudo sfdisk [[-d|--dump]] [path/to/device] > [path/to/file.dump]
copy

Restore a partition layout
$ sudo sfdisk [path/to/device] < [path/to/file.dump]
copy

Set the type of a partition
$ sfdisk --part-type [path/to/device] [partition_number] [swap]
copy

Delete a partition
$ sfdisk --delete [path/to/device] [partition_number]
copy

Display help
$ sfdisk [[-h|--help]]
copy

SYNOPSIS

sfdisk [options] device
sfdisk -s device
sfdisk -l [device]
sfdisk -d [device]

PARAMETERS

-d, --dump
    Dumps the current partition table of a device to standard output in a format that can be used as input for sfdisk.

-l, --list
    Lists the partitions on the specified device (or all devices if none is specified), showing their start, end, size, and type.

-s, --show-size
    Prints the size of the specified device in sectors.

-N number, --part-number number
    Specifies the partition number to act upon for operations like deleting or creating.

-n, --no-act
    Performs a dry run; shows what would be done without making any actual changes to the disk.

-u unit, --unit unit
    Specifies the unit for sizes and offsets. Common units are 'sectors', 'cylinders', 'MiB', 'GiB'.

--delete [partno]
    Deletes all partitions or a specific partition number if provided.

--type type
    When creating a new partition, sets the partition type (e.g., Linux, FAT32, swap).

--json
    Outputs information in a machine-readable JSON format, suitable for scripting and parsing.

--label type
    Specifies the disk label type to use (e.g., 'dos' for MBR, 'gpt' for GPT).

DESCRIPTION

sfdisk is a powerful, non-interactive tool for managing partition tables on block devices. Designed primarily for scripting, it enables automated creation, modification, listing, and checking of disk partitions. It supports various partition table formats, including MBR (DOS), GPT, BSD, Sun, and SGI.

Unlike interactive tools, sfdisk operates by reading and writing partition layout descriptions, often from standard input or files, making it ideal for system deployment, backup, and recovery scenarios. Its output is easily parsable, allowing users to back up a disk's partition structure and restore it on another, or even on the same disk. It offers precise control over partition start and end sectors, sizes, types, and bootable flags. It is part of the util-linux project.

CAVEATS

sfdisk is a powerful tool, and misuse can lead to severe data loss. Always back up important data before making partition changes. Because it's non-interactive, it offers no "undo" prompt, executing commands directly based on the provided input. It requires root privileges to operate on devices. After modifying a partition table, the kernel might need to be informed of the changes (e.g., via partprobe or a reboot) for them to be recognized by the system. sfdisk manages only the partition table, not the filesystems themselves; filesystems must be created separately using tools like mkfs.

INPUT FORMAT FOR PARTITION CREATION

When creating or modifying partitions, sfdisk typically reads a description of the desired partition layout from standard input or a file. This description uses a simple, line-oriented format where each line represents a partition and its attributes (e.g., start, size, type). For example:
start=2048,size=1048576,type=83
start=*,size=+,type=82
This allows for precise, programmatic control over the disk's partitioning. Attributes can often be specified using shorthand or relative values (e.g., '*' for the next available sector, '+' for fill remaining space).

COMMON USE CASES

Beyond simple partitioning, sfdisk is extensively used for:
- Automated OS installations and system deployments where partitioning needs to be done consistently across many machines.
- Disk imaging and cloning, allowing the exact partition layout of one disk to be replicated onto another.
- Backup and restoration of partition tables, providing a robust method to recover disk layouts in case of corruption or accidental deletion.

HISTORY

sfdisk has been a long-standing component of the util-linux package, providing a non-interactive alternative to the more interactive fdisk utility. Its design focuses on scriptability, making it a foundational tool for automated system deployments and disk imaging solutions in Linux environments. Over time, it has evolved to support modern partition table formats like GPT, alongside the traditional MBR (DOS) format, ensuring its continued relevance in contemporary system administration.

SEE ALSO

fdisk(8), parted(8), gdisk(8), mkfs(8), mount(8), lsblk(8), partprobe(8)

Copied to clipboard