0

I'm trying to build a VM using cloud-init and qemu-kvm as hypervisor. I have the following script that builds the VM with user data:

# Create an overlay image
qemu-img create -f qcow2 -b "$CLOUD_BASE_IMG" "$1".img

qemu-img resize "$1".img +22G

# Build seed image with the user data and the networking config
cloud-localds "$CUR_PATH"/seed_"$1".img "$CUR_PATH"/user-data.yaml

# Boot the VM
if [ "$1" == "vm2" ]; then
    sudo qemu-system-x86_64 \
        -hda "$CUR_PATH"/"$1".img \
        -hdb "$CUR_PATH"/seed_"$1".img \
        -m 2G --enable-kvm \
        -serial file:"$1".log \
        -device e1000,netdev=mgmt,mac=00:AA:BB:CC:01:99 -netdev user,id=mgmt,hostfwd=tcp::2022-:22 \
        -device virtio-net-pci,netdev=data1,mac=00:0a:0a:0a:02:01,ioeventfd=on,mrg_rxbuf=on -netdev tap,ifname=vm2.1,id=data1,script=no,downscript=no
fi

This works fine, but now I want to build the VM and configure networking through cloud-init. I've read out there (ref, ref) that with cloud-localds can be passed network configuration like this:

cloud-localds -v --network-config=network-config-v1.yaml \
    seed.img user-data.yaml meta-data.yaml

If I try to do that, the VM loads, but the user data is not loaded properly, and then I cannot access the VM.

Also, I was not able to found any information of -v flag and --network-config flag in official documentation

Any suggestions about how can I pass networking configuration this way?

1 Answers1

1

I'm using a network-config with static addresses to build a seed image too. DHCP resolution needs to be disabled to skip the (60 seconds) timeout and to prevent messing with DNS resolution afterwards.

But the basic network with DHCP is set up before the cloud-init configuration is applied. The template in /etc/network/cloud-interfaces-template is used for that:

auto $INTERFACE
allow-hotplug $INTERFACE

iface $INTERFACE inet dhcp

The best way I found so far to override that template and disable early DHCP is to patch the image before first start, patching the image in e.g. bootcmd cloud-init stage is too late.)

I'm using something like this script to alter the base image:

qemu-nbd --connect=/dev/nbd0 "$CLOUD_BASE_IMG"
fdisk /dev/nbd0 -l
mkdir /tmp/nbd
mount /dev/nbd0p1 /tmp/nbd
sed -i 's/dhcp/manual/' /tmp/nbd/etc/network/cloud-interfaces-template
umount /tmp/nbd
rmdir /tmp/nbd
qemu-nbd --disconnect /dev/nbd0

Not great, I'd prefer a pristine base image. If you are fine with the DHCP timeout, you could fix the interface (and DNS resolution) settings in bootcmd though.

zany
  • 881
  • 1
  • 7
  • 16