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