7

Cloud-init provides the disk_setup, fs_setup and mounts modules to partition, format and mount a volume to a virtual machine, however these options remain undocumented at both http://cloudinit.readthedocs.org and AWS.

Does there exist a functional example of cloud-init configuration that will partition, format and mount a single EBS volume at AWS to an instance provisioned as follows:

"Volume" : {
  "Type" : "AWS::EC2::Volume",
  "Properties" : {
    "Size" : "100",
    "VolumeType" : "gp2",
    "AvailabilityZone" : { "Fn::GetAtt" : [ "ServerInstance", "AvailabilityZone" ] }
  },
  "DeletionPolicy" : "Delete"
},
"MountVolume" : {
  "Type" : "AWS::EC2::VolumeAttachment",
  "Properties" : {
    "InstanceId" : { "Ref" : "ServerInstance" },
    "VolumeId"  : { "Ref" : "Volume" },
    "Device" : "/dev/sdh"
  }
},

The closest I have so far is the following (with syntax issues corrected):

disk_setup:
  /dev/xvdh:
    layout: true
    overwrite: false
    table_type: 'mbr'
fs_setup: 
  - label: data
    device: /dev/xvdh0
    filesystem: ext4
mounts:
- [ xvdh0, /opt/data ]

According to the cloud-init-output.log the attempt to partition fails as follows:

2015-12-08 15:23:11,534 - util.py[WARNING]: Failed partitioning operation
'list' object has no attribute 'splitlines'

(For reasons undocumented an attempt to create a volume called /dev/sdh results in a partition appearing called /dev/xvdh, thus the mismatch in naming)

Graham Leggett
  • 287
  • 3
  • 15

2 Answers2

3

I know this is old, but here's what I came up with:

mounts:
    - [ "LABEL=rkt", "/var/lib/rkt" ]

fs_setup:
    ## rkt
    -   device: /dev/xvdg
        partition: none
        label: rkt
        filesystem: ext4

It formats /dev/xvdg -- unpartitioned -- as ext4 and labels it accordingly.

I had to add disk-setup to the cloud_init_modules list in /etc/cloud/cloud.cfg; it's not there by default, so fs_setup (which is processed by cc_disk_setup) wouldn't have any affect.

blalor
  • 186
0

There are two ways depending on which version of cloud-init your distro is using:

  • cloud-init >= 24.2, allows a pretty straighforward solution with device_aliases, disk_setup, fs_setup and mounts
  • cloud-init < 24.2, allows for a slightly less elegant solution where fs_setup cannot be used for NVMe (the bug is #5246 that was solved by #5263 which was released on cloud-init 24.2 changelog), and you need to use x-systemd.makefs instead.

cloud-init >= 24.2

As I commented on this other answer this is possible with cloud-init 24.2 (released on July 2024) and tested with Fedora 41 Rawhide 20240711:

#cloud-config
device_aliases:
  disk1: /dev/disk/by-id/nvme-Amazon_Elastic_Block_Store_vol0a250869ccd411b30
disk_setup:
  disk1:
    table_type: gpt
    layout: [50,25,25]
    overwrite: true
fs_setup:
  - label: disk1-earth
    filesystem: xfs
    device: disk1
    partition: 1
  - label: disk1-mars
    filesystem: xfs
    device: disk1
    partition: 2
  - label: disk1-venus
    filesystem: xfs
    device: disk1
    partition: 3
mounts:
  - [ LABEL=disk1-earth, /earth, xfs, "defaults,nofail,x-systemd.device-timeout=30"]
  - [ LABEL=disk1-mars,  /mars, xfs, "defaults,nofail,x-systemd.device-timeout=30"]
  - [ LABEL=disk1-venus, /venus, xfs, "defaults,nofail,x-systemd.device-timeout=30"]
mounts_default_fields: [ None, None, "auto", "defaults,nofail", "0", "2"]

Note that this differs from the OP in that since now EBS volumes are NVMe and you need to refer to them using the EBS volume id since they can get enumerate differently)

cloud-init < 24.2

If you can't use cloud-init 24.2, you can achieve the same effect in a slightly less elegant way by using x-systemd.makefs mount option to format instead of using fs_setup at the expense of losing the disk partition label:

#cloud-config
device_aliases:
  disk1: /dev/disk/by-id/nvme-Amazon_Elastic_Block_Store_vol0a250869ccd411b30
disk_setup:
  disk1:
    table_type: gpt
    layout: [50,25,25]
    overwrite: true
mounts:
  - [ /dev/disk/by-id/nvme-Amazon_Elastic_Block_Store_vol0a250869ccd411b30-part1, /earth, xfs, "defaults,nofail,x-systemd.device-timeout=30s,x-systemd.makefs"]
  - [ /dev/disk/by-id/nvme-Amazon_Elastic_Block_Store_vol0a250869ccd411b30-part2, /mars, xfs, "defaults,nofail,x-systemd.device-timeout=30s,x-systemd.makefs"]
  - [ /dev/disk/by-id/nvme-Amazon_Elastic_Block_Store_vol0a250869ccd411b30-part3, /venus, xfs, "defaults,nofail,x-systemd.device-timeout=30s,x-systemd.makefs"]
mounts_default_fields: [ None, None, "auto", "defaults,nofail", "0", "2"]