Day 2: File Systems and Lvm
There are 2 meanings of file system under Linux:
1. The Storage Format (The Physical View):
- A file system is a way to organize files on the storage device.
- Windows: NTFS, FAT32, FAT
- Linux: btrfs, ext3, ext4, XFS, ZFS (technically Unix), swap
- [yt] Files & File Systems: Crash Course Computer Science
- [yt] Every Linux File System Explained in 3 Minutes
- [read] Wiki: File system
2. The Directory structure (logical view) aka Filesystem Hierarchy Standard
(FHS).
The Filesystem Hierarchy Standard (FHS) is a formal specification that defines the main directories, their locations, and what types of files they should contain on a Linux (or Unix-like) operating system.
The FHS doesn’t just list directories - it specifies what must/may go in each, and sometimes what must not.
| Directory | FHS Rule (simplified) |
|---|---|
/ | The root directory. Must contain /bin, /boot, /dev, /etc, /home, /lib, /media, /mnt, /opt, /proc, /root, /run, /sbin, /srv, /sys, /tmp, /usr, /var. |
/bin | Essential user command binaries (e.g., ls, cp, sh). Must be available in single-user mode. Cannot have subdirectories. |
/boot | Static files for the boot loader (kernel, initrd). |
/dev | Device files. Managed by kernel/devfs or udev. |
/etc | Host-specific system configuration. No binary executables allowed. |
/home | User home directories (except root). Optional on small systems, but standard. |
/lib | Essential shared libraries and kernel modules for /bin and /sbin. |
/media | Mount point for removable media (CD-ROM, USB). |
/mnt | Temporary mount point for file systems (manual mounting). |
/opt | Add-on application software packages (static, self-contained). Each package gets its own subdirectory (/opt/package/). |
/proc | Virtual file system for process and kernel information. |
/root | Home directory for the root user. Must not be under /home. |
/run | Runtime variable data (since boot). System daemons store PID files, sockets here. |
/sbin | System binaries for administration (e.g., fdisk, mount). Only root uses them typically. |
/srv | Data for services provided by the system (e.g., /srv/www/ for web server, /srv/ftp/). |
/sys | Virtual file system for kernel objects (devices, drivers). |
/tmp | Temporary files. Usually cleared on reboot. Sticky bit set. |
/usr | Secondary hierarchy - read-only, sharable user data. Contains most user commands, libraries, documentation. Historically “user system resources”. Must have /usr/bin, /usr/lib, /usr/local, /usr/share, etc. |
/var | Variable data - files that change size (logs, spool, cache, lock files). |
Linux uses a virtual directory structure. The virtual directory contains a
single base directory - root directory /.
- [video] Learning The Linux File System 2025: Learning The Linux File System 2025
mkfs - ("make filesystem") is used to format a storage partition with a specific file system type.
Basic syntax:
mkfs [options] <device>Common usage:
mkfs -t ext4 /dev/sdb1- formats/dev/sdb1as ext4.mkfs.ext4 /dev/sdb1- formats/dev/sdb1as ext4.mkfs.xfs /dev/sdb1- formats as XFS.mkfs.vfat /dev/sdb1- formats as FAT32.mkfs.ntfs /dev/sdb1- formats as NTFS.
Key points:
- Destroys all existing data on the target partition - use with extreme caution.
- Typically run as
root(withsudo). - The device must be unmounted before formatting.
- Many distributions provide
mkfsas a wrapper for file‑system‑specific commands (mkfs.ext4,mkfs.btrfs, etc.).
Example:
sudo umount /dev/sdb1
sudo mkfs.ext4 /dev/sdb1
sudo mount /dev/sdb1 /mnt/mydrivemount - attach a file system to the directory tree
Basic syntax:
mount [options] <device> <directory>Common usage:
mount /dev/sdb1 /mnt/usb- mounts/dev/sdb1at/mnt/usbmount -t ext4 /dev/sda2 /home- explicitly specifies file system typemount -a- mounts all file systems listed in/etc/fstab
Key points:
- The target directory (mount point) must already exist.
- Usually run as
root(or withsudo). - Without arguments,
mountlists all currently mounted file systems. - Many modern systems auto‑mount removable media (so you rarely need manual
mountfor USB drives).
Example:
sudo mkdir -p /mnt/data
sudo mount /dev/sdc1 /mnt/dataumount - detach a mounted file system
Basic syntax:
umount <device_or_directory>Common usage:
umount /mnt/usb- unmounts by mount pointumount /dev/sdb1- unmounts by device name
Key points:
- You cannot unmount a file system that is currently in use (open files, active processes).
- Use
lsoforfuserto find what’s using it. - Add
-l(lazy unmount) to detach now and clean up later, or-f(force) for stubborn cases.
Example:
sudo umount /mnt/data/etc/fstab
A system configuration file that defines how and where file systems are automatically mounted at boot time or when mount -a is run.
Format (one line per file system):<device> <mount_point> <fs_type> <options> <dump> <pass>
Example line:/dev/sda1 /boot ext4 defaults 0 2
Column meanings:
- Device - Partition (e.g.,
/dev/sda1), UUID (UUID=...), or network share. - Mount point - Directory where it will be attached (must exist).
- File system type -
ext4,xfs,ntfs,swap,auto. - Options -
defaults,ro(read‑only),noauto(don’t auto‑mount),user(allow ordinary users to mount), etc. - Dump - Backup flag (usually
0). - Pass - Boot‑time
fsckorder (0= skip,1= root,2= other file systems).
Key points:
- Read by
mount -aand the init system at boot. - Mistakes here can make your system fail to boot.
- Using
UUID=is preferred over device names (/dev/sda1) because device names can change. - After editing, run
mount -ato test for errors without rebooting.
Example typical entry:
UUID=1234-abcd /home ext4 defaults 0 2df– Reports disk space usage of mounted file systems (e.g.,df -hfor human‑readable sizes). Shows total, used, free space, and mount points.du– Estimates file and directory space usage (e.g.,du -sh /home). Useful for finding large folders.-s= summary,-h= human‑readable.iostat– Reports CPU and I/O statistics for devices and partitions. Helps identify disk bottlenecks. Often used with-xfor extended device info.lsblk– Lists information about all available block devices (disks, partitions, LVM). Shows tree‑like output, sizes, mount points, and UUIDs.fsck– Checks and repairs file system consistency (e.g.,fsck /dev/sda1). Must usually be run on unmounted file systems. Rarely run manually today because boot‑time checks handle most issues.
Storage alternatives
Multipath – Provides redundant physical paths (cables, controllers) between the server and a storage device (e.g., SAN). If one path fails, I/O automatically switches to another. Improves reliability and load‑balancing. Managed by
multipathdin Linux.Logical Volume Manager (LVM) – A software layer that abstracts physical disks into flexible storage pools. Lets you create, resize, and move logical volumes without repartitioning. Components: Physical Volumes (PVs) → Volume Group (VG) → Logical Volumes (LVs).
RAID (Redundant Array of Independent Disks) – Combines multiple physical disks into one logical unit for redundancy, performance, or both. Common levels: RAID 0 (striping, no redundancy), RAID 1 (mirroring), RAID 5 (striping with parity), RAID 10 (mirrored stripes). Can be hardware‑ or software‑based (e.g.,
mdadmin Linux).