]> git.proxmox.com Git - pve-container.git/blame - src/PVE/VZDump/LXC.pm
Use foreach_volume instead of foreach_mountpoint-variants
[pve-container.git] / src / PVE / VZDump / LXC.pm
CommitLineData
d14a9a1b
DM
1package PVE::VZDump::LXC;
2
3use strict;
4use warnings;
8ebb49ee 5
d14a9a1b 6use File::Basename;
8ebb49ee
TL
7use File::Path;
8
d14a9a1b 9use PVE::Cluster qw(cfs_read_file);
8ebb49ee 10use PVE::INotify;
86ee2a21 11use PVE::LXC::Config;
8ebb49ee
TL
12use PVE::LXC;
13use PVE::Storage;
514b5f82 14use PVE::Tools;
8ebb49ee 15use PVE::VZDump;
d14a9a1b
DM
16
17use base qw (PVE::VZDump::Plugin);
18
4ca61ce8
DM
19my $default_mount_point = "/mnt/vzsnap0";
20
d14a9a1b 21my $rsync_vm = sub {
50ee6ffe 22 my ($self, $task, $to, $text, $first) = @_;
d14a9a1b 23
59a999af
WB
24 my $disks = $task->{disks};
25 my $from = $disks->[0]->{dir} . '/';
d14a9a1b
DM
26 $self->loginfo ("starting $text sync $from to $to");
27
d14a9a1b
DM
28 my $opts = $self->{vzdump}->{opts};
29
9c9a3b8c
WB
30 my @xattr = $task->{no_xattrs} ? () : ('-X', '-A');
31
32 my $rsync = ['rsync', '--stats', @xattr, '--numeric-ids',
50ee6ffe
WB
33 '-aH', '--delete', '--no-whole-file',
34 ($first ? '--sparse' : '--inplace'),
bd8e1739
WB
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}};
d14a9a1b 39
59a999af 40 my $starttime = time();
5a8a3087
WB
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) {
bd8e1739 51 push @$rsync, "$from/.$disk->{mp}";
5a8a3087 52 }
bd8e1739 53 $self->cmd([@$rsync, $to]);
d14a9a1b
DM
54 my $delay = time () - $starttime;
55
56 $self->loginfo ("$text sync finished ($delay seconds)");
57};
58
59sub new {
60 my ($class, $vzdump) = @_;
7808afef 61
d14a9a1b
DM
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();
7808afef 71
d14a9a1b
DM
72 $self->{vmlist} = PVE::LXC::config_list();
73
74 return $self;
75}
76
77sub type {
78 return 'lxc';
79}
80
81sub vm_status {
82 my ($self, $vmid) = @_;
83
84 my $running = PVE::LXC::check_running($vmid) ? 1 : 0;
7808afef
AL
85
86 return wantarray ? ($running, $running ? 'running' : 'stopped') : $running;
d14a9a1b
DM
87}
88
4ca61ce8 89my $check_mountpoint_empty = sub {
b739f640
DM
90 my ($mountpoint) = @_;
91
235dbdf3 92 die "mount point '$mountpoint' is not a directory\n" if ! -d $mountpoint;
4ca61ce8 93
b739f640
DM
94 PVE::Tools::dir_glob_foreach($mountpoint, qr/.*/, sub {
95 my $entry = shift;
96 return if $entry eq '.' || $entry eq '..';
235dbdf3 97 die "mount point '$mountpoint' not empty\n";
b739f640 98 });
d14a9a1b
DM
99};
100
101sub prepare {
102 my ($self, $task, $vmid, $mode) = @_;
103
67afe46e 104 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::Config->load_config($vmid);
57ed5ed0 105 my $storage_cfg = $self->{storecfg};
d14a9a1b 106
05812a36
WL
107 $self->loginfo("CT Name: $conf->{hostname}")
108 if defined($conf->{hostname});
109
d14a9a1b
DM
110 my $running = PVE::LXC::check_running($vmid);
111
2e57a9f6
WB
112 my $disks = $task->{disks} = [];
113 my $exclude_dirs = $task->{exclude_dirs} = [];
d14a9a1b 114
27916659 115 $task->{hostname} = $conf->{'hostname'} || "CT$vmid";
d14a9a1b 116
01dce99b
WB
117 my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
118 $task->{userns_cmd} = PVE::LXC::userns_command($id_map);
4c98d66c
FG
119 $task->{rootuid} = $rootuid;
120 $task->{rootgid} = $rootgid;
121
5040d81c 122 my $volids = $task->{volids} = [];
015740e6 123 PVE::LXC::Config->foreach_volume($conf, sub {
2e57a9f6
WB
124 my ($name, $data) = @_;
125 my $volid = $data->{volume};
126 my $mount = $data->{mp};
7c921c80 127 my $type = $data->{type};
d14a9a1b 128
ca8aaa5a 129 return if !$volid || !$mount;
2e57a9f6 130
1a8269bc 131 if (!PVE::LXC::Config->mountpoint_backup_enabled($name, $data)) {
2e57a9f6 132 push @$exclude_dirs, $mount;
235dbdf3 133 $self->loginfo("excluding $type mount point $name ('$mount') from backup");
2e57a9f6
WB
134 return;
135 }
136
99a0bcb0 137 $data->{name} = $name;
362a853a
FG
138
139 # immutable raw base images need RO mount
140 if ($conf->{template} && !defined($data->{ro})) {
141 $data->{ro} = 1;
142 }
2e57a9f6 143 push @$disks, $data;
5040d81c 144 push @$volids, $volid
b5990246 145 if $type eq 'volume';
2e57a9f6 146 });
d14a9a1b 147
d14a9a1b 148 if ($mode eq 'snapshot') {
4518000b 149 if (!PVE::LXC::Config->has_feature('snapshot', $conf, $storage_cfg, undef, undef, 1)) {
c31ad455 150 die "mode failure - some volumes do not support snapshots\n";
4ca61ce8 151 }
d14a9a1b 152
2d3f23be 153
4ca61ce8
DM
154 if ($conf->{snapshots} && $conf->{snapshots}->{vzdump}) {
155 $self->loginfo("found old vzdump snapshot (force removal)");
91458f71
FG
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 });
4ca61ce8 161 }
d14a9a1b 162
22a91261
WB
163 my $rootdir = $default_mount_point;
164 mkpath $rootdir;
165 &$check_mountpoint_empty($rootdir);
d14a9a1b 166
c31ad455 167 # set snapshot_count (freezes CT if snapshot_count > 1)
5040d81c 168 $task->{snapshot_count} = scalar(@$volids);
f5313774 169 } elsif ($mode eq 'stop') {
22a91261
WB
170 my $rootdir = $default_mount_point;
171 mkpath $rootdir;
172 &$check_mountpoint_empty($rootdir);
f5313774 173 } elsif ($mode eq 'suspend') {
632eca5a 174 my $pid = PVE::LXC::find_lxc_pid($vmid);
2e57a9f6
WB
175 foreach my $disk (@$disks) {
176 $disk->{dir} = "/proc/$pid/root$disk->{mp}";
177 }
f5313774
DM
178 $task->{snapdir} = $task->{tmpdir};
179 } else {
2d3f23be 180 unlock_vm($self, $vmid);
f5313774 181 die "unknown mode '$mode'\n"; # should not happen
d14a9a1b 182 }
b6c491ee
WB
183
184 if ($mode ne 'suspend') {
c31ad455 185 # If we perform mount operations, let's unshare the mount namespace
b6c491ee
WB
186 # to not influence the running host.
187 PVE::Tools::unshare(PVE::Tools::CLONE_NEWNS);
a78425d7 188 PVE::Tools::run_command(['mount', '--make-rslave', '/']);
b6c491ee 189 }
d14a9a1b
DM
190}
191
192sub lock_vm {
193 my ($self, $vmid) = @_;
2d3f23be 194
ad408fe1 195 PVE::LXC::Config->set_lock($vmid, 'backup');
d14a9a1b
DM
196}
197
198sub unlock_vm {
199 my ($self, $vmid) = @_;
2d3f23be 200
ad408fe1 201 PVE::LXC::Config->remove_lock($vmid, 'backup')
d14a9a1b
DM
202}
203
4ca61ce8
DM
204sub snapshot {
205 my ($self, $task, $vmid) = @_;
206
c31ad455 207 $self->loginfo("create storage snapshot 'vzdump'");
4ca61ce8
DM
208
209 # todo: freeze/unfreeze if we have more than one volid
91458f71
FG
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 });
4ca61ce8 215 $task->{cleanup}->{remove_snapshot} = 1;
7808afef 216
4ca61ce8 217 # reload config
67afe46e 218 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::Config->load_config($vmid);
c31ad455 219 die "unable to read vzdump snapshot config - internal error"
4ca61ce8
DM
220 if !($conf->{snapshots} && $conf->{snapshots}->{vzdump});
221
2e57a9f6 222 my $disks = $task->{disks};
5040d81c 223 my $volids = $task->{volids};
4ca61ce8 224
22a91261 225 my $rootdir = $default_mount_point;
2e57a9f6
WB
226 my $storage_cfg = $self->{storecfg};
227
5040d81c 228 PVE::Storage::activate_volumes($storage_cfg, $volids, 'vzdump');
2e57a9f6
WB
229 foreach my $disk (@$disks) {
230 $disk->{dir} = "${rootdir}$disk->{mp}";
4c98d66c 231 PVE::LXC::mountpoint_mount($disk, $rootdir, $storage_cfg, 'vzdump', $task->{rootuid}, $task->{rootgid});
2e57a9f6
WB
232 }
233
234 $task->{snapdir} = $rootdir;
4ca61ce8
DM
235}
236
d14a9a1b
DM
237sub copy_data_phase1 {
238 my ($self, $task) = @_;
239
9c9a3b8c
WB
240 if (my $mntinfo = PVE::VZDump::get_mount_info($task->{snapdir})) {
241 if ($mntinfo->{fstype} =~ /^nfs4?/) {
b67c14c0
WB
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");
9c9a3b8c
WB
246 $task->{no_xattrs} = 1;
247 }
248 }
249
50ee6ffe 250 $self->$rsync_vm($task, $task->{snapdir}, "first", 1);
d14a9a1b
DM
251}
252
253sub copy_data_phase2 {
254 my ($self, $task) = @_;
255
50ee6ffe 256 $self->$rsync_vm($task, $task->{snapdir}, "final", 0);
d14a9a1b
DM
257}
258
259sub stop_vm {
260 my ($self, $task, $vmid) = @_;
261
f90f7e53
TL
262 my $opts = $self->{vzdump}->{opts};
263 my $timeout = $opts->{stopwait} * 60;
264
b1bad293 265 PVE::LXC::vm_stop($vmid, 0, $timeout);
d14a9a1b
DM
266}
267
268sub start_vm {
269 my ($self, $task, $vmid) = @_;
270
be8ec1b0 271 $self->cmd(['systemctl', 'start', "pve-container\@$vmid"]);
d14a9a1b
DM
272}
273
274sub suspend_vm {
275 my ($self, $task, $vmid) = @_;
276
277 $self->cmd ("lxc-freeze -n $vmid");
278}
279
280sub resume_vm {
281 my ($self, $task, $vmid) = @_;
282
283 $self->cmd ("lxc-unfreeze -n $vmid");
284}
285
286sub assemble {
287 my ($self, $task, $vmid) = @_;
288
99a0bcb0 289 my $opts = $self->{vzdump}->{opts};
514b5f82 290
67afe46e 291 my $conf = PVE::LXC::Config->load_config($vmid);
48314d4e 292 delete $conf->{lock};
514b5f82 293 delete $conf->{snapshots};
f626ac9d 294 delete $conf->{parent};
53fafd3f 295 delete $conf->{pending};
514b5f82 296
99a0bcb0
DM
297 my $tmpdir = $task->{tmpdir};
298
299 mkpath "$tmpdir/etc/vzdump/";
300
1b4cf758 301 PVE::Tools::file_set_contents("$tmpdir/etc/vzdump/pct.conf", PVE::LXC::Config::write_pct_config("/lxc/$vmid.conf", $conf));
bf040874
WL
302
303 my $firewall ="/etc/pve/firewall/$vmid.fw";
d1671a80 304 my $fwconftmp = "$tmpdir/etc/vzdump/pct.fw";
99a0bcb0
DM
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 }
d1671a80 311 } else {
99a0bcb0
DM
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;
bf040874 318 }
d14a9a1b
DM
319}
320
321sub archive {
322 my ($self, $task, $vmid, $filename, $comp) = @_;
cbd6753d 323
b7ec90ed 324 my $disks = $task->{disks};
5a8a3087 325 my @sources;
b7ec90ed 326
459fd4d2 327 if ($task->{mode} eq 'stop') {
2e57a9f6 328 my $storage_cfg = $self->{storecfg};
8ebb49ee 329
00cc0416 330 PVE::Storage::activate_volumes($storage_cfg, $task->{volids});
8ebb49ee
TL
331
332 my $rootdir = $default_mount_point;
2e57a9f6
WB
333 foreach my $disk (@$disks) {
334 $disk->{dir} = "${rootdir}$disk->{mp}";
4c98d66c 335 PVE::LXC::mountpoint_mount($disk, $rootdir, $storage_cfg, undef, $task->{rootuid}, $task->{rootgid});
5a8a3087
WB
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}";
2e57a9f6
WB
339 }
340 $task->{snapdir} = $rootdir;
5040d81c
FG
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 }
5a8a3087
WB
347 } else {
348 # the data was rsynced to a temporary location, only use '.' to avoid
349 # having mountpoints duplicated
350 push @sources, '.';
459fd4d2
DM
351 }
352
d14a9a1b 353 my $opts = $self->{vzdump}->{opts};
d14a9a1b 354 my $snapdir = $task->{snapdir};
cbd6753d 355 my $tmpdir = $task->{tmpdir};
d14a9a1b 356
01dce99b 357 my $userns_cmd = $task->{userns_cmd};
99a0bcb0
DM
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
d14a9a1b 388 } else {
99a0bcb0
DM
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 }
d14a9a1b
DM
425 }
426}
427
428sub cleanup {
429 my ($self, $task, $vmid) = @_;
430
67afe46e 431 my $conf = PVE::LXC::Config->load_config($vmid);
d14a9a1b 432
0c44de7b 433 if ($task->{mode} ne 'suspend') {
96d20be2
DM
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 }
2e57a9f6 439 }
b739f640 440
4ca61ce8
DM
441 if ($task->{cleanup}->{remove_snapshot}) {
442 $self->loginfo("remove vzdump snapshot");
4518000b 443 PVE::LXC::Config->snapshot_delete($vmid, 'vzdump', 0);
d14a9a1b 444 }
d14a9a1b
DM
445}
446
4471;