This tutorial builds on basic fdisk knowledge to teach intermediate skills for managing disk partitions. You’ll learn advanced partitioning techniques, troubleshooting, and real-world scenarios for system administrators.
Step 1: Review Basic fdisk Commands
Before diving into advanced topics, let's review the basics of fdisk. It's a command-line utility for managing disk partitions on Linux systems. Key commands include:
- p: Print the partition table
- n: Create a new partition
- d: Delete a partition
- w: Write changes to disk
- q: Quit without saving changes
sudo fdisk /dev/sdaPractice Exercise
Use fdisk to list the partition table of /dev/sdb. Identify the number of partitions and their sizes.
Show Solution
sudo fdisk -l /dev/sdb
# The output will display the partition table, including partition numbers, sizes, and types.Step 2: Create a New Partition with Specific Size
Learn how to create a partition with a specific size. For example, you might need to allocate 10GB for a new data partition.
- Start
fdiskon the target disk. - Use the
ncommand to create a new partition. - Specify the partition type (primary or extended).
- Set the start and end sectors or specify the size directly (e.g.,
+10G).
sudo fdisk /dev/sda
n
p
1
+10G
wPractice Exercise
Create a 5GB primary partition on /dev/sdc. Verify the partition was created successfully.
Show Solution
sudo fdisk /dev/sdc
n
p
1
+5G
w
# Verify with:
sudo fdisk -l /dev/sdc
# Look for the new 5GB partition in the output.Step 3: Change Partition Types
Sometimes, you need to change a partition's type (e.g., from Linux to swap). Use the t command in fdisk to modify the partition type.
- List the partition table to identify the partition number.
- Use the
tcommand and specify the partition number. - Enter the hex code for the desired partition type (e.g.,
82for swap).
sudo fdisk /dev/sda
t
1
82
wPractice Exercise
Change the type of the second partition on /dev/sdb to swap (hex code 82). Verify the change.
Show Solution
sudo fdisk /dev/sdb
t
2
82
w
# Verify with:
sudo fdisk -l /dev/sdb
# Check the 'Type' column for the second partition.Step 4: Resize Partitions Safely
Resizing partitions can be risky, but fdisk allows you to delete and recreate partitions with new sizes. Always back up data before proceeding.
- Delete the partition you want to resize.
- Create a new partition with the desired size.
- Ensure the new partition starts at the same sector to avoid data loss.
sudo fdisk /dev/sda
d
1
n
p
1
+20G
wPractice Exercise
Resize the first partition on /dev/sdc from 5GB to 8GB. Ensure no data is lost by starting at the same sector.
Show Solution
sudo fdisk /dev/sdc
d
1
n
p
1
+8G
w
# Replace with the original start sector from the partition table.Step 5: Recover Deleted Partitions
Accidentally deleted a partition? Use fdisk to recreate it with the exact same parameters (start sector, size, and type).
- Note the details of the deleted partition from the partition table.
- Recreate the partition with the same start sector and size.
- Verify the partition is restored correctly.
sudo fdisk /dev/sda
n
p
1
t
1
wPractice Exercise
Simulate a partition deletion on /dev/sdd and recover it using the original parameters.
Show Solution
# Simulate deletion:
sudo fdisk /dev/sdd
d
1
w
# Recover:
sudo fdisk /dev/sdd
n
p
1
t
1
w
# Replace , , and with the original values.Step 6: Advanced: Align Partitions for Performance
Proper partition alignment is crucial for performance, especially on SSDs. Use fdisk to align partitions to 1MB boundaries.
- Start
fdiskand create a new partition. - Set the start sector to a multiple of 2048 (1MB alignment).
- Verify alignment using
partedorfdisk.
sudo fdisk /dev/sda
n
p
1
2048
+10G
wPractice Exercise
Create a 10GB partition on /dev/sde aligned to a 1MB boundary. Verify the alignment.
Show Solution
sudo fdisk /dev/sde
n
p
1
2048
+10G
w
# Verify alignment:
sudo parted /dev/sde align-check optimal 1
# Output should indicate proper alignment.Step 7: Troubleshoot Partition Issues
Learn to diagnose and fix common partition issues, such as corrupted partition tables or misaligned partitions.
- Use
fdisk -lto inspect the partition table. - Check for overlapping partitions or incorrect sizes.
- Recreate partitions if necessary.
sudo fdisk -l /dev/sdaPractice Exercise
Inspect /dev/sdf for partition issues. Identify any overlapping partitions and fix them.
Show Solution
sudo fdisk -l /dev/sdf
# Look for overlapping partitions in the output.
# If found, delete and recreate the affected partitions with correct parameters.Step 8: Automate Partitioning with Scripts
Automate repetitive partitioning tasks using shell scripts. For example, create a script to partition a new disk with a standard layout.
- Write a script that uses
fdiskcommands. - Use
echoto pass input tofdisk. - Test the script on a non-critical disk.
echo -e 'n\np\n1\n\n+10G\nw' | sudo fdisk /dev/sdaPractice Exercise
Write a script to create a 5GB primary partition and a 10GB extended partition on /dev/sdg.
Show Solution
#!/bin/bash
echo -e 'n\np\n1\n\n+5G\nn\ne\n2\n\n+10G\nw' | sudo fdisk /dev/sdg
# Run the script and verify the partitions with `fdisk -l /dev/sdg`.Sign in to take Cornell notes on this lesson — they save automatically and stay with your account.