]> git.proxmox.com Git - pve-container.git/blob - src/PVE/VZDump/LXC.pm
f9616e0bb44838d3282ea85997b58134e74feef3
[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, $first) = @_;
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',
32 ($first ? '--sparse' : '--inplace'),
33 '--one-file-system', '--relative'];
34 push @$rsync, "--bwlimit=$opts->{bwlimit}" if $opts->{bwlimit};
35 push @$rsync, map { "--exclude=$_" } @{$self->{vzdump}->{findexcl}};
36 push @$rsync, map { "--exclude=$_" } @{$task->{exclude_dirs}};
37
38 my $starttime = time();
39 # See the rsync(1) manpage for --relative in conjunction with /./ in paths.
40 # This is the only way to have exclude-dirs work together with the
41 # --one-file-system option.
42 # This way we can pass multiple source paths and tell rsync which directory
43 # they're supposed to be relative to.
44 # Otherwise with eg. using multiple rsync commands means the --exclude
45 # directives need to be modified for every command as they are meant to be
46 # relative to the rootdir, while rsync treats them as relative to the
47 # source dir.
48 foreach my $disk (@$disks) {
49 push @$rsync, "$from/.$disk->{mp}";
50 }
51 $self->cmd([@$rsync, $to]);
52 my $delay = time () - $starttime;
53
54 $self->loginfo ("$text sync finished ($delay seconds)");
55 };
56
57 sub new {
58 my ($class, $vzdump) = @_;
59
60 PVE::VZDump::check_bin('lxc-stop');
61 PVE::VZDump::check_bin('lxc-start');
62 PVE::VZDump::check_bin('lxc-freeze');
63 PVE::VZDump::check_bin('lxc-unfreeze');
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
118 my $volids = $task->{volids} = [];
119 PVE::LXC::Config->foreach_mountpoint($conf, sub {
120 my ($name, $data) = @_;
121 my $volid = $data->{volume};
122 my $mount = $data->{mp};
123 my $type = $data->{type};
124
125 return if !$volid || !$mount;
126
127 if (!PVE::LXC::Config->mountpoint_backup_enabled($name, $data)) {
128 push @$exclude_dirs, $mount;
129 $self->loginfo("excluding $type mount point $name ('$mount') from backup");
130 return;
131 }
132
133 push @$disks, $data;
134 push @$volids, $volid
135 if $type eq 'volume';
136 });
137
138 if ($mode eq 'snapshot') {
139 if (!PVE::LXC::Config->has_feature('snapshot', $conf, $storage_cfg, undef, undef, 1)) {
140 die "mode failure - some volumes do not support snapshots\n";
141 }
142
143
144 if ($conf->{snapshots} && $conf->{snapshots}->{vzdump}) {
145 $self->loginfo("found old vzdump snapshot (force removal)");
146 PVE::LXC::Config->lock_config($vmid, sub {
147 $self->unlock_vm($vmid);
148 PVE::LXC::Config->snapshot_delete($vmid, 'vzdump', 1);
149 $self->lock_vm($vmid);
150 });
151 }
152
153 my $rootdir = $default_mount_point;
154 mkpath $rootdir;
155 &$check_mountpoint_empty($rootdir);
156
157 # set snapshot_count (freezes CT if snapshot_count > 1)
158 $task->{snapshot_count} = scalar(@$volids);
159 } elsif ($mode eq 'stop') {
160 my $rootdir = $default_mount_point;
161 mkpath $rootdir;
162 &$check_mountpoint_empty($rootdir);
163 PVE::Storage::activate_volumes($storage_cfg, $volids);
164 } elsif ($mode eq 'suspend') {
165 my $pid = PVE::LXC::find_lxc_pid($vmid);
166 foreach my $disk (@$disks) {
167 $disk->{dir} = "/proc/$pid/root$disk->{mp}";
168 }
169 $task->{snapdir} = $task->{tmpdir};
170 } else {
171 unlock_vm($self, $vmid);
172 die "unknown mode '$mode'\n"; # should not happen
173 }
174
175 if ($mode ne 'suspend') {
176 # If we perform mount operations, let's unshare the mount namespace
177 # to not influence the running host.
178 PVE::Tools::unshare(PVE::Tools::CLONE_NEWNS);
179 PVE::Tools::run_command(['mount', '--make-rslave', '/']);
180 }
181 }
182
183 sub lock_vm {
184 my ($self, $vmid) = @_;
185
186 PVE::LXC::Config->set_lock($vmid, 'backup');
187 }
188
189 sub unlock_vm {
190 my ($self, $vmid) = @_;
191
192 PVE::LXC::Config->remove_lock($vmid, 'backup')
193 }
194
195 sub snapshot {
196 my ($self, $task, $vmid) = @_;
197
198 $self->loginfo("create storage snapshot 'vzdump'");
199
200 # todo: freeze/unfreeze if we have more than one volid
201 PVE::LXC::Config->lock_config($vmid, sub {
202 $self->unlock_vm($vmid);
203 PVE::LXC::Config->snapshot_create($vmid, 'vzdump', 0, "vzdump backup snapshot");
204 $self->lock_vm($vmid);
205 });
206 $task->{cleanup}->{remove_snapshot} = 1;
207
208 # reload config
209 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::Config->load_config($vmid);
210 die "unable to read vzdump snapshot config - internal error"
211 if !($conf->{snapshots} && $conf->{snapshots}->{vzdump});
212
213 my $disks = $task->{disks};
214 my $volids = $task->{volids};
215
216 my $rootdir = $default_mount_point;
217 my $storage_cfg = $self->{storecfg};
218
219 PVE::Storage::activate_volumes($storage_cfg, $volids, 'vzdump');
220 foreach my $disk (@$disks) {
221 $disk->{dir} = "${rootdir}$disk->{mp}";
222 PVE::LXC::mountpoint_mount($disk, $rootdir, $storage_cfg, 'vzdump');
223 }
224
225 $task->{snapdir} = $rootdir;
226 }
227
228 sub copy_data_phase1 {
229 my ($self, $task) = @_;
230
231 if (my $mntinfo = PVE::VZDump::get_mount_info($task->{snapdir})) {
232 if ($mntinfo->{fstype} =~ /^nfs4?/) {
233 warn "temporary directory is on NFS, disabling xattr and acl support"
234 . ", consider configuring a local tmpdir via /etc/vzdump.conf\n";
235 $task->{no_xattrs} = 1;
236 }
237 }
238
239 $self->$rsync_vm($task, $task->{snapdir}, "first", 1);
240 }
241
242 sub copy_data_phase2 {
243 my ($self, $task) = @_;
244
245 $self->$rsync_vm($task, $task->{snapdir}, "final", 0);
246 }
247
248 sub stop_vm {
249 my ($self, $task, $vmid) = @_;
250
251 my $opts = $self->{vzdump}->{opts};
252 my $timeout = $opts->{stopwait} * 60;
253
254 $self->cmd("lxc-stop -n $vmid -t $timeout");
255
256 # make sure container is stopped
257 $self->cmd("lxc-wait -n $vmid -s STOPPED");
258 }
259
260 sub start_vm {
261 my ($self, $task, $vmid) = @_;
262
263 $self->cmd(['systemctl', 'start', "pve-container\@$vmid"]);
264 }
265
266 sub suspend_vm {
267 my ($self, $task, $vmid) = @_;
268
269 $self->cmd ("lxc-freeze -n $vmid");
270 }
271
272 sub resume_vm {
273 my ($self, $task, $vmid) = @_;
274
275 $self->cmd ("lxc-unfreeze -n $vmid");
276 }
277
278 sub assemble {
279 my ($self, $task, $vmid) = @_;
280
281 my $tmpdir = $task->{tmpdir};
282
283 mkpath "$tmpdir/etc/vzdump/";
284
285 my $conf = PVE::LXC::Config->load_config($vmid);
286 delete $conf->{lock};
287 delete $conf->{snapshots};
288 delete $conf->{parent};
289
290 PVE::Tools::file_set_contents("$tmpdir/etc/vzdump/pct.conf", PVE::LXC::Config::write_pct_config("/lxc/$vmid.conf", $conf));
291
292 my $firewall ="/etc/pve/firewall/$vmid.fw";
293 if (-e $firewall) {
294 PVE::Tools::file_copy($firewall, "$tmpdir/etc/vzdump/pct.fw");
295 $task->{fw} = 1;
296 }
297 }
298
299 sub archive {
300 my ($self, $task, $vmid, $filename, $comp) = @_;
301
302 my $disks = $task->{disks};
303 my @sources;
304
305 if ($task->{mode} eq 'stop') {
306 my $rootdir = $default_mount_point;
307 my $storage_cfg = $self->{storecfg};
308 foreach my $disk (@$disks) {
309 $disk->{dir} = "${rootdir}$disk->{mp}";
310 PVE::LXC::mountpoint_mount($disk, $rootdir, $storage_cfg);
311 # add every enabled mountpoint (since we use --one-file-system)
312 # mp already starts with a / so we only need to add the dot
313 push @sources, ".$disk->{mp}";
314 }
315 $task->{snapdir} = $rootdir;
316 } elsif ($task->{mode} eq 'snapshot') {
317 # mounting the vzdump snapshots and setting $snapdir is already done,
318 # but we need to include all mountpoints here!
319 foreach my $disk (@$disks) {
320 push @sources, ".$disk->{mp}";
321 }
322 } else {
323 # the data was rsynced to a temporary location, only use '.' to avoid
324 # having mountpoints duplicated
325 push @sources, '.';
326 }
327
328 my $opts = $self->{vzdump}->{opts};
329 my $snapdir = $task->{snapdir};
330 my $tmpdir = $task->{tmpdir};
331
332 my $userns_cmd = $task->{userns_cmd};
333 my $tar = [@$userns_cmd, 'tar', 'cpf', '-', '--totals',
334 @PVE::Storage::Plugin::COMMON_TAR_FLAGS,
335 '--one-file-system', '--warning=no-file-ignored'];
336
337 # note: --remove-files does not work because we do not
338 # backup all files (filters). tar complains:
339 # Cannot rmdir: Directory not empty
340 # we disable this optimization for now
341 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
342 # push @$tar, "--remove-files"; # try to save space
343 #}
344
345 # The directory parameter can give an alternative directory as source.
346 # the second parameter gives the structure in the tar.
347 push @$tar, "--directory=$tmpdir", './etc/vzdump/pct.conf';
348 push @$tar, "./etc/vzdump/pct.fw" if $task->{fw};
349 push @$tar, "--directory=$snapdir";
350 push @$tar, '--no-anchored', '--exclude=lost+found' if $userns_cmd;
351 push @$tar, '--anchored';
352 push @$tar, map { "--exclude=.$_" } @{$self->{vzdump}->{findexcl}};
353
354 push @$tar, @sources;
355
356 my $cmd = [ $tar ];
357
358 my $bwl = $opts->{bwlimit}*1024; # bandwidth limit for cstream
359 push @$cmd, [ 'cstream', '-t', $bwl ] if $opts->{bwlimit};
360 push @$cmd, [ split(/\s+/, $comp) ] if $comp;
361
362 if ($opts->{stdout}) {
363 $self->cmd($cmd, output => ">&" . fileno($opts->{stdout}));
364 } else {
365 push @{$cmd->[-1]}, \(">" . PVE::Tools::shellquote($filename));
366 $self->cmd($cmd);
367 }
368 }
369
370 sub cleanup {
371 my ($self, $task, $vmid) = @_;
372
373 my $conf = PVE::LXC::Config->load_config($vmid);
374
375 if ($task->{mode} ne 'suspend') {
376 my $rootdir = $default_mount_point;
377 my $disks = $task->{disks};
378 foreach my $disk (reverse @$disks) {
379 PVE::Tools::run_command(['umount', '-l', '-d', $disk->{dir}]) if $disk->{dir};
380 }
381 }
382
383 if ($task->{cleanup}->{remove_snapshot}) {
384 $self->loginfo("remove vzdump snapshot");
385 PVE::LXC::Config->snapshot_delete($vmid, 'vzdump', 0);
386 }
387 }
388
389 1;