[Cloud-Init] Can't find how to set UUID + Hostname with NoCloud + Template

TeBeCo

Member
Jul 6, 2022
1
0
6
Disclamer:
I never used Proxmox ever or QEMU / qm / cloud-init until about 1 weeks ago.
There's probably things to do in a better way, feel free to send how you would have done the same (no ansible / terraform for this, I'm looking it aside that one)

hello


I'm trying to script the creation of a template and at the same time of multiple VMs
* VM must have one static IP on route range (internet)
* VM must have one static IP / other to communicate together (virtual bridge created)
* VM must have a predicatable hostname and unique
* VM must have a sudo cat /sys/class/dmi/id/product_uuid

removing the uuid key from the --smbios1 command will preserve the hostname

I'm using a cloud-init + ds=nocloud base image to do that (tried ubuntu and debian)
I used to do:
qm set $VMID --smbios1 serial=$(echo -n "ds=nocloud;hostname=$HOSTNAME" | base64),base64=1
in order to set the hostname and it did work
issue is that the uuid is not automatically generated and thus the file /sys/class/dmi/id/product_uuid is not existing
(which causes potential issue for kubeadm)
I tried to change the command to:
qm set $VMID --smbios1 uuid=$(uuidgen),serial=$(echo -n "ds=nocloud;hostname=$HOSTNAME" | base64),base64=1
I now have:
Code:
> sudo cat /sys/class/dmi/id/product_uuid
d370591a-b367-44d3-9759-1f72b2e80c1c
Code:
> sudo dmidecode -t system -t baseboard
# dmidecode 3.2
Getting SMBIOS data from sysfs.
SMBIOS 2.8 present.
Handle 0x0100, DMI type 1, 27 bytes
System Information
        Manufacturer: QEMU
        Product Name: Standard PC (i440FX + PIIX, 1996)
        Version: pc-i440fx-6.2
        Serial Number: ds=nocloud;hostname=kube-control-plane-01 <==========
        UUID: d370591a-b367-44d3-9759-1f72b2e80c1c <==========
        Wake-up Type: Power Switch
        SKU Number: Not Specified
        Family: Not Specified
Handle 0x2000, DMI type 32, 11 bytes
System Boot Information
        Status: No errors detected
but also ...
Code:
> echo $HOSTNAME
ubuntu
> cat /etc/hostname
ubuntu
it's not anymore using kube-control-plane-01

yes it's just a script / not ansible / terraform etc ... one thing at a time
Script:
Code:
#!/bin/bash
DEBIAN_IMGDISK="/root/debian-11-genericcloud-amd64.raw"
UBUNU_IMGDISK="/root/focal-server-cloudimg-amd64.img"
IMGDISK=$DEBIAN_IMGDISK
FORCE=1
TMPL_ID=9000
TMPL_STORAGE="local-lvm"
CPLANE_COUNT=1
CPLANE_VMNAME="kube-control-plane-0"
CPLANE_VMID_BASE=1000
CPLANE_LAST_OCTET_START=10
NODE_COUNT=2
NODE_VMNAME="kube-node-0"
NODE_VMID_BASE=1100
NODE_LAST_OCTET_START=20
if [ $FORCE == 1 ]
then
  qm destroy $TMPL_ID -purge -destroy-unreferenced-disks 1
  for (( i=0; i<$CPLANE_COUNT; i++ ))
  do
    qm stop $(($i + $CPLANE_VMID_BASE))
    qm destroy $(($i + $CPLANE_VMID_BASE)) -purge -destroy-unreferenced-disks 1
  done
  for (( i=0; i<$NODE_COUNT; i++ ))
  do
    qm stop $(($i + $NODE_VMID_BASE))
    qm destroy $(($i + $NODE_VMID_BASE)) -purge -destroy-unreferenced-disks 1
  done
fi
if [ ! -f /etc/pve/nodes/proxmox/qemu-server/$TMPL_ID.conf ]
then
  cp -f userconfig-base.yml /var/lib/vz/snippets/userconfig-base.yml
  qm create $TMPL_ID --name template \
      --memory 2048 \
      --cores 2 \
      --net0 virtio,bridge=vmbr0 \
      --net1 virtio,bridge=vmbr1
  qm importdisk $TMPL_ID $UBUNU_IMGDISK $TMPL_STORAGE
  qm set $TMPL_ID --ostype l26
  qm set $TMPL_ID --scsihw virtio-scsi-pci --scsi0 $TMPL_STORAGE:vm-$TMPL_ID-disk-0,size=32G
  qm set $TMPL_ID --boot c --bootdisk scsi0
  qm set $TMPL_ID --ide2 $TMPL_STORAGE:cloudinit
  qm set $TMPL_ID --serial0 socket --vga serial0
  qm set $TMPL_ID --agent enabled=1
  qm set $TMPL_ID --cicustom "user=local:snippets/userconfig-base.yml"
  qm set $TMPL_ID --nameserver 192.168.104.1
  qm template $TMPL_ID
fi
############# CONTROL PLANE #############
for (( i=0; i<$CPLANE_COUNT; i++ ))
do
  HOSTNAME="$CPLANE_VMNAME$(($i + 1))"
  VMID=$(($CPLANE_VMID_BASE + $i))
  LAST_OCTET=$(($CPLANE_LAST_OCTET_START + $i))
  INSTANCEID=$(uuidgen)

  echo $HOSTNAME
  qm clone $TMPL_ID $VMID --name $HOSTNAME
  qm set $VMID --ipconfig0 ip=192.168.104.$LAST_OCTET/24,gw=192.168.104.1
  qm set $VMID --ipconfig1 ip=10.0.0.$LAST_OCTET/24
  # sudo cat /sys/class/dmi/id/product_uuid;sudo dmidecode -t system -t baseboard
  qm set $VMID --smbios1 uuid=$(uuidgen),serial=$(echo -n "ds=nocloud;hostname=$HOSTNAME" | base64),base64=1
  # qm resize $VMID scsi0 32G
  qm start $VMID
done

# uncomment if you want worker nodes
# ############# NODE POOL #############
# for (( i=0; i<$NODE_COUNT; i++ ))
# do
#   HOSTNAME="$NODE_VMNAME$(($i + 1))"
#   VMID=$(($NODE_VMID_BASE + $i))
#   LAST_OCTET=$(($NODE_LAST_OCTET_START + $i))
#   INSTANCEID=$(uuidgen)
#   echo $HOSTNAME
#   qm clone $TMPL_ID $VMID --name $HOSTNAME
#   qm set $VMID --ipconfig0 ip=192.168.104.$LAST_OCTET/24,gw=192.168.104.1
#   qm set $VMID --ipconfig1 ip=10.0.0.$LAST_OCTET/24
#   qm set $VMID --smbios1 base64=1,serial=$(echo -n "ds=nocloud;h=$HOSTNAME" | base64)
#   # qm resize $VMID scsi0 32G
#   qm start $VMID
# done

the userconfig-base.yml
Code:
#cloud-config
users:
  - name: root
    shell: "/bin/bash"
    ssh_authorized_keys:
      - ecdsa-sha2-nistp521 xxxx
  - name: foobar
    sudo: ALL=(ALL) NOPASSWD:ALL
    shell: "/bin/bash"
    ssh_authorized_keys:
      - ecdsa-sha2-nistp521 xxxx

write_files:
  - path: /etc/sysctl.d/50-disable-ipv6.conf
    content: |
      net.ipv6.conf.all.disable_ipv6=1
      net.ipv6.conf.default.disable_ipv6=1

runcmd:
  - systemctl start qemu-guest-agent
  - sysctl -p /etc/sysctl.d/50-disable-ipv6.conf

package_update: true
package_upgrade: true
packages:
  - qemu-guest-agent
  - net-tools

apt:
  conf: | # APT config
    APT {
      Get {
        Assume-Yes "true";
        Fix-Broken "true";
      };
    };
 
Last edited: