If you are a sysadmin, managing your Linux server’s hard disk partition is a critical task.
In this tutorial, we’ll focus on how to use sfdisk command-line utility to manage your hard disk partitions.
While sfdisk is similar to fdisk, there are few partition manipulation activities that are very easy to perform in sfdisk. For example, you can create new partitions in an non-interactive method using sfdisk.
The following sfdisk activities are covered in this tutorial:
- View Existing Disk Partitions
- Change Display Units (Display Size in MB)
- Create New Disk Partitions (Interactive Method)
- Verify Partition Table Consistency
- Display Total Partition Size
- Dump Disk Partition Details to Text File
- Create New Partitions from Text File (Non-interactive Method)
- Display Extended Partition Details
- Display Disk Geometry for Debugging
- Display All Disk Partition types
- Manipulate Only a Single Disk Partition
- Dry-run Mode for Verification
- Mark a Specific Partition as Bootable
- Change Partition Type
- Create New Partitions from Shell Script (Non-interactive Method)
1. View Existing Disk Partitions
Using -l option you can view all your disk partitions. -l stands for list.
By default this will display partitions of all the disks on your systems.
# sfdisk -l Disk /dev/sda: 1044 cylinders, 255 heads, 63 sectors/track Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/sda1 * 0+ 1044- 1045- 8387584 83 Linux /dev/sda2 0 - 0 0 0 Empty /dev/sda3 0 - 0 0 0 Empty /dev/sda4 0 - 0 0 0 Empty Disk /dev/sdf: 1044 cylinders, 255 heads, 63 sectors/track
In the above:
- It displays the partitions for both sda and sdf device.
- sda has only one partition. i.e sda1. But, the -l option will still always display all the 4 primary partition information. Since, in this example, sda has only one partition (i.e sda1), the remaining 3 partitions (sda2, sda3, sda4) will says “Empty” in the last column.
- The device sdf doesn’t have any partition. i.e Nothing has been created on this device yet. So, it doesn’t list anything for this device.
You can also view disk partitions of a specific device by passing the device name as shown below.
# sfdisk -l /dev/sdf
Instead of -l, you can also use –list as shown below.
sfdisk --list sfdisk --list /dev/sdf
If you are new to managing hard disk, you may also want to understand how to use the regular trusted fdisk command: 7 Linux fdisk Command Examples to Manage Hard Disk Partition
2. Change Display Units (Display Size in MB)
By default, sfdisk will display the size in blocks. In the above sfdisk -l output, look at the column called “#blocks”.
However, if you want to display the size in MB, then use the -u option as shown below. -u stands for Unit. You have to specify the unit type. In this example, the unit type we are specifying is M (which is MB)
-uM option will replace the “#cyls” column from the above output with “MiB” (size in MB) as shown below.
# sfdisk -l -uM Disk /dev/sdf: 1044 cylinders, 255 heads, 63 sectors/track Units: 1MiB = 1024*1024 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End MiB #blocks Id System /dev/sdf1 0+ 8032- 8033- 8225279+ 83 Linux /dev/sdf2 8032+ 8189- 157- 160650 83 Linux /dev/sdf3 0 - 0 0 0 Empty /dev/sdf4 0 - 0 0 0 Empty
The following are the different unit types that you can specify:
- M for MB
- C for number of cylinders (this is the default)
- S for number of sectors
- B for number of blocks (this is also display in the default output along with #cylinders)
# sfdisk -l -uM # sfdisk -l -uC # sfdisk -l -uS # sfdisk -l -uB
3. Create New Disk Partitions (Interactive Method)
In the following example, we’ll take an empty hard disk (sdf) and create partitions on it using sfdisk.
# sfdisk /dev/sdf
The following is the output of the above command broken down into multiple section to understand it easily.
First, it will check the sdf to make sure it is not currently mounted or getting used by somebody else.
Checking that no-one is using this disk right now ... OK
Next, it will display the current partition information on this device. As seen from here, currently there are no partition on sdf device.
Disk /dev/sdf: 1044 cylinders, 255 heads, 63 sectors/track sfdisk: /dev/sdf: unrecognized partition table type Old situation: sfdisk: No partitions found
Next, it will ask you to enter partition size for all 4 primary partition. Even if you want to create only one partition, it will prompt you 4 different time for all primary partitions.
In the following:
- In this example, I like to create two partition.
- So, when it prompted for “/dev/sdf1 :”, I’m creating the 1st partition. Here, I entered “0 1024”. 0 is the start of the cylinder, and 1024 is the end of the cylinder for the 1st partition.
- Next, when it prompted for “/dev/sdf2 :”, I’m creating the 2nd partition. Here, I just pressed enter, which will take all the remaining space and create the 2nd partition.
- Next, I just pressed enter for sdf3 and sdf4. Since there is no space left on sdf, it will just be Empty as shown below.
- Also, please note that by default it will create the partition as “Linux” type. If you want to change it to different type, you should specify the type.
Input in the following format; absent fields get a default value. <start> <size> <type [E,S,L,X,hex]> <bootable [-,*]> <c,h,s> <c,h,s> Usually you only need to specify <start> and <size> (and perhaps <type>). /dev/sdf1 :0 1024 /dev/sdf1 0+ 1023 1024- 8225279+ 83 Linux /dev/sdf2 : /dev/sdf2 1024 1043 20 160650 83 Linux /dev/sdf3 : /dev/sdf3 0 - 0 0 0 Empty /dev/sdf4 : /dev/sdf4 0 - 0 0 0 Empty
Now, sfdisk will display the partitions that it will create. As shown below, it will be creating two partitions (sdf1 and sdf2).
New situation: Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/sdf1 0+ 1023 1024- 8225279+ 83 Linux /dev/sdf2 1024 1043 20 160650 83 Linux /dev/sdf3 0 - 0 0 0 Empty /dev/sdf4 0 - 0 0 0 Empty Warning: no primary partition is marked bootable (active) This does not matter for LILO, but the DOS MBR will not boot this disk.
Finally, it will ask for confirmation. If you’ve made a mistake and don’t want to create the above partitions, type “q” to exit. Here, I typed y to create these two new partitions.
Do you want to write this to disk? [ynq] y Successfully wrote the new partition table Re-reading the partition table ... If you created or changed a DOS partition, /dev/foo7, say, then use dd(1) to zero the first 512 bytes: dd if=/dev/zero of=/dev/foo7 bs=512 count=1 (See fdisk(8).)
Finally, I verified that the partitions got created successfully using sfdisk -l as shown below.
# sfdisk -l /dev/sdf Disk /dev/sdf: 1044 cylinders, 255 heads, 63 sectors/track Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/sdf1 0+ 1023 1024- 8225279+ 83 Linux /dev/sdf2 1024 1043 20 160650 83 Linux /dev/sdf3 0 - 0 0 0 Empty /dev/sdf4 0 - 0 0 0 Empty
On a related note, for creating large size partitions, refer to this: How to Create Partition on Linux for >2TB Size using Parted GPT
4. Verify Partition Table Consistency
Use -V (upper-case V) option to perform consistency check on the partition tables. V stands for verify.
As shown in the following output, it will say “OK” when everything is good. This will also display any warning messages.
# sfdisk -V Warning: partition 1 does not end at a cylinder boundary /dev/sda: OK Warning: no primary partition is marked bootable (active) This does not matter for LILO, but the DOS MBR will not boot this disk. /dev/sdf: OK
You can also verify only a specific device as shown below.
# sfdisk -V /dev/sdf
Also, you can combine V and l option as shown below.
sfdisk -lV
If you are performing the verification from inside a shell script, then you can use -q option along with V. q stands for quiet. In this case, it will not display any output. But, you can check the staus of the command to check whether the consistency check passed or failed as shown below.
# sfdisk -Vq # echo $? 0
If you want to check the consistency of the disk content itself, then use this method: 10 Linux Fsck Command Examples to Check and Repair Filesystem
5. Display Total Partition Size
To display the total size of the disk including all the partitions, use the -s option. -s stands for size.
This will display the total partition size in number of blocks.
By default, this will display the total partition size for each and every hard disk on your system. Also, it will display the total size for all the disks on your system as shown below.
# sfdisk -s /dev/sda: 8388608 /dev/sdf: 8388608 total: 16777216 blocks
You can also specify the individual hard disk (or) partition as shown below.
# sfdisk -s /dev/sdf 8388608 # sfdisk -s /dev/sdf1 1654694
6. Dump Disk Partition Details to Text File
sfdisk also provides the option for you to dump all the disk partition information into a text file.
For this use the -d option. -d stands for dump.
By default, -d will dump the output on the screen. This will include the partition information for all the devices on your system.
# sfdisk -d
You can also dump the partition information of a particular device as shown below.
# sfdisk -d /dev/sda
To dump it to a text file, do the following. This will dump the partition information of sda into sda.out text file.
# sfdisk -d /dev/sda > sda.out # cat sda.out # partition table of /dev/sda unit: sectors /dev/sda1 : start= 2048, size= 16775168, Id=83, bootable /dev/sda2 : start= 0, size= 0, Id= 0 /dev/sda3 : start= 0, size= 0, Id= 0 /dev/sda4 : start= 0, size= 0, Id= 0
7. Create New Partitions from Text File (Non-interactive Method)
The beauty of the -d option is that you can take a partition dump of a particular device, and use that to create a similar partition dump on another device.
For example, if you want to replicate the current partition on sda device onto sdf device, then you can use the -d option as shown below.
First, take the dump of sda device as shown in the previous example to sda.out file.
In the above example, as we see we have one partition on sda device.
Now, on the sdf device, currently we have two partitions.
# sfdisk -l /dev/sdf Disk /dev/sdf: 1044 cylinders, 255 heads, 63 sectors/track Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/sdf1 0+ 499 500- 4016249+ 83 Linux /dev/sdf2 500 1043 544 4369680 83 Linux /dev/sdf3 0 - 0 0 0 Empty /dev/sdf4 0 - 0 0 0 Empty
Now, use the sda.out file from the previous example, and use it to create similar partitions on the sdf device as shown below.
# sfdisk /dev/sdf < sda.out
The above command will use the partition information from sda.out file and use it to create new partition on sdf automatically without asking any input from the user as shown below.
Checking that no-one is using this disk right now ... OK Disk /dev/sdf: 1044 cylinders, 255 heads, 63 sectors/track Old situation: Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/sdf1 0+ 499 500- 4016249+ 83 Linux /dev/sdf2 500 1043 544 4369680 83 Linux /dev/sdf3 0 - 0 0 0 Empty /dev/sdf4 0 - 0 0 0 Empty New situation: Units: sectors of 512 bytes, counting from 0 Device Boot Start End #sectors Id System /dev/sdf1 * 2048 16777215 16775168 83 Linux /dev/sdf2 0 - 0 0 Empty /dev/sdf3 0 - 0 0 Empty /dev/sdf4 0 - 0 0 Empty Warning: partition 1 does not end at a cylinder boundary Successfully wrote the new partition table Re-reading the partition table ... If you created or changed a DOS partition, /dev/foo7, say, then use dd(1) to zero the first 512 bytes: dd if=/dev/zero of=/dev/foo7 bs=512 count=1 (See fdisk(8).)
Now, as you see from the following output sdf has one new partition, which is similar to sda.
# sfdisk -l Disk /dev/sda: 1044 cylinders, 255 heads, 63 sectors/track Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/sda1 * 0+ 1044- 1045- 8387584 83 Linux /dev/sda2 0 - 0 0 0 Empty /dev/sda3 0 - 0 0 0 Empty /dev/sda4 0 - 0 0 0 Empty Disk /dev/sdf: 1044 cylinders, 255 heads, 63 sectors/track Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/sdf1 * 0+ 1044- 1045- 8387584 83 Linux /dev/sdf2 0 - 0 0 0 Empty /dev/sdf3 0 - 0 0 0 Empty /dev/sdf4 0 - 0 0 0 Empty
8. Display Extended Partition Details
x stands for eXtended partition. When you combine x along with l, then it will also display the non-primary extended partitions as shown below.
# sfdisk -lx /dev/sdf Disk /dev/sdf: 1044 cylinders, 255 heads, 63 sectors/track Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/sdf1 0+ 205 206- 1654694+ 83 Linux /dev/sdf2 206 411 206 1654695 83 Linux /dev/sdf3 412 514 103 827347+ 82 Linux swap / Solaris /dev/sdf4 515 1043 529 4249192+ 5 Extended /dev/sdf5 515+ 617 103- 827347 83 Linux - 618 1043 426 3421845 5 Extended - 515 514 0 0 0 Empty - 515 514 0 0 0 Empty /dev/sdf6 618+ 1043 426- 3421844+ 83 Linux - 618 617 0 0 0 Empty - 618 617 0 0 0 Empty - 618 617 0 0 0 Empty
You can also use -x option when you are creating new partitions in which case, sfdisk will ask for (or expect) input descriptors for them.
9. Display Disk Geometry for Debugging
When you are debugging any issues related to partitions tables, one of the things that you should verify is whether partition table information is same on the device as it is understood by kernel.
It is a good idea to verify that the kernel’s idea of the geometry of the device is same as the real geometry of the device by directly reading the partition table.
-G option (upper-case G) will display the real geometry of the device by directly reading the partition table as shown below.
# sfdisk -G /dev/sda: 1044 cylinders, 255 heads, 63 sectors/track /dev/sdf: 1044 cylinders, 255 heads, 63 sectors/track
-g option (lower-case g) will display the kernel’s idea of the geometry of the device.
# sfdisk -g /dev/sda: 1044 cylinders, 255 heads, 63 sectors/track /dev/sdf: 1044 cylinders, 255 heads, 63 sectors/track
In this example, both the output matches like it should. If this doesn’t match, then there is some problem.
10. Display All Disk Partition types
To view all the supported partition types that can be used while creating new partitions using sfdisk, use the -T option. T stands for type (i.e partition type).
This will display the partition type id, and descriptive name. While creating the partition you’ll specify the partition type id. For example, use 82 as type for swap partition.
# sfdisk -T Id Name 0 Empty 1 FAT12 2 XENIX root 3 XENIX usr 4 FAT16 <32M 5 Extended 6 FAT16 .. .. 82 Linux swap / Solaris 83 Linux 84 OS/2 hidden C: drive 85 Linux extended 86 NTFS volume set 87 NTFS volume set 88 Linux plaintext 8e Linux LVM .. ..
Regarding swap space: 2 Ways to Add Swap Space Using dd, mkswap and swapon
11. Manipulate Only a Single Disk Partition
You can use -N option to manipulate a particular disk partition. -N stands for number (i.e partition number).
You can use this to create a new partition on an existing disk which already have other partitions.
In the following example, in the sdf device, we already have one partition. But, we want to create a 2nd partition.
# sfdisk /dev/sdf -N2 Checking that no-one is using this disk right now ... OK Disk /dev/sdf: 1044 cylinders, 255 heads, 63 sectors/track Old situation: Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/sdf1 0+ 511 512- 4112639+ 83 Linux /dev/sdf2 0 - 0 0 0 Empty /dev/sdf3 0 - 0 0 0 Empty /dev/sdf4 0 - 0 0 0 Empty Input in the following format; absent fields get a default value. <start> <size> <type [E,S,L,X,hex]> <bootable [-,*]> <c,h,s> <c,h,s> Usually you only need to specify <start> and <size> (and perhaps <type>). /dev/sdf2 :512 1024 Warning: given size (1024) exceeds max allowable size (532) /dev/sdf2 :512 532 /dev/sdf2 512 1043 532 4273290 0 Empty New situation: Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/sdf1 0+ 511 512- 4112639+ 83 Linux /dev/sdf2 512 1043 532 4273290 0 Empty /dev/sdf3 0 - 0 0 0 Empty /dev/sdf4 0 - 0 0 0 Empty Warning: no primary partition is marked bootable (active) This does not matter for LILO, but the DOS MBR will not boot this disk. Do you want to write this to disk? [ynq] y Successfully wrote the new partition table Re-reading the partition table ... If you created or changed a DOS partition, /dev/foo7, say, then use dd(1) to zero the first 512 bytes: dd if=/dev/zero of=/dev/foo7 bs=512 count=1 (See fdisk(8).)
In the above:
- We used -N2 option to manipulate only the 2nd partition.
- This which means that, sfdisk will ask you for the partition information only for sdf2 as shown above.
- This will not modify the existing sdf1 partition.
- Also, this will not prompt you for information on sdf3 and sdf4
12. Dry-run Mode for Verification
Before you really create a new partition, or change any information on the partition tables, you want to make sure whether the syntax of the command you have is all good.
In that case, it is best to use the -n option, which will really go through all the motions without really doing anything.
-n option is a dry-run option, which will go through whatever the command is supposed to do, but will not save anything, or change anything.
For example, when you are changing a partition type, you can use -n option, which will go through the command but will not really change anything as shown below.
# sfdisk -n --change-id /dev/sdf 1 82 sfdisk: -n flag was given: Nothing changed
You can also use this when you are creating new partitions as shown below. This will go through the command and display everything in the output, but without really creating new partitions.
sfdisk -n /dev/sdf < sda.out
13. Mark a Specific Partition as Bootable
Use -A option to mark a specific partition as boot partition. -A stands for activate.
The following example is activating 2nd partition (sdf2) as active partition (i.e marking as boot). Please note that if any other partition on this device (sdf) is already marked for boot, they’ll be unmarked now.
# sfdisk -A2 /dev/sdf Done
As you see from the following output, only sdf2 is made active. The “*” in the 2nd column (i.e Boot) indicates that this is marked as boot partition.
# sfdisk -l /dev/sdf Disk /dev/sdf: 1044 cylinders, 255 heads, 63 sectors/track Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/sdf1 0+ 511 512- 4112639+ 83 Linux /dev/sdf2 * 512 1043 532 4273290 0 Empty /dev/sdf3 0 - 0 0 0 Empty /dev/sdf4 0 - 0 0 0 Empty
14. Change Partition Type
You can change partition type of an existing partition.
First, use the –print-id option to display the partition type of a specific partition. The following command will display the partition type of sdf1. Here 83 in the output indicates that sdf1 is Linux partition type.
# sfdisk --print-id /dev/sdf 1 83
The following output 0 indicates that the sdf2 partition is currently empty.
# sfdisk --print-id /dev/sdf 2 0
Now, use the –change-id option as shown below to change the partition type of a partition device.
In this example, we are changing the partition type of sdf2 partition to 83 (which is Linux partition)
# sfdisk --change-id /dev/sdf 2 83 Done
As you see from the following output, now the sdf2 is set to Linux partition type (.i.e Look at the last column “System”)
# sfdisk -l /dev/sdf Disk /dev/sdf: 1044 cylinders, 255 heads, 63 sectors/track Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/sdf1 0+ 511 512- 4112639+ 83 Linux /dev/sdf2 * 512 1043 532 4273290 83 Linux /dev/sdf3 0 - 0 0 0 Empty /dev/sdf4 0 - 0 0 0 Empty
15. Create New Partitions from Shell Script (Non-interactive Method)
One of the great benefit of sfdisk is that you can create partition on-the-fly from command-line without any interaction from sfdisk.
This is very helpful when you want to automate the partition creation from a shell-script.
Currently, as you see from the following output, the sdf device is currently empty. This doesn’t have any partitions.
# sfdisk -l /dev/sdf Disk /dev/sdf: 1044 cylinders, 255 heads, 63 sectors/track Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/sdf1 0 - 0 0 0 Empty /dev/sdf2 0 - 0 0 0 Empty /dev/sdf3 0 - 0 0 0 Empty /dev/sdf4 0 - 0 0 0 Empty
Now, execute the following command, which will automatically create three partitions (sdf1, sdf2, sdf3) using the values provided below.
# sfdisk /dev/sdf <<EOF 0,512 ,512 ; ; EOF
In the above:
- There should be 4 lines in the above input for all 4 primary partitions.
- The first line indicates that it should start from 1st cylinder until 512 to create 1st partition (i.e sdf1)
- The second line indicates that it should start from the last available cylinder as the first cylinder and from there use +512 cylinders to create the 2nd partition (i.e sdf2)
- The third line has semi-colon, which indicates that use the default values, and don’t prompt anything from the user for the 3rd partition. In this case, it will use all remaining cylinders on the disk and create 3rd partition (i.e sdf3)
- The fourth line has semi-colon, which indicates that use the default values, and don’t prompt anything from the user for the 4th partition. In this case, since there is no space left, it will not create anything for 4th partition (i.e sdf4)
The following is the output of the above sfdisk command. Please note that this is not asking for any input from the user.
Checking that no-one is using this disk right now ... OK Disk /dev/sdf: 1044 cylinders, 255 heads, 63 sectors/track Old situation: Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/sdf1 0 - 0 0 0 Empty /dev/sdf2 0 - 0 0 0 Empty /dev/sdf3 0 - 0 0 0 Empty /dev/sdf4 0 - 0 0 0 Empty New situation: Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/sdf1 0+ 511 512- 4112639+ 83 Linux /dev/sdf2 512 1023 512 4112640 83 Linux /dev/sdf3 1024 1043 20 160650 83 Linux /dev/sdf4 0 - 0 0 0 Empty Warning: no primary partition is marked bootable (active) This does not matter for LILO, but the DOS MBR will not boot this disk. Successfully wrote the new partition table Re-reading the partition table ... If you created or changed a DOS partition, /dev/foo7, say, then use dd(1) to zero the first 512 bytes: dd if=/dev/zero of=/dev/foo7 bs=512 count=1 (See fdisk(8).)
Verify that the three partitions got created automatically using the provided values.
# sfdisk -l /dev/sdf Disk /dev/sdf: 1044 cylinders, 255 heads, 63 sectors/track Units: cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/sdf1 0+ 511 512- 4112639+ 83 Linux /dev/sdf2 512 1023 512 4112640 83 Linux /dev/sdf3 1024 1043 20 160650 83 Linux /dev/sdf4 0 - 0 0 0 Empty
Comments on this entry are closed.
LANG=C sfdisk -h | grep unit
-u, –unit S deprecated, only sector unit is supported
sfdisk -v
sfdisk z util-linux 2.29.2