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