Partitions

Lab: Managing Linux 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:

lsblk

To 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/sdb

Step 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/sdb1

Step 3: Mount and Unmount the Partition

First, create a directory to serve as the mount point:

sudo mkdir /mnt/new_data

Now, mount the partition to that directory:

sudo mount /dev/sdb1 /mnt/new_data

To unmount it:

sudo umount /dev/sdb1

To 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 -h

To check a filesystem for errors (ensure it’s unmounted first):

sudo fsck /dev/sdb1

Optional: Managing Swap Space

To format a partition as swap:

sudo mkswap /dev/sda2

To enable a swap partition:

sudo swapon /dev/sda2

To disable it:

sudo swapoff /dev/sda2

Leave a Reply

Your email address will not be published. Required fields are marked *