2

I am trying to make a template for my Ubuntu VMs mostly by following Techno Tim's instructions here. I differ from Tim by wanting to also install qemu-guest-agent so I can get the IP of the vm without having to do it manually. I do this before importing the .img by using the command virt-customize -a focal-minimal-cloudimg-amd64.img --install qemu-guest-agent. But unfortunately this does me the favor of setting the machine-id, which I do not want in my template. Now, I could also run virt-customize -a focal-minimal-cloudimg-amd64.img --delete /etc/machine-id as well as in /var/lib/dbus/machine-id which does the trick and deletes it. But unfortunately during boot of any new VM from that, the system boots with many errors because machine-id is not there and for some reason /etc is mounted as read only. Is there any way to fix this?

I can get around this by instead of deleting with virt-customize, I can put in a firstboot script through virt-customize that deletes and regenerates a machine-id with a reboot to make it take effect, but this seems like a less than ideal way to do this. Is there a better way?

1 Answer 1

5

Do not delete /etc/machine-id. Instead, truncate it with a command like truncate -s 0 /etc/machine-id /var/lib/dbus/machine-id. The machine-id file does not get created if it is deleted, but it will get populated with a new value if it is empty.

links

Indeed /etc/machine-id must at least exist (empty is okay) in order to regenerate it -- then systemd will create a new one in a temporary location in /run, bind-mount /etc/machine-id to it, and systemd-machine-id-commit.service will then write it to /etc/machine-id once the fs becomes read-write

2
  • 2
    That did the trick, thanks so much! I used the command virt-customize -a focal-minimal-cloudimg-amd64.img --install qemu-guest-agent --truncate /etc/machine-id in case anyone else needs it. It seemsvirt-customize doesn't create /var/lib/dbus/machine-id because it said it didn't exist when i tried to delete it. Commented May 27, 2022 at 18:11
  • While facing an IP conflict between two VMs sharing the same cloud-init template, --truncate /etc/machine-id helped me a lot. To be sure the template is completely clean and stateless I also added --run-command 'cloud-init clean --logs'--run-command 'rm -rf /var/lib/cloud/'. Commented Jun 18 at 15:49

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.