]> git.proxmox.com Git - pve-container.git/blame - src/PVE/VZDump/LXC.pm
skip pending changes while taking backup
[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);
4c98d66c
FG
117 $task->{rootuid} = $rootuid;
118 $task->{rootgid} = $rootgid;
119
01dce99b 120
5040d81c 121 my $volids = $task->{volids} = [];
d250604f 122 PVE::LXC::Config->foreach_mountpoint($conf, sub {
2e57a9f6
WB
123 my ($name, $data) = @_;
124 my $volid = $data->{volume};
125 my $mount = $data->{mp};
7c921c80 126 my $type = $data->{type};
d14a9a1b 127
ca8aaa5a 128 return if !$volid || !$mount;
2e57a9f6 129
1a8269bc 130 if (!PVE::LXC::Config->mountpoint_backup_enabled($name, $data)) {
2e57a9f6 131 push @$exclude_dirs, $mount;
235dbdf3 132 $self->loginfo("excluding $type mount point $name ('$mount') from backup");
2e57a9f6
WB
133 return;
134 }
135
136 push @$disks, $data;
5040d81c 137 push @$volids, $volid
b5990246 138 if $type eq 'volume';
2e57a9f6 139 });
d14a9a1b 140
d14a9a1b 141 if ($mode eq 'snapshot') {
4518000b 142 if (!PVE::LXC::Config->has_feature('snapshot', $conf, $storage_cfg, undef, undef, 1)) {
c31ad455 143 die "mode failure - some volumes do not support snapshots\n";
4ca61ce8 144 }
d14a9a1b 145
2d3f23be 146
4ca61ce8
DM
147 if ($conf->{snapshots} && $conf->{snapshots}->{vzdump}) {
148 $self->loginfo("found old vzdump snapshot (force removal)");
91458f71
FG
149 PVE::LXC::Config->lock_config($vmid, sub {
150 $self->unlock_vm($vmid);
151 PVE::LXC::Config->snapshot_delete($vmid, 'vzdump', 1);
152 $self->lock_vm($vmid);
153 });
4ca61ce8 154 }
d14a9a1b 155
22a91261
WB
156 my $rootdir = $default_mount_point;
157 mkpath $rootdir;
158 &$check_mountpoint_empty($rootdir);
d14a9a1b 159
c31ad455 160 # set snapshot_count (freezes CT if snapshot_count > 1)
5040d81c 161 $task->{snapshot_count} = scalar(@$volids);
f5313774 162 } elsif ($mode eq 'stop') {
22a91261
WB
163 my $rootdir = $default_mount_point;
164 mkpath $rootdir;
165 &$check_mountpoint_empty($rootdir);
5040d81c 166 PVE::Storage::activate_volumes($storage_cfg, $volids);
f5313774 167 } elsif ($mode eq 'suspend') {
632eca5a 168 my $pid = PVE::LXC::find_lxc_pid($vmid);
2e57a9f6
WB
169 foreach my $disk (@$disks) {
170 $disk->{dir} = "/proc/$pid/root$disk->{mp}";
171 }
f5313774
DM
172 $task->{snapdir} = $task->{tmpdir};
173 } else {
2d3f23be 174 unlock_vm($self, $vmid);
f5313774 175 die "unknown mode '$mode'\n"; # should not happen
d14a9a1b 176 }
b6c491ee
WB
177
178 if ($mode ne 'suspend') {
c31ad455 179 # If we perform mount operations, let's unshare the mount namespace
b6c491ee
WB
180 # to not influence the running host.
181 PVE::Tools::unshare(PVE::Tools::CLONE_NEWNS);
a78425d7 182 PVE::Tools::run_command(['mount', '--make-rslave', '/']);
b6c491ee 183 }
d14a9a1b
DM
184}
185
186sub lock_vm {
187 my ($self, $vmid) = @_;
2d3f23be 188
ad408fe1 189 PVE::LXC::Config->set_lock($vmid, 'backup');
d14a9a1b
DM
190}
191
192sub unlock_vm {
193 my ($self, $vmid) = @_;
2d3f23be 194
ad408fe1 195 PVE::LXC::Config->remove_lock($vmid, 'backup')
d14a9a1b
DM
196}
197
4ca61ce8
DM
198sub snapshot {
199 my ($self, $task, $vmid) = @_;
200
c31ad455 201 $self->loginfo("create storage snapshot 'vzdump'");
4ca61ce8
DM
202
203 # todo: freeze/unfreeze if we have more than one volid
91458f71
FG
204 PVE::LXC::Config->lock_config($vmid, sub {
205 $self->unlock_vm($vmid);
206 PVE::LXC::Config->snapshot_create($vmid, 'vzdump', 0, "vzdump backup snapshot");
207 $self->lock_vm($vmid);
208 });
4ca61ce8
DM
209 $task->{cleanup}->{remove_snapshot} = 1;
210
211 # reload config
67afe46e 212 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::Config->load_config($vmid);
c31ad455 213 die "unable to read vzdump snapshot config - internal error"
4ca61ce8
DM
214 if !($conf->{snapshots} && $conf->{snapshots}->{vzdump});
215
2e57a9f6 216 my $disks = $task->{disks};
5040d81c 217 my $volids = $task->{volids};
4ca61ce8 218
22a91261 219 my $rootdir = $default_mount_point;
2e57a9f6
WB
220 my $storage_cfg = $self->{storecfg};
221
5040d81c 222 PVE::Storage::activate_volumes($storage_cfg, $volids, 'vzdump');
2e57a9f6
WB
223 foreach my $disk (@$disks) {
224 $disk->{dir} = "${rootdir}$disk->{mp}";
4c98d66c 225 PVE::LXC::mountpoint_mount($disk, $rootdir, $storage_cfg, 'vzdump', $task->{rootuid}, $task->{rootgid});
2e57a9f6
WB
226 }
227
228 $task->{snapdir} = $rootdir;
4ca61ce8
DM
229}
230
d14a9a1b
DM
231sub copy_data_phase1 {
232 my ($self, $task) = @_;
233
9c9a3b8c
WB
234 if (my $mntinfo = PVE::VZDump::get_mount_info($task->{snapdir})) {
235 if ($mntinfo->{fstype} =~ /^nfs4?/) {
b67c14c0
WB
236 $self->loginfo(
237 "temporary directory is on NFS, disabling xattr and acl"
238 ." support, consider configuring a local tmpdir via"
239 ." /etc/vzdump.conf\n");
9c9a3b8c
WB
240 $task->{no_xattrs} = 1;
241 }
242 }
243
50ee6ffe 244 $self->$rsync_vm($task, $task->{snapdir}, "first", 1);
d14a9a1b
DM
245}
246
247sub copy_data_phase2 {
248 my ($self, $task) = @_;
249
50ee6ffe 250 $self->$rsync_vm($task, $task->{snapdir}, "final", 0);
d14a9a1b
DM
251}
252
253sub stop_vm {
254 my ($self, $task, $vmid) = @_;
255
f90f7e53
TL
256 my $opts = $self->{vzdump}->{opts};
257 my $timeout = $opts->{stopwait} * 60;
258
b1bad293 259 PVE::LXC::vm_stop($vmid, 0, $timeout);
d14a9a1b
DM
260}
261
262sub start_vm {
263 my ($self, $task, $vmid) = @_;
264
be8ec1b0 265 $self->cmd(['systemctl', 'start', "pve-container\@$vmid"]);
d14a9a1b
DM
266}
267
268sub suspend_vm {
269 my ($self, $task, $vmid) = @_;
270
271 $self->cmd ("lxc-freeze -n $vmid");
272}
273
274sub resume_vm {
275 my ($self, $task, $vmid) = @_;
276
277 $self->cmd ("lxc-unfreeze -n $vmid");
278}
279
280sub assemble {
281 my ($self, $task, $vmid) = @_;
282
cbd6753d 283 my $tmpdir = $task->{tmpdir};
d14a9a1b 284
cbd6753d 285 mkpath "$tmpdir/etc/vzdump/";
514b5f82 286
67afe46e 287 my $conf = PVE::LXC::Config->load_config($vmid);
48314d4e 288 delete $conf->{lock};
514b5f82 289 delete $conf->{snapshots};
f626ac9d 290 delete $conf->{parent};
53fafd3f 291 delete $conf->{pending};
514b5f82 292
1b4cf758 293 PVE::Tools::file_set_contents("$tmpdir/etc/vzdump/pct.conf", PVE::LXC::Config::write_pct_config("/lxc/$vmid.conf", $conf));
bf040874
WL
294
295 my $firewall ="/etc/pve/firewall/$vmid.fw";
d1671a80 296 my $fwconftmp = "$tmpdir/etc/vzdump/pct.fw";
bf040874 297 if (-e $firewall) {
d1671a80
FG
298 PVE::Tools::file_copy($firewall, $fwconftmp);
299 } else {
300 PVE::Tools::file_set_contents($fwconftmp, '');
bf040874 301 }
d1671a80 302 $task->{fw} = 1;
d14a9a1b
DM
303}
304
305sub archive {
306 my ($self, $task, $vmid, $filename, $comp) = @_;
cbd6753d 307
b7ec90ed 308 my $disks = $task->{disks};
5a8a3087 309 my @sources;
b7ec90ed 310
459fd4d2 311 if ($task->{mode} eq 'stop') {
22a91261 312 my $rootdir = $default_mount_point;
2e57a9f6
WB
313 my $storage_cfg = $self->{storecfg};
314 foreach my $disk (@$disks) {
315 $disk->{dir} = "${rootdir}$disk->{mp}";
4c98d66c 316 PVE::LXC::mountpoint_mount($disk, $rootdir, $storage_cfg, undef, $task->{rootuid}, $task->{rootgid});
5a8a3087
WB
317 # add every enabled mountpoint (since we use --one-file-system)
318 # mp already starts with a / so we only need to add the dot
319 push @sources, ".$disk->{mp}";
2e57a9f6
WB
320 }
321 $task->{snapdir} = $rootdir;
5040d81c
FG
322 } elsif ($task->{mode} eq 'snapshot') {
323 # mounting the vzdump snapshots and setting $snapdir is already done,
324 # but we need to include all mountpoints here!
325 foreach my $disk (@$disks) {
326 push @sources, ".$disk->{mp}";
327 }
5a8a3087
WB
328 } else {
329 # the data was rsynced to a temporary location, only use '.' to avoid
330 # having mountpoints duplicated
331 push @sources, '.';
459fd4d2
DM
332 }
333
d14a9a1b 334 my $opts = $self->{vzdump}->{opts};
d14a9a1b 335 my $snapdir = $task->{snapdir};
cbd6753d 336 my $tmpdir = $task->{tmpdir};
d14a9a1b 337
01dce99b
WB
338 my $userns_cmd = $task->{userns_cmd};
339 my $tar = [@$userns_cmd, 'tar', 'cpf', '-', '--totals',
5fa038ab 340 @PVE::Storage::Plugin::COMMON_TAR_FLAGS,
d5cb7040 341 '--one-file-system', '--warning=no-file-ignored'];
d14a9a1b
DM
342
343 # note: --remove-files does not work because we do not
344 # backup all files (filters). tar complains:
345 # Cannot rmdir: Directory not empty
c31ad455 346 # we disable this optimization for now
d14a9a1b 347 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
bd734680 348 # push @$tar, "--remove-files"; # try to save space
d14a9a1b
DM
349 #}
350
c31ad455 351 # The directory parameter can give an alternative directory as source.
cbd6753d 352 # the second parameter gives the structure in the tar.
bd734680 353 push @$tar, "--directory=$tmpdir", './etc/vzdump/pct.conf';
bf040874 354 push @$tar, "./etc/vzdump/pct.fw" if $task->{fw};
bd734680 355 push @$tar, "--directory=$snapdir";
e87a47ff 356 push @$tar, '--no-anchored', '--exclude=lost+found' if $userns_cmd;
4953cff8 357 push @$tar, '--anchored';
bd734680 358 push @$tar, map { "--exclude=.$_" } @{$self->{vzdump}->{findexcl}};
332b1243 359
5a8a3087 360 push @$tar, @sources;
bd734680
WB
361
362 my $cmd = [ $tar ];
cbd6753d 363
d14a9a1b 364 my $bwl = $opts->{bwlimit}*1024; # bandwidth limit for cstream
bd734680 365 push @$cmd, [ 'cstream', '-t', $bwl ] if $opts->{bwlimit};
3889966a 366 push @$cmd, [ split(/\s+/, $comp) ] if $comp;
d14a9a1b 367
d14a9a1b 368 if ($opts->{stdout}) {
f01c9293 369 $self->cmd($cmd, output => ">&" . fileno($opts->{stdout}));
d14a9a1b 370 } else {
bd734680 371 push @{$cmd->[-1]}, \(">" . PVE::Tools::shellquote($filename));
f01c9293 372 $self->cmd($cmd);
d14a9a1b
DM
373 }
374}
375
376sub cleanup {
377 my ($self, $task, $vmid) = @_;
378
67afe46e 379 my $conf = PVE::LXC::Config->load_config($vmid);
d14a9a1b 380
0c44de7b 381 if ($task->{mode} ne 'suspend') {
96d20be2
DM
382 my $rootdir = $default_mount_point;
383 my $disks = $task->{disks};
384 foreach my $disk (reverse @$disks) {
385 PVE::Tools::run_command(['umount', '-l', '-d', $disk->{dir}]) if $disk->{dir};
386 }
2e57a9f6 387 }
b739f640 388
4ca61ce8
DM
389 if ($task->{cleanup}->{remove_snapshot}) {
390 $self->loginfo("remove vzdump snapshot");
4518000b 391 PVE::LXC::Config->snapshot_delete($vmid, 'vzdump', 0);
d14a9a1b 392 }
d14a9a1b
DM
393}
394
3951;