Introduction to Linux Disk Partitioning with sfdisk
In this practical lab, we'll delve into the Linux sfdisk
command, a vital tool for effective disk partition management. Discover how to leverage sfdisk
to create, remove, and modify disk partitions efficiently, all without relying on a graphical interface. We'll also explore the crucial techniques of backing up and restoring partition tables using sfdisk
. This lab offers real-world examples and hands-on practice with the sfdisk
command, a highly valuable skill for system administrators and Linux enthusiasts alike.
Understanding the sfdisk Command: A System Admin's Essential Tool
This section introduces the sfdisk
command, a powerful utility for systemadmin tasks involving disk partition management within Linux environments. sfdisk
empowers you to create, delete, and modify disk partitions directly from the command line, bypassing the need for graphical tools.
First, let's verify the installed sfdisk
version on your system:
sfdisk --version
Example output:
sfdisk from util-linux 2.38
Next, we'll use sfdisk
to list the existing partitions on a designated disk. For this demonstration, let's assume the disk is located at /dev/sdb
:
sudo sfdisk -l /dev/sdb
Example output:
Disk /dev/sdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 41943039 41940992 20G 83 Linux
This command displays the partition table for the /dev/sdb
disk, providing critical information such as start and end sectors, size, and partition type.
In the following step, we'll explore how to partition a disk effectively using the sfdisk
command.
Step-by-Step Guide: Partitioning a Disk Using sfdisk
In this part, you'll learn how to partition a disk with the sfdisk
command.
Let's start by creating a new virtual disk for our practical example. We'll create a 20 GB disk and attach it to an Ubuntu container:
sudo fallocate -l 20G /tmp/disk.img
sudo losetup /dev/loop0 /tmp/disk.img
Now, we're ready to utilize sfdisk
to establish a new partition table on the /dev/loop0
disk:
sudo sfdisk /dev/loop0 << EOF
label: dos
unit: sectors
/dev/loop0p1 : start=2048, size=20971520, type=83
EOF
This command generates a fresh DOS-style partition table featuring a single Linux partition spanning the entire disk.
Let's confirm the validity of the newly created partition table:
sudo sfdisk -l /dev/loop0
Example output:
Disk /dev/loop0: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model:
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6d7f3a26
Device Boot Start End Sectors Size Id Type
/dev/loop0p1 2048 41943039 41940992 20G 83 Linux
The output confirms that we have successfully created a new 20 GB Linux partition on the /dev/loop0
disk.
Next, we'll explore how to backup and restore partition tables utilizing the sfdisk
command.
Safeguarding Your Data: Backup and Restore Partition Tables Using sfdisk
In this final step, we will focus on the essential task of backing up and restoring partition tables using the sfdisk
command.
First, let's create a backup of the partition table for the /dev/loop0
disk:
sudo sfdisk -d /dev/loop0 > partition_backup.txt
This command generates a text file named partition_backup.txt
, which encapsulates the current partition table configuration.
Now, let's simulate a scenario where we need to restore the partition table. We can achieve this by first deleting the existing partition:
sudo sfdisk -d /dev/loop0 | sudo sfdisk --force /dev/loop0 -X
This command removes the current partition on the /dev/loop0
disk.
To restore the partition table, we'll use the backup file we created earlier:
sudo sfdisk /dev/loop0 < partition_backup.txt
This command reads the partition table configuration from the partition_backup.txt
file and applies it to the /dev/loop0
disk.
Let's confirm that the partition table has been successfully restored:
sudo sfdisk -l /dev/loop0
Example output:
Disk /dev/loop0: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model:
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6d7f3a26
Device Boot Start End Sectors Size Id Type
/dev/loop0p1 2048 41943039 41940992 20G 83 Linux
The output confirms that the partition table has been restored successfully.
Congratulations! You've now acquired the skills to use the sfdisk
command for partitioning disks and backing up/restoring partition tables within Linux environments. As a root user, you can execute these commands.
Conclusion: Mastering Disk Partitioning with sfdisk
This lab has provided you with comprehensive knowledge of the sfdisk
command, a crucial tool for systemadmin tasks involving disk partition management in Linux. You've learned the fundamental usage of sfdisk
, including version verification, listing existing partitions, and creating new partition tables. You've also mastered the process of creating a new DOS-style partition table featuring a single Linux partition that occupies the entire disk. This hands-on experience with sfdisk
builds a strong foundation for effective disk partition management within Linux environments.