Managing Disk Partitions with 'fdisk' (with examples)
- Linux
- December 17, 2024
fdisk
is a powerful command-line utility used for managing partition tables and partitions on a hard disk in Linux and other Unix-like operating systems. It allows system administrators to create, modify, delete, and display information about disk partitions. Its ability to perform these tasks efficiently makes it an essential tool for disk management. With a straightforward interface, fdisk
equips users with the capabilities needed to organize and maximize the utility of disk space.
Use Case 1: List Partitions
Code:
sudo fdisk -l
Motivation:
Listing partitions is crucial, particularly for system administrators and users needing to review the current state of disk partitions before making modifications. By obtaining a clear overview, they can avoid accidental data loss by ensuring they are working on the correct disk and partition.
Explanation:
The command sudo
grants superuser privileges, allowing the fdisk
tool to access necessary disk information since managing hardware typically requires elevated permissions. The -l
flag is a specific option to fdisk
, which instructs the utility to list all available partitions and their associated details across all disks present in the system.
Example output:
Disk /dev/sda: 120 GiB, 120000000000 bytes, 234375000 sectors
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: gpt
Disk identifier: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
Device Start End Sectors Size Type
/dev/sda1 2048 1026047 1024000 500M EFI System
/dev/sda2 1026048 234373119 233347072 111.3G Linux filesystem
Use Case 2: Start the Partition Manipulator
Code:
sudo fdisk /dev/sdX
Motivation:
Administrators need direct access to a specific disk to make changes, such as creating or deleting partitions. Initiating the partition manipulator allows them to interactively perform these tasks. This command opens up the disk for detailed manipulation.
Explanation:
sudo
is used again to provide the necessary permissions. The fdisk
command is followed by the specific path to the disk you wish to modify, denoted as /dev/sdX
. Here, sdX
represents the target disk you want to manage. It is essential to replace X
with the appropriate identifier for the actual disk you are working on, ensuring that operations are applied to the correct device.
Example output:
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):
Use Case 3: Create a Partition
Code:
n
Motivation:
Creating new partitions is a frequent task, whether to organize data, set up different operating system installations, or prepare virtual machine environments. This functionality is critical for efficient disk utilization and management according to specific user needs.
Explanation:
Once you have started fdisk
on a particular disk (/dev/sdX
), entering the command n
initiates the creation of a new partition. It will prompt you for further details, such as the type of partition (primary or extended) and the size. This interaction sets up a structured allocation of disk space for various purposes.
Example output:
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):
Use Case 4: Select a Partition to Delete
Code:
d
Motivation:
Deleting partitions might be necessary when reorganizing disk space or removing unnecessary partitions to reclaim space. This functionality helps maintain an efficient and clean disk environment.
Explanation:
In the interactive fdisk
session, inputting d
allows you to select a partition for deletion. It will ask which partition to remove if multiple exist. This action erases the partition table entry from the disk but does not securely wipe the data.
Example output:
Command (m for help): d
Partition number (1-4):
Use Case 5: View the Partition Table
Code:
p
Motivation:
Reviewing the current partition table provides insight into how the disk is divided. This can be essential after making changes to ensure the desired configuration is established or before making changes to evaluate space constraints.
Explanation:
Entering p
within an active fdisk
session displays the current partition table for the specified disk. It shows information related to each partition, such as start and end sectors, size, and type, giving a snapshot of current disk usage and layout.
Example output:
Command (m for help): p
Disk /dev/sda: 120 GiB, 120000000000 bytes, 234375000 sectors
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: gpt
Disk identifier: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
Device Start End Sectors Size Type
/dev/sda1 2048 1026047 1024000 500M EFI System
/dev/sda2 1026048 234373119 233347072 111.3G Linux filesystem
/dev/sda3 234373120 234375000 1881 872K Linux filesystem
Use Case 6: Write the Changes Made
Code:
w
Motivation:
Writing changes is essential to commit any modifications made during the fdisk
session. This action makes sure that the new partition layout is saved onto the disk, applying all changes made to the partition table.
Explanation:
The command w
saves the modifications to the partition table. All changes you’ve done in the current fdisk
session (such as creating or deleting partitions) are executed on the physical disk only after issuing this command.
Example output:
Command (m for help): w
The partition table has been altered.
Syncing disks.
Use Case 7: Discard the Changes Made
Code:
q
Motivation:
Having the ability to exit without saving changes is crucial in preventing accidental modifications that could lead to data loss. Users can explore fdisk
functionalities or inspect the disk layout without committing any changes.
Explanation:
The q
command exits the fdisk
session without saving any alterations made to the partition table. This is particularly useful if you decide against implementing changes after reviewing the current disk setup or spotting an error during manipulation.
Example output:
Command (m for help): q
Use Case 8: Open a Help Menu
Code:
m
Motivation:
Accessing help easily while in fdisk
gives users on-the-go guidance, streamlining the operation without needing external documentation. It ensures users are aware of available commands before making significant changes.
Explanation:
Pressing m
within an fdisk
session displays a help menu, listing all available commands for disk manipulation. This interactive help guide fosters a more intuitive user experience by providing immediate access to command descriptions.
Example output:
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
t change a partition's system id
v verify the partition table
w write table to disk and exit
x extra functionality
Conclusion:
The fdisk
command is a versatile and essential tool for anyone managing Linux-based systems. By understanding and utilizing its capabilities, users can perform crucial tasks like listing, creating, modifying, and deleting partitions with ease. Each use case demonstrates specific scenarios where fdisk
improves efficiency and productivity, offering essential functionalities for maintaining and optimizing disk layouts.