]> git.proxmox.com Git - pve-container.git/blob - src/PVE/VZDump/LXC.pm
LXC: separate freeze_thaw to explicit freeze and thaw methods
[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
65 my $self = bless {};
66
67 $self->{vzdump} = $vzdump;
68 $self->{storecfg} = PVE::Storage::config();
69
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;
83
84 return wantarray ? ($running, $running ? 'running' : 'stopped') : $running;
85 }
86
87 my $check_mountpoint_empty = sub {
88 my ($mountpoint) = @_;
89
90 die "mount point '$mountpoint' is not a directory\n" if ! -d $mountpoint;
91
92 PVE::Tools::dir_glob_foreach($mountpoint, qr/.*/, sub {
93 my $entry = shift;
94 return if $entry eq '.' || $entry eq '..';
95 die "mount point '$mountpoint' not empty\n";
96 });
97 };
98
99 sub prepare {
100 my ($self, $task, $vmid, $mode) = @_;
101
102 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::Config->load_config($vmid);
103 my $storage_cfg = $self->{storecfg};
104
105 $self->loginfo("CT Name: $conf->{hostname}")
106 if defined($conf->{hostname});
107
108 my $running = PVE::LXC::check_running($vmid);
109
110 my $disks = $task->{disks} = [];
111 my $exclude_dirs = $task->{exclude_dirs} = [];
112
113 $task->{hostname} = $conf->{'hostname'} || "CT$vmid";
114
115 my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
116 $task->{userns_cmd} = PVE::LXC::userns_command($id_map);
117 $task->{rootuid} = $rootuid;
118 $task->{rootgid} = $rootgid;
119
120 my $volids = $task->{volids} = [];
121 PVE::LXC::Config->foreach_volume($conf, sub {
122 my ($name, $data) = @_;
123 my $volid = $data->{volume};
124 my $mount = $data->{mp};
125 my $type = $data->{type};
126
127 return if !$volid || !$mount;
128
129 if (!PVE::LXC::Config->mountpoint_backup_enabled($name, $data)) {
130 push @$exclude_dirs, $mount;
131 $self->loginfo("excluding $type mount point $name ('$mount') from backup");
132 return;
133 }
134
135 $data->{name} = $name;
136
137 # immutable raw base images need RO mount
138 if ($conf->{template} && !defined($data->{ro})) {
139 $data->{ro} = 1;
140 }
141 push @$disks, $data;
142 push @$volids, $volid
143 if $type eq 'volume';
144 });
145
146 if ($mode eq 'snapshot') {
147 if (!PVE::LXC::Config->has_feature('snapshot', $conf, $storage_cfg, undef, undef, 1)) {
148 die "mode failure - some volumes do not support snapshots\n";
149 }
150
151
152 if ($conf->{snapshots} && $conf->{snapshots}->{vzdump}) {
153 $self->loginfo("found old vzdump snapshot (force removal)");
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 });
159 }
160
161 my $rootdir = $default_mount_point;
162 mkpath $rootdir;
163 &$check_mountpoint_empty($rootdir);
164
165 # set snapshot_count (freezes CT if snapshot_count > 1)
166 $task->{snapshot_count} = scalar(@$volids);
167 } elsif ($mode eq 'stop') {
168 my $rootdir = $default_mount_point;
169 mkpath $rootdir;
170 &$check_mountpoint_empty($rootdir);
171 } elsif ($mode eq 'suspend') {
172 my $pid = PVE::LXC::find_lxc_pid($vmid);
173 foreach my $disk (@$disks) {
174 $disk->{dir} = "/proc/$pid/root$disk->{mp}";
175 }
176 $task->{snapdir} = $task->{tmpdir};
177 } else {
178 unlock_vm($self, $vmid);
179 die "unknown mode '$mode'\n"; # should not happen
180 }
181
182 if ($mode ne 'suspend') {
183 # If we perform mount operations, let's unshare the mount namespace
184 # to not influence the running host.
185 PVE::Tools::unshare(PVE::Tools::CLONE_NEWNS);
186 PVE::Tools::run_command(['mount', '--make-rslave', '/']);
187 }
188 }
189
190 sub lock_vm {
191 my ($self, $vmid) = @_;
192
193 PVE::LXC::Config->set_lock($vmid, 'backup');
194 }
195
196 sub unlock_vm {
197 my ($self, $vmid) = @_;
198
199 PVE::LXC::Config->remove_lock($vmid, 'backup')
200 }
201
202 sub snapshot {
203 my ($self, $task, $vmid) = @_;
204
205 $self->loginfo("create storage snapshot 'vzdump'");
206
207 # todo: freeze/unfreeze if we have more than one volid
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 });
213 $task->{cleanup}->{remove_snapshot} = 1;
214
215 # reload config
216 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::Config->load_config($vmid);
217 die "unable to read vzdump snapshot config - internal error"
218 if !($conf->{snapshots} && $conf->{snapshots}->{vzdump});
219
220 my $disks = $task->{disks};
221 my $volids = $task->{volids};
222
223 my $rootdir = $default_mount_point;
224 my $storage_cfg = $self->{storecfg};
225
226 PVE::Storage::activate_volumes($storage_cfg, $volids, 'vzdump');
227 foreach my $disk (@$disks) {
228 $disk->{dir} = "${rootdir}$disk->{mp}";
229 PVE::LXC::mountpoint_mount($disk, $rootdir, $storage_cfg, 'vzdump', $task->{rootuid}, $task->{rootgid});
230 }
231
232 $task->{snapdir} = $rootdir;
233 }
234
235 sub copy_data_phase1 {
236 my ($self, $task) = @_;
237
238 if (my $mntinfo = PVE::VZDump::get_mount_info($task->{snapdir})) {
239 if ($mntinfo->{fstype} =~ /^nfs4?/) {
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");
244 $task->{no_xattrs} = 1;
245 }
246 }
247
248 $self->$rsync_vm($task, $task->{snapdir}, "first", 1);
249 }
250
251 sub copy_data_phase2 {
252 my ($self, $task) = @_;
253
254 $self->$rsync_vm($task, $task->{snapdir}, "final", 0);
255 }
256
257 sub stop_vm {
258 my ($self, $task, $vmid) = @_;
259
260 my $opts = $self->{vzdump}->{opts};
261 my $timeout = $opts->{stopwait} * 60;
262
263 PVE::LXC::vm_stop($vmid, 0, $timeout);
264 }
265
266 sub start_vm {
267 my ($self, $task, $vmid) = @_;
268
269 $self->cmd(['systemctl', 'start', "pve-container\@$vmid"]);
270 }
271
272 sub suspend_vm {
273 my ($self, $task, $vmid) = @_;
274
275 PVE::LXC::freeze($vmid);
276 }
277
278 sub resume_vm {
279 my ($self, $task, $vmid) = @_;
280
281 PVE::LXC::thaw($vmid);
282 }
283
284 sub assemble {
285 my ($self, $task, $vmid) = @_;
286
287 my $opts = $self->{vzdump}->{opts};
288
289 my $conf = PVE::LXC::Config->load_config($vmid);
290 delete $conf->{lock};
291 delete $conf->{snapshots};
292 delete $conf->{parent};
293 delete $conf->{pending};
294
295 my $tmpdir = $task->{tmpdir};
296
297 mkpath "$tmpdir/etc/vzdump/";
298
299 PVE::Tools::file_set_contents("$tmpdir/etc/vzdump/pct.conf", PVE::LXC::Config::write_pct_config("/lxc/$vmid.conf", $conf));
300
301 my $firewall ="/etc/pve/firewall/$vmid.fw";
302 my $fwconftmp = "$tmpdir/etc/vzdump/pct.fw";
303
304 if ($self->{vzdump}->{opts}->{pbs}) {
305 # fixme: do not store pct.conf and fw.conf into $tmpdir
306 if (-e $firewall) {
307 PVE::Tools::file_copy($firewall, $fwconftmp);
308 }
309 } else {
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;
316 }
317 }
318
319 sub archive {
320 my ($self, $task, $vmid, $filename, $comp) = @_;
321
322 my $disks = $task->{disks};
323 my @sources;
324
325 if ($task->{mode} eq 'stop') {
326 my $storage_cfg = $self->{storecfg};
327
328 PVE::Storage::activate_volumes($storage_cfg, $task->{volids});
329
330 my $rootdir = $default_mount_point;
331 foreach my $disk (@$disks) {
332 $disk->{dir} = "${rootdir}$disk->{mp}";
333 PVE::LXC::mountpoint_mount($disk, $rootdir, $storage_cfg, undef, $task->{rootuid}, $task->{rootgid});
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}";
337 }
338 $task->{snapdir} = $rootdir;
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 }
345 } else {
346 # the data was rsynced to a temporary location, only use '.' to avoid
347 # having mountpoints duplicated
348 push @sources, '.';
349 }
350
351 my $opts = $self->{vzdump}->{opts};
352 my $snapdir = $task->{snapdir};
353 my $tmpdir = $task->{tmpdir};
354
355 my $userns_cmd = $task->{userns_cmd};
356
357 if ($self->{vzdump}->{opts}->{pbs}) {
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
386 } else {
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 }
423 }
424 }
425
426 sub cleanup {
427 my ($self, $task, $vmid) = @_;
428
429 my $conf = PVE::LXC::Config->load_config($vmid);
430
431 if ($task->{mode} ne 'suspend') {
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 }
437 }
438
439 if ($task->{cleanup}->{remove_snapshot}) {
440 $self->loginfo("remove vzdump snapshot");
441 PVE::LXC::Config->snapshot_delete($vmid, 'vzdump', 0);
442 }
443 }
444
445 1;