Linux

15 Linux sfdisk Command Examples to Manage Disk Partitions

[ad_1]

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:

  1. View Existing Disk Partitions
  2. Change Display Units (Display Size in MB)
  3. Create New Disk Partitions (Interactive Method)
  4. Verify Partition Table Consistency
  5. Display Total Partition Size
  6. Dump Disk Partition Details to Text File
  7. Create New Partitions from Text File (Non-interactive Method)
  8. Display Extended Partition Details
  9. Display Disk Geometry for Debugging
  10. Display All Disk Partition types
  11. Manipulate Only a Single Disk Partition
  12. Dry-run Mode for Verification
  13. Mark a Specific Partition as Bootable
  14. Change Partition Type
  15. 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.

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.

Instead of -l, you can also use –list as shown below.

If you are new to managing hard disk, you may also want to understand how to use the regular trusted fdisk command.

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.

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)

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.

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.

Next, it will display the current partition information on this device. As seen from here, currently there are no partition on sdf device.

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.
Now, sfdisk will display the partitions that it will create. As shown below, it will be creating two partitions (sdf1 and sdf2).
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.
Finally, I verified that the partitions got created successfully using sfdisk -l as shown below.

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.

You can also verify only a specific device as shown below.

Also, you can combine V and l option as shown below.

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.

 

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.

You can also specify the individual hard disk (or) partition as shown below.

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.

You can also dump the partition information of a particular device as shown below.

To dump it to a text file, do the following. This will dump the partition information of sda into sda.out text file.

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.

Now, use the sda.out file from the previous example, and use it to create similar partitions on the sdf device as shown below.

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.

Now, as you see from the following output sdf has one new partition, which is similar to sda.

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.

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.

-g option (lower-case g) will display the kernel’s idea of the geometry of the device.

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.

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.

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.

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.

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.

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.

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.

The following output 0 indicates that the sdf2 partition is currently empty.

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)

As you see from the following output, now the sdf2 is set to Linux partition type (.i.e Look at the last column “System”)

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.

Now, execute the following command, which will automatically create three partitions (sdf1, sdf2, sdf3) using the values provided below.

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.

Verify that the three partitions got created automatically using the provided values.

 

[ad_2]

Facebook Comments

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button