Introduction to Linux Disk Partitioning with fdisk
This comprehensive lab provides a hands-on guide to mastering disk partition management using the Linux fdisk
command. A critical tool for any systemadmin, fdisk
empowers you to create, delete, and resize disk partitions on your Linux systems. This tutorial delves into the purpose, syntax, and practical applications of fdisk
, complete with step-by-step examples demonstrating partition creation, deletion, and resizing. This lab is ideal for users seeking to effectively manage storage space and optimize their Linux system's performance.
Understanding the fdisk Command: Purpose and Syntax
This section focuses on the fundamental aspects of the fdisk
command within the Linux environment. You'll gain a solid understanding of its core purpose and learn the essential syntax required for effective use. The fdisk
command is a command-line utility designed specifically for creating, deleting, and manipulating disk partitions.
Let's begin by defining the primary function of the fdisk
command. Its primary role is to allow you to create new disk partitions, modify the size of pre-existing partitions, and alter the partition type. This functionality is vital for efficient storage management and is particularly beneficial when configuring and optimizing storage on your Linux system.
The general syntax of the fdisk
command is as follows:
sudo fdisk [options] [device]
Here's a breakdown of each component of the command:
sudo
: This prefix is crucial for executing the command with elevated privileges. Thefdisk
command necessitates root access to perform disk modifications.fdisk
: This is the command's name, invoking the disk partitioning utility.[options]
: These are optional flags that provide flexibility in customizing the behavior of thefdisk
command to suit specific needs.[device]
: This specifies the target disk device for manipulation, typically represented as/dev/sda
or similar.
Key options frequently used with the fdisk
command include:
-l
: This option lists the partition table associated with the specified device, providing a snapshot of the current partitioning scheme.-u
: This option dictates that sectors be used as the unit of measurement instead of cylinders, offering more granular control.-c=dos
: This option sets the compatibility mode to DOS, useful for certain legacy systems or specific partitioning requirements.
Example command output:
$ sudo fdisk -l /dev/sda
Disk /dev/sda: 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/sda1 2048 41943039 41940992 20G 83 Linux
In the example above, the fdisk -l /dev/sda
command is used to display the partition table information for the /dev/sda
device. This reveals a 20 GB virtual disk with a single partition, designated as /dev/sda1
, configured for Linux.
Creating New Partitions with the fdisk Command
This section guides you through the process of creating new partitions on your Linux system using the fdisk
command. Practical application is emphasized to solidify understanding.
Initially, let's examine the current partition layout on the /dev/sda
device:
$ sudo fdisk -l /dev/sda
Disk /dev/sda: 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/sda1 2048 41943039 41940992 20G 83 Linux
The output indicates the presence of one partition, /dev/sda1
, utilizing the entire 20 GB disk capacity.
Now, proceed to create a new partition using the fdisk
command as follows:
$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2):
First sector (2048-41943039, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943039, default 41943039): +10G
Created a new partition 2 of type 'Linux' and of size 10 GiB.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
In this detailed example:
- We initiate the
fdisk
interactive mode by executingsudo fdisk /dev/sda
. - We choose to create a new partition by typing
n
. - We opt for a primary partition (
p
) as the partition type. - We accept the default partition number
2
. - We accept the default starting sector
2048
. - We define the partition size as
+10G
, resulting in a 10 GB partition. - Finally, we write the changes to the disk by typing
w
.
Example output illustrating the newly created partition:
$ sudo fdisk -l /dev/sda
Disk /dev/sda: 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/sda1 2048 41943039 41940992 20G 83 Linux
/dev/sda2 41943040 61071359 19128320 10G 83 Linux
As evidenced by the output, a new partition, /dev/sda2
, has been successfully created with a capacity of 10 GB.
Deleting and Resizing Partitions Using the fdisk Command
This section provides instructions on how to delete and resize partitions using the fdisk
command. This skill is fundamental for systemadmin tasks related to storage optimization.
First, let's display the existing partitions on the /dev/sda
device:
$ sudo fdisk -l /dev/sda
Disk /dev/sda: 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/sda1 2048 41943039 41940992 20G 83 Linux
/dev/sda2 41943040 61071359 19128320 10G 83 Linux
Now, let's proceed to delete the /dev/sda2
partition:
$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 has been deleted.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
In this process:
- We enter
fdisk
interactive mode by runningsudo fdisk /dev/sda
. - We select the delete option by typing
d
. - We specify the partition number
2
to delete the/dev/sda2
partition. - We save the changes to disk by typing
w
.
Next, let's resize the remaining /dev/sda1
partition to utilize the entire disk space:
$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): d
Partition number (1,2, default 1): 1
Partition 1 has been deleted.
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-41943039, default 41943039):
Created a new partition 1 of type 'Linux' and of size 20 GiB.
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
In this scenario:
- We first delete the
/dev/sda1
partition by typingd
and specifying1
as the partition number. - We then create a new primary partition (
p
) by typingn
. - We accept the default partition number
1
. - We accept the default first sector
2048
. - We accept the default last sector
41943039
to utilize the entire 20 GB disk capacity. - Finally, we save the changes by typing
w
.
Example output demonstrating the resized partition:
$ sudo fdisk -l /dev/sda
Disk /dev/sda: 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/sda1 2048 41943039 41940992 20G 83 Linux
The output confirms that the /dev/sda1
partition now occupies the entirety of the 20 GB disk space.
Summary
This lab provided a practical introduction to the fdisk
command in Linux. You learned that fdisk
is a crucial command-line tool used by systemadmin to create, delete, and manage disk partitions. We covered the basic syntax of fdisk
, including the use of sudo
for elevated privileges, explored available options, and demonstrated how to list partition tables. Furthermore, you gained hands-on experience in creating new partitions and managing existing ones through deletion and resizing, empowering you to effectively manage storage on Linux systems.