Disks & mounting
A server is running out of space, so your boss plugs in a brand-new 10 GB disk. Linux sees the disk, but you can't store anything on it yet. You have to partition it, format it and mount it. By the end of this lesson you'll have done all three, and made the disk survive a reboot.
You will learn
- How Linux names disks:
/dev/sda,/dev/sdb1, andlsblkto see them fdiskto partition andmkfsto format: xfs on Rocky, ext4 on Ubuntumount,umount, and why you get “target is busy”/etc/fstaband UUIDs fromblkid, so the disk is still there after a reboot
Disks are files too
On Linux, hardware shows up as special files in /dev. The first disk is /dev/sda, the second is /dev/sdb, and so on. A disk is divided into partitions, which get numbers: /dev/sdb1 is the first partition on the second disk. (Fast NVMe drives use names like /dev/nvme0n1p1, and cloud servers often use /dev/vda. Same idea.)
lsblk # tree of disks and partitions, and where they're mounted sudo fdisk -l # details of every disk sudo dmesg | tail # kernel messages: did Linux notice the new disk? df -hT # how full each mounted filesystem is, and its type
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─rl-root 253:0 0 17G 0 lvm /
└─rl-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 10G 0 disk ← the new disk: no partitions, not mounted
dmesg shows the kernel's own messages, like hardware being plugged in. On Ubuntu it needs sudo, and on Rocky it works without it, although sudo never hurts.
Step 1: partition with fdisk
fdisk is interactive: it asks questions and nothing is saved until you press w (write). Press q to quit without saving if you get confused.
sudo fdisk /dev/sdb
Then answer its questions like this:
Command (m for help): n # n = new partition Select (default p): p # primary Partition number (1-4, default 1): # Enter = accept the default First sector …: # Enter Last sector …: # Enter = use the whole disk Command (m for help): p # print, to check before saving Command (m for help): w # write and exit
Partitioning and formatting erase what's on the disk. sudo fdisk /dev/sda on a real server would be aimed at the system disk. Always run lsblk first, and make sure the disk you're about to touch is the empty one with nothing mounted.
Step 2: format with mkfs
A filesystem is the way files are organized on a partition. This is where the families differ:
sudo mkfs.xfs /dev/sdb1
Rocky's default filesystem is XFS. It's built for big files and big servers. It can grow but never shrink.
sudo mkfs.ext4 /dev/sdb1
Ubuntu's default is ext4, the classic Linux filesystem. It can grow and shrink. It creates a lost+found folder for file repairs.
Both families can use both filesystems. Just install the tools (xfsprogs on Ubuntu). Sticking to the default is a good habit, because it's what the next admin will expect.
Step 3: mount it
Windows gives a new disk a letter like E:. Linux has only one tree, so you mount the disk onto an empty folder, called the mount point. After that, whatever you save in that folder lands on the new disk.
sudo mkdir /data sudo mount /dev/sdb1 /data df -hT /data # check it: size, type, where sudo umount /data # unmount (note the spelling: umount, no n!)
You can't unmount a disk that's in use, and the most common user is you: your terminal's current folder is inside it. cd ~ first. If it's still busy, lsof /data shows which program is holding it.
Step 4: survive a reboot with /etc/fstab
A plain mount only lasts until the next reboot. To mount a disk automatically at boot, add a line to /etc/fstab (the “file system table”). It has six columns:
UUID=e645634b-9477-… /data ext4 defaults 0 0
# what (disk ID) where type options dump fsck-order
Use the UUID, the disk's unique ID, rather than /dev/sdb1. Device names can change if you add another disk, but the UUID never does. Get it with blkid:
sudo blkid /dev/sdb1 echo "UUID=$(sudo blkid -s UUID -o value /dev/sdb1) /data xfs defaults 0 0" | sudo tee -a /etc/fstab sudo systemctl daemon-reload sudo mount -a
sudo blkid /dev/sdb1 echo "UUID=$(sudo blkid -s UUID -o value /dev/sdb1) /data ext4 defaults 0 0" | sudo tee -a /etc/fstab sudo systemctl daemon-reload sudo mount -a
$( ) runs the command inside it and pastes its output right there, so you don't have to copy the long UUID by hand. You could also open /etc/fstab in nano and type the line yourself.
mount -asudo mount -a mounts everything in fstab, right now. If there's a typo, you get the error now, while you can still fix it. If you don't test and the server reboots with a broken fstab line, it can stop in emergency mode and refuse to boot normally. Adding nofail to the options (defaults,nofail) lets it boot even if that disk is missing.
systemd turns fstab lines into “mount units” behind the scenes, which is why mount reminds you to run systemctl daemon-reload after editing it (Linux Basics, lesson 15).
Try it: add a new disk 💽
This server has a brand-new, empty 10 GB disk. Make it usable at /data, permanently.
Quick check
1. What is /dev/sdb2?
✓ The letter is the disk (a, b, c…) and the number is the partition.
2. Why use a UUID in /etc/fstab instead of /dev/sdb1?
✓ Otherwise a new disk could make /data mount the wrong one.
3. sudo umount /data says “target is busy.” What's the first thing to check?
✓ Then lsof /data if it's still busy.
4. You added a line to /etc/fstab. What should you do before you ever reboot?
✓ A broken fstab can stop a server from booting. Test while you're still logged in.