Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i use cloud-init to load a datadisk on an ubuntu VM in azure

I have been trying use cloud-init to partition and mount a datadisk on a Ubuntu VM in azure. Most of the online examples show how to do that for the temporary disk but not for data disk. I tried dabbling around with it with out much luck. Please let me know if I am doing something wrong in the below code or if its not possible to do it with cloud-init

#cloud-config
device_aliases: {'ephemeral0': '/dev/sdb','datadisk': '/dev/sdc1'}

disk_setup:
    ephemeral0:
         table_type: mbr
         layout: True
         overwrite: False
    /dev/sdc1:
         table_type: mbr
         layout: True
         overwrite: False

fs_setup:
    - label: ephemeral0
      filesystem: ext4
      device: ephemeral0.1
      replace_fs: ntfs
    - cmd: mkfs -t %(filesystem)s -L %(label)s %(device)s
      label: '/dev/sdc1/'
      filesystem: ext4
      device: '/dev/sdc1/'
      replace_fs: ntfs

mounts:
    - ["ephemeral0.1", "/mnt"]
    - ["/dev/sdc1/", "/datadisk"]
like image 993
Teja Avatar asked Dec 13 '22 13:12

Teja


1 Answers

I agree -- not many examples of this common scenario. I think part of the problem you're facing above is that you're referencing a partition instead of a disk in disk_setup.

With Azure, the first data disk attached to the VM will usually be identified as /dev/sdc, the second will be /dev/sdd, and so on. This isn't guaranteed, however. The documentation here indicates that there are circumstances which can result in a different drive letter being assigned. As a result, we'll reference the disks using a build-in alias. This alias is guarantee to always be mapped using the LUN assigned in the ARM template (or disk definition). These aliases follow the form /dev/disk/azure/scsi1/lun# (with partitions aliased as /dev/disk/azure/scsi1/lun#-part#).

If you're using ARM, the template will include a reference in the VM definition to the drive. As part of that definition, you will specify the LUN value. You can reference that assigned value in your cloud-init. For example, the following ARM snippet will create /dev/disk/azure/scsi1/lun0:

"dataDisks": [
{
    "lun": 0,
    "name": "[concat(variables('vmName'),'-datadisk0')]",
    "createOption": "Attach",
    "managedDisk":
    {
        "id": "[resourceId('Microsoft.Compute/disks/', 
                concat(variables('vmName'),'-datadisk0'))]"
    }
},

Knowing this, we can construct the contents for a cloud-config. First, we define the data disk. I suggest using GPT as the table type for to enable support for disks and partitions that are > 2TiB.

disk_setup:
    /dev/disk/azure/scsi1/lun0:
        table_type: gpt
        layout: True
        overwrite: True

Next, we specify the file system setup for the disks. We reference each partition and declare the file system to be used.

fs_setup:
    - device: /dev/disk/azure/scsi1/lun0
      partition: 1
      filesystem: ext4

Finally, we mount the partitions. The process used by cloud-init will create the folders and mount the specified partitions. I'm using the recommended nofail (ensures the VM can boot in the event of issues or a detached drive) along with noexec (which prevents executing binaries on that partition). Since we've placed the file system on the first partition of lun0, we need to mount lun0-part1.

mounts:
    - ["/dev/disk/azure/scsi1/lun0-part1", "/datadisk", auto, "defaults,noexec,nofail"]
like image 96
Ken Muse Avatar answered Feb 05 '23 14:02

Ken Muse