n0derunner

    Using cloud-init with AHV command line

    Published: (Updated: ) in AHV, , by .

    TL;DR

    Steps

    #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
    #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/
    $ 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

    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:~$
    

    Comments

    Leave a Comment