[pve-devel] [PATCH qemu-server 3/4] fix #5301: convert added volume disks to base image for templates

Daniel Kral d.kral at proxmox.com
Thu Oct 17 13:51:23 CEST 2024


This will automatically convert imported volume disks and newly
allocated VM volume disks (i.e. no efidisks, tpmstate disks, cloudinit
images, etc.) to a base volume, if the VM is a template.

Previously, this required a user to manually convert the
imported/allocated disk with `qm template --disk <disk>`.

Signed-off-by: Daniel Kral <d.kral at proxmox.com>
---

Notes:
    Some testing notes
    
    As I was unfamiliar with the internal behavior of the `update_vm_api`
    and `create_disks` functions before, I wanted to make sure that nothing
    has been broken and wrote some tests in a shell script. I've begun some
    work on rewriting them as Perl test cases, but I think that would be
    better suited in a separate patch series.
    
    Here is the script I've come up with, which losely tests whether the
    vmconfig updates changed the disks to a base VM image or not. This will
    need some qcow2 image that will be imported (which I have done with a
    debian generic cloud image as described in the forum post mentioned in
    the bug report [0]).
    
    These tests also include the test cases for the changes done to the
    `importdisk` API endpoint from patch #2 and #3. Otherwise, the tests
    with template creation involved will fail as expected on unpatched
    qemu-server and will pass with this patch series applied.
    
    ```
    
    TEST_VMID="500"
    TEST_STORAGE="local-lvm"
    TEST_IMPORT_IMAGE="/root/importdisk.qcow2"
    
    qm destroy $TEST_VMID
    
    qm create $TEST_VMID --name "import-tester" --memory 1024 \
            --net0 "virtio,bridge=vmbr0" --boot "order=scsi0" \
            --ostype "l26" --scsihw "virtio-scsi-pci" \
            --scsi0 "$TEST_STORAGE:0,import-from=$TEST_IMPORT_IMAGE"
    
    function test_qmset {
            qm set $TEST_VMID "--$1" $2
            qm config $TEST_VMID | grep "$1" | grep "$3" \
                    && echo -ne "\033[1;32m[SUCCESS] " \
                    || echo -ne "\033[1;31m[FAIL] "
            echo -e "'qm set $TEST_VMID --$1 $2' for $3\033[0m"
    }
    
    function test_importdisk {
            qm disk import $TEST_VMID $TEST_IMPORT_IMAGE $TEST_STORAGE --target-disk "$1"
            qm config $TEST_VMID | grep "$1" | grep "$2" \
                    && echo -ne "\033[1;32m[SUCCESS] " \
                    || echo -ne "\033[1;31m[FAIL] "
            echo -e "'qm disk import $TEST_VMID $TEST_IMPORT_IMAGE $TEST_STORAGE --target-disk $1' for $2\033[0m"
    }
    
    test_qmset "scsi1" "$TEST_STORAGE:0,import-from=$TEST_IMPORT_IMAGE" "vm"
    test_qmset "scsi2" "$TEST_STORAGE:8" "vm"
    test_qmset "efidisk0" "$TEST_STORAGE:1,efitype=4m,pre-enrolled-keys=1" "vm"
    test_qmset "tpmstate0" "$TEST_STORAGE:1,version=v2.0" "vm"
    test_qmset "ide2" "$TEST_STORAGE:cloudinit" "vm"
    
    test_importdisk "unused0" "vm"
    test_importdisk "scsi3" "vm"
    
    qm set $TEST_VMID --delete "scsi1,scsi2,scsi3,efidisk0,tpmstate0,ide2"
    qm set $TEST_VMID --delete "unused0,unused1,unused2,unused3,unused4,unused5"
    
    qm template $TEST_VMID
    qm config $TEST_VMID | grep "template: 1" \
            && echo -e "\033[1;32mTemplate created successfully.\033[0m" \
            || echo "\033[1;31mFailed at creating template.\033[0m"
    qm config $TEST_VMID | grep "scsi0" | grep "base" \
            && echo -e "\033[1;32mTemplate creation successfully changed scsi0 to base image.\033[0m" \
            || echo "\033[1;31mFailed at changing scsi0 to base image in template creation.\033[0m"
    
    test_qmset "scsi1" "$TEST_STORAGE:0,import-from=$TEST_IMPORT_IMAGE" "base"
    test_qmset "scsi2" "$TEST_STORAGE:8" "base"
    test_qmset "efidisk0" "$TEST_STORAGE:1,efitype=4m,pre-enrolled-keys=1" "vm"
    test_qmset "tpmstate0" "$TEST_STORAGE:1,version=v2.0" "vm"
    test_qmset "ide2" "$TEST_STORAGE:cloudinit" "vm"
    
    test_importdisk "unused0" "vm"
    test_importdisk "scsi3" "base"
    ```
    
    [0] https://forum.proxmox.com/threads/142987/#post-642014

 PVE/API2/Qemu.pm | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index d25a79fe..9f123d12 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -460,6 +460,11 @@ my sub create_disks : prototype($$$$$$$$$$) {
 				'skip-config-update' => 1,
 			    },
 			);
+
+			# change imported disk to a base volume in case the VM is a template
+			$dst_volid = PVE::Storage::vdisk_create_base($storecfg, $dst_volid)
+			    if PVE::QemuConfig->is_template($conf);
+
 			push @$vollist, $dst_volid;
 		    }
 		}
@@ -489,6 +494,10 @@ my sub create_disks : prototype($$$$$$$$$$) {
 		    $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, "raw", undef, $size);
 		} else {
 		    $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, undef, $size);
+
+		    # change created disk to a base volume in case the VM is a template
+		    $volid = PVE::Storage::vdisk_create_base($storecfg, $volid)
+			if PVE::QemuConfig->is_template($conf);
 		}
 		push @$vollist, $volid;
 		$disk->{file} = $volid;
-- 
2.39.5





More information about the pve-devel mailing list