TL;DR
- A shared host keeps HOME on a data EBS volume separate from root. When the VM fails, I want to attach that volume to a new VM.
- The new VM’s first-boot helper did “mkfs if empty, refuse if a filesystem exists”. Relaxing it to “mount if ext4” risks mounting the wrong disk or a copy.
- The disk is identified by its NVMe serial (the EBS volume ID) and admitted only when its ext4 UUID matches the one recorded at creation. No mkfs, no wipefs.
- If another device has the same UUID, the helper stops. A volume created from a snapshot carries the same UUID.
A new VM’s first boot formats the data disk
Each host has a root volume and a data EBS volume for HOME. At creation, a root first-boot helper formats the data volume as ext4, mounts it on /home, and starts the service.
Before formatting, the helper checks that the disk has no fstype and no UUID, is not mounted, and that wipefs finds no signatures. If any check fails, it stops without formatting. AWS documentation says the same: volumes created from snapshots usually already have a filesystem, and running mkfs on top destroys the data.
The helper writes a journal before and after formatting. If it finds format_started, mkfs died partway. It does not continue automatically; a person looks at it.
That “refuse an existing filesystem” rule is exactly what blocks recovery.
What if it just mounted any ext4?
The simplest relaxation is to mount, without formatting, any disk that already has ext4. Here is what that would allow.
- Mounting a different disk.
/dev/nvme1n1is not guaranteed to be the same disk as last time. NVMe device names follow the order in which devices answer at boot. - Mounting a snapshot copy. With a restored copy attached for inspection, two disks show the same UUID.
UUID=in fstab does not guarantee which one it picks. - Detaching before the old VM has stopped. Force detach gives the instance no chance to flush filesystem caches. AWS calls it a last resort.
- Accepting an unknown ext4. Being ext4 says nothing about whether it was this host’s HOME.
In every case the mount succeeds. What breaks is the data.
Identify the disk by NVMe serial, not device name
EBS puts the volume ID, without the hyphen, in the NVMe serial number. The helper requires exactly one device whose lsblk SERIAL matches the volume ID, and requires it to be a whole disk with no partitions.
serial = volume_id.replace("-", "")
matches = [r for r in rows if str(r.get("serial", "")).strip() == serial]
if len(matches) != 1 or matches[0].get("children"):
raise ValueError("exact data volume is not uniquely attached")
AWS documentation also recommends stable identifiers such as the volume ID or the mount point instead of device names.
Record the UUID when the host is created
The UUID is what says “this ext4 was this host’s HOME”. At creation, the control plane stores the instance ID, volume ID and ext4 UUID returned by the helper. A remote command reporting Success is not enough; the output is strictly validated first.
A later record that changes the UUID of the same volume, or that arrives for a different host, is rejected. Hosts created before the UUID was recorded are never guessed into recovery.
During recovery, the helper takes the existing-data path only when the new VM’s fixed configuration file contains existing_data_uuid. It then requires all of the following:
- the service is not running
- there is no host configuration file and no
/homefstab line yet /homeis empty and not mounted- the disk’s fstype is ext4, its UUID matches the record, and it is not mounted
If all hold, the helper writes formatted and the UUID to the journal and skips the formatting step. It does not call mkfs, wipefs or chown.
Stop if a second disk shows the same UUID
UUIDs carry over to snapshot copies. Before mounting, the helper walks every disk and partition and requires that only the identified device has that UUID.
pending, found = list(rows), []
while pending:
row = pending.pop()
pending.extend(row.get("children", []))
if row.get("uuid") == uuid:
found.append(row["path"])
if found != [device]:
raise ValueError("filesystem UUID is not unique to the exact volume")
The mount uses the verified device path, not UUID=. The helper then checks the mount source and UUID again with findmnt before writing configuration and starting the service.
fstab gets UUID=... /home ext4 defaults,nofail, the form AWS recommends. Later boots resolve by UUID=, so not attaching the original and a copy to the same VM at once stays the job of whatever performs the swap.
Detach only after the old VM is confirmed stopped
The swap runs in this order:
- confirm the old VM is stopped in AWS
- revoke the old boot and its credentials
- detach normally, without force
- attach to the new VM
- confirm the new boot’s enrollment
IAM cannot enforce either “no force” or “already stopped”. The control plane checks this order.
Terraform state keeps the original VM, every later generation, and the data volume, all with prevent_destroy. A later maintenance apply cannot delete an old VM or move the data volume back to an earlier generation.
If the EBS volume itself fails, inspect only the copy
When the volume itself is damaged, a new volume is created from a snapshot. The original stays untouched.
The copy is attached to a validation VM with no network and no instance profile. Only there is the journal replayed, followed by a read-only fsck and a read-only mount. Nothing inside the copy is executed. A copy that passes goes to the new VM through the same UUID-matching path described above.
Whether to run mkfs depends on whether this is the disk with the recorded UUID, not on whether ext4 is present. Device names, the presence of a filesystem, and UUID= resolution each fail to prove “this is this host’s data” on their own.