]> git.proxmox.com Git - pve-container.git/commitdiff
setup: avoid writing truncated machine-id if it didn't exist
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 26 Apr 2023 14:22:35 +0000 (16:22 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 26 Apr 2023 14:58:10 +0000 (16:58 +0200)
Allows an admin to prepare a template that will have the first-boot
condition set on first start, as we only want to disable first-boot
condition but (re)generate also a machine-id on clone if the
machine-id already exist and isn't set to "uninitialized".

Link: https://forum.proxmox.com/threads/126291/
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
src/PVE/LXC/Setup/Base.pm

index 547abdb9ae7c32052443e2711e159878d2cef6b6..c88bf03ba0ea547ebc1c9cebfe00c2afb674c727 100644 (file)
@@ -506,11 +506,18 @@ sub clear_machine_id {
         $self->ct_unlink($dbus_machine_id_path);
     }
 
-    # truncate on clone to avoid that FirstBoot condition is set
-    if ($clone && ($uses_systemd || $machine_id_existed)) {
-       $self->ct_file_set_contents($machine_id_path, "\n");
-    } elsif (!$clone && $machine_id_existed) {
-       $self->ct_unlink($machine_id_path);
+    if ($machine_id_existed) {
+       # truncate exiting ones on clone to avoid FirstBoot condition. admins can override this by
+       # removing the machine-id file or setting it to uninitialized before creating a template, or
+       # cloning a guest - as per machine-id(5) man page. TODO: add explicit switch to API?
+       if ($clone) {
+           my $old_machine_id = $self->ct_file_read_firstline($machine_id_path) // '';
+           if ($uses_systemd && $old_machine_id ne 'uninitialized') {
+               $self->ct_file_set_contents($machine_id_path, "\n") if $uses_systemd;
+           }
+       } else {
+           $self->ct_unlink($machine_id_path);
+       }
     }
 }