[pve-devel] [POC storage 12/23] add backup provider example

Fiona Ebner f.ebner at proxmox.com
Tue Jul 23 11:56:13 CEST 2024


The example uses a simple directory structure to save the backups,
grouped by guest ID. VM backups are saved as configuration files and
qcow2 images, with backing files when doing incremental backups.
Container backups are saved as configuration files and a tar file or
squashfs image (added to test the 'directory' restore mechanism).

Whether to use incremental VM backups and how to save container
archives can be configured in the storage configuration.

The 'nbdinfo' binary from the 'libnbd-bin' package is required for
VM backups, the 'mksquashfs' binary from the 'squashfs-tools' package
is required for backup mechanism 'squashfs' for containers.

Signed-off-by: Fiona Ebner <f.ebner at proxmox.com>
---
 src/PVE/BackupProvider/DirectoryExample.pm    | 533 ++++++++++++++++++
 src/PVE/BackupProvider/Makefile               |   2 +-
 .../Custom/BackupProviderDirExamplePlugin.pm  | 289 ++++++++++
 src/PVE/Storage/Custom/Makefile               |   5 +
 src/PVE/Storage/Makefile                      |   1 +
 5 files changed, 829 insertions(+), 1 deletion(-)
 create mode 100644 src/PVE/BackupProvider/DirectoryExample.pm
 create mode 100644 src/PVE/Storage/Custom/BackupProviderDirExamplePlugin.pm
 create mode 100644 src/PVE/Storage/Custom/Makefile

diff --git a/src/PVE/BackupProvider/DirectoryExample.pm b/src/PVE/BackupProvider/DirectoryExample.pm
new file mode 100644
index 0000000..1de3b93
--- /dev/null
+++ b/src/PVE/BackupProvider/DirectoryExample.pm
@@ -0,0 +1,533 @@
+package PVE::BackupProvider::DirectoryExample;
+
+use strict;
+use warnings;
+
+use Fcntl qw(SEEK_SET);
+use File::Path qw(mkpath rmtree);
+use IO::File;
+use JSON;
+
+use PVE::Storage::Plugin;
+use PVE::Tools qw(file_get_contents file_read_firstline file_set_contents run_command);
+
+use base qw(PVE::BackupProvider::Plugin);
+
+use constant {
+    BLKDISCARD => 0x1277, # see linux/fs.h
+};
+
+# Private helpers
+
+my sub log_dir {
+    my ($scfg) = @_;
+
+    return "$scfg->{path}/log";
+}
+
+# Try to use the same bitmap ID as last time for incremental backup if the storage is configured for
+# incremental VM backup. Need to start fresh if there is no previous ID or the associated backup
+# doesn't exist.
+my sub get_bitmap_id {
+    my ($self, $vmid, $vmtype) = @_;
+
+    return if $self->{'storage-plugin'}->get_vm_backup_mode($self->{scfg}) ne 'incremental';
+
+    my $previous_info_dir = "$self->{scfg}->{path}/$vmid/";
+
+    my $previous_info_file = "$previous_info_dir/previous-info";
+    my $info = file_read_firstline($previous_info_file) // '';
+    $self->{$vmid}->{'old-previous-info'} = $info;
+    my ($bitmap_id, $previous_backup_id) = $info =~ m/^(\d+)\s+(\d+)$/;
+    my $previous_backup_dir =
+	$previous_backup_id ? "$self->{scfg}->{path}/$vmid/$vmtype-$previous_backup_id" : undef;
+
+    if ($bitmap_id && -d $previous_backup_dir) {
+	$self->{$vmid}->{'previous-backup-dir'} = $previous_backup_dir;
+    } else {
+	# need to start fresh if there is no previous ID or the associated backup doesn't exist
+	$bitmap_id = $self->{'job-id'};
+    }
+
+    $self->{$vmid}->{'bitmap-id'} = $bitmap_id;
+    mkpath $previous_info_dir;
+    file_set_contents($previous_info_file, "$bitmap_id $self->{'job-id'}");
+
+    return $bitmap_id;
+}
+
+# Backup Provider API
+
+sub new {
+    my ($class, $storage_plugin, $scfg, $storeid, $log_function) = @_;
+
+    die "need 'nbdinfo' binary from package libnbd-bin\n" if !-e "/usr/bin/nbdinfo";
+
+    my $self = bless {
+	scfg => $scfg,
+	storeid => $storeid,
+	'storage-plugin' => $storage_plugin,
+	'log-info' => sub { $log_function->("info", $_[0]) },
+	'log-warning' => sub { $log_function->("warn", $_[0]) },
+	'log-error' => sub { $log_function->("err", $_[0]) },
+    }, $class;
+
+    return $self;
+}
+
+sub provider_name {
+    my ($self) = @_;
+
+    return 'dir provider example';
+}
+
+# Hooks
+
+sub job_start {
+    my ($self, $start_time) = @_;
+
+    my $log_dir = log_dir($self->{scfg});
+
+    mkpath $log_dir;
+
+    my $log_file = "${log_dir}/${start_time}.log";
+
+    $self->{'job-id'} = $start_time;
+
+    $self->{'log-info'}->("job start hook called");
+
+    run_command(["modprobe", "nbd"]);
+
+    $self->{'log-info'}->("backup provider initialized successfully for new job $start_time");
+}
+
+sub job_end {
+    my ($self) = @_;
+
+    $self->{'log-info'}->("job end hook called");
+}
+
+sub job_abort {
+    my ($self, $error) = @_;
+
+    $self->{'log-info'}->("job abort hook called");
+}
+
+sub backup_start {
+    my ($self, $vmid, $vmtype) = @_;
+
+    $self->{'log-info'}->("backup start hook called");
+
+    my $backup_dir = $self->{scfg}->{path} . "/" . $self->backup_get_target($vmid, $vmtype);
+
+    mkpath $backup_dir;
+    $self->{$vmid}->{'backup-dir'} = $backup_dir;
+    $self->{$vmid}->{'task-size'} = 0;
+}
+
+sub backup_end {
+    my ($self, $vmid) = @_;
+
+    $self->{'log-info'}->("backup end hook called");
+}
+
+sub backup_abort {
+    my ($self, $vmid, $error) = @_;
+
+    $self->{'log-info'}->("backup abort hook called");
+
+    $self->{$vmid}->{failed} = 1;
+
+    my $dir = $self->{$vmid}->{'backup-dir'};
+
+    eval { rmtree $dir };
+    $self->{'log-warning'}->("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);
+    }
+}
+
+sub backup_get_mechanism {
+    my ($self, $vmid, $vmtype) = @_;
+
+    return ('directory', undef) if $vmtype eq 'lxc';
+    return ('nbd', get_bitmap_id($self, $vmid, $vmtype)) if $vmtype eq 'qemu';
+
+    die "unsupported guest type '$vmtype'\n";
+}
+
+sub backup_get_target {
+    my ($self, $vmid, $vmtype) = @_;
+
+    return "${vmid}/${vmtype}-$self->{'job-id'}";
+}
+
+sub backup_get_task_size {
+    my ($self, $vmid) = @_;
+
+    return $self->{$vmid}->{'task-size'};
+}
+
+sub backup_guest_config {
+    my ($self, $vmid, $filename) = @_;
+
+    my $data = file_get_contents($filename);
+    my $target = "$self->{$vmid}->{'backup-dir'}/guest.conf";
+    file_set_contents($target, $data);
+
+    $self->{$vmid}->{'task-size'} += -s $target;
+}
+
+sub backup_firewall_config {
+    my ($self, $vmid, $filename) = @_;
+
+    my $data = file_get_contents($filename);
+    my $target = "$self->{$vmid}->{'backup-dir'}/firewall.conf";
+    file_set_contents($target, $data);
+
+    $self->{$vmid}->{'task-size'} += -s $target;
+}
+
+sub backup_handle_log_file {
+    my ($self, $vmid, $filename) = @_;
+
+    my $log_dir = $self->{$vmid}->{'backup-dir'};
+    if ($self->{$vmid}->{failed}) {
+	$log_dir .= ".failed";
+    }
+    mkpath $log_dir;
+
+    my $data = file_get_contents($filename);
+    my $target = "${log_dir}/backup.log";
+    file_set_contents($target, $data);
+}
+
+sub backup_nbd {
+    my ($self, $vmid, $devicename, $nbd_path, $bitmap_mode, $bitmap_name, $bandwidth_limit) = @_;
+
+    # TODO honor bandwidth_limit
+
+    my $target = "$self->{$vmid}->{'backup-dir'}/${devicename}.qcow2";
+    my $previous_backup_dir = $self->{$vmid}->{'previous-backup-dir'};
+    my $incremental = $previous_backup_dir && $bitmap_mode eq 'reuse';
+
+    my $target_base = $incremental ? "${previous_backup_dir}/${devicename}.qcow2" : undef;
+
+    my $nbd_info_uri = "nbd+unix:///${devicename}?socket=${nbd_path}";
+    my $qemu_nbd_uri = "nbd:unix:${nbd_path}:exportname=${devicename}";
+
+    my $size;
+    my $parse_size = sub {
+	my ($line) = @_;
+	if ($line =~ m/^(\d+)$/) {
+	    die "got duplicate output from nbdinfo while querying size - $line\n" if defined($size);
+	    $size = $1;
+	} else {
+	    die "got unexpected output from nbdinfo while querying size - $line\n";
+	}
+    };
+
+    my $errfunc = sub { $self->{'log-warning'}->("$_[0]\n"); };
+
+    run_command(["nbdinfo", "--size", $nbd_info_uri], outfunc => $parse_size, errfunc => $errfunc);
+    my $create_cmd = ["qemu-img", "create", "-f", "qcow2", $target, $size];
+    push $create_cmd->@*, "-b", $target_base, "-F", "qcow2" if $target_base;
+    run_command($create_cmd);
+
+    # If there is no dirty bitmap, it can be treated as if there's a full dirty one.
+    # Is an array of hashes with offset, length, type, description. The first bit of 'type' is set
+    # when the bitmap is dirty, see QEMU's docs/interop/nbd.txt
+    my $dirty_bitmap;
+    if ($bitmap_mode ne 'none') {
+	my $json = '';
+	run_command(
+	    ["nbdinfo", $nbd_info_uri, "--map=qemu:dirty-bitmap:${bitmap_name}", "--json"],
+	    outfunc => sub { $json .= $_[0]; },
+	    errfunc => $errfunc,
+	);
+	$dirty_bitmap = eval { decode_json($json); };
+	die "failed to decode JSON for dirty bitmap status - $@" if $@;
+    } else {
+	$dirty_bitmap = [{ offset =>  0, length => $size, type => 1, description => "dirty" }];
+    }
+    eval {
+	run_command(["qemu-nbd", "-c", "/dev/nbd0", $qemu_nbd_uri, "--format=raw", "--discard=on"]);
+	# allows to easily write to qcow2 target
+	run_command(["qemu-nbd", "-c", "/dev/nbd1", $target, "--format=qcow2"]);
+
+	my $block_size = 4 * 1024 * 1024; # 4 MiB
+
+	my $in_fh = IO::File->new("/dev/nbd0", "r+")
+	    or die "unable to open NBD backup source - $!\n";
+	my $out_fh = IO::File->new("/dev/nbd1", "r+")
+	    or die "unable to open NBD backup target - $!\n";
+
+	my $buffer = '';
+
+	for my $region ($dirty_bitmap->@*) {
+	    next if ($region->{type} & 0x1) == 0; # not dirty
+	    my ($region_length) = $region->{length} =~ /^(\d+)$/; # untaint
+	    my ($region_offset) = $region->{offset} =~ /^(\d+)$/; # untaint
+
+	    sysseek($in_fh, $region_offset, SEEK_SET)
+		// die "unable to seek '$region_offset' in NBD backup source - $!";
+	    sysseek($out_fh, $region_offset, SEEK_SET)
+		// die "unable to seek '$region_offset' in NBD backup target - $!";
+
+	    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;
+		}
+
+		ioctl($in_fh, BLKDISCARD, pack('QQ', int($offset), int($request_size)));
+
+		$local_offset += $request_size;
+	    }
+	}
+    };
+    my $err = $@;
+
+    eval { run_command(["qemu-nbd", "-d", "/dev/nbd0" ]); };
+    $self->{'log-warning'}->("unable to disconnect NBD backup source - $@") if $@;
+    eval { run_command(["qemu-nbd", "-d", "/dev/nbd1" ]); };
+    $self->{'log-warning'}->("unable to disconnect NBD backup target - $@") if $@;
+
+    die $err if $err;
+}
+
+my sub backup_directory_tar {
+    my ($self, $vmid, $directory, $userns_cmd, $exclude_paths, $sources, $bandwidth_limit) = @_;
+
+    # essentially copied from PVE/VZDump/LXC.pm' archive()
+
+    # 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
+    );
+
+    my $tar = [$userns_cmd->@*, 'tar', 'cpf', '-', '--totals', @tar_flags];
+
+    push @$tar, "--directory=$directory";
+
+    my @exclude_no_anchored = ();
+    my @exclude_anchored = ();
+    for my $pattern ($exclude_paths->@*) {
+	if ($pattern !~ m|^/|) {
+	    push @exclude_no_anchored, $pattern;
+	} else {
+	    push @exclude_anchored, $pattern;
+	}
+    }
+
+    push @$tar, '--no-anchored';
+    push @$tar, '--exclude=lost+found' if scalar($userns_cmd->@*) > 0;
+    push @$tar, map { "--exclude=$_" } @exclude_no_anchored;
+
+    push @$tar, '--anchored';
+    push @$tar, map { "--exclude=.$_" } @exclude_anchored;
+
+    push @$tar, $sources->@*;
+
+    my $cmd = [ $tar ];
+
+    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));
+
+    my $logfunc = sub {
+	my $line = shift;
+	$self->{'log-info'}->("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
+# proxmox-backup-client/rsync or xattrs/ACL/etc. work as expected!
+my sub backup_directory_squashfs {
+    my ($self, $vmid, $directory, $exclude_paths, $bandwidth_limit) = @_;
+
+    my $target = "$self->{$vmid}->{'backup-dir'}/archive.sqfs";
+
+    my $mksquashfs = ['mksquashfs', $directory, $target, '-quiet', '-no-progress'];
+
+    push $mksquashfs->@*, '-wildcards';
+
+    for my $pattern ($exclude_paths->@*) {
+	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 ];
+
+    push @$cmd, [ 'cstream', '-t', $bandwidth_limit * 1024 ] if $bandwidth_limit;
+
+    my $logfunc = sub {
+	my $line = shift;
+	$self->{'log-info'}->("mksquashfs: $line");
+    };
+
+    PVE::Tools::run_command($cmd, logfunc => $logfunc);
+
+    return;
+};
+
+sub backup_directory {
+    my ($self, $vmid, $directory, $id_map, $exclude_paths, $sources, $bandwidth_limit) = @_;
+
+    my $userns_cmd = [];
+    # copied from PVE::LXC::userns_command
+    $userns_cmd = ['lxc-usernsexec', (map { ('-m', join(':', $_->@*)) } $id_map->@*), '--']
+	if scalar($id_map->@*) > 0;
+
+    my $backup_mode = $self->{'storage-plugin'}->get_lxc_backup_mode($self->{scfg});
+    if ($backup_mode eq 'tar') {
+	backup_directory_tar(
+	    $self, $vmid, $directory, $userns_cmd, $exclude_paths, $sources, $bandwidth_limit);
+    } elsif ($backup_mode eq 'squashfs') {
+	backup_directory_squashfs($self, $vmid, $directory, $exclude_paths, $bandwidth_limit);
+    } else {
+	die "got unexpected backup mode '$backup_mode' from storage plugin\n";
+    }
+}
+
+# Restore API
+
+sub restore_get_mechanism {
+    my ($self, $volname, $storeid) = @_;
+
+    my (undef, $relative_backup_dir) = $self->{'storage-plugin'}->parse_volname($volname);
+    my ($vmtype) = $relative_backup_dir =~ m!^\d+/([a-z]+)-!;
+
+    return ('qemu-img', $vmtype) if $vmtype eq 'qemu';
+
+    if ($vmtype eq 'lxc') {
+	my (undef, $relative_backup_dir) = $self->{'storage-plugin'}->parse_volname($volname);
+	return ('tar', $vmtype) if -e "$self->{scfg}->{path}/${relative_backup_dir}/archive.tar";
+	return ('directory', $vmtype)
+	    if -e "$self->{scfg}->{path}/${relative_backup_dir}/archive.sqfs";
+
+	die "unable to find archive '$volname'\n";
+    }
+
+    die "cannot restore unexpected guest type '$vmtype'\n";
+}
+
+sub extract_guest_config {
+    my ($self, $volname, $storeid) = @_;
+
+    my (undef, $relative_backup_dir) = $self->{'storage-plugin'}->parse_volname($volname);
+    my $filename = "$self->{scfg}->{path}/${relative_backup_dir}/guest.conf";
+
+    return file_get_contents($filename);
+}
+
+sub extract_firewall_config {
+    my ($self, $volname, $storeid) = @_;
+
+    my (undef, $relative_backup_dir) = $self->{'storage-plugin'}->parse_volname($volname);
+    my $filename = "$self->{scfg}->{path}/${relative_backup_dir}/firewall.conf";
+
+    return if !-e $filename;
+
+    return file_get_contents($filename);
+}
+
+sub restore_qemu_get_device_info {
+    my ($self, $volname, $storeid) = @_;
+
+    my $res = {};
+
+    my (undef, $relative_backup_dir) = $self->{'storage-plugin'}->parse_volname($volname);
+    my $backup_dir = "$self->{scfg}->{path}/${relative_backup_dir}";
+
+    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);
+    }
+
+    return $res;
+}
+
+sub restore_qemu_img_init {
+    my ($self, $volname, $storeid, $devicename) = @_;
+
+    my (undef, $relative_backup_dir) = $self->{'storage-plugin'}->parse_volname($volname);
+    return "$self->{scfg}->{path}/${relative_backup_dir}/${devicename}.qcow2";
+}
+
+sub restore_qemu_img_cleanup {
+    my ($self, $volname, $storeid, $devicename) = @_;
+
+    return;
+}
+
+sub restore_tar_init {
+    my ($self, $volname, $storeid) = @_;
+
+    my (undef, $relative_backup_dir) = $self->{'storage-plugin'}->parse_volname($volname);
+    return "$self->{scfg}->{path}/${relative_backup_dir}/archive.tar";
+}
+
+sub restore_tar_cleanup {
+    my ($self, $volname, $storeid) = @_;
+
+    return;
+}
+
+sub restore_directory_init {
+    my ($self, $volname, $storeid) = @_;
+
+    my (undef, $relative_backup_dir, $vmid) = $self->{'storage-plugin'}->parse_volname($volname);
+    my $archive = "$self->{scfg}->{path}/${relative_backup_dir}/archive.sqfs";
+
+    my $mount_point = "/run/backup-provider-example/${vmid}.mount";
+    mkpath $mount_point;
+
+    run_command(['mount', '-o', 'ro', $archive, $mount_point]);
+
+    return $mount_point;
+}
+
+sub restore_directory_cleanup {
+    my ($self, $volname, $storeid) = @_;
+
+    my (undef, undef, $vmid) = $self->{'storage-plugin'}->parse_volname($volname);
+    my $mount_point = "/run/backup-provider-example/${vmid}.mount";
+
+    run_command(['umount', $mount_point]);
+
+    return;
+}
+
+1;
diff --git a/src/PVE/BackupProvider/Makefile b/src/PVE/BackupProvider/Makefile
index 53cccac..94b3c30 100644
--- a/src/PVE/BackupProvider/Makefile
+++ b/src/PVE/BackupProvider/Makefile
@@ -1,4 +1,4 @@
-SOURCES = Plugin.pm
+SOURCES = DirectoryExample.pm Plugin.pm
 
 .PHONY: install
 install:
diff --git a/src/PVE/Storage/Custom/BackupProviderDirExamplePlugin.pm b/src/PVE/Storage/Custom/BackupProviderDirExamplePlugin.pm
new file mode 100644
index 0000000..ec616f3
--- /dev/null
+++ b/src/PVE/Storage/Custom/BackupProviderDirExamplePlugin.pm
@@ -0,0 +1,289 @@
+package PVE::Storage::Custom::BackupProviderDirExamplePlugin;
+
+use strict;
+use warnings;
+
+use File::Basename qw(basename);
+
+use PVE::BackupProvider::DirectoryExample;
+use PVE::Tools;
+
+use base qw(PVE::Storage::Plugin);
+
+# Helpers
+
+sub get_vm_backup_mode {
+    my ($class, $scfg) = @_;
+
+    return $scfg->{'vm-backup-mode'} // properties()->{'vm-backup-mode'}->{'default'};
+}
+
+sub get_lxc_backup_mode {
+    my ($class, $scfg) = @_;
+
+    return $scfg->{'lxc-backup-mode'} // properties()->{'lxc-backup-mode'}->{'default'};
+}
+
+# Configuration
+
+sub api {
+    return 11;
+}
+
+sub type {
+    return 'backup-provider-dir-example';
+}
+
+sub plugindata {
+    return {
+	content => [ { backup => 1, none => 1 }, { backup => 1 }],
+    };
+}
+
+sub properties {
+    return {
+	'lxc-backup-mode' => {
+	    description => "How to create LXC backups. tar - create a tar archive."
+		." squashfs - create a squashfs image. Requires squashfs-tools to be installed.",
+	    type => 'string',
+	    enum => [qw(tar squashfs)],
+	    default => 'tar',
+	},
+	'vm-backup-mode' => {
+	    description => "How to create backups. full - always create full backups."
+		." incremental - create incremental backups when possible, fallback to full when"
+		." necessary, e.g. guest is a container, VM disk's bitmap is invalid.",
+	    type => 'string',
+	    enum => [qw(full incremental)],
+	    default => 'full',
+	},
+    };
+}
+
+sub options {
+    return {
+	path => { fixed => 1 },
+	'lxc-backup-mode' => { optional => 1 },
+	'vm-backup-mode' => { optional => 1 },
+    };
+}
+
+# Storage implementation
+
+# NOTE a proper backup storage should implement this
+sub prune_backups {
+    my ($class, $scfg, $storeid, $keep, $vmid, $type, $dryrun, $logfunc) = @_;
+
+    die "not implemented";
+}
+
+sub parse_volname {
+    my ($class, $volname) = @_;
+
+    if ($volname =~ m!^backup/((\d+)/[a-z]+-\d+)$!) {
+	my ($filename, $vmid) = ($1, $2);
+	return ('backup', $filename, $vmid);
+    }
+
+    die "unable to parse volume name '$volname'\n";
+}
+
+sub path {
+    my ($class, $scfg, $volname, $storeid, $snapname) = @_;
+
+    die "volume snapshot is not possible on backup-provider-example volume" if $snapname;
+
+    my ($type, $filename, $vmid) = $class->parse_volname($volname);
+
+    return ("$scfg->{path}/${filename}", $vmid, $type);
+}
+
+sub create_base {
+    my ($class, $storeid, $scfg, $volname) = @_;
+
+    die "cannot create base image in backup-provider storage\n";
+}
+
+sub clone_image {
+    my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
+
+    die "can't clone images in backup-provider-example storage\n";
+}
+
+sub alloc_image {
+    my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
+
+    die "can't allocate space in backup-provider-example storage\n";
+}
+
+# NOTE a proper backup storage should implement this
+sub free_image {
+    my ($class, $storeid, $scfg, $volname, $isBase) = @_;
+
+    # if it's a backing file, it would need to be merged into the upper image first.
+
+    die "not implemented";
+}
+
+sub list_images {
+    my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
+
+    my $res = [];
+
+    return $res;
+}
+
+sub list_volumes {
+    my ($class, $storeid, $scfg, $vmid, $content_types) = @_;
+
+    my $path = $scfg->{path};
+
+    my $res = [];
+    for my $type ($content_types->@*) {
+	next if $type ne 'backup';
+
+	my @guest_dirs = glob("$path/*");
+	for my $guest_dir (@guest_dirs) {
+	    next if !-d $guest_dir || $guest_dir !~ m!/(\d+)$!;
+
+	    my $vmid = basename($guest_dir);
+
+	    my @backup_dirs = glob("$guest_dir/*");
+	    for my $backup_dir (@backup_dirs) {
+		next if !-d $backup_dir || $backup_dir !~ m!/(lxc|qemu)-(\d+)$!;
+		my ($subtype, $backup_id) = ($1, $2);
+
+		my $size = 0;
+		my @backup_files = glob("$backup_dir/*");
+		$size += -s $_ for @backup_files;
+
+		push $res->@*, {
+		    volid => "$storeid:backup/$vmid/$subtype-$backup_id",
+		    vmid => $vmid,
+		    format => "directory",
+		    ctime => $backup_id,
+		    size => $size,
+		    subtype => $subtype,
+		    content => $type,
+		    # TODO parent for incremental
+		};
+	    }
+	}
+    }
+
+    return $res;
+}
+
+sub activate_storage {
+    my ($class, $storeid, $scfg, $cache) = @_;
+
+    my $path = $scfg->{path};
+
+    my $timeout = 2;
+    if (!PVE::Tools::run_fork_with_timeout($timeout, sub {-d $path})) {
+	die "unable to activate storage '$storeid' - directory '$path' does not exist or is"
+	    ." unreachable\n";
+    }
+
+    return 1;
+}
+
+sub deactivate_storage {
+    my ($class, $storeid, $scfg, $cache) = @_;
+
+    return 1;
+}
+
+sub activate_volume {
+    my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
+
+    die "volume snapshot is not possible on backup-provider-example volume" if $snapname;
+
+    return 1;
+}
+
+sub deactivate_volume {
+    my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
+
+    die "volume snapshot is not possible on backup-provider-example volume" if $snapname;
+
+    return 1;
+}
+
+sub get_volume_attribute {
+    my ($class, $scfg, $storeid, $volname, $attribute) = @_;
+
+    return;
+}
+
+# NOTE a proper backup storage should implement this to support backup notes and
+# setting protected status.
+sub update_volume_attribute {
+    my ($class, $scfg, $storeid, $volname, $attribute, $value) = @_;
+
+    die "attribute '$attribute' is not supported on backup-provider-example volume";
+}
+
+sub volume_size_info {
+    my ($class, $scfg, $storeid, $volname, $timeout) = @_;
+
+    my (undef, $relative_backup_dir) = $class->parse_volname($volname);
+    my ($ctime) = $relative_backup_dir =~ m/-(\d+)$/;
+    my $backup_dir = "$scfg->{path}/${relative_backup_dir}";
+
+    my $size = 0;
+    my @backup_files = glob("$backup_dir/*");
+    for my $backup_file (@backup_files) {
+	if ($backup_file =~ m!\.qcow2$!) {
+	    $size += $class->file_size_info($backup_file);
+	} else {
+	    $size += -s $backup_file;
+	}
+    }
+
+    my $parent; # TODO for incremental
+
+    return wantarray ? ($size, 'directory', $size, $parent, $ctime) : $size;
+}
+
+sub volume_resize {
+    my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
+
+    die "volume resize is not possible on backup-provider-example volume";
+}
+
+sub volume_snapshot {
+    my ($class, $scfg, $storeid, $volname, $snap) = @_;
+
+    die "volume snapshot is not possible on backup-provider-example volume";
+}
+
+sub volume_snapshot_rollback {
+    my ($class, $scfg, $storeid, $volname, $snap) = @_;
+
+    die "volume snapshot rollback is not possible on backup-provider-example volume";
+}
+
+sub volume_snapshot_delete {
+    my ($class, $scfg, $storeid, $volname, $snap) = @_;
+
+    die "volume snapshot delete is not possible on backup-provider-example volume";
+}
+
+sub volume_has_feature {
+    my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
+
+    return 0;
+}
+
+# Get the backup provider class for a new backup job.
+#
+# $bandwidth_limit is in KiB/s
+sub new_backup_provider {
+    my ($class, $scfg, $storeid, $bandwidth_limit, $log_function) = @_;
+
+    return PVE::BackupProvider::DirectoryExample->new(
+	$class, $scfg, $storeid, $bandwidth_limit, $log_function);
+}
+
+1;
diff --git a/src/PVE/Storage/Custom/Makefile b/src/PVE/Storage/Custom/Makefile
new file mode 100644
index 0000000..c1e3eca
--- /dev/null
+++ b/src/PVE/Storage/Custom/Makefile
@@ -0,0 +1,5 @@
+SOURCES = BackupProviderDirExamplePlugin.pm
+
+.PHONY: install
+install:
+	for i in ${SOURCES}; do install -D -m 0644 $$i ${DESTDIR}${PERLDIR}/PVE/Storage/Custom/$$i; done
diff --git a/src/PVE/Storage/Makefile b/src/PVE/Storage/Makefile
index d5cc942..acd37f4 100644
--- a/src/PVE/Storage/Makefile
+++ b/src/PVE/Storage/Makefile
@@ -19,4 +19,5 @@ SOURCES= \
 .PHONY: install
 install:
 	for i in ${SOURCES}; do install -D -m 0644 $$i ${DESTDIR}${PERLDIR}/PVE/Storage/$$i; done
+	make -C Custom install
 	make -C LunCmd install
-- 
2.39.2





More information about the pve-devel mailing list