]> git.proxmox.com Git - pve-container.git/blob - src/PVE/VZDump/LXC.pm
fix previous commit (s/snapshot/suspend/)
[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 $rsync = ['rsync', '--stats', '-X', '--numeric-ids',
28 '-aH', '--delete', '--no-whole-file', '--inplace',
29 '--one-file-system', '--relative'];
30 push @$rsync, "--bwlimit=$opts->{bwlimit}" if $opts->{bwlimit};
31 push @$rsync, map { "--exclude=$_" } @{$self->{vzdump}->{findexcl}};
32 push @$rsync, map { "--exclude=$_" } @{$task->{exclude_dirs}};
33
34 my $starttime = time();
35 # See the rsync(1) manpage for --relative in conjunction with /./ in paths.
36 # This is the only way to have exclude-dirs work together with the
37 # --one-file-system option.
38 # This way we can pass multiple source paths and tell rsync which directory
39 # they're supposed to be relative to.
40 # Otherwise with eg. using multiple rsync commands means the --exclude
41 # directives need to be modified for every command as they are meant to be
42 # relative to the rootdir, while rsync treats them as relative to the
43 # source dir.
44 foreach my $disk (@$disks) {
45 push @$rsync, "$from/.$disk->{mp}";
46 }
47 $self->cmd([@$rsync, $to]);
48 my $delay = time () - $starttime;
49
50 $self->loginfo ("$text sync finished ($delay seconds)");
51 };
52
53 sub new {
54 my ($class, $vzdump) = @_;
55
56 PVE::VZDump::check_bin('lxc-stop');
57 PVE::VZDump::check_bin('lxc-start');
58 PVE::VZDump::check_bin('lxc-freeze');
59 PVE::VZDump::check_bin('lxc-unfreeze');
60
61 my $self = bless {};
62
63 $self->{vzdump} = $vzdump;
64 $self->{storecfg} = PVE::Storage::config();
65
66 $self->{vmlist} = PVE::LXC::config_list();
67
68 return $self;
69 }
70
71 sub type {
72 return 'lxc';
73 }
74
75 sub vm_status {
76 my ($self, $vmid) = @_;
77
78 my $running = PVE::LXC::check_running($vmid) ? 1 : 0;
79
80 return wantarray ? ($running, $running ? 'running' : 'stopped') : $running;
81 }
82
83 my $check_mountpoint_empty = sub {
84 my ($mountpoint) = @_;
85
86 die "mountpoint '$mountpoint' is not a directory\n" if ! -d $mountpoint;
87
88 PVE::Tools::dir_glob_foreach($mountpoint, qr/.*/, sub {
89 my $entry = shift;
90 return if $entry eq '.' || $entry eq '..';
91 die "mountpoint '$mountpoint' not empty\n";
92 });
93 };
94
95 # The container might have *different* symlinks than the host. realpath/abs_path
96 # use the actual filesystem to resolve links.
97 sub sanitize_mountpoint {
98 my ($mp) = @_;
99 $mp = '/' . $mp; # we always start with a slash
100 $mp =~ s@/{2,}@/@g; # collapse sequences of slashes
101 $mp =~ s@/\./@@g; # collapse /./
102 $mp =~ s@/\.(/)?$@$1@; # collapse a trailing /. or /./
103 $mp =~ s@(.*)/[^/]+/\.\./@$1/@g; # collapse /../ without regard for symlinks
104 $mp =~ s@/\.\.(/)?$@$1@; # collapse trailing /.. or /../ disregarding symlinks
105 return $mp;
106 }
107
108 sub prepare {
109 my ($self, $task, $vmid, $mode) = @_;
110
111 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::load_config($vmid);
112 my $storage_cfg = $self->{storecfg};
113
114 my $running = PVE::LXC::check_running($vmid);
115
116 my $disks = $task->{disks} = [];
117 my $exclude_dirs = $task->{exclude_dirs} = [];
118
119 $task->{hostname} = $conf->{'hostname'} || "CT$vmid";
120
121 # fixme: when do we deactivate ??
122 PVE::LXC::foreach_mountpoint($conf, sub {
123 my ($name, $data) = @_;
124 my $volid = $data->{volume};
125 my $mount = $data->{mp};
126
127 $mount = $data->{mp} = sanitize_mountpoint($mount);
128
129 return if !$volid || !$mount || $volid =~ m|^/|;
130
131 if ($name ne 'rootfs' && !$data->{backup}) {
132 push @$exclude_dirs, $mount;
133 return;
134 }
135
136 push @$disks, $data;
137 });
138 my $volid_list = [map { $_->{volume} } @$disks];
139 PVE::Storage::activate_volumes($storage_cfg, $volid_list);
140
141 if ($mode eq 'snapshot') {
142 if (!PVE::LXC::has_feature('snapshot', $conf, $storage_cfg)) {
143 die "mode failure - some volumes does not support snapshots\n";
144 }
145
146 if ($conf->{snapshots} && $conf->{snapshots}->{vzdump}) {
147 $self->loginfo("found old vzdump snapshot (force removal)");
148 PVE::LXC::snapshot_delete($vmid, 'vzdump', 0);
149 }
150
151 my $rootdir = $default_mount_point;
152 mkpath $rootdir;
153 &$check_mountpoint_empty($rootdir);
154
155 # set snapshot_count (freezes CT it snapshot_count > 1)
156 $task->{snapshot_count} = scalar(@$volid_list);
157 } elsif ($mode eq 'stop') {
158 my $rootdir = $default_mount_point;
159 mkpath $rootdir;
160 &$check_mountpoint_empty($rootdir);
161 } elsif ($mode eq 'suspend') {
162 my $pid = PVE::LXC::find_lxc_pid($vmid);
163 foreach my $disk (@$disks) {
164 $disk->{dir} = "/proc/$pid/root$disk->{mp}";
165 }
166 $task->{snapdir} = $task->{tmpdir};
167 } else {
168 die "unknown mode '$mode'\n"; # should not happen
169 }
170 }
171
172 sub lock_vm {
173 my ($self, $vmid) = @_;
174
175 PVE::LXC::lock_aquire($vmid);
176 }
177
178 sub unlock_vm {
179 my ($self, $vmid) = @_;
180
181 PVE::LXC::lock_release($vmid);
182 }
183
184 sub snapshot {
185 my ($self, $task, $vmid) = @_;
186
187 $self->loginfo("create storage snapshot snapshot");
188
189 # todo: freeze/unfreeze if we have more than one volid
190 PVE::LXC::snapshot_create($vmid, 'vzdump', "vzdump backup snapshot");
191 $task->{cleanup}->{remove_snapshot} = 1;
192
193 # reload config
194 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::load_config($vmid);
195 die "unable to read vzdump shanpshot config - internal error"
196 if !($conf->{snapshots} && $conf->{snapshots}->{vzdump});
197
198 my $disks = $task->{disks};
199 my $volid_list = [map { $_->{volume} } @$disks];
200
201 my $rootdir = $default_mount_point;
202 my $storage_cfg = $self->{storecfg};
203
204 foreach my $disk (@$disks) {
205 $disk->{dir} = "${rootdir}$disk->{mp}";
206 PVE::LXC::mountpoint_mount($disk, $rootdir, $storage_cfg, 'vzdump');
207 }
208
209 $task->{snapdir} = $rootdir;
210 }
211
212 sub copy_data_phase1 {
213 my ($self, $task) = @_;
214
215 $self->$rsync_vm($task, $task->{snapdir}, "first");
216 }
217
218 sub copy_data_phase2 {
219 my ($self, $task) = @_;
220
221 $self->$rsync_vm($task, $task->{snapdir}, "final");
222 }
223
224 sub stop_vm {
225 my ($self, $task, $vmid) = @_;
226
227 $self->cmd("lxc-stop -n $vmid");
228 }
229
230 sub start_vm {
231 my ($self, $task, $vmid) = @_;
232
233 $self->cmd ("lxc-start -n $vmid");
234 }
235
236 sub suspend_vm {
237 my ($self, $task, $vmid) = @_;
238
239 $self->cmd ("lxc-freeze -n $vmid");
240 }
241
242 sub resume_vm {
243 my ($self, $task, $vmid) = @_;
244
245 $self->cmd ("lxc-unfreeze -n $vmid");
246 }
247
248 sub assemble {
249 my ($self, $task, $vmid) = @_;
250
251 my $tmpdir = $task->{tmpdir};
252
253 mkpath "$tmpdir/etc/vzdump/";
254
255 my $conf = PVE::LXC::load_config($vmid);
256 delete $conf->{snapshots};
257 delete $conf->{'pve.parent'};
258
259 PVE::Tools::file_set_contents("$tmpdir/etc/vzdump/pct.conf", PVE::LXC::write_pct_config("/lxc/$vmid.conf", $conf));
260 }
261
262 sub archive {
263 my ($self, $task, $vmid, $filename, $comp) = @_;
264
265 my $disks = $task->{disks};
266 my @sources;
267
268 if ($task->{mode} eq 'stop') {
269 my $rootdir = $default_mount_point;
270 my $storage_cfg = $self->{storecfg};
271 foreach my $disk (@$disks) {
272 $disk->{dir} = "${rootdir}$disk->{mp}";
273 PVE::LXC::mountpoint_mount($disk, $rootdir, $storage_cfg);
274 # add every enabled mountpoint (since we use --one-file-system)
275 # mp already starts with a / so we only need to add the dot
276 push @sources, ".$disk->{mp}";
277 }
278 $task->{snapdir} = $rootdir;
279 } else {
280 # the data was rsynced to a temporary location, only use '.' to avoid
281 # having mountpoints duplicated
282 push @sources, '.';
283 }
284
285 my $opts = $self->{vzdump}->{opts};
286 my $snapdir = $task->{snapdir};
287 my $tmpdir = $task->{tmpdir};
288
289 my $tar = ['tar', 'cpf', '-',
290 '--totals', '--sparse', '--numeric-owner', '--xattrs',
291 '--one-file-system'];
292
293 # note: --remove-files does not work because we do not
294 # backup all files (filters). tar complains:
295 # Cannot rmdir: Directory not empty
296 # we we disable this optimization for now
297 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
298 # push @$tar, "--remove-files"; # try to save space
299 #}
300
301 # The directory parameter can give a alternative directory as source.
302 # the second parameter gives the structure in the tar.
303 push @$tar, "--directory=$tmpdir", './etc/vzdump/pct.conf';
304 push @$tar, "--directory=$snapdir";
305 push @$tar, map { "--exclude=.$_" } @{$self->{vzdump}->{findexcl}};
306
307 push @$tar, @sources;
308
309 my $cmd = [ $tar ];
310
311 my $bwl = $opts->{bwlimit}*1024; # bandwidth limit for cstream
312 push @$cmd, [ 'cstream', '-t', $bwl ] if $opts->{bwlimit};
313 push @$cmd, [ $comp ] if $comp;
314
315 if ($opts->{stdout}) {
316 push @{$cmd->[-1]}, \(">&" . fileno($opts->{stdout}));
317 } else {
318 push @{$cmd->[-1]}, \(">" . PVE::Tools::shellquote($filename));
319 }
320 $self->cmd($cmd);
321 }
322
323 sub cleanup {
324 my ($self, $task, $vmid) = @_;
325
326 my $conf = PVE::LXC::load_config($vmid);
327
328 if ($task->{mode} ne 'suspend') {
329 my $rootdir = $default_mount_point;
330 my $disks = $task->{disks};
331 foreach my $disk (reverse @$disks) {
332 PVE::Tools::run_command(['umount', '-l', '-d', $disk->{dir}]) if $disk->{dir};
333 }
334 }
335
336 if ($task->{cleanup}->{remove_snapshot}) {
337 $self->loginfo("remove vzdump snapshot");
338 PVE::LXC::snapshot_delete($vmid, 'vzdump', 0);
339 }
340 }
341
342 1;