]> git.proxmox.com Git - pve-container.git/blob - src/PVE/VZDump/LXC.pm
09c4d470a13ac443d72edffd01750e377f2d36d2
[pve-container.git] / src / PVE / VZDump / LXC.pm
1 package PVE::VZDump::LXC;
2
3 use strict;
4 use warnings;
5
6 use File::Basename;
7 use File::Path;
8
9 use PVE::Cluster qw(cfs_read_file);
10 use PVE::INotify;
11 use PVE::LXC::Config;
12 use PVE::LXC;
13 use PVE::Storage;
14 use PVE::Tools;
15 use PVE::VZDump;
16
17 use base qw (PVE::VZDump::Plugin);
18
19 my $default_mount_point = "/mnt/vzsnap0";
20
21 my $rsync_vm = sub {
22 my ($self, $task, $to, $text, $first) = @_;
23
24 my $disks = $task->{disks};
25 my $from = $disks->[0]->{dir} . '/';
26 $self->loginfo ("starting $text sync $from to $to");
27
28 my $opts = $self->{vzdump}->{opts};
29
30 my @xattr = $task->{no_xattrs} ? () : ('-X', '-A');
31
32 my $rsync = ['rsync', '--stats', @xattr, '--numeric-ids',
33 '-aH', '--delete', '--no-whole-file',
34 ($first ? '--sparse' : '--inplace'),
35 '--one-file-system', '--relative'];
36 push @$rsync, "--bwlimit=$opts->{bwlimit}" if $opts->{bwlimit};
37 push @$rsync, map { "--exclude=$_" } @{$self->{vzdump}->{findexcl}};
38 push @$rsync, map { "--exclude=$_" } @{$task->{exclude_dirs}};
39
40 my $starttime = time();
41 # See the rsync(1) manpage for --relative in conjunction with /./ in paths.
42 # This is the only way to have exclude-dirs work together with the
43 # --one-file-system option.
44 # This way we can pass multiple source paths and tell rsync which directory
45 # they're supposed to be relative to.
46 # Otherwise with eg. using multiple rsync commands means the --exclude
47 # directives need to be modified for every command as they are meant to be
48 # relative to the rootdir, while rsync treats them as relative to the
49 # source dir.
50 foreach my $disk (@$disks) {
51 push @$rsync, "$from/.$disk->{mp}";
52 }
53 $self->cmd([@$rsync, $to]);
54 my $delay = time () - $starttime;
55
56 $self->loginfo ("$text sync finished ($delay seconds)");
57 };
58
59 sub new {
60 my ($class, $vzdump) = @_;
61
62 PVE::VZDump::check_bin('lxc-stop');
63 PVE::VZDump::check_bin('lxc-start');
64 PVE::VZDump::check_bin('lxc-freeze');
65 PVE::VZDump::check_bin('lxc-unfreeze');
66
67 my $self = bless {};
68
69 $self->{vzdump} = $vzdump;
70 $self->{storecfg} = PVE::Storage::config();
71
72 $self->{vmlist} = PVE::LXC::config_list();
73
74 return $self;
75 }
76
77 sub type {
78 return 'lxc';
79 }
80
81 sub vm_status {
82 my ($self, $vmid) = @_;
83
84 my $running = PVE::LXC::check_running($vmid) ? 1 : 0;
85
86 return wantarray ? ($running, $running ? 'running' : 'stopped') : $running;
87 }
88
89 my $check_mountpoint_empty = sub {
90 my ($mountpoint) = @_;
91
92 die "mount point '$mountpoint' is not a directory\n" if ! -d $mountpoint;
93
94 PVE::Tools::dir_glob_foreach($mountpoint, qr/.*/, sub {
95 my $entry = shift;
96 return if $entry eq '.' || $entry eq '..';
97 die "mount point '$mountpoint' not empty\n";
98 });
99 };
100
101 sub prepare {
102 my ($self, $task, $vmid, $mode) = @_;
103
104 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::Config->load_config($vmid);
105 my $storage_cfg = $self->{storecfg};
106
107 $self->loginfo("CT Name: $conf->{hostname}")
108 if defined($conf->{hostname});
109
110 my $running = PVE::LXC::check_running($vmid);
111
112 my $disks = $task->{disks} = [];
113 my $exclude_dirs = $task->{exclude_dirs} = [];
114
115 $task->{hostname} = $conf->{'hostname'} || "CT$vmid";
116
117 my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
118 $task->{userns_cmd} = PVE::LXC::userns_command($id_map);
119 $task->{rootuid} = $rootuid;
120 $task->{rootgid} = $rootgid;
121
122 my $volids = $task->{volids} = [];
123 PVE::LXC::Config->foreach_mountpoint($conf, sub {
124 my ($name, $data) = @_;
125 my $volid = $data->{volume};
126 my $mount = $data->{mp};
127 my $type = $data->{type};
128
129 return if !$volid || !$mount;
130
131 if (!PVE::LXC::Config->mountpoint_backup_enabled($name, $data)) {
132 push @$exclude_dirs, $mount;
133 $self->loginfo("excluding $type mount point $name ('$mount') from backup");
134 return;
135 }
136
137 $data->{name} = $name;
138
139 # immutable raw base images need RO mount
140 if ($conf->{template} && !defined($data->{ro})) {
141 $data->{ro} = 1;
142 }
143 push @$disks, $data;
144 push @$volids, $volid
145 if $type eq 'volume';
146 });
147
148 if ($mode eq 'snapshot') {
149 if (!PVE::LXC::Config->has_feature('snapshot', $conf, $storage_cfg, undef, undef, 1)) {
150 die "mode failure - some volumes do not support snapshots\n";
151 }
152
153
154 if ($conf->{snapshots} && $conf->{snapshots}->{vzdump}) {
155 $self->loginfo("found old vzdump snapshot (force removal)");
156 PVE::LXC::Config->lock_config($vmid, sub {
157 $self->unlock_vm($vmid);
158 PVE::LXC::Config->snapshot_delete($vmid, 'vzdump', 1);
159 $self->lock_vm($vmid);
160 });
161 }
162
163 my $rootdir = $default_mount_point;
164 mkpath $rootdir;
165 &$check_mountpoint_empty($rootdir);
166
167 # set snapshot_count (freezes CT if snapshot_count > 1)
168 $task->{snapshot_count} = scalar(@$volids);
169 } elsif ($mode eq 'stop') {
170 my $rootdir = $default_mount_point;
171 mkpath $rootdir;
172 &$check_mountpoint_empty($rootdir);
173 } elsif ($mode eq 'suspend') {
174 my $pid = PVE::LXC::find_lxc_pid($vmid);
175 foreach my $disk (@$disks) {
176 $disk->{dir} = "/proc/$pid/root$disk->{mp}";
177 }
178 $task->{snapdir} = $task->{tmpdir};
179 } else {
180 unlock_vm($self, $vmid);
181 die "unknown mode '$mode'\n"; # should not happen
182 }
183
184 if ($mode ne 'suspend') {
185 # If we perform mount operations, let's unshare the mount namespace
186 # to not influence the running host.
187 PVE::Tools::unshare(PVE::Tools::CLONE_NEWNS);
188 PVE::Tools::run_command(['mount', '--make-rslave', '/']);
189 }
190 }
191
192 sub lock_vm {
193 my ($self, $vmid) = @_;
194
195 PVE::LXC::Config->set_lock($vmid, 'backup');
196 }
197
198 sub unlock_vm {
199 my ($self, $vmid) = @_;
200
201 PVE::LXC::Config->remove_lock($vmid, 'backup')
202 }
203
204 sub snapshot {
205 my ($self, $task, $vmid) = @_;
206
207 $self->loginfo("create storage snapshot 'vzdump'");
208
209 # todo: freeze/unfreeze if we have more than one volid
210 PVE::LXC::Config->lock_config($vmid, sub {
211 $self->unlock_vm($vmid);
212 PVE::LXC::Config->snapshot_create($vmid, 'vzdump', 0, "vzdump backup snapshot");
213 $self->lock_vm($vmid);
214 });
215 $task->{cleanup}->{remove_snapshot} = 1;
216
217 # reload config
218 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::Config->load_config($vmid);
219 die "unable to read vzdump snapshot config - internal error"
220 if !($conf->{snapshots} && $conf->{snapshots}->{vzdump});
221
222 my $disks = $task->{disks};
223 my $volids = $task->{volids};
224
225 my $rootdir = $default_mount_point;
226 my $storage_cfg = $self->{storecfg};
227
228 PVE::Storage::activate_volumes($storage_cfg, $volids, 'vzdump');
229 foreach my $disk (@$disks) {
230 $disk->{dir} = "${rootdir}$disk->{mp}";
231 PVE::LXC::mountpoint_mount($disk, $rootdir, $storage_cfg, 'vzdump', $task->{rootuid}, $task->{rootgid});
232 }
233
234 $task->{snapdir} = $rootdir;
235 }
236
237 sub copy_data_phase1 {
238 my ($self, $task) = @_;
239
240 if (my $mntinfo = PVE::VZDump::get_mount_info($task->{snapdir})) {
241 if ($mntinfo->{fstype} =~ /^nfs4?/) {
242 $self->loginfo(
243 "temporary directory is on NFS, disabling xattr and acl"
244 ." support, consider configuring a local tmpdir via"
245 ." /etc/vzdump.conf\n");
246 $task->{no_xattrs} = 1;
247 }
248 }
249
250 $self->$rsync_vm($task, $task->{snapdir}, "first", 1);
251 }
252
253 sub copy_data_phase2 {
254 my ($self, $task) = @_;
255
256 $self->$rsync_vm($task, $task->{snapdir}, "final", 0);
257 }
258
259 sub stop_vm {
260 my ($self, $task, $vmid) = @_;
261
262 my $opts = $self->{vzdump}->{opts};
263 my $timeout = $opts->{stopwait} * 60;
264
265 PVE::LXC::vm_stop($vmid, 0, $timeout);
266 }
267
268 sub start_vm {
269 my ($self, $task, $vmid) = @_;
270
271 $self->cmd(['systemctl', 'start', "pve-container\@$vmid"]);
272 }
273
274 sub suspend_vm {
275 my ($self, $task, $vmid) = @_;
276
277 $self->cmd ("lxc-freeze -n $vmid");
278 }
279
280 sub resume_vm {
281 my ($self, $task, $vmid) = @_;
282
283 $self->cmd ("lxc-unfreeze -n $vmid");
284 }
285
286 sub assemble {
287 my ($self, $task, $vmid) = @_;
288
289 my $opts = $self->{vzdump}->{opts};
290
291 my $conf = PVE::LXC::Config->load_config($vmid);
292 delete $conf->{lock};
293 delete $conf->{snapshots};
294 delete $conf->{parent};
295 delete $conf->{pending};
296
297 my $tmpdir = $task->{tmpdir};
298
299 mkpath "$tmpdir/etc/vzdump/";
300
301 PVE::Tools::file_set_contents("$tmpdir/etc/vzdump/pct.conf", PVE::LXC::Config::write_pct_config("/lxc/$vmid.conf", $conf));
302
303 my $firewall ="/etc/pve/firewall/$vmid.fw";
304 my $fwconftmp = "$tmpdir/etc/vzdump/pct.fw";
305
306 if ($opts->{scfg}->{type} eq 'pbs') {
307 # fixme: do not store pct.conf and fw.conf into $tmpdir
308 if (-e $firewall) {
309 PVE::Tools::file_copy($firewall, $fwconftmp);
310 }
311 } else {
312 if (-e $firewall) {
313 PVE::Tools::file_copy($firewall, $fwconftmp);
314 } else {
315 PVE::Tools::file_set_contents($fwconftmp, '');
316 }
317 $task->{fw} = 1;
318 }
319 }
320
321 sub archive {
322 my ($self, $task, $vmid, $filename, $comp) = @_;
323
324 my $disks = $task->{disks};
325 my @sources;
326
327 if ($task->{mode} eq 'stop') {
328 my $storage_cfg = $self->{storecfg};
329
330 PVE::Storage::activate_volumes($storage_cfg, $task->{volids});
331
332 my $rootdir = $default_mount_point;
333 foreach my $disk (@$disks) {
334 $disk->{dir} = "${rootdir}$disk->{mp}";
335 PVE::LXC::mountpoint_mount($disk, $rootdir, $storage_cfg, undef, $task->{rootuid}, $task->{rootgid});
336 # add every enabled mountpoint (since we use --one-file-system)
337 # mp already starts with a / so we only need to add the dot
338 push @sources, ".$disk->{mp}";
339 }
340 $task->{snapdir} = $rootdir;
341 } elsif ($task->{mode} eq 'snapshot') {
342 # mounting the vzdump snapshots and setting $snapdir is already done,
343 # but we need to include all mountpoints here!
344 foreach my $disk (@$disks) {
345 push @sources, ".$disk->{mp}";
346 }
347 } else {
348 # the data was rsynced to a temporary location, only use '.' to avoid
349 # having mountpoints duplicated
350 push @sources, '.';
351 }
352
353 my $opts = $self->{vzdump}->{opts};
354 my $snapdir = $task->{snapdir};
355 my $tmpdir = $task->{tmpdir};
356
357 my $userns_cmd = $task->{userns_cmd};
358
359 if ($opts->{scfg}->{type} eq 'pbs') {
360
361 my $rootdir = $default_mount_point;
362 my $param = [];
363
364 push @$param, "pct.conf:$tmpdir/etc/vzdump/pct.conf";
365
366 my $fw_conf = "$tmpdir/etc/vzdump/pct.fw";
367 if (-f $fw_conf) {
368 push @$param, "fw.conf:$fw_conf";
369 }
370
371 push @$param, "root.pxar:$rootdir";
372
373 foreach my $disk (@$disks) {
374 push @$param, '--include-dev', $disk->{dir};
375 }
376
377 push @$param, '--skip-lost-and-found' if $userns_cmd;
378
379 push @$param, '--backup-type', 'ct';
380 push @$param, '--backup-id', $vmid;
381 push @$param, '--backup-time', $task->{backup_time};
382
383 my $logfunc = sub { my $line = shift; $self->loginfo($line); };
384 PVE::Storage::PBSPlugin::run_raw_client_cmd(
385 $opts->{scfg}, $opts->{storage}, 'backup', $param,
386 logfunc => $logfunc, userns_cmd => $userns_cmd);
387
388 } else {
389
390 my $tar = [@$userns_cmd, 'tar', 'cpf', '-', '--totals',
391 @PVE::Storage::Plugin::COMMON_TAR_FLAGS,
392 '--one-file-system', '--warning=no-file-ignored'];
393
394 # note: --remove-files does not work because we do not
395 # backup all files (filters). tar complains:
396 # Cannot rmdir: Directory not empty
397 # we disable this optimization for now
398 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
399 # push @$tar, "--remove-files"; # try to save space
400 #}
401
402 # The directory parameter can give an alternative directory as source.
403 # the second parameter gives the structure in the tar.
404 push @$tar, "--directory=$tmpdir", './etc/vzdump/pct.conf';
405 push @$tar, "./etc/vzdump/pct.fw" if $task->{fw};
406 push @$tar, "--directory=$snapdir";
407 push @$tar, '--no-anchored', '--exclude=lost+found' if $userns_cmd;
408 push @$tar, '--anchored';
409 push @$tar, map { "--exclude=.$_" } @{$self->{vzdump}->{findexcl}};
410
411 push @$tar, @sources;
412
413 my $cmd = [ $tar ];
414
415 my $bwl = $opts->{bwlimit}*1024; # bandwidth limit for cstream
416 push @$cmd, [ 'cstream', '-t', $bwl ] if $opts->{bwlimit};
417 push @$cmd, [ split(/\s+/, $comp) ] if $comp;
418
419 if ($opts->{stdout}) {
420 $self->cmd($cmd, output => ">&" . fileno($opts->{stdout}));
421 } else {
422 push @{$cmd->[-1]}, \(">" . PVE::Tools::shellquote($filename));
423 $self->cmd($cmd);
424 }
425 }
426 }
427
428 sub cleanup {
429 my ($self, $task, $vmid) = @_;
430
431 my $conf = PVE::LXC::Config->load_config($vmid);
432
433 if ($task->{mode} ne 'suspend') {
434 my $rootdir = $default_mount_point;
435 my $disks = $task->{disks};
436 foreach my $disk (reverse @$disks) {
437 PVE::Tools::run_command(['umount', '-l', '-d', $disk->{dir}]) if $disk->{dir};
438 }
439 }
440
441 if ($task->{cleanup}->{remove_snapshot}) {
442 $self->loginfo("remove vzdump snapshot");
443 PVE::LXC::Config->snapshot_delete($vmid, 'vzdump', 0);
444 }
445 }
446
447 1;