Filesystems and Mounting#

Concepts#

What Is a Filesystem?#

A filesystem organizes data on a partition — it defines how files are stored, named, and retrieved. Without a filesystem, a partition is just raw bytes.

Filesystem Description Default On
ext4 Standard Linux filesystem. Reliable, mature, well-supported. Ubuntu, Debian
xfs High-performance, good for large files. RHEL, CentOS
btrfs Modern, supports snapshots, compression, RAID. openSUSE, Fedora (optional)
vfat/FAT32 Simple, cross-platform. Max file size 4GB. USB drives, EFI partitions
ntfs Windows filesystem. Linux has read/write support. Windows drives
tmpfs Lives in RAM, not on disk. Fast, cleared on reboot. /tmp, /run

Creating a Filesystem#

sudo mkfs.ext4 /dev/sdb1          # ext4
sudo mkfs.xfs /dev/sdb1           # XFS
sudo mkfs.vfat /dev/sdb1          # FAT32
sudo mkfs.btrfs /dev/sdb1         # Btrfs

# With a label
sudo mkfs.ext4 -L "mydata" /dev/sdb1

Warning: mkfs erases all data on the partition.

Mounting#

Mounting connects a filesystem to a directory in the file tree. The directory becomes the access point for the filesystem’s contents.

# Mount a partition
sudo mount /dev/sdb1 /mnt/data

# Mount with specific options
sudo mount -o ro /dev/sdb1 /mnt/data          # read-only
sudo mount -o noexec /dev/sdb1 /mnt/data       # prevent execution

# Mount by UUID (more reliable than device name)
sudo mount UUID=abcd-1234 /mnt/data

# View all mounted filesystems
mount | column -t
findmnt
df -h

# Unmount
sudo umount /mnt/data
# or
sudo umount /dev/sdb1

Note: You cannot unmount a filesystem if any process is using it. Check with lsof /mnt/data or fuser -m /mnt/data.

/etc/fstab — Persistent Mounts#

/etc/fstab defines filesystems that are mounted automatically at boot.

cat /etc/fstab

Format:

# <filesystem>              <mountpoint>  <type>  <options>       <dump> <pass>
UUID=abcd-1234-5678          /             ext4    errors=remount-ro 0      1
UUID=efgh-9012               /home         ext4    defaults        0      2
UUID=ABCD-EF01               /boot/efi     vfat    umask=0077      0      1
/dev/sda3                    none          swap    sw              0      0
tmpfs                        /tmp          tmpfs   defaults        0      0
Field Meaning
filesystem Device (UUID preferred over /dev/sdX)
mountpoint Where to mount
type Filesystem type (ext4, xfs, swap, tmpfs)
options Mount options (defaults, ro, noexec, noatime)
dump Backup flag (0 = no, 1 = yes). Usually 0.
pass fsck order (0 = skip, 1 = root, 2 = other)

Adding an Entry to fstab#

# 1. Get the UUID
sudo blkid /dev/sdb1

# 2. Create the mount point
sudo mkdir -p /mnt/data

# 3. Add to fstab
echo "UUID=your-uuid-here  /mnt/data  ext4  defaults  0  2" | sudo tee -a /etc/fstab

# 4. Test (mount everything in fstab that isn't already mounted)
sudo mount -a

# 5. Verify
df -h /mnt/data

Always use UUID in fstab, not /dev/sdX names. Device names can change (e.g., after adding a disk), but UUIDs are permanent.

df and du — Disk Space#

# Filesystem disk usage
df -h                    # human-readable
df -h /                  # specific mount point
df -T                    # show filesystem type

# Directory disk usage
du -sh /var/log           # total size of a directory
du -sh /home/*            # size of each user's home
du -h --max-depth=1 /var  # one level deep

Checking and Repairing Filesystems#

# Check a filesystem (must be unmounted)
sudo fsck /dev/sdb1
sudo fsck.ext4 /dev/sdb1

# Check and auto-repair
sudo fsck -y /dev/sdb1

# Check the root filesystem at next boot
sudo touch /forcefsck

Lab#

Exercise 1: View Mounted Filesystems#

df -hT
findmnt --real
cat /etc/fstab

Exercise 2: Examine UUIDs#

sudo blkid
lsblk -f

Exercise 3: Disk Usage Analysis#

df -h
du -sh /var/* 2>/dev/null | sort -rh | head -10
du -sh /home/$(whoami)

Exercise 4: Mount a tmpfs#

# Create a temporary RAM-based filesystem
sudo mkdir -p /mnt/ramdisk
sudo mount -t tmpfs -o size=100M tmpfs /mnt/ramdisk

# Use it
echo "This is in RAM!" > /mnt/ramdisk/test.txt
df -h /mnt/ramdisk

# Clean up
sudo umount /mnt/ramdisk
sudo rmdir /mnt/ramdisk

Review#

1. What is the default filesystem on Ubuntu and Debian?

ext4. It is mature, reliable, and well-supported. Both distros use it by default for the root filesystem.

2. What does mounting do?

Mounting connects a filesystem (on a partition) to a directory in the file tree. The directory becomes the access point. Files on the partition are accessible through that directory.

3. Why should you use UUIDs instead of `/dev/sdX` names in fstab?

Device names (/dev/sda, /dev/sdb) can change when disks are added, removed, or reordered. UUIDs are unique identifiers that stay the same regardless of device order.

4. What is the difference between `df` and `du`?

df shows filesystem-level usage (how much of the partition is used). du shows directory-level usage (how much space a specific directory and its contents occupy).

5. What happens if fstab has an error?

The system may fail to boot or boot into emergency mode. Always test with sudo mount -a after editing fstab, and keep a backup.


Previous: Block Devices and Partitions | Next: LVM and Swap