public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pve-devel] [PATCH qemu-server 1/4] importdisk: add 'target-disk' option to add imported volume to disk
@ 2024-10-17 11:51 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
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Daniel Kral @ 2024-10-17 11:51 UTC (permalink / raw)
  To: pve-devel

Implements the "target-disk" option for the importdisk command, which
allows a disk to be imported and directly used instead of marking it as
an unused disk (e.g. unused0), which is the default behavior.

Signed-off-by: Daniel Kral <d.kral@proxmox.com>
---
 PVE/CLI/qm.pm | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm
index d3dbf7b4..30282f6f 100755
--- a/PVE/CLI/qm.pm
+++ b/PVE/CLI/qm.pm
@@ -29,7 +29,7 @@ use PVE::Tools qw(extract_param file_get_contents);
 use PVE::API2::Qemu::Agent;
 use PVE::API2::Qemu;
 use PVE::QemuConfig;
-use PVE::QemuServer::Drive;
+use PVE::QemuServer::Drive qw(is_valid_drivename);
 use PVE::QemuServer::Helpers;
 use PVE::QemuServer::Agent qw(agent_available);
 use PVE::QemuServer::ImportDisk;
@@ -579,6 +579,12 @@ __PACKAGE__->register_method ({
 		enum => [ 'raw', 'qcow2', 'vmdk' ],
 		optional => 1,
 	    },
+	    'target-disk' => {
+		type => 'string',
+		description => 'The disk name where the volume will be imported to (e.g. scsi1).',
+		enum => [PVE::QemuServer::Drive::valid_drive_names_with_unused()],
+		optional => 1,
+	    },
 	},
     },
     returns => { type => 'null'},
@@ -589,6 +595,10 @@ __PACKAGE__->register_method ({
 	my $source = extract_param($param, 'source');
 	my $storeid = extract_param($param, 'storage');
 	my $format = extract_param($param, 'format');
+	my $target_disk = extract_param($param, 'target-disk');
+
+	# do_import does not allow invalid drive names (e.g. unused0)
+	$target_disk = undef if !is_valid_drivename($target_disk);
 
 	my $vm_conf = PVE::QemuConfig->load_config($vmid);
 	PVE::QemuConfig->check_lock($vm_conf);
@@ -602,7 +612,16 @@ __PACKAGE__->register_method ({
 	    if !$target_storage_config->{content}->{images};
 
 	print "importing disk '$source' to VM $vmid ...\n";
-	my ($drive_id, $volid) = PVE::QemuServer::ImportDisk::do_import($source, $vmid, $storeid, { format => $format });
+
+	my ($drive_id, $volid) = PVE::QemuServer::ImportDisk::do_import(
+	    $source,
+	    $vmid,
+	    $storeid,
+	    {
+		drive_name => $target_disk,
+		format => $format,
+	    });
+
 	print "Successfully imported disk as '$drive_id:$volid'\n";
 
 	return;
-- 
2.39.5



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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [pve-devel] [PATCH qemu-server 2/4] importdisk: convert imported volume disks to base images for templates
  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 ` Daniel Kral
  2024-10-17 11:51 ` [pve-devel] [PATCH qemu-server 3/4] fix #5301: convert added volume disks to base image " Daniel Kral
  2024-10-17 11:51 ` [pve-devel] [PATCH qemu-server 4/4] templates: add documentation to template_create Daniel Kral
  2 siblings, 0 replies; 4+ messages in thread
From: Daniel Kral @ 2024-10-17 11:51 UTC (permalink / raw)
  To: pve-devel

Automatically converts any imported volume disk to a base volume image
if the VM is a template and the volume was imported using the
"target-disk" option, as "unused" disks are not needed to be converted
as they won't be cloned with either linked nor full clones.

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

Notes:
    Change to the command line output
    
    There is a small change to the console output at the end, because (a)
    it produces less code than correctly setting the $drive_id and $volid
    again, and (b) it is more consistent with the console prints of
    `create_disks` used when creating new VM disks.

 PVE/CLI/qm.pm | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm
index 30282f6f..e65684bd 100755
--- a/PVE/CLI/qm.pm
+++ b/PVE/CLI/qm.pm
@@ -622,7 +622,13 @@ __PACKAGE__->register_method ({
 		format => $format,
 	    });
 
-	print "Successfully imported disk as '$drive_id:$volid'\n";
+	$vm_conf = PVE::QemuConfig->load_config($vmid);
+
+	# change imported _used_ disk to a base volume in case the VM is a template
+	PVE::QemuServer::template_create($vmid, $vm_conf, $drive_id)
+	    if is_valid_drivename($drive_id) && PVE::QemuConfig->is_template($vm_conf);
+
+	print "$drive_id: successfully imported disk '$vm_conf->{$drive_id}'\n";
 
 	return;
     }});
-- 
2.39.5



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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [pve-devel] [PATCH qemu-server 3/4] fix #5301: convert added volume disks to base image for templates
  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
  2024-10-17 11:51 ` [pve-devel] [PATCH qemu-server 4/4] templates: add documentation to template_create Daniel Kral
  2 siblings, 0 replies; 4+ messages in thread
From: Daniel Kral @ 2024-10-17 11:51 UTC (permalink / raw)
  To: pve-devel

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [pve-devel] [PATCH qemu-server 4/4] templates: add documentation to template_create
  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 ` [pve-devel] [PATCH qemu-server 3/4] fix #5301: convert added volume disks to base image " Daniel Kral
@ 2024-10-17 11:51 ` Daniel Kral
  2 siblings, 0 replies; 4+ messages in thread
From: Daniel Kral @ 2024-10-17 11:51 UTC (permalink / raw)
  To: pve-devel

Signed-off-by: Daniel Kral <d.kral@proxmox.com>
---
 PVE/QemuServer.pm | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index b26da505..114bafc5 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -7794,7 +7794,17 @@ sub qga_check_running {
     return 1;
 }
 
-sub template_create {
+=head3 template_create($vmid, $conf [, $disk])
+
+Converts all used disk volumes for the VM with the identifier C<$vmid> and
+configuration C<$conf> to base images (e.g. for VM templates).
+
+If the optional C<$disk> parameter is set, it will only convert the disk
+volume at the specified drive name (e.g. "scsi0").
+
+=cut
+
+sub template_create : prototype($$;$) {
     my ($vmid, $conf, $disk) = @_;
 
     my $storecfg = PVE::Storage::config();
@@ -7811,6 +7821,8 @@ sub template_create {
 	my $voliddst = PVE::Storage::vdisk_create_base($storecfg, $volid);
 	$drive->{file} = $voliddst;
 	$conf->{$ds} = print_drive($drive);
+
+	# write vm config on every change in case this fails on subsequent iterations
 	PVE::QemuConfig->write_config($vmid, $conf);
     });
 }
-- 
2.39.5



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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-10-17 11:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [pve-devel] [PATCH qemu-server 3/4] fix #5301: convert added volume disks to base image " Daniel Kral
2024-10-17 11:51 ` [pve-devel] [PATCH qemu-server 4/4] templates: add documentation to template_create Daniel Kral

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