From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 471301FF168 for ; Tue, 26 Nov 2024 16:23:43 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id B5A59588C; Tue, 26 Nov 2024 16:23:41 +0100 (CET) From: Filip Schauer To: pve-devel@lists.proxmox.com Date: Tue, 26 Nov 2024 16:23:19 +0100 Message-Id: <20241126152325.113926-2-f.schauer@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241126152325.113926-1-f.schauer@proxmox.com> References: <20241126152325.113926-1-f.schauer@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.028 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 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 storage v5 1/7] plugin: allow volume import of iso, snippets, vztmpl and import X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" Extend volume import functionality to support 'iso', 'snippets', 'vztmpl', and 'import' types, in addition to the existing support for 'images' and 'rootdir'. This is a prerequisite for the ability to move ISOs, snippets and container templates between nodes. Existing behavior for importing VM disks and container volumes remains unchanged. Signed-off-by: Filip Schauer --- src/PVE/Storage/Plugin.pm | 72 ++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 24 deletions(-) diff --git a/src/PVE/Storage/Plugin.pm b/src/PVE/Storage/Plugin.pm index 0b0b5a8..c010d9c 100644 --- a/src/PVE/Storage/Plugin.pm +++ b/src/PVE/Storage/Plugin.pm @@ -1684,6 +1684,8 @@ sub volume_export_formats { or return; my ($size, $format) = file_size_info($file); + return ('raw+size') if !defined($format); + if ($with_snapshots) { return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk'); return (); @@ -1712,14 +1714,18 @@ sub volume_import { # XXX: Should we bother with conversion routines at this level? This won't # happen without manual CLI usage, so for now we just error out... - die "cannot import format $format into a file of format $file_format\n" - if $data_format ne $file_format && !($data_format eq 'tar' && $file_format eq 'subvol'); + if (($vtype eq 'images' || $vtype eq 'rootdir') && $data_format ne $file_format && + !($data_format eq 'tar' && $file_format eq 'subvol') + ) { + die "cannot import format $format into a file of format $file_format\n"; + } # Check for an existing file first since interrupting alloc_image doesn't # free it. my $file = $class->path($scfg, $volname, $storeid); if (-e $file) { - die "file '$file' already exists\n" if !$allow_rename; + die "file '$file' already exists\n" + if !$allow_rename || ($vtype ne 'images' && $vtype ne 'rootdir'); warn "file '$file' already exists - importing with a different name\n"; $name = undef; } @@ -1727,29 +1733,44 @@ sub volume_import { my ($size) = read_common_header($fh); $size = int($size/1024); - eval { - my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size); - my $oldname = $volname; - $volname = $allocname; - if (defined($name) && $allocname ne $oldname) { - die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n"; + if ($vtype eq 'images' || $vtype eq 'rootdir') { + eval { + my $allocname = $class->alloc_image($storeid, $scfg, $vmid, $file_format, $name, $size); + my $oldname = $volname; + $volname = $allocname; + if (defined($name) && $allocname ne $oldname) { + die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n"; + } + my $file = $class->path($scfg, $volname, $storeid) + or die "internal error: failed to get path to newly allocated volume $volname\n"; + if ($data_format eq 'raw' || $data_format eq 'qcow2' || $data_format eq 'vmdk') { + run_command(['dd', "of=$file", 'conv=sparse', 'bs=64k'], + input => '<&'.fileno($fh)); + } elsif ($data_format eq 'tar') { + run_command(['tar', @COMMON_TAR_FLAGS, '-C', $file, '-xf', '-'], + input => '<&'.fileno($fh)); + } else { + die "volume import format '$format' not available for $class"; + } + }; + if (my $err = $@) { + eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format) }; + warn $@ if $@; + die $err; } - my $file = $class->path($scfg, $volname, $storeid) - or die "internal error: failed to get path to newly allocated volume $volname\n"; - if ($data_format eq 'raw' || $data_format eq 'qcow2' || $data_format eq 'vmdk') { - run_command(['dd', "of=$file", 'conv=sparse', 'bs=64k'], - input => '<&'.fileno($fh)); - } elsif ($data_format eq 'tar') { - run_command(['tar', @COMMON_TAR_FLAGS, '-C', $file, '-xf', '-'], - input => '<&'.fileno($fh)); - } else { - die "volume import format '$format' not available for $class"; + } elsif (grep { $vtype eq $_ } qw(import iso snippets vztmpl)) { + eval { + run_command(['dd', "of=$file", 'bs=64k'], input => '<&'.fileno($fh)); + }; + if (my $err = $@) { + if (-e $file) { + eval { unlink($file) }; + warn $@ if $@; + } + die $err; } - }; - if (my $err = $@) { - eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format) }; - warn $@ if $@; - die $err; + } else { + die "importing volume of type '$vtype' not implemented\n"; } return "$storeid:$volname"; @@ -1759,6 +1780,9 @@ sub volume_import_formats { my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_; if ($scfg->{path} && !defined($base_snapshot)) { my $format = ($class->parse_volname($volname))[6]; + + return ('raw+size') if !defined($format); + if ($with_snapshots) { return ($format.'+size') if ($format eq 'qcow2' || $format eq 'vmdk'); return (); -- 2.39.5 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel