[pve-devel] r5066 - in qemu-server/pve2: . PVE PVE/VZDump

svn-commits at proxmox.com svn-commits at proxmox.com
Thu Aug 26 13:40:29 CEST 2010


Author: dietmar
Date: 2010-08-26 11:40:29 +0000 (Thu, 26 Aug 2010)
New Revision: 5066

Added:
   qemu-server/pve2/PVE/
   qemu-server/pve2/PVE/Makefile
   qemu-server/pve2/PVE/VZDump/
   qemu-server/pve2/PVE/VZDump/Makefile
   qemu-server/pve2/PVE/VZDump/QemuServer.pm
Removed:
   qemu-server/pve2/VZDump::QemuServer.pm
Modified:
   qemu-server/pve2/Makefile
Log:
* PVE/*: created directory hierachy for library compoments


Modified: qemu-server/pve2/Makefile
===================================================================
--- qemu-server/pve2/Makefile	2010-08-26 11:13:31 UTC (rev 5065)
+++ qemu-server/pve2/Makefile	2010-08-26 11:40:29 UTC (rev 5066)
@@ -14,7 +14,7 @@
 MANDIR=${PREFIX}/share/man
 DOCDIR=${PREFIX}/share/doc
 MAN1DIR=${MANDIR}/man1/
-PERLDIR=${PREFIX}/share/perl5
+export PERLDIR=${PREFIX}/share/perl5
 PERLINCDIR=${PERLDIR}/asm-x86_64
 
 ARCH:=$(shell dpkg-architecture -qDEB_BUILD_ARCH)
@@ -58,7 +58,7 @@
 	install -d ${DESTDIR}/var/run/qemu-server
 	install -d ${DESTDIR}/var/lock/qemu-server
 	install -D -m 0644 QemuServer.pm ${DESTDIR}${PERLDIR}/PVE/QemuServer.pm
-	install -D -m 0644 VZDump::QemuServer.pm ${DESTDIR}${PERLDIR}/PVE/VZDump/QemuServer.pm
+	make -C PVE install
 	install -m 0755 qm ${DESTDIR}${SBINDIR}
 	install -m 0755 qmigrate ${DESTDIR}${SBINDIR}
 	install -m 0755 qmrestore ${DESTDIR}${SBINDIR}
@@ -76,7 +76,7 @@
 deb ${DEB}: ${PKGSOURCES}
 	rm -rf debian
 	mkdir debian
-	make DESTDIR=debian install
+	make DESTDIR=${CURDIR}/debian install
 	install -d -m 0755 debian/DEBIAN
 	install -m 0644 control debian/DEBIAN
 	install -m 0755 postinst debian/DEBIAN
@@ -102,8 +102,10 @@
 
 .PHONY: clean
 clean: 	
-	rm -rf debian *~ *.deb control vzsyscalls.ph _h2ph_pre.ph ${PACKAGE}-*.tar.gz dist
+	rm -rf debian *.deb control vzsyscalls.ph _h2ph_pre.ph ${PACKAGE}-*.tar.gz dist
+	find . -name '*~' -exec rm {} ';'
 
+
 .PHONY: distclean
 distclean: clean
 

Added: qemu-server/pve2/PVE/Makefile
===================================================================
--- qemu-server/pve2/PVE/Makefile	                        (rev 0)
+++ qemu-server/pve2/PVE/Makefile	2010-08-26 11:40:29 UTC (rev 5066)
@@ -0,0 +1,4 @@
+
+.PHONY: install
+install:
+	make -C VZDump install
\ No newline at end of file

Added: qemu-server/pve2/PVE/VZDump/Makefile
===================================================================
--- qemu-server/pve2/PVE/VZDump/Makefile	                        (rev 0)
+++ qemu-server/pve2/PVE/VZDump/Makefile	2010-08-26 11:40:29 UTC (rev 5066)
@@ -0,0 +1,4 @@
+
+.PHONY: install
+install:
+	install -D -m 0644 QemuServer.pm ${DESTDIR}${PERLDIR}/PVE/VZDump/QemuServer.pm

Added: qemu-server/pve2/PVE/VZDump/QemuServer.pm
===================================================================
--- qemu-server/pve2/PVE/VZDump/QemuServer.pm	                        (rev 0)
+++ qemu-server/pve2/PVE/VZDump/QemuServer.pm	2010-08-26 11:40:29 UTC (rev 5066)
@@ -0,0 +1,454 @@
+package PVE::VZDump::QemuServer;
+
+#    Copyright (C) 2007-2009 Proxmox Server Solutions GmbH
+#
+#    Copyright: vzdump is under GNU GPL, the GNU General Public License.
+#
+#    This program is free software; you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation; version 2 dated June, 1991.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
+#
+#    You should have received a copy of the GNU General Public License
+#    along with this program; if not, write to the
+#    Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+#    MA 02110-1301, USA.
+#
+#    Author: Dietmar Maurer <dietmar at proxmox.com>
+
+use strict;
+use warnings;
+use File::Path;
+use File::Basename;
+use PVE::VZDump;
+use PVE::Storage;
+use PVE::QemuServer;
+use Sys::Hostname;
+use IO::File;
+
+use base qw (PVE::VZDump::Plugin);
+
+sub new {
+    my ($class, $vzdump) = @_;
+    
+    PVE::VZDump::check_bin ('qm');
+
+    my $self = bless { vzdump => $vzdump };
+
+    $self->{qm} = PVE::QemuServer->new();
+
+    $self->{vmlist} = PVE::QemuServer::vzlist();
+
+    return $self;
+};
+
+
+sub type {
+    return 'qemu';
+}
+
+sub vmlist {
+    my ($self) = @_;
+
+    return [ keys %{$self->{vmlist}} ];
+}
+
+sub prepare {
+    my ($self, $task, $vmid, $mode) = @_;
+
+    $task->{disks} = [];
+
+    my $conf = $self->{vmlist}->{$vmid} = $self->{qm}->load_config ($vmid);
+
+    $task->{hostname} = $conf->{name};
+
+    my $di = $conf->{diskinfo};
+
+    my $lvmmap = PVE::VZDump::get_lvm_mapping();
+
+    my $hostname = hostname(); 
+
+    my $ind = {};
+    my $mountinfo = {};
+    my $mountind = 0;
+
+    my $snapshot_count = 0;
+
+    foreach my $ds (sort keys %$di) {
+	next if PVE::QemuServer::drive_is_cdrom ($di->{$ds});
+
+	my $volid = $di->{$ds}->{file};
+
+	my $path;
+
+	my ($storeid, $volname) = PVE::Storage::parse_volume_id ($volid, 1);
+	if ($storeid) {
+	    $path = PVE::Storage::path ($self->{qm}->{storecfg}, $volid);
+	} else {
+	    $path = $volid;
+	}
+
+	$self->{qm}->activate_volumes ($conf);
+
+	die "no such volume '$volid'\n" if ! -e $path;
+
+	my $diskinfo = { path => $path , volid => $volid, storeid => $storeid, 
+			 snappath => $path, virtdev => $ds };
+
+	if (-b $path) {
+
+	    $diskinfo->{type} = 'block';
+
+	    $diskinfo->{filename} = "vm-disk-$ds.raw";
+
+	    if ($mode eq 'snapshot') {
+		my ($lvmvg, $lvmlv) = @{$lvmmap->{$path}} if defined ($lvmmap->{$path});
+		die ("mode failure - unable to detect lvm volume group\n") if !$lvmvg;
+
+		$ind->{$lvmvg} = 0 if !defined $ind->{$lvmvg};
+		$diskinfo->{snapname} = "vzsnap-$hostname-$ind->{$lvmvg}";
+		$diskinfo->{snapdev} = "/dev/$lvmvg/$diskinfo->{snapname}";
+		$diskinfo->{lvmvg} = $lvmvg;
+		$diskinfo->{lvmlv} = $lvmlv;
+		$diskinfo->{snappath} = $diskinfo->{snapdev};
+		$ind->{$lvmvg}++;
+
+		$snapshot_count++;
+	    }
+
+	} else {
+
+	    $diskinfo->{type} = 'file';
+
+	    my (undef, $dir, $ext) = fileparse ($path, qr/\.[^.]*/);
+
+	    $diskinfo->{filename} = "vm-disk-$ds$ext";
+
+	    if ($mode eq 'snapshot') {
+	    
+		my ($srcdev, $lvmpath, $lvmvg, $lvmlv, $fstype) =
+		    PVE::VZDump::get_lvm_device ($dir, $lvmmap);
+
+		my $targetdev = PVE::VZDump::get_lvm_device ($task->{dumpdir}, $lvmmap);
+
+		die ("mode failure - unable to detect lvm volume group\n") if !$lvmvg;
+		die ("mode failure - wrong lvm mount point '$lvmpath'\n") if $dir !~ m|/?$lvmpath/?|;
+		die ("mode failure - unable to dump into snapshot (use option --dumpdir)\n") 
+		    if $targetdev eq $srcdev;
+		
+		$ind->{$lvmvg} = 0 if !defined $ind->{$lvmvg};
+		    
+		my $info = $mountinfo->{$lvmpath};
+		if (!$info) {
+		    my $snapname = "vzsnap-$hostname-$ind->{$lvmvg}";
+		    my $snapdev = "/dev/$lvmvg/$snapname";
+		    $mountinfo->{$lvmpath} = $info = {
+			snapdev => $snapdev,
+			snapname => $snapname,
+			mountpoint => "/mnt/vzsnap$mountind",
+		    };
+		    $ind->{$lvmvg}++;
+		    $mountind++;
+
+		    $snapshot_count++;
+		} 
+
+		$diskinfo->{snapdev} = $info->{snapdev};
+		$diskinfo->{snapname} = $info->{snapname};
+		$diskinfo->{mountpoint} = $info->{mountpoint};
+		
+		$diskinfo->{lvmvg} = $lvmvg;
+		$diskinfo->{lvmlv} = $lvmlv;
+		
+		$diskinfo->{fstype}  = $fstype;
+		$diskinfo->{lvmpath} = $lvmpath;
+
+		$diskinfo->{snappath} = $path;
+		$diskinfo->{snappath} =~ s|/?$lvmpath/?|$diskinfo->{mountpoint}/|;
+	    }
+	}
+
+	push @{$task->{disks}}, $diskinfo;
+
+    }
+
+    $task->{snapshot_count} = $snapshot_count;
+}
+
+sub vm_status {
+    my ($self, $vmid) = @_;
+
+    my $status_text = $self->cmd ("qm status $vmid");
+    chomp $status_text;
+
+    my $running = $status_text =~ m/running/ ? 1 : 0;
+   
+    return wantarray ? ($running, $status_text) : $running; 
+}
+
+sub lock_vm {
+    my ($self, $vmid) = @_;
+
+    $self->cmd ("qm set $vmid --lock backup");
+}
+
+sub unlock_vm {
+    my ($self, $vmid) = @_;
+
+    $self->cmd ("qm --skiplock set $vmid --lock ''");
+}
+
+sub stop_vm {
+    my ($self, $task, $vmid) = @_;
+
+    my $opts = $self->{vzdump}->{opts};
+
+    my $wait = $opts->{stopwait} * 60;
+    # send shutdown and wait
+    $self->cmd ("qm --skiplock shutdown $vmid && qm wait $vmid $wait");
+}
+
+sub start_vm {
+    my ($self, $task, $vmid) = @_;
+
+    $self->cmd ("qm --skiplock start $vmid");
+}
+
+sub suspend_vm {
+    my ($self, $task, $vmid) = @_;
+
+    $self->cmd ("qm --skiplock suspend $vmid");
+}
+
+sub resume_vm {
+    my ($self, $task, $vmid) = @_;
+
+    $self->cmd ("qm --skiplock resume $vmid");
+}
+
+sub snapshot_alloc {
+    my ($self, $volid, $name, $size, $srcdev) = @_;
+
+    my $cmd = "lvcreate --size ${size}M --snapshot --name '$name' '$srcdev'";
+
+    my ($storeid, $volname) = PVE::Storage::parse_volume_id ($volid, 1);
+    if ($storeid) {
+
+	my $scfg = PVE::Storage::storage_config ($self->{qm}->{storecfg}, $storeid);
+
+	# lock shared storage
+	return PVE::Storage::cluster_lock_storage ($storeid, $scfg->{shared}, undef, sub {
+
+	    if ($scfg->{type} eq 'lvm') {
+		my $vg = $scfg->{vgname};
+
+		$self->cmd ($cmd);
+
+	    } else {
+		die "can't allocate snapshot on storage type '$scfg->{type}'\n";
+	    }
+	});
+    } else {
+	$self->cmd ($cmd);
+    }
+}
+
+sub snapshot_free {
+    my ($self, $volid, $name, $snapdev, $noerr) = @_;
+
+    my $cmd = "lvremove -f '$snapdev'";
+
+    eval {
+	my ($storeid, $volname) = PVE::Storage::parse_volume_id ($volid, 1);
+	if ($storeid) {
+
+	    my $scfg = PVE::Storage::storage_config ($self->{qm}->{storecfg}, $storeid);
+
+	    # lock shared storage
+	    return PVE::Storage::cluster_lock_storage ($storeid, $scfg->{shared}, undef, sub {
+
+		if ($scfg->{type} eq 'lvm') {
+		    my $vg = $scfg->{vgname};
+
+		    $self->cmd ($cmd);
+
+		} else {
+		    die "can't allocate snapshot on storage type '$scfg->{type}'\n";
+		}
+	    });
+	} else {
+	    $self->cmd ($cmd);
+	}
+    };
+    die $@ if !$noerr;
+    $self->logerr ($@) if $@;
+}
+
+sub snapshot {
+    my ($self, $task, $vmid) = @_;
+
+    my $opts = $self->{vzdump}->{opts};
+
+    my $mounts = {};
+
+    foreach my $di (@{$task->{disks}}) {
+	if ($di->{type} eq 'block') {
+
+	    if (-b $di->{snapdev}) {
+		$self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");
+		$self->snapshot_free ($di->{volid}, $di->{snapname}, $di->{snapdev}, 1); 
+	    }
+
+	    $di->{cleanup_lvm} = 1;
+	    $self->snapshot_alloc ($di->{volid}, $di->{snapname}, $opts->{size},
+				   "/dev/$di->{lvmvg}/$di->{lvmlv}"); 
+
+	} elsif ($di->{type} eq 'file') {
+
+	    next if defined ($mounts->{$di->{mountpoint}}); # already mounted
+
+	    # note: files are never on shared storage, so we use $di->{path} instead
+	    # of $di->{volid} (avoid PVE:Storage calls because path start with /)
+
+	    if (-b $di->{snapdev}) {
+		$self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");	    
+	    
+		$self->cmd_noerr ("umount $di->{mountpoint}");
+
+		$self->snapshot_free ($di->{path}, $di->{snapname}, $di->{snapdev}, 1); 
+	    }
+
+	    mkpath $di->{mountpoint}; # create mount point for lvm snapshot
+
+	    $di->{cleanup_lvm} = 1;
+
+	    $self->snapshot_alloc ($di->{path}, $di->{snapname}, $opts->{size},
+				   "/dev/$di->{lvmvg}/$di->{lvmlv}"); 
+	    
+	    my $mopts = $di->{fstype} eq 'xfs' ? "-o nouuid" : '';
+
+	    $di->{snapshot_mount} = 1;
+
+	    $self->cmd ("mount -t $di->{fstype} $mopts $di->{snapdev} $di->{mountpoint}");
+
+	    $mounts->{$di->{mountpoint}} = 1;
+
+	} else {
+	    die "implement me";
+	}
+    }
+}
+
+sub get_size {
+    my $path = shift;
+
+    if (-f $path) {
+	return -s $path;
+    } elsif (-b $path) {
+	my $fh = IO::File->new ($path, "r");
+	die "unable to open '$path' to detect device size\n" if !$fh;
+	my $size = sysseek $fh, 0, 2;
+	$fh->close();
+	die "unable to detect device size for '$path'\n" if !$size;
+	return $size;
+    }
+}
+
+sub assemble {
+    my ($self, $task, $vmid) = @_;
+
+    my $conffile = PVE::QemuServer::config_file ($vmid);
+
+    my $outfile = "$task->{tmpdir}/qemu-server.conf";
+
+    my $outfd;
+    my $conffd;
+
+    eval {
+
+	$outfd = IO::File->new (">$outfile") ||
+	    die "unable to open '$outfile'";
+	$conffd = IO::File->new ($conffile, 'r') ||
+	    die "unable open '$conffile'";
+
+	while (defined (my $line = <$conffd>)) {
+	    next if $line =~ m/^\#vzdump\#/; # just to be sure
+	    print $outfd $line;
+	}
+
+	foreach my $di (@{$task->{disks}}) {
+	    if ($di->{type} eq 'block' || $di->{type} eq 'file') {
+		my $size = get_size ($di->{snappath});
+		my $storeid = $di->{storeid} || '';
+		print $outfd "#vzdump#map:$di->{virtdev}:$di->{filename}:$size:$storeid:\n";
+	    } else {
+		die "internal error";
+	    }
+	}
+    };
+    my $err = $@;
+
+    close ($outfd) if $outfd;
+    close ($conffd) if $conffd;
+    
+    die $err if $err;
+}
+
+sub archive {
+    my ($self, $task, $vmid, $filename) = @_;
+
+    my $conffile = "$task->{tmpdir}/qemu-server.conf";
+
+    my $opts = $self->{vzdump}->{opts};
+
+    my $starttime = time ();
+
+    my $fh;
+
+    my $bwl = $opts->{bwlimit}*1024; # bandwidth limit for cstream
+
+    my @filea = ($conffile, 'qemu-server.conf'); # always first file in tar
+    foreach my $di (@{$task->{disks}}) {
+	if ($di->{type} eq 'block' || $di->{type} eq 'file') {
+	    push @filea, $di->{snappath}, $di->{filename};
+	} else {
+	    die "implement me";
+	}
+    }
+
+    my $out = ">$filename";
+    $out = "|cstream -t $bwl $out" if $opts->{bwlimit};
+    $out = "|gzip $out" if $opts->{compress};
+
+    my $files = join (' ', map { "'$_'" } @filea);
+    
+    $self->cmd("/usr/lib/qemu-server/vmtar $files $out");
+}
+
+sub cleanup {
+    my ($self, $task, $vmid) = @_;
+
+   foreach my $di (@{$task->{disks}}) {
+       
+       if ($di->{snapshot_mount}) {
+	   $self->cmd_noerr ("umount $di->{mountpoint}");
+       }
+
+       if ($di->{cleanup_lvm}) {
+	   if (-b $di->{snapdev}) {
+	       if ($di->{type} eq 'block') {
+		   $self->snapshot_free ($di->{volid}, $di->{snapname}, $di->{snapdev}, 1);
+	       } elsif ($di->{type} eq 'file') {
+		   $self->snapshot_free ($di->{path}, $di->{snapname}, $di->{snapdev}, 1);
+	       }
+	   }
+       }
+   }
+
+}
+
+1;

Deleted: qemu-server/pve2/VZDump::QemuServer.pm
===================================================================
--- qemu-server/pve2/VZDump::QemuServer.pm	2010-08-26 11:13:31 UTC (rev 5065)
+++ qemu-server/pve2/VZDump::QemuServer.pm	2010-08-26 11:40:29 UTC (rev 5066)
@@ -1,454 +0,0 @@
-package PVE::VZDump::QemuServer;
-
-#    Copyright (C) 2007-2009 Proxmox Server Solutions GmbH
-#
-#    Copyright: vzdump is under GNU GPL, the GNU General Public License.
-#
-#    This program is free software; you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation; version 2 dated June, 1991.
-#
-#    This program is distributed in the hope that it will be useful,
-#    but WITHOUT ANY WARRANTY; without even the implied warranty of
-#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#    GNU General Public License for more details.
-#
-#    You should have received a copy of the GNU General Public License
-#    along with this program; if not, write to the
-#    Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
-#    MA 02110-1301, USA.
-#
-#    Author: Dietmar Maurer <dietmar at proxmox.com>
-
-use strict;
-use warnings;
-use File::Path;
-use File::Basename;
-use PVE::VZDump;
-use PVE::Storage;
-use PVE::QemuServer;
-use Sys::Hostname;
-use IO::File;
-
-use base qw (PVE::VZDump::Plugin);
-
-sub new {
-    my ($class, $vzdump) = @_;
-    
-    PVE::VZDump::check_bin ('qm');
-
-    my $self = bless { vzdump => $vzdump };
-
-    $self->{qm} = PVE::QemuServer->new();
-
-    $self->{vmlist} = PVE::QemuServer::vzlist();
-
-    return $self;
-};
-
-
-sub type {
-    return 'qemu';
-}
-
-sub vmlist {
-    my ($self) = @_;
-
-    return [ keys %{$self->{vmlist}} ];
-}
-
-sub prepare {
-    my ($self, $task, $vmid, $mode) = @_;
-
-    $task->{disks} = [];
-
-    my $conf = $self->{vmlist}->{$vmid} = $self->{qm}->load_config ($vmid);
-
-    $task->{hostname} = $conf->{name};
-
-    my $di = $conf->{diskinfo};
-
-    my $lvmmap = PVE::VZDump::get_lvm_mapping();
-
-    my $hostname = hostname(); 
-
-    my $ind = {};
-    my $mountinfo = {};
-    my $mountind = 0;
-
-    my $snapshot_count = 0;
-
-    foreach my $ds (sort keys %$di) {
-	next if PVE::QemuServer::drive_is_cdrom ($di->{$ds});
-
-	my $volid = $di->{$ds}->{file};
-
-	my $path;
-
-	my ($storeid, $volname) = PVE::Storage::parse_volume_id ($volid, 1);
-	if ($storeid) {
-	    $path = PVE::Storage::path ($self->{qm}->{storecfg}, $volid);
-	} else {
-	    $path = $volid;
-	}
-
-	$self->{qm}->activate_volumes ($conf);
-
-	die "no such volume '$volid'\n" if ! -e $path;
-
-	my $diskinfo = { path => $path , volid => $volid, storeid => $storeid, 
-			 snappath => $path, virtdev => $ds };
-
-	if (-b $path) {
-
-	    $diskinfo->{type} = 'block';
-
-	    $diskinfo->{filename} = "vm-disk-$ds.raw";
-
-	    if ($mode eq 'snapshot') {
-		my ($lvmvg, $lvmlv) = @{$lvmmap->{$path}} if defined ($lvmmap->{$path});
-		die ("mode failure - unable to detect lvm volume group\n") if !$lvmvg;
-
-		$ind->{$lvmvg} = 0 if !defined $ind->{$lvmvg};
-		$diskinfo->{snapname} = "vzsnap-$hostname-$ind->{$lvmvg}";
-		$diskinfo->{snapdev} = "/dev/$lvmvg/$diskinfo->{snapname}";
-		$diskinfo->{lvmvg} = $lvmvg;
-		$diskinfo->{lvmlv} = $lvmlv;
-		$diskinfo->{snappath} = $diskinfo->{snapdev};
-		$ind->{$lvmvg}++;
-
-		$snapshot_count++;
-	    }
-
-	} else {
-
-	    $diskinfo->{type} = 'file';
-
-	    my (undef, $dir, $ext) = fileparse ($path, qr/\.[^.]*/);
-
-	    $diskinfo->{filename} = "vm-disk-$ds$ext";
-
-	    if ($mode eq 'snapshot') {
-	    
-		my ($srcdev, $lvmpath, $lvmvg, $lvmlv, $fstype) =
-		    PVE::VZDump::get_lvm_device ($dir, $lvmmap);
-
-		my $targetdev = PVE::VZDump::get_lvm_device ($task->{dumpdir}, $lvmmap);
-
-		die ("mode failure - unable to detect lvm volume group\n") if !$lvmvg;
-		die ("mode failure - wrong lvm mount point '$lvmpath'\n") if $dir !~ m|/?$lvmpath/?|;
-		die ("mode failure - unable to dump into snapshot (use option --dumpdir)\n") 
-		    if $targetdev eq $srcdev;
-		
-		$ind->{$lvmvg} = 0 if !defined $ind->{$lvmvg};
-		    
-		my $info = $mountinfo->{$lvmpath};
-		if (!$info) {
-		    my $snapname = "vzsnap-$hostname-$ind->{$lvmvg}";
-		    my $snapdev = "/dev/$lvmvg/$snapname";
-		    $mountinfo->{$lvmpath} = $info = {
-			snapdev => $snapdev,
-			snapname => $snapname,
-			mountpoint => "/mnt/vzsnap$mountind",
-		    };
-		    $ind->{$lvmvg}++;
-		    $mountind++;
-
-		    $snapshot_count++;
-		} 
-
-		$diskinfo->{snapdev} = $info->{snapdev};
-		$diskinfo->{snapname} = $info->{snapname};
-		$diskinfo->{mountpoint} = $info->{mountpoint};
-		
-		$diskinfo->{lvmvg} = $lvmvg;
-		$diskinfo->{lvmlv} = $lvmlv;
-		
-		$diskinfo->{fstype}  = $fstype;
-		$diskinfo->{lvmpath} = $lvmpath;
-
-		$diskinfo->{snappath} = $path;
-		$diskinfo->{snappath} =~ s|/?$lvmpath/?|$diskinfo->{mountpoint}/|;
-	    }
-	}
-
-	push @{$task->{disks}}, $diskinfo;
-
-    }
-
-    $task->{snapshot_count} = $snapshot_count;
-}
-
-sub vm_status {
-    my ($self, $vmid) = @_;
-
-    my $status_text = $self->cmd ("qm status $vmid");
-    chomp $status_text;
-
-    my $running = $status_text =~ m/running/ ? 1 : 0;
-   
-    return wantarray ? ($running, $status_text) : $running; 
-}
-
-sub lock_vm {
-    my ($self, $vmid) = @_;
-
-    $self->cmd ("qm set $vmid --lock backup");
-}
-
-sub unlock_vm {
-    my ($self, $vmid) = @_;
-
-    $self->cmd ("qm --skiplock set $vmid --lock ''");
-}
-
-sub stop_vm {
-    my ($self, $task, $vmid) = @_;
-
-    my $opts = $self->{vzdump}->{opts};
-
-    my $wait = $opts->{stopwait} * 60;
-    # send shutdown and wait
-    $self->cmd ("qm --skiplock shutdown $vmid && qm wait $vmid $wait");
-}
-
-sub start_vm {
-    my ($self, $task, $vmid) = @_;
-
-    $self->cmd ("qm --skiplock start $vmid");
-}
-
-sub suspend_vm {
-    my ($self, $task, $vmid) = @_;
-
-    $self->cmd ("qm --skiplock suspend $vmid");
-}
-
-sub resume_vm {
-    my ($self, $task, $vmid) = @_;
-
-    $self->cmd ("qm --skiplock resume $vmid");
-}
-
-sub snapshot_alloc {
-    my ($self, $volid, $name, $size, $srcdev) = @_;
-
-    my $cmd = "lvcreate --size ${size}M --snapshot --name '$name' '$srcdev'";
-
-    my ($storeid, $volname) = PVE::Storage::parse_volume_id ($volid, 1);
-    if ($storeid) {
-
-	my $scfg = PVE::Storage::storage_config ($self->{qm}->{storecfg}, $storeid);
-
-	# lock shared storage
-	return PVE::Storage::cluster_lock_storage ($storeid, $scfg->{shared}, undef, sub {
-
-	    if ($scfg->{type} eq 'lvm') {
-		my $vg = $scfg->{vgname};
-
-		$self->cmd ($cmd);
-
-	    } else {
-		die "can't allocate snapshot on storage type '$scfg->{type}'\n";
-	    }
-	});
-    } else {
-	$self->cmd ($cmd);
-    }
-}
-
-sub snapshot_free {
-    my ($self, $volid, $name, $snapdev, $noerr) = @_;
-
-    my $cmd = "lvremove -f '$snapdev'";
-
-    eval {
-	my ($storeid, $volname) = PVE::Storage::parse_volume_id ($volid, 1);
-	if ($storeid) {
-
-	    my $scfg = PVE::Storage::storage_config ($self->{qm}->{storecfg}, $storeid);
-
-	    # lock shared storage
-	    return PVE::Storage::cluster_lock_storage ($storeid, $scfg->{shared}, undef, sub {
-
-		if ($scfg->{type} eq 'lvm') {
-		    my $vg = $scfg->{vgname};
-
-		    $self->cmd ($cmd);
-
-		} else {
-		    die "can't allocate snapshot on storage type '$scfg->{type}'\n";
-		}
-	    });
-	} else {
-	    $self->cmd ($cmd);
-	}
-    };
-    die $@ if !$noerr;
-    $self->logerr ($@) if $@;
-}
-
-sub snapshot {
-    my ($self, $task, $vmid) = @_;
-
-    my $opts = $self->{vzdump}->{opts};
-
-    my $mounts = {};
-
-    foreach my $di (@{$task->{disks}}) {
-	if ($di->{type} eq 'block') {
-
-	    if (-b $di->{snapdev}) {
-		$self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");
-		$self->snapshot_free ($di->{volid}, $di->{snapname}, $di->{snapdev}, 1); 
-	    }
-
-	    $di->{cleanup_lvm} = 1;
-	    $self->snapshot_alloc ($di->{volid}, $di->{snapname}, $opts->{size},
-				   "/dev/$di->{lvmvg}/$di->{lvmlv}"); 
-
-	} elsif ($di->{type} eq 'file') {
-
-	    next if defined ($mounts->{$di->{mountpoint}}); # already mounted
-
-	    # note: files are never on shared storage, so we use $di->{path} instead
-	    # of $di->{volid} (avoid PVE:Storage calls because path start with /)
-
-	    if (-b $di->{snapdev}) {
-		$self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");	    
-	    
-		$self->cmd_noerr ("umount $di->{mountpoint}");
-
-		$self->snapshot_free ($di->{path}, $di->{snapname}, $di->{snapdev}, 1); 
-	    }
-
-	    mkpath $di->{mountpoint}; # create mount point for lvm snapshot
-
-	    $di->{cleanup_lvm} = 1;
-
-	    $self->snapshot_alloc ($di->{path}, $di->{snapname}, $opts->{size},
-				   "/dev/$di->{lvmvg}/$di->{lvmlv}"); 
-	    
-	    my $mopts = $di->{fstype} eq 'xfs' ? "-o nouuid" : '';
-
-	    $di->{snapshot_mount} = 1;
-
-	    $self->cmd ("mount -t $di->{fstype} $mopts $di->{snapdev} $di->{mountpoint}");
-
-	    $mounts->{$di->{mountpoint}} = 1;
-
-	} else {
-	    die "implement me";
-	}
-    }
-}
-
-sub get_size {
-    my $path = shift;
-
-    if (-f $path) {
-	return -s $path;
-    } elsif (-b $path) {
-	my $fh = IO::File->new ($path, "r");
-	die "unable to open '$path' to detect device size\n" if !$fh;
-	my $size = sysseek $fh, 0, 2;
-	$fh->close();
-	die "unable to detect device size for '$path'\n" if !$size;
-	return $size;
-    }
-}
-
-sub assemble {
-    my ($self, $task, $vmid) = @_;
-
-    my $conffile = PVE::QemuServer::config_file ($vmid);
-
-    my $outfile = "$task->{tmpdir}/qemu-server.conf";
-
-    my $outfd;
-    my $conffd;
-
-    eval {
-
-	$outfd = IO::File->new (">$outfile") ||
-	    die "unable to open '$outfile'";
-	$conffd = IO::File->new ($conffile, 'r') ||
-	    die "unable open '$conffile'";
-
-	while (defined (my $line = <$conffd>)) {
-	    next if $line =~ m/^\#vzdump\#/; # just to be sure
-	    print $outfd $line;
-	}
-
-	foreach my $di (@{$task->{disks}}) {
-	    if ($di->{type} eq 'block' || $di->{type} eq 'file') {
-		my $size = get_size ($di->{snappath});
-		my $storeid = $di->{storeid} || '';
-		print $outfd "#vzdump#map:$di->{virtdev}:$di->{filename}:$size:$storeid:\n";
-	    } else {
-		die "internal error";
-	    }
-	}
-    };
-    my $err = $@;
-
-    close ($outfd) if $outfd;
-    close ($conffd) if $conffd;
-    
-    die $err if $err;
-}
-
-sub archive {
-    my ($self, $task, $vmid, $filename) = @_;
-
-    my $conffile = "$task->{tmpdir}/qemu-server.conf";
-
-    my $opts = $self->{vzdump}->{opts};
-
-    my $starttime = time ();
-
-    my $fh;
-
-    my $bwl = $opts->{bwlimit}*1024; # bandwidth limit for cstream
-
-    my @filea = ($conffile, 'qemu-server.conf'); # always first file in tar
-    foreach my $di (@{$task->{disks}}) {
-	if ($di->{type} eq 'block' || $di->{type} eq 'file') {
-	    push @filea, $di->{snappath}, $di->{filename};
-	} else {
-	    die "implement me";
-	}
-    }
-
-    my $out = ">$filename";
-    $out = "|cstream -t $bwl $out" if $opts->{bwlimit};
-    $out = "|gzip $out" if $opts->{compress};
-
-    my $files = join (' ', map { "'$_'" } @filea);
-    
-    $self->cmd("/usr/lib/qemu-server/vmtar $files $out");
-}
-
-sub cleanup {
-    my ($self, $task, $vmid) = @_;
-
-   foreach my $di (@{$task->{disks}}) {
-       
-       if ($di->{snapshot_mount}) {
-	   $self->cmd_noerr ("umount $di->{mountpoint}");
-       }
-
-       if ($di->{cleanup_lvm}) {
-	   if (-b $di->{snapdev}) {
-	       if ($di->{type} eq 'block') {
-		   $self->snapshot_free ($di->{volid}, $di->{snapname}, $di->{snapdev}, 1);
-	       } elsif ($di->{type} eq 'file') {
-		   $self->snapshot_free ($di->{path}, $di->{snapname}, $di->{snapdev}, 1);
-	       }
-	   }
-       }
-   }
-
-}
-
-1;




More information about the pve-devel mailing list