From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id E8A601FF0AF for ; Fri, 20 Nov 2026 17:07:22 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id C7AC421886; Fri, 20 Nov 2026 17:06:36 +0100 (CET) From: Jakob Klocker To: pve-devel@lists.proxmox.com Subject: [PATCH pve-storage-plugin-examples v4 1/7] backup provider example: run proxmox-perltidy Date: Fri, 20 Nov 2026 17:05:04 +0100 Message-ID: <20261120160510.311796-2-j.klocker@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261120160510.311796-1-j.klocker@proxmox.com> References: <20261120160510.311796-1-j.klocker@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1795190776402 X-SPAM-LEVEL: Spam detection results: 0 AWL 1.109 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: UUA3UPFAV3CEZJXD2OUSD6HX7HUJMKI3 X-Message-ID-Hash: UUA3UPFAV3CEZJXD2OUSD6HX7HUJMKI3 X-MailFrom: j.klocker@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: No functional change intended. Signed-off-by: Jakob Klocker --- .../BackupProvider/Plugin/DirectoryExample.pm | 629 +++++++++--------- 1 file changed, 324 insertions(+), 305 deletions(-) diff --git a/backup-provider-directory/src/PVE/BackupProvider/Plugin/DirectoryExample.pm b/backup-provider-directory/src/PVE/BackupProvider/Plugin/DirectoryExample.pm index 365ce40..f61198b 100644 --- a/backup-provider-directory/src/PVE/BackupProvider/Plugin/DirectoryExample.pm +++ b/backup-provider-directory/src/PVE/BackupProvider/Plugin/DirectoryExample.pm @@ -45,76 +45,79 @@ my sub bind_next_free_dev_nbd_node { # This avoids race conditions between allocation and use. die "file '/sys/module/nbd' does not exist - 'nbd' kernel module not loaded?" - if !-e "/sys/module/nbd"; + if !-e "/sys/module/nbd"; my $line = PVE::Tools::file_read_firstline("/sys/module/nbd/parameters/nbds_max") - or die "could not read 'nbds_max' parameter file for 'nbd' kernel module\n"; + or die "could not read 'nbds_max' parameter file for 'nbd' kernel module\n"; my ($nbds_max) = ($line =~ m/(\d+)/) - or die "could not determine 'nbds_max' parameter for 'nbd' kernel module\n"; + or die "could not determine 'nbds_max' parameter for 'nbd' kernel module\n"; my $filename = "/run/qemu-server/reserved-dev-nbd-nodes"; my $code = sub { - my $expiretime = 60; - my $ctime = time(); - - my $used = {}; - my $latest = [0, 0]; - - if (my $fh = IO::File->new ($filename, "r")) { - while (my $line = <$fh>) { - if ($line =~ m/^(\d+)\s(\d+)$/) { - my ($n, $timestamp) = ($1, $2); - - $latest = [$n, $timestamp] if $latest->[1] <= $timestamp; - - if (($timestamp + $expiretime) > $ctime) { - $used->{$n} = $timestamp; # not expired - } - } - } - } - - my $new_n; - for (my $count = 0; $count < $nbds_max; $count++) { - my $n = ($latest->[0] + $count) % $nbds_max; - my $block_device = "/dev/nbd${n}"; - next if $used->{$n}; # reserved - next if !-e $block_device; - - my $st = File::stat::stat("/run/lock/qemu-nbd-nbd${n}"); - next if defined($st) && S_ISSOCK($st->mode) && $st->uid == 0; # in use - - # Used to avoid looping if there are other issues then the NBD node being in use - my $socket_error = 0; - eval { - my $errfunc = sub { - my ($line) = @_; - $socket_error = 1 if $line =~ m/^qemu-nbd: Failed to set NBD socket$/; - log_warn($line); - }; - run_command(["qemu-nbd", "-c", $block_device, $options->@*], errfunc => $errfunc); - }; - if (my $err = $@) { - die $err if !$socket_error; - log_warn("unable to bind $block_device - trying next one"); - next; - } - $used->{$n} = $ctime; - $new_n = $n; - last; - } - - my $data = ""; - $data .= "$_ $used->{$_}\n" for keys $used->%*; - - PVE::Tools::file_set_contents($filename, $data); - - return defined($new_n) ? "/dev/nbd${new_n}" : undef; + my $expiretime = 60; + my $ctime = time(); + + my $used = {}; + my $latest = [0, 0]; + + if (my $fh = IO::File->new($filename, "r")) { + while (my $line = <$fh>) { + if ($line =~ m/^(\d+)\s(\d+)$/) { + my ($n, $timestamp) = ($1, $2); + + $latest = [$n, $timestamp] if $latest->[1] <= $timestamp; + + if (($timestamp + $expiretime) > $ctime) { + $used->{$n} = $timestamp; # not expired + } + } + } + } + + my $new_n; + for (my $count = 0; $count < $nbds_max; $count++) { + my $n = ($latest->[0] + $count) % $nbds_max; + my $block_device = "/dev/nbd${n}"; + next if $used->{$n}; # reserved + next if !-e $block_device; + + my $st = File::stat::stat("/run/lock/qemu-nbd-nbd${n}"); + next if defined($st) && S_ISSOCK($st->mode) && $st->uid == 0; # in use + + # Used to avoid looping if there are other issues then the NBD node being in use + my $socket_error = 0; + eval { + my $errfunc = sub { + my ($line) = @_; + $socket_error = 1 if $line =~ m/^qemu-nbd: Failed to set NBD socket$/; + log_warn($line); + }; + run_command( + ["qemu-nbd", "-c", $block_device, $options->@*], + errfunc => $errfunc, + ); + }; + if (my $err = $@) { + die $err if !$socket_error; + log_warn("unable to bind $block_device - trying next one"); + next; + } + $used->{$n} = $ctime; + $new_n = $n; + last; + } + + my $data = ""; + $data .= "$_ $used->{$_}\n" for keys $used->%*; + + PVE::Tools::file_set_contents($filename, $data); + + return defined($new_n) ? "/dev/nbd${new_n}" : undef; }; my $block_device = - PVE::Tools::lock_file('/run/lock/qemu-server/reserved-dev-nbd-nodes.lock', 10, $code); + PVE::Tools::lock_file('/run/lock/qemu-server/reserved-dev-nbd-nodes.lock', 10, $code); die $@ if $@; die "unable to find free /dev/nbdX block device node\n" if !$block_device; @@ -128,10 +131,10 @@ sub new { my ($class, $storage_plugin, $scfg, $storeid, $log_function) = @_; my $self = bless { - scfg => $scfg, - storeid => $storeid, - 'storage-plugin' => $storage_plugin, - 'log-function' => $log_function, + scfg => $scfg, + storeid => $storeid, + 'storage-plugin' => $storage_plugin, + 'log-function' => $log_function, }, $class; return $self; @@ -151,8 +154,8 @@ sub job_init { log_info($self, "job init called"); if (!-e '/sys/module/nbd/parameters') { - die "required 'nbd' kernel module not loaded - use 'modprobe nbd nbds_max=128' to load it" - ." manually\n"; + die "required 'nbd' kernel module not loaded - use 'modprobe nbd nbds_max=128' to load it" + . " manually\n"; } log_info($self, "backup provider initialized successfully for new job $start_time"); @@ -203,46 +206,43 @@ my sub update_previous_info { my $previous_info_file = "$self->{scfg}->{path}/$vmid/previous-info"; if (defined(my $info = $self->{$vmid}->{previous})) { - file_set_contents($previous_info_file, to_json($info)); + file_set_contents($previous_info_file, to_json($info)); } else { - unlink($previous_info_file); + unlink($previous_info_file); } } - sub backup_cleanup { my ($self, $vmid, $vmtype, $success, $info) = @_; if ($success) { - log_info($self, "backup cleanup called - success"); - eval { - update_previous_info($self, $vmid, $self->{$vmid}->{previous}); - }; - if (my $err = $@) { - log_error($self, "failed to update previous-info file: $err"); - } - my $size = 0; - my $backup_dir = $self->{$vmid}->{'backup-dir'}; - my @backup_files = glob("$backup_dir/*"); - $size += -s $_ for @backup_files; - my $stats = { 'archive-size' => $size }; - return { 'stats' => $stats }; + log_info($self, "backup cleanup called - success"); + eval { update_previous_info($self, $vmid, $self->{$vmid}->{previous}); }; + if (my $err = $@) { + log_error($self, "failed to update previous-info file: $err"); + } + my $size = 0; + my $backup_dir = $self->{$vmid}->{'backup-dir'}; + my @backup_files = glob("$backup_dir/*"); + $size += -s $_ for @backup_files; + my $stats = { 'archive-size' => $size }; + return { 'stats' => $stats }; } else { - log_info($self, "backup cleanup called - failure"); + log_info($self, "backup cleanup called - failure"); - $self->{$vmid}->{failed} = 1; + $self->{$vmid}->{failed} = 1; - if (my $dir = $self->{$vmid}->{'backup-dir'}) { - eval { remove_tree($dir) }; - log_warning($self, "unable to clean up $dir - $@") if $@; - } + if (my $dir = $self->{$vmid}->{'backup-dir'}) { + eval { remove_tree($dir) }; + log_warning($self, "unable to clean up $dir - $@") if $@; + } - # Restore old previous-info so next attempt can re-use bitmap again - if (my $info = $self->{$vmid}->{'old-previous-info'}) { - my $previous_info_dir = "$self->{scfg}->{path}/$vmid/"; - my $previous_info_file = "$previous_info_dir/previous-info"; - file_set_contents($previous_info_file, $info); - } + # Restore old previous-info so next attempt can re-use bitmap again + if (my $info = $self->{$vmid}->{'old-previous-info'}) { + my $previous_info_dir = "$self->{scfg}->{path}/$vmid/"; + my $previous_info_file = "$previous_info_dir/previous-info"; + file_set_contents($previous_info_file, $info); + } } } @@ -270,20 +270,20 @@ sub backup_vm_query_incremental { my $info = get_previous_info_tainted($self, $vmid); for my $device_name (keys $volumes->%*) { - $out->{$device_name} = 'new'; + $out->{$device_name} = 'new'; - my $prev_file = $info->{$device_name}; - next if !defined $prev_file; - # it's type-time/disk.qcow2 - next if $prev_file !~ m!^([^/]+/[^/]+\.qcow2)$!; - $prev_file = $1; # untaint + my $prev_file = $info->{$device_name}; + next if !defined $prev_file; + # it's type-time/disk.qcow2 + next if $prev_file !~ m!^([^/]+/[^/]+\.qcow2)$!; + $prev_file = $1; # untaint - my $full_path = "$self->{scfg}->{path}/$vmid/$prev_file"; + my $full_path = "$self->{scfg}->{path}/$vmid/$prev_file"; - if (-e $full_path) { - $self->{$vmid}->{previous}->{$device_name} = $prev_file; - $out->{$device_name} = 'use'; - } + if (-e $full_path) { + $self->{$vmid}->{previous}->{$device_name} = $prev_file; + $out->{$device_name} = 'use'; + } } return $out; @@ -303,7 +303,7 @@ sub backup_handle_log_file { my $log_dir = $self->{$vmid}->{'backup-dir'}; if ($self->{$vmid}->{failed}) { - $log_dir .= ".failed"; + $log_dir .= ".failed"; } make_path($log_dir); die "unable to create directory $log_dir\n" if !-d $log_dir; @@ -314,7 +314,16 @@ sub backup_handle_log_file { } my sub backup_file { - my ($self, $vmid, $device_name, $size, $in_fh, $bitmap_mode, $next_dirty_region, $bandwidth_limit) = @_; + my ( + $self, + $vmid, + $device_name, + $size, + $in_fh, + $bitmap_mode, + $next_dirty_region, + $bandwidth_limit, + ) = @_; # TODO honor bandwidth_limit @@ -322,78 +331,82 @@ my sub backup_file { my $create_cmd = ["qemu-img", "create", "-f", "qcow2", $target, $size]; if (my $previous_file = $self->{$vmid}->{previous}->{$device_name}) { - my $target_base = "../$previous_file"; - push $create_cmd->@*, "-b", $target_base, "-F", "qcow2"; + my $target_base = "../$previous_file"; + push $create_cmd->@*, "-b", $target_base, "-F", "qcow2"; } run_command($create_cmd); my $nbd_node; eval { - # allows to easily write to qcow2 target - $nbd_node = bind_next_free_dev_nbd_node([$target, '--format=qcow2']); - # FIXME use nbdfuse like in qemu-server rather than qemu-nbd. Seems like there is a race and - # sysseek() can fail with "Invalid argument" if done too early... - sleep 1; - - my $block_size = 4 * 1024 * 1024; # 4 MiB - - my $out_fh = IO::File->new($nbd_node, "r+") - or die "unable to open NBD backup target - $!\n"; - - my $buffer = ''; - my $skip_discard; - - while (scalar((my $region_offset, my $region_length) = $next_dirty_region->())) { - sysseek($in_fh, $region_offset, SEEK_SET) - // die "unable to seek '$region_offset' in NBD backup source - $!\n"; - sysseek($out_fh, $region_offset, SEEK_SET) - // die "unable to seek '$region_offset' in NBD backup target - $!\n"; - - my $local_offset = 0; # within the region - while ($local_offset < $region_length) { - my $remaining = $region_length - $local_offset; - my $request_size = $remaining < $block_size ? $remaining : $block_size; - my $offset = $region_offset + $local_offset; - - my $read = sysread($in_fh, $buffer, $request_size); - die "failed to read from backup source - $!\n" if !defined($read); - die "premature EOF while reading backup source\n" if $read == 0; - - my $written = 0; - while ($written < $read) { - my $res = syswrite($out_fh, $buffer, $request_size - $written, $written); - die "failed to write to backup target - $!\n" if !defined($res); - die "unable to progress writing to backup target\n" if $res == 0; - $written += $res; - } - - if (!$skip_discard) { - eval { PVE::Storage::Common::deallocate($in_fh, $offset, $request_size); }; - if (my $err = $@) { - # Just assume that if one request didn't work, others won't either. - log_warning( - $self, "discard source failed (skipping further discards) - $err"); - $skip_discard = 1; - } - } - - $local_offset += $request_size; - } - } - $out_fh->sync(); + # allows to easily write to qcow2 target + $nbd_node = bind_next_free_dev_nbd_node([$target, '--format=qcow2']); + # FIXME use nbdfuse like in qemu-server rather than qemu-nbd. Seems like there is a race and + # sysseek() can fail with "Invalid argument" if done too early... + sleep 1; + + my $block_size = 4 * 1024 * 1024; # 4 MiB + + my $out_fh = IO::File->new($nbd_node, "r+") + or die "unable to open NBD backup target - $!\n"; + + my $buffer = ''; + my $skip_discard; + + while (scalar((my $region_offset, my $region_length) = $next_dirty_region->())) { + sysseek($in_fh, $region_offset, SEEK_SET) + // die "unable to seek '$region_offset' in NBD backup source - $!\n"; + sysseek($out_fh, $region_offset, SEEK_SET) + // die "unable to seek '$region_offset' in NBD backup target - $!\n"; + + my $local_offset = 0; # within the region + while ($local_offset < $region_length) { + my $remaining = $region_length - $local_offset; + my $request_size = $remaining < $block_size ? $remaining : $block_size; + my $offset = $region_offset + $local_offset; + + my $read = sysread($in_fh, $buffer, $request_size); + die "failed to read from backup source - $!\n" if !defined($read); + die "premature EOF while reading backup source\n" if $read == 0; + + my $written = 0; + while ($written < $read) { + my $res = syswrite($out_fh, $buffer, $request_size - $written, $written); + die "failed to write to backup target - $!\n" if !defined($res); + die "unable to progress writing to backup target\n" if $res == 0; + $written += $res; + } + + if (!$skip_discard) { + eval { PVE::Storage::Common::deallocate($in_fh, $offset, $request_size); }; + if (my $err = $@) { + # Just assume that if one request didn't work, others won't either. + log_warning( + $self, + "discard source failed (skipping further discards) - $err", + ); + $skip_discard = 1; + } + } + + $local_offset += $request_size; + } + } + $out_fh->sync(); }; my $err = $@; - $self->{$vmid}->{previous}->{$device_name} = "$self->{$vmid}->{'archive-subdir'}/${device_name}.qcow2"; + $self->{$vmid}->{previous}->{$device_name} = + "$self->{$vmid}->{'archive-subdir'}/${device_name}.qcow2"; - eval { run_command(['qemu-nbd', '-d', $nbd_node ]); }; + eval { run_command(['qemu-nbd', '-d', $nbd_node]); }; log_warning($self, "unable to disconnect NBD backup target - $@") if $@; die $err if $err; } my sub backup_nbd { - my ($self, $vmid, $device_name, $size, $nbd_path, $bitmap_mode, $bitmap_name, $bandwidth_limit) = @_; + my ($self, $vmid, $device_name, $size, $nbd_path, $bitmap_mode, $bitmap_name, $bandwidth_limit) + = @_; # TODO honor bandwidth_limit @@ -411,69 +424,70 @@ my sub backup_nbd { # set when the bitmap is dirty, see QEMU's docs/interop/nbd.txt my $dirty_bitmap = []; if ($bitmap_mode ne 'none') { - my $input = IO::File->new(); - my $info = IO::File->new(); - $error_fh = IO::File->new(); - my $nbdinfo_cmd = ["nbdinfo", $nbd_info_uri, "--map=qemu:dirty-bitmap:${bitmap_name}"]; - $cpid = open3($input, $info, $error_fh, $nbdinfo_cmd->@*) - or die "failed to spawn nbdinfo child - $!\n"; - - $next_dirty_region = sub { - my ($offset, $length, $type); - do { - my $line = <$info>; - return if !$line; - die "unexpected output from nbdinfo - $line\n" - if $line !~ m/^\s*(\d+)\s*(\d+)\s*(\d+)/; # also untaints - ($offset, $length, $type) = ($1, $2, $3); - } while (($type & 0x1) == 0); # not dirty - return ($offset, $length); - }; + my $input = IO::File->new(); + my $info = IO::File->new(); + $error_fh = IO::File->new(); + my $nbdinfo_cmd = ["nbdinfo", $nbd_info_uri, "--map=qemu:dirty-bitmap:${bitmap_name}"]; + $cpid = open3($input, $info, $error_fh, $nbdinfo_cmd->@*) + or die "failed to spawn nbdinfo child - $!\n"; + + $next_dirty_region = sub { + my ($offset, $length, $type); + do { + my $line = <$info>; + return if !$line; + die "unexpected output from nbdinfo - $line\n" + if $line !~ m/^\s*(\d+)\s*(\d+)\s*(\d+)/; # also untaints + ($offset, $length, $type) = ($1, $2, $3); + } while (($type & 0x1) == 0); # not dirty + return ($offset, $length); + }; } else { - my $done = 0; - $next_dirty_region = sub { - return if $done; - $done = 1; - return (0, $size); - }; + my $done = 0; + $next_dirty_region = sub { + return if $done; + $done = 1; + return (0, $size); + }; } my $nbd_node; eval { - $nbd_node = bind_next_free_dev_nbd_node([$qemu_nbd_uri, "--format=raw", "--discard=on"]); - - my $in_fh = IO::File->new($nbd_node, 'r+') - or die "unable to open NBD backup source '$nbd_node' - $!\n"; - - backup_file( - $self, - $vmid, - $device_name, - $size, - $in_fh, - $bitmap_mode, - $next_dirty_region, - $bandwidth_limit, - ); + $nbd_node = + bind_next_free_dev_nbd_node([$qemu_nbd_uri, "--format=raw", "--discard=on"]); + + my $in_fh = IO::File->new($nbd_node, 'r+') + or die "unable to open NBD backup source '$nbd_node' - $!\n"; + + backup_file( + $self, + $vmid, + $device_name, + $size, + $in_fh, + $bitmap_mode, + $next_dirty_region, + $bandwidth_limit, + ); }; my $err = $@; - eval { run_command(["qemu-nbd", "-d", $nbd_node ]); }; + eval { run_command(["qemu-nbd", "-d", $nbd_node]); }; log_warning($self, "unable to disconnect NBD backup source - $@") if $@; if ($cpid) { - my $waited; - my $wait_limit = 5; - for ($waited = 0; $waited < $wait_limit && waitpid($cpid, POSIX::WNOHANG) == 0; $waited++) { - kill 15, $cpid if $waited == 0; - sleep 1; - } - if ($waited == $wait_limit) { - kill 9, $cpid; - sleep 1; - log_warning($self, "unable to collect nbdinfo child process") - if waitpid($cpid, POSIX::WNOHANG) == 0; - } + my $waited; + my $wait_limit = 5; + for ($waited = 0; $waited < $wait_limit && waitpid($cpid, POSIX::WNOHANG) == 0; $waited++) { + kill 15, $cpid if $waited == 0; + sleep 1; + } + if ($waited == $wait_limit) { + kill 9, $cpid; + sleep 1; + log_warning($self, "unable to collect nbdinfo child process") + if waitpid($cpid, POSIX::WNOHANG) == 0; + } } die $err if $err; @@ -485,29 +499,29 @@ my sub backup_vm_volume { my $backup_mechanism = $self->{'storage-plugin'}->get_vm_backup_mechanism($self->{scfg}); if ($backup_mechanism eq 'nbd') { - backup_nbd( - $self, - $vmid, - $device_name, - $info->{size}, - $info->{'nbd-path'}, - $info->{'bitmap-mode'}, - $info->{'bitmap-name'}, - $bandwidth_limit, - ); + backup_nbd( + $self, + $vmid, + $device_name, + $info->{size}, + $info->{'nbd-path'}, + $info->{'bitmap-mode'}, + $info->{'bitmap-name'}, + $bandwidth_limit, + ); } elsif ($backup_mechanism eq 'file-handle') { - backup_file( - $self, - $vmid, - $device_name, - $info->{size}, - $info->{'file-handle'}, - $info->{'bitmap-mode'}, - $info->{'next-dirty-region'}, - $bandwidth_limit, - ); + backup_file( + $self, + $vmid, + $device_name, + $info->{size}, + $info->{'file-handle'}, + $info->{'bitmap-mode'}, + $info->{'next-dirty-region'}, + $bandwidth_limit, + ); } else { - die "internal error - unknown VM backup mechansim '$backup_mechanism'\n"; + die "internal error - unknown VM backup mechansim '$backup_mechanism'\n"; } } @@ -518,13 +532,18 @@ sub backup_vm { file_set_contents($target, $guest_config); if (my $firewall_config = $info->{'firewall-config'}) { - $target = "$self->{$vmid}->{'backup-dir'}/firewall.conf"; - file_set_contents($target, $firewall_config); + $target = "$self->{$vmid}->{'backup-dir'}/firewall.conf"; + file_set_contents($target, $firewall_config); } for my $device_name (sort keys $volumes->%*) { - backup_vm_volume( - $self, $vmid, $device_name, $volumes->{$device_name}, $info->{'bandwidth-limit'}); + backup_vm_volume( + $self, + $vmid, + $device_name, + $volumes->{$device_name}, + $info->{'bandwidth-limit'}, + ); } } @@ -535,10 +554,10 @@ my sub backup_directory_tar { # copied from PVE::Storage::Plugin::COMMON_TAR_FLAGS my @tar_flags = qw( - --one-file-system - -p --sparse --numeric-owner --acls - --xattrs --xattrs-include=user.* --xattrs-include=security.capability - --warning=no-file-ignored --warning=no-xattr-write + --one-file-system + -p --sparse --numeric-owner --acls + --xattrs --xattrs-include=user.* --xattrs-include=security.capability + --warning=no-file-ignored --warning=no-xattr-write ); my $tar = ['tar', 'cpf', '-', '--totals', @tar_flags]; @@ -548,11 +567,11 @@ my sub backup_directory_tar { my @exclude_no_anchored = (); my @exclude_anchored = (); for my $pattern ($exclude_patterns->@*) { - if ($pattern !~ m|^/|) { - push @exclude_no_anchored, $pattern; - } else { - push @exclude_anchored, $pattern; - } + if ($pattern !~ m|^/|) { + push @exclude_no_anchored, $pattern; + } else { + push @exclude_anchored, $pattern; + } } push @$tar, '--no-anchored'; @@ -564,22 +583,22 @@ my sub backup_directory_tar { push @$tar, '--', $sources->@*; - my $cmd = [ $tar ]; + my $cmd = [$tar]; - push @$cmd, [ 'cstream', '-t', $bandwidth_limit * 1024 ] if $bandwidth_limit; + push @$cmd, ['cstream', '-t', $bandwidth_limit * 1024] if $bandwidth_limit; my $target = "$self->{$vmid}->{'backup-dir'}/archive.tar"; - push @{$cmd->[-1]}, \(">" . PVE::Tools::shellquote($target)); + push @{ $cmd->[-1] }, \(">" . PVE::Tools::shellquote($target)); my $logfunc = sub { - my $line = shift; - log_info($self, "tar: $line"); + my $line = shift; + log_info($self, "tar: $line"); }; PVE::Tools::run_command($cmd, logfunc => $logfunc); return; -}; +} # NOTE This only serves as an example to illustrate the 'directory' restore mechanism. It is not # fleshed out properly, e.g. I didn't check if exclusion is compatible with @@ -594,26 +613,26 @@ my sub backup_directory_squashfs { push $mksquashfs->@*, '-wildcards'; for my $pattern ($exclude_patterns->@*) { - if ($pattern !~ m|^/|) { # non-anchored - push $mksquashfs->@*, '-e', "... $pattern"; - } else { # anchored - push $mksquashfs->@*, '-e', substr($pattern, 1); # need to strip leading slash - } + if ($pattern !~ m|^/|) { # non-anchored + push $mksquashfs->@*, '-e', "... $pattern"; + } else { # anchored + push $mksquashfs->@*, '-e', substr($pattern, 1); # need to strip leading slash + } } - my $cmd = [ $mksquashfs ]; + my $cmd = [$mksquashfs]; - push @$cmd, [ 'cstream', '-t', $bandwidth_limit * 1024 ] if $bandwidth_limit; + push @$cmd, ['cstream', '-t', $bandwidth_limit * 1024] if $bandwidth_limit; my $logfunc = sub { - my $line = shift; - log_info($self, "mksquashfs: $line"); + my $line = shift; + log_info($self, "mksquashfs: $line"); }; PVE::Tools::run_command($cmd, logfunc => $logfunc); return; -}; +} sub backup_container { my ($self, $vmid, $guest_config, $exclude_patterns, $info) = @_; @@ -622,30 +641,30 @@ sub backup_container { file_set_contents($target, $guest_config); if (my $firewall_config = $info->{'firewall-config'}) { - $target = "$self->{$vmid}->{'backup-dir'}/firewall.conf"; - file_set_contents($target, $firewall_config); + $target = "$self->{$vmid}->{'backup-dir'}/firewall.conf"; + file_set_contents($target, $firewall_config); } my $backup_mode = $self->{'storage-plugin'}->get_lxc_backup_mode($self->{scfg}); if ($backup_mode eq 'tar') { - backup_directory_tar( - $self, - $vmid, - $info->{directory}, - $exclude_patterns, - $info->{sources}, - $info->{'bandwidth-limit'}, - ); + backup_directory_tar( + $self, + $vmid, + $info->{directory}, + $exclude_patterns, + $info->{sources}, + $info->{'bandwidth-limit'}, + ); } elsif ($backup_mode eq 'squashfs') { - backup_directory_squashfs( - $self, - $vmid, - $info->{directory}, - $exclude_patterns, - $info->{'bandwidth-limit'}, - ); + backup_directory_squashfs( + $self, + $vmid, + $info->{directory}, + $exclude_patterns, + $info->{'bandwidth-limit'}, + ); } else { - die "got unexpected backup mode '$backup_mode' from storage plugin\n"; + die "got unexpected backup mode '$backup_mode' from storage plugin\n"; } } @@ -660,19 +679,19 @@ sub restore_get_mechanism { return ('qemu-img', $vmtype) if $vmtype eq 'qemu'; if ($vmtype eq 'lxc') { - my (undef, $relative_backup_dir) = $self->{'storage-plugin'}->parse_volname($volname); + my (undef, $relative_backup_dir) = $self->{'storage-plugin'}->parse_volname($volname); - if (-e "$self->{scfg}->{path}/${relative_backup_dir}/archive.tar") { - $self->{'restore-mechanisms'}->{$volname} = 'tar'; - return ('tar', $vmtype); - } + if (-e "$self->{scfg}->{path}/${relative_backup_dir}/archive.tar") { + $self->{'restore-mechanisms'}->{$volname} = 'tar'; + return ('tar', $vmtype); + } - if (-e "$self->{scfg}->{path}/${relative_backup_dir}/archive.sqfs") { - $self->{'restore-mechanisms'}->{$volname} = 'directory'; - return ('directory', $vmtype) - } + if (-e "$self->{scfg}->{path}/${relative_backup_dir}/archive.sqfs") { + $self->{'restore-mechanisms'}->{$volname} = 'directory'; + return ('directory', $vmtype); + } - die "unable to find archive '$volname'\n"; + die "unable to find archive '$volname'\n"; } die "cannot restore unexpected guest type '$vmtype'\n"; @@ -708,9 +727,9 @@ sub restore_vm_init { my @backup_files = glob("$backup_dir/*"); for my $backup_file (@backup_files) { - next if $backup_file !~ m!^(.*/(.*)\.qcow2)$!; - $backup_file = $1; # untaint - $res->{$2}->{size} = PVE::Storage::Plugin::file_size_info($backup_file, undef, 'qcow2'); + next if $backup_file !~ m!^(.*/(.*)\.qcow2)$!; + $backup_file = $1; # untaint + $res->{$2}->{size} = PVE::Storage::Plugin::file_size_info($backup_file, undef, 'qcow2'); } return $res; @@ -735,7 +754,7 @@ sub restore_vm_volume_init { my $nbd_node = bind_next_free_dev_nbd_node([$image]); $self->{"${volname}/${device_name}"}->{'nbd-node'} = $nbd_node; return { - 'qemu-img-path' => $nbd_node, + 'qemu-img-path' => $nbd_node, }; } @@ -743,7 +762,7 @@ sub restore_vm_volume_cleanup { my ($self, $volname, $device_name, $info) = @_; if (my $nbd_node = delete($self->{"${volname}/${device_name}"}->{'nbd-node'})) { - PVE::Tools::run_command(['qemu-nbd', '-d', $nbd_node]); + PVE::Tools::run_command(['qemu-nbd', '-d', $nbd_node]); } return; @@ -786,11 +805,11 @@ sub restore_container_init { my ($self, $volname, $info) = @_; if ($self->{'restore-mechanisms'}->{$volname} eq 'tar') { - return restore_tar_init($self, $volname); + return restore_tar_init($self, $volname); } elsif ($self->{'restore-mechanisms'}->{$volname} eq 'directory') { - return restore_directory_init($self, $volname); + return restore_directory_init($self, $volname); } else { - die "no restore mechanism set for '$volname'\n"; + die "no restore mechanism set for '$volname'\n"; } } @@ -798,11 +817,11 @@ sub restore_container_cleanup { my ($self, $volname, $info) = @_; if ($self->{'restore-mechanisms'}->{$volname} eq 'tar') { - return; # nothing to do + return; # nothing to do } elsif ($self->{'restore-mechanisms'}->{$volname} eq 'directory') { - return restore_directory_cleanup($self, $volname); + return restore_directory_cleanup($self, $volname); } else { - die "no restore mechanism set for '$volname'\n"; + die "no restore mechanism set for '$volname'\n"; } } -- 2.47.3