7

According to documentation, using terraform, I'm able to create a droplet on digital ocean:

resource "digitalocean_volume" "foobar" {
  region      = "nyc1"
  name        = "baz"
  size        = 100
  description = "an example volume"
}

So, I'm also able to add a volume to it:

resource "digitalocean_droplet" "foobar" {
  name       = "baz"
  size       = "1gb"
  image      = "coreos-stable"
  region     = "nyc1"
  volume_ids = ["${digitalocean_volume.foobar.id}"]
}

I'd like to know how to mount this on a desired location. I need to mount it automatically. I mean, when droplet is up I need to the volume is mounted. I was thinking about using chef...

Any ideas?

Jordi
  • 20,868
  • 39
  • 149
  • 333

2 Answers2

8

To mount the volume automatically, you can use user_data via cloud init to run a script as follow:

This is how your digitalocean_droplet resources should reflect:

resource "digitalocean_droplet" "foobar" {
  name       = "baz"
  size       = "1gb"
  image      = "coreos-stable"
  region     = "nyc1"
  volume_ids = ["${digitalocean_volume.foobar.id}"]
   # user data
  user_data = "${data.template_cloudinit_config.cloudinit-example.rendered}"
}

Then your cloud.init file that contains the cloudinit_config should be as bellow. It will reference the shell script in ${TERRAFORM_HOME}/scripts/disk.sh that would mount your volume automatically:

provider "cloudinit" {}


data "template_file" "shell-script" {
  template = "${file("scripts/disk.sh")}"
  
}
data "template_cloudinit_config" "cloudinit-example" {

  gzip = false
  base64_encode = false

  part {
    content_type = "text/x-shellscript"
    content      = "${data.template_file.shell-script.rendered}"
  }

}

The shell script to mount the volume automatically on startup is in ${TERRAFORM_HOME}/scripts/disk.sh

It will first check if a file system exist. If true it wouldn't format the disk if not it will

#!/bin/bash


DEVICE_FS=`blkid -o value -s TYPE ${DEVICE}`
if [ "`echo -n $DEVICE_FS`" == "" ] ; then
        mkfs.ext4 ${DEVICE}
fi
mkdir -p /data
echo '${DEVICE} /data ext4 defaults 0 0' >> /etc/fstab
mount /data

  

I hope this helps

Mikesname
  • 8,781
  • 2
  • 44
  • 57
Innocent Anigbo
  • 4,435
  • 1
  • 19
  • 19
  • 1
    A provider called `cloudinit` doesn't exist in Terraform (or it's plugins). Additionally, where is `DEVICE` set at or coming from? – Dids May 30 '18 at 08:09
  • I understand now, in the data.template_file.shell-script you can have set vars, there you can define what DEVICE is set to and it will be replaced by terraform when applying the file – Rumbles Mar 13 '19 at 09:00
  • I wrote this for aws but just notice that the commands are slightly different for ubuntu 18 https://stackoverflow.com/a/63126594/8723007 – valem Jul 28 '20 at 03:29
7

Mounting the volume needs to be done from the guest OS itself using mount, fstab, etc.

The digital ocean docs cover this here.

Using Chef you could use resource_mount to mount it in an automated fashion.

The device name will be /dev/disk/by-id/scsi-0DO_Volume_YOUR_VOLUME_NAME. So, using the example from the Terraform docs, it would be /dev/disk/by-id/scsi-0DO_Volume_baz.

James
  • 1,748
  • 1
  • 11
  • 14
c3st7n
  • 1,891
  • 13
  • 15
  • I need to mount it automatically with droplet. – Jordi May 19 '17 at 13:16
  • Are you using any config management like puppet, chef or ansible to confugure the droplet after boot? If not, you could simply run a mount command from your user-data. – c3st7n May 19 '17 at 13:18
  • Yes! Nevertheless, I'm not quite to understand where the volume is created as a device (I mean `/dev/???`). Could you take a look on [this documentation](https://www.terraform.io/docs/providers/do/r/volume.html). Are you able to figure out where the volume is located? – Jordi May 19 '17 at 13:44
  • If you read the documentation I listed it shows that the device name will be `/dev/disk/by-id/scsi-0DO_Volume_YOUR_VOLUME_NAME`. So using the example from the terraform docs it would be `/dev/disk/by-id/scsi-0DO_Volume_baz`. – c3st7n May 19 '17 at 13:51