How to use cloud-init to mount block storage that's already formatted and ready to mount?

I’m using Ubuntu 16.04 on a droplet using Terraform. I have an existing volume that’s already been formatted that I would like to mount to /home so I can persist my user directory between applications from terraform.

Unfortunately, while /etc/cloud/cloud.cfg lists mounts in it’s cloud_init_modules no entry is ever written to /etc/fstab.

This is in my userdata:

  - - '/dev/disk/by-id/scsi-0DO_Volume_volume-name-here-part1'
    - '/home'
    - 'ext4'
    - 'defaults,nofail,discard'
    - '0'
    - '2'
  - zsh
  - git
  - ufw
  - name: demo
    groups: sudo
    shell: /bin/zsh
    sudo: ['ALL=(ALL) NOPASSWD:ALL']
      - 'ssh-rsa <snip>'
  # Secure SSHD
  - [ sed, -i, -e, 's/^PermitRootLogin yes/PermitRootLogin no/', '/etc/ssh/sshd_config' ]
  - [ service, sshd, restart]
  - [ rm, -f, /root/.ssh/authorized_keys ]
  # Secure UFW
  - ufw default deny incoming
  - ufw default allow outgoing
  - ufw allow ssh
  - ufw enable

If I run this command as root, cloud-init -d single -n mounts, the entry is written to /etc/fstab and /home is mounted. Then I need to run mkhomedir_helper demo to recreate my home directory.

How can I get the mounts module to run automatically? The cloud-config docs are…less than ideal and don’t really explain anything in the examples except how to structure the mounts hash.

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
February 16, 2017
Accepted Answer

The cloud-config docs are indeed less than ideal… I took some time to investigate this and discussed it with one of our engineers. It looks like due to changes in the version shipping with Ubuntu 16.04, the mounts module is not currently run on our platform requiring some changes in the vendor-data we provide.

I can confirm that this does work as expected on Ubuntu 14.04:

 - [ /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01, /mnt/volume-nyc1-01, "ext4", "defaults,nofail,discard", "0", "0" ]

As a work-a-round on Ubuntu 16.04, you would be able to translate this into something usable by runcmd like so:

 - mkdir -p /mnt/volume-nyc1-01
 - sudo mount -o discard,defaults /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01 /mnt/volume-nyc1-01
 - echo /dev/disk/by-id/scsi-0DO_Volume_volume-nyc1-01, /mnt/volume-nyc1-01, "ext4", "defaults,nofail,discard", "0", "0" | tee -a /etc/fstab

I’ll provide an update here when a fix has been released. Thanks for helping us catch this!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel