Linux Storage Management and Filesystems: Managing Disks and Partitions
Storage management is a core skill for Linux system administrators. Understanding block devices, partitions, filesystems, and mounting is essential for managing disk space, organizing data, and ensuring system reliability. This comprehensive guide covers everything from basic device concepts to advanced filesystem operations.
Understanding Device Types
Block Devices
Block Devices: Storage devices that store data in fixed-size blocks or sectors.
Characteristics:
- Data is read and written in blocks
- Examples: Hard drives, solid-state drives, USB drives
- Represented as
/dev/sda,/dev/sdb, etc. - Can be partitioned and formatted
Common Block Devices:
/dev/sda,/dev/sdb: SCSI/SATA disks/dev/nvme0n1: NVMe SSDs/dev/vda: Virtual disks (in VMs)
Character Devices
Character Devices: Devices that transfer data character by character.
Characteristics:
- Data is transferred one character at a time
- Examples: Keyboards, mice, serial ports
- Represented as
/dev/tty,/dev/console, etc. - Used for streaming data
Filesystems
A filesystem is a method (data structure) used to organize and store data on storage devices.
Common Linux Filesystems
ext2 (Extended Filesystem 2):
- Older filesystem, non-journaling
- Used in some older Linux releases
- Fast but less resilient to crashes
ext3 (Extended Filesystem 3):
- Enhanced version of ext2 with journaling
- Better recovery after crashes
- Widely used in older systems
ext4 (Extended Filesystem 4):
- Improved version of ext3
- Supports volumes up to 1 exabyte
- Files up to 16 terabytes
- Default on many Linux distributions
- Journaling filesystem
XFS (X Filesystem):
- 64-bit, high-performance journaling filesystem
- Fast recovery
- Efficient handling of large files
- Default on RHEL 7+ and many enterprise systems
BTRFS (B-Tree Filesystem):
- Modern copy-on-write filesystem
- Advanced features: snapshots, subvolumes
- Supports volumes up to 16 exabytes
- Files up to 18 quintillion per volume
- Built-in RAID and compression
NTFS: Microsoft's proprietary journaling filesystem (for Windows compatibility)
FAT32: Older filesystem compatible with different operating systems
Filesystem Types
Journaling Filesystems:
- Record changes before writing to disk
- Better recovery after crashes
- Examples: ext3, ext4, XFS, BTRFS, NTFS
Non-Journaling Filesystems:
- Write changes directly to disk
- Faster but less resilient
- Example: ext2
Network Filesystems
- SMB/CIFS: Server Message Block / Common Internet File System
- NFS: Network File System
- SSHFS: Secure Shell File System
- FTP/SFTP: File Transfer Protocols
- SCP: Secure Copy Protocol
- RSYNC: Remote Sync
Inodes and Virtual Filesystem
Index Node (inode)
Inode: A data structure that stores metadata about files and directories.
Metadata Includes:
- File type, permissions, owner, group
- Size, timestamps
- Data block locations
- Location: Root of filesystem
- Size: Typically 128 or 256 bytes
Inode Limits:
- Each filesystem has a fixed number of inodes
- Running out of inodes prevents file creation (even with free space)
- Check with:
df -i
Virtual Filesystem (VFS)
VFS: An abstraction layer allowing multiple filesystems to be accessed through a common interface.
Virtual Filesystem Types:
/proc: Process information/sys: System information/dev: Device files/run: Runtime information/tmp: Temporary files/mnt: Mount points/media: Removable media/home: User home directories/var: Variable data/usr: User programs
Filesystem Hierarchy Standard (FHS)
The FHS defines the standard directory structure for Linux systems.
Key Directories
/: Root directory/bin: Essential user binaries/boot: Boot loader files/dev: Device files/etc: System configuration files/home: User home directories/lib: Library files/media: Removable media mount points/mnt: Temporary mount points/opt: Optional software/proc: Process information (virtual)/root: Root user home directory/run: Runtime information/sbin: System binaries/srv: Service data/sys: System information (virtual)/tmp: Temporary files/usr: User programs/var: Variable data
Partitions
Partition Types
Primary Partitions:
- Bootable partitions
- Contains one filesystem or logical drive
- Maximum of 4 primary partitions per disk
- Sometimes referred to as volumes
Extended Partitions:
- Contains several filesystems (logical drives)
- Used to overcome the 4-partition limit
- Cannot be directly formatted
- Acts as a container for logical partitions
Logical Partitions:
- Additional partitions within an extended partition
- Partitioned and allocated as independent units
- Functions as a separate drive
- Numbered starting from 5 (e.g.,
/dev/sda5)
Partitioning Tools
fdisk - Partition Table Manipulator
Basic Usage:
# List all partitions
fdisk -l
# List partitions on specific device
fdisk -l /dev/sda
# Interactive mode
sudo fdisk /dev/sda
Interactive Commands:
n: Create a new partitionp: Print the partition tabled: Delete a partitionq: Quit without savingw: Write changes to disk
Example Workflow:
sudo fdisk /dev/sda
# n (new partition)
# p (primary)
# 1 (partition number)
# Enter (default start)
# +10G (size)
# w (write and exit)
parted - GNU Parted Utility
Basic Usage:
# Interactive mode
sudo parted /dev/sda
# Non-interactive commands
sudo parted /dev/sda print
sudo parted /dev/sda mkpart primary ext4 1MiB 10GiB
Parted Commands:
select <device>: Select a devicemklabel <label>: Create a new disk label (e.g., gpt, msdos)mkpart <part-type> <fs-type> <start> <end>: Create partitionrm <number>: Remove a partitionprint: Display partition tableresizepart <number> <end>: Resize a partitionquit: Exit
partprobe - Inform OS of Partition Changes
Usage:
# Inform kernel of partition table changes
sudo partprobe /dev/sda
# Or with options
sudo partprobe -s # Display changes
sudo partprobe -d # Debug mode
sudo partprobe -v # Verbose mode
When to Use:
- After creating or deleting partitions
- Before formatting new partitions
- Ensures kernel recognizes partition changes immediately
Formatting Filesystems
mkfs - Create Filesystem
Basic Usage:
# Create ext4 filesystem
sudo mkfs -t ext4 /dev/sda1
# Or using filesystem-specific command
sudo mkfs.ext4 /dev/sda1
sudo mkfs.xfs /dev/sda1
Common Options:
-t <type>: Specify filesystem type-c: Check for bad blocks before creating-L <label>: Set filesystem label-v: Verbose output
Filesystem-Specific Commands:
# ext2/ext3/ext4
sudo mkfs.ext4 /dev/sda1
sudo mke2fs -t ext4 /dev/sda1
# XFS
sudo mkfs.xfs /dev/sda1
# BTRFS
sudo mkfs.btrfs /dev/sda1
Filesystem Labels
Setting Labels:
# ext2/ext3/ext4
sudo e2label /dev/sda1 mylabel
# XFS
sudo xfs_admin -L mylabel /dev/sda1
# View label
sudo blkid /dev/sda1
Mounting Filesystems
mount - Mount a Filesystem
Basic Syntax: mount [options] <device> <mount_point>
Basic Usage:
# Mount a device
sudo mount /dev/sda1 /mnt/data
# Mount with filesystem type
sudo mount -t ext4 /dev/sda1 /mnt/data
# Mount with options
sudo mount -o rw,noatime /dev/sda1 /mnt/data
Common Mount Options:
auto: Automatically mount at bootnoauto: Do not automatically mountexec: Allow execution of binariesnoexec: Do not allow executionsuid: Allow setuid permissionnosuid: Do not allow setuidro: Read-onlyrw: Read-writeuser: Allow users to mountnouser: Only root can mountsync: Synchronize data writesasync: Asynchronous writesatime: Update access timenoatime: Do not update access timerelatime: Update access time relative to modification time
Mounting All Filesystems:
# Mount all filesystems in /etc/fstab
sudo mount -a
umount - Unmount a Filesystem
Basic Syntax: umount [options] <mount_point_or_device>
Common Options:
-l: Lazy unmount (detach filesystem immediately)-f: Force unmount-r: Remount as read-only if unmount fails-v: Verbose output-a: Unmount all filesystems in/etc/fstab
Examples:
# Unmount by mount point
sudo umount /mnt/data
# Unmount by device
sudo umount /dev/sda1
# Lazy unmount (if busy)
sudo umount -l /mnt/data
# Force unmount
sudo umount -f /mnt/data
FUSE (Filesystem in USErspace)
FUSE: Allows non-privileged users to create their own filesystems.
Location: /dev/fuse
Use Cases:
- SSHFS (mount remote filesystems via SSH)
- Encrypted filesystems
- Cloud storage integration
- Custom filesystem implementations
/etc/fstab - Filesystem Table
The /etc/fstab file lists filesystems to mount at boot.
fstab Format
Each line contains six fields:
<device> <mount_point> <filesystem_type> <options> <dump> <pass>
Field Explanations:
- Device: Device name, UUID, or label
- Mount Point: Directory where filesystem is mounted
- Filesystem Type: Type of filesystem (ext4, xfs, etc.)
- Options: Mount options (comma-separated)
- Dump: Backup frequency (0 = no backup)
- Pass: Filesystem check order (0 = no check, 1 = root, 2 = others)
Example fstab Entry
UUID=12345678-1234-1234-1234-123456789abc /mnt/data ext4 defaults 0 2
Using UUID (recommended):
# Get UUID
sudo blkid /dev/sda1
# Add to /etc/fstab
UUID=abc123... /mnt/data ext4 defaults 0 2
Using Labels:
LABEL=mydata /mnt/data ext4 defaults 0 2
Using Device Names (not recommended):
/dev/sda1 /mnt/data ext4 defaults 0 2
Filesystem Information Files
/etc/mtab
Purpose: Lists currently mounted filesystems (legacy, now often symlinked to /proc/mounts)
Fields:
- Device name or UUID
- Mount point
- Filesystem type
- Mount options
/proc/mounts
Purpose: Virtual file showing currently mounted filesystems
Usage:
cat /proc/mounts
/proc/filesystems
Purpose: Lists filesystem types supported by the kernel
Usage:
cat /proc/filesystems
/proc/partitions
Purpose: Lists partition information
Usage:
cat /proc/partitions
Filesystem Maintenance
fsck - Filesystem Check
Purpose: Check and repair filesystem consistency
Basic Usage:
# Check filesystem (must be unmounted)
sudo fsck /dev/sda1
# Check with specific type
sudo fsck -t ext4 /dev/sda1
# Automatic repair
sudo fsck -a /dev/sda1
# Interactive repair
sudo fsck -r /dev/sda1
# Verbose output
sudo fsck -V /dev/sda1
Filesystem-Specific Commands:
# ext2/ext3/ext4
sudo e2fsck /dev/sda1
# XFS (check only, no repair)
sudo xfs_repair -n /dev/sda1
Important: Filesystems should be unmounted before running fsck (except for read-only checks).
Filesystem Resize
resize2fs - Resize ext2/ext3/ext4 filesystems
Basic Usage:
# Resize to partition size
sudo resize2fs /dev/sda1
# Resize to specific size
sudo resize2fs /dev/sda1 20G
# Shrink to minimum
sudo resize2fs -M /dev/sda1
# Print minimum size
sudo resize2fs -P /dev/sda1
XFS Resize:
# XFS can only be grown, not shrunk
sudo xfs_growfs /mnt/data
tune2fs - Adjust ext2/ext3/ext4 Parameters
Common Operations:
# List filesystem information
sudo tune2fs -l /dev/sda1
# Set maximum mount count
sudo tune2fs -c 30 /dev/sda1
# Set check interval
sudo tune2fs -i 2w /dev/sda1 # 2 weeks
# Set reserved blocks percentage
sudo tune2fs -m 5 /dev/sda1 # 5%
# Set filesystem label
sudo tune2fs -L mylabel /dev/sda1
Special Device Files
Device Identification
/dev/disk/by-id: Hardware serial numbers
- Persistent identifier based on hardware serial
- Example:
/dev/disk/by-id/scsi-SATA_ST1000DM003_ABC123
/dev/disk/by-path: Physical path to device
- Shortest physical path
- Example:
/dev/disk/by-path/pci-0000:00:1f.2-ata-1
/dev/disk/by-uuid: Filesystem UUID
- Universally unique identifier
- Assigned when filesystem is formatted
- Most reliable for
/etc/fstab
Special Device Files
/dev/null: Null device (discards all data)
# Discard output
command > /dev/null 2>&1
/dev/zero: Zero device (returns null bytes)
# Create zero-filled file
dd if=/dev/zero of=file bs=1M count=100
/dev/random: Random device (returns random bytes)
# Generate random data
head -c 10 /dev/random
/dev/urandom: Unpredictable random device
# Generate random data (non-blocking)
head -c 10 /dev/urandom
/dev/loop0: Loop device (mount files as filesystems)
# Mount ISO file
sudo mount -o loop file.iso /mnt/iso
Best Practices
Partition Planning
- Plan Ahead: Consider future growth when partitioning
- Use Separate Partitions: Separate
/,/home,/var,/tmp - Use LVM: Consider LVM for flexibility
- Document Layout: Keep records of partition layouts
Mount Point Guidelines
- Use Standard Locations:
/mntfor temporary,/mediafor removable - Use UUIDs in fstab: More reliable than device names
- Test fstab Entries: Use
mount -ato test before reboot - Backup fstab: Always backup before editing
Filesystem Selection
- ext4: Good general-purpose choice
- XFS: Excellent for large files and high performance
- BTRFS: Advanced features, good for modern systems
- Match Use Case: Choose based on workload requirements
Troubleshooting
Common Issues
"Device Busy" Error:
- Check what's using the device:
lsof /mnt/dataorfuser -m /mnt/data - Unmount all subdirectories first
- Use lazy unmount:
umount -l
Filesystem Errors:
- Run
fsckon unmounted filesystem - Check
/var/log/messagesfor errors - Review
dmesgfor kernel messages
Mount Failures:
- Verify device exists:
lsblk - Check filesystem type:
blkid - Verify mount point exists and is empty
- Check
/etc/fstabsyntax
Diagnostic Commands
# List block devices
lsblk
# Show disk usage
df -h
# Show inode usage
df -i
# Show mounted filesystems
mount
findmnt
# Show filesystem information
blkid
tune2fs -l /dev/sda1
Conclusion
Storage management and filesystems are fundamental skills for Linux administrators. Understanding block devices, partitions, filesystem types, and mounting enables you to effectively manage disk space and organize data. From basic partitioning with fdisk to advanced filesystem operations with tune2fs, these concepts form the foundation of Linux storage administration.
By following best practices for partition planning, mount point management, and filesystem selection, you can build reliable and maintainable storage configurations. Regular maintenance with fsck and proper /etc/fstab configuration ensure your filesystems remain healthy and accessible.
In the next article, we'll explore Logical Volume Management (LVM), which provides flexible storage management beyond traditional partitions. Stay tuned!