Sub topic 7
Formatting and Mounting a Volume on Linux
This chapter assumes you can create and attach an EBS volume (covered in the previous chapter), that you are comfortable with a Linux shell and sudo, and that you know what a file system and a partition are. A newly attached EBS volume is a raw block device. The instance can see it, but you cannot store files on it until you put a file system on it and mount it.
The working model
Attaching a volume gives the operating system a block device. Three more steps turn it into usable storage:
- Identify the device the kernel created.
- Create a file system on it, which is done once and only for an empty volume.
- Mount the file system at a directory, and record the mount in
/etc/fstabso it returns after a reboot.
Step 1: Identify the device
On Nitro-based instances, EBS volumes appear as NVMe devices. The --device /dev/sdf value you gave to attach-volume is only a request. The kernel names the device /dev/nvme1n1, /dev/nvme2n1, and so on. The numbering depends on the order in which devices are detected, so it is not a reliable identifier.
Run lsblk to list block devices:
lsblkA typical result looks like this:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
nvme0n1 259:0 0 8G 0 disk
├─nvme0n1p1 259:1 0 8G 0 part /
└─nvme0n1p128 259:2 0 1M 0 part
nvme1n1 259:3 0 100G 0 disknvme0n1 is the root volume. It has partitions and a mount point. nvme1n1 is the new 100 GiB volume. It has no partitions and no mount point. Match the device by size and by the absence of a mount point.
To check which EBS volume a device belongs to, read its serial number. For EBS volumes on Nitro instances, the serial is the volume ID without the hyphen:
lsblk -o NAME,SIZE,SERIAL,MOUNTPOINTIf the serial shows vol0123456789abcdef0, the device is vol-0123456789abcdef0. Use this check whenever an instance has several volumes of the same size.
Step 2: Check for an existing file system
Run this check before formatting:
sudo file -s /dev/nvme1n1/dev/nvme1n1: datameans the volume has no file system and is empty.- A result such as
SGI XFS filesystem dataorLinux rev 1.0 ext4 filesystem datameans a file system already exists.
This matters most for volumes created from a snapshot. Those volumes carry the snapshot's file system and data.
Step 3: Create the file system
XFS and ext4 are the two common choices on Linux. XFS is the default on Amazon Linux 2023 and handles large files and parallel I/O well. ext4 is widely supported and is fine for general use. Choose one and use it consistently across your fleet so your tools and runbooks stay the same.
lsblk
sudo mkfs -t xfs /dev/nvme1n1Here the file system is placed directly on the whole device, with no partition table. This is normal for a data volume that holds a single file system. It also makes growing the volume easier later, because there is no partition to extend first.
For ext4 the command is sudo mkfs -t ext4 /dev/nvme1n1.
Step 4: Mount the file system
Create a mount point and mount the device:
sudo mkdir /data
sudo mount /dev/nvme1n1 /data
df -hT /dataThe df -hT output shows the file system type, size, and mount point. You can now write files under /data. If you wrote to /data before mounting, those files are hidden while the volume is mounted, because the mount covers the directory's earlier contents.
A mount made with the mount command lasts only until the next reboot. To make it permanent you need an /etc/fstab entry.
Step 5: Make the mount persistent
/etc/fstab lists the file systems the system mounts at boot. Use the file system's UUID in the entry, not the device name. Device names such as /dev/nvme1n1 can change between boots, and a wrong name could mount the wrong volume.
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 -aReplace <uuid-from-blkid> with the real UUID before you run the echo line. The fields in the entry are:
| Field | Example | Meaning |
|---|---|---|
| Device | `UUID=...` | The file system to mount, identified by UUID |
| Mount point | `/data` | The directory where it appears |
| Type | `xfs` | The file system type |
| Options | `defaults,nofail` | Mount options, comma-separated |
| Dump | `0` | Legacy backup flag; use 0 |
| Pass | `2` | Order of boot-time file system checks; 0 skips the check |
The nofail option is important. Without it, a missing or failed volume makes the boot wait and can drop the instance into emergency mode, where you may not be able to connect by SSH. With nofail, the boot continues and the mount is simply absent.
Two details about the last field. XFS does not use fsck at boot, so a pass value of 0 is also acceptable for XFS. For ext4, 2 is the usual value for non-root file systems.
Then run sudo mount -a. This mounts every fstab entry that is not already mounted. If your new line has a typo, mount -a reports the error now, while you still have a working shell, and not at the next reboot. If the volume is already mounted, mount -a does nothing for it. To test the entry fully, run sudo umount /data first, then sudo mount -a, then check with df -h /data.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `mount: wrong fs type, bad option, bad superblock` | The device has no file system, or the type is wrong | Run `file -s` on the device. Format it only if it is empty. |
| `/dev/nvme1n1` is not in `lsblk` | The volume is not attached, or it is in a different Availability Zone | Check the attachment state with `aws ec2 describe-volumes`. The volume and instance must share an Availability Zone. |
| Mount works, but is gone after reboot | No `fstab` entry | Add the UUID entry and run `mount -a` to test it. |
| Instance does not finish booting after a reboot | `fstab` entry for a missing volume, without `nofail` | Add `nofail`. Fix the entry from a rescue instance if you cannot connect. |
| Permission denied when writing to `/data` | The new file system's root directory is owned by `root` | Run `sudo chown ec2-user:ec2-user /data`, or set ownership for your own user or group. |
| Wrong volume mounted after a reboot | `fstab` uses a device name, not a UUID | Use `UUID=` entries. |
Notes on partitions and duplicate UUIDs
Putting the file system directly on the device is simple, but some teams prefer a partition table, for example with parted. In that case you format and mount the partition (/dev/nvme1n1p1), and growing the volume later requires growpart before you extend the file system.
XFS UUIDs must be unique on a host. If you restore a snapshot of a mounted XFS volume to a new volume and attach it to the same instance, the mount can fail because both file systems share a UUID. Use sudo mount -o nouuid /dev/nvme2n1 /mnt/restore for a temporary mount, or attach the restored volume to a different instance.
Exercise
You attach a new 100 GiB volume to a Nitro instance. lsblk shows nvme0n1 (with partitions, mounted at /) and nvme1n1 (no partitions, no mount point). You format nvme1n1 with XFS and mount it at /data. Which two changes make the mount safe across reboots, and why does each matter?
Show answer
First, reference the file system by UUID (from blkid) in /etc/fstab, because NVMe device names such as /dev/nvme1n1 can change between boots and the wrong volume could be mounted. Second, add the nofail option, so a missing or failed volume does not block boot or drop the instance into emergency mode. Test the entry with sudo mount -a before rebooting.
Exercise
A colleague restores a snapshot to a new volume, attaches it, and runs sudo mkfs -t xfs on it before mounting. What went wrong, and what should they have done first?
Show answer
A volume created from a snapshot already contains the snapshot's file system and data, and mkfs erases it. They should have run sudo file -s <device> to see whether a file system exists, and then mounted the device directly.