From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id 69FC81FF170
	for <inbox@lore.proxmox.com>; Thu, 17 Oct 2024 13:51:52 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 3F770EADA;
	Thu, 17 Oct 2024 13:52:10 +0200 (CEST)
From: Daniel Kral <d.kral@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Thu, 17 Oct 2024 13:51:23 +0200
Message-Id: <20241017115124.75539-3-d.kral@proxmox.com>
X-Mailer: git-send-email 2.39.5
In-Reply-To: <20241017115124.75539-1-d.kral@proxmox.com>
References: <20241017115124.75539-1-d.kral@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.001 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to
 Validity was blocked. See
 https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more
 information.
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH qemu-server 3/4] fix #5301: convert added volume
 disks to base image for templates
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.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