Debian Storage Management
Introduction
Storage management is a crucial aspect of Debian system administration. Properly configured storage ensures system stability, optimal performance, and data security. This guide covers the essential concepts and tools for managing storage on Debian systems, from basic disk operations to advanced storage configurations.
Whether you're setting up a new Debian server, managing an existing system, or troubleshooting storage issues, understanding these fundamentals will help you make informed decisions about your storage architecture.
Basic Storage Concepts
Understanding Linux Storage Hierarchy
In Debian and other Linux distributions, storage devices follow a hierarchical structure:
- Physical Storage Devices - Actual hardware (HDDs, SSDs, USB drives)
- Partitions - Sections of physical devices
- Filesystems - Organizational structures formatted onto partitions
- Mount Points - Directories where filesystems are accessible in the directory tree
Storage Device Naming
Debian uses a consistent naming convention for storage devices:
- SATA/SCSI/USB drives:
/dev/sdX
(where X is a letter: a, b, c, etc.) - NVMe drives:
/dev/nvmeXnY
(X = controller number, Y = namespace) - Virtual disks:
/dev/vdX
(in virtual machines) - Partitions:
/dev/sdXY
(where Y is a number: 1, 2, 3, etc.)
For example, the first partition on the first SATA drive would be /dev/sda1
.
Disk Partitioning
Viewing Disk Information
Before partitioning, you need to identify your disks:
# List all block devices
lsblk
# More detailed disk information
sudo fdisk -l
Example output:
# lsblk output
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 80G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 16G 0 part [SWAP]
└─sda3 8:3 0 63G 0 part /
sdb 8:16 0 500G 0 disk
└─sdb1 8:17 0 500G 0 part /data
Partitioning Tools
Debian provides several tools for disk partitioning:
1. fdisk (command-line utility)
# Start fdisk for /dev/sdb
sudo fdisk /dev/sdb
# Common fdisk commands:
# m - display help
# p - print partition table
# n - add new partition
# d - delete partition
# w - write changes and exit
# q - quit without saving
2. parted (more advanced command-line tool)
# Start parted for /dev/sdb
sudo parted /dev/sdb
# Common parted commands:
# print - display partition table
# mklabel gpt - create GPT partition table
# mkpart primary ext4 0% 100% - create partition using entire disk
# quit - exit parted
3. gparted (graphical interface)
For systems with a GUI:
# Install gparted
sudo apt update
sudo apt install gparted
# Run gparted (requires graphical environment)
sudo gparted
Partition Table Types
Debian supports two main partition table types:
-
MBR (Master Boot Record)
- Traditional partition scheme
- Limited to 4 primary partitions or 3 primary + 1 extended
- Maximum disk size of 2TB
-
GPT (GUID Partition Table)
- Modern partition scheme
- Supports up to 128 partitions by default
- No practical size limitations
- Required for UEFI systems
# Create GPT partition table using parted
sudo parted /dev/sdb mklabel gpt
Filesystem Management
Common Filesystem Types
Debian supports various filesystem types:
- ext4 - Default Linux filesystem, good general-purpose choice
- XFS - Good for large files and high-performance systems
- Btrfs - Modern filesystem with advanced features like snapshots
- F2FS - Optimized for flash storage devices
- NTFS/FAT32 - For compatibility with Windows (requires additional packages)
Creating Filesystems
After partitioning, you need to create a filesystem:
# Format a partition with ext4
sudo mkfs.ext4 /dev/sdb1
# Format with XFS
sudo mkfs.xfs /dev/sdb1
# Format with Btrfs
sudo mkfs.btrfs /dev/sdb1
Mounting Filesystems
To access a filesystem, you need to mount it:
Temporary Mounting
# Create a mount point
sudo mkdir -p /mnt/data
# Mount the filesystem
sudo mount /dev/sdb1 /mnt/data
# Verify the mount
df -h /mnt/data
Persistent Mounting with /etc/fstab
For persistence across reboots, edit the /etc/fstab
file:
# Add a line to /etc/fstab
sudo nano /etc/fstab
Add an entry like this:
# Device Mount point FS type Options Dump Pass
/dev/sdb1 /data ext4 defaults 0 2
You can also use UUID for more reliable mounting:
# Find UUID of partition
sudo blkid /dev/sdb1
Example fstab entry using UUID:
UUID=67e4e46a-e126-472d-b054-8a8197c3f6ea /data ext4 defaults 0 2
After editing fstab:
# Create the mount point if it doesn't exist
sudo mkdir -p /data
# Test if the entry is valid
sudo mount -a
# Check if mounted
df -h /data
Unmounting Filesystems
# Unmount by device or mount point
sudo umount /dev/sdb1
# or
sudo umount /data
Logical Volume Management (LVM)
LVM adds a layer of abstraction between physical disks and filesystems, enabling flexible storage management.
LVM Concepts
- Physical Volumes (PVs) - Physical disks or partitions
- Volume Groups (VGs) - Collections of PVs
- Logical Volumes (LVs) - Virtual partitions created from VGs
Setting Up LVM
1. Install LVM package
sudo apt update
sudo apt install lvm2
2. Create Physical Volumes
# Create PV on entire disk
sudo pvcreate /dev/sdb
# Create PV on a partition
sudo pvcreate /dev/sdc1
# Display PVs
sudo pvs
sudo pvdisplay
3. Create a Volume Group
# Create a VG named "data_vg" using two PVs
sudo vgcreate data_vg /dev/sdb /dev/sdc1
# Display VGs
sudo vgs
sudo vgdisplay
4. Create Logical Volumes
# Create 100GB LV for home
sudo lvcreate -n home_lv -L 100G data_vg
# Create LV using percentage of VG
sudo lvcreate -n backup_lv -l 30%VG data_vg
# Display LVs
sudo lvs
sudo lvdisplay
5. Create Filesystem and Mount
# Create filesystem
sudo mkfs.ext4 /dev/data_vg/home_lv
# Mount manually
sudo mkdir -p /home2
sudo mount /dev/data_vg/home_lv /home2
# Add to fstab for persistence
echo "/dev/data_vg/home_lv /home2 ext4 defaults 0 2" | sudo tee -a /etc/fstab
Managing LVM
Extending a Volume Group
# Add a new disk to an existing VG
sudo pvcreate /dev/sdd
sudo vgextend data_vg /dev/sdd
Extending a Logical Volume
# Extend LV by 50GB
sudo lvextend -L +50G /dev/data_vg/home_lv
# Resize filesystem to use new space
sudo resize2fs /dev/data_vg/home_lv
Creating Snapshots
LVM allows point-in-time snapshots:
# Create 5GB snapshot of home_lv
sudo lvcreate -s -n home_snap -L 5G /dev/data_vg/home_lv
# Mount snapshot for access
sudo mkdir -p /mnt/snapshot
sudo mount /dev/data_vg/home_snap /mnt/snapshot
Disk Usage Monitoring
Checking Disk Space
# Overall disk usage
df -h
# Disk usage of specific directory
du -sh /var/log
# Sorted disk usage by directory
du -h --max-depth=1 /var | sort -hr
Finding Large Files
# Find files larger than 100MB
sudo find / -type f -size +100M -exec ls -lh {} \; | sort -k5 -hr
Monitoring I/O Performance
# Install iotop
sudo apt install iotop
# Monitor disk I/O
sudo iotop
RAID Configuration
Debian supports software RAID through the mdadm
utility.
Installing mdadm
sudo apt update
sudo apt install mdadm
Creating a RAID Array
Example of creating a RAID 1 (mirror) array:
# Create RAID 1 with two disks
sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc
# Check RAID status
cat /proc/mdstat
sudo mdadm --detail /dev/md0
# Create filesystem on RAID array
sudo mkfs.ext4 /dev/md0
# Mount the RAID array
sudo mkdir -p /data
sudo mount /dev/md0 /data
RAID Configuration Persistence
Save the RAID configuration:
# Save RAID configuration
sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
# Update initramfs
sudo update-initramfs -u
# Add to fstab for persistent mounting
echo "/dev/md0 /data ext4 defaults 0 2" | sudo tee -a /etc/fstab
Swap Management
Swap space provides virtual memory when physical RAM is full.
Creating a Swap Partition
# Create a swap partition using fdisk or parted
# Then format it as swap
sudo mkswap /dev/sdb2
# Enable the swap
sudo swapon /dev/sdb2
# Add to fstab for persistence
echo "/dev/sdb2 none swap sw 0 0" | sudo tee -a /etc/fstab
Creating a Swap File
# Create 4GB swap file
sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# Add to fstab
echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
Adjusting Swap Settings
# Check current swappiness (0-100, lower means less swapping)
cat /proc/sys/vm/swappiness
# Change temporarily
sudo sysctl vm.swappiness=10
# Change permanently
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
Storage Troubleshooting
Checking Filesystem Errors
# Check ext4 filesystem (must be unmounted)
sudo umount /dev/sdb1
sudo e2fsck -f /dev/sdb1
# Force check on next boot
sudo touch /forcefsck
Recovering Deleted Files
Install and use testdisk
for recovery:
sudo apt install testdisk
# Run testdisk
sudo testdisk
Fixing Bad Blocks
# Check for bad blocks and mark them
sudo badblocks -v /dev/sdb1 > bad-blocks
sudo e2fsck -l bad-blocks /dev/sdb1
S.M.A.R.T. Monitoring
# Install smartmontools
sudo apt install smartmontools
# Check drive health
sudo smartctl -a /dev/sda
# Run short self-test
sudo smartctl -t short /dev/sda
# Show test results
sudo smartctl -l selftest /dev/sda
Advanced Storage Topics
Encrypted Storage with LUKS
# Install cryptsetup
sudo apt install cryptsetup
# Create encrypted partition
sudo cryptsetup luksFormat /dev/sdb1
# Open encrypted partition
sudo cryptsetup luksOpen /dev/sdb1 encrypted_data
# Create filesystem
sudo mkfs.ext4 /dev/mapper/encrypted_data
# Mount
sudo mkdir -p /secure
sudo mount /dev/mapper/encrypted_data /secure
Add to fstab with crypttab for persistence:
# Create entry in /etc/crypttab
echo "encrypted_data UUID=$(sudo blkid -s UUID -o value /dev/sdb1) none luks" | sudo tee -a /etc/crypttab
# Add to fstab
echo "/dev/mapper/encrypted_data /secure ext4 defaults 0 2" | sudo tee -a /etc/fstab
Network Storage
NFS Client Setup
# Install NFS client
sudo apt install nfs-common
# Create mount point
sudo mkdir -p /mnt/nfs
# Mount NFS share
sudo mount -t nfs server:/share /mnt/nfs
# Add to fstab
echo "server:/share /mnt/nfs nfs defaults 0 0" | sudo tee -a /etc/fstab
Samba Client Setup
# Install Samba client
sudo apt install cifs-utils
# Create credentials file
echo "username=user" | sudo tee /etc/samba/credentials
echo "password=pass" | sudo tee -a /etc/samba/credentials
sudo chmod 600 /etc/samba/credentials
# Mount SMB share
sudo mkdir -p /mnt/smb
sudo mount -t cifs //server/share /mnt/smb -o credentials=/etc/samba/credentials
# Add to fstab
echo "//server/share /mnt/smb cifs credentials=/etc/samba/credentials,vers=3.0 0 0" | sudo tee -a /etc/fstab
Summary
In this guide, we've covered the essential aspects of Debian storage management:
- Basic storage concepts and disk partitioning
- Filesystem creation, mounting, and management
- Advanced storage with LVM for flexibility
- RAID configuration for redundancy
- Swap space management
- Troubleshooting storage issues
- Encrypted and network storage options
Mastering storage management is crucial for effective Debian system administration. These skills will help you design, implement, and maintain reliable storage infrastructures for various use cases.
Exercises
- Create a 1GB swap file, enable it, and configure it to be mounted at boot.
- Set up LVM with two physical volumes, creating separate logical volumes for
/var
and/home
. - Create a RAID 1 array using two partitions and mount it at
/data
. - Configure an encrypted partition and set it to be automatically mounted at boot time.
- Write a simple bash script that reports disk usage and sends an alert when any filesystem is more than 80% full.
Additional Resources
- Debian Wiki - LVM
- Debian Wiki - Software RAID
- Debian Administrator's Handbook
- Linux Documentation Project - Storage
- The
man
pages for commands likefdisk
,parted
,lvm
,mdadm
, etc.
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)