]> git.proxmox.com Git - pve-container.git/blame - src/PVE/VZDump/LXC.pm
vzdump: log the warning about NFS properly
[pve-container.git] / src / PVE / VZDump / LXC.pm
CommitLineData
d14a9a1b
DM
1package PVE::VZDump::LXC;
2
3use strict;
4use warnings;
5use File::Path;
6use File::Basename;
7use PVE::INotify;
8use PVE::Cluster qw(cfs_read_file);
9use PVE::Storage;
10use PVE::VZDump;
11use PVE::LXC;
86ee2a21 12use PVE::LXC::Config;
514b5f82 13use PVE::Tools;
d14a9a1b
DM
14
15use base qw (PVE::VZDump::Plugin);
16
4ca61ce8
DM
17my $default_mount_point = "/mnt/vzsnap0";
18
d14a9a1b 19my $rsync_vm = sub {
50ee6ffe 20 my ($self, $task, $to, $text, $first) = @_;
d14a9a1b 21
59a999af
WB
22 my $disks = $task->{disks};
23 my $from = $disks->[0]->{dir} . '/';
d14a9a1b
DM
24 $self->loginfo ("starting $text sync $from to $to");
25
d14a9a1b
DM
26 my $opts = $self->{vzdump}->{opts};
27
9c9a3b8c
WB
28 my @xattr = $task->{no_xattrs} ? () : ('-X', '-A');
29
30 my $rsync = ['rsync', '--stats', @xattr, '--numeric-ids',
50ee6ffe
WB
31 '-aH', '--delete', '--no-whole-file',
32 ($first ? '--sparse' : '--inplace'),
bd8e1739
WB
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}};
d14a9a1b 37
59a999af 38 my $starttime = time();
5a8a3087
WB
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) {
bd8e1739 49 push @$rsync, "$from/.$disk->{mp}";
5a8a3087 50 }
bd8e1739 51 $self->cmd([@$rsync, $to]);
d14a9a1b
DM
52 my $delay = time () - $starttime;
53
54 $self->loginfo ("$text sync finished ($delay seconds)");
55};
56
57sub 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
75sub type {
76 return 'lxc';
77}
78
79sub 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
4ca61ce8 87my $check_mountpoint_empty = sub {
b739f640
DM
88 my ($mountpoint) = @_;
89
235dbdf3 90 die "mount point '$mountpoint' is not a directory\n" if ! -d $mountpoint;
4ca61ce8 91
b739f640
DM
92 PVE::Tools::dir_glob_foreach($mountpoint, qr/.*/, sub {
93 my $entry = shift;
94 return if $entry eq '.' || $entry eq '..';
235dbdf3 95 die "mount point '$mountpoint' not empty\n";
b739f640 96 });
d14a9a1b
DM
97};
98
99sub prepare {
100 my ($self, $task, $vmid, $mode) = @_;
101
67afe46e 102 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::Config->load_config($vmid);
57ed5ed0 103 my $storage_cfg = $self->{storecfg};
d14a9a1b 104
05812a36
WL
105 $self->loginfo("CT Name: $conf->{hostname}")
106 if defined($conf->{hostname});
107
d14a9a1b
DM
108 my $running = PVE::LXC::check_running($vmid);
109
2e57a9f6
WB
110 my $disks = $task->{disks} = [];
111 my $exclude_dirs = $task->{exclude_dirs} = [];
d14a9a1b 112
27916659 113 $task->{hostname} = $conf->{'hostname'} || "CT$vmid";
d14a9a1b 114
01dce99b
WB
115 my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
116 $task->{userns_cmd} = PVE::LXC::userns_command($id_map);
117
5040d81c 118 my $volids = $task->{volids} = [];
d250604f 119 PVE::LXC::Config->foreach_mountpoint($conf, sub {
2e57a9f6
WB
120 my ($name, $data) = @_;
121 my $volid = $data->{volume};
122 my $mount = $data->{mp};
7c921c80 123 my $type = $data->{type};
d14a9a1b 124
ca8aaa5a 125 return if !$volid || !$mount;
2e57a9f6 126
1a8269bc 127 if (!PVE::LXC::Config->mountpoint_backup_enabled($name, $data)) {
2e57a9f6 128 push @$exclude_dirs, $mount;
235dbdf3 129 $self->loginfo("excluding $type mount point $name ('$mount') from backup");
2e57a9f6
WB
130 return;
131 }
132
133 push @$disks, $data;
5040d81c 134 push @$volids, $volid
b5990246 135 if $type eq 'volume';
2e57a9f6 136 });
d14a9a1b 137
d14a9a1b 138 if ($mode eq 'snapshot') {
4518000b 139 if (!PVE::LXC::Config->has_feature('snapshot', $conf, $storage_cfg, undef, undef, 1)) {
c31ad455 140 die "mode failure - some volumes do not support snapshots\n";
4ca61ce8 141 }
d14a9a1b 142
2d3f23be 143
4ca61ce8
DM
144 if ($conf->{snapshots} && $conf->{snapshots}->{vzdump}) {
145 $self->loginfo("found old vzdump snapshot (force removal)");
91458f71
FG
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 });
4ca61ce8 151 }
d14a9a1b 152
22a91261
WB
153 my $rootdir = $default_mount_point;
154 mkpath $rootdir;
155 &$check_mountpoint_empty($rootdir);
d14a9a1b 156
c31ad455 157 # set snapshot_count (freezes CT if snapshot_count > 1)
5040d81c 158 $task->{snapshot_count} = scalar(@$volids);
f5313774 159 } elsif ($mode eq 'stop') {
22a91261
WB
160 my $rootdir = $default_mount_point;
161 mkpath $rootdir;
162 &$check_mountpoint_empty($rootdir);
5040d81c 163 PVE::Storage::activate_volumes($storage_cfg, $volids);
f5313774 164 } elsif ($mode eq 'suspend') {
632eca5a 165 my $pid = PVE::LXC::find_lxc_pid($vmid);
2e57a9f6
WB
166 foreach my $disk (@$disks) {
167 $disk->{dir} = "/proc/$pid/root$disk->{mp}";
168 }
f5313774
DM
169 $task->{snapdir} = $task->{tmpdir};
170 } else {
2d3f23be 171 unlock_vm($self, $vmid);
f5313774 172 die "unknown mode '$mode'\n"; # should not happen
d14a9a1b 173 }
b6c491ee
WB
174
175 if ($mode ne 'suspend') {
c31ad455 176 # If we perform mount operations, let's unshare the mount namespace
b6c491ee
WB
177 # to not influence the running host.
178 PVE::Tools::unshare(PVE::Tools::CLONE_NEWNS);
a78425d7 179 PVE::Tools::run_command(['mount', '--make-rslave', '/']);
b6c491ee 180 }
d14a9a1b
DM
181}
182
183sub lock_vm {
184 my ($self, $vmid) = @_;
2d3f23be 185
ad408fe1 186 PVE::LXC::Config->set_lock($vmid, 'backup');
d14a9a1b
DM
187}
188
189sub unlock_vm {
190 my ($self, $vmid) = @_;
2d3f23be 191
ad408fe1 192 PVE::LXC::Config->remove_lock($vmid, 'backup')
d14a9a1b
DM
193}
194
4ca61ce8
DM
195sub snapshot {
196 my ($self, $task, $vmid) = @_;
197
c31ad455 198 $self->loginfo("create storage snapshot 'vzdump'");
4ca61ce8
DM
199
200 # todo: freeze/unfreeze if we have more than one volid
91458f71
FG
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 });
4ca61ce8
DM
206 $task->{cleanup}->{remove_snapshot} = 1;
207
208 # reload config
67afe46e 209 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::Config->load_config($vmid);
c31ad455 210 die "unable to read vzdump snapshot config - internal error"
4ca61ce8
DM
211 if !($conf->{snapshots} && $conf->{snapshots}->{vzdump});
212
2e57a9f6 213 my $disks = $task->{disks};
5040d81c 214 my $volids = $task->{volids};
4ca61ce8 215
22a91261 216 my $rootdir = $default_mount_point;
2e57a9f6
WB
217 my $storage_cfg = $self->{storecfg};
218
5040d81c 219 PVE::Storage::activate_volumes($storage_cfg, $volids, 'vzdump');
2e57a9f6
WB
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;
4ca61ce8
DM
226}
227
d14a9a1b
DM
228sub copy_data_phase1 {
229 my ($self, $task) = @_;
230
9c9a3b8c
WB
231 if (my $mntinfo = PVE::VZDump::get_mount_info($task->{snapdir})) {
232 if ($mntinfo->{fstype} =~ /^nfs4?/) {
b67c14c0
WB
233 $self->loginfo(
234 "temporary directory is on NFS, disabling xattr and acl"
235 ." support, consider configuring a local tmpdir via"
236 ." /etc/vzdump.conf\n");
9c9a3b8c
WB
237 $task->{no_xattrs} = 1;
238 }
239 }
240
50ee6ffe 241 $self->$rsync_vm($task, $task->{snapdir}, "first", 1);
d14a9a1b
DM
242}
243
244sub copy_data_phase2 {
245 my ($self, $task) = @_;
246
50ee6ffe 247 $self->$rsync_vm($task, $task->{snapdir}, "final", 0);
d14a9a1b
DM
248}
249
250sub stop_vm {
251 my ($self, $task, $vmid) = @_;
252
f90f7e53
TL
253 my $opts = $self->{vzdump}->{opts};
254 my $timeout = $opts->{stopwait} * 60;
255
b1bad293 256 PVE::LXC::vm_stop($vmid, 0, $timeout);
d14a9a1b
DM
257}
258
259sub start_vm {
260 my ($self, $task, $vmid) = @_;
261
be8ec1b0 262 $self->cmd(['systemctl', 'start', "pve-container\@$vmid"]);
d14a9a1b
DM
263}
264
265sub suspend_vm {
266 my ($self, $task, $vmid) = @_;
267
268 $self->cmd ("lxc-freeze -n $vmid");
269}
270
271sub resume_vm {
272 my ($self, $task, $vmid) = @_;
273
274 $self->cmd ("lxc-unfreeze -n $vmid");
275}
276
277sub assemble {
278 my ($self, $task, $vmid) = @_;
279
cbd6753d 280 my $tmpdir = $task->{tmpdir};
d14a9a1b 281
cbd6753d 282 mkpath "$tmpdir/etc/vzdump/";
514b5f82 283
67afe46e 284 my $conf = PVE::LXC::Config->load_config($vmid);
48314d4e 285 delete $conf->{lock};
514b5f82 286 delete $conf->{snapshots};
f626ac9d 287 delete $conf->{parent};
514b5f82 288
1b4cf758 289 PVE::Tools::file_set_contents("$tmpdir/etc/vzdump/pct.conf", PVE::LXC::Config::write_pct_config("/lxc/$vmid.conf", $conf));
bf040874
WL
290
291 my $firewall ="/etc/pve/firewall/$vmid.fw";
292 if (-e $firewall) {
293 PVE::Tools::file_copy($firewall, "$tmpdir/etc/vzdump/pct.fw");
294 $task->{fw} = 1;
295 }
d14a9a1b
DM
296}
297
298sub archive {
299 my ($self, $task, $vmid, $filename, $comp) = @_;
cbd6753d 300
b7ec90ed 301 my $disks = $task->{disks};
5a8a3087 302 my @sources;
b7ec90ed 303
459fd4d2 304 if ($task->{mode} eq 'stop') {
22a91261 305 my $rootdir = $default_mount_point;
2e57a9f6
WB
306 my $storage_cfg = $self->{storecfg};
307 foreach my $disk (@$disks) {
308 $disk->{dir} = "${rootdir}$disk->{mp}";
309 PVE::LXC::mountpoint_mount($disk, $rootdir, $storage_cfg);
5a8a3087
WB
310 # add every enabled mountpoint (since we use --one-file-system)
311 # mp already starts with a / so we only need to add the dot
312 push @sources, ".$disk->{mp}";
2e57a9f6
WB
313 }
314 $task->{snapdir} = $rootdir;
5040d81c
FG
315 } elsif ($task->{mode} eq 'snapshot') {
316 # mounting the vzdump snapshots and setting $snapdir is already done,
317 # but we need to include all mountpoints here!
318 foreach my $disk (@$disks) {
319 push @sources, ".$disk->{mp}";
320 }
5a8a3087
WB
321 } else {
322 # the data was rsynced to a temporary location, only use '.' to avoid
323 # having mountpoints duplicated
324 push @sources, '.';
459fd4d2
DM
325 }
326
d14a9a1b 327 my $opts = $self->{vzdump}->{opts};
d14a9a1b 328 my $snapdir = $task->{snapdir};
cbd6753d 329 my $tmpdir = $task->{tmpdir};
d14a9a1b 330
01dce99b
WB
331 my $userns_cmd = $task->{userns_cmd};
332 my $tar = [@$userns_cmd, 'tar', 'cpf', '-', '--totals',
5fa038ab 333 @PVE::Storage::Plugin::COMMON_TAR_FLAGS,
d5cb7040 334 '--one-file-system', '--warning=no-file-ignored'];
d14a9a1b
DM
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
c31ad455 339 # we disable this optimization for now
d14a9a1b 340 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
bd734680 341 # push @$tar, "--remove-files"; # try to save space
d14a9a1b
DM
342 #}
343
c31ad455 344 # The directory parameter can give an alternative directory as source.
cbd6753d 345 # the second parameter gives the structure in the tar.
bd734680 346 push @$tar, "--directory=$tmpdir", './etc/vzdump/pct.conf';
bf040874 347 push @$tar, "./etc/vzdump/pct.fw" if $task->{fw};
bd734680 348 push @$tar, "--directory=$snapdir";
e87a47ff 349 push @$tar, '--no-anchored', '--exclude=lost+found' if $userns_cmd;
4953cff8 350 push @$tar, '--anchored';
bd734680 351 push @$tar, map { "--exclude=.$_" } @{$self->{vzdump}->{findexcl}};
332b1243 352
5a8a3087 353 push @$tar, @sources;
bd734680
WB
354
355 my $cmd = [ $tar ];
cbd6753d 356
d14a9a1b 357 my $bwl = $opts->{bwlimit}*1024; # bandwidth limit for cstream
bd734680 358 push @$cmd, [ 'cstream', '-t', $bwl ] if $opts->{bwlimit};
3889966a 359 push @$cmd, [ split(/\s+/, $comp) ] if $comp;
d14a9a1b 360
d14a9a1b 361 if ($opts->{stdout}) {
f01c9293 362 $self->cmd($cmd, output => ">&" . fileno($opts->{stdout}));
d14a9a1b 363 } else {
bd734680 364 push @{$cmd->[-1]}, \(">" . PVE::Tools::shellquote($filename));
f01c9293 365 $self->cmd($cmd);
d14a9a1b
DM
366 }
367}
368
369sub cleanup {
370 my ($self, $task, $vmid) = @_;
371
67afe46e 372 my $conf = PVE::LXC::Config->load_config($vmid);
d14a9a1b 373
0c44de7b 374 if ($task->{mode} ne 'suspend') {
96d20be2
DM
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 }
2e57a9f6 380 }
b739f640 381
4ca61ce8
DM
382 if ($task->{cleanup}->{remove_snapshot}) {
383 $self->loginfo("remove vzdump snapshot");
4518000b 384 PVE::LXC::Config->snapshot_delete($vmid, 'vzdump', 0);
d14a9a1b 385 }
d14a9a1b
DM
386}
387
3881;