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