可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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}/script/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}/script/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
回答2:
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_mound 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.