On Unix-like operating systems, the sfdisk command is a partition table editor. It is similar to fdisk and cfdisk, but with additional features. Additionally, unlike those two programs, sfdisk can run non-interactively.
This page describes the Linux version of sfdisk.
Description
sfdisk has four (main) uses: it can list the size of a partition, list the partitions on a device, check the partitions on a device, and - this one is very dangerous! - it can repartition a device.
- Description
- Syntax
- Input Format
- Examples
- Related commands
- Linux commands help
sfdisk doesn’t understand the GPT (GUID Partition Table) format and it is not designed for large partitions. In these cases use the more advanced GNU parted.
Listing partition sizes
The following command:
sfdisk -s partition
gives the size of partition in blocks. This may be useful in connection with programs like mkswap. Here partition is usually something like /dev/hda1 or /dev/sdb12, but may also be an entire disk, like /dev/xda. The following example command, and output, shows the size of partition /dev/hda9:
sfdisk -s /dev/hda9
81599
If the partition argument is omitted, sfdisk lists the sizes of all disks, and the total:
sfdisk -s
/dev/hda: 208896 /dev/hdb: 1025136 /dev/hdc: 1031063 /dev/sda: 8877895 /dev/sdb: 1758927 total: 12901917 blocks
Listing partitions
The second type of invocation:
sfdisk -l device
lists the partitions on the specified device. If the device argument is omitted, the partitions on all hard disks are listed. The following example lists the partitions on device /dev/hdc:
sfdisk -l /dev/hdc
Disk /dev/hdc: 16 heads, 63 sectors, 2045 cylinders Units = cylinders of 516096 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/hdc1 0+ 406 407- 205096+ 83 Linux native /dev/hdc2 407 813 407 205128 83 Linux native /dev/hdc3 814 2044 1231 620424 83 Linux native /dev/hdc4 0 - 0 0 0 Empty
The trailing - and + signs indicate that rounding has taken place, and that the actual value is slightly less or more. To see the exact values, ask for a listing with sectors as the unit (using the “-u S” option).
Checking partitions
The third type of invocation:
sfdisk -V device
will apply various consistency checks to the partition tables on device. It either prints “OK”, or complains. The -V option can be used together with -l. For example, in a shell script you could use
sfdisk -V -q device
which would only return the status of device.
Creating partitions
The fourth type of invocation:
sfdisk device
causes sfdisk to read the specification for the desired partitioning of device from standard input, and then to change the partition tables on that disk. Thus it is possible to use sfdisk from a shell script. When sfdisk determines that its standard input is a terminal, it will be conversational; otherwise it will abort on any error.
BE EXTREMELY CAREFUL. ONE TYPING MISTAKE AND ALL YOUR DATA IS LOST!
As a precaution, you can save the sectors changed by sfdisk:
% sfdisk /dev/hdd -O hdd-partition-sectors.save
Then, if you discover you did something stupid before anything else is written to disk, it may be possible to recover the old situation with:
% sfdisk /dev/hdd -I hdd-partition-sectors.save
This command is not the same as saving the old partition table: a readable version of the old partition table can be saved using the -d option. However, if you create logical partitions, the sectors describing them are located somewhere on disk, possibly on sectors that were not part of the partition table before. Thus, the information the -O option saves is not a binary version of the output of -d.
Syntax
sfdisk [options] device
sfdisk -s [partition]
Options
sfdisk accepts the following command-line options:
Technical Details And Background Theory
Block 0 of a disk (the Master Boot Record) contains among other things four partition descriptors. The partitions described here are called primary partitions.
sfdisk -d /dev/hda > hda.out
sfdisk /dev/hda < hda.out
sfdisk /dev/hdb -N5
,,,*
sfdisk –print-id /dev/hdb 5
6
sfdisk –change-id /dev/hdb 5 83
OK
A partition descriptor has 6 fields:
struct partition { unsigned char bootable; /* 0 or 0x80 */ hsc begin_hsc; unsigned char id; hsc end_hsc; unsigned int starting_sector; unsigned int nr_of_sectors; }
The two hsc fields indicate head, sector and cylinder of the begin and the end of the partition. Since each hsc field only takes 3 bytes, only 24 bits are available, which does not suffice for big disks (say > 8 GB). In fact, due to the wasteful representation (that uses a byte for the number of heads, which is often 16), problems already start with 0.5 GB. However, Linux does not use these fields, and problems can arise only at boot time, before Linux starts. For more details, see your LILO documentation.
Each partition has a type, its “Id”, and if this type is 5 or f (“extended partition”) the starting sector of the partition again contains 4 partition descriptors. MSDOS only uses the first two of these: the first one an actual data partition, and the second one again an extended partition (or empty). In this way one gets a chain of extended partitions. Other operating systems have slightly different conventions. Linux also accepts type 85 as equivalent to 5 and f - this can be useful if one wants to have extended partitions under Linux past the 1024 cylinder boundary, without DOS FDISK hanging. If there is no good reason, only use 5, which is understood by other systems.
Partitions that are not primary or extended are called logical. Often, one cannot boot from logical partitions (because finding them is more involved than only looking at the MBR). Note that of an extended partition only the Id and the start are used. There are various conventions about what to write in the other fields. One should not try to use extended partitions for data storage or swap.
Input Format
sfdisk reads lines of the form
where each line fills one partition descriptor.
Fields are separated by whitespace, or comma or semicolon possibly followed by whitespace; initial and trailing whitespace is ignored. Numbers can be octal, decimal or hexadecimal; decimal is default. When a field is absent or empty, a default value is used.
The <c,h,s> parts can (and probably should) be omitted - sfdisk computes them from
Bootable is specified as [*|-], with as default not-bootable. The value of this field is irrelevant for Linux - when Linux runs it was alread booted - but might play a role for certain boot loaders and for other operating systems. For example, when there’s several primary DOS partitions, DOS assigns C: to the first among these that is bootable.
Id is given in hex, without the 0x prefix, or is [E|S|L|X], where L (LINUX_NATIVE (83)) is the default, S is LINUX_SWAP (82), E is EXTENDED_PARTITION (5), and X is LINUX_EXTENDED (85).
The default value of start is the first nonassigned sector/cylinder/…
The default value of size is as much as possible (until next partition or end-of-disk).
However, for the four partitions inside an extended partition, the defaults are: Linux partition, Extended partition, Empty, Empty.
But when the -N option (change a single partition only) is given, the default for each field is its previous value.
A ‘+’ can be specified instead of a number for size, which means as much as possible. This is useful with the -N option.
Examples
sfdisk /dev/hdc « EOF
0,407 ,407 ; ; EOF
This command (and the listed input) partitions /dev/hdc as indicated above.
sfdisk /dev/hdb « EOF
,3,L ,60,L ,19,S ,,E ,130,L ,130,L ,130,L ,,L EOF
This command (and the listed input) will partition /dev/hdb into two Linux partitions of 3 and 60 cylinders, a swap space of 19 cylinders, and an extended partition covering the rest. Inside the extended partition there are four Linux logical partitions, three of 130 cylinders and one covering the rest.
With the -x option, the number of input lines must be a multiple of 4: you have to list the two empty partitions you never want using two blank lines. Without the -x option, you give one line for the partitions inside an extended partition, instead of four, and terminate with end-of-file (^D). And sfdisk will assume your input line represents the first of four, that the second one is extended, and the 3rd and 4th are empty.
Related commands
cfdisk — A more user-friendly version of the fdisk disk partitioning utility.fdisk — A disk partitioning utility.mkfs — Build a Linux file system, usually a hard disk partition.parted — A disk partition manipulation program.partprobe — Inform the operating system about changes to the partition table.