Volumes

Figure 1013. A volume: The easy way Slide presentation
resource "hcloud_server" "helloServer" {
  server_type  =  "cx22"
...
}

resource "hcloud_volume" "volume01" {
  name      = "volume1"
  size      = 10
  server_id = hcloud_server.helloServer.id
  automount = true
  format    = "xfs"
}
df
...
/mnt/HC_Volume_100723816

Figure 1014. Volume details Slide presentation
output "volume_id" {
 value=hcloud_volume.volume01.id
 description = "The volume's id"
}
# ls /dev/disk/by-id/*100723816
/dev/disk/by-id/scsi-0HC_Volume_100723816
terraform apply
...
hello_ip_addr="37.27.22.189"
volume_id="100723816"

Desired /etc/fstab:

/dev/disk/by-id/scsi-0HC_Volume_100723816 
  /volume01 xfs discard,nofail,defaults 0 0

exercise No. 9

Auto mounting a volume

Q:

Follow Figure 1013, “A volume: The easy way ” adding an a auto mounted volume to an existing server.

Tip

Due to a Cloud-init implementation quirk consider the auto mount workaround hint.

Figure 1015. Providing a mount point's name Slide presentation
resource "hcloud_server" "helloServer" {
    ...
  user_data = templatefile("tpl/userData.yml", {
     ...
    volume01Id = hcloud_volume.volume01.id
  })
}
resource "hcloud_volume" "volume01" {
  server_id = hcloud_server.helloServer.id
  ...
}

Problem: Cyclic dependency

helloServer <--> volume01

Figure 1016. Solution: Independent resource creation Slide presentation
main.tf userData.yml.tpl
resource "hcloud_volume" "vol01" {
  size = 10  ....
}
resource "hcloud_server" "hello" {
  user_data = templatefile(
    "userData.yml.tpl", {
       # No cycle
       volId=hcloud_volume.vol01.id
  })  ...
}
resource "hcloud_volume_attachment"
  "main" {
  volume_id=hcloud_volume.vol01.id
  server_id=hcloud_server.hello.id
}
echo 
 `/bin/ls /dev/disk/by-id/*${volId}` 
 /vol01 xfs discard,nofail,defaults 0 0
  >> /etc/fstab

exercise No. 10

Mount point's name specification

Q:

In Auto mounting a volume the mount point's name had been auto generated by Cloud-init. Modify your setup and define /volume01 as the new mount point. Avoiding the dependency cycle from Figure 1015, “Providing a mount point's name ” you may follow:

  • Create volume and server independently

    Tip

    Choose a common location value for server and volume e.g. nbg1.

  • Attach volume to server by virtue of hcloud_volume_attachment setting automount = false.

  • Pass the volume's id to your Cloud-init template and add a corresponding line to /etc/fstab.

    Tip

    You may have to execute systemctl daemon-reload after modifying /etc/fstab

  • Execute mount -a for taking your changes into effect