]>
git.proxmox.com Git - qemu-server.git/blob - PVE/VZDump/QemuServer.pm
1 package PVE
::VZDump
::QemuServer
;
3 # Copyright (C) 2007-2009 Proxmox Server Solutions GmbH
5 # Copyright: vzdump is under GNU GPL, the GNU General Public License.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; version 2 dated June, 1991.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the
18 # Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 # Author: Dietmar Maurer <dietmar@proxmox.com>
34 use base qw
(PVE
::VZDump
::Plugin
);
37 my ($class, $vzdump) = @_;
39 PVE
::VZDump
::check_bin
('qm');
41 my $self = bless { vzdump
=> $vzdump };
43 $self->{vmlist
} = PVE
::QemuServer
::vzlist
();
44 $self->{storecfg
} = PVE
::Storage
::config
();
57 return [ keys %{$self->{vmlist
}} ];
61 my ($self, $task, $vmid, $mode) = @_;
65 my $conf = $self->{vmlist
}->{$vmid} = PVE
::QemuServer
::load_config
($vmid);
67 $task->{hostname
} = $conf->{name
};
69 my $lvmmap = PVE
::VZDump
::get_lvm_mapping
();
71 my $hostname = hostname
();
77 my $snapshot_count = 0;
79 PVE
::QemuServer
::foreach_drive
($conf, sub {
80 my ($ds, $drive) = @_;
82 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
84 if (defined($drive->{backup
}) && $drive->{backup
} eq "no") {
85 $self->loginfo("exclude disk '$ds' (backup=no)");
89 my $volid = $drive->{file
};
93 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
95 PVE
::Storage
::activate_storage
($self->{storecfg
}, $storeid);
96 $path = PVE
::Storage
::path
($self->{storecfg
}, $volid);
103 die "no such volume '$volid'\n" if ! -e
$path;
105 my $diskinfo = { path
=> $path , volid
=> $volid, storeid
=> $storeid,
106 snappath
=> $path, virtdev
=> $ds };
110 $diskinfo->{type
} = 'block';
112 $diskinfo->{filename
} = "vm-disk-$ds.raw";
114 if ($mode eq 'snapshot') {
115 my ($lvmvg, $lvmlv) = @{$lvmmap->{$path}} if defined ($lvmmap->{$path});
116 die ("mode failure - unable to detect lvm volume group\n") if !$lvmvg;
118 $ind->{$lvmvg} = 0 if !defined $ind->{$lvmvg};
119 $diskinfo->{snapname
} = "vzsnap-$hostname-$ind->{$lvmvg}";
120 $diskinfo->{snapdev
} = "/dev/$lvmvg/$diskinfo->{snapname}";
121 $diskinfo->{lvmvg
} = $lvmvg;
122 $diskinfo->{lvmlv
} = $lvmlv;
123 $diskinfo->{snappath
} = $diskinfo->{snapdev
};
131 $diskinfo->{type
} = 'file';
133 my (undef, $dir, $ext) = fileparse
($path, qr/\.[^.]*/);
135 $diskinfo->{filename
} = "vm-disk-$ds$ext";
137 if ($mode eq 'snapshot') {
139 my ($srcdev, $lvmpath, $lvmvg, $lvmlv, $fstype) =
140 PVE
::VZDump
::get_lvm_device
($dir, $lvmmap);
142 my $targetdev = PVE
::VZDump
::get_lvm_device
($task->{dumpdir
}, $lvmmap);
144 die ("mode failure - unable to detect lvm volume group\n") if !$lvmvg;
145 die ("mode failure - wrong lvm mount point '$lvmpath'\n") if $dir !~ m
|/?$lvmpath/?
|;
146 die ("mode failure - unable to dump into snapshot (use option --dumpdir)\n")
147 if $targetdev eq $srcdev;
149 $ind->{$lvmvg} = 0 if !defined $ind->{$lvmvg};
151 my $info = $mountinfo->{$lvmpath};
153 my $snapname = "vzsnap-$hostname-$ind->{$lvmvg}";
154 my $snapdev = "/dev/$lvmvg/$snapname";
155 $mountinfo->{$lvmpath} = $info = {
157 snapname
=> $snapname,
158 mountpoint
=> "/mnt/vzsnap$mountind",
166 $diskinfo->{snapdev
} = $info->{snapdev
};
167 $diskinfo->{snapname
} = $info->{snapname
};
168 $diskinfo->{mountpoint
} = $info->{mountpoint
};
170 $diskinfo->{lvmvg
} = $lvmvg;
171 $diskinfo->{lvmlv
} = $lvmlv;
173 $diskinfo->{fstype
} = $fstype;
174 $diskinfo->{lvmpath
} = $lvmpath;
176 $diskinfo->{snappath
} = $path;
177 $diskinfo->{snappath
} =~ s
|/?$lvmpath/?
|$diskinfo->{mountpoint
}/|;
181 push @{$task->{disks
}}, $diskinfo;
185 $task->{snapshot_count
} = $snapshot_count;
189 my ($self, $vmid) = @_;
191 my $status_text = $self->cmd ("qm status $vmid");
194 my $running = $status_text =~ m/running/ ?
1 : 0;
196 return wantarray ?
($running, $status_text) : $running;
200 my ($self, $vmid) = @_;
202 $self->cmd ("qm set $vmid --lock backup");
206 my ($self, $vmid) = @_;
208 $self->cmd ("qm --skiplock set $vmid --lock ''");
212 my ($self, $task, $vmid) = @_;
214 my $opts = $self->{vzdump
}->{opts
};
216 my $wait = $opts->{stopwait
} * 60;
217 # send shutdown and wait
218 $self->cmd ("qm --skiplock shutdown $vmid && qm wait $vmid $wait");
222 my ($self, $task, $vmid) = @_;
224 $self->cmd ("qm --skiplock start $vmid");
228 my ($self, $task, $vmid) = @_;
230 $self->cmd ("qm --skiplock suspend $vmid");
234 my ($self, $task, $vmid) = @_;
236 $self->cmd ("qm --skiplock resume $vmid");
240 my ($self, $volid, $name, $size, $srcdev) = @_;
242 my $cmd = "lvcreate --size ${size}M --snapshot --name '$name' '$srcdev'";
244 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
247 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
249 # lock shared storage
250 return PVE
::Storage
::cluster_lock_storage
($storeid, $scfg->{shared
}, undef, sub {
252 if ($scfg->{type
} eq 'lvm') {
253 my $vg = $scfg->{vgname
};
258 die "can't allocate snapshot on storage type '$scfg->{type}'\n";
267 my ($self, $volid, $name, $snapdev, $noerr) = @_;
269 my $cmd = "lvremove -f '$snapdev'";
272 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
275 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
277 # lock shared storage
278 return PVE
::Storage
::cluster_lock_storage
($storeid, $scfg->{shared
}, undef, sub {
280 if ($scfg->{type
} eq 'lvm') {
281 my $vg = $scfg->{vgname
};
286 die "can't allocate snapshot on storage type '$scfg->{type}'\n";
294 $self->logerr ($@) if $@;
298 my ($self, $task, $vmid) = @_;
300 my $opts = $self->{vzdump
}->{opts
};
304 foreach my $di (@{$task->{disks
}}) {
305 if ($di->{type
} eq 'block') {
307 if (-b
$di->{snapdev
}) {
308 $self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");
309 $self->snapshot_free ($di->{volid
}, $di->{snapname
}, $di->{snapdev
}, 1);
312 $di->{cleanup_lvm
} = 1;
313 $self->snapshot_alloc ($di->{volid
}, $di->{snapname
}, $opts->{size
},
314 "/dev/$di->{lvmvg}/$di->{lvmlv}");
316 } elsif ($di->{type
} eq 'file') {
318 next if defined ($mounts->{$di->{mountpoint
}}); # already mounted
320 # note: files are never on shared storage, so we use $di->{path} instead
321 # of $di->{volid} (avoid PVE:Storage calls because path start with /)
323 if (-b
$di->{snapdev
}) {
324 $self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");
326 $self->cmd_noerr ("umount $di->{mountpoint}");
328 $self->snapshot_free ($di->{path
}, $di->{snapname
}, $di->{snapdev
}, 1);
331 mkpath
$di->{mountpoint
}; # create mount point for lvm snapshot
333 $di->{cleanup_lvm
} = 1;
335 $self->snapshot_alloc ($di->{path
}, $di->{snapname
}, $opts->{size
},
336 "/dev/$di->{lvmvg}/$di->{lvmlv}");
338 my $mopts = $di->{fstype
} eq 'xfs' ?
"-o nouuid" : '';
340 $di->{snapshot_mount
} = 1;
342 $self->cmd ("mount -t $di->{fstype} $mopts $di->{snapdev} $di->{mountpoint}");
344 $mounts->{$di->{mountpoint
}} = 1;
358 my $fh = IO
::File-
>new ($path, "r");
359 die "unable to open '$path' to detect device size\n" if !$fh;
360 my $size = sysseek $fh, 0, 2;
362 die "unable to detect device size for '$path'\n" if !$size;
368 my ($self, $task, $vmid) = @_;
370 my $conffile = PVE
::QemuServer
::config_file
($vmid);
372 my $outfile = "$task->{tmpdir}/qemu-server.conf";
379 $outfd = IO
::File-
>new (">$outfile") ||
380 die "unable to open '$outfile'";
381 $conffd = IO
::File-
>new ($conffile, 'r') ||
382 die "unable open '$conffile'";
384 while (defined (my $line = <$conffd>)) {
385 next if $line =~ m/^\#vzdump\#/; # just to be sure
389 foreach my $di (@{$task->{disks
}}) {
390 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
391 my $size = get_size
($di->{snappath
});
392 my $storeid = $di->{storeid
} || '';
393 print $outfd "#vzdump#map:$di->{virtdev}:$di->{filename}:$size:$storeid:\n";
395 die "internal error";
401 close ($outfd) if $outfd;
402 close ($conffd) if $conffd;
408 my ($self, $task, $vmid, $filename) = @_;
410 my $conffile = "$task->{tmpdir}/qemu-server.conf";
412 my $opts = $self->{vzdump
}->{opts
};
414 my $starttime = time ();
418 my $bwl = $opts->{bwlimit
}*1024; # bandwidth limit for cstream
420 my @filea = ($conffile, 'qemu-server.conf'); # always first file in tar
421 foreach my $di (@{$task->{disks
}}) {
422 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
423 push @filea, $di->{snappath
}, $di->{filename
};
429 my $out = ">$filename";
430 $out = "|cstream -t $bwl $out" if $opts->{bwlimit
};
431 $out = "|gzip $out" if $opts->{compress
};
433 my $files = join (' ', map { "'$_'" } @filea);
435 $self->cmd("/usr/lib/qemu-server/vmtar $files $out");
439 my ($self, $task, $vmid) = @_;
441 foreach my $di (@{$task->{disks
}}) {
443 if ($di->{snapshot_mount
}) {
444 $self->cmd_noerr ("umount $di->{mountpoint}");
447 if ($di->{cleanup_lvm
}) {
448 if (-b
$di->{snapdev
}) {
449 if ($di->{type
} eq 'block') {
450 $self->snapshot_free ($di->{volid
}, $di->{snapname
}, $di->{snapdev
}, 1);
451 } elsif ($di->{type
} eq 'file') {
452 $self->snapshot_free ($di->{path
}, $di->{snapname
}, $di->{snapdev
}, 1);