]> git.proxmox.com Git - pve-container.git/blob - src/PVE/VZDump/LXC.pm
vzdump: always check if we have a valid rootfs volid
[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 die "missing root volid (no volid)\n" if !$volid;
118
119 # fixme: when do we deactivate ??
120 PVE::Storage::activate_volumes($self->{storecfg}, [$volid]);
121
122 if ($mode eq 'snapshot') {
123
124 die "mode failure - storage does not support snapshots\n"
125 if !PVE::Storage::volume_has_feature($self->{storecfg}, 'snapshot', $volid);
126
127 my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
128
129 my $scfg = PVE::Storage::storage_config($self->{storecfg}, $sid);
130
131 # we only handle well known types for now, because the storage
132 # library dos not handle mount/unmount of snapshots
133
134 if ($scfg->{type} ne 'zfs') {
135 $diskinfo->{mountpoint} = "/mnt/vzsnap0";
136 } else {
137 die "mode failure - storage does not support snapshot mount\n"
138 }
139
140 PVE::Storage::volume_snapshot($self->{storecfg}, $volid, '__vzdump__');
141 $task->{cleanup}->{snap_volid} = $volid;
142
143 die "implement me";
144
145 } elsif ($mode eq 'stop') {
146 my $mountpoint = "/mnt/vzsnap0";
147 my $path = PVE::Storage::path($self->{storecfg}, $volid);
148 &$loop_mount_image($path, $mountpoint);
149 $task->{cleanup}->{snapshot_mount} = 1;
150 $diskinfo->{dir} = $diskinfo->{mountpoint} = $mountpoint;
151 $task->{snapdir} = $diskinfo->{dir};
152 } elsif ($mode eq 'suspend') {
153 my $tasks_fn = "/sys/fs/cgroup/cpu/lxc/$vmid/tasks";
154 my $init_pid = PVE::Tools::file_read_firstline($tasks_fn);
155 if ($init_pid =~ m/^(\d+)$/) {
156 $diskinfo->{dir} = "/proc/$1/root";
157 } else {
158 die "unable to find container init task\n";
159 }
160 $task->{snapdir} = $task->{tmpdir};
161 } else {
162 die "unknown mode '$mode'\n"; # should not happen
163 }
164 }
165
166 sub lock_vm {
167 my ($self, $vmid) = @_;
168
169 PVE::LXC::lock_aquire($vmid);
170 }
171
172 sub unlock_vm {
173 my ($self, $vmid) = @_;
174
175 PVE::LXC::lock_release($vmid);
176 }
177
178 sub copy_data_phase1 {
179 my ($self, $task) = @_;
180
181 $self->$rsync_vm($task, "$task->{diskinfo}->{dir}/", $task->{snapdir}, "first");
182 }
183
184 sub copy_data_phase2 {
185 my ($self, $task) = @_;
186
187 $self->$rsync_vm ($task, "$task->{diskinfo}->{dir}/", $task->{snapdir}, "final");
188 }
189
190 sub stop_vm {
191 my ($self, $task, $vmid) = @_;
192
193 $self->cmd("lxc-stop -n $vmid");
194 }
195
196 sub start_vm {
197 my ($self, $task, $vmid) = @_;
198
199 $self->cmd ("lxc-start -n $vmid");
200 }
201
202 sub suspend_vm {
203 my ($self, $task, $vmid) = @_;
204
205 $self->cmd ("lxc-freeze -n $vmid");
206 }
207
208 sub resume_vm {
209 my ($self, $task, $vmid) = @_;
210
211 $self->cmd ("lxc-unfreeze -n $vmid");
212 }
213
214 sub assemble {
215 my ($self, $task, $vmid) = @_;
216
217 my $dir = $task->{snapdir};
218
219 $task->{cleanup}->{etc_vzdump} = 1;
220
221 mkpath "$dir/etc/vzdump/";
222
223 my $conf = PVE::LXC::load_config($vmid);
224 delete $conf->{snapshots};
225 delete $conf->{'pve.parent'};
226
227 PVE::Tools::file_set_contents("$dir/etc/vzdump/pct.conf", PVE::LXC::write_pct_config("/lxc/$vmid.conf", $conf));
228 }
229
230 sub archive {
231 my ($self, $task, $vmid, $filename, $comp) = @_;
232
233 my $findexcl = $self->{vzdump}->{findexcl};
234 my $findargs = join (' ', @$findexcl) . ' -print0';
235 my $opts = $self->{vzdump}->{opts};
236
237 my $srcdir = $task->{diskinfo}->{dir};
238 my $snapdir = $task->{snapdir};
239
240 my $taropts = "--totals --sparse --numeric-owner --no-recursion --xattrs --one-file-system";
241
242 # note: --remove-files does not work because we do not
243 # backup all files (filters). tar complains:
244 # Cannot rmdir: Directory not empty
245 # we we disable this optimization for now
246 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
247 # $taropts .= " --remove-files"; # try to save space
248 #}
249
250 my $cmd = "(";
251
252 $cmd .= "cd $snapdir;find . $findargs|sed 's/\\\\/\\\\\\\\/g'|";
253 $cmd .= "tar cpf - $taropts ./etc/vzdump/pct.conf --null -T -";
254 my $bwl = $opts->{bwlimit}*1024; # bandwidth limit for cstream
255 $cmd .= "|cstream -t $bwl" if $opts->{bwlimit};
256 $cmd .= "|$comp" if $comp;
257
258 $cmd .= ")";
259
260 if ($opts->{stdout}) {
261 $self->cmd ($cmd, output => ">&" . fileno($opts->{stdout}));
262 } else {
263 $self->cmd ("$cmd >$filename");
264 }
265 }
266
267 sub cleanup {
268 my ($self, $task, $vmid) = @_;
269
270 my $di = $task->{diskinfo};
271
272 if ($task->{cleanup}->{snapshot_mount}) {
273 # Note: sleep to avoid 'device is busy' message.
274 # Seems Kernel need some time to cleanup open file list,
275 # for example when we stop the tar with kill (stop task)
276 # We use -d to automatically free used loop devices
277 sleep(1);
278 $self->cmd_noerr("umount -d $di->{mountpoint}");
279 }
280
281 if (my $volid = $task->{cleanup}->{snap_volid}) {
282 eval { PVE::Storage::volume_snapshot_delete($self->{storecfg}, $volid, '__vzdump__'); };
283 warn $@ if $@;
284 }
285
286 if ($task->{cleanup}->{etc_vzdump}) {
287 my $dir = "$task->{snapdir}/etc/vzdump";
288 eval { rmtree $dir if -d $dir; };
289 $self->logerr ($@) if $@;
290 }
291
292 }
293
294 1;