What is Disk Partitioning
Disk partitioning involves dividing a physical disk into separate logical sections. Each section, or partition, acts as an independent unit where a file system can be created. Once a partition is created, a file system is built on top of it and mounted to make it accessible.
Why Partition a Disk?
Disk Space Efficiency: Partitioning allows better allocation of disk space for various uses.
Data Encapsulation: If something happens to one partition, other partitions remain unaffected, improving data safety.
Limit Data Growth: Partitioning can prevent uncontrolled data growth by setting limits on each partition.
Disk Partitioning Schema
The MBR (Master Boot Record) Partitioning Schema is a traditional partitioning system, limiting a disk to four primary partitions. However, one of those can be an extended partition, which can hold multiple logical partitions.
Partitioning Example:
3P: Three primary partitions
2P + 1E: Two primary partitions and one extended partition
1P + 1E: One primary partition and one extended partition
A primary partition is a basic partition that can hold data, it is created on a hard disk (secondary memory). As the name implies, an extended partition extends the partition table and can hold multiple logical partitions, which are containers for the actual data.
Disk Identification
IDE (Integrated Drive Electronics):
/dev/hds
NVME (Non-Volatile Memory Express):
/dev/nvme0n1
SCSI (Small Computer System Interface):
/dev/sda
Virtual Drive:
/dev/vda
Types of File Systems
Filesystem | Description | Journaling | Max Individual File Size | Max Filesystem Size |
ext2 | Second Extended Filesystem. | No | 16GB to 2TB | 32TB |
ext3 | Third Extended Filesystem. | Yes | 16GB to 2TB | 32TB |
ext4 | Fourth Extended Filesystem. | Yes | 16GB to 16TB | - |
xfs | High-performance 64-bit journaling filesystem. | Yes | - | 8 exbibytes |
Viewing Partitions
fdisk -l
parted -l
lsblk
Adding a New Disk
First, add a hard disk of SCSI type of a specific disk size and create
Newly added disks do not reflect automatically and require a system reboot. To avoid rebooting:
echo "- - -" > /sys/class/scsi_host/host0/scan
Explanation: The three dashes act as wildcards (channel, SCSI target ID, LUN). LUN is essentially a way to refer to a specific storage volume or device
Scan for all hosts in
/sys/class/scsi_host/
:
Scan Script
#!/bin/bash
# List of all hosts
hosts="host0 host1 host2 host3 host4 host5 host6 host7 host8 host9
host10 host11 host12 host13 host14 host15 host16 host17 host18 host19
host20 host21 host22 host23 host24 host25 host26 host27 host28 host29
host30 host31 host32"
# Base path to the SCSI host directories
base_path="/sys/class/scsi_host"
# Loop through each host and perform the scan
for host in $hosts; do
echo "Scanning $host..."
echo '- - -' > "$base_path/$host/scan"
done
echo "Scan complete for all hosts."
Partitioning with fdisk
Start the partition administration:
fdisk /dev/sdb
Create 2 Primary Partitions and 1 Extended Partition.
Primary Partition Creation
Both of the same size.
Logical Partition Creation
Create 2 Logical Partitions of equal size within the Extended Partition.
Save partition information.
Update Partition Table
Use the following command to update the partition table without restarting:
partprobe /dev/sdb
Verify Partitions
lsblk /dev/sdb1
fdisk -l
parted -l
Formatting a Partition
Use ext4
filesystem for formatting:
mkfs.ext4 /dev/sdb1
Mounting
Temporary Mounting:
cd / mkdir /data mount /dev/sdb1 /data/
- Temporary mount information is stored in
/etc/mtab
.
- Temporary mount information is stored in
Permanent Mounting: Add an entry in
/etc/fstab
:When we reboot the system, the above mount will get unmounted it’s a temporary mount.
To make it permanent mounting we need to make its entry add an entry in
/etc/fstab
file./dev/sdb1 /data ext4 defaults 0 0
We can also add the UUID instead of the device name:
UUID=123e4567-e89b-12d3-a456-426614174000 /data ext4 defaults 0 0
Find UUID:
blkid /dev/sdb1
/etc/fstab
fields:Field | Description --------------------|-------------------------------------------------------- Device Name/ID | The device or partition being mounted (e.g., /dev/sda1, UUID=xxx, LABEL=xxx). Mount Point | The directory where the device is mounted (e.g., /mnt/data, /home). File System Type | The type of file system (e.g., ext4, xfs, nfs). Mount Options | Options for how the file system is mounted (e.g., rw, ro, noexec). Dump | Used by the dump utility, usually 0 (no dump) or 1 (dump enabled). Sequence Check | Used by fsck to check the file system order during boot (e.g., 0 or 1).
Unmounting
Temporary unmount:
umount /data/
Verify unmount:
df -h
As it is unmounted currently, but when we reboot the system the directory will get mounted again as we have an permanent entry in /etc/fstab file.
mount -a : mounts all the stuff in /ect/fstab
Deleting a Partition
Start
fdisk
:fdisk /dev/sdb
Use the
d
command to delete the partition andw
to save changes.Update partition table:
partprobe /dev/sdb
Verify:
lsblk parted -l
Troubleshooting
You cannot unmount the directory if you are in it:
cd /data/ umount /data
Cannot unmount if another user is making changes:
fuser -cu /dev/sdb1 # To check which useres are using the partition fuser -ck /dev/sdb1 # To remove the users from working on the partition
Swap Memory and Space
Swap mem is a dedicated hard disk partition used as an extension for mem (RAM), when ram gets full the system moves less active data pages to the swap space on the hard disk to free up ram for new processes.
Swap Memory Calculation
If RAM < 2GB: Swap = 2 × RAM Size.
If RAM > 2GB: Swap = RAM Size + 2GB.
Commands
Check help:
swapon --help
Creating Swap Partition
Change Partition ID:
fdisk /dev/sdb
Use
t
to change the partition type.Save changes and update the partition table:
partprobe /dev/sdb
Verify
Format as Swap Filesystem:
mkswap /dev/sdb2
Verify:
parted -l
Activate Swap Space:
swapon /dev/sdb2
Verify:
swapon -s
Deactivate Swap Space:
swapoff /dev/sdb2