Skip to content
$ lynuxpro
← Linux

LESSON 10 OF 26 · INTERMEDIATE · 19 MIN

Disks, filesystems and storage

In one minute

  • How Linux uses disks: how a disk is prepared, attached to the folder tree, and how you check and grow the space.
  • "Disk full" is one of the most common production problems. Logs grow, Docker images pile up, a database gets bigger, and suddenly the app cannot save anything.
  • A disk is split into partitions. Each partition gets a filesystem (a way of organising files). The filesystem is then mounted (attached) to a folder, such as /data, so it appears in the tree.
  • 👉 When a disk is filling up, when you add a new disk, or when you grow a cloud disk.

Think of it like: a new shelf for your house. The shelf (disk) is divided into sections (partitions), you put boxes and labels on it (filesystem), then you hang it on a wall in a room (mount point) so people can use it.

How Linux sees storage

Physical disk (/dev/nvme1n1)  └── Partition (/dev/nvme1n1p1)        └── Filesystem (ext4 or xfs)              └── Mounted at a folder (/data)
OUTPUTterminalutf-8 · 4L
WordMeaning
Block deviceA disk or partition, shown as a file in /dev
PartitionA slice of a disk
FilesystemHow data is organised on a partition (ext4, xfs)
Mount pointThe folder where the filesystem appears in the tree

Listing disks: lsblk

lsblk lists every disk and partition, and where each one is attached.

👉 Before you touch any disk, and to find a newly added disk.

lsblk
SHELLbashutf-8 · 1L
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTSnvme0n1     259:0    0   30G  0 disk├─nvme0n1p1 259:1    0 29.9G  0 part /└─nvme0n1p15 259:3   0  106M  0 part /boot/efinvme1n1     259:4    0  100G  0 disk
OUTPUTterminalutf-8 · 5L

Here nvme1n1 is a 100 GB disk with no partitions and no mount point. That is a new, unused disk.

Remember it: lsblk = list block devices.

How full is each disk? df

df shows how much space is used and free on each filesystem (disk free).

👉 The first command when someone says "the disk is full".

df -h
SHELLbashutf-8 · 1L
Filesystem      Size  Used Avail Use% Mounted on/dev/root        30G  8.2G   22G  28% /tmpfs           2.0G     0  2.0G   0% /dev/shm
OUTPUTterminalutf-8 · 3L

-h shows sizes people can read. Set alerts when Use% passes 80–85%. At 100%, apps start failing in strange ways.

A filesystem can also run out of inodes even when there is free space. An inode is the slot that each file needs, so millions of tiny files can use them all up:

df -i
OUTPUTterminalutf-8 · 1L

What is using the space? du

du shows how much space a folder uses (disk usage).

👉 After df says a disk is full, to find what filled it.

du -sh /var/log
SHELLbashutf-8 · 1L
du -sh /var/* | sort -h          # biggest folders under /var, largest lastdu -ah /var/log | sort -h | tail -n 10
OUTPUTterminalutf-8 · 2L

Go down one level at a time: / → /var → /var/lib → /var/lib/docker, until you find the cause. Usual suspects:

FolderUsual causeSafe fix
/var/logLogs never cleaned upSet up logrotate, compress old logs
/var/lib/dockerOld images and stopped containersdocker system prune
/tmpLeftover temporary filesDelete old files
/var/cache/aptDownloaded package filesapt clean
/var/lib/journalA big systemd journaljournalctl --vacuum-size=500M

Remember it: df = how full is the disk. du = which folder filled it.

The trap: deleted, but still open

You delete a huge log, but df still says the disk is full. Why? A running program still has the file open, so Linux cannot free the space until the program lets go.

sudo lsof +L1                     # files deleted but still opensudo systemctl restart app        # or truncate instead of deleting:: > /var/log/app/huge.log         # empties the file in place, space freed immediately
OUTPUTterminalutf-8 · 3L

Empty (truncate) live logs. Do not delete them.

Adding a new disk

👉 You attached a new disk (for example, a new cloud volume) and want to use it.

lsblk                                   # 1. find the new disk, e.g. /dev/nvme1n1sudo mkfs.ext4 /dev/nvme1n1             # 2. create a filesystem (erases the disk!)sudo mkdir -p /data                     # 3. create a mount pointsudo mount /dev/nvme1n1 /data           # 4. mount itdf -h /data                             # 5. confirm
OUTPUTterminalutf-8 · 5L
sudo mount /dev/xvdf /data
SHELLbashutf-8 · 1L

Making it stay after a reboot: /etc/fstab

/etc/fstab is the list of disks to mount at every boot.

A plain mount is forgotten after a reboot.

Add one line, using the disk's UUID (its permanent ID). Names like /dev/sdb can change between reboots. A UUID never does.

sudo blkid /dev/nvme1n1# UUID="3f1c...e2" TYPE="ext4"# /etc/fstabUUID=3f1c...e2  /data  ext4  defaults,nofail  0  2
OUTPUTterminalutf-8 · 5L
  • nofail lets the server still boot if the disk is missing.
  • Test before you reboot with sudo mount -a. A broken fstab line can stop a server from booting at all.

ext4 or xfs?

ext4xfs
Default onUbuntu, DebianRHEL, Amazon Linux
Grow while in useYes (resize2fs)Yes (xfs_growfs)
ShrinkYes (unmounted)No
Good atGeneral use, very matureBig files and many writes at once

Usually you just use your distribution's default.

LVM: flexible volumes

LVM (Logical Volume Manager) pools several disks together, so you can grow a volume without re-slicing disks.

👉 On servers where storage needs keep growing.

Physical volumes (disks)  →  Volume group (pool)  →  Logical volumes (what you mount)
OUTPUTterminalutf-8 · 1L
sudo pvcreate /dev/nvme1n1sudo vgextend vg0 /dev/nvme1n1                 # add the disk to the poolsudo lvextend -r -L +50G /dev/vg0/data         # grow the volume and its filesystem
OUTPUTterminalutf-8 · 3L

-r grows the filesystem together with the volume.

Think of it like: pouring water from several bottles (disks) into one big jug (volume group), then filling glasses (logical volumes) from the jug as needed.

Growing a cloud disk (AWS EBS)

👉 A cloud server's disk is filling up and you made it bigger in AWS.

  1. Make the volume bigger in the AWS console or CLI.
  2. Grow the partition: sudo growpart /dev/nvme0n1 1
  3. Grow the filesystem: sudo resize2fs /dev/nvme0n1p1 (ext4) or sudo xfs_growfs / (xfs)
  4. Check with df -h.

No reboot and no downtime.

Real-world scenario: disk full at 3 a.m.

df -hdu -sh /var/loglsblk
SHELLbashutf-8 · 3L
df -h                                  # / is at 100%du -sh /var/* | sort -h                # /var/log is 18Gdu -ah /var/log | sort -h | tail -n 5  # app.log is 17G: > /var/log/app/app.log               # free space immediately# then fix the cause: add logrotate for the app
OUTPUTterminalutf-8 · 5L

First the quick fix (empty the log), then the lasting fix (log rotation, covered in a later lesson).

Common mistakes

  • Running mkfs on the wrong disk, which erases its data. Check three times with lsblk first.
  • Using /dev/sdb names in fstab instead of UUIDs.
  • Deleting open log files, then wondering why the space did not come back.
  • Editing fstab and rebooting without testing with mount -a.

Practice

  1. Show all disks and find the unused one.
  2. Show free space on every filesystem in readable sizes.
  3. Find how much space /var/log uses.
  4. Write the fstab line to safely mount a disk with UUID abcd-1234 at /data as ext4.

Cheat sheet

CommandWhat it does
lsblkList disks and partitions
df -h / df -iSpace / inodes used per filesystem
du -sh dirSize of a folder
mount dev dir / umount dirAttach / detach a filesystem
blkidShow filesystem UUIDs
mount -aTest fstab
: > fileEmpty a file in place

Key takeaways

  • A disk is sliced into partitions, given a filesystem, then attached to a folder.
  • df -h shows how full each disk is. du -sh shows which folder is big.
  • If a log is huge, empty it with : > file instead of deleting it.
  • To keep a disk attached after reboot, add it to /etc/fstab with its UUID, then test with mount -a.
  • Cloud disks can be made bigger without turning the server off.