diff --git a/create-lx-container.sh b/create-lx-container.sh index 9702238..ab46206 100755 --- a/create-lx-container.sh +++ b/create-lx-container.sh @@ -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--/', 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 # ---