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