Partitions
What are Partitions in Linux?
In Linux, a partition is a logical division of a physical hard drive into separate sections. This allows you to use different file systems, separate system files from user data, and manage storage more efficiently and securely.
Each partition is treated as a separate disk. For example, /dev/sda1 refers to the first partition on the first SCSI disk.
How Partitions Work
Key Partition Types:
- Primary Partition: The main partitions that can store an operating system. MBR disks are limited to four primary partitions.
- Extended Partition: A special type of partition that can hold multiple logical partitions. It’s a workaround for the four-partition limit.
- Logical Partition: A partition that resides within an extended partition.
Common File Systems:
ext4: The most common default file system for many Linux distributions.xfs: A high-performance file system, good for large files.btrfs: A modern file system with advanced features like snapshots and built-in RAID.swap: A special file system used for virtual memory (swap space).
Partition Management Workflow
Step 1: View and Create Partitions
First, list the block devices to see your disks and partitions:
lsblkTo create a new partition, you would use a tool like fdisk or parted. This is an advanced operation that modifies the disk’s partition table.
sudo fdisk /dev/sdbStep 2: Create a Filesystem (Formatting)
Once a partition is created (e.g., /dev/sdb1), you must format it with a filesystem:
sudo mkfs.ext4 /dev/sdb1Step 3: Mount and Unmount the Partition
First, create a directory to serve as the mount point:
sudo mkdir /mnt/new_dataNow, mount the partition to that directory:
sudo mount /dev/sdb1 /mnt/new_dataTo unmount it:
sudo umount /dev/sdb1To make a mount permanent, you must add an entry to the /etc/fstab file.
Step 4: Check Usage and Health
To check disk usage in a human-readable format:
df -hTo check a filesystem for errors (ensure it’s unmounted first):
sudo fsck /dev/sdb1Optional: Managing Swap Space
To format a partition as swap:
sudo mkswap /dev/sda2To enable a swap partition:
sudo swapon /dev/sda2To disable it:
sudo swapoff /dev/sda2