sfdisk Command in Linux



The sfdisk command is a crucial tool in the Linux ecosystem for handling partition tables. It offers a level of control that is both powerful and efficient. It stands out as a versatile partitioning utility, capable of creating, modifying, and deleting partitions, ensuring your system's storage is configured just the way you need it.

Unlike some other partitioning tools, sfdisk operates with a simplicity that belies its power, allowing both novice and experienced users to make precise adjustments to their disk configurations. Whether you're setting up a new system, resizing partitions to optimize space, or backing up your current partition layout, sfdisk offers the functionality you need to keep your disk management tasks streamlined and effective.

Table of Contents

Here is a comprehensive guide to the options available with the sfdisk command −

Syntax of sfdisk Command

The general syntax for the sfdisk command is as follows −

sfdisk [options] device

Here −

  • [options] specifies the various options that can be applied to control the behavior of sfdisk.
  • device represents the disk device you are operating on (e.g., /dev/sda).

sfdisk Command Options

Listed below are some options that can be utilized with the Linux sfdisk command −

Option Description
-N partition-number Apply changes to a specified partition.
-A, --activate Toggle the bootable flag for specified partitions.
--backup-pt-sectors Backup the partition table sectors in binary format.
--delete Delete all or specified partitions.
-d, --dump Dump the partitions of a device in a format usable as input for sfdisk.
-g, --show-geometry List the geometry of all or specified devices.
-J, --json Dump the partitions of a device in JSON format.
-l, --list List the partitions of all or specified devices.
-F, --list-free List the free unpartitioned areas on all or specified devices.
--part-attrs Change the GPT partition attribute bits.
--part-label Change the GPT partition name (label).
--part-type Change the partition type.
--part-uuid Change the GPT partition UUID.
--disk-id Change the disk identifier.
--discard-free Discard any unused (unpartitioned) sectors on the device.
-r, --reorder Renumber the partitions, ordering them by their start offset.
-s, --show-size List the sizes of all or specified devices.
-T, --list-types Print all supported types for the current disk label or the label specified by --label.
-V, --verify Test whether the partition table and partitions seem correct.
--relocate Relocate the partition table header, currently supported for GPT header only.
-a, --append Append specified partitions without creating a new partition table.
-b, --backup Back up the current partition table sectors before partitioning.
--color[=when] Colorize the output with the optional argument for auto, never, or always.
-f, --force Disable all consistency checking.
--Linux Deprecated and ignored option.
--lock[=mode] Use an exclusive BSD lock for the device or file it operates on.
-n, --no-act Do everything except writing to the device.
--no-reread Do not check if the device is in use after writing the partition table.
--no-tell-kernel Don’t inform the kernel about partition changes.
-O, --backup-file path Override the default backup file name.
--move-data[=path] Move data after partition relocation.
--move-use-fsync Use the fsync(2) system call after each write when moving data.
-o, --output list Specify which output columns to print.
-q, --quiet Suppress extra info messages.
--sector-size sectorsize Specify the sector size of the disk.
-u, --unit S Deprecated option; only sector unit supported.
-X, --label type Specify the disk label type.
-Y, --label-nested type Force editing of a nested disk label.
-w, --wipe when Wipe filesystem, RAID, and partition-table signatures from the device.
-W, --wipe-partitions when Wipe signatures from a newly created partition.

Examples of sfdisk Command in Linux

Here are some practical scenarios where sfdisk can be effectively used −

  • Listing the Partition Table
  • Creating a New Partition
  • Dumping the Partition Table
  • Resizing a Partition
  • Activating a Partition

Listing the Partition Table

When you want to see the current partitions on your disk, use −

sudo sfdisk -l /dev/sda

This command outputs the partition table of /dev/sda, showing you details like the start and end sectors, sizes, and partition types.

Listing the Partition Table Using sfdisk

Creating a New Partition

To create a new partition, you can use −

echo ', +100M,83' | sudo sfdisk /dev/sda

This command pipes the partition specifications into sfdisk. The , +100M,83 part means you're creating a new partition of 100MB, of type Linux (83).

Dumping the Partition Table

In case you need to save the current partition table to a file for backup or replication, use −

sudo sfdisk -d /dev/sda > partitions.txt

This command dumps the partition table of /dev/sda into a text file named partitions.txt.

Dumping the Partition Table Using sfdisk

Resizing a Partition

If you need to resize an existing partition, try this −

echo ', +500M' | sfdisk -N 1 /dev/sda

Here, you're resizing the first partition (-N 1) on /dev/sda to an additional 500MB.

Activating a Partition

To make a partition bootable, you can run −

sudo sfdisk -A /dev/sda 1

This command activates the first partition on /dev/sda, setting the bootable flag.

Activating a Partition Using sfdisk

Conclusion

The sfdisk command in Linux is a powerful utility for managing disk partitions. By understanding its purpose, syntax, options, and practical usage scenarios, you can effectively control the partition tables on your disks.

Whether you need to list partitions, create new ones, dump partition tables, resize partitions, or activate them, mastering the sfdisk command provides a flexible and powerful solution. Incorporating the sfdisk command into your system administration toolkit enhances your ability to manage disk partitions seamlessly.

Advertisements