]> git.proxmox.com Git - pve-container.git/blame - src/PVE/VZDump/LXC.pm
don't let lxc handle container reboots directly
[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 {
59a999af 20 my ($self, $task, $to, $text) = @_;
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',
bd8e1739
WB
31 '-aH', '--delete', '--no-whole-file', '--inplace',
32 '--one-file-system', '--relative'];
33 push @$rsync, "--bwlimit=$opts->{bwlimit}" if $opts->{bwlimit};
34 push @$rsync, map { "--exclude=$_" } @{$self->{vzdump}->{findexcl}};
35 push @$rsync, map { "--exclude=$_" } @{$task->{exclude_dirs}};
d14a9a1b 36
59a999af 37 my $starttime = time();
5a8a3087
WB
38 # See the rsync(1) manpage for --relative in conjunction with /./ in paths.
39 # This is the only way to have exclude-dirs work together with the
40 # --one-file-system option.
41 # This way we can pass multiple source paths and tell rsync which directory
42 # they're supposed to be relative to.
43 # Otherwise with eg. using multiple rsync commands means the --exclude
44 # directives need to be modified for every command as they are meant to be
45 # relative to the rootdir, while rsync treats them as relative to the
46 # source dir.
47 foreach my $disk (@$disks) {
bd8e1739 48 push @$rsync, "$from/.$disk->{mp}";
5a8a3087 49 }
bd8e1739 50 $self->cmd([@$rsync, $to]);
d14a9a1b
DM
51 my $delay = time () - $starttime;
52
53 $self->loginfo ("$text sync finished ($delay seconds)");
54};
55
56sub new {
57 my ($class, $vzdump) = @_;
58
59 PVE::VZDump::check_bin('lxc-stop');
60 PVE::VZDump::check_bin('lxc-start');
61 PVE::VZDump::check_bin('lxc-freeze');
62 PVE::VZDump::check_bin('lxc-unfreeze');
63
64 my $self = bless {};
65
66 $self->{vzdump} = $vzdump;
67 $self->{storecfg} = PVE::Storage::config();
68
69 $self->{vmlist} = PVE::LXC::config_list();
70
71 return $self;
72}
73
74sub type {
75 return 'lxc';
76}
77
78sub vm_status {
79 my ($self, $vmid) = @_;
80
81 my $running = PVE::LXC::check_running($vmid) ? 1 : 0;
82
83 return wantarray ? ($running, $running ? 'running' : 'stopped') : $running;
84}
85
4ca61ce8 86my $check_mountpoint_empty = sub {
b739f640
DM
87 my ($mountpoint) = @_;
88
4ca61ce8
DM
89 die "mountpoint '$mountpoint' is not a directory\n" if ! -d $mountpoint;
90
b739f640
DM
91 PVE::Tools::dir_glob_foreach($mountpoint, qr/.*/, sub {
92 my $entry = shift;
93 return if $entry eq '.' || $entry eq '..';
4ca61ce8 94 die "mountpoint '$mountpoint' not empty\n";
b739f640 95 });
d14a9a1b
DM
96};
97
98sub prepare {
99 my ($self, $task, $vmid, $mode) = @_;
100
67afe46e 101 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::Config->load_config($vmid);
57ed5ed0 102 my $storage_cfg = $self->{storecfg};
d14a9a1b 103
05812a36
WL
104 $self->loginfo("CT Name: $conf->{hostname}")
105 if defined($conf->{hostname});
106
d14a9a1b
DM
107 my $running = PVE::LXC::check_running($vmid);
108
2e57a9f6
WB
109 my $disks = $task->{disks} = [];
110 my $exclude_dirs = $task->{exclude_dirs} = [];
d14a9a1b 111
27916659 112 $task->{hostname} = $conf->{'hostname'} || "CT$vmid";
d14a9a1b 113
01dce99b
WB
114 my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
115 $task->{userns_cmd} = PVE::LXC::userns_command($id_map);
116
5040d81c 117 my $volids = $task->{volids} = [];
d250604f 118 PVE::LXC::Config->foreach_mountpoint($conf, sub {
2e57a9f6
WB
119 my ($name, $data) = @_;
120 my $volid = $data->{volume};
121 my $mount = $data->{mp};
7c921c80 122 my $type = $data->{type};
d14a9a1b 123
ca8aaa5a 124 return if !$volid || !$mount;
2e57a9f6 125
1a8269bc 126 if (!PVE::LXC::Config->mountpoint_backup_enabled($name, $data)) {
2e57a9f6 127 push @$exclude_dirs, $mount;
f59c9670 128 $self->loginfo("excluding $type mountpoint $name ('$mount') from backup");
2e57a9f6
WB
129 return;
130 }
131
132 push @$disks, $data;
5040d81c 133 push @$volids, $volid
b5990246 134 if $type eq 'volume';
2e57a9f6 135 });
d14a9a1b 136
d14a9a1b 137 if ($mode eq 'snapshot') {
4518000b 138 if (!PVE::LXC::Config->has_feature('snapshot', $conf, $storage_cfg, undef, undef, 1)) {
c31ad455 139 die "mode failure - some volumes do not support snapshots\n";
4ca61ce8 140 }
d14a9a1b 141
2d3f23be 142
4ca61ce8
DM
143 if ($conf->{snapshots} && $conf->{snapshots}->{vzdump}) {
144 $self->loginfo("found old vzdump snapshot (force removal)");
91458f71
FG
145 PVE::LXC::Config->lock_config($vmid, sub {
146 $self->unlock_vm($vmid);
147 PVE::LXC::Config->snapshot_delete($vmid, 'vzdump', 1);
148 $self->lock_vm($vmid);
149 });
4ca61ce8 150 }
d14a9a1b 151
22a91261
WB
152 my $rootdir = $default_mount_point;
153 mkpath $rootdir;
154 &$check_mountpoint_empty($rootdir);
d14a9a1b 155
c31ad455 156 # set snapshot_count (freezes CT if snapshot_count > 1)
5040d81c 157 $task->{snapshot_count} = scalar(@$volids);
f5313774 158 } elsif ($mode eq 'stop') {
22a91261
WB
159 my $rootdir = $default_mount_point;
160 mkpath $rootdir;
161 &$check_mountpoint_empty($rootdir);
5040d81c 162 PVE::Storage::activate_volumes($storage_cfg, $volids);
f5313774 163 } elsif ($mode eq 'suspend') {
632eca5a 164 my $pid = PVE::LXC::find_lxc_pid($vmid);
2e57a9f6
WB
165 foreach my $disk (@$disks) {
166 $disk->{dir} = "/proc/$pid/root$disk->{mp}";
167 }
f5313774
DM
168 $task->{snapdir} = $task->{tmpdir};
169 } else {
2d3f23be 170 unlock_vm($self, $vmid);
f5313774 171 die "unknown mode '$mode'\n"; # should not happen
d14a9a1b 172 }
b6c491ee
WB
173
174 if ($mode ne 'suspend') {
c31ad455 175 # If we perform mount operations, let's unshare the mount namespace
b6c491ee
WB
176 # to not influence the running host.
177 PVE::Tools::unshare(PVE::Tools::CLONE_NEWNS);
178 PVE::Tools::run_command(['mount', '--make-rprivate', '/']);
179 }
d14a9a1b
DM
180}
181
182sub lock_vm {
183 my ($self, $vmid) = @_;
2d3f23be 184
ad408fe1 185 PVE::LXC::Config->set_lock($vmid, 'backup');
d14a9a1b
DM
186}
187
188sub unlock_vm {
189 my ($self, $vmid) = @_;
2d3f23be 190
ad408fe1 191 PVE::LXC::Config->remove_lock($vmid, 'backup')
d14a9a1b
DM
192}
193
4ca61ce8
DM
194sub snapshot {
195 my ($self, $task, $vmid) = @_;
196
c31ad455 197 $self->loginfo("create storage snapshot 'vzdump'");
4ca61ce8
DM
198
199 # todo: freeze/unfreeze if we have more than one volid
91458f71
FG
200 PVE::LXC::Config->lock_config($vmid, sub {
201 $self->unlock_vm($vmid);
202 PVE::LXC::Config->snapshot_create($vmid, 'vzdump', 0, "vzdump backup snapshot");
203 $self->lock_vm($vmid);
204 });
4ca61ce8
DM
205 $task->{cleanup}->{remove_snapshot} = 1;
206
207 # reload config
67afe46e 208 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::Config->load_config($vmid);
c31ad455 209 die "unable to read vzdump snapshot config - internal error"
4ca61ce8
DM
210 if !($conf->{snapshots} && $conf->{snapshots}->{vzdump});
211
2e57a9f6 212 my $disks = $task->{disks};
5040d81c 213 my $volids = $task->{volids};
4ca61ce8 214
22a91261 215 my $rootdir = $default_mount_point;
2e57a9f6
WB
216 my $storage_cfg = $self->{storecfg};
217
5040d81c 218 PVE::Storage::activate_volumes($storage_cfg, $volids, 'vzdump');
2e57a9f6
WB
219 foreach my $disk (@$disks) {
220 $disk->{dir} = "${rootdir}$disk->{mp}";
221 PVE::LXC::mountpoint_mount($disk, $rootdir, $storage_cfg, 'vzdump');
222 }
223
224 $task->{snapdir} = $rootdir;
4ca61ce8
DM
225}
226
d14a9a1b
DM
227sub copy_data_phase1 {
228 my ($self, $task) = @_;
229
9c9a3b8c
WB
230 if (my $mntinfo = PVE::VZDump::get_mount_info($task->{snapdir})) {
231 if ($mntinfo->{fstype} =~ /^nfs4?/) {
232 warn "temporary directory is on NFS, disabling xattr and acl support"
233 . ", consider configuring a local tmpdir via /etc/vzdump.conf\n";
234 $task->{no_xattrs} = 1;
235 }
236 }
237
59a999af 238 $self->$rsync_vm($task, $task->{snapdir}, "first");
d14a9a1b
DM
239}
240
241sub copy_data_phase2 {
242 my ($self, $task) = @_;
243
59a999af 244 $self->$rsync_vm($task, $task->{snapdir}, "final");
d14a9a1b
DM
245}
246
247sub stop_vm {
248 my ($self, $task, $vmid) = @_;
249
250 $self->cmd("lxc-stop -n $vmid");
c434e3b4
DM
251
252 # make sure container is stopped
253 $self->cmd("lxc-wait -n $vmid -s STOPPED");
d14a9a1b
DM
254}
255
256sub start_vm {
257 my ($self, $task, $vmid) = @_;
258
259 $self->cmd ("lxc-start -n $vmid");
260}
261
262sub suspend_vm {
263 my ($self, $task, $vmid) = @_;
264
265 $self->cmd ("lxc-freeze -n $vmid");
266}
267
268sub resume_vm {
269 my ($self, $task, $vmid) = @_;
270
271 $self->cmd ("lxc-unfreeze -n $vmid");
272}
273
274sub assemble {
275 my ($self, $task, $vmid) = @_;
276
cbd6753d 277 my $tmpdir = $task->{tmpdir};
d14a9a1b 278
cbd6753d 279 mkpath "$tmpdir/etc/vzdump/";
514b5f82 280
67afe46e 281 my $conf = PVE::LXC::Config->load_config($vmid);
48314d4e 282 delete $conf->{lock};
514b5f82 283 delete $conf->{snapshots};
f626ac9d 284 delete $conf->{parent};
514b5f82 285
1b4cf758 286 PVE::Tools::file_set_contents("$tmpdir/etc/vzdump/pct.conf", PVE::LXC::Config::write_pct_config("/lxc/$vmid.conf", $conf));
bf040874
WL
287
288 my $firewall ="/etc/pve/firewall/$vmid.fw";
289 if (-e $firewall) {
290 PVE::Tools::file_copy($firewall, "$tmpdir/etc/vzdump/pct.fw");
291 $task->{fw} = 1;
292 }
d14a9a1b
DM
293}
294
295sub archive {
296 my ($self, $task, $vmid, $filename, $comp) = @_;
cbd6753d 297
b7ec90ed 298 my $disks = $task->{disks};
5a8a3087 299 my @sources;
b7ec90ed 300
459fd4d2 301 if ($task->{mode} eq 'stop') {
22a91261 302 my $rootdir = $default_mount_point;
2e57a9f6
WB
303 my $storage_cfg = $self->{storecfg};
304 foreach my $disk (@$disks) {
305 $disk->{dir} = "${rootdir}$disk->{mp}";
306 PVE::LXC::mountpoint_mount($disk, $rootdir, $storage_cfg);
5a8a3087
WB
307 # add every enabled mountpoint (since we use --one-file-system)
308 # mp already starts with a / so we only need to add the dot
309 push @sources, ".$disk->{mp}";
2e57a9f6
WB
310 }
311 $task->{snapdir} = $rootdir;
5040d81c
FG
312 } elsif ($task->{mode} eq 'snapshot') {
313 # mounting the vzdump snapshots and setting $snapdir is already done,
314 # but we need to include all mountpoints here!
315 foreach my $disk (@$disks) {
316 push @sources, ".$disk->{mp}";
317 }
5a8a3087
WB
318 } else {
319 # the data was rsynced to a temporary location, only use '.' to avoid
320 # having mountpoints duplicated
321 push @sources, '.';
459fd4d2
DM
322 }
323
d14a9a1b 324 my $opts = $self->{vzdump}->{opts};
d14a9a1b 325 my $snapdir = $task->{snapdir};
cbd6753d 326 my $tmpdir = $task->{tmpdir};
d14a9a1b 327
01dce99b
WB
328 my $userns_cmd = $task->{userns_cmd};
329 my $tar = [@$userns_cmd, 'tar', 'cpf', '-', '--totals',
fc4e132e 330 @$PVE::LXC::COMMON_TAR_FLAGS,
d5cb7040 331 '--one-file-system', '--warning=no-file-ignored'];
d14a9a1b
DM
332
333 # note: --remove-files does not work because we do not
334 # backup all files (filters). tar complains:
335 # Cannot rmdir: Directory not empty
c31ad455 336 # we disable this optimization for now
d14a9a1b 337 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
bd734680 338 # push @$tar, "--remove-files"; # try to save space
d14a9a1b
DM
339 #}
340
c31ad455 341 # The directory parameter can give an alternative directory as source.
cbd6753d 342 # the second parameter gives the structure in the tar.
bd734680 343 push @$tar, "--directory=$tmpdir", './etc/vzdump/pct.conf';
bf040874 344 push @$tar, "./etc/vzdump/pct.fw" if $task->{fw};
bd734680 345 push @$tar, "--directory=$snapdir";
e87a47ff 346 push @$tar, '--no-anchored', '--exclude=lost+found' if $userns_cmd;
4953cff8 347 push @$tar, '--anchored';
bd734680 348 push @$tar, map { "--exclude=.$_" } @{$self->{vzdump}->{findexcl}};
332b1243 349
5a8a3087 350 push @$tar, @sources;
bd734680
WB
351
352 my $cmd = [ $tar ];
cbd6753d 353
d14a9a1b 354 my $bwl = $opts->{bwlimit}*1024; # bandwidth limit for cstream
bd734680 355 push @$cmd, [ 'cstream', '-t', $bwl ] if $opts->{bwlimit};
3889966a 356 push @$cmd, [ split(/\s+/, $comp) ] if $comp;
d14a9a1b 357
d14a9a1b 358 if ($opts->{stdout}) {
f01c9293 359 $self->cmd($cmd, output => ">&" . fileno($opts->{stdout}));
d14a9a1b 360 } else {
bd734680 361 push @{$cmd->[-1]}, \(">" . PVE::Tools::shellquote($filename));
f01c9293 362 $self->cmd($cmd);
d14a9a1b
DM
363 }
364}
365
366sub cleanup {
367 my ($self, $task, $vmid) = @_;
368
67afe46e 369 my $conf = PVE::LXC::Config->load_config($vmid);
d14a9a1b 370
0c44de7b 371 if ($task->{mode} ne 'suspend') {
96d20be2
DM
372 my $rootdir = $default_mount_point;
373 my $disks = $task->{disks};
374 foreach my $disk (reverse @$disks) {
375 PVE::Tools::run_command(['umount', '-l', '-d', $disk->{dir}]) if $disk->{dir};
376 }
2e57a9f6 377 }
b739f640 378
4ca61ce8
DM
379 if ($task->{cleanup}->{remove_snapshot}) {
380 $self->loginfo("remove vzdump snapshot");
4518000b 381 PVE::LXC::Config->snapshot_delete($vmid, 'vzdump', 0);
d14a9a1b 382 }
d14a9a1b
DM
383}
384
3851;