[SOLVED] Can't get cloud init to work

Dec 13, 2021
24
6
8
Hi I have created the following script to create a VM and with a cloud-init config:
Bash:
#!/bin/bash

echo "Loading variables"
VMID=230
STORAGE=storage
IMAGE=debian-11-genericcloud-amd64.qcow2
SNIPPETNAME=debian-docker-cloud-init.yml
echo "VMID: $VMID"
echo "STORAGE: $STORAGE"
echo "IMAGE: $IMAGE"
echo "SNIPPETNAME: $SNIPPETNAME"


echo "Downloading image"
wget -P /tmp https://cloud.debian.org/images/cloud/bullseye/latest/$IMAGE

echo "Checking if VM exists"
VMSTATUS=$(qm status $VMID)
if [[ $? -eq 0 ]]
then
    echo "VM already exists checking if its running"
    if [[ "$VMSTATUS" = "status: running" ]]
    then
        echo "Stopping vm"
        qm stop $VMID
    fi
    echo "Removing OS disk"
    qm set $VMID --delete scsi1
    qm set $VMID --delete unused0
else
    echo "Creating vm"
    qm create $VMID --name "debian-docker" --memory 10240 --sockets 1 --core 4 --net0 virtio="46:4A:5E:3C:8A:49",bridge=vmbr0 --description "Debian bullseye cloud image"

    echo "Adding data disk"
    qm set $VMID --scsi0 $STORAGE:64
    
    echo "Adding cloud-init device"
    qm set $VMID --ide2 $STORAGE:cloudinit

    echo "Add vga serial"
    qm set $VMID --serial0 socket --vga serial0
fi

echo "Importing OS disk"
qm importdisk $VMID /tmp/$IMAGE $STORAGE

echo "Removing image"
rm /tmp/$IMAGE

echo "Adding OS disk to vm"
qm set $VMID --scsihw virtio-scsi-pci --scsi1 $STORAGE:vm-$VMID-disk-1

echo "Resizing OS disk"
qm resize $VMID scsi1 +5G

echo "Installing cloud-init snippet"
cp cloud-init.yml /storage/snippets/snippets/$SNIPPETNAME

echo "Set cloud init config"
qm set $VMID --cicustom "user=snippets:snippets/$SNIPPETNAME"

echo "Set boot disk"
qm set $VMID --boot c --bootdisk scsi1

echo "Starting vm"
qm start $VMID

Then I have the following cloud-init.yml file
YAML:
hostname: debian-docker
manage_etc_hosts: true
ssh:
  install-server: true
  allow-pw: true
  disable_root: false
  ssh_quiet_keygen: true
  allow_public_ssh_keys: true
chpasswd:
  expire: False
users:
  - default
  - name: peter
    gecos: peter
    groups: adm, users, sudo, docker
    lock_passwd: false
    sudo: ALL=(ALL) NOPASSWD:ALL
    shell: /bin/bash
    passwd: "$6$qwdd8gt3bTs2HGut$FwwV5Q4kNycOipS0VMctcaJY06uVChnd8ST5TFtsioex1y9yWLKj.LreUm0r0x8fqldNM6sx5n.QctlaxGUBl1"
  - name: root
    lock_passwd: false
    hashed_passwd: "$6$qwdd8gt3bTs2HGut$FwwV5Q4kNycOipS0VMctcaJY06uVChnd8ST5TFtsioex1y9yWLKj.LreUm0r0x8fqldNM6sx5n.QctlaxGUBl1"

I generated the password hash using echo peter| mkpasswd -m sha-512 -s but no matter what I do I can't login, not with the terminal in proxmox and not using ssh (putty).

What am I doing wrong? (How do I verify that my cloud init file was run?)
 
One way to check if the file is run is to start terminal immediately after VM start, ie "qm start 100 && qm terminal 100". Then watch the console messages.

I dont think that your config is correct - there are too many layers in your paths. You should be seeing your custom file when you do:
pvesm list storage

Its unclear what is happening with your storage as well. You appear to have "storage" and then reference another storage called "snippets", where you have a subfolder...

Start with output of "pvesm status", follow with "list".

The correct path would be : cicustom: user=filestoragename:snippets/filename



Blockbridge : Ultra low latency all-NVME shared storage for Proxmox - https://www.blockbridge.com/proxmox
 
  • Like
Reactions: PeterAndersson
Hi thanks for your quick reply.
  1. One way to check if the file is run is to start terminal immediately after VM start, ie "qm start 100 && qm terminal 100". Then watch the console messages. I have attached a file with the output (not sure i was able to capture all of it.) I can see some cloud init output but I'm somewhat unsure what to look for. (But i can't see any lines that seem to create any users (not root and not peter)
  2. I dont think that your config is correct - there are too many layers in your paths. I'm 99% sure that its correct as if i change any part of the path i get a file not found error (also I can see my file in the ui)
  3. You should be seeing your custom file when you do:pvesm list storage i get the following output
    Code:
    Volid                     Format  Type              Size VMID
    storage:subvol-210-disk-0 subvol  rootdir    34359738368 210
    storage:vm-100-disk-0     raw     images    137438953472 100
    storage:vm-230-cloudinit  raw     images         4194304 230
    storage:vm-230-disk-0     raw     images     68719476736 230
    storage:vm-230-disk-1     raw     images      7516192768 230
  4. Start with output of "pvesm status",
    Code:
    Name             Type     Status           Total            Used       Available        %
    local             dir     active        60837724         4536752        53178184    7.46%
    local-lvm     lvmthin   disabled               0               0               0      N/A
    snippets          dir     active      7488904960             256      7488904704    0.00%
    storage       zfspool     active     11422793728      3933889009      7488904718   34.44%
  5. pvesm list snippets
    Code:
    Volid                                          Format  Type      Size VMID
    snippets:snippets/debian-docker-cloud-init.yml snippet snippets  1722
 

Attachments

Last edited:
your problem is right here:
Code:
 6.654154] cloud-init[500]: 2022-09-23 07:07:38,315 - __init__.py[WARNING]: Unhandled non-multipart (text/x-not-multipart) userdata: 'b'hostname: debian-docker'...'

this is when execution of userdata fails
https://cloudinit.readthedocs.io/en/latest/topics/format.html
Code:
Begins with: #cloud-config or Content-Type: text/cloud-config when using a MIME archive.

add #cloud-config as first line


Blockbridge : Ultra low latency all-NVME shared storage for Proxmox - https://www.blockbridge.com/proxmox
 
  • Like
Reactions: PeterAndersson