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

February 15, 2017 694 views
System Tools Block Storage Ubuntu 16.04

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 cloudinitmodules 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.

1 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!

  • @davidkolb Just wanted to let you know a fix was deployed for this. I've been able to successfully mount a volume on boot with the cloud-config file above.

Have another answer? Share your knowledge.