Section: System Administration (8)
Updated: August 2011
sfdisk [options] device
sfdisk has four (main) uses: list the size of a partition, list the partitions on a device, check the partitions on a device, and - very dangerous - repartition a device.
sfdisk -s partition gives the size of partition in blocks. This may be useful in connection with programs like mkswap?(8). Here partition is usually something like /dev/hda1 or /dev/sdb12, but may also be an entire disk, like /dev/xda.
% sfdisk -s /dev/hda9 81599
If the partition argument is omitted, sfdisk will list the sizes of all block devices, and the total:
% sfdisk -s /dev/hda: 208896 /dev/hdb: 1025136 /dev/hdc: 1031063 /dev/sda: 8877895 /dev/sdb: 1758927 total: 12901917 blocks
The second type of invocation: sfdisk -l device will list the partitions on the specified device. If the device argument is omitted, the partitions on all block devices are listed.
% 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 unit (-u S).
The third type of invocation: sfdisk -V device will apply various consistency checks to the partition tables on device. It prints `OK' or complains. The -V option can be used together with -l. In a shell script one might use sfdisk -V -q device which only returns a status.
The fourth type of invocation: sfdisk device will cause sfdisk to read the specification for the desired partitioning of device from standard input, and then to change the partition tables on that block device. 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, one can save the sectors changed by sfdisk:
% sfdisk /dev/hdd -O hdd-partition-sectors.save ...
Then, if you discover that you did something stupid before anything else has been written to the block device, it may be possible to recover the old situation with:
% sfdisk /dev/hdd -I hdd-partition-sectors.save
(This 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 block device, 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.)
There are many options.
% sfdisk -d /dev/hda > hda.out % sfdisk /dev/hda < hda.out
will correct the bad last extended partition that the OS/2 fdisk creates.:
% sfdisk /dev/hdb -N5 ,,,*
will make the fifth partition on /dev/hdb bootable (`active') and change nothing else. (Probably this fifth partition is called /dev/hdb5, but you are free to call it something else, like `/my_equipment/disks/2/5' or so).:
% sfdisk --activate /dev/sdaWhen a device name is given as option argument, the partitions specified as command argument will have the bootable flag switched on. Other partitions for the same device will have the bootable flag cleared. For example, with the following command the partitions 1 and 4 are set to be bootable, while 2 and 3 are cleared:
% sfdisk --activate=/dev/sda 1 4If only a single partition needs to be activated, then the partition number must be given as option argument, and the device as command argument. For example:
% sfdisk --activate=1 /dev/sdaThe activate option is turned by default on when the program's invocation name is activate.:
% sfdisk --print-id /dev/hdb 5 6 % sfdisk --change-id /dev/hdb 5 83 OK
first reports that /dev/hdb5 has Id 6, and then changes that into 83.:
% sfdisk --list-types | grep HiddenNotice that the Hidden NTFS WinRE (Windows Recovery Environment) does not have non-hidden equivalent.:
Block 0 of a block device (the Master Boot Record) contains among other things four partition descriptors. The partitions described here are called primary partitions.
A partition descriptor has 6 fields:
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 block devices (say > 8 GB). In fact, due to the wasteful representation (that uses a byte for the number of heads, which is typically 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 has been started. For more details, see the 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, you should just 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 the process of finding them is more involved than just 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.
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 <start> and <size> and the block device geometry as given by the kernel or specified using the -H, -S, -C flags.
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 might play a role for certain boot loaders and for other operating systems. For example, when there are 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-device).
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.
sfdisk /dev/hdc << EOF 0,407 ,407 ; ; EOF
will partition /dev/hdc just as indicated above.
sfdisk /dev/hdb << EOF ,3,L ,60,L ,19,S ,,E ,130,L ,130,L ,130,L ,,L EOF
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 that you never want using two blank lines. Without the -x option, you give one line for the partitions inside a extended partition, instead of four, and terminate with end-of-file (^D). (And sfdisk will assume that your input line represents the first of four, that the second one is extended, and the 3rd and 4th are empty.)
The DOS 6.x FORMAT command looks for some information in the first sector of the data area of the partition, and treats this information as more reliable than the information in the partition table. DOS FORMAT expects DOS FDISK to clear the first 512 bytes of the data area of a partition whenever a size change occurs. DOS FORMAT will look at this extra information even if the /U flag is given - we consider this a bug in DOS FORMAT and DOS FDISK.
The bottom line is that if you use sfdisk to change the size of a DOS partition table entry, then you must also use dd to zero the first 512 bytes of that partition before using DOS FORMAT to format the partition. For example, if you were using sfdisk to make a DOS partition table entry for /dev/hda1, then (after exiting sfdisk and rebooting Linux so that the partition table information is valid) you would use the command "dd if=/dev/zero of=/dev/hda1 bs=512 count=1" to zero the first 512 bytes of the partition. BE EXTREMELY CAREFUL if you use the dd command, since a small typo can make all of the data on your block device useless.
For best results, you should always use an OS-specific partition table program. For example, you should make DOS partitions with the DOS FDISK program and Linux partitions with the Linux sfdisk program.
Stephen Tweedie reported (930515): `Most reports of superblock corruption turn out to be due to bad partitioning, with one filesystem overrunning the start of the next and corrupting its superblock. I have even had this problem with the supposedly-reliable DRDOS. This was quite possibly due to DRDOS-6.0's FDISK command. Unless I created a blank track or cylinder between the DRDOS partition and the immediately following one, DRDOS would happily stamp all over the start of the next partition. Mind you, as long as I keep a little free device space after any DRDOS partition, I don't have any other problems with the two coexisting on the one drive.'
A. V. Le Blanc writes in README.efdisk: `Dr. DOS 5.0 and 6.0 has been reported to have problems cooperating with Linux, and with this version of efdisk in particular. This efdisk sets the system type to hexadecimal 81. Dr. DOS seems to confuse this with hexadecimal 1, a DOS code. If you use Dr. DOS, use the efdisk command 't' to change the system code of any Linux partitions to some number less than hexadecimal 80; I suggest 41 and 42 for the moment.'
A. V. Le Blanc writes in his README.fdisk: `DR-DOS 5.0 and 6.0 are reported to have difficulties with partition ID codes of 80 or more. The Linux `fdisk' used to set the system type of new partitions to hexadecimal 81. DR-DOS seems to confuse this with hexadecimal 1, a DOS code. The values 82 for swap and 83 for file systems should not cause problems with DR-DOS. If they do, you may use the `fdisk' command `t' to change the system code of any Linux partitions to some number less than hexadecimal 80; I suggest 42 and 43 for the moment.'
In fact, it seems that only 4 bits are significant for the DRDOS FDISK, so that for example 11 and 21 are listed as DOS 2.0. However, DRDOS itself seems to use the full byte. I have not been able to reproduce any corruption with DRDOS or its fdisk.
There are too many options.
There is no support for non-DOS partition types.
The sfdisk command is part of the util-linux package and is available from ftp://ftp.kernel.org/pub/linux/utils/util-linux/.
Tutoriais de Tecnologia Web