Introduction
In this lab, we will explore the Linux sfdisk command, a powerful tool for managing disk partitions. We will learn how to use sfdisk to create, delete, and modify disk partitions without the need for a graphical user interface. We will also cover how to backup and restore partition tables using sfdisk. This lab is designed to provide practical examples and hands-on experience with the sfdisk command, which is a valuable skill for system administrators and Linux enthusiasts.
Introduction to sfdisk Command
In this step, we will explore the sfdisk command, which is a powerful tool for managing disk partitions in Linux. The sfdisk command allows you to create, delete, and modify disk partitions without the need for a graphical user interface.
First, let's check the version of sfdisk installed on our system:
sfdisk --version
Example output:
sfdisk from util-linux 2.38
Next, we can use the sfdisk command to list the current partitions on a disk. Let's assume we have a disk 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, including the start and end sectors, size, and partition type.
In the next step, we will learn how to partition a disk using the sfdisk command.
Partitioning a Disk with sfdisk
In this step, we will learn how to use the sfdisk command to partition a disk.
First, let's create a new virtual disk that we can use for this example. We'll create a 20 GB disk and attach it to our Ubuntu container:
sudo fallocate -l 20G /tmp/disk.img
sudo losetup /dev/loop0 /tmp/disk.img
Now, we can use sfdisk to create 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 creates a new DOS-style partition table with a single Linux partition that occupies the entire disk.
Let's verify the new 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 shows that we have successfully created a new 20 GB Linux partition on the /dev/loop0 disk.
In the next step, we will learn how to backup and restore partition tables using the sfdisk command.
Backup and Restore Partition Table with sfdisk
In this final step, we will learn how to backup and restore partition tables using the sfdisk command.
First, let's backup the partition table of the /dev/loop0 disk:
sudo sfdisk -d /dev/loop0 > partition_backup.txt
This command creates a text file named partition_backup.txt that contains the current partition table configuration.
Now, let's simulate a scenario where we need to restore the partition table. We can do this by first deleting the existing partition:
sudo sfdisk -d /dev/loop0 | sudo sfdisk --force /dev/loop0 -X
This command deletes the existing partition on the /dev/loop0 disk.
To restore the partition table, we can 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 verify that the partition table has been 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 shows that the partition table has been successfully restored.
Congratulations! You have now learned how to use the sfdisk command to partition disks and backup/restore partition tables in Linux.
Summary
In this lab, you learned how to use the sfdisk command, a powerful tool for managing disk partitions in Linux. You explored the basic usage of sfdisk, including checking the version, listing current partitions, and creating a new partition table on a disk. You learned how to create a new DOS-style partition table with a single Linux partition that occupies the entire disk. This hands-on experience with sfdisk provides a solid foundation for managing disk partitions in a Linux environment.



