102.1 Design hard disk layout¶
Basics¶
Unlike Windows which uses drive letters (A:, C:, D:, etc.), Linux puts everything in one big tree starting with / (called root). Any partition, disk, CD, USB, or network drive gets placed somewhere inside this tree.
External devices (USB, CD, etc.) are typically mounted at /media/ or /mnt/.
Before a disk can be used, it needs to be partitioned. A partition is a logical subset of the physical disk. Every disk needs at least one partition, and information about partitions is stored in a partition table. This table includes information about the first and last sectors of the partition, its type, and further details.
Inside each partition there is a filesystem. The filesystem describes the way information is actually stored on the disk - how directories are organized, the relationships between them, and where the data for each file lives.
Unix directories¶
The Filesystem Hierarchy Standard (FHS) defines where programs, configs, logs, and more are stored. The latest revision is from 2015.
| Directory | Description |
|---|---|
bin |
Essential command binaries |
boot |
Static files of the boot loader |
dev |
Device files |
etc |
Host-specific system configuration |
home |
Home directory of the users |
lib |
Essential shared libraries and kernel modules |
media |
Mount point for removable media |
mnt |
Mount point for mounting a filesystem temporarily |
opt |
Add-on application software packages |
root |
Home directory of the root user |
sbin |
Essential system binaries |
srv |
Data for services provided by this system |
tmp |
Temporary files, sometimes purged on each boot |
usr |
Secondary hierarchy |
var |
Variable data (logs, etc.) |
Partitions¶
Devices are defined at /dev/ with different naming conventions depending on the disk type:
- PATA (obsolete):
/dev/hdc - SATA & SCSI disks:
/dev/sda - SD/eMMC & bare NAND/NOR devices:
/dev/mmcblk0, partitions as/dev/mmcblk0p0 - NVMe drives:
/dev/nvme0, partitions as/dev/nvme0n1
Partitions are self-contained sections on a disk. The OS sees them as standalone disks:
/dev/sda1- first partition of the first SCSI disk/dev/hdb3- third partition on the second disk
BIOS systems use MBR and can have up to 4 partitions per disk. Instead of creating 4 Primary partitions, you can create an Extended partition and define more Logical partitions inside it. An Extended partition is just an empty container for creating Logical partitions.
Partition numbering on MBR:
/dev/sda3- the 3rd primary partition on the first disk/dev/sdb5- the first logical partition on the second disk (logical partitions always start at 5)/dev/sda7- the 3rd logical partition of the first physical disk
UEFI systems use GUID Partition Table (GPT), which supports up to 128 partitions per device - no need for extended or logical partitions.
Linux systems can mount these partitions on different paths. For example, you can have a separate disk with one huge partition for /home and another for /var/logs/.
# fdisk /dev/sda
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 43094015 43091968 20.6G 83 Linux
/dev/sda2 43094016 92078390 48984375 23.4G 83 Linux
/dev/sda3 92080126 625141759 533061634 254.2G 5 Extended
/dev/sda5 92080128 107702271 15622144 7.5G 82 Linux swap / Solaris
/dev/sda6 107704320 625141759 517437440 246.8G 83 Linux
Mount points¶
Before a filesystem can be accessed on Linux, it needs to be mounted - attached to a specific point in the directory tree called a mount point. When mounted, the contents of the filesystem become available under that path. For example, if a partition contains /john, /jack, and /carol, mounting it under /home makes those accessible as /home/john, /home/jack, and /home/carol.
The mount point directory must exist before mounting. If the directory already contains files, those files become unavailable (hidden) until the filesystem is unmounted.
Traditionally, /mnt was the directory for mounting all external devices. This has been superseded by /media, which is now the default mount point for user-removable media (external disks, USB flash drives, memory card readers, optical disks). On most modern distros, removable devices are automatically mounted under /media/USER/LABEL when connected.
When manually mounting a filesystem, good practice is to mount it under /mnt.
Keeping things separated¶
Some directories should be kept on separate partitions:
/booton its own partition ensures the system can still boot if the root filesystem crashes/homeon a separate partition lets you reinstall the system without touching user data/varon a separate partition protects the root filesystem from a misbehaved process filling up all the space with log data. If/varis under/and fills completely, it may trigger a kernel panic and filesystem corruption
You may also want to keep / on a fast SSD and larger directories like /home and /var on slower, bigger hard disks.
Commands¶
parted¶
$ sudo parted /dev/sda p
Model: ATA ST320LT000-9VL14 (scsi)
Disk /dev/sda: 320GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 1049kB 22.1GB 22.1GB primary ext4 boot
2 22.1GB 47.1GB 25.1GB primary ext4
3 47.1GB 320GB 273GB extended
5 47.1GB 55.1GB 7999MB logical linux-swap(v1)
6 55.1GB 320GB 265GB logical
fdisk¶
# sudo fdisk /dev/sda
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 43094015 43091968 20.6G 83 Linux
/dev/sda2 43094016 92078390 48984375 23.4G 83 Linux
/dev/sda3 92080126 625141759 533061634 254.2G 5 Extended
/dev/sda5 92080128 107702271 15622144 7.5G 82 Linux swap / Solaris
/dev/sda6 107704320 625141759 517437440 246.8G 83 Linux
gparted¶
A graphical tool for managing disks and partitions.
LVM¶
Logical Volume Manager lets you resize partitions and combine space from different disks. It adds a flexible layer between physical disks and the filesystems the OS actually uses.
Partitions alone cannot span multiple disks - LVM solves this by abstracting the limitations of physical devices into "pools" of disk space that can be combined or distributed more flexibly.
The main concepts:
- Physical Volume (PV): A whole drive or a partition. It is better to define partitions and not use whole disks (unpartitioned).
- Volume Group (VG): A collection of one or more PVs. The OS sees the VG as one big disk. PVs in one VG can have different sizes or even be on different physical disks.
- Logical Volume (LV): The OS sees LVs as partitions. You can format an LV with a filesystem and use it normally.
Each Volume Group is subdivided into fixed-sized pieces called extents. Extents on a PV are called Physical Extents (PE), while those on an LV are Logical Extents (LE). The default extent size is 4 MB.
The size of a Logical Volume is defined by the size of the physical extents multiplied by the number of extents on the volume. To grow an LV, the administrator just adds more extents from the pool available in the Volume Group. Extents can also be removed to shrink the LV.
After an LV is created, it appears as a normal block device at /dev/VGNAME/LVNAME. It can be formatted with standard utilities (like mkfs.ext4) and mounted the usual way - manually with mount or automatically via /etc/fstab.
Design hard disk layout¶
Disk layout depends on usage. Two partitions that almost always exist are swap and /boot.
swap¶
Swap works like extended memory. The kernel pages memory to this partition (or file) when RAM is full. It is enough to format one partition with the swap filesystem and define it in /etc/fstab.
The swap partition cannot be mounted like a normal filesystem - you cannot browse its contents like a regular directory.
A system can have multiple swap partitions (though uncommon), and Linux also supports swap files instead of partitions, which can be useful to quickly increase swap space when needed.
There is no strict formula for swap size. The PDF provides a Red Hat-sourced recommendation:
| RAM | Recommended swap | Swap with hibernation |
|---|---|---|
| < 2 GB | 2x RAM | 3x RAM |
| 2-8 GB | Equal to RAM | 2x RAM |
| 8-64 GB | At least 4 GB | 1.5x RAM |
| > 64 GB | At least 4 GB | Not recommended |
/boot¶
The boot partition contains files used by the bootloader (usually GRUB2, or GRUB Legacy on older systems) to load the operating system. Its files are stored in /boot/grub.
Technically a separate boot partition is not required, since GRUB can mount the root partition and load the files from a /boot directory on it. But a separate partition may be desired for safety (ensuring boot even if the root filesystem crashes), or if the root partition uses a filesystem, encryption, or compression method the bootloader cannot understand.
The boot partition is usually the first partition on the disk. This is because the original IBM PC BIOS addressed disks using CHS (cylinders, heads, sectors), with a maximum disk size of 528 MB. Anything past that mark would not be accessible on legacy systems. So for maximum compatibility, the boot partition is located at the start of the disk and ends before cylinder 1024.
A good size for /boot is around 300 MB. This partition must be accessible by BIOS/UEFI during boot (no network drives).
EFI System Partition (ESP)¶
On UEFI systems, there is a /boot/efi mount point called the EFI System Partition (ESP). This contains the bootloader and kernel images for the installed operating systems.
The ESP is formatted with a FAT-based filesystem. On a GPT disk it has a globally unique identifier of C12A7328-F81F-11D2-BA4B-00A0C93EC93B. On an MBR disk the partition ID is 0xEF.
On machines running Windows, the ESP is usually the first partition on the disk, although this is not required.
Case one: Desktop computer¶
One swap, one /boot, and allocate all remaining space to / (root).
Case two: Network workstation¶
/boot and / should be local (on a physical disk connected to the machine). /home can be mounted from a network drive (NFS, SMB, SSH, etc.), letting users sit at any workstation, log in, and have their own home directory available. Swap can be local or network.
Case three: Server¶
/boot is still local. Based on usage, /home can be local or network. /var is often separated because logs and other frequently-updated files live there, and it may be placed on more advanced storage (like RAID) to prevent data loss. Some admins also separate /usr and write-protect it (read-only filesystem), or mount /usr from the network so updating one file on the network storage updates it for all servers.
Bonus: zram¶
Three different methods to add swap to a Linux system, across three different distros:
- Debian 11: Uses a swap partition
- Ubuntu 22.04: Uses a swap file
- Fedora 36: Uses
zram
zram is a virtual disk that lives in your RAM. It can be used as swap space or be mounted anywhere (a common example is /tmp). Instead of paging to a physical disk, zram compresses memory pages and keeps them in RAM, which is much faster than writing to disk.
Summary¶
I have a Linux system where everything sits in one big tree starting at /. Unlike Windows drive letters, any disk, partition, USB, or network share just gets attached (mounted) somewhere inside this tree. Before a disk is usable, it needs at least one partition, and inside that partition lives a filesystem that organizes the actual data.
Some directories are worth keeping on separate partitions. /boot gets its own partition for safety (so the system can still boot even if the root filesystem crashes), and it should be accessible by BIOS/UEFI (no network drives). /home on its own partition protects user data during a reinstall. /var on its own partition prevents a runaway log file from filling the root filesystem and crashing the system. On UEFI systems, the ESP is mounted at /boot/efi and formatted with a FAT-based filesystem.
When traditional partitioning isn't flexible enough, LVM adds a layer between physical disks and filesystems. Physical Volumes get grouped into Volume Groups, which get carved into Logical Volumes. The OS sees each LV as a normal partition at /dev/VGNAME/LVNAME, but the admin can grow or shrink them by adding or removing extents without migrating data to a bigger disk.
Swap space gives the kernel somewhere to page memory when RAM runs out. It can be a dedicated partition, a swap file, or even compressed RAM via zram. The right size depends on the system's use case, with the general guideline ranging from equal-to-RAM up to 2x RAM depending on the amount installed and whether hibernation is needed.