]>
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 if (scalar(keys %{$conf->{snapshots
}})) {
50 die "VM contains snapshots - unable to backup\n";
53 $task->{hostname
} = $conf->{name
};
55 my $lvmmap = PVE
::VZDump
::get_lvm_mapping
();
57 my $hostname = PVE
::INotify
::nodename
();
63 my $snapshot_count = 0;
67 PVE
::QemuServer
::foreach_drive
($conf, sub {
68 my ($ds, $drive) = @_;
70 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
72 if (defined($drive->{backup
}) && $drive->{backup
} eq "no") {
73 $self->loginfo("exclude disk '$ds' (backup=no)");
77 my $volid = $drive->{file
};
79 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
80 push @$vollist, $volid if $storeid;
81 $drivehash->{$ds} = $drive;
84 PVE
::Storage
::activate_volumes
($self->{storecfg
}, $vollist);
86 while (my ($ds, $drive) = each %$drivehash) {
88 my $volid = $drive->{file
};
92 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, 1);
94 $path = PVE
::Storage
::path
($self->{storecfg
}, $volid);
101 die "no such volume '$volid'\n" if ! -e
$path;
103 my $diskinfo = { path
=> $path , volid
=> $volid, storeid
=> $storeid,
104 snappath
=> $path, virtdev
=> $ds };
108 $diskinfo->{type
} = 'block';
110 $diskinfo->{filename
} = "vm-disk-$ds.raw";
112 if ($mode eq 'snapshot') {
113 my ($lvmvg, $lvmlv) = @{$lvmmap->{$path}} if defined ($lvmmap->{$path});
114 die ("mode failure - unable to detect lvm volume group\n") if !$lvmvg;
116 $ind->{$lvmvg} = 0 if !defined $ind->{$lvmvg};
117 $diskinfo->{snapname
} = "vzsnap-$hostname-$ind->{$lvmvg}";
118 $diskinfo->{snapdev
} = "/dev/$lvmvg/$diskinfo->{snapname}";
119 $diskinfo->{lvmvg
} = $lvmvg;
120 $diskinfo->{lvmlv
} = $lvmlv;
121 $diskinfo->{snappath
} = $diskinfo->{snapdev
};
129 $diskinfo->{type
} = 'file';
131 my (undef, $dir, $ext) = fileparse
($path, qr/\.[^.]*/);
133 $diskinfo->{filename
} = "vm-disk-$ds$ext";
135 if ($mode eq 'snapshot') {
137 my ($srcdev, $lvmpath, $lvmvg, $lvmlv, $fstype) =
138 PVE
::VZDump
::get_lvm_device
($dir, $lvmmap);
140 my $targetdev = PVE
::VZDump
::get_lvm_device
($task->{dumpdir
}, $lvmmap);
142 die ("mode failure - unable to detect lvm volume group\n") if !$lvmvg;
143 die ("mode failure - wrong lvm mount point '$lvmpath'\n") if $dir !~ m
|/?$lvmpath/?
|;
144 die ("mode failure - unable to dump into snapshot (use option --dumpdir)\n")
145 if $targetdev eq $srcdev;
147 $ind->{$lvmvg} = 0 if !defined $ind->{$lvmvg};
149 my $info = $mountinfo->{$lvmpath};
151 my $snapname = "vzsnap-$hostname-$ind->{$lvmvg}";
152 my $snapdev = "/dev/$lvmvg/$snapname";
153 $mountinfo->{$lvmpath} = $info = {
155 snapname
=> $snapname,
156 mountpoint
=> "/mnt/vzsnap$mountind",
164 $diskinfo->{snapdev
} = $info->{snapdev
};
165 $diskinfo->{snapname
} = $info->{snapname
};
166 $diskinfo->{mountpoint
} = $info->{mountpoint
};
168 $diskinfo->{lvmvg
} = $lvmvg;
169 $diskinfo->{lvmlv
} = $lvmlv;
171 $diskinfo->{fstype
} = $fstype;
172 $diskinfo->{lvmpath
} = $lvmpath;
174 $diskinfo->{snappath
} = $path;
175 $diskinfo->{snappath
} =~ s
|/?$lvmpath/?
|$diskinfo->{mountpoint
}/|;
179 push @{$task->{disks
}}, $diskinfo;
182 $task->{snapshot_count
} = $snapshot_count;
186 my ($self, $vmid) = @_;
188 my $running = PVE
::QemuServer
::check_running
($vmid) ?
1 : 0;
190 return wantarray ?
($running, $running ?
'running' : 'stopped') : $running;
194 my ($self, $vmid) = @_;
196 $self->cmd ("qm set $vmid --lock backup");
200 my ($self, $vmid) = @_;
202 $self->cmd ("qm unlock $vmid");
206 my ($self, $task, $vmid) = @_;
208 my $opts = $self->{vzdump
}->{opts
};
210 my $wait = $opts->{stopwait
} * 60;
211 # send shutdown and wait
212 $self->cmd ("qm shutdown $vmid --skiplock --keepActive --timeout $wait");
216 my ($self, $task, $vmid) = @_;
218 $self->cmd ("qm start $vmid --skiplock");
222 my ($self, $task, $vmid) = @_;
224 $self->cmd ("qm suspend $vmid --skiplock");
228 my ($self, $task, $vmid) = @_;
230 $self->cmd ("qm resume $vmid --skiplock");
234 my ($self, $storeid, $name, $size, $srcdev) = @_;
236 my $cmd = "lvcreate --size ${size}M --snapshot --name '$name' '$srcdev'";
240 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
242 # lock shared storage
243 return PVE
::Storage
::Plugin-
>cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
252 my ($self, $storeid, $name, $snapdev, $noerr) = @_;
254 my $cmd = ['lvremove', '-f', $snapdev];
256 # loop, because we often get 'LV in use: not deactivating'
257 # we use run_command() because we do not want to log errors here
262 my $scfg = PVE
::Storage
::storage_config
($self->{storecfg
}, $storeid);
263 # lock shared storage
264 return PVE
::Storage
::Plugin-
>cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
265 PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> sub {});
268 PVE
::Tools
::run_command
($cmd, outfunc
=> sub {}, errfunc
=> sub {});
278 $self->loginfo("lvremove failed - trying again in $wait seconds") if $wait >= 8;
285 my ($self, $task, $vmid) = @_;
287 my $opts = $self->{vzdump
}->{opts
};
291 foreach my $di (@{$task->{disks
}}) {
292 if ($di->{type
} eq 'block') {
294 if (-b
$di->{snapdev
}) {
295 $self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");
296 $self->snapshot_free ($di->{storeid
}, $di->{snapname
}, $di->{snapdev
}, 1);
299 $di->{cleanup_lvm
} = 1;
300 $self->snapshot_alloc ($di->{storeid
}, $di->{snapname
}, $opts->{size
},
301 "/dev/$di->{lvmvg}/$di->{lvmlv}");
303 } elsif ($di->{type
} eq 'file') {
305 next if defined ($mounts->{$di->{mountpoint
}}); # already mounted
307 if (-b
$di->{snapdev
}) {
308 $self->loginfo ("trying to remove stale snapshot '$di->{snapdev}'");
310 $self->cmd_noerr ("umount $di->{mountpoint}");
311 $self->snapshot_free ($di->{storeid
}, $di->{snapname
}, $di->{snapdev
}, 1);
314 mkpath
$di->{mountpoint
}; # create mount point for lvm snapshot
316 $di->{cleanup_lvm
} = 1;
318 $self->snapshot_alloc ($di->{storeid
}, $di->{snapname
}, $opts->{size
},
319 "/dev/$di->{lvmvg}/$di->{lvmlv}");
321 my $mopts = $di->{fstype
} eq 'xfs' ?
"-o nouuid" : '';
323 $di->{snapshot_mount
} = 1;
325 $self->cmd ("mount -n -t $di->{fstype} $mopts $di->{snapdev} $di->{mountpoint}");
327 $mounts->{$di->{mountpoint
}} = 1;
341 my $fh = IO
::File-
>new ($path, "r");
342 die "unable to open '$path' to detect device size\n" if !$fh;
343 my $size = sysseek $fh, 0, 2;
345 die "unable to detect device size for '$path'\n" if !$size;
351 my ($self, $task, $vmid) = @_;
353 my $conffile = PVE
::QemuServer
::config_file
($vmid);
355 my $outfile = "$task->{tmpdir}/qemu-server.conf";
362 $outfd = IO
::File-
>new (">$outfile") ||
363 die "unable to open '$outfile'";
364 $conffd = IO
::File-
>new ($conffile, 'r') ||
365 die "unable open '$conffile'";
367 while (defined (my $line = <$conffd>)) {
368 next if $line =~ m/^\#vzdump\#/; # just to be sure
372 foreach my $di (@{$task->{disks
}}) {
373 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
374 my $size = get_size
($di->{snappath
});
375 my $storeid = $di->{storeid
} || '';
376 print $outfd "#vzdump#map:$di->{virtdev}:$di->{filename}:$size:$storeid:\n";
378 die "internal error";
384 close ($outfd) if $outfd;
385 close ($conffd) if $conffd;
391 my ($self, $task, $vmid, $filename, $comp) = @_;
393 my $conffile = "$task->{tmpdir}/qemu-server.conf";
395 my $opts = $self->{vzdump
}->{opts
};
397 my $starttime = time ();
401 my @filea = ($conffile, 'qemu-server.conf'); # always first file in tar
402 foreach my $di (@{$task->{disks
}}) {
403 if ($di->{type
} eq 'block' || $di->{type
} eq 'file') {
404 push @filea, $di->{snappath
}, $di->{filename
};
410 my $files = join (' ', map { "'$_'" } @filea);
412 # no sparse file scan when we use compression
413 my $sparse = $comp ?
'' : '-s';
415 my $cmd = "/usr/lib/qemu-server/vmtar $sparse $files";
416 my $bwl = $opts->{bwlimit
}*1024; # bandwidth limit for cstream
417 $cmd .= "|cstream -t $bwl" if $opts->{bwlimit
};
418 $cmd .= "|$comp" if $comp;
420 if ($opts->{stdout
}) {
421 $self->cmd ($cmd, output
=> ">&=" . fileno($opts->{stdout
}));
423 $self->cmd ("$cmd >$filename");
428 my ($self, $task, $vmid) = @_;
430 foreach my $di (@{$task->{disks
}}) {
432 if ($di->{snapshot_mount
}) {
433 $self->cmd_noerr ("umount $di->{mountpoint}");
436 if ($di->{cleanup_lvm
}) {
437 if (-b
$di->{snapdev
}) {
438 if ($di->{type
} eq 'block') {
439 $self->snapshot_free ($di->{storeid
}, $di->{snapname
}, $di->{snapdev
}, 1);
440 } elsif ($di->{type
} eq 'file') {
441 $self->snapshot_free ($di->{storeid
}, $di->{snapname
}, $di->{snapdev
}, 1);