Partitions And Mounting
Technical Background
Cloud volumes in cloud environments such as Hetzner are virtual block devices that can be dynamically attached to a virtual machine. They can be automatically mounted, partitioned, and formatted with different file systems. In Linux, storage devices are represented as /dev/sdX
, and partitions as /dev/sdXn
. Mounting is the process of attaching a file system (ext4, xfs, etc.) to a directory tree so that files become accessible. Persistent mounts are typically defined in /etc/fstab
.
In this exercise, you extend a Terraform configuration with an attached volume, manually explore partitions using fdisk
, create multiple file systems, and configure persistent mounts via /etc/fstab
. A known bug in Hetzner’s volume automount feature requires triggering udevadm
to detect the device after creation.
Note
Unlike previous exercises, most of the partitioning and mounting steps are manual and not automated in Terraform. This is intentional to give hands-on experience with Linux storage management.
Solution
Prerequisits
Create a main.tf
, outputs.tf
, /tpl/*
variables.tf
, network.tf
,providers.tf
and secrets.auto.tfvars
like in 14 Solving Known Host Quirk.
Add an additional volume using Terraform
- Create a file
volumes.tf
that contains:
resource "hcloud_volume" "volume01" {
name = "volume1"
size = 10
server_id = hcloud_server.web.id
automount = true
format = "xfs"
}
- Output the volume device by adding this to
outputs.tf
:
output "volume_device" {
value = hcloud_volume.volume01.linux_device
}
- Apply configuration:
terraform init
terraform apply
- Reboot the server :
sudo reboot
Info
Needed due to Hetzner automount bug.
- Validate the mount after reboot:
df -h
Fail
Your volume probably doesn't show up in the list. This is due the automount Hcloud bug.
Warning
Automount doesn't work when a server is recreated with with Hetzner
Info
If the volume does not show up, add this to userData.yml
:
- udevadm trigger -c add -s block -p ID_VENDOR=HC --verbose -p ID_MODEL=Volume
Unmounting the Volume
- Change into the mount directory:
cd /mnt/HC_Volume_...
- Try unmounting:
sudo umount /mnt/HC_Volume_...
Fail
You will see:
umount: target is busy.
- Change to
/
and unmount again:
cd /
sudo umount /mnt/HC_Volume_...
Success
Unmounting should be possible.
Partitioning a Volume
- Identify the device:
lsblk
Success
Output: usually /dev/sdb
- Create two partitions:
sudo fdisk /dev/sdb
Info
In fdisk
:
- Press n
→ new partition
- p
→ primary
- Partition 1 → +5G
- Partition 2 → default remaining size
- Press t
→ set type → 83
(Linux)
- Press w
→ write changes
- Check results:
sudo fdisk -l /dev/sdb
Success
Should show /dev/sdb1
and /dev/sdb2
.
Create File System
- Create ext4 on the first partition:
sudo mkfs -t ext4 /dev/sdb1
- Create xfs on the second partition:
sudo mkfs -t xfs /dev/sdb2
Mount Manually
- Create mount points:
sudo mkdir /disk1 /disk2
- Mount partitions:
sudo mount /dev/sdb1 /disk1
- Use
blkid
to get the UUID of the second partition:
sudo blkid /dev/sdb2
Success
Output: /dev/sdb2: UUID=""
- Test with files:
sudo touch /disk1/testfile
ls /disk1
Success
Output should show testfile
- Unmount both and show content:
sudo umount /disk1 /disk2
ls -l /disk1
Fail
testfile
shouldn't show up anymore
Persistent Mounting for Partition
- Get UUID of second partition:
sudo blkid /dev/sdb2
- Edit
/etc/fstab
:
/dev/sdb1 /disk1 ext4 defaults,nofail 0 2
UUID=<uuid> /disk2 xfs defaults,nofail 0 2
- Test:
sudo mount -a
- Reboot:
sudo reboot
- Validate:
df -h
Success
Both partitions are now mounted automatically on boot and ready for use.
Warning
- When automount is true you may still require a reboot.
fstab
mistakes can break boot; always usenofail
.- Ensure you are not inside a mount point before unmounting.
- Make sure you're using the right
server_id
.