Cloud-Init – Fixing Ignored Static Networking Configuration in Ubuntu 22.04

cloud-initubuntu-22.04

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.

Best Answer

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-data.yaml and network-config.yaml; adjust as appropriate.)

Related Topic