19 - Volumes (Hetzner Auto-Format)
Working Code:
terraform/exercise-19-volume-manual/
The Problem: Server local disks are ephemeral. Delete the server, lose the data.
The Solution: Use external Volume (block storage) that survives server deletion.
Objective
Attach a 10GB volume using Hetzner's auto-format and auto-mount features.
How-to
1. Create & Attach Volume
Hetzner can format and mount the volume automatically:
resource "hcloud_volume" "data_volume" {
name = "data-volume"
size = 10
location = "hel1"
format = "ext4" # Hetzner formats it for you
}
resource "hcloud_volume_attachment" "main_attachment" {
volume_id = hcloud_volume.data_volume.id
server_id = hcloud_server.web.id
automount = true # Hetzner mounts it automatically
}2. Verify
After terraform apply, SSH into the server:
lsblk # See the volume (usually /dev/sdb)
df -h # Volume is already mounted (typically at /mnt/HC_Volume_*)Hetzner automatically formats and mounts the volume, saving you manual steps.
3. Test Persistence
echo "Important Data" > /mnt/HC_Volume_*/file.txt
# Destroy server but keep volume → data survives!4. Identify the Volume
lsblkThe Hetzner volume is the 10G disk — the device name (sda or sdb) can change between reboots and even from what Terraform's output shows. Always identify it by size via lsblk, not by assuming a fixed name.
5. Wipe and Partition
sudo wipefs -a /dev/<device>
sudo fdisk /dev/<device>Inside fdisk:
g ← new GPT partition table
n ← new partition
1 ← partition number
Enter ← accept default first sector
+5G ← last sector
n ← new partition
2 ← partition number
Enter ← accept default first sector
Enter ← accept default last sector (remaining space)
w ← write and exit6. Format Partitions
sudo mkfs -t ext4 /dev/<device>1
sudo mkfs -t xfs /dev/<device>27. Mount Partitions
sudo mkdir /disk1 /disk2
sudo mount /dev/<device>1 /disk1
sudo mount /dev/<device>2 /disk2
df -h | grep disk8. Make Persistent with fstab
Always use UUIDs — device names change across reboots, UUIDs don't.
sudo blkid /dev/<device>1 /dev/<device>2
echo "UUID=<uuid1> /disk1 ext4 defaults 0 0" | sudo tee -a /etc/fstab
echo "UUID=<uuid2> /disk2 xfs defaults 0 0" | sudo tee -a /etc/fstab
sudo mount -a # test — should produce no errors9. Reboot and Verify
sudo reboot
# reconnect with ./bin/ssh
df -h | grep disk # both /disk1 and /disk2 should still be mountedProblems & Learnings
Common Issues
- Device name changes between reboots — the volume may be
sdaorsdbdepending on boot order. Always uselsblkto identify it by size, and use UUIDs in/etc/fstab(never/dev/sdX) automount = truepre-creates a partition — if the Terraform config usesautomount = true, Hetzner will format the disk as a single partition before you log in. Usesudo wipefs -a /dev/<device>to clear it before runningfdisk- fdisk
+5Ggoes on the Last sector prompt, not First sector — press Enter to accept the default first sector, then type+5G
Key Takeaways
- Use UUIDs in
/etc/fstabfor reliable persistent mounts regardless of device naming lsblkis the right tool to identify disks;blkidgives you the UUIDssudo mount -atests your fstab before rebooting — a typo here can make the server unbootable
Related Exercises
- 20 - Volume Auto - Full control with cloud-init