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