From: negineri <negineri@sfc.wide.ad.jp>
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 [thread overview]
Message-ID: <20261124212608.12996-2-negineri@sfc.wide.ad.jp> (raw)
In-Reply-To: <20261124212608.12996-1-negineri@sfc.wide.ad.jp>
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 <negineri@sfc.wide.ad.jp>
---
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
next prev parent reply other threads:[~2026-11-24 21:27 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 21:26 [PATCH qemu-server/pve-manager 0/2] fix #7100: allow disabling cloud-init /etc/hosts management negineri
2026-11-24 21:26 ` negineri [this message]
2026-11-24 21:26 ` [PATCH pve-manager 2/2] fix #7100: ui: cloud-init: allow configuring " negineri
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124212608.12996-2-negineri@sfc.wide.ad.jp \
--to=negineri@sfc.wide.ad.jp \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox