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