From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id D52521FF066 for ; Tue, 24 Nov 2026 22:27:17 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id D51312153C; Tue, 24 Nov 2026 22:27:12 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=sfc.wide.ad.jp; s=mail2; t=1795555598; bh=fDPvvk/dhAH0/CJwigsQzwzlJJTsx3UX/rn5AzH4G9c=; h=From:To:Subject:Date:In-Reply-To:References:From; b=s3CP2tiB7X7Q+F3+PGgzqrIsE+lshtGQbvQqyP1FmIAMP83lx2e8LuatDe7Xm+Ege wAy7n0sObdJFZrW3rzZm7zuH9xaUPvM2aAoxJtGPM2RBu4zIqxYepnK+Cp8li0/n7B HQuoeB58ZrRYafqwMOt7x58EAAtvJ1k9wEvRkatfKeI71HS55uAgOtb/vezJEce7ND a9eHZlnqx5t3CA3AGk4hCg+YEE31rZTC3DAUe05DSYLIXX0C3SNwA49FUvx64YrWrN i8dwZRLjzubosSEcnPb3VehQf74lbHiAe9/4ogP8aEnZtPSbCMYJGmfdgFT4Bc+SuK KMCy5PLhuzkOQ== From: negineri To: pve-devel@lists.proxmox.com Subject: [PATCH qemu-server 1/2] fix #7100: cloud-init: make /etc/hosts management configurable Date: Wed, 25 Nov 2026 06:26:07 +0900 Message-ID: <20261124212608.12996-2-negineri@sfc.wide.ad.jp> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261124212608.12996-1-negineri@sfc.wide.ad.jp> References: <20261124212608.12996-1-negineri@sfc.wide.ad.jp> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 DKIM_SIGNED 0.1 Message has a DKIM or DK signature, not necessarily valid DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's domain DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from domain DMARC_PASS -0.1 DMARC pass policy SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: IGQBZMPH76VPM4V4LX4657XUH53VBJB5 X-Message-ID-Hash: IGQBZMPH76VPM4V4LX4657XUH53VBJB5 X-MailFrom: negineri@sfc.wide.ad.jp X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Add the cimanagehosts boolean option to control manage_etc_hosts in generated cloud-config user data. Keep the default enabled to preserve the behavior of existing guests, and emit false explicitly when disabled. Register the option for VM.Config.Cloudinit permission checks. The cloud-init schema also includes it in pending image changes and generated option documentation. Treat zero as a configured value when reporting the current and pending state. Test adding, applying, changing and deleting zero. Cover default, enabled, disabled and removed values in the cloud-init dump tests for NoCloud and ConfigDrive2. Signed-off-by: negineri --- src/PVE/API2/Qemu.pm | 9 ++--- src/PVE/QemuServer.pm | 6 ++++ src/PVE/QemuServer/Cloudinit.pm | 3 +- src/test/run_api_cloudinit_dump_tests.pl | 42 ++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm index 53c5f1bf..1499b2ad 100644 --- a/src/PVE/API2/Qemu.pm +++ b/src/PVE/API2/Qemu.pm @@ -977,6 +977,7 @@ my $cloudinitoptions = { citype => 1, ciuser => 1, ciupgrade => 1, + cimanagehosts => 1, sshkeys => 1, $cloudinitoptions_net->%*, }; @@ -2188,14 +2189,14 @@ __PACKAGE__->register_method({ # All already existing values (+ their new value, if it exists) for my $opt (keys %$cloudinitoptions) { - next if !$conf->{$opt}; + next if !defined($conf->{$opt}); next if $added =~ m/$opt/; my $item = { key => $opt, }; - if (my $pending = $ci->{$opt}) { - $item->{value} = $pending; + if (defined($ci->{$opt})) { + $item->{value} = $ci->{$opt}; $item->{pending} = $conf->{$opt}; } else { $item->{value} = $conf->{$opt}; @@ -2206,7 +2207,7 @@ __PACKAGE__->register_method({ # Now, we'll find the deleted ones for my $opt (keys %$ci) { - next if $conf->{$opt}; + next if defined($conf->{$opt}); push @$res, { key => $opt, delete => 1 }; } diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm index ecbaa27f..29de6177 100644 --- a/src/PVE/QemuServer.pm +++ b/src/PVE/QemuServer.pm @@ -818,6 +818,12 @@ my $confdesc_cloudinit = { . ' recommended. Use ssh keys instead. Also note that older cloud-init versions do not' . ' support hashed passwords.', }, + cimanagehosts => { + optional => 1, + type => 'boolean', + description => 'cloud-init: Manage /etc/hosts in the guest.', + default => 1, + }, ciupgrade => { optional => 1, type => 'boolean', diff --git a/src/PVE/QemuServer/Cloudinit.pm b/src/PVE/QemuServer/Cloudinit.pm index f7f8c8e1..c99e392c 100644 --- a/src/PVE/QemuServer/Cloudinit.pm +++ b/src/PVE/QemuServer/Cloudinit.pm @@ -150,7 +150,8 @@ sub cloudinit_userdata { my $content = "#cloud-config\n"; $content .= "hostname: $hostname\n"; - $content .= "manage_etc_hosts: true\n"; + my $manage_hosts = ($conf->{cimanagehosts} // 1) ? 'true' : 'false'; + $content .= "manage_etc_hosts: $manage_hosts\n"; $content .= "fqdn: $fqdn\n"; my $username = $conf->{ciuser}; diff --git a/src/test/run_api_cloudinit_dump_tests.pl b/src/test/run_api_cloudinit_dump_tests.pl index a4db1857..e2625c54 100755 --- a/src/test/run_api_cloudinit_dump_tests.pl +++ b/src/test/run_api_cloudinit_dump_tests.pl @@ -64,4 +64,46 @@ $can_config_cloudinit = 1; like(dump_config('user'), qr/^password: Secret1!$/m, 'user data: password shown with privilege'); ok(defined(eval { dump_config('meta') }), 'meta data: available with privilege'); +for my $citype ('nocloud', 'configdrive2') { + for my $case ( + ['default', undef, 'true'], + ['enabled', 1, 'true'], + ['disabled', 0, 'false'], + ) { + my ($name, $value, $expected) = @$case; + $conf = { %$base_conf, citype => $citype }; + $conf->{cimanagehosts} = $value if defined($value); + like( + dump_config('user'), + qr/^manage_etc_hosts: $expected$/m, + "$citype user data: manage /etc/hosts $name", + ); + } + + delete $conf->{cimanagehosts}; + like( + dump_config('user'), + qr/^manage_etc_hosts: true$/m, + "$citype user data: removing option restores default", + ); +} + +subtest 'cloud-init pending reports zero as a configured value' => sub { + for my $case ( + ['added false', 0, { added => 'cimanagehosts' }, { pending => 0 }], + ['applied false', 0, {}, { value => 0 }], + ['false to true', 1, { cimanagehosts => 0 }, { value => 0, pending => 1 }], + ['true to false', 0, { cimanagehosts => 1 }, { value => 1, pending => 0 }], + ['deleted false', undef, { cimanagehosts => 0 }, { delete => 1 }], + ) { + my ($name, $value, $previous, $expected) = @$case; + $conf = { %$base_conf, 'special-sections' => { cloudinit => $previous } }; + $conf->{cimanagehosts} = $value if defined($value); + + my $rows = PVE::API2::Qemu->cloudinit_pending({ node => 'node', vmid => 100 }); + my @matching = grep { $_->{key} eq 'cimanagehosts' } @$rows; + is_deeply(\@matching, [{ key => 'cimanagehosts', %$expected }], $name); + } +}; + done_testing(); -- 2.56.1