It isn't random. The "randomness" is based on whether 10 minutes have elapsed between building the image, and starting the server – that is why sometimes the same image gives different results.
The issue is that you have not cleared the machine id in the image.
DigitalOcean inject a first-boot script via cloud-init to generate a new machine id if there was one in the base image. The heuristic this script uses to tell if a machine id is baked into the image is to check if the mtime on /etc/machine-id
is more than 10 minutes ago.
/var/lib/cloud/scripts/per-instance/machine_id.sh
:
# record timestamp on machine-id for testing
# If /etc/machine_id is over 10m old on first-boot, delete it
if [ -f /etc/machine-id ]; then
if [ $DIFF -lt 600 ]; then
exit 0
fi
rm -rf /etc/machine-id
fi
The systemd journal stores log files on disk in a directory named after the machine-id, so changing the machine id makes it lose track of those logs.
You can see the logs from before the machine id changed by telling journalctl
to load logs from the old directory, e.g:
journalctl -D /var/log/journal/4ff0f6fdda274a6b9d2b9287b8a15c81
The fix here is to clear the machine-id in your baked images. You can do this by removing /var/lib/dbus/machine-id
and /etc/machine-id
, or alternatively you can use the machine-id script from packer-virt-sysprep, which is a library of useful scripts for preparing a VM to be an image.