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