Exercise - Initialize an Attached Volume with a Provisioner
A previous exercise asked you to create a volume and attach it to the leader instance. Of course, merely creating and attaching a volume to an instance will not make it useful to the operating system. You also need to format and partition the volume, set up a file system on it and mount the partition for it to function nicely in your operating system. In this exercise you are to create an inline remote-exec provisioner to perform these functions. The provisioner should be added to the resource that attaches the volume to the instance so that both are ready when it runs. Note that while this example may be compelling and useful, automatically formatting volumes is certainly dangerous and not recommended in all situations!
There are multiple ways to format and partition volumes, but we will be using some of the suggestions from
this page to do the work in a non-interactive way.
Assuming that your volume is attached as /dev/sdb
, this series of commands should
format and initialize the whole volume into one partition and mount it as /mnt/terratest
:
sudo parted /dev/sdb mklabel gpt
sudo parted -a opt /dev/sdb mkpart primary ext4 0% 100%
sudo mkfs.ext4 -L terratest /dev/sdb1
sudo mkdir -p /mnt/terratest
sudo mount -o defaults /dev/sdb1 /mnt/terratest