]> git.proxmox.com Git - pve-container.git/blob - src/PVE/VZDump/LXC.pm
new helper mountpoint_backup_enabled()
[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::LXC::Config;
13 use PVE::Tools;
14
15 use base qw (PVE::VZDump::Plugin);
16
17 my $default_mount_point = "/mnt/vzsnap0";
18
19 my $rsync_vm = sub {
20 my ($self, $task, $to, $text) = @_;
21
22 my $disks = $task->{disks};
23 my $from = $disks->[0]->{dir} . '/';
24 $self->loginfo ("starting $text sync $from to $to");
25
26 my $opts = $self->{vzdump}->{opts};
27
28 my @xattr = $task->{no_xattrs} ? () : ('-X', '-A');
29
30 my $rsync = ['rsync', '--stats', @xattr, '--numeric-ids',
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}};
36
37 my $starttime = time();
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) {
48 push @$rsync, "$from/.$disk->{mp}";
49 }
50 $self->cmd([@$rsync, $to]);
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
86 my $check_mountpoint_empty = sub {
87 my ($mountpoint) = @_;
88
89 die "mountpoint '$mountpoint' is not a directory\n" if ! -d $mountpoint;
90
91 PVE::Tools::dir_glob_foreach($mountpoint, qr/.*/, sub {
92 my $entry = shift;
93 return if $entry eq '.' || $entry eq '..';
94 die "mountpoint '$mountpoint' not empty\n";
95 });
96 };
97
98 sub prepare {
99 my ($self, $task, $vmid, $mode) = @_;
100
101 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::Config->load_config($vmid);
102 my $storage_cfg = $self->{storecfg};
103
104 my $running = PVE::LXC::check_running($vmid);
105
106 my $disks = $task->{disks} = [];
107 my $exclude_dirs = $task->{exclude_dirs} = [];
108
109 $task->{hostname} = $conf->{'hostname'} || "CT$vmid";
110
111 my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
112 $task->{userns_cmd} = PVE::LXC::userns_command($id_map);
113
114 my $volids = $task->{volids} = [];
115 PVE::LXC::Config->foreach_mountpoint($conf, sub {
116 my ($name, $data) = @_;
117 my $volid = $data->{volume};
118 my $mount = $data->{mp};
119 my $type = $data->{type};
120
121 return if !$volid || !$mount;
122
123 if (!PVE::LXC::Config->mountpoint_backup_enabled($name, $data)) {
124 push @$exclude_dirs, $mount;
125 return;
126 }
127
128 push @$disks, $data;
129 push @$volids, $volid
130 if $type eq 'volume';
131 });
132
133 if ($mode eq 'snapshot') {
134 if (!PVE::LXC::Config->has_feature('snapshot', $conf, $storage_cfg, undef, undef, 1)) {
135 die "mode failure - some volumes do not support snapshots\n";
136 }
137
138
139 if ($conf->{snapshots} && $conf->{snapshots}->{vzdump}) {
140 $self->loginfo("found old vzdump snapshot (force removal)");
141 PVE::LXC::Config->lock_config($vmid, sub {
142 $self->unlock_vm($vmid);
143 PVE::LXC::Config->snapshot_delete($vmid, 'vzdump', 1);
144 $self->lock_vm($vmid);
145 });
146 }
147
148 my $rootdir = $default_mount_point;
149 mkpath $rootdir;
150 &$check_mountpoint_empty($rootdir);
151
152 # set snapshot_count (freezes CT if snapshot_count > 1)
153 $task->{snapshot_count} = scalar(@$volids);
154 } elsif ($mode eq 'stop') {
155 my $rootdir = $default_mount_point;
156 mkpath $rootdir;
157 &$check_mountpoint_empty($rootdir);
158 PVE::Storage::activate_volumes($storage_cfg, $volids);
159 } elsif ($mode eq 'suspend') {
160 my $pid = PVE::LXC::find_lxc_pid($vmid);
161 foreach my $disk (@$disks) {
162 $disk->{dir} = "/proc/$pid/root$disk->{mp}";
163 }
164 $task->{snapdir} = $task->{tmpdir};
165 } else {
166 unlock_vm($self, $vmid);
167 die "unknown mode '$mode'\n"; # should not happen
168 }
169
170 if ($mode ne 'suspend') {
171 # If we perform mount operations, let's unshare the mount namespace
172 # to not influence the running host.
173 PVE::Tools::unshare(PVE::Tools::CLONE_NEWNS);
174 PVE::Tools::run_command(['mount', '--make-rprivate', '/']);
175 }
176 }
177
178 sub lock_vm {
179 my ($self, $vmid) = @_;
180
181 PVE::LXC::Config->set_lock($vmid, 'backup');
182 }
183
184 sub unlock_vm {
185 my ($self, $vmid) = @_;
186
187 PVE::LXC::Config->remove_lock($vmid, 'backup')
188 }
189
190 sub snapshot {
191 my ($self, $task, $vmid) = @_;
192
193 $self->loginfo("create storage snapshot 'vzdump'");
194
195 # todo: freeze/unfreeze if we have more than one volid
196 PVE::LXC::Config->lock_config($vmid, sub {
197 $self->unlock_vm($vmid);
198 PVE::LXC::Config->snapshot_create($vmid, 'vzdump', 0, "vzdump backup snapshot");
199 $self->lock_vm($vmid);
200 });
201 $task->{cleanup}->{remove_snapshot} = 1;
202
203 # reload config
204 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::Config->load_config($vmid);
205 die "unable to read vzdump snapshot config - internal error"
206 if !($conf->{snapshots} && $conf->{snapshots}->{vzdump});
207
208 my $disks = $task->{disks};
209 my $volids = $task->{volids};
210
211 my $rootdir = $default_mount_point;
212 my $storage_cfg = $self->{storecfg};
213
214 PVE::Storage::activate_volumes($storage_cfg, $volids, 'vzdump');
215 foreach my $disk (@$disks) {
216 $disk->{dir} = "${rootdir}$disk->{mp}";
217 PVE::LXC::mountpoint_mount($disk, $rootdir, $storage_cfg, 'vzdump');
218 }
219
220 $task->{snapdir} = $rootdir;
221 }
222
223 sub copy_data_phase1 {
224 my ($self, $task) = @_;
225
226 if (my $mntinfo = PVE::VZDump::get_mount_info($task->{snapdir})) {
227 if ($mntinfo->{fstype} =~ /^nfs4?/) {
228 warn "temporary directory is on NFS, disabling xattr and acl support"
229 . ", consider configuring a local tmpdir via /etc/vzdump.conf\n";
230 $task->{no_xattrs} = 1;
231 }
232 }
233
234 $self->$rsync_vm($task, $task->{snapdir}, "first");
235 }
236
237 sub copy_data_phase2 {
238 my ($self, $task) = @_;
239
240 $self->$rsync_vm($task, $task->{snapdir}, "final");
241 }
242
243 sub stop_vm {
244 my ($self, $task, $vmid) = @_;
245
246 $self->cmd("lxc-stop -n $vmid");
247
248 # make sure container is stopped
249 $self->cmd("lxc-wait -n $vmid -s STOPPED");
250 }
251
252 sub start_vm {
253 my ($self, $task, $vmid) = @_;
254
255 $self->cmd ("lxc-start -n $vmid");
256 }
257
258 sub suspend_vm {
259 my ($self, $task, $vmid) = @_;
260
261 $self->cmd ("lxc-freeze -n $vmid");
262 }
263
264 sub resume_vm {
265 my ($self, $task, $vmid) = @_;
266
267 $self->cmd ("lxc-unfreeze -n $vmid");
268 }
269
270 sub assemble {
271 my ($self, $task, $vmid) = @_;
272
273 my $tmpdir = $task->{tmpdir};
274
275 mkpath "$tmpdir/etc/vzdump/";
276
277 my $conf = PVE::LXC::Config->load_config($vmid);
278 delete $conf->{lock};
279 delete $conf->{snapshots};
280 delete $conf->{'pve.parent'};
281
282 PVE::Tools::file_set_contents("$tmpdir/etc/vzdump/pct.conf", PVE::LXC::Config::write_pct_config("/lxc/$vmid.conf", $conf));
283
284 my $firewall ="/etc/pve/firewall/$vmid.fw";
285 if (-e $firewall) {
286 PVE::Tools::file_copy($firewall, "$tmpdir/etc/vzdump/pct.fw");
287 $task->{fw} = 1;
288 }
289 }
290
291 sub archive {
292 my ($self, $task, $vmid, $filename, $comp) = @_;
293
294 my $disks = $task->{disks};
295 my @sources;
296
297 if ($task->{mode} eq 'stop') {
298 my $rootdir = $default_mount_point;
299 my $storage_cfg = $self->{storecfg};
300 foreach my $disk (@$disks) {
301 $disk->{dir} = "${rootdir}$disk->{mp}";
302 PVE::LXC::mountpoint_mount($disk, $rootdir, $storage_cfg);
303 # add every enabled mountpoint (since we use --one-file-system)
304 # mp already starts with a / so we only need to add the dot
305 push @sources, ".$disk->{mp}";
306 }
307 $task->{snapdir} = $rootdir;
308 } elsif ($task->{mode} eq 'snapshot') {
309 # mounting the vzdump snapshots and setting $snapdir is already done,
310 # but we need to include all mountpoints here!
311 foreach my $disk (@$disks) {
312 push @sources, ".$disk->{mp}";
313 }
314 } else {
315 # the data was rsynced to a temporary location, only use '.' to avoid
316 # having mountpoints duplicated
317 push @sources, '.';
318 }
319
320 my $opts = $self->{vzdump}->{opts};
321 my $snapdir = $task->{snapdir};
322 my $tmpdir = $task->{tmpdir};
323
324 my $userns_cmd = $task->{userns_cmd};
325 my $tar = [@$userns_cmd, 'tar', 'cpf', '-', '--totals',
326 @$PVE::LXC::COMMON_TAR_FLAGS,
327 '--one-file-system', '--warning=no-file-ignored'];
328
329 # note: --remove-files does not work because we do not
330 # backup all files (filters). tar complains:
331 # Cannot rmdir: Directory not empty
332 # we disable this optimization for now
333 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
334 # push @$tar, "--remove-files"; # try to save space
335 #}
336
337 # The directory parameter can give an alternative directory as source.
338 # the second parameter gives the structure in the tar.
339 push @$tar, "--directory=$tmpdir", './etc/vzdump/pct.conf';
340 push @$tar, "./etc/vzdump/pct.fw" if $task->{fw};
341 push @$tar, "--directory=$snapdir";
342 push @$tar, '--no-anchored', '--exclude=lost+found' if $userns_cmd;
343 push @$tar, '--anchored';
344 push @$tar, map { "--exclude=.$_" } @{$self->{vzdump}->{findexcl}};
345
346 push @$tar, @sources;
347
348 my $cmd = [ $tar ];
349
350 my $bwl = $opts->{bwlimit}*1024; # bandwidth limit for cstream
351 push @$cmd, [ 'cstream', '-t', $bwl ] if $opts->{bwlimit};
352 push @$cmd, [ split(/\s+/, $comp) ] if $comp;
353
354 if ($opts->{stdout}) {
355 $self->cmd($cmd, output => ">&" . fileno($opts->{stdout}));
356 } else {
357 push @{$cmd->[-1]}, \(">" . PVE::Tools::shellquote($filename));
358 $self->cmd($cmd);
359 }
360 }
361
362 sub cleanup {
363 my ($self, $task, $vmid) = @_;
364
365 my $conf = PVE::LXC::Config->load_config($vmid);
366
367 if ($task->{mode} ne 'suspend') {
368 my $rootdir = $default_mount_point;
369 my $disks = $task->{disks};
370 foreach my $disk (reverse @$disks) {
371 PVE::Tools::run_command(['umount', '-l', '-d', $disk->{dir}]) if $disk->{dir};
372 }
373 }
374
375 if ($task->{cleanup}->{remove_snapshot}) {
376 $self->loginfo("remove vzdump snapshot");
377 PVE::LXC::Config->snapshot_delete($vmid, 'vzdump', 0);
378 }
379 }
380
381 1;