7. storage and filesystems - mishraxharshit/harshitxmishra.github.io GitHub Wiki

Phase 7 — Storage and Filesystems

Previous: [Phase 6 — Networking](Phase-6-Networking) | Next: [Phase 8 — Shell Scripting](Phase-8-Shell-Scripting)


7.1 How Linux Sees Storage

In Linux, every storage device appears as a file in /dev/.

Physical disk    →  /dev/sda   (first SATA/SCSI disk)
                    /dev/sdb   (second disk)
                    /dev/nvme0n1  (NVMe SSD)
                    /dev/mmcblk0  (SD card)

Partitions       →  /dev/sda1  (first partition on sda)
                    /dev/sda2  (second partition on sda)

A partition holds a filesystem. A filesystem organises data into files and directories. You make a filesystem accessible by mounting it to a directory.

Disk → Partition → Filesystem → Mount Point (directory in the tree)

7.2 Checking Disk Usage

# Disk space by filesystem (human-readable)
df -h
# Filesystem      Size  Used Avail Use% Mounted on
# /dev/sda1        50G   18G   30G  37% /
# /dev/sda2       200G  120G   74G  62% /home
# tmpfs           7.8G  1.2M  7.8G   1% /dev/shm

# Directory size
du -sh /var/log           # total size of /var/log
du -sh /var/log/*         # size of each item inside
du -sh * | sort -rh       # sort by size, largest first

# Find the largest files on the system
find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null | sort -k5 -rh | head -20

# Check inode usage (inodes are metadata structures, can run out)
df -i

7.3 Listing Block Devices

# lsblk: tree view of block devices
lsblk
# NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
# sda      8:0    0   50G  0 disk
# ├─sda1   8:1    0   48G  0 part /
# └─sda2   8:2    0    2G  0 part [SWAP]
# sdb      8:16   0  200G  0 disk
# └─sdb1   8:17   0  200G  0 part /home

# With filesystem info
lsblk -f

# List all disks with details
sudo fdisk -l

7.4 Partitioning a Disk

Warning: Incorrect partitioning destroys data. Work on the correct device.

# fdisk: interactive partitioning tool
sudo fdisk /dev/sdb

# Inside fdisk:
# p — print current partition table
# n — create new partition
# d — delete partition
# t — change partition type
# w — write changes and exit (changes only take effect here)
# q — quit without saving

# After creating a partition, create a filesystem on it
sudo mkfs.ext4 /dev/sdb1          # ext4 filesystem
sudo mkfs.xfs /dev/sdb1           # XFS filesystem
sudo mkfs.vfat /dev/sdb1          # FAT32 (for USB drives)

7.5 Mounting and Unmounting

# Mount a filesystem
sudo mount /dev/sdb1 /mnt/data
sudo mount /dev/sdb1 /mnt/data -o ro   # read-only
sudo mount -t ext4 /dev/sdb1 /mnt/data # specify filesystem type

# Unmount (must not be in use)
sudo umount /mnt/data
sudo umount /dev/sdb1   # by device

# See what is currently mounted
mount | grep -v "^cgroup"
cat /proc/mounts

# Persistent mounts: add to /etc/fstab
sudo blkid /dev/sdb1     # get UUID of partition
# /dev/sdb1: UUID="a1b2c3d4-e5f6-..." TYPE="ext4"

sudo nano /etc/fstab
# Add a line:
# UUID=a1b2c3d4-e5f6-...  /mnt/data  ext4  defaults  0  2

# Test fstab without rebooting
sudo mount -a    # mounts everything in fstab that is not yet mounted

7.6 How Filesystems Work Internally

Every file in Linux is represented by an inode. An inode stores:

  • File type (regular, directory, symlink, etc.)
  • Permissions, owner, group
  • File size
  • Timestamps (created, modified, accessed)
  • Pointers to data blocks on disk

A directory is itself a special file: a list of (filename, inode number) pairs.

# See inode information
stat notes.txt
# File: notes.txt
# Size: 1024       Blocks: 8       IO Block: 4096  regular file
# Device: 802h     Inode: 2098177  Links: 1
# Access: (0644/-rw-r--r--)  Uid: 1000  Gid: 1000
# Access: 2024-01-15 10:22:00
# Modify: 2024-01-14 09:15:33
# Change: 2024-01-14 09:15:33

# See inode numbers in ls
ls -li

This explains why renaming a file (moving within the same filesystem) is instant: only the directory entry changes, not the data. Copying is slow: data must be written to new blocks and a new inode created.


7.7 Swap Space

Swap is disk space used as overflow when RAM is full. It is much slower than RAM.

# Check current swap
swapon --show
free -h

# Create a swap file (alternative to swap partition)
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

# Make permanent (add to /etc/fstab)
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

# Adjust swappiness (0 = use swap as little as possible, 100 = aggressive)
cat /proc/sys/vm/swappiness
sudo sysctl vm.swappiness=10
# Permanent: add vm.swappiness=10 to /etc/sysctl.conf

Phase 7 Exercises

Exercise 1: Run df -h. Which filesystem is your root partition on? How much space is used and available?

Exercise 2: Run du -sh /var/log/* and identify the three largest log files or directories.

Exercise 3: Use lsblk to draw a diagram of the disk and partition structure of your system.

Exercise 4: Use stat on any file. Explain the difference between the Access, Modify, and Change timestamps.


Previous: [Phase 6 — Networking](Phase-6-Networking) | Next: [Phase 8 — Shell Scripting](Phase-8-Shell-Scripting)