Introduction
Disk partitioning is a fundamental
skill for anyone working with Linux. It's the foundation of storage management,
allowing you to organize your hard drive, install multiple operating systems,
separate your data, and manage storage efficiently. This hands-on guide will
walk you through a practical lab exercise, creating multiple partitions on a
new hard drive in a virtual machine using fdisk. I'll explain each step clearly and include troubleshooting
tips for common problems. Even if you're a complete beginner, you'll be able to
follow along!
What You'll Need
- A computer with virtualization software (like
VirtualBox or VMware) installed.
- An Ubuntu (or any Linux distribution) virtual machine
(VM).
- Basic familiarity with the Linux command line.
Setting Up the Virtual Hard Drive
- Add a New Hard Drive to the VM:
- Shut down your VM completely.
- Open your virtualization software and go to the
settings of your Ubuntu VM.
- Find the "Storage" or "Hard
Drives" section.
- Click the button to add a new hard drive.
- Choose "Create a new virtual hard disk."
- Select VDI (VirtualBox Disk Image) or the recommended
format for your virtualization software.
- Choose "Dynamically allocated" for the
storage (this means the disk file will grow as you add data).
- Select a location on your computer to save the hard
drive file and choose an appropriate size. (10GB is usually enough for
this exercise).
- Finish the wizard.
- Start Your VM:
Boot up your Ubuntu virtual machine.
Partitioning the Disk with fdisk
- Identify the New Disk: Open a terminal and use the lsblk
command. This lists all block devices (hard drives and partitions). Your
new disk will likely be /dev/sdb, but double-check! It might be /dev/sdc
or something else if you already have other drives. (Screenshot of lsblk
showing the new disk)
- Run fdisk:
Bash
sudo
fdisk /dev/sdb # Replace /dev/sdb with
the correct device name if necessary
- Create Partitions:
Inside the fdisk utility, follow these steps:
- g (Create
a new GPT partition table. This is important for newer systems and can
also help resolve some errors, even if your disk is under 2TB. It's a
good practice.)
- n (New
partition)
- p
(Primary partition)
- 1
(Partition number)
- Press Enter to accept the default first sector.
- +1G (or
whatever size you want for the first partition - 1GB in this example. You
can use M for MB, G for GB, etc.)
- Repeat the n, p, and number steps for two more primary partitions.
- n (New
partition)
- e
(Extended partition)
- 4
(Partition number)
- (Press Enter to accept defaults)
- n (New
partition)
- l
(Logical partition)
- 5
(Partition number)
- (Press Enter to accept defaults)
- n (New
partition)
- l
(Logical partition)
- 6
(Partition number)
- (Press Enter to accept defaults)
- w
(Write changes and exit) (Screenshot of fdisk
commands)
- Verify Partitions:
Use lsblk again to confirm that your partitions (/dev/sdb1,
/dev/sdb2,
/dev/sdb3,
/dev/sdb5,
/dev/sdb6)
have been created. (Screenshot of lsblk showing the new partitions)
Formatting and Mounting
- Format the Partitions: We'll use the ext4 filesystem:
Bash
sudo
mkfs.ext4 /dev/sdb1
sudo
mkfs.ext4 /dev/sdb2
sudo
mkfs.ext4 /dev/sdb3
sudo
mkfs.ext4 /dev/sdb5
sudo
mkfs.ext4 /dev/sdb6
- Create Mount Points:
These are folders where you'll access your partitions:
Bash
sudo
mkdir /mnt/partition1
sudo
mkdir /mnt/partition2
sudo
mkdir /mnt/partition3
sudo
mkdir /mnt/partition5
sudo
mkdir /mnt/partition6
- Mount the Partitions:
Bash
sudo
mount /dev/sdb1 /mnt/partition1
sudo
mount /dev/sdb2 /mnt/partition2
sudo
mount /dev/sdb3 /mnt/partition3
sudo
mount /dev/sdb5 /mnt/partition5
sudo
mount /dev/sdb6 /mnt/partition6
- Make Mounts Permanent (Crucial!): Edit the /etc/fstab file:
Bash
sudo
nano /etc/fstab
Add these
lines (replace the example UUIDs with your actual UUIDs, which you can find
using sudo blkid):
UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
/mnt/partition1 ext4 defaults 0 2
UUID=yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy
/mnt/partition2 ext4 defaults 0 2
UUID=zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz
/mnt/partition3 ext4 defaults 0 2
UUID=aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa
/mnt/partition5 ext4 defaults 0 2
UUID=bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb
/mnt/partition6 ext4 defaults 0 2
Save and
close the file. (Screenshot of /etc/fstab)
- Test the Mounts:
Bash
sudo
mount -a # Mounts all partitions in
/etc/fstab
lsblk # Verify the mounts
``` *(Screenshot of `lsblk` showing mounted
partitions)*
Troubleshooting
- "Value out of range" Error: If you get this error during fdisk, it
often means there's leftover data on the disk. Use g inside fdisk to
create a new GPT partition table.
- Mounting Issues:
Double-check your /etc/fstab file. Make sure each partition has its own line and
that the UUIDs are correct. Use sudo
blkid to get the UUIDs.
Conclusion
Congratulations! You've successfully
partitioned a hard drive in Linux. This is a fundamental skill that will serve
you well in your Linux journey. Remember to practice and experiment. The more
you work with disk partitioning, the more comfortable you'll become.
I plan to explore different
partitioning tools and techniques, including LVM (Logical Volume Management).
Call to Action
What are your go-to disk
partitioning tips? Share them in the comments below!
Hashtags
#Linux #SysAdmin #Cybersecurity
#DiskPartitioning #fdisk #GPT #Troubleshooting #HandsOnLab #VirtualBox
Post a Comment
If you have any queries regarding this topic, please let me know.