Linux Disk Management: Partitioning, Formatting, and Mounting
Understanding Linux Disk Fundamentals
Effective disk management is crucial for maintaining a healthy and efficient Linux system. This involves understanding the physical and logical structure of disks, how to partition them, format them with a file system, and mount them for use.
Physical and Logical Disk Structure
A traditional hard disk drive (HDD) consists of several key physical components:
- Platters: Circular disks that store data magnetically.
- Spindle: Rotates the platters at a specific speed (RPM), typically 5400 or 7200 for consumer drives, and 10k or 15k for enterprise drives.
- Read/Write Heads: Access data on the platters. The number of heads corresponds to the number of platter surfaces.
Logically, data is organized on the disk in a hierarchy:
- Tracks: Concentric circles on a platter surface where data is stored.
- Sectors: The smallest addressable unit on a track, typically 512 bytes. The operating system groups sectors into blocks for efficiency.
- Cylinders: A set of tracks at the same position on all platter surfaces. This allows all head to move together to access data, which is faster than moving a single head.
Disk Arrays (RAID)
RAID (Redundant Array of Independent Disks) combines multiple physical disks into a single logical unit to improve performance, data redundancy, or both.
- RAID 0 (Striping):strong> Distributes data across disks for maximum performance but offers no redundancy. If one disk fails, all data is lost.
- RAID 1 (Mirroring):strong> Duplicates data across two or more disks. Provides high redundancy at the cost of storage capacity (e.g., two 1TB disks yield 1TB of usable space).
- RAID 5 (Striping with Parity):strong> Distributes data and parity information across three or more disks. Offers a balance of performance, capacity, and redundancy. Can withstand a single disk failure.
- RAID 10 (Mirroring + Striping):strong> Combines the benefits of RAID 1 and RAID 0, offering high performance and redundancy, but at a higher cost.
Logical Volume Management (LVM)
LVM provides a layer of abstraction over physical storage. It allows you to create flexible storage pools (Volume Groups) from one or more physical disks or partitions. Within these groups, you can create logical volumes that can be easily resized, both up and down, providing greater flexibility than traditional partitioning.
Disk Partitioning
Partitioning divides a disk into one or more logical sections. The method used depends on the disk size and the system's boot requirements.
Partition Table Types
- MBR (Master Boot Record): The traditional standard, supporting disks up to 2TB and a maximum of four primary partitions. One primary partition can be designated as an extended partition to hold multiple logical partitions.
- GPT (GUID Partition Table): A modern standard that supports disks larger than 2TB and up to 128 primary partitions. It is the recommended choice for modern systems.
Partitioning a Disk Smaller Than 2TB with fdisk
The fdisk utility is commonly used for managing MBR-partitioned disks.
lsblk
# Identify the new disk, e.g., /dev/nvme0n1
# Start the fdisk utility for the target disk
sudo fdisk /dev/nvme0n1
# Inside the fdisk prompt, use the following commands:
# n - Create a new partition
# p - Primary partition
# 1 - Partition number
# [Enter] - Use default start sector
# +10G - Specify the size (e.g., 10 Gigabytes)
# After creating the partition, write the changes:
# w - Write table to disk and exit
Partitioning a Disk Larger Than 2TB with parted
For disks larger than 2TB, the parted utility is required to create a GPT partition table.
# Start the parted utility for the target disk
sudo parted /dev/sdc
# Inside the parted prompt:
# mklabel gpt - Create a new GPT partition table
# mkpart primary 0% 50% - Create a primary partition using 50% of the disk
# quit - Exit parted
# Inform the OS of the partition table changes
sudo partprobe /dev/sdc
Formatting and Mounting
Once a partition is created, it must be formatted with a file system before it can store data. Common Linux file systems include ext4 and XFS.
Creating a File System
The mkfs command is used to create a file system on a partition. For example, to format a partition with the XFS file system:
# Format the partition /dev/nvme0n1p1 with XFS
sudo mkfs.xfs /dev/nvme0n1p1
Mounting the File System
After formatting, the partition must be mounted to a directory in the file system hierarchy to be acccessible.
# Create a mount point directory
sudo mkdir /data
# Mount the partition to the directory
sudo mount /dev/nvme0n1p1 /data
# Verify the mount
df -h
Persistent Mounting with /etc/fstab
To ensure the partition is mounted automatically at boot, an entry must be added to the /etc/fstab file. It's best practice to use the partition's UUID.
# First, find the UUID of the partition
sudo blkid /dev/nvme0n1p1
# Add the following line to /etc/fstab, replacing UUID and /data
# UUID=your-partition-uuid /data xfs defaults 0 2
Managing Swap Space
Swap space is used as virtual memory when the system's RAM is full. It can be adjusted by creating a swap file.
Creating and Enabling a Swap File
# Create a 2GB swap file
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
# Set the correct permissions
sudo chmod 600 /swapfile
# Format the file as swap space
sudo mkswap /swapfile
# Enable the swap file
sudo swapon /swapfile
# Verify the new swap space is active
free -h
Disabling and Removing a Swap File
# Disable the swap file
sudo swapoff /swapfile
# Remove the file
sudo rm /swapfile
Troubleshooting Disk Issues
When a disk appears full, it's important to determine the cause. Two common issues are:
- Full Data Blocks: The disk has run out of space to store files.
- Full Inodes: The disk has run out of index nodes, which are used to track individual files, even if the total data block space is not fully utilized. This often happens when storing a very large number of small files.