# Design hard disk layout

> Which partitions a Linux system needs, where they get mounted, how /boot and the ESP fit the firmware, and the basics of LVM.

Source: https://aminbytes.dev/bytes/installation-and-packages/hard-disk-layout/


Before you install Linux, you decide how the disk gets split up. That choice sticks around for years. Put everything in one big partition and a runaway log file can fill the disk until nothing can write, not even the login process. Split it badly and you end up with a full `/` next to a half-empty `/home`.

A good layout keeps important parts apart, matches what the machine is for, and fits what the firmware needs to boot. On the exam, this objective is about the *design*: which pieces exist and why. The tools that actually create partitions (`fdisk`, `gdisk`, `parted`) come in topic 104.

## Partitions and mount points

A **partition** is a slice of a disk that the system treats as its own device, like `/dev/sda1` or `/dev/nvme0n1p2`. You put a filesystem on it, then attach it to the directory tree at a **mount point**: an ordinary, usually empty directory such as `/home`.

Linux has a single tree that starts at `/`. There are no drive letters. Each partition just shows up somewhere in that tree. Here's a typical RHEL 9 virtual machine:

```bash
lsblk
```

```text
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda             8:0    0   40G  0 disk
├─sda1          8:1    0  600M  0 part /boot/efi
├─sda2          8:2    0    1G  0 part /boot
└─sda3          8:3    0 38.4G  0 part
  ├─rhel-root 253:0    0   35G  0 lvm  /
  └─rhel-swap 253:1    0  3.4G  0 lvm  [SWAP]
```

Four things to notice: an ESP at `/boot/efi`, a separate `/boot`, an LVM area holding `/`, and swap. You'll meet all of them below. To see what is mounted where right now, use `findmnt` or `df -h`. The list that gets mounted at every boot lives in `/etc/fstab`.

## The usual filesystems

| Mount point | What lives there | Why give it its own partition |
| --- | --- | --- |
| `/` (root) | Everything not mounted elsewhere: `/etc`, `/usr`, `/bin`, `/lib`, `/dev` | Required. The only one you can't skip. |
| `/boot` | Kernels (`vmlinuz-*`), initramfs images, GRUB files | The boot loader must be able to read it, before LVM, RAID or encryption are running |
| `/boot/efi` | The EFI System Partition (UEFI machines only) | The firmware itself reads it, and it must be FAT |
| `/home` | Users' files | Reinstall the OS without touching user data; stop users from filling `/` |
| `/var` | Logs, mail and print spools, caches, databases (`/var/lib`) | It grows and changes all day. If it fills up, `/` still has room. |
| `/tmp` | Temporary files | Limit its size, mount it with `noexec` or `nosuid` |
| swap | Not a filesystem: memory overflow | Never mounted in the tree, only switched on |

Other common candidates are `/srv` (data a server publishes), `/opt` (add-on software) and `/var/log` on its own.

> [!IMPORTANT]
> Some directories **must** stay on the root filesystem, because the system needs them before it can mount anything else: `/etc` (it holds `fstab`), `/bin`, `/sbin`, `/lib` and `/dev`. On modern systems `/bin`, `/sbin` and `/lib` are links into `/usr`, so `/usr` is kept on `/` too, unless the initramfs mounts it early.

## /boot and what the firmware needs

The boot loader runs before Linux, so it only understands simple things. How you lay out `/boot` depends on the firmware.

**BIOS with an MBR disk.** The BIOS runs the code in the first 512 bytes of the disk, the Master Boot Record (MBR). GRUB puts a tiny stage there and the rest in the gap before the first partition. MBR disks allow 4 primary partitions, or 3 primary plus 1 *extended* partition that holds *logical* ones (`/dev/sda5` and up), and they top out at 2 TiB.

**BIOS with a GPT disk.** GPT has no gap after the MBR, so GRUB needs a small **BIOS boot partition** (about 1 MiB, no filesystem, type `EF02` in `gdisk`).

**UEFI.** The firmware reads boot loaders as files from the **EFI System Partition (ESP)**: a FAT32 partition (type `EF00` in `gdisk`) usually mounted at `/boot/efi`. Debian and RHEL both mount it there. 300 MiB to 1 GiB is a common size; the RHEL installer uses 600 MiB.

```bash
sudo ls /boot/efi/EFI/
```

```text
BOOT  debian
```

Each installed OS gets its own folder (`debian`, `ubuntu`, `redhat`, `fedora`). `BOOT` holds the fallback loader the firmware tries when nothing else is set up.

**A separate `/boot`** (around 1 GiB) is needed when GRUB can't read the root filesystem directly: root on LVM in older setups, on software RAID, encrypted with LUKS, or on a filesystem GRUB doesn't support. It also gives the kernels a simple, safe home. (Side note: Very old BIOSes could only read the first 1024 cylinders (about 504 MiB, later 8 GiB) of a disk. That's why old guides insist on putting `/boot` at the very start of the disk. Modern firmware doesn't care.)

## Swap space

**Swap** is disk space the kernel uses when RAM runs short. It moves memory pages that haven't been used for a while out to disk, freeing RAM for active work. It's also where a laptop saves its memory when it hibernates.

You can use a swap **partition** or a swap **file**. Check what you have:

```bash
swapon --show
free -h
```

```text
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition 3.4G   0B   -2

               total        used        free      shared  buff/cache   available
Mem:           3.6Gi       812Mi       2.4Gi        12Mi       661Mi       2.8Gi
Swap:          3.4Gi          0B       3.4Gi
```

How big? The old rule was "twice the RAM". Today a common guide, similar to Red Hat's advice, looks like this:

| RAM | Swap (no hibernation) |
| --- | --- |
| Up to 2 GB | 2 × RAM |
| 2 to 8 GB | Same as RAM |
| 8 to 64 GB | At least 4 GB |

If the machine hibernates, swap must be at least as large as RAM.

A swap partition is prepared with `mkswap` and switched on with `swapon` (its `/etc/fstab` line has `swap` as the type and `none` as the mount point). A swap file is the flexible option on a running system:

```bash
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
```

## Tailor the layout to the job

There's no single right answer. Think about what will grow and what must never fill up.

- **Desktop or laptop:** ESP, `/` and `/home`, plus swap (big enough for hibernation if you use it). Keeping `/home` separate means you can reinstall without losing your files.
- **General server:** separate `/var` (or at least `/var/log`) so logs can't fill `/`. Maybe `/tmp` with `noexec`, `/srv` for served data, and `/home` if users log in.
- **Database or mail server:** put the data directory (`/var/lib/mysql`, `/var/lib/pgsql`, `/var/spool/mail`) on its own, ideally fast, disk.
- **Cloud VM:** often just one root filesystem that grows with the disk. Data goes on separate block volumes you can attach and resize.
- **Many disks:** spread the busy filesystems across them, and use RAID or LVM for space and safety.

## LVM basics

Fixed partitions are hard to resize. The **Logical Volume Manager (LVM)** adds a layer in between, so you can grow filesystems, add disks and take snapshots while the system runs.

It has three layers:

1. **Physical volume (PV):** a disk or partition prepared for LVM with `pvcreate`.
2. **Volume group (VG):** a pool of storage made from one or more PVs with `vgcreate`. Space is handed out in small chunks called *physical extents* (4 MiB by default).
3. **Logical volume (LV):** a slice of the pool with `lvcreate`. You put a filesystem on it and mount it like a partition.

```text
 /dev/sdb   /dev/sdc        ← physical volumes (PV)
     \         /
      [ data ]              ← volume group (VG)
      /      \
   home      backup         ← logical volumes (LV)
```

Try it on a spare disk in a VM (here `/dev/sdb`, check yours with `lsblk` first: this erases it):

```bash
sudo pvcreate /dev/sdb
sudo vgcreate data /dev/sdb
sudo lvcreate -n home -L 10G data
sudo mkfs.ext4 /dev/data/home
```

```text
  Physical volume "/dev/sdb" successfully created.
  Volume group "data" successfully created
  Logical volume "home" created.
```

The new volume appears as both `/dev/data/home` and `/dev/mapper/data-home`. Look at each layer with the short report commands (the long versions are `pvdisplay`, `vgdisplay` and `lvdisplay`):

```bash
sudo pvs; sudo vgs; sudo lvs
```

```text
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sdb   data lvm2 a--  <20.00g <10.00g
  VG   #PV #LV #SN Attr   VSize   VFree
  data   1   1   0 wz--n- <20.00g <10.00g
  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home data -wi-a----- 10.00g
```

Running out of room? Grow the volume and its filesystem in one go with `-r` (resize filesystem):

```bash
sudo lvextend -r -L +5G /dev/data/home
```

When the group itself is full, add another disk with `pvcreate /dev/sdc` and `vgextend data /dev/sdc`.

> [!TIP]
> Leave some free space in the volume group instead of handing it all out at install time. Growing a volume later takes seconds. Shrinking one is slow and risky, and XFS can't shrink at all.

## Exam checklist

- Linux has one directory tree. Partitions are attached to it at **mount points**, listed in `/etc/fstab`.
- Know what lives in `/`, `/boot`, `/home` and `/var`, and why you'd separate each one.
- `/etc`, `/bin`, `/sbin`, `/lib` and `/dev` must be on the root filesystem.
- UEFI needs an **ESP**: FAT32, usually mounted at `/boot/efi`, with boot loaders in `EFI/<name>/`.
- BIOS reads the **MBR** (first 512 bytes). GPT on BIOS with GRUB needs a small BIOS boot partition.
- A separate `/boot` is used when the boot loader can't read root (LVM, RAID, encryption).
- **Swap** is a partition or a file, made with `mkswap`, enabled with `swapon`, checked with `swapon --show` and `free`.
- LVM layers: **PV** (`pvcreate`) → **VG** (`vgcreate`) → **LV** (`lvcreate`), grown with `lvextend` and `vgextend`.
- Plan by use: separate `/var` on servers, `/home` on desktops, data on its own disk for databases.


## Byte check

1. A new server boots with UEFI firmware. Which partition does it **need** for the boot loader?
   - **An EFI System Partition (ESP) formatted as FAT32** (correct)
   - A 1 MiB BIOS boot partition with no filesystem
   - A swap partition at the start of the disk
   UEFI firmware reads boot loaders as `.efi` files from a FAT-formatted ESP, usually mounted at `/boot/efi`. The BIOS boot partition is only for GRUB on BIOS machines with a GPT disk.

2. Which directory must **always** live on the root filesystem and can't be a separate partition?
   - `/var`
   - **`/etc`** (correct)
   - `/home`
   `/etc` holds `fstab` and the rest of the configuration needed to mount everything else, so it has to be there when `/` is mounted.

3. In LVM, what do you build a volume group from?
   - Logical volumes
   - **Physical volumes** (correct)
   - Mount points
   Disks or partitions become physical volumes (`pvcreate`), physical volumes are pooled into a volume group (`vgcreate`), and logical volumes are cut from the group (`lvcreate`).


