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