# Managing Storage and File Systems

> Chapter 12 of the Courseiva LINUX-PLUS curriculum — https://courseiva.com/learn/linux-plus/managing-storage-and-file-systems

**Official objective:** 4.1 — Manage storage devices, partitions, and file systems

## Introduction

How do you organise a hard drive so that the operating system knows where to put your files, programs, and system logs without everything spilling into a chaotic mess? That is the core problem that managing storage and file systems solves, and it matters for the CompTIA Linux+ exam because every Linux system — from a tiny Raspberry Pi to a giant data centre server — relies on these fundamental concepts to store and retrieve data reliably.

## The Kitchen Renovation Analogy

First, you decide to reorganise your kitchen because you can never find the pots and pans when you need them, which leads to a complete renovation project. In this renovation, the empty kitchen is like a brand new hard drive — it has plenty of space but nothing is organised yet. The cupboards and drawers are the partitions: you decide how big each section should be and what it will hold. One cupboard for plates (the root partition for system files), another for dry food (the home partition for user data), and a deep drawer for pots (a swap partition for memory overflow).

Once the cupboards are planned, you install shelves and drawer dividers — this is like creating a file system (such as ext4 or XFS) on each partition. The shelves give structure so items are stored efficiently and you can find them quickly. Next, you label each shelf to know exactly where the teaspoons go — that is mounting: making the partition accessible at a specific point (the mount point) so the operating system can read and write data there.

Finally, when you buy a new set of mixing bowls, you might rearrange a cupboard to fit them — that is resizing a partition. And if a shelf breaks and you lose a corner of the kitchen, you back up your recipe books to the cloud so you don't lose your signature dishes. This whole process mirrors how storage professionals manage disks, partitions, and file systems on Linux servers.

## Core explanation

When you first look at a blank hard drive, it is basically a large, empty block of potential storage — just like an empty warehouse. But the Linux operating system cannot just dump files onto this raw space; it needs structure. That structure comes in layers: first, the physical disk, then partitions, then a file system, and finally mounting those partitions to make them usable.

A hard drive (also called a disk drive or block device) is a physical spinning platter or solid-state memory chip that stores data as bits. Linux identifies these devices with names like /dev/sda (the first SCSI or SATA disk) or /dev/nvme0n1 (the first NVMe solid state drive). A partition is a logical section of that disk. Think of it as dividing the warehouse into separate rooms: one room for shipping, one for receiving, one for storage. You can have up to four primary partitions on a traditional MBR (Master Boot Record) disk, or many more on a GPT (GUID Partition Table) disk, which is the modern standard.

Once you have a partition, it is still just a raw chunk of disk space. To store files and folders, you need a file system — this is the organisational structure that dictates how data is saved, named, retrieved, and deleted. Common Linux file systems include:

- ext4 (Fourth Extended File System): the default for many Linux distributions, stable and feature-rich.
- XFS: excellent for large files and high performance, often used in media servers and data warehouses.
- Btrfs (B-tree File System): supports snapshots and advanced features like compression and subvolumes.
- swap: not a file system for storing files, but a space used by the kernel to move data out of RAM when memory runs low.

To create a file system on a partition, you use a command like mkfs.ext4 /dev/sda1. This writes the file system metadata — the index of what goes where — onto the partition. After that, the partition is ready to hold data, but the system still cannot see it until you mount it. Mounting means attaching that partition to a specific directory in the Linux directory tree. For example, if you mount /dev/sda1 at /home, then any file saved under /home is physically stored on that partition. The directory where it attaches is called the mount point.

Linux uses a single unified directory tree starting at the root directory (/). Every file and folder, no matter which disk or partition it lives on, is accessible from this tree. This is different from Windows, which assigns drive letters like C: and D:. The root filesystem (/) must always be accessible, so it is mounted at boot time. Other partitions, such as /var (for variable data like logs) or /boot (for boot loader files), can be mounted as needed.

Finally, devices like USB flash drives or external hard drives are managed similarly. They appear as block devices (e.g., /dev/sdb1) and must be mounted before use. Modern desktop environments often mount them automatically, but on a server or in an exam, you might need to mount them manually using the mount command and unmount them with umount.

All of this matters because without proper storage management, a system can run out of space in critical areas, fail to boot, or lose data. Understanding how to partition, format, mount, and verify disks is the foundation of Linux system administration.

## Real-world context

Let us walk through a typical day for a junior Linux administrator at a small e-commerce company. The website runs on a Linux server, and you are responsible for keeping the storage healthy.

This morning, the monitoring system sends an alert: the /var partition is 95% full. /var is where system logs, database files, and temporary data accumulate. If it fills completely, the database might crash, and the website could go offline. You need to investigate and fix it.

You start by logging into the server via SSH (Secure Shell) and running df -h. This command shows disk usage for all mounted file systems. You can see that /dev/sda2 (mounted at /var) is indeed nearly full. Next, you use du -sh /var/log to find that the log files are the culprit — they have grown huge because the application logs everything at the debug level.

To resolve this, you have several options:
- Clean up old logs: use logrotate or manually delete archived logs with rm, but you must be careful not to delete active logs.
- Add a new disk: if physical space is available, you could attach a new hard drive to the server, partition it, format it with ext4, and mount it at /var/log to offload logs to dedicated storage.
- Resize the existing partition: if the disk has unallocated space, you can use tools like fdisk or parted to extend the partition, then resize the file system with resize2fs (for ext4) to use the new space.

In this scenario, you decide to add a new 100GB disk. After physically installing it (or, on a virtual machine, attaching a virtual disk through the hypervisor), you must follow these steps:
1. Identify the new disk with lsblk (list block devices). It shows up as /dev/sdb.
2. Create a partition using fdisk /dev/sdb. You create a primary partition that uses the full disk.
3. Format the partition with mkfs.ext4 /dev/sdb1 to create the file system.
4. Create a mount point directory with mkdir /var/log_new.
5. Mount the new partition temporarily: mount /dev/sdb1 /var/log_new.
6. Copy the existing logs from /var/log to /var/log_new using rsync -av /var/log/ /var/log_new/.
7. Remove the old logs from /var/log to free space.
8. Update /etc/fstab (the file that controls automatic mounts at boot) with a line for /dev/sdb1 so it mounts at /var/log automatically on every reboot.
9. Remount the partition at the correct location: unmount /var/log_new and mount /dev/sdb1 /var/log.

After this, the server is back to normal. You document the changes and set up a log rotation policy to prevent future issues. This daily maintenance — checking disk usage, adding storage, mounting partitions, and troubleshooting full disks — is exactly what the exam tests.

## Exam focus

The XK0-006 exam tests your ability to manage storage at a practical level, not just theory. Expect these specific question types and traps.

First, you will see questions about identifying block devices. The exam loves to present output from lsblk, fdisk -l, or df and ask which device corresponds to which mount point. A common trap is that they use multiple disks (sda, sdb, nvme0n1) and expect you to read the output carefully. For example, they might show df -h output where /boot is mounted on /dev/sda1 and /home on /dev/sdb2, and ask which partition holds the kernel files. The correct answer is /dev/sda1 because /boot contains the kernel.

Second, you must know the commands to create and manage partitions. They often test fdisk and parted. A trap here is confusing MBR (supports 4 primary partitions) with GPT (supports up to 128 partitions). You might be asked: "Which partition table type is required for a disk larger than 2TB?" The answer is GPT, because MBR's addressing limits it to 2TB. Another trap: fdisk uses a menu interface (press 'n' for new, 'w' to write changes), but beginners sometimes forget to write changes before exiting.

Third, file system creation is a frequent topic. You need to know the mkfs command and the specific variants: mkfs.ext4, mkfs.xfs, mkswap. The exam may ask which file system to choose for a database server that needs high performance for large files. The correct answer is XFS. A trap: some beginners confuse mkfs with mount or think mkfs formats the entire disk (it only formats a partition).

Fourth, mounting and /etc/fstab are heavily tested. You must understand the fields in an fstab entry: device, mount point, file system type, options (like defaults, noatime), dump flag (usually 0), and pass (fsck order). A common trap is that they give an fstab line with an error (e.g., wrong file system type) and ask why the system fails to boot. The pass flag also matters: the root filesystem should have pass=1, other partitions pass=2, and swap pass=0.

Finally, swap space is a recurring topic. Know how to create a swap partition (mkswap) and enable it (swapon). The exam might present a system with low memory and ask how to add swap without rebooting. The answer: create a swap file (dd if=/dev/zero of=/swapfile bs=1M count=1024, then mkswap /swapfile, swapon /swapfile). A trap is thinking you need a dedicated partition for swap when a file works fine.

To memorise key definitions:
- Partition: logical division of a disk.
- File system: data structure that organises files on a partition.
- Mount: attaching a file system to a directory in the tree.
- Block device: a device (like a disk) that reads/writes data in blocks.
- inode: a data structure that stores metadata about a file (not the filename or content).

## Step by step

1. **Identify the new disk** — After physically or virtually attaching a new disk, run lsblk or fdisk -l to see all block devices. The new disk will appear as /dev/sdb or /dev/nvme1n1. This step ensures you know the exact device name to avoid overwriting the wrong disk.
2. **Create partitions on the disk** — Use fdisk /dev/sdb (or parted) to define partitions. Inside fdisk, press 'n' for a new partition, choose primary or extended, then set the size. Writing changes with 'w' saves the partition table to the disk. This is necessary because Linux cannot write directly to a raw disk without partition boundaries.
3. **Format the partition with a file system** — Run mkfs.ext4 /dev/sdb1 (or mkfs.xfs if using XFS) to write the file system onto the partition. This creates the metadata structures like inodes and superblocks that organise file storage. Without this, the partition is just blank space.
4. **Create a mount point directory** — Create an empty directory where the partition will be accessible, e.g., sudo mkdir /mnt/new_data. Every mount point must exist as a directory in the root tree before mounting. Choose a location that makes logical sense, like /data or /backups.
5. **Mount the partition temporarily** — Use sudo mount /dev/sdb1 /mnt/new_data to attach the file system to the directory. Now any file written to /mnt/new_data is physically stored on /dev/sdb1. This works until the next reboot unless added to fstab.
6. **Add an entry to /etc/fstab for automatic mounting** — Edit /etc/fstab and add a line like: /dev/sdb1 /mnt/new_data ext4 defaults 0 2. This tells the system to mount the partition automatically at boot. The fields are: device, mount point, file system type, options (defaults), dump (0 = no backup), pass (2 = filesystem check order). Incorrect entries can cause boot failures.
7. **Verify the mount** — Run mount -a to test all entries in fstab without rebooting, then use df -h to confirm the new partition shows up with the expected size and mount point. This step catches typos in fstab before they cause problems on next boot.

## Comparisons

### MBR (Master Boot Record) vs GPT (GUID Partition Table)

**MBR (Master Boot Record):**
- Supports a maximum disk size of 2TB.
- Allows up to 4 primary partitions (or 3 primary + 1 extended).
- Stores partition table in a single location, making it vulnerable to corruption.

**GPT (GUID Partition Table):**
- Supports disks up to 9.4 zettabytes (practically unlimited).
- Allows up to 128 primary partitions by default.
- Stores redundant partition table at beginning and end of disk for reliability.

### Partition vs File System

**Partition:**
- A logical division of a physical disk created with fdisk or parted.
- Defines boundaries on the disk but does not organise user data.
- Can exist without a file system (e.g., raw database partitions).

**File System:**
- A data structure (ext4, XFS, etc.) written onto a partition using mkfs.
- Organises files, directories, and metadata (inodes, superblock).
- Cannot be created on a raw disk without a partition.

### Swap Partition vs Swap File

**Swap Partition:**
- A dedicated partition on the disk for swap space.
- Requires repartitioning of the disk to resize or create.
- Can be faster because it is contiguous on the disk.

**Swap File:**
- A regular file on an existing file system used as swap.
- Easy to create, resize, or delete without touching partitions.
- May have slightly more overhead due to filesystem layer but is very flexible.

### df (disk free) vs du (disk usage)

**df (disk free):**
- Shows free and used space for entire mounted file systems.
- Useful for a quick overview of disk capacity.
- Reports from the file system's superblock.

**du (disk usage):**
- Shows disk usage for specific directories and files.
- Useful for finding what is consuming space within a partition.
- Scans the directory tree, so it can be slower but more precise.

### fdisk vs parted

**fdisk:**
- Older, menu-driven tool for creating and deleting MBR partitions.
- Limited support for GPT partitions (though modern fdisk does support GPT).
- Best for simple MBR disk management.

**parted:**
- Modern tool that fully supports both MBR and GPT partition tables.
- Can resize partitions and uses command-line or interactive mode.
- Best for GPT disks and when you need to manage partition sizes precisely.

## Diagram

_Flowchart showing how a physical disk is partitioned, each partition is formatted with a file system, and then mounted to different directories in the Linux tree._

```mermaid
flowchart TD
    A[Physical Disk /dev/sda] --> B[Partition /dev/sda1]
    A --> C[Partition /dev/sda2]
    B --> D[Create File System
mkfs.ext4]
    C --> E[Create File System
mkfs.xfs]
    D --> F[Mount at /home]
    E --> G[Mount at /var]
    F --> H[Files in /home
stored here]
    G --> I[Files in /var
stored here]
    style A fill:#f9f,stroke:#333,stroke-width:2px
    style F fill:#ccf
    style G fill:#ccf
```

## Common misconceptions

- **Misconception:** You can just drag and drop files onto a new hard drive without formatting it first. **Reality:** A hard drive must be partitioned and then formatted with a file system (like ext4) before the operating system can store files on it. (In many consumer operating systems like Windows and macOS, formatting happens automatically or is hidden, so beginners assume disks are ready to use out of the box.)
- **Misconception:** The /etc/fstab file is only used for swap partitions. **Reality:** /etc/fstab controls the automatic mounting of all file systems — including root (/), /home, /var, and swap — at boot time. (Swap entries are simple and often the first example given, leading beginners to think fstab is only for swap.)
- **Misconception:** Partitions and file systems are the same thing. **Reality:** A partition is a logical section of a disk; a file system is the structure placed on that partition to organise files. You can have a partition without a file system (like a raw partition for a database). (The terms are used loosely in tutorials, and both involve 'dividing' space, so beginners conflate them.)
- **Misconception:** You cannot add more space to a partition without rebooting the system. **Reality:** Many modern file systems (ext4, XFS) support online resizing, meaning you can grow a partition while the system is running, though shrinking often requires unmounting. (Older operating systems required reboots, and the perception of storage as 'physical and fixed' persists.)
- **Misconception:** All Linux file systems are identical and interchangeable. **Reality:** Different file systems have different strengths: ext4 is robust and default, XFS is optimised for large files, Btrfs supports snapshots, and swap is not for file storage at all. (Beginners often hear 'Linux uses ext4' and assume it is the only option, ignoring specialised use cases.)

## Key takeaways

- Every physical storage device in Linux is represented as a block device file under /dev/, such as /dev/sda or /dev/nvme0n1.
- A partition is a logical division of a disk, and you can have up to four primary partitions on an MBR disk or 128 on a GPT disk.
- You must create a file system (e.g., ext4, XFS) on a partition using mkfs before data can be stored there.
- Mounting attaches a file system to a directory in the root tree, making it accessible through paths like /home or /var.
- The /etc/fstab file defines which file systems mount automatically at boot, with fields for device, mount point, type, options, dump, and pass.
- The df command shows disk space usage for mounted file systems, while du shows disk usage for directories and files.
- Swap space, either a partition or a file, acts as overflow memory when RAM is full, and is managed with mkswap and swapon.
- Logical Volume Manager (LVM) allows you to pool multiple disks into volume groups and create flexible logical volumes that can be resized without repartitioning.

## FAQ

**What is the difference between /dev/sda and /dev/sda1?**

/dev/sda refers to the entire first SCSI/SATA disk, while /dev/sda1 refers to the first partition on that disk. Always format and mount the partition, not the whole disk.

**How do I see how much free disk space I have in Linux?**

Use the df command with the -h flag (human-readable), like df -h. It shows each mounted file system, its total size, used space, free space, and mount point.

**Can I use a USB drive without partitioning it?**

Technically yes, but it is not recommended. Most Linux tools expect a partition table. It is safer to create at least one partition (e.g., /dev/sdb1) and format that.

**What does it mean to 'mount' a file system?**

Mounting makes the storage on a partition accessible at a specific directory (mount point) in the Linux directory tree. Without mounting, the partition is invisible to users and applications.

**How do I add swap space without repartitioning?**

Create a swap file using dd (e.g., dd if=/dev/zero of=/swapfile bs=1M count=1024), set it up with mkswap /swapfile, and enable it with swapon /swapfile. Add it to fstab for persistence.

**What is the difference between ext4 and XFS?**

ext4 is the default file system for many Linux distributions, good for general use with files up to 16TB. XFS is optimised for large files and high concurrency, often used in data centres and media servers.

**Why can't I write to a partition after mounting it?**

Check the mount options: if the partition was mounted read-only (e.g., due to filesystem errors or the 'ro' option), you cannot write. Use mount | grep <mountpoint> to see the current options and remount with rw (read-write).

## Check your understanding

1. **A system administrator issues the command 'mount /dev/sdb1 /mnt/data'. What is /mnt/data called?**

   Answer: /mnt/data is the mount point — the directory where the file system on /dev/sdb1 is attached to the root directory tree.

2. **Which partition table type is required for a disk that is 3TB in size?**

   Answer: GPT (GUID Partition Table) is required, because MBR cannot address disks larger than 2TB.

3. **After creating a new partition on /dev/sdc, what command would you use to create an ext4 file system on it?**

   Answer: The command is 'mkfs.ext4 /dev/sdc1' (assuming the partition is /dev/sdc1).

4. **A server fails to boot after an edit to /etc/fstab. What is the most likely cause?**

   Answer: The most likely cause is a typo in one of the fstab fields (e.g., wrong device name, wrong file system type, or incorrect mount point), preventing the system from mounting a necessary partition.

5. **What is the purpose of the 'pass' field in the /etc/fstab file?**

   Answer: The 'pass' field determines the order in which file systems are checked by fsck at boot: the root filesystem typically has pass=1, other partitions have pass=2, and swap or removable devices have pass=0 (no check).

---

Interactive version with quiz and diagrams: https://courseiva.com/learn/linux-plus/managing-storage-and-file-systems
