2

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.

1
  • Your metadata file looks incorrect: everything under enp1s0 needs to be indented.
    – larsks
    Commented May 1, 2023 at 11:45

1 Answer 1

2

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.)

3
  • 2
    Thanks larsks. The --cloud-init is extra dope! For those who wish to see more details and explanations, I have written about it here: techchorus.net/blog/… Commented May 1, 2023 at 16:23
  • I'm pretty sure ALL the cloud config files must have "#cloud-config" as their first line.
    – mr.zog
    Commented Apr 10 at 23:55
  • @mr.zog that's incorrect. Only the user-data blob needs to start with #cloud-config (if it is a cloud-config format file; an appropriate shebang line if it's a script).
    – larsks
    Commented Apr 11 at 0:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .