Using cloud-init with AHV command line

TL;DR

  • Using cloud-init with AHV is conceptually identical to using KVM/QEMU- we need to use a few different tools with AHV
  • You will need a Linux image that is configured to use cloud-init. A good source is cloud-images.ubuntu.com
  • We will create a cloud-init textual file and create a mountable version using the cloud-localds tool on a Linux host
  • We will attach the cloud-init enabled ubuntu image and our cloud-init customization file to the VM at boot time
  • At boottime ubuntu will access the cloud-init data mounted as a CDROM and do the customization for us

Steps

  • Download an appropriate image into the AHV container repository – so that we can clone from it in a later step
#From any CVM in the cluster
acli image.create ubuntu-cloud-image-20.04-server-cloudinit image_type=kDiskImage source_url=https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img container=imagectr
  • Create a cloud-init file on a Nutanix container. The easiest way to do this is to mount a container to a remote Linux host by editing the NFS whitelist then simply mount the NFS export. In my example the CVM is at 10.56.66.36 – the container is named compress_ctr and I mount it on my Linux box at /cvm-compress
#On any CVM
<ncli> container add-to-nfs-whitelist name=compress_ctr ip-subnet-masks="10.57.16.250/255.255.252.0"
#On a Linux Host
$ sudo mount -o nolock 10.56.66.36:/compress_ctr /cvm-compress/
  • Now create whatever cloud-init file you want to use to customize the image. In my example I create a password for the default ubuntu user and allow logins. I save this file in the /cvm-compress mountpoint then run cloud-localds to turn the textfile into a mountable image
$ cd /cvm-compress
# Now create the cloud-init text file
$ cat >user-data <<EOF
#cloud-config
password: asdfqwer
chpasswd: { expire: False }
ssh_pwauth: True
EOF
# create the image file in .img format - you may need to install a package
# $ sudo apt install cloud-image-utils
$ cloud-localds user-data.img user-data
$ ls /cvm-compress
user-data user-data.img

At this point we have a cloud-init enabled Linux disk image in the AHV image repository – and a cloud-init file in both text and disk-image format accessible to the CVM because the files are on an AOS container.

The magic to get this to work is that we add the disk-image version of the cloud-init file as a CDROM to the VM as the second disk.

#Use acli to create a VM to attach the disks to.
$ acli
<acropolis> vm.create test1-cloud-init-blog num_vcpus=4 memory=8G

#Attach the boot disk image that we downloaded earlier - this image knows to look for cloud-init data
<acropolis> vm.disk_create test1-cloud-init-blog clone_from_image=ubuntu-cloud-image-20.04-server-cloudinit container=testctr bus=scsi

#Attach the disk image version of the cloud init datafile as a CDROM
<acropolis> vm.disk_create test1-cloud-init-blog cdrom=true clone_from_adsf_file=/compress_ctr/user-data.img

#Boot the VM
<acropolis> vm.on test1-cloud-init-blog
  • Once the VM is booted up you can access the console using the username ubuntu and the password asdfqwer as specified in the cloud-init file

Bonus – Adding nutanix user and enabling ssh

This cloud-init file enables ssh and adds a nutanix user. I pass in my public key so I can connect to the nutanix user.

  1. Get the public key for this user
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC3JOFLyhdseXqTmcWllnnKnNiEMON6ynmSaNjJg5O9dgBMk8wjIDq8mj14FZ8XubSFwUOR7wfGJunEmqFcmDWwI1nDmrjuF5xEi6Vok+OohWG1QCubNIvrfJo5lxPQC7yJL3VmCcOPJEDkDHIWtTesZkYh4gqXIoRMT2BgfdGiuuyA+FHRlBUd8ogOEFYLzXgIkSVddU/QkfcZaSd1hyk35+bJqjvQNxEq5kzssZn4Hke2bjeaCpsM8/c2lnv8vTCs12tySOlehOK9jmPWDmQueU+o/7j+H/XozHxWrC9NMdCfxf7e4LyeA8dOhQElVBjMvfC2BFDrUxJTRlGzX96dH+1FrkmdKmSpNcAzreysyU3W3ou3wznZ9aeu/a9wTuEmhGsSKmrIdTgD4IRWOEHAzI6/He1Q8UormIqZEv6jIRVK4ttxCs//JukA5URIAi3nS8+0A4ryqMiPSHz0FmwtEjy4JDoBN2i0pix60KhfVwn8m6mP2+eKypcFyHSWDSE= gary@gary-perflab
  1. Create the cloud-init text file. This cloud config creates the nutanix user and creates the password as nutanix/4u. It also adds the public key for my user.
#cloud-config
---
ssh_pwauth: true
users:
-   lock-passwd: false
    name: nutanix
    passwd: $6$c77AnqxhStcbnZis$reWKWl9Jm7pkTtzl5QR52wnU1jYEStqHqhEV1wE1OUd.6tj.RgpFsQqlnRcNXoEJJ6sXLydo/jt2flv3C48PJ1
    shell: /bin/bash
    ssh_authorized_keys: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC3JOFLyhdseXqTmcWllnnKnNiEMON6ynmSaNjJg5O9dgBMk8wjIDq8mj14FZ8XubSFwUOR7wfGJunEmqFcmDWwI1nDmrjuF5xEi6Vok+OohWG1QCubNIvrfJo5lxPQC7yJL3VmCcOPJEDkDHIWtTesZkYh4gqXIoRMT2BgfdGiuuyA+FHRlBUd8ogOEFYLzXgIkSVddU/QkfcZaSd1hyk35+bJqjvQNxEq5kzssZn4Hke2bjeaCpsM8/c2lnv8vTCs12tySOlehOK9jmPWDmQueU+o/7j+H/XozHxWrC9NMdCfxf7e4LyeA8dOhQElVBjMvfC2BFDrUxJTRlGzX96dH+1FrkmdKmSpNcAzreysyU3W3ou3wznZ9aeu/a9wTuEmhGsSKmrIdTgD4IRWOEHAzI6/He1Q8UormIqZEv6jIRVK4ttxCs//JukA5URIAi3nS8+0A4ryqMiPSHz0FmwtEjy4JDoBN2i0pix60KhfVwn8m6mP2+eKypcFyHSWDSE= gary@gary-perflab
    sudo: ALL=(ALL) NOPASSWD:ALL
  1. Create the binary image from the above text file gary@gary-perflab:/cvm-compress$ cloud-localds user-data-nutanix-ssh.img user-data-nutanix-ssh
  2. Create a VM in acli and attach the user-data image file to the vm using the ADSF path /compress_ctr/user-data-nutanix-ssh.img
<acropolis> vm.create test4-cloud-init-blog num_vcpus=4 memory=8G
<acropolis> vm.disk_create test4-cloud-init-blog clone_from_image=ubuntu-cloud-image-20.04-server-cloudinit  bus=scsi
<acropolis> vm.disk_create test4-cloud-init-blog cdrom=true clone_from_adsf_file=/compress_ctr/user-data-nutanix-ssh.img
<acropolis> vm.nic_create test4-cloud-init-blog network=DHCP162
<acropolis> vm.on test4-cloud-init-blog
  1. Connect to the vm via ssh using either keys or username/password
gary@gary-perflab:/cvm-compress$ ssh nutanix@10.57.72.38
Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.15.0-46-generic x86_64)
...
nutanix@ubuntu:~$

or using passwords

garymbp:notes gary$ ssh nutanix@10.57.72.38
Warning: Permanently added '10.57.72.38' (ED25519) to the list of known hosts.
nutanix@10.57.72.38's password:
Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.15.0-46-generic x86_64)
...
nutanix@ubuntu:~$

Leave a Comment