Linux Storage & Filesystem
At a glance#
- Purpose: Reference on Linux storage concepts: device naming, partition tables, filesystems, mounting, LVM and troubleshooting.
- Applies to: Any Linux distribution.
- Risk: Low as a reference - individual commands shown are destructive and marked as such.
- Time: Reference article.
1. Storage Concepts#
Device naming in Linux#
- Explanation:
Linux treats everything as a file, even disks. Whole disks appear as /dev/sda, /dev/sdb. Partitions are numbered (/dev/sda1). NVMe SSDs use /dev/nvme0n1 (disk) and /dev/nvme0n1p1 (partition).
👉 Important because you must know whether you’re formatting a whole disk or just a partition.
- Demo:
``bash lsblk -f ``
Shows disks, partitions, filesystems, and mount points.
Virtual vs physical disks#
- Explanation:
In VMs or cloud, disks are virtual devices, but Linux handles them the same as physical disks. Admins must understand this to troubleshoot when a cloud disk gets detached or resized.
- Demo:
``bash lsblk ``
Compare physical server (sda, sdb) vs VM (often shows VDA/NVMe).
Partition table formats: MBR vs GPT#
- Explanation:
- MBR: Old format, supports up to 2TB disks, 4 partitions max.
- GPT: Modern, supports huge disks, stores backup tables, nearly unlimited partitions.
👉 Use GPT for new systems unless legacy BIOS needs MBR.
- Demo:
``bash sudo parted /dev/sdb print sudo parted /dev/sdb mklabel gpt ``
Kernel detection of new disks#
- Explanation:
When adding new storage, Linux logs detection in the kernel ring buffer. Useful to confirm if OS can see the disk.
- Demo:
``bash dmesg | grep sd ls /sys/block/ ``
2. Filesystem Types#
ext4#
- Explanation: Default Linux FS. Reliable, supports large files, shrinking possible. Good general-purpose choice.
- Demo:
``bash sudo mkfs.ext4 /dev/sdb1 ``
xfs#
- Explanation: Enterprise FS, great performance, scales well to TB–PB. Can’t shrink.
- Demo:
``bash sudo mkfs.xfs /dev/sdb2 ``
tmpfs#
- Explanation: In-memory FS. Extremely fast but volatile (lost on reboot). Used for
/tmp, cache. - Demo:
``bash sudo mount -t tmpfs -o size=100M tmpfs /mnt/tmpfs df -hT | grep tmpfs ``
Choosing FS based on workload#
- Logs → xfs (handles many writes).
- Databases → xfs (performance) or ext4 (shrinkable).
- Media/archive → ext4 (simple, safe).
3. Mounting & Fstab#
Temporary vs persistent mounts#
- Explanation:
mount= temporary, disappears on reboot./etc/fstab= permanent, system auto-mounts at boot.
Using UUIDs is safer than /dev/sdX since disk order can change.
- Demo:
```bash sudo mount /dev/sdb1 /mnt/data findmnt /mnt/data
blkid /dev/sdb1 # Add to /etc/fstab: UUID=<uuid> /mnt/data ext4 defaults 0 2 sudo mount -a ```
Mount options & troubleshooting#
- Explanation:
Options like noexec, ro, nodev improve security.
If a mount is busy (process still using it), umount may fail. Use lazy unmount (-l) or force (-f).
- Demo:
``bash tail -f /mnt/data/file & sudo umount /mnt/data # will fail sudo umount -l /mnt/data ``
4. Disk Usage & Monitoring#
Checking usage#
- Explanation:
df -h→ space usage.du -sh→ size of directory.lsblk→ disk layout.findmnt→ see mounted filesystems.
- Demo:
``bash df -hT du -sh /var/log/* lsblk -f findmnt ``
Inode exhaustion#
- Explanation:
Even if free space exists, if inodes (file metadata entries) run out, you can’t create new files. Common with apps generating tons of small files.
- Demo:
``bash mkdir /mnt/inodetest sudo mount -t tmpfs -o size=50M tmpfs /mnt/inodetest for i in $(seq 1 50000); do touch /mnt/inodetest/file$i; done df -i /mnt/inodetest touch /mnt/inodetest/newfile # will fail with "No space left on device" ``
ncdu tool#
- Explanation:
ncdu is a curses-based interactive disk usage analyzer. Great for exploring space hogs.
- Demo:
``bash sudo apt install ncdu -y sudo ncdu /var/log ``
5. Troubleshooting Storage#
Disk full#
- Explanation: Services fail if no free space.
- Demo:
``bash dd if=/dev/zero of=/mnt/data/bigfile bs=1M count=500 df -h /mnt/data ``
Deleted file still holding space#
- Explanation: If a process has a file open, deleting it won’t free space until process ends.
- Demo:
``bash tail -f /mnt/data/bigfile & rm /mnt/data/bigfile df -h /mnt/data lsof | grep deleted ``
Filesystem corruption#
- Explanation: Power loss or crashes can corrupt disks.
fsckrepairs. Never run on mounted FS. - Demo:
``bash sudo umount /dev/sdb1 sudo fsck /dev/sdb1 ``
Swap usage#
- Explanation: Swap extends RAM. If system swaps heavily, performance suffers.
- Demo:
``bash swapon --show free -h ``
6. LVM (Logical Volume Manager)#
Why LVM?#
- Explanation:
Lets you resize disks, combine multiple disks, take snapshots. Flexible and enterprise-grade.
LVM Workflow Demo#
sudo apt install lvm2 -y
sudo pvcreate /dev/sdc
sudo vgcreate vgdata /dev/sdc
sudo lvcreate -L 500M -n lvdata vgdata
sudo mkfs.ext4 /dev/vgdata/lvdata
sudo mkdir /mnt/lvm
sudo mount /dev/vgdata/lvdata /mnt/lvm
df -hT /mnt/lvmExtend LV#
- Explanation: Grow disks without downtime.
- Demo:
``bash sudo lvextend -L +200M /dev/vgdata/lvdata sudo resize2fs /dev/vgdata/lvdata df -hT /mnt/lvm ``
Shrink LV (advanced, risky)#
- Explanation: Only ext4 supports shrink, must unmount first.
- Demo:
``bash sudo umount /mnt/lvm sudo e2fsck -f /dev/vgdata/lvdata sudo resize2fs /dev/vgdata/lvdata 300M sudo lvreduce -L 300M /dev/vgdata/lvdata sudo mount /dev/vgdata/lvdata /mnt/lvm ``
Snapshot#
- Explanation: Take point-in-time copies, useful before patching or risky operations.
- Demo:
``bash sudo lvcreate -s -L 100M -n lvsnap /dev/vgdata/lvdata sudo mount /dev/vgdata/lvsnap /mnt/snap ls /mnt/snap sudo umount /mnt/snap sudo lvremove -y /dev/vgdata/lvsnap ``