Sub topic 7
Formatting and Mounting a Volume on Linux
This chapter assumes you can run the AWS CLI, log in to a Linux instance over SSH, and know what a file system and a block device are. It also assumes you have already created a volume and attached it to an instance in the same Availability Zone.
What attaching does and does not do
Attaching a volume gives the instance a raw block device. It has no file system, so you cannot store files on it yet. A new, empty volume needs three steps: format it, mount it, and make the mount persistent.
On Nitro-based instances, EBS volumes appear as NVMe devices. If you attached the volume as /dev/sdf, the kernel names it something like /dev/nvme1n1. The device name you passed to attach-volume is only a hint. The nvme number depends on the order in which devices are discovered, so always check with lsblk before you format anything.
Format and mount
Run these commands on the instance.
lsblk
sudo mkfs -t xfs /dev/nvme1n1
sudo mkdir /data
sudo mount /dev/nvme1n1 /data
sudo blkid /dev/nvme1n1
# Add the UUID printed by blkid to /etc/fstab so the mount survives reboots:
echo 'UUID=<uuid-from-blkid> /data xfs defaults,nofail 0 2' | sudo tee -a /etc/fstab
sudo mount -aHere is what each step does:
lsblklists block devices. The root volume has partitions such asnvme0n1p1. Your new volume shows as a disk with no partitions and no mount point.mkfs -t xfswrites an XFS file system to the device. It erases whatever is on the device.mkdirandmountcreate a mount point and attach the file system to it. Files written to/datanow go to the volume.blkidprints the file system's UUID. Replace<uuid-from-blkid>in theecholine with that value before you run it.mount -amounts everything listed in/etc/fstab. It reports errors from a bad entry now, rather than at the next boot.
Why use the UUID and nofail
The /etc/fstab file lists file systems to mount at boot. Two choices in the entry above matter.
- UUID instead of device name. Device names such as
/dev/nvme1n1can change between reboots. A UUID belongs to the file system, so the right volume mounts every time. nofail. Without it, a missing volume makes the boot fail and may drop the instance into emergency mode. Withnofail, the instance boots and skips the mount. This matters on cloud instances where you may have no console access.
The last two fields are the dump flag (0) and the fsck pass order (2). XFS does not use fsck at boot, so the pass value has little effect here.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `lsblk` does not show the volume | Volume and instance are in different Availability Zones, or the attachment is not yet `attached` | Check with `describe-volumes` and use a volume in the instance's zone |
| `mount: wrong fs type` | The volume has no file system | Format it, after confirming it is empty |
| `mount -a` reports a bad entry | Typo or leftover placeholder in `fstab` | Edit `/etc/fstab` and run `mount -a` again |
| Instance fails to boot after reboot | `fstab` entry lacks `nofail` and the volume is missing | Add `nofail` and fix the entry |
| Permission denied writing to `/data` | The mount point is owned by root | Use `sudo chown` to give your user ownership |
Exercise
You attached a volume as /dev/sdf to a Nitro instance, but lsblk shows nvme1n1. Why must you use a UUID in /etc/fstab and add nofail?
Show answer
The kernel assigns NVMe names by discovery order, so /dev/nvme1n1 may refer to a different device after a reboot. The UUID identifies the file system itself and always matches the right volume. The nofail option lets the instance finish booting if the volume is missing, instead of failing at boot.
Exercise
A volume was created from a snapshot. Should you run mkfs -t xfs on it before mounting? How can you check?
Show answer
No. A volume restored from a snapshot already holds the file system and data, and mkfs would erase them. Run sudo file -s /dev/nvme1n1. If it names a file system, mount the device directly. If it says data, the device is empty and needs formatting.