Part 2 · System administration · Lesson 04 · 60 minutes
Add a disk and mount it
- 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
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.
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/sda2NAME 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 3Device Start End Sectors Size Type
disk.img1 2048 34815 32768 16M Linux filesystem
disk.img2 34816 129023 94208 46M Linux filesystemA 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.
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: 4096The 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/krtmpfs
Filesystem Size Used Avail Use% Mounted on
tmpfs 4.0M 0 4.0M 0% /mnt/kr
hiThe 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 ext4The 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.
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 warningsThe 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 /swapfileSetting 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 -2mkswap 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 0mkdir -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.txtDo this with image files, a RAM file system and a swap file:
- Make a disk image
lab.imgof 32 MiB with a GPT table and two partitions. The first is 8 MiB, the second takes the rest. - Make a file system image
data.imgof 8 MiB. It is ext4, has the labelkr-dataand 64 inodes, and holds the folderseed. - Read
fstab.taskwith the tools of the worked example. - Mount a
tmpfsof 4 MiB on/mnt/kr, read its type withfindmnt, and unmount it again. - Make a swap file
/swapfile-taskof 16 MiB, switch it on, readswapon --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 --verifyandsudo mount -abefore you reboot. Usenofailfor disks that may be absent, or the machine waits at boot. - Mount data file systems with
nodev,nosuid,noexecwhere the data needs no programs. CIS Benchmarks ask for this on/tmp,/var/tmpand/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 thatsfdisk /dev/sdb < sdb.tablerestores. - A file system can be encrypted below the file system with LUKS (
cryptsetup). A stolen disk is then unreadable. It is the answer to thermnote of Part 1. - Run
fsckonly 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 withvgextend, runlvextend -L +5G /dev/pool/data, thenresize2fs /dev/pool/data(xfs:xfs_growfs).