]> git.proxmox.com Git - pve-container.git/blob - src/PVE/VZDump/LXC.pm
do not pass 'noerr' flags to parse_volume_id
[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 $rsync_vm = sub {
17 my ($self, $task, $from, $to, $text) = @_;
18
19 $self->loginfo ("starting $text sync $from to $to");
20
21 my $starttime = time();
22
23 my $opts = $self->{vzdump}->{opts};
24
25 my $rsyncopts = "--stats -x -X --numeric-ids";
26
27 $rsyncopts .= " --bwlimit=$opts->{bwlimit}" if $opts->{bwlimit};
28
29 $self->cmd ("rsync $rsyncopts -aH --delete --no-whole-file --inplace '$from' '$to'");
30
31 my $delay = time () - $starttime;
32
33 $self->loginfo ("$text sync finished ($delay seconds)");
34 };
35
36 sub new {
37 my ($class, $vzdump) = @_;
38
39 PVE::VZDump::check_bin('lxc-stop');
40 PVE::VZDump::check_bin('lxc-start');
41 PVE::VZDump::check_bin('lxc-freeze');
42 PVE::VZDump::check_bin('lxc-unfreeze');
43
44 my $self = bless {};
45
46 $self->{vzdump} = $vzdump;
47 $self->{storecfg} = PVE::Storage::config();
48
49 $self->{vmlist} = PVE::LXC::config_list();
50
51 return $self;
52 }
53
54 sub type {
55 return 'lxc';
56 }
57
58 sub vm_status {
59 my ($self, $vmid) = @_;
60
61 my $running = PVE::LXC::check_running($vmid) ? 1 : 0;
62
63 return wantarray ? ($running, $running ? 'running' : 'stopped') : $running;
64 }
65
66 my $loop_mount_image = sub {
67 my ($image_path, $mountpoint) = @_;
68
69 my $loopdev;
70 my $mounted;
71 eval {
72 my $parser = sub {
73 my $line = shift;
74 $loopdev = $line if $line =~m|^/dev/loop\d+$|;
75 };
76 PVE::Tools::run_command(['losetup', '--find', '--show', $image_path], outfunc => $parser);
77
78 File::Path::mkpath($mountpoint);
79 PVE::Tools::run_command(['mount', '-t', 'ext4', $loopdev, $mountpoint]);
80 $mounted = 1;
81 };
82 if (my $err = $@) {
83 if ($mounted) {
84 eval { PVE::Tools::run_command(['umount', '-d', $mountpoint]) };
85 warn $@ if $@;
86 } else {
87 eval { PVE::Tools::run_command(['losetup', '-d', $loopdev]) if $loopdev; };
88 warn $@ if $@;
89 }
90 die $err;
91 }
92 };
93
94 sub prepare {
95 my ($self, $task, $vmid, $mode) = @_;
96
97 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::load_config($vmid);
98
99 PVE::LXC::foreach_mountpoint($conf, sub {
100 my ($ms, $mountpoint) = @_;
101
102 return if $ms eq 'rootfs';
103 # TODO: implement support for mountpoints
104 die "unable to backup mountpoint '$ms' - feature not implemented\n";
105 });
106
107 my $running = PVE::LXC::check_running($vmid);
108
109 my $diskinfo = {};
110 $task->{diskinfo} = $diskinfo;
111
112 $task->{hostname} = $conf->{'hostname'} || "CT$vmid";
113
114 my $rootinfo = PVE::LXC::parse_ct_mountpoint($conf->{rootfs});
115 my $volid = $rootinfo->{volume};
116
117 # fixme: when do we deactivate ??
118 PVE::Storage::activate_volumes($self->{storecfg}, [$volid]) if $volid;
119
120 if ($mode eq 'snapshot') {
121
122 die "mode failure - storage does not support snapshots (no volid)\n"
123 if !$volid;
124
125 die "mode failure - storage does not support snapshots\n"
126 if !PVE::Storage::volume_has_feature($self->{storecfg}, 'snapshot', $volid);
127
128 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
129
130 my $scfg = PVE::Storage::storage_config($self->{storecfg}, $sid);
131
132 # we only handle well known types for now, because the storage
133 # library dos not handle mount/unmount of snapshots
134
135 if ($scfg->{type} ne 'zfs') {
136 $diskinfo->{mountpoint} = "/mnt/vzsnap0";
137 } else {
138 die "mode failure - storage does not support snapshot mount\n"
139 }
140
141 PVE::Storage::volume_snapshot($self->{storecfg}, $volid, '__vzdump__');
142 $task->{cleanup}->{snap_volid} = $volid;
143
144 die "implement me";
145
146 } elsif ($mode eq 'stop') {
147 my $mountpoint = "/mnt/vzsnap0";
148 my $path = PVE::Storage::path($self->{storecfg}, $volid);
149 &$loop_mount_image($path, $mountpoint);
150 $task->{cleanup}->{snapshot_mount} = 1;
151 $diskinfo->{dir} = $diskinfo->{mountpoint} = $mountpoint;
152 $task->{snapdir} = $diskinfo->{dir};
153 } elsif ($mode eq 'suspend') {
154 my $tasks_fn = "/sys/fs/cgroup/cpu/lxc/$vmid/tasks";
155 my $init_pid = PVE::Tools::file_read_firstline($tasks_fn);
156 if ($init_pid =~ m/^(\d+)$/) {
157 $diskinfo->{dir} = "/proc/$1/root";
158 } else {
159 die "unable to find container init task\n";
160 }
161 $task->{snapdir} = $task->{tmpdir};
162 } else {
163 die "unknown mode '$mode'\n"; # should not happen
164 }
165 }
166
167 sub lock_vm {
168 my ($self, $vmid) = @_;
169
170 PVE::LXC::lock_aquire($vmid);
171 }
172
173 sub unlock_vm {
174 my ($self, $vmid) = @_;
175
176 PVE::LXC::lock_release($vmid);
177 }
178
179 sub copy_data_phase1 {
180 my ($self, $task) = @_;
181
182 $self->$rsync_vm($task, "$task->{diskinfo}->{dir}/", $task->{snapdir}, "first");
183 }
184
185 sub copy_data_phase2 {
186 my ($self, $task) = @_;
187
188 $self->$rsync_vm ($task, "$task->{diskinfo}->{dir}/", $task->{snapdir}, "final");
189 }
190
191 sub stop_vm {
192 my ($self, $task, $vmid) = @_;
193
194 $self->cmd("lxc-stop -n $vmid");
195 }
196
197 sub start_vm {
198 my ($self, $task, $vmid) = @_;
199
200 $self->cmd ("lxc-start -n $vmid");
201 }
202
203 sub suspend_vm {
204 my ($self, $task, $vmid) = @_;
205
206 $self->cmd ("lxc-freeze -n $vmid");
207 }
208
209 sub resume_vm {
210 my ($self, $task, $vmid) = @_;
211
212 $self->cmd ("lxc-unfreeze -n $vmid");
213 }
214
215 sub assemble {
216 my ($self, $task, $vmid) = @_;
217
218 my $dir = $task->{snapdir};
219
220 $task->{cleanup}->{etc_vzdump} = 1;
221
222 mkpath "$dir/etc/vzdump/";
223
224 my $conf = PVE::LXC::load_config($vmid);
225 delete $conf->{snapshots};
226 delete $conf->{'pve.parent'};
227
228 PVE::Tools::file_set_contents("$dir/etc/vzdump/pct.conf", PVE::LXC::write_pct_config("/lxc/$vmid.conf", $conf));
229 }
230
231 sub archive {
232 my ($self, $task, $vmid, $filename, $comp) = @_;
233
234 my $findexcl = $self->{vzdump}->{findexcl};
235 my $findargs = join (' ', @$findexcl) . ' -print0';
236 my $opts = $self->{vzdump}->{opts};
237
238 my $srcdir = $task->{diskinfo}->{dir};
239 my $snapdir = $task->{snapdir};
240
241 my $taropts = "--totals --sparse --numeric-owner --no-recursion --xattrs --one-file-system";
242
243 # note: --remove-files does not work because we do not
244 # backup all files (filters). tar complains:
245 # Cannot rmdir: Directory not empty
246 # we we disable this optimization for now
247 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
248 # $taropts .= " --remove-files"; # try to save space
249 #}
250
251 my $cmd = "(";
252
253 $cmd .= "cd $snapdir;find . $findargs|sed 's/\\\\/\\\\\\\\/g'|";
254 $cmd .= "tar cpf - $taropts ./etc/vzdump/pct.conf --null -T -";
255 my $bwl = $opts->{bwlimit}*1024; # bandwidth limit for cstream
256 $cmd .= "|cstream -t $bwl" if $opts->{bwlimit};
257 $cmd .= "|$comp" if $comp;
258
259 $cmd .= ")";
260
261 if ($opts->{stdout}) {
262 $self->cmd ($cmd, output => ">&" . fileno($opts->{stdout}));
263 } else {
264 $self->cmd ("$cmd >$filename");
265 }
266 }
267
268 sub cleanup {
269 my ($self, $task, $vmid) = @_;
270
271 my $di = $task->{diskinfo};
272
273 if ($task->{cleanup}->{snapshot_mount}) {
274 # Note: sleep to avoid 'device is busy' message.
275 # Seems Kernel need some time to cleanup open file list,
276 # for example when we stop the tar with kill (stop task)
277 # We use -d to automatically free used loop devices
278 sleep(1);
279 $self->cmd_noerr("umount -d $di->{mountpoint}");
280 }
281
282 if (my $volid = $task->{cleanup}->{snap_volid}) {
283 eval { PVE::Storage::volume_snapshot_delete($self->{storecfg}, $volid, '__vzdump__'); };
284 warn $@ if $@;
285 }
286
287 if ($task->{cleanup}->{etc_vzdump}) {
288 my $dir = "$task->{snapdir}/etc/vzdump";
289 eval { rmtree $dir if -d $dir; };
290 $self->logerr ($@) if $@;
291 }
292
293 }
294
295 1;