[pve-devel] [RFC manager 1/2] api: backup: Add endpoint for disk included status

Aaron Lauterer a.lauterer at proxmox.com
Thu Dec 12 11:27:44 CET 2019


This endpoint provides information if disks and mountpoints of guests are
included in a VZDump job.

The returned object is formatted to be used with the TreePanel of ExtJS.

Signed-off-by: Aaron Lauterer <a.lauterer at proxmox.com>
---

Unfortunately the logic of which disk / mp will be included is deeply
engrained in the LXC and Qemu VZDump plugins and hard to use outside.
Since AFAIU there are not that many cases I implemented the checks
again.

 PVE/API2/Backup.pm | 168 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 168 insertions(+)

diff --git a/PVE/API2/Backup.pm b/PVE/API2/Backup.pm
index 86377c0a..e7baaa3b 100644
--- a/PVE/API2/Backup.pm
+++ b/PVE/API2/Backup.pm
@@ -324,4 +324,172 @@ __PACKAGE__->register_method({
 	die "$@" if ($@);
     }});
 
+__PACKAGE__->register_method({
+    name => 'get_disk_status',
+    path => '{id}/included_disks',
+    method => 'GET',
+    protected => 1,
+    description => "Shows included guests and the backup status of their disks.",
+    permissions => {
+	check => ['perm', '/', ['Sys.Audit']],
+    },
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    id => $vzdump_job_id_prop
+	},
+    },
+    returns => {
+	type => 'object',
+	properties => {
+	    children => {
+		type => 'array',
+		items => {
+		    type => 'object',
+		    properties => {
+			id => {
+			    type => 'string',
+			    description => 'ID of the guest.',
+			},
+			type => {
+			    type => 'string',
+			    description => 'Type of the guest, VM or CT.',
+			},
+			children => {
+			    type => 'array',
+			    description => 'The disks or mount points of the guest with the information if they will be included in backups.',
+			    items => {
+				type => 'object',
+				properties => {
+				    id => {
+					type => 'string',
+					description => 'Name of the disk or mount point.',
+				    },
+				    status => {
+					type => 'string',
+					description => 'The included status of the disk or mount point.',
+				    },
+				},
+			    },
+			},
+		    },
+		},
+	    },
+	},
+    },
+    code => sub {
+	my ($param) = @_;
+
+	my $vzconf = cfs_read_file('vzdump.cron');
+	my $jobs = $vzconf->{jobs} || [];
+	my $job;
+
+	foreach my $j (@$jobs) {
+	    $job = $j if $j->{id} eq $param->{id};
+	}
+	raise_param_exc({ id => "No such job '$param->{id}'" }) if !$job;
+
+	my @vmids;
+	my $vmlist = PVE::Cluster::get_vmlist();
+
+	# get VMIDs from pool or selected individual guests
+	if ($job->{pool}) {
+	    @vmids = @{PVE::API2Tools::get_resource_pool_guest_members($job->{pool})};
+	} elsif ($job->{vmid}) {
+	    @vmids = PVE::Tools::split_list(extract_param($job, 'vmid'));
+	}
+
+	# get excluded guests
+	my @exclude = PVE::Tools::split_list(extract_param($job, 'exclude'));
+	@exclude = @{PVE::VZDump::check_vmids(@exclude)};
+	@vmids = @{PVE::VZDump::check_vmids(@vmids)};
+
+	my $excludehash = { map { $_ => 1 } @exclude };
+
+	# no list of guests? (from pool or manually)
+	# get all except excluded
+	if (!@vmids) {
+	    if ($job->{all} && $job->{node}) {
+		foreach my $id (keys %{ $vmlist->{ids} }) {
+		    push @vmids, $id
+			if $vmlist->{ids}->{$id}->{node} eq $job->{node} && !$excludehash->{$id};
+		}
+	    } elsif ($job->{all}) {
+		foreach my $id (keys %{ $vmlist->{ids} }) {
+		    push @vmids, $id if !$excludehash->{$id};
+		}
+	    }
+	}
+
+	@vmids = sort {$a <=> $b} @vmids;
+
+	# prepare API response
+	my $result = {
+	    children => [],
+	    leaf => 0,
+	};
+
+	foreach (@vmids) {
+	    my $id = $_;
+	    my $type = $vmlist->{ids}->{$id}->{type};
+	    my $node = $vmlist->{ids}->{$id}->{node};
+
+	    my $guest = {
+		id => $id,
+		children => [],
+		leaf => 0,
+	    };
+
+	    if ($type eq 'qemu') {
+		my $conf = PVE::QemuConfig->load_config($id, $node);
+
+		$guest->{id} = $guest->{id} . " " . $conf->{name};
+		$guest->{type} = 'VM';
+
+		PVE::QemuServer::foreach_drive($conf, sub {
+			my ($key, $attr) = @_;
+
+			return if PVE::QemuServer::drive_is_cdrom($attr);
+
+			my $status = 'true';
+
+			$status = 'false' if (exists($attr->{backup}) && !$attr->{backup});
+
+			my $disk = {
+			    id => $key . ' - '. $attr->{file},
+			    status => $status,
+			    leaf => 1,
+			};
+			push @{$guest->{children}}, $disk;
+		});
+	    } elsif ($type eq 'lxc') {
+		my $conf = PVE::LXC::Config->load_config($id, $node);
+
+		$guest->{id} = $guest->{id} . " " . $conf->{hostname};
+		$guest->{type} = 'CT';
+
+		PVE::LXC::Config->foreach_mountpoint($conf, sub {
+			my ($key, $attr) = @_;
+
+			my $status = 'false';
+
+			$status = 'true' if ($key eq 'rootfs' || (exists($attr->{backup}) && $attr->{backup}));
+			$status = 'not possible' if ($attr->{type} ne 'volume');
+
+			my $disk = {
+			    id => $key . ' - '. $attr->{volume},
+			    status => $status,
+			    leaf => 1,
+			};
+			push @{$guest->{children}}, $disk;
+		});
+	    } else {
+		die "VMID $id is not Qemu nor LXC guest\n";
+	    }
+
+	    push @{$result->{children}}, $guest;
+	}
+
+	return $result;
+    }});
 1;
-- 
2.20.1





More information about the pve-devel mailing list