Linux and Sysadmin

Linux Disk Management and Partitioning Guide

Linux Disk Management and Partitioning Guide
Photo by Antonio Moreno Nadal on Pexels

Linux Disk Management and Partitioning Guide

Understanding disk management and partitioning is a fundamental skill for Linux system administrators and users. Whether you’re setting up a new server, expanding storage capacity, or optimizing your system’s performance, knowing how to properly manage disks is essential. This comprehensive guide will walk you through the essential tools, commands, and best practices for Linux disk management.

Understanding Linux Disk Structure

In Linux, storage devices are represented as special files in the /dev directory. Hard drives typically appear as /dev/sda, /dev/sdb, and so on, while NVMe drives use the naming convention /dev/nvme0n1, /dev/nvme0n2, etc. Partitions on these drives are numbered sequentially, such as /dev/sda1, /dev/sda2, or /dev/nvme0n1p1.

Understanding this naming convention is crucial for effective disk management, especially when working with multiple storage devices or when deploying infrastructure on cloud platforms like Kamatera, where you might need to configure additional storage volumes for your Linux servers.

Viewing Disk Information

Before making any changes to your disks, you need to gather information about your current storage configuration. Several commands provide this information:

Using lsblk

The lsblk command displays a tree view of all block devices:

lsblk

This shows device names, sizes, types, and mount points in an easy-to-read format.

Using fdisk -l

For more detailed information, use:

sudo fdisk -l

This displays partition tables, sizes, and partition types for all attached storage devices.

Using df and du

To check disk space usage, use df -h for filesystem usage and du -sh for directory sizes.

Partition Tables: MBR vs GPT

Two main partition table formats exist: Master Boot Record (MBR) and GUID Partition Table (GPT).

MBR (Master Boot Record)

MBR is the older standard with these limitations:

  • Maximum disk size of 2TB
  • Maximum of four primary partitions
  • Extended partitions required for more than four partitions

GPT (GUID Partition Table)

GPT is the modern standard offering:

  • Support for disks larger than 2TB
  • Up to 128 partitions by default
  • Better data integrity with redundant partition tables
  • Required for UEFI systems

For new installations, GPT is recommended unless you have specific compatibility requirements.

Essential Partitioning Tools

fdisk

The traditional partition editor, best for MBR partition tables but also supports GPT:

sudo fdisk /dev/sda

Interactive commands include n for new partition, d for delete, w for write changes, and q to quit.

parted

A more advanced tool supporting both MBR and GPT with better scripting capabilities:

sudo parted /dev/sda

Parted can resize partitions and perform operations in a single command, making it ideal for automation.

gdisk

Specialized for GPT partition tables, offering similar interface to fdisk:

sudo gdisk /dev/sda

Creating and Managing Partitions

Here’s a practical example of creating a new partition using fdisk:

sudo fdisk /dev/sdb
n (create new partition)
p (primary partition)
1 (partition number)
[Enter] (default first sector)
+10G (size of 10GB)
w (write changes)

After creating a partition, inform the kernel of the changes:

sudo partprobe /dev/sdb

Deleting Partitions

To delete a partition, use fdisk’s delete command:

sudo fdisk /dev/sdb
d (delete partition)
1 (partition number to delete)
w (write changes)

Warning: Deleting a partition destroys all data on it. Always backup important data first.

File Systems and Formatting

After creating a partition, you need to format it with a filesystem. Common Linux filesystems include:

ext4

The most common Linux filesystem, offering excellent performance and reliability:

sudo mkfs.ext4 /dev/sdb1

XFS

Excellent for large files and high-performance scenarios:

sudo mkfs.xfs /dev/sdb1

Btrfs

Modern filesystem with advanced features like snapshots and compression:

sudo mkfs.btrfs /dev/sdb1

For those looking to deepen their Linux system administration skills, platforms like DataCamp offer excellent courses that cover filesystem management and other essential Linux concepts.

Mounting and Unmounting Drives

To access a partition, you must mount it to a directory:

sudo mkdir /mnt/mydata
sudo mount /dev/sdb1 /mnt/mydata

Permanent Mounts with fstab

To automatically mount a partition at boot, edit /etc/fstab:

sudo nano /etc/fstab

Add an entry like:

/dev/sdb1  /mnt/mydata  ext4  defaults  0  2

Or use UUID for better reliability:

UUID=xxxx-xxxx  /mnt/mydata  ext4  defaults  0  2

Find the UUID with:

sudo blkid /dev/sdb1

Logical Volume Management (LVM)

LVM provides flexible disk management by abstracting physical storage into logical volumes. This allows easy resizing and management.

LVM Components

  • Physical Volumes (PV): Physical disks or partitions
  • Volume Groups (VG): Pools of physical volumes
  • Logical Volumes (LV): Virtual partitions created from volume groups

Creating an LVM Setup

sudo pvcreate /dev/sdb1
sudo vgcreate myvg /dev/sdb1
sudo lvcreate -L 20G -n mylv myvg
sudo mkfs.ext4 /dev/myvg/mylv
sudo mount /dev/myvg/mylv /mnt/mydata

Extending Logical Volumes

One of LVM’s major advantages is easy volume expansion:

sudo lvextend -L +10G /dev/myvg/mylv
sudo resize2fs /dev/myvg/mylv

Best Practices and Tips

Always Backup Data

Before performing any disk operations, especially partitioning or formatting, always backup critical data. Mistakes can lead to permanent data loss.

Use Descriptive Labels

Label your filesystems for easier identification:

sudo e2label /dev/sdb1 "MyDataDrive"

Monitor Disk Health

Regularly check disk health using SMART tools:

sudo smartctl -a /dev/sda

Plan Your Partition Scheme

Consider separating /home, /var, and /tmp into separate partitions for better security and easier maintenance.

Document Your Configuration

Keep notes about your partition layout, especially in production environments. This helps during troubleshooting and disaster recovery.

Test Before Production

Practice disk management operations in a test environment before implementing them on production systems.

Use GPT for New Systems

Unless you have specific legacy requirements, always use GPT partition tables for new installations.

Consider LVM from the Start

Implementing LVM during initial system setup provides maximum flexibility for future storage needs without requiring system downtime.

Mastering Linux disk management takes practice and patience. Start with non-critical systems, experiment with different tools, and gradually build your confidence. With these skills, you’ll be well-equipped to handle any storage challenge your Linux systems present.

Follow Networkyy

Join 125,000+ IT professionals:

Leave a Reply

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