public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Daniel Kral <d.kral@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH qemu-server 3/4] fix #5301: convert added volume disks to base image for templates
Date: Thu, 17 Oct 2024 13:51:23 +0200	[thread overview]
Message-ID: <20241017115124.75539-3-d.kral@proxmox.com> (raw)
In-Reply-To: <20241017115124.75539-1-d.kral@proxmox.com>

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@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



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


  parent reply	other threads:[~2024-10-17 11:51 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-17 11:51 [pve-devel] [PATCH qemu-server 1/4] importdisk: add 'target-disk' option to add imported volume to disk Daniel Kral
2024-10-17 11:51 ` [pve-devel] [PATCH qemu-server 2/4] importdisk: convert imported volume disks to base images for templates Daniel Kral
2024-10-17 11:51 ` Daniel Kral [this message]
2024-10-17 11:51 ` [pve-devel] [PATCH qemu-server 4/4] templates: add documentation to template_create Daniel Kral

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=20241017115124.75539-3-d.kral@proxmox.com \
    --to=d.kral@proxmox.com \
    --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
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal