Volumes

Figure 1034. 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 1035. Unix mount Slide presentation

exercise No. 15

Partitions and mounting

Q:

This is actually a series of exercises learning about partitions, the underlying file systems and mount procedures rather than an actual question.

  1. After rebooting get info on your local file systems to identify the newly attached volume:

    # df -h
    Filesystem      Size  Used Avail Use% Mounted on
    udev            1.9G     0  1.9G   0% /dev
    tmpfs           382M  680K  382M   1% /run
    /dev/sda1        38G  2.1G   34G   6% /
    tmpfs           1.9G     0  1.9G   0% /dev/shm
    tmpfs           5.0M     0  5.0M   0% /run/lock
    /dev/sda15      241M  138K  241M   1% /boot/efi
    tmpfs           382M     0  382M   0% /run/user/1000
    /dev/sdb        10475520  106088  10369432   2% /mnt/HC_Volume_...

    The Terraform defined volume should be visible similar to the above. Your volume's entry will typically read /dev/sdb or similar. Note this value. How does the device relate to your linux_device output string?

  2. cd to the mounted volume. Now try using umount to unmount the file system. What do you observe?

  3. Leave your extra volume by e.g., cd /. Then repeat the previous step.

  4. We now want to divide our volume into two partitions. Read the fdisk man page and create two primary partitions each covering roughly half the volume's size using e.g., fdisk /dev/sdb. Typical names of these two new partitions will be /dev/sdb1 and /dev/sdb2. Both should have a Linux (83) partition Id.

  5. We now create two different file systems on our two new partitions:

    • Use mkfs -t ext4 /dev/sdb1 for creating an ext4 file system on your first partition.

    • Use mkfs -t xfs /dev/sdb2 for creating an xfs file system on your second partition.

  6. Create two mount points for your respective partitions:

    mkdir /disk1 /disk2
  7. Use the mount command to manually mount your first partition to /disk1.

  8. Use the blkid command to get your second partition's uuid value. Then use mount again this time using the uuid value to mount your second partition to /disk2.

  9. Copy or create some content in /disk1. Then umount both drives and watch this content vanishing from the file system tree.

  10. We now want to automate mounting our two partitions on system boot automatically. Read fstab and supply two respective entries in /etc/fstab. Like with your manual mount use /dev/sdb1 for your first drive and the second partition's uuid value for the second.

    Read about the -a option: Issuing mount -a should mount both partitions.

    Once this works, reboot your system. Mounting should have happened automatically.

Figure 1036. Volume details Slide presentation
output "volume_device" {
 value = hcloud_volume.volume01.linux_device
 description = "The volume's device"
}
terraform apply
...
volume_device = "/dev/disk/by-id/scsi-0HC_Volume_102626366"

Desired /etc/fstab entry:

/dev/disk/by-id/scsi-0HC_Volume_102626366 /volume01 xfs rw,defaults 0 2

Figure 1037. 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 1038. 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", {
       device=hcloud_volume.vol01.linux_device
  })  ...
}
resource "hcloud_volume_attachment"  "main" {
  volume_id=hcloud_volume.vol01.id
  server_id=hcloud_server.hello.id
}

userData.yml.tpl:

echo ${device} /${name} xfs rw,defaults 0 2 >> /etc/fstab

exercise No. 16

Mount point's name specification

Q:

In Partitions and mounting the mount point's name had been auto generated by Cloud-init e.g, /mnt/HC_Volume_102626366. Modify your setup and define /volume01 as mount point. 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 linux_device id and name to your Cloud-init template. Use the latter for creating a mount point and both values for creating a corresponding line in /etc/fstab.

  • Let cloud-init execute both systemctl daemon-reload to account for modifying /etc/fstab and mount -a for taking your changes into effect