]> git.proxmox.com Git - pve-container.git/blob - src/PVE/VZDump/LXC.pm
ccad9cd561273a316ffb2d9ae46186b84fdd0252
[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, $from, $to, $text) = @_;
20
21 $self->loginfo ("starting $text sync $from to $to");
22
23 my $starttime = time();
24
25 my $opts = $self->{vzdump}->{opts};
26
27 my $rsyncopts = "--stats -x -X --numeric-ids";
28
29 $rsyncopts .= " --bwlimit=$opts->{bwlimit}" if $opts->{bwlimit};
30
31 $self->cmd ("rsync $rsyncopts -aH --delete --no-whole-file --inplace '$from' '$to'");
32
33 my $delay = time () - $starttime;
34
35 $self->loginfo ("$text sync finished ($delay seconds)");
36 };
37
38 sub new {
39 my ($class, $vzdump) = @_;
40
41 PVE::VZDump::check_bin('lxc-stop');
42 PVE::VZDump::check_bin('lxc-start');
43 PVE::VZDump::check_bin('lxc-freeze');
44 PVE::VZDump::check_bin('lxc-unfreeze');
45
46 my $self = bless {};
47
48 $self->{vzdump} = $vzdump;
49 $self->{storecfg} = PVE::Storage::config();
50
51 $self->{vmlist} = PVE::LXC::config_list();
52
53 return $self;
54 }
55
56 sub type {
57 return 'lxc';
58 }
59
60 sub vm_status {
61 my ($self, $vmid) = @_;
62
63 my $running = PVE::LXC::check_running($vmid) ? 1 : 0;
64
65 return wantarray ? ($running, $running ? 'running' : 'stopped') : $running;
66 }
67
68 my $check_mountpoint_empty = sub {
69 my ($mountpoint) = @_;
70
71 die "mountpoint '$mountpoint' is not a directory\n" if ! -d $mountpoint;
72
73 PVE::Tools::dir_glob_foreach($mountpoint, qr/.*/, sub {
74 my $entry = shift;
75 return if $entry eq '.' || $entry eq '..';
76 die "mountpoint '$mountpoint' not empty\n";
77 });
78 };
79
80 sub prepare {
81 my ($self, $task, $vmid, $mode) = @_;
82
83 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::load_config($vmid);
84
85 PVE::LXC::foreach_mountpoint($conf, sub {
86 my ($ms, $mountpoint) = @_;
87
88 return if $ms eq 'rootfs';
89 # TODO: implement support for mountpoints
90 die "unable to backup mountpoint '$ms' - feature not implemented\n";
91 });
92
93 my $running = PVE::LXC::check_running($vmid);
94
95 my $diskinfo = $task->{diskinfo} = {};
96
97 $task->{hostname} = $conf->{'hostname'} || "CT$vmid";
98
99 my $rootinfo = PVE::LXC::parse_ct_mountpoint($conf->{rootfs});
100 $diskinfo->{volid} = $rootinfo->{volume};
101
102 die "missing root volid (no volid)\n" if !$diskinfo->{volid};
103
104 # fixme: when do we deactivate ??
105 PVE::Storage::activate_volumes($self->{storecfg}, [$diskinfo->{volid}]);
106
107 if ($mode eq 'snapshot') {
108
109 if (!PVE::LXC::has_feature('snapshot', $conf, $self->{storecfg})) {
110 die "mode failure - some volumes does not support snapshots\n";
111 }
112
113 if ($conf->{snapshots} && $conf->{snapshots}->{vzdump}) {
114 $self->loginfo("found old vzdump snapshot (force removal)");
115 PVE::LXC::snapshot_delete($vmid, 'vzdump', 0);
116 }
117
118 my $rootdir = $default_mount_point;
119 mkpath $rootdir;
120 &$check_mountpoint_empty($rootdir);
121
122 # set snapshot_count (freezes CT it snapshot_count > 1)
123 my $volid_list = PVE::LXC::get_vm_volumes($conf);
124 $task->{snapshot_count} = scalar(@$volid_list);
125
126 } elsif ($mode eq 'stop') {
127 my $rootdir = $default_mount_point;
128 mkpath $rootdir;
129 &$check_mountpoint_empty($rootdir);
130 } elsif ($mode eq 'suspend') {
131 my $pid = PVE::LXC::find_lxc_pid($vmid);
132 $diskinfo->{dir} = "/proc/$pid/root";
133 $task->{snapdir} = $task->{tmpdir};
134 } else {
135 die "unknown mode '$mode'\n"; # should not happen
136 }
137 }
138
139 sub lock_vm {
140 my ($self, $vmid) = @_;
141
142 PVE::LXC::lock_aquire($vmid);
143 }
144
145 sub unlock_vm {
146 my ($self, $vmid) = @_;
147
148 PVE::LXC::lock_release($vmid);
149 }
150
151 sub snapshot {
152 my ($self, $task, $vmid) = @_;
153
154 my $diskinfo = $task->{diskinfo};
155
156 $self->loginfo("create storage snapshot snapshot");
157
158 # todo: freeze/unfreeze if we have more than one volid
159 PVE::LXC::snapshot_create($vmid, 'vzdump', "vzdump backup snapshot");
160 $task->{cleanup}->{remove_snapshot} = 1;
161
162 # reload config
163 my $conf = $self->{vmlist}->{$vmid} = PVE::LXC::load_config($vmid);
164 die "unable to read vzdump shanpshot config - internal error"
165 if !($conf->{snapshots} && $conf->{snapshots}->{vzdump});
166
167 # my $snapconf = $conf->{snapshots}->{vzdump};
168 # my $volid_list = PVE::LXC::get_vm_volumes($snapconf);
169 my $volid_list = [$diskinfo->{volid}];
170
171 my $rootdir = $default_mount_point;
172
173 my $mp = { volume => $diskinfo->{volid}, mp => "/" };
174 PVE::LXC::mountpoint_mount($mp, $rootdir, $self->{storecfg}, 'vzdump');
175
176 $diskinfo->{dir} = $diskinfo->{mountpoint} = $rootdir;
177 $task->{snapdir} = $diskinfo->{dir};
178 }
179
180 sub copy_data_phase1 {
181 my ($self, $task) = @_;
182
183 $self->$rsync_vm($task, "$task->{diskinfo}->{dir}/", $task->{snapdir}, "first");
184 }
185
186 sub copy_data_phase2 {
187 my ($self, $task) = @_;
188
189 $self->$rsync_vm ($task, "$task->{diskinfo}->{dir}/", $task->{snapdir}, "final");
190 }
191
192 sub stop_vm {
193 my ($self, $task, $vmid) = @_;
194
195 $self->cmd("lxc-stop -n $vmid");
196 }
197
198 sub start_vm {
199 my ($self, $task, $vmid) = @_;
200
201 $self->cmd ("lxc-start -n $vmid");
202 }
203
204 sub suspend_vm {
205 my ($self, $task, $vmid) = @_;
206
207 $self->cmd ("lxc-freeze -n $vmid");
208 }
209
210 sub resume_vm {
211 my ($self, $task, $vmid) = @_;
212
213 $self->cmd ("lxc-unfreeze -n $vmid");
214 }
215
216 sub assemble {
217 my ($self, $task, $vmid) = @_;
218
219 my $tmpdir = $task->{tmpdir};
220
221 mkpath "$tmpdir/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("$tmpdir/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 if ($task->{mode} eq 'stop') {
234 my $rootdir = $default_mount_point;
235 my $diskinfo = $task->{diskinfo};
236
237 my $volid_list = [$diskinfo->{volid}];
238 my $mp = { volume => $diskinfo->{volid}, mp => "/" };
239
240 $self->loginfo("mounting container root at '$rootdir'");
241 PVE::LXC::mountpoint_mount($mp, $rootdir, $self->{storecfg});
242
243 $diskinfo->{dir} = $diskinfo->{mountpoint} = $rootdir;
244 $task->{snapdir} = $diskinfo->{dir};
245 }
246
247 my $findexcl = $self->{vzdump}->{findexcl};
248 push @$findexcl, "'('", '-path', "./etc/vzdump", "-prune", "')'", '-o';
249
250 my $findargs = join (' ', @$findexcl) . ' -print0';
251 my $opts = $self->{vzdump}->{opts};
252
253 my $srcdir = $task->{diskinfo}->{dir};
254 my $snapdir = $task->{snapdir};
255 my $tmpdir = $task->{tmpdir};
256
257 my $taropts = "--totals --sparse --numeric-owner --no-recursion --xattrs --one-file-system";
258
259 # note: --remove-files does not work because we do not
260 # backup all files (filters). tar complains:
261 # Cannot rmdir: Directory not empty
262 # we we disable this optimization for now
263 #if ($snapdir eq $task->{tmpdir} && $snapdir =~ m|^$opts->{dumpdir}/|) {
264 # $taropts .= " --remove-files"; # try to save space
265 #}
266
267 my $cmd = "(";
268
269 $cmd .= "cd $snapdir;find . $findargs|sed 's/\\\\/\\\\\\\\/g'|";
270 $cmd .= "tar cpf - $taropts ";
271 # The directory parameter can give a alternative directory as source.
272 # the second parameter gives the structure in the tar.
273 $cmd .= "--directory=$tmpdir ./etc/vzdump/pct.conf ";
274 $cmd .= "--directory=$snapdir --null -T -";
275
276 my $bwl = $opts->{bwlimit}*1024; # bandwidth limit for cstream
277 $cmd .= "|cstream -t $bwl" if $opts->{bwlimit};
278 $cmd .= "|$comp" if $comp;
279
280 $cmd .= ")";
281
282 if ($opts->{stdout}) {
283 $self->cmd ($cmd, output => ">&" . fileno($opts->{stdout}));
284 } else {
285 $self->cmd ("$cmd >$filename");
286 }
287 }
288
289 sub cleanup {
290 my ($self, $task, $vmid) = @_;
291
292 my $diskinfo = $task->{diskinfo};
293
294 if (my $rootdir = $diskinfo->{mountpoint}) {
295 PVE::Tools::run_command(['umount', '-l', '-d', $rootdir]);
296 };
297
298 if ($task->{cleanup}->{remove_snapshot}) {
299 $self->loginfo("remove vzdump snapshot");
300 PVE::LXC::snapshot_delete($vmid, 'vzdump', 0);
301 }
302 }
303
304 1;