Score:0

Cloud-init in kvm guests (Debian and Centos) not setting interface settings

ng flag

Im trying to use cloud-init to setup kvm guests (currently Debian 11 and CentOS Stream 8), and I am looking for help in correcting the meta-data and user-data files. Everything else seems to work, but network interface settings aren't set, though I do see cloud-init logs showing that they are. Please see below:

For debian:

    cat << EOF > /var/lib/libvirt/images/$meta_data_file
instance-id: $vm_name
local-hostname: $vm_name
hostname: $vm_name
fqdn: $vm_name
manage_etc_hosts: true
EOF

cat << EOF > /var/lib/libvirt/images/$cloud_config_file
#cloud-config

# Hostname management
preserve_hostname: false
hostname: $vm_name
fqdn: $vm_name
network:
  version: 2
  ethernets:
    eth0:
      match:
        name: e*
      dhcp4: false
      addresses: 
        - 10.10.0.25/24
      gateway4: 10.10.0.254
      nameservers:
        addresses:
          - 10.50.0.23
          - 10.50.0.17
          - 10.50.0.18
      search: [testing,production,admin,internal]
      routes:
        - to: 10.50.0.0/24
          via: 10.10.0.249
users:
  - default
  - name: admin
    sudo: ALL=(ALL) NOPASSWD:ALL
    groups: wheel, sudo, admin
    home: /home/admin
    shell: /bin/bash
    hashed_passwd: $adminpasswd
    lock_passwd: false
    ssh_pwauth: True
    chpasswd: { expire: False }
    ssh-authorized-keys:
      - ssh-rsa ...
      - ssh-rsa ...
# only cert auth via ssh (console access can still login)
ssh_pwauth: True
disable_root: false
chpasswd:
  list: |
     root:$rtpwd
  expire: False
runcmd:
  # disable dhcp for eth0
  - [ sh, -c, sed -e '/iface eth0 inet dhcp/s/^/#/g' -i /etc/network/interfaces ]
bootcmd:
  - cloud-init-per always fix-debian-autonet rm /etc/udev/rules.d/75-cloud-ifupdown.rules
    - cloud-init-per always fix-debian-netconfig rm /run/network/interfaces.d/*
  - cloud-init-per once ifdown ifdown ens3
  - cloud-init-per once bugfix rm /run/network/interfaces.d/ens3
  - cloud-init-per once ifup ifup ens3

# Configure where output will go
output:
  all: ">> /var/log/cloud-init.log"
# configure interaction with ssh server
ssh_svcname: ssh
ssh_deletekeys: True
ssh_genkeytypes: ['rsa', 'ecdsa']
package_update: true
package_upgrade: true
packages:
  - bind9-utils
  - vim
  - freeipa-client
  - cloud-utils-growpart
power_state:
  delay: "+2" #minutes
  mode: reboot
  message: Run completed
  timeout: 120 #seconds
  condition: True
EOF

For Centos Stream 8:

cat << EOF > /var/lib/libvirt/images/$cloud_config_file
#cloud-config

# Hostname management
preserve_hostname: false
hostname: $vm_name
fqdn: $vm_name
network:
  version: 2
  ethernets:
    eth0:
      match:
        name: e*
      dhcp4: false
      addresses: 
        - 10.50.0.26/24
      gateway4: 10.50.0.254
      nameservers:
        addresses:
          - 10.15.0.23
          - 10.15.0.17
          - 10.15.0.18
      search: [testing,production,admin,internal]
      routes:
        - to: 10.15.0.0/24
          via: 10.50.0.249

users:
  - default
  - name: admin
    sudo: ALL=(ALL) NOPASSWD:ALL
    groups: wheel, sudo, admin
    home: /home/admin
    shell: /bin/bash
    hashed_passwd: $adminpasswd
    lock_passwd: false
    ssh_pwauth: True
    chpasswd: { expire: False }
    ssh-authorized-keys:
      - ssh-rsa ...
      - ssh-rsa ...
ssh_pwauth: True
disable_root: false
chpasswd:
  list: |
     root:$rtpwd
  expire: False

# Configure where output will go
output:
  all: ">> /var/log/cloud-init.log"
# configure interaction with ssh server
ssh_svcname: ssh
ssh_deletekeys: True
ssh_genkeytypes: ['rsa', 'ecdsa']
package_update: true
package_upgrade: true
packages:
  - bind9-utils
  - vim
  - freeipa-client
  - cloud-utils-growpart
power_state:
  delay: "+2" #minutes
  mode: reboot
  message: Run completed
  timeout: 120 #seconds
  condition: True
EOF

Please what am I doing wrong?

Score:0
ng flag

My first mistake appears to be adding network configuration to user-data. Got it working with a separate config file for network(network-config) and appropriate ENI versions for debian and centos.

For debian:

cat << EOF > $network_config
  version: 1
  config:
    - type: physical
      name: eth0
      subnets:
        - type: static
          address: 10.10.0.26
          gateway: 10.10.0.254
    - type: route
      destination: 10.100.0.0/24
      gateway: 10.10.0.249
    - type: nameserver
      address:
        - 10.100.0.23
        - 10.100.0.17
        - 10.100.0.18
      search:
        - testing.mydom
        - production.mydom
        - admin.mydom
EOF

For centos:

cat << EOF > $network_config
  version: 1
  config:
    - type: physical
      name: eth0
      subnets:
        - type: static
          address: 10.10.0.26
          gateway: 10.10.0.254
    - type: route
      destination: 10.100.0.0/24
      gateway: 10.10.0.249
    - type: nameserver
      address:
        - 10.100.0.23
        - 10.100.0.17
        - 10.100.0.18
      search:
        - testing.mydom
        - production.mydom
        - admin.mydom
EOF

We'll see what happens for Ubuntu images (and to fix package 'updation' and installation).

SinaOwolabi avatar
ng flag
Debian config works fine for Ubuntu cloud images as well (tried with jammy images).
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.