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