]>
git.proxmox.com Git - qemu-server.git/blob - PVE/VZDump/QemuServer.pm
1 package PVE
::VZDump
::QemuServer
;
9 use PVE
::Cluster
qw(cfs_read_file);
11 use PVE
::Storage
::Plugin
;
16 use base qw
(PVE
::VZDump
::Plugin
);
19 my ($class, $vzdump) = @_;
21 PVE
::VZDump
::check_bin
('qm');
23 my $self = bless { vzdump
=> $vzdump };
25 $self->{vmlist
} = PVE
::QemuServer
::vzlist
();
26 $self->{storecfg
} = PVE
::Storage
::config
();
39 return [ keys %{$self->{vmlist
}} ];
43 my ($self, $task, $vmid, $mode) = @_;
47 my $conf = $self->{vmlist
}->{$vmid} = PVE
::QemuServer
::load_config
($vmid);
49 $task->{hostname
} = $conf->{name
};
51 my $lvmmap = PVE
::VZDump
::get_lvm_mapping
();
53 my $hostname = PVE
::INotify
::nodename
();
59 my $snapshot_count = 0;
63 PVE
::QemuServer
::foreach_drive
($conf, sub {
64 my ($ds, $drive) = @_;
66 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
68 if (defined($drive->{backup
}) && $drive->{backup
} eq "no") {
69 $self->loginfo("exclude disk '$ds' (backup=no)");
73 my $volid = $drive->{file
};
75 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
76 push @$vollist, $volid if $storeid;
77 $drivehash->{$ds} = $drive;
80 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
82 while (my ($ds, $drive) = each %$drivehash) {
84 my $volid = $drive->{file
};
88 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
90 $path = PVE
::Storage
::path
($self->{storecfg
}, $volid);
97 die "no such volume '$volid'\n" if ! -e
$path;
99 my $diskinfo = { path
=> $path , volid
=> $volid, storeid
=> $storeid,
100 snappath
=> $path, virtdev
=> $ds };
104 $diskinfo->{type
} = 'block';
106 $diskinfo->{filename
} = "vm-disk-$ds.raw";
108 if ($mode eq 'snapshot') {
109 my ($lvmvg, $lvmlv) = @{$lvmmap->{$path}} if defined ($lvmmap->{$path});
110 die ("mode failure - unable to detect lvm volume group\n") if !$lvmvg;
112 $ind->{$lvmvg} = 0 if !defined $ind->{$lvmvg};
113 $diskinfo->{snapname
} = "vzsnap-$hostname-$ind->{$lvmvg}";
114 $diskinfo->{snapdev
} = "/dev/$lvmvg/$diskinfo->{snapname}";
115 $diskinfo->{lvmvg
} = $lvmvg;
116 $diskinfo->{lvmlv
} = $lvmlv;
117 $diskinfo->{snappath
} = $diskinfo->{snapdev
};
125 $diskinfo->{type
} = 'file';
127 my (undef, $dir, $ext) = fileparse
($path, qr/\.[^.]*/);
129 $diskinfo->{filename
} = "vm-disk-$ds$ext";
131 if ($mode eq 'snapshot') {
133 my ($srcdev, $lvmpath, $lvmvg, $lvmlv, $fstype) =
134 PVE
::VZDump
::get_lvm_device
($dir, $lvmmap);
136 my $targetdev = PVE
::VZDump
::get_lvm_device
($task->{dumpdir
}, $lvmmap);
138 die ("mode failure - unable to detect lvm volume group\n") if !$lvmvg;
139 die ("mode failure - wrong lvm mount point '$lvmpath'\n") if $dir !~ m
|/?$lvmpath/?
|;
140 die ("mode failure - unable to dump into snapshot (use option --dumpdir)\n")
141 if $targetdev eq $srcdev;
143 $ind->{$lvmvg} = 0 if !defined $ind->{$lvmvg};
145 my $info = $mountinfo->{$lvmpath};
147 my $snapname = "vzsnap-$hostname-$ind->{$lvmvg}";
148 my $snapdev = "/dev/$lvmvg/$snapname";
149 $mountinfo->{$lvmpath} = $info = {
151 snapname
=> $snapname,
152 mountpoint
=> "/mnt/vzsnap$mountind",
160 $diskinfo->{snapdev
} = $info->{snapdev
};
161 $diskinfo->{snapname
} = $info->{snapname
};
162 $diskinfo->{mountpoint
} = $info->{mountpoint
};
164 $diskinfo->{lvmvg
} = $lvmvg;
165 $diskinfo->{lvmlv
} = $lvmlv;
167 $diskinfo->{fstype
} = $fstype;
168 $diskinfo->{lvmpath
} = $lvmpath;
170 $diskinfo->{snappath
} = $path;
171 $diskinfo->{snappath
} =~ s
|/?$lvmpath/?
|$diskinfo->{mountpoint
}/|;
175 push @{$task->{disks
}}, $diskinfo;
178 $task->{snapshot_count
} = $snapshot_count;
182 my ($self, $vmid) = @_;
184 my $running = PVE
::QemuServer
::check_running
($vmid) ?
1 : 0;
186 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
190 my ($self, $vmid) = @_;
192 $self->cmd ("qm set $vmid --lock backup");
196 my ($self, $vmid) = @_;
198 $self->cmd ("qm unlock $vmid");
202 my ($self, $task, $vmid) = @_;
204 my $opts = $self->{vzdump
}->{opts
};
206 my $wait = $opts->{stopwait
} * 60;
207 # send shutdown and wait
208 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
212 my ($self, $task, $vmid) = @_;
214 $self->cmd ("qm start $vmid --skiplock");
218 my ($self, $task, $vmid) = @_;
220 $self->cmd ("qm suspend $vmid --skiplock");
224 my ($self, $task, $vmid) = @_;
226 $self->cmd ("qm resume $vmid --skiplock");
230 my ($self, $storeid, $name, $size, $srcdev) = @_;
232 my $cmd = "lvcreate --size ${size}M --snapshot --name '$name' '$srcdev'";
236 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
238 # lock shared storage
239 return PVE
::Storage
::Plugin-
>cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
248 my ($self, $storeid, $name, $snapdev, $noerr) = @_;
250 my $cmd = ['lvremove', '-f', $snapdev];
252 # loop, because we often get 'LV in use: not deactivating'
253 # we use run_command() because we do not want to log errors here
258 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
259 # lock shared storage
260 return PVE
::Storage
::Plugin-
>cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
261 PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> sub {});
264 PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> sub {});
274 $self->loginfo("lvremove failed - trying again in $wait seconds") if $wait >= 8;
281 my ($self, $task, $vmid) = @_;
283 my $opts = $self->{vzdump
}->{opts
};
287 foreach my $di (@{$task->{disks
}}) {
288 if ($di->{type
} eq 'block') {
290 if (-b
$di->{snapdev
}) {
291 $self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");
292 $self->snapshot_free ($di->{storeid
}, $di->{snapname
}, $di->{snapdev
}, 1);
295 $di->{cleanup_lvm
} = 1;
296 $self->snapshot_alloc ($di->{storeid
}, $di->{snapname
}, $opts->{size
},
297 "/dev/$di->{lvmvg}/$di->{lvmlv}");
299 } elsif ($di->{type
} eq 'file') {
301 next if defined ($mounts->{$di->{mountpoint
}}); # already mounted
303 if (-b
$di->{snapdev
}) {
304 $self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");
306 $self->cmd_noerr ("umount $di->{mountpoint}");
307 $self->snapshot_free ($di->{storeid
}, $di->{snapname
}, $di->{snapdev
}, 1);
310 mkpath
$di->{mountpoint
}; # create mount point for lvm snapshot
312 $di->{cleanup_lvm
} = 1;
314 $self->snapshot_alloc ($di->{storeid
}, $di->{snapname
}, $opts->{size
},
315 "/dev/$di->{lvmvg}/$di->{lvmlv}");
317 my $mopts = $di->{fstype
} eq 'xfs' ?
"-o nouuid" : '';
319 $di->{snapshot_mount
} = 1;
321 $self->cmd ("mount -n -t $di->{fstype} $mopts $di->{snapdev} $di->{mountpoint}");
323 $mounts->{$di->{mountpoint
}} = 1;
337 my $fh = IO
::File-
>new ($path, "r");
338 die "unable to open '$path' to detect device size\n" if !$fh;
339 my $size = sysseek $fh, 0, 2;
341 die "unable to detect device size for '$path'\n" if !$size;
347 my ($self, $task, $vmid) = @_;
349 my $conffile = PVE
::QemuServer
::config_file
($vmid);
351 my $outfile = "$task->{tmpdir}/qemu-server.conf";
358 $outfd = IO
::File-
>new (">$outfile") ||
359 die "unable to open '$outfile'";
360 $conffd = IO
::File-
>new ($conffile, 'r') ||
361 die "unable open '$conffile'";
363 while (defined (my $line = <$conffd>)) {
364 next if $line =~ m/^\#vzdump\#/; # just to be sure
368 foreach my $di (@{$task->{disks
}}) {
369 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
370 my $size = get_size
($di->{snappath
});
371 my $storeid = $di->{storeid
} || '';
372 print $outfd "#vzdump#map:$di->{virtdev}:$di->{filename}:$size:$storeid:\n";
374 die "internal error";
380 close ($outfd) if $outfd;
381 close ($conffd) if $conffd;
387 my ($self, $task, $vmid, $filename, $comp) = @_;
389 my $conffile = "$task->{tmpdir}/qemu-server.conf";
391 my $opts = $self->{vzdump
}->{opts
};
393 my $starttime = time ();
397 my @filea = ($conffile, 'qemu-server.conf'); # always first file in tar
398 foreach my $di (@{$task->{disks
}}) {
399 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
400 push @filea, $di->{snappath
}, $di->{filename
};
406 my $files = join (' ', map { "'$_'" } @filea);
408 # no sparse file scan when we use compression
409 my $sparse = $comp ?
'' : '-s';
411 my $cmd = "/usr/lib/qemu-server/vmtar $sparse $files";
412 my $bwl = $opts->{bwlimit
}*1024; # bandwidth limit for cstream
413 $cmd .= "|cstream -t $bwl" if $opts->{bwlimit
};
414 $cmd .= "|$comp" if $comp;
416 if ($opts->{stdout
}) {
417 $self->cmd ($cmd, output
=> ">&=" . fileno($opts->{stdout
}));
419 $self->cmd ("$cmd >$filename");
424 my ($self, $task, $vmid) = @_;
426 foreach my $di (@{$task->{disks
}}) {
428 if ($di->{snapshot_mount
}) {
429 $self->cmd_noerr ("umount $di->{mountpoint}");
432 if ($di->{cleanup_lvm
}) {
433 if (-b
$di->{snapdev
}) {
434 if ($di->{type
} eq 'block') {
435 $self->snapshot_free ($di->{storeid
}, $di->{snapname
}, $di->{snapdev
}, 1);
436 } elsif ($di->{type
} eq 'file') {
437 $self->snapshot_free ($di->{storeid
}, $di->{snapname
}, $di->{snapdev
}, 1);