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