Score:2

cloud-init ignores staitc networking configuration

in flag

I am performing these steps on Ubuntu 22.04 host:

Download the cloud image

qemu-img create -b ../jammy-server-cloudimg-amd64.img -f qcow2 -F qcow2 myubuntu2204test01-base.img 10G

Create the file meta-data in the directory myubuntu2204test01:

instance-id: myubuntu2204test01
local-hostname: myubuntu2204test01.example.com
network:
  version: 2
  ethernets:
    enp1s0: 
    dhcp4: no
    addresses: [192.168.122.146/24]
    nameservers:
         addresses: [192.168.122.1]
    routes:
    - to: 0.0.0.0/0
      via: 192.168.122.1

Create the file user-data in the directory myubuntu2204test01:

#cloud-config
users:
  - name: someuser
    ssh_authorized_keys:
      - ssh-ed25519 somekey comment
    sudo: ["ALL=(ALL) NOPASSWD:ALL"]
    groups: sudo
    shell: /bin/bash

Generate the ISO image for ci data

genisoimage -output cidata.iso -V cidata -r -J user-data meta-data

Create The VM myubuntu2204test01

virt-install \
--name=testvmubuntu2204 \
--ram=512 --vcpus=1 \
--import \
--disk path=myubuntu2204test01-base.img,format=qcow2 \
--disk path=cidata.iso,device=cdrom \
--os-variant=ubuntu22.04 \
--network bridge=virbr0,model=virtio \
--graphics vnc,listen=0.0.0.0 --noautoconsole

The VM is created. But the VM does not have static IP networking as defined in meta-data. The VM has a default DHCP network configuration.

What is missing or incorrect in this process to create static IP for the guest VM?

I have tried defining the networking in both user-data and meta-data files, both of them fails to create static networking for the guest VM.

pt flag
Your metadata file looks incorrect: everything under `enp1s0` needs to be indented.
Score:2
pt flag

According to the cloud-init documentation:

Network configuration can also be provided to cloud-init in either Networking Config Version 1 or Networking Config Version 2 by providing that yaml formatted data in a file named network-config. If found, this file will override a network-interfaces file.

It looks ike you're trying to use a version 2 network configuration; that means you're providing it in the wrong file. You need to add network-config rather than meta-data.

As I mentioned in the comment, you also need to correct the format of the network config file:

instance-id: myubuntu2204test01
local-hostname: myubuntu2204test01.example.com
network:
  version: 2
  ethernets:
    enp1s0:
      dhcp4: no
      addresses: [192.168.122.146/24]
      nameservers:
           addresses: [192.168.122.1]
      routes:
      - to: 0.0.0.0/0
        via: 192.168.122.1

You can simplify your life by using the --cloud-init option to virt-install rather than building the config disk yourself:

virt-install \
--name=testvmubuntu2204 \
--ram=512 \
--vcpus=1 \
--import \
--disk path=myubuntu2204test01-base.img,format=qcow2 \
--os-variant=ubuntu22.04 \
--network bridge=virbr0,model=virtio \
--graphics vnc,listen=0.0.0.0 --noautoconsole \
--cloud-init user-data=user-data.yaml,network-config=network-config.yaml

(This presumes you have files locally named user-dat.ayaml and network-config.yaml; adjust as appropriate.)

Sudheer Satyanarayana avatar
in flag
Thanks larsks. The --cloud-init is extra dope! For those who wish to see more details and explanations, I have written about it here: https://www.techchorus.net/blog/automating-virtual-machine-installation-using-libvirt-virsh-and-cloud-init/
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.