Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Part 2 · System administration · Lesson 04 · 60 minutes

Add a disk and mount it

After this lesson you can
  • Read the block devices of a system and the file systems on them
  • Partition a disk image, make a file system on it and read its facts
  • Mount a file system now and at every boot, and add swap
Lab files of this lesson (10)

The lab folder as it stands after this lesson. Unzip it in ~/krlab.

More

The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.

Scenario

The database server krbox is out of room, and the data disk cannot grow. The hosting team adds a second virtual disk, /dev/sdb, of 20 GB. Your lead asks you to make it usable: a partition, a file system, a mount point /data that returns after every reboot, and a swap file. A mistake on the wrong device destroys data, so you first practise on a disk that is only a file, and you read the real commands before you use them.

The test machine has one disk, so the steps use a loop device (step 4), a disk that is a file. For /dev/sdb the commands are the same with another device name.

Recall

Which two commands show first which file system is full, and then which folder holds the data?

df -h shows the file system, and du -h --max-depth=1 FOLDER | sort -rh shows the folders. df -i checks the inodes.

What does the word "(deleted)" after a link in /proc/PID/fd mean?

The process holds a file that no longer has a name. Its blocks stay in use until the process closes the file.

The idea

A disk is a block device: a long row of equal blocks that the kernel reads and writes. Four layers turn it into folders and files.

From a disk to a folder
Disk /dev/sdbthe whole devicePartition /dev/sdb1one slice of the diskFile system ext4the structure of filesMount point /datawhere it shows in the treeDisk /dev/sdbthe whole devicePartition /dev/sdb1one slice of the diskFile system ext4the structure of filesMount point /datawhere it shows in the tree

A partition is a slice of the disk. A partition table at the start of the disk records the slices. The modern table is GPT; the old one is MBR. A file system (ext4, xfs, btrfs) is the structure written into a partition so that it can hold files. Mounting attaches a file system to a folder, the mount point: after mount /dev/sdb1 /data, the folder /data shows the files of that partition.

Names: /dev/sda, /dev/vda and /dev/nvme0n1 are disks, and a number or p1 after them names a partition. The name of a disk can change after a reboot, so a file that must last uses the UUID, an identifier that mkfs writes into the file system.

Swap is disk space that the kernel uses when memory is full.

Worked example

1. Read what the system has

lsblk lists block devices as a tree. -o chooses the columns. findmnt lists the mounted file systems, and -n hides the header. blkid (block ID) prints the label, the UUID and the type of each file system. The output comes from a test virtual machine, shortened.

lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS | grep -v loop
findmnt -n -o SOURCE,FSTYPE /
sudo blkid /dev/sda2
NAME    SIZE TYPE FSTYPE MOUNTPOINTS
sda      20G disk
├─sda1  100M part vfat   /boot/efi
└─sda2 19.9G part ext4   /
/dev/sda2 ext4
/dev/sda2: LABEL="rootfs" UUID="13bf4261-5483-44f0-bb88-22d5851139ad" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="d4fccac1-0638-46f2-b6ba-e26f60fa5137"

The disk sda has two partitions: sda1 holds the boot files and sda2 is the root file system. The FSTYPE column of lsblk stays empty for a device with no file system, and MOUNTPOINTS is empty for one that is not mounted. In a container you see no real disk, and sudo blkid prints nothing.

2. Partition a disk that is a file

A file can stand in for a disk. truncate -s 64M disk.img makes a file of 64 MiB. sfdisk (script fdisk) writes a partition table from a script on its input. label: gpt asks for a GPT table, and each following line is start,size,type: an empty start means "next free place", 16M is the size, and L is the type "Linux". The last line has no size, so it takes the rest. fdisk -l (list) prints the table.

mkdir -p ~/krlab/linux-admin/storage && cd ~/krlab/linux-admin/storage
truncate -s 64M disk.img
printf 'label: gpt\n,16M,L\n,,L\n' | sfdisk -q disk.img
fdisk -l disk.img | tail -n 3
Device     Start    End Sectors Size Type
disk.img1   2048  34815   32768  16M Linux filesystem
disk.img2  34816 129023   94208  46M Linux filesystem

A sector is 512 bytes. The first partition starts at sector 2048 (1 MiB), after room for the table.

On a real second disk, sudo fdisk /dev/sdb does the same by menu: g makes a GPT table, n a partition (accept the defaults), p prints the table, and w writes it and leaves. w writes at once, so check the disk name with lsblk twice. This was not run for this lesson.

3. Make a file system and read it

mkfs.ext4 (make file system) writes an ext4 file system. -q is quiet, -F allows a plain file, and -U sets a fixed UUID. -d tree copies the folder tree into the new file system, so no mount is needed. blkid and dumpe2fs -h (header only) read the facts.

A mkfs command, shortened
mkfs.ext4write an ext4 file system-L labdatathe label-N 256the number of inodes-d treecopy this folder inpart.imgthe image filemkfs.ext4write an ext4 file system-L labdatathe label-N 256the number of inodes-d treecopy this folder inpart.imgthe image file
mkdir -p tree/docs && echo hello > tree/docs/hello.txt
head -c 100000 /dev/zero | tr '\0' z > tree/big.bin
truncate -s 16M part.img
mkfs.ext4 -q -F -L labdata -U 11111111-2222-3333-4444-555555555555 -N 256 -d tree part.img
blkid part.img
dumpe2fs -h part.img 2>/dev/null | grep -E 'volume name|Block size|Inode count|Block count'
part.img: LABEL="labdata" UUID="11111111-2222-3333-4444-555555555555" BLOCK_SIZE="4096" TYPE="ext4"
Filesystem volume name:   labdata
Inode count:              256
Block count:              4096
Block size:               4096

The file system has 4096 blocks of 4096 bytes, which is 16 MiB, and 256 inodes. Every ext4 file system also has a folder lost+found, for pieces that a repair finds.

4. Mount and unmount

mount attaches a file system to a folder, and findmnt FOLDER shows that it is there. umount FOLDER detaches it, and fails with "target is busy" while a program uses a file in it. Practise first on a tmpfs, a file system in memory that needs no disk. -o size=4m limits it to 4 MiB.

sudo mkdir -p /mnt/kr
sudo mount -t tmpfs -o size=4m tmpfs /mnt/kr
findmnt -n -o FSTYPE /mnt/kr
df -h /mnt/kr
echo hi | sudo tee /mnt/kr/a.txt
sudo umount /mnt/kr
ls /mnt/kr
tmpfs
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           4.0M     0  4.0M   0% /mnt/kr
hi

The last ls prints nothing: the file lived in memory and went away with the mount.

A file system image mounts through a loop device: a device name that the kernel makes for a file, so that the file looks like a disk. losetup (loop set-up) makes one: --find takes a free one and --show prints its name. losetup -d (detach) frees it. The free name depends on your machine (packages can already use /dev/loop0 and more), so the commands keep the name in the variable L and use "$L". With the part.img of step 3, write a file, unmount, and mount again by the short way: mount -o loop makes the loop device itself, and umount frees it.

cd ~/krlab/linux-admin/storage
L=$(sudo losetup --find --show part.img)
echo "$L"
sudo mount "$L" /mnt/kr
ls /mnt/kr
echo hi | sudo tee /mnt/kr/new.txt
sudo umount /mnt/kr
sudo losetup -d "$L"
sudo mount -o loop part.img /mnt/kr
ls /mnt/kr
findmnt -n -o SOURCE,FSTYPE /mnt/kr
sudo umount /mnt/kr
/dev/loop0
big.bin
docs
lost+found
hi
big.bin
docs
lost+found
new.txt
/dev/loop0 ext4

The output shows /dev/loop0; your number can differ. new.txt stays after the second mount: unlike the tmpfs, the file lives in the image. A real second disk works the same way, with the partition name in place of the loop device. A wrong device name erases the data of that disk. On a real disk, run lsblk first, and read the size and the mount points. On the real disk of the scenario you would run sudo mkfs.ext4 -L data /dev/sdb1 and sudo mount /dev/sdb1 /data. These two lines were not run for this lesson, because the test machine has one disk.

5. Make the mount permanent

At boot the system reads /etc/fstab (file system table). Each line has six fields.

One line of /etc/fstab
UUID=11111111-...what to mount (a UUID is best)/datawhere: the mount pointext4the file system typedefaults,noexecthe options0dump: use 02check order: 1 root, 2 othersUUID=11111111-...what to mount (a UUID is best)/datawhere: the mount pointext4the file system typedefaults,noexecthe options0dump: use 02check order: 1 root, 2 others

nofail lets the system boot when the disk is missing, and noexec forbids running programs from the file system.

A mistake in this file can stop the boot. Test a line before you trust it. Make the mount point, write the line in a sample file, and let findmnt verify it.

sudo mkdir -p /data
echo "UUID=11111111-2222-3333-4444-555555555555 /data ext4 defaults,noexec 0 2" > fstab.sample
findmnt --fstab --tab-file fstab.sample -n -o TARGET,FSTYPE,OPTIONS
findmnt --verify --tab-file fstab.sample
/data  ext4   defaults,noexec
/data
   [E] unreachable on boot required source: UUID=11111111-2222-3333-4444-555555555555

0 parse errors, 1 error, 0 warnings

The sample parses, but the check reports an error: no disk with that UUID is attached. That is the right result, and it is why you verify before a reboot. (Without the folder /data, it also reports the missing target.) On the real system, take the UUID from blkid, add the line to /etc/fstab, run sudo mount -a (it mounts everything in the file and shows a wrong line at once), and only then reboot.

6. Add swap

mkswap marks a file or partition as swap. A swap file must belong to root and must not be readable by others, so chmod 600 first (swapon warns about any other owner or mode). fallocate -l 64M reserves a file of 64 MiB quickly. swapon switches the swap on, swapon --show lists the swap in use, and swapoff switches it off. If /swapfile exists on your system, choose another name.

sudo fallocate -l 64M /swapfile && sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
swapon --show
sudo swapoff /swapfile
sudo rm /swapfile
Setting up swapspace version 1, size = 64 MiB (67104768 bytes)
no label, UUID=0d6c09ef-c15c-4d26-8fc7-aa4552f42ca1
NAME      TYPE SIZE USED PRIO
/swapfile file  64M   0B   -2

mkswap alone does not use the file: swapon does. Then swapon --show lists it with the TYPE file. To keep a swap file after a reboot, skip the last two lines and add the line /swapfile none swap sw 0 0 to /etc/fstab. A default Ubuntu Server can already use /swap.img, made by the installer: if swapon --show lists it too, the numbers of your own file add to it.

Your turn

Make the folder ~/krlab/linux-admin/storage-task, save this sample in it as fstab.task, and make the seed folder with the commands after it. They are the task data and not the task.

storage-task/fstab.task

UUID=aaaaaaaa-0000-4000-8000-000000000001 /       ext4 errors=remount-ro 0 1
UUID=bbbbbbbb-0000-4000-8000-000000000002 /data   xfs  defaults,noexec   0 2
UUID=cccccccc-0000-4000-8000-000000000003 /backup ext4 defaults,nofail   0 2
/swapfile                                 none    swap sw                0 0
mkdir -p ~/krlab/linux-admin/storage-task && cd ~/krlab/linux-admin/storage-task
mkdir -p seed/sub && echo alpha > seed/a.txt && echo beta > seed/sub/b.txt

Do this with image files, a RAM file system and a swap file:

  1. Make a disk image lab.img of 32 MiB with a GPT table and two partitions. The first is 8 MiB, the second takes the rest.
  2. Make a file system image data.img of 8 MiB. It is ext4, has the label kr-data and 64 inodes, and holds the folder seed.
  3. Read fstab.task with the tools of the worked example.
  4. Mount a tmpfs of 4 MiB on /mnt/kr, read its type with findmnt, and unmount it again.
  5. Make a swap file /swapfile-task of 16 MiB, switch it on, read swapon --show, then switch it off and delete it. A container cannot do this: read step 6 instead.

Then answer from your own results.

Last, write the swap line for a file /swapfile as it belongs in fstab, and check that fstab.task already holds it.

Check yourself

Why does an fstab line use a UUID and not /dev/sdb1?

Device names can change when disks are added, removed or found in another order. The UUID is written into the file system and stays the same.

You ran mount on /data, and the files that were in /data before have gone. Did you lose them?

No. The mount hides them. After umount /data they are back. Mount on an empty folder to avoid the confusion.

umount says "target is busy". What do you do?

A program has a file open there, or a shell sits in the folder. Leave the folder with cd, and find the program with ps and /proc/PID/fd (lesson 5). Stop it first, and do not force the unmount.

Why is "mkfs" the most dangerous command of this lesson?

It overwrites the file system on the device you name, and the old files are lost. A wrong letter (sda for sdb) erases the system disk. Read lsblk first, and check the name a second time before you press Enter.

Field notes

  • Test an fstab change with findmnt --verify and sudo mount -a before you reboot. Use nofail for disks that may be absent, or the machine waits at boot.
  • Mount data file systems with nodev,nosuid,noexec where the data needs no programs. CIS Benchmarks ask for this on /tmp, /var/tmp and /dev/shm. It stops an attacker from running a dropped program from there.
  • Back up before you change a partition table, for example sudo sfdisk -d /dev/sdb > sdb.table. The dump is a text file that sfdisk /dev/sdb < sdb.table restores.
  • A file system can be encrypted below the file system with LUKS (cryptsetup). A stolen disk is then unreadable. It is the answer to the rm note of Part 1.
  • Run fsck only on an unmounted file system.
  • LVM (Logical Volume Manager) puts a flexible layer between disks and file systems. It was not run for this lesson. It has three layers: physical volumes (pvcreate /dev/sdb1) go into a volume group (vgcreate pool /dev/sdb1), and a logical volume is a slice of it (lvcreate -L 10G -n data pool). Make the file system on /dev/pool/data. To grow it, add a disk with vgextend, run lvextend -L +5G /dev/pool/data, then resize2fs /dev/pool/data (xfs: xfs_growfs).