]> git.proxmox.com Git - qemu-server.git/commitdiff
api: create disks: avoid adding secondary cloud-init drives
authorFabian Ebner <f.ebner@proxmox.com>
Fri, 6 May 2022 10:11:14 +0000 (12:11 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 12 Sep 2022 16:04:59 +0000 (18:04 +0200)
This will break possibly existing workflows like
1. add second cloud-init
2. remove first cloud-init
to change the cloud-init storage.

On the other hand, it avoids unintended misconfiguration of having
mutliple cloud-init drives with potentially different settings.

Also in preparation for adding cloud-init-related API calls, where
not being able to assume that there's only one cloud-init drive/state
would complicate things quite a bit.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
PVE/API2/Qemu.pm
PVE/QemuConfig.pm

index d9ef201e0d41d099d5ede2d76b40a3cd1a85c785..3ec31c26898644114c7727d9030a6545626dd849 100644 (file)
@@ -328,6 +328,15 @@ my $create_disks = sub {
        } elsif (defined($volname) && $volname eq 'cloudinit') {
            $storeid = $storeid // $default_storage;
            die "no storage ID specified (and no default storage)\n" if !$storeid;
+
+           if (
+               my $ci_key = PVE::QemuConfig->has_cloudinit($conf, $ds)
+               || PVE::QemuConfig->has_cloudinit($conf->{pending} || {}, $ds)
+               || PVE::QemuConfig->has_cloudinit($res, $ds)
+           ) {
+               die "$ds - cloud-init drive is already attached at '$ci_key'\n";
+           }
+
            my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
            my $name = "vm-$vmid-cloudinit";
 
@@ -424,6 +433,16 @@ my $create_disks = sub {
                my ($vtype) = PVE::Storage::parse_volname($storecfg, $volid);
                die "cannot use volume $volid - content type needs to be 'images' or 'iso'"
                    if $vtype ne 'images' && $vtype ne 'iso';
+
+               if (PVE::QemuServer::Drive::drive_is_cloudinit($disk)) {
+                   if (
+                       my $ci_key = PVE::QemuConfig->has_cloudinit($conf, $ds)
+                       || PVE::QemuConfig->has_cloudinit($conf->{pending} || {}, $ds)
+                       || PVE::QemuConfig->has_cloudinit($res, $ds)
+                   ) {
+                       die "$ds - cloud-init drive is already attached at '$ci_key'\n";
+                   }
+               }
            }
 
            PVE::Storage::activate_volumes($storecfg, [ $volid ]) if $storeid;
index b75227b58257e896698174ab85a878adfff3b0eb..482c7abd9bd278d000396a32360178614a9dec6b 100644 (file)
@@ -521,4 +521,19 @@ sub __snapshot_rollback_get_unused {
 
 # END implemented abstract methods from PVE::AbstractConfig
 
+sub has_cloudinit {
+    my ($class, $conf, $skip) = @_;
+
+    my $found;
+
+    $class->foreach_volume($conf, sub {
+       my ($key, $volume) = @_;
+
+       return if ($skip && $skip eq $key) || $found;
+       $found = $key if PVE::QemuServer::Drive::drive_is_cloudinit($volume);
+    });
+
+    return $found;
+}
+
 1;