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

  1. 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"
}
  1. Output the volume device by adding this to outputs.tf:
output "volume_device" {
    value = hcloud_volume.volume01.linux_device
}
  1. Apply configuration:
terraform init
terraform apply
  1. Reboot the server :
sudo reboot

Info

Needed due to Hetzner automount bug.

  1. 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

  1. Change into the mount directory:
cd /mnt/HC_Volume_...
  1. Try unmounting:
sudo umount /mnt/HC_Volume_...

Fail

You will see:

umount: target is busy.

  1. Change to / and unmount again:
cd /
sudo umount /mnt/HC_Volume_...

Success

Unmounting should be possible.

Partitioning a Volume

  1. Identify the device:
lsblk

Success

Output: usually /dev/sdb

  1. 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

  1. Check results:
sudo fdisk -l /dev/sdb

Success

Should show /dev/sdb1 and /dev/sdb2.

Create File System

  1. Create ext4 on the first partition:
sudo mkfs -t ext4 /dev/sdb1
  1. Create xfs on the second partition:
sudo mkfs -t xfs /dev/sdb2

Mount Manually

  1. Create mount points:
sudo mkdir /disk1 /disk2
  1. Mount partitions:
sudo mount /dev/sdb1 /disk1
  1. Use blkid to get the UUID of the second partition:
sudo blkid /dev/sdb2

Success

Output: /dev/sdb2: UUID=""

  1. Test with files:
sudo touch /disk1/testfile
ls /disk1

Success

Output should show testfile

  1. Unmount both and show content:
sudo umount /disk1 /disk2
ls -l /disk1

Fail

testfile shouldn't show up anymore

Persistent Mounting for Partition

  1. Get UUID of second partition:
sudo blkid /dev/sdb2
  1. Edit /etc/fstab:
/dev/sdb1   /disk1  ext4  defaults,nofail  0  2
UUID=<uuid> /disk2  xfs   defaults,nofail  0  2
  1. Test:
sudo mount -a
  1. Reboot:
sudo reboot
  1. 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 use nofail.
  • Ensure you are not inside a mount point before unmounting.
  • Make sure you're using the right server_id.

Terraform Hetzner Docs

Terraform Outputs

Hcloud Terraform Automount Bug

Hcloud Volumes

Linux CLI