create-lx-container.sh: give each new container its own machine-id.

The debian template copies the rootfs 1:1 from the cache
(/var/cache/lxc/debian/rootfs-<release>-<arch>/), including the cached
/etc/machine-id. All containers created from the same cache therefore
shared the same machine-id.

After lxc-create, write a new random machine-id into the container's
rootfs and make /var/lib/dbus/machine-id a symlink to /etc/machine-id.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrisandClaude Opus 5.5 committed 2026-11-21 16:15:20 +01:00
1 parent 0fb729fa2f
commit 80739344bf
1 file changed
+45
+45
View File
@@ -921,6 +921,51 @@ else
fi
# ---
# - Give the new LX Container its own machine-id
# -
# - The debian template copies the rootfs 1:1 (rsync) from the cache
# - '/var/cache/lxc/debian/rootfs-<release>-<arch>/', including the cached
# - file '/etc/machine-id'. Without this step all containers created from
# - the same cache share the same machine-id.
# -
# - The new ID is generated randomly here (not with systemd-machine-id-setup,
# - which might take over a VM-UUID).
# ---
_machine_id_file="${LXC_CONTAINER_DIR}/rootfs/etc/machine-id"
_dbus_machine_id_file="${LXC_CONTAINER_DIR}/rootfs/var/lib/dbus/machine-id"
msg="Set new machine-id for Lx Container '$LXC_NAME' .."
length_msg=${#msg}
echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m"
_new_machine_id="$(tr -d '-' < /proc/sys/kernel/random/uuid)"
# - Remove first: never write through a (possibly absolute) symlink,
# - which would point to the host's /etc/machine-id.
if [[ "$_new_machine_id" =~ ^[0-9a-f]{32}$ ]] \
&& rm -f "$_machine_id_file" 2> $err_msg \
&& echo "$_new_machine_id" > "$_machine_id_file" 2>> $err_msg \
&& chmod 0444 "$_machine_id_file" 2>> $err_msg ; then
if [[ -d "$(dirname "$_dbus_machine_id_file")" ]] ; then
rm -f "$_dbus_machine_id_file"
ln -s /etc/machine-id "$_dbus_machine_id_file" 2>> $err_msg
fi
msg_ok="New machine-id '${_new_machine_id}' set."
printf "\033[1G [ \033[32m\033[1mok\033[m ] %-${length_msg}s\n" "$msg_ok"
else
msg_failed="Setting a new machine-id for '$LXC_NAME' failed!"
printf "\033[1G [ \033[31m\033[1mfailed\033[m ] %-${length_msg}s\n" "$msg_failed"
warn "$(cat $err_msg)
The container will share its machine-id with all other containers
created from the same cache. Fix it later with:
/root/bin/fix-machine-id.sh --force --apply (inside the container)"
fi
# ---
# - Configure the new LX Container
# ---