]>
Commit | Line | Data |
---|---|---|
b6cf0a66 DM |
1 | package PVE::Storage; |
2 | ||
3 | use strict; | |
ffd6f2f3 | 4 | use warnings; |
dec97937 | 5 | use Data::Dumper; |
ffd6f2f3 | 6 | |
b6cf0a66 DM |
7 | use POSIX; |
8 | use IO::Select; | |
b6cf0a66 | 9 | use IO::File; |
b6cf0a66 DM |
10 | use File::Basename; |
11 | use File::Path; | |
b6cf0a66 | 12 | use Cwd 'abs_path'; |
7a2d5c1a | 13 | use Socket; |
b6cf0a66 | 14 | |
1689e627 | 15 | use PVE::Tools qw(run_command file_read_firstline $IPV6RE); |
83d7192f | 16 | use PVE::Cluster qw(cfs_read_file cfs_write_file cfs_lock_file); |
b6cf0a66 DM |
17 | use PVE::Exception qw(raise_param_exc); |
18 | use PVE::JSONSchema; | |
19 | use PVE::INotify; | |
88c3abaf | 20 | use PVE::RPCEnvironment; |
b6cf0a66 | 21 | |
1dc01b9f DM |
22 | use PVE::Storage::Plugin; |
23 | use PVE::Storage::DirPlugin; | |
24 | use PVE::Storage::LVMPlugin; | |
610798bc | 25 | use PVE::Storage::LvmThinPlugin; |
1dc01b9f DM |
26 | use PVE::Storage::NFSPlugin; |
27 | use PVE::Storage::ISCSIPlugin; | |
0509010d | 28 | use PVE::Storage::RBDPlugin; |
caf1960c | 29 | use PVE::Storage::SheepdogPlugin; |
86616554 | 30 | use PVE::Storage::ISCSIDirectPlugin; |
f4648aef | 31 | use PVE::Storage::GlusterfsPlugin; |
85fda4dd | 32 | use PVE::Storage::ZFSPoolPlugin; |
4f914e6e | 33 | use PVE::Storage::ZFSPlugin; |
14770890 | 34 | use PVE::Storage::DRBDPlugin; |
b6cf0a66 | 35 | |
1dc01b9f DM |
36 | # load and initialize all plugins |
37 | PVE::Storage::DirPlugin->register(); | |
38 | PVE::Storage::LVMPlugin->register(); | |
610798bc | 39 | PVE::Storage::LvmThinPlugin->register(); |
1dc01b9f DM |
40 | PVE::Storage::NFSPlugin->register(); |
41 | PVE::Storage::ISCSIPlugin->register(); | |
0509010d | 42 | PVE::Storage::RBDPlugin->register(); |
caf1960c | 43 | PVE::Storage::SheepdogPlugin->register(); |
86616554 | 44 | PVE::Storage::ISCSIDirectPlugin->register(); |
f4648aef | 45 | PVE::Storage::GlusterfsPlugin->register(); |
85fda4dd | 46 | PVE::Storage::ZFSPoolPlugin->register(); |
4f914e6e | 47 | PVE::Storage::ZFSPlugin->register(); |
14770890 | 48 | PVE::Storage::DRBDPlugin->register(); |
1dc01b9f | 49 | PVE::Storage::Plugin->init(); |
b6cf0a66 | 50 | |
1dc01b9f | 51 | my $UDEVADM = '/sbin/udevadm'; |
b6cf0a66 | 52 | |
1dc01b9f | 53 | # PVE::Storage utility functions |
b6cf0a66 DM |
54 | |
55 | sub config { | |
56 | return cfs_read_file("storage.cfg"); | |
57 | } | |
58 | ||
83d7192f FG |
59 | sub write_config { |
60 | my ($cfg) = @_; | |
61 | ||
62 | cfs_write_file('storage.cfg', $cfg); | |
63 | } | |
64 | ||
b6cf0a66 DM |
65 | sub lock_storage_config { |
66 | my ($code, $errmsg) = @_; | |
67 | ||
68 | cfs_lock_file("storage.cfg", undef, $code); | |
69 | my $err = $@; | |
70 | if ($err) { | |
71 | $errmsg ? die "$errmsg: $err" : die $err; | |
72 | } | |
73 | } | |
74 | ||
b6cf0a66 DM |
75 | sub storage_config { |
76 | my ($cfg, $storeid, $noerr) = @_; | |
77 | ||
78 | die "no storage id specified\n" if !$storeid; | |
1a3459ac | 79 | |
b6cf0a66 DM |
80 | my $scfg = $cfg->{ids}->{$storeid}; |
81 | ||
82 | die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg); | |
83 | ||
84 | return $scfg; | |
85 | } | |
86 | ||
87 | sub storage_check_node { | |
88 | my ($cfg, $storeid, $node, $noerr) = @_; | |
89 | ||
1dc01b9f | 90 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
91 | |
92 | if ($scfg->{nodes}) { | |
93 | $node = PVE::INotify::nodename() if !$node || ($node eq 'localhost'); | |
94 | if (!$scfg->{nodes}->{$node}) { | |
da156fb3 | 95 | die "storage '$storeid' is not available on node '$node'\n" if !$noerr; |
b6cf0a66 DM |
96 | return undef; |
97 | } | |
98 | } | |
99 | ||
100 | return $scfg; | |
101 | } | |
102 | ||
103 | sub storage_check_enabled { | |
104 | my ($cfg, $storeid, $node, $noerr) = @_; | |
105 | ||
1dc01b9f | 106 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
107 | |
108 | if ($scfg->{disable}) { | |
109 | die "storage '$storeid' is disabled\n" if !$noerr; | |
110 | return undef; | |
111 | } | |
112 | ||
113 | return storage_check_node($cfg, $storeid, $node, $noerr); | |
114 | } | |
115 | ||
116 | sub storage_ids { | |
117 | my ($cfg) = @_; | |
118 | ||
1dc01b9f | 119 | return keys %{$cfg->{ids}}; |
b6cf0a66 DM |
120 | } |
121 | ||
1dc01b9f DM |
122 | sub file_size_info { |
123 | my ($filename, $timeout) = @_; | |
b6cf0a66 | 124 | |
1dc01b9f | 125 | return PVE::Storage::Plugin::file_size_info($filename, $timeout); |
b6cf0a66 DM |
126 | } |
127 | ||
20ccac1b AD |
128 | sub volume_size_info { |
129 | my ($cfg, $volid, $timeout) = @_; | |
130 | ||
f18199e5 DM |
131 | my ($storeid, $volname) = parse_volume_id($volid, 1); |
132 | if ($storeid) { | |
133 | my $scfg = storage_config($cfg, $storeid); | |
134 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
135 | return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout); | |
136 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
137 | return file_size_info($volid, $timeout); | |
138 | } else { | |
139 | return 0; | |
140 | } | |
20ccac1b AD |
141 | } |
142 | ||
7e6c05dc AD |
143 | sub volume_resize { |
144 | my ($cfg, $volid, $size, $running) = @_; | |
145 | ||
146 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
147 | if ($storeid) { | |
148 | my $scfg = storage_config($cfg, $storeid); | |
149 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
150 | return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running); | |
151 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
f824c722 | 152 | die "resize file/device '$volid' is not possible\n"; |
7e6c05dc | 153 | } else { |
f824c722 | 154 | die "unable to parse volume ID '$volid'\n"; |
7e6c05dc AD |
155 | } |
156 | } | |
157 | ||
1597f1f9 WL |
158 | sub volume_rollback_is_possible { |
159 | my ($cfg, $volid, $snap) = @_; | |
e0852ba7 | 160 | |
1597f1f9 WL |
161 | my ($storeid, $volname) = parse_volume_id($volid, 1); |
162 | if ($storeid) { | |
163 | my $scfg = storage_config($cfg, $storeid); | |
164 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
165 | return $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap); | |
166 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
f824c722 | 167 | die "snapshot rollback file/device '$volid' is not possible\n"; |
1597f1f9 | 168 | } else { |
f824c722 | 169 | die "unable to parse volume ID '$volid'\n"; |
1597f1f9 WL |
170 | } |
171 | } | |
172 | ||
db60719c | 173 | sub volume_snapshot { |
f5640e7d | 174 | my ($cfg, $volid, $snap) = @_; |
db60719c AD |
175 | |
176 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
177 | if ($storeid) { | |
178 | my $scfg = storage_config($cfg, $storeid); | |
179 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
f5640e7d | 180 | return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap); |
db60719c | 181 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { |
f824c722 | 182 | die "snapshot file/device '$volid' is not possible\n"; |
db60719c | 183 | } else { |
f824c722 | 184 | die "unable to parse volume ID '$volid'\n"; |
db60719c AD |
185 | } |
186 | } | |
187 | ||
22a2a633 AD |
188 | sub volume_snapshot_rollback { |
189 | my ($cfg, $volid, $snap) = @_; | |
190 | ||
191 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
192 | if ($storeid) { | |
193 | my $scfg = storage_config($cfg, $storeid); | |
194 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b3f302c6 | 195 | $plugin->volume_rollback_is_possible($scfg, $storeid, $volname, $snap); |
22a2a633 AD |
196 | return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap); |
197 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
f824c722 | 198 | die "snapshot rollback file/device '$volid' is not possible\n"; |
22a2a633 | 199 | } else { |
f824c722 | 200 | die "unable to parse volume ID '$volid'\n"; |
22a2a633 AD |
201 | } |
202 | } | |
203 | ||
5753c9d1 AD |
204 | sub volume_snapshot_delete { |
205 | my ($cfg, $volid, $snap, $running) = @_; | |
206 | ||
207 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
208 | if ($storeid) { | |
209 | my $scfg = storage_config($cfg, $storeid); | |
210 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
27cc55d4 | 211 | return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running); |
5753c9d1 | 212 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { |
f824c722 | 213 | die "snapshot delete file/device '$volid' is not possible\n"; |
5753c9d1 | 214 | } else { |
f824c722 | 215 | die "unable to parse volume ID '$volid'\n"; |
5753c9d1 AD |
216 | } |
217 | } | |
218 | ||
99473759 AD |
219 | sub volume_has_feature { |
220 | my ($cfg, $feature, $volid, $snap, $running) = @_; | |
221 | ||
222 | my ($storeid, $volname) = parse_volume_id($volid, 1); | |
223 | if ($storeid) { | |
224 | my $scfg = storage_config($cfg, $storeid); | |
225 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
226 | return $plugin->volume_has_feature($scfg, $feature, $storeid, $volname, $snap, $running); | |
227 | } elsif ($volid =~ m|^(/.+)$| && -e $volid) { | |
228 | return undef; | |
229 | } else { | |
230 | return undef; | |
231 | } | |
232 | } | |
233 | ||
1dc01b9f DM |
234 | sub get_image_dir { |
235 | my ($cfg, $storeid, $vmid) = @_; | |
b6cf0a66 | 236 | |
1dc01b9f DM |
237 | my $scfg = storage_config($cfg, $storeid); |
238 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 239 | |
1dc01b9f | 240 | my $path = $plugin->get_subdir($scfg, 'images'); |
b6cf0a66 | 241 | |
1dc01b9f | 242 | return $vmid ? "$path/$vmid" : $path; |
b6cf0a66 DM |
243 | } |
244 | ||
1dc01b9f | 245 | sub get_private_dir { |
b6cf0a66 DM |
246 | my ($cfg, $storeid, $vmid) = @_; |
247 | ||
1dc01b9f DM |
248 | my $scfg = storage_config($cfg, $storeid); |
249 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 250 | |
1dc01b9f | 251 | my $path = $plugin->get_subdir($scfg, 'rootdir'); |
d22a6133 | 252 | |
1dc01b9f | 253 | return $vmid ? "$path/$vmid" : $path; |
d22a6133 DM |
254 | } |
255 | ||
b6cf0a66 DM |
256 | sub get_iso_dir { |
257 | my ($cfg, $storeid) = @_; | |
258 | ||
1dc01b9f DM |
259 | my $scfg = storage_config($cfg, $storeid); |
260 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 261 | |
1dc01b9f | 262 | return $plugin->get_subdir($scfg, 'iso'); |
b6cf0a66 DM |
263 | } |
264 | ||
265 | sub get_vztmpl_dir { | |
266 | my ($cfg, $storeid) = @_; | |
267 | ||
1dc01b9f DM |
268 | my $scfg = storage_config($cfg, $storeid); |
269 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 270 | |
1dc01b9f | 271 | return $plugin->get_subdir($scfg, 'vztmpl'); |
b6cf0a66 DM |
272 | } |
273 | ||
568de3d1 DM |
274 | sub get_backup_dir { |
275 | my ($cfg, $storeid) = @_; | |
276 | ||
1dc01b9f DM |
277 | my $scfg = storage_config($cfg, $storeid); |
278 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 279 | |
1dc01b9f | 280 | return $plugin->get_subdir($scfg, 'backup'); |
b6cf0a66 DM |
281 | } |
282 | ||
283 | # library implementation | |
284 | ||
b6cf0a66 DM |
285 | sub parse_vmid { |
286 | my $vmid = shift; | |
287 | ||
288 | die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/; | |
289 | ||
290 | return int($vmid); | |
291 | } | |
292 | ||
ec4b0dc7 AD |
293 | sub parse_volname { |
294 | my ($cfg, $volid) = @_; | |
295 | ||
296 | my ($storeid, $volname) = parse_volume_id($volid); | |
297 | ||
298 | my $scfg = storage_config($cfg, $storeid); | |
299 | ||
300 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
a6f12626 DM |
301 | |
302 | # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) | |
303 | ||
ec4b0dc7 AD |
304 | return $plugin->parse_volname($volname); |
305 | } | |
306 | ||
b6cf0a66 DM |
307 | sub parse_volume_id { |
308 | my ($volid, $noerr) = @_; | |
309 | ||
a7f3d909 | 310 | return PVE::Storage::Plugin::parse_volume_id($volid, $noerr); |
b6cf0a66 DM |
311 | } |
312 | ||
ad27ee3e DM |
313 | sub volume_is_base { |
314 | my ($cfg, $volid) = @_; | |
315 | ||
316 | my ($sid, $volname) = parse_volume_id($volid, 1); | |
317 | return 0 if !$sid; | |
318 | ||
319 | if (my $scfg = $cfg->{ids}->{$sid}) { | |
320 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 321 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = |
ad27ee3e DM |
322 | $plugin->parse_volname($volname); |
323 | return $isBase ? 1 : 0; | |
324 | } else { | |
325 | # stale volid with undefined storage - so we can just guess | |
326 | if ($volid =~ m/base-/) { | |
327 | return 1; | |
328 | } | |
329 | } | |
330 | ||
331 | return 0; | |
332 | } | |
333 | ||
b6cf0a66 DM |
334 | # try to map a filesystem path to a volume identifier |
335 | sub path_to_volume_id { | |
336 | my ($cfg, $path) = @_; | |
337 | ||
338 | my $ids = $cfg->{ids}; | |
339 | ||
1dc01b9f | 340 | my ($sid, $volname) = parse_volume_id($path, 1); |
b6cf0a66 | 341 | if ($sid) { |
1dc01b9f | 342 | if (my $scfg = $ids->{$sid}) { |
188aca38 | 343 | if ($scfg->{path}) { |
1dc01b9f DM |
344 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
345 | my ($vtype, $name, $vmid) = $plugin->parse_volname($volname); | |
b6cf0a66 DM |
346 | return ($vtype, $path); |
347 | } | |
348 | } | |
349 | return (''); | |
350 | } | |
351 | ||
1a3459ac | 352 | # Note: abs_path() return undef if $path doesn not exist |
75d75990 DM |
353 | # for example when nfs storage is not mounted |
354 | $path = abs_path($path) || $path; | |
b6cf0a66 DM |
355 | |
356 | foreach my $sid (keys %$ids) { | |
1dc01b9f DM |
357 | my $scfg = $ids->{$sid}; |
358 | next if !$scfg->{path}; | |
359 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
360 | my $imagedir = $plugin->get_subdir($scfg, 'images'); | |
361 | my $isodir = $plugin->get_subdir($scfg, 'iso'); | |
362 | my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl'); | |
363 | my $backupdir = $plugin->get_subdir($scfg, 'backup'); | |
364 | my $privatedir = $plugin->get_subdir($scfg, 'rootdir'); | |
b6cf0a66 DM |
365 | |
366 | if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) { | |
367 | my $vmid = $1; | |
368 | my $name = $2; | |
fcbec654 DM |
369 | |
370 | my $vollist = $plugin->list_images($sid, $scfg, $vmid); | |
371 | foreach my $info (@$vollist) { | |
372 | my ($storeid, $volname) = parse_volume_id($info->{volid}); | |
373 | my $volpath = $plugin->path($scfg, $volname, $storeid); | |
374 | if ($volpath eq $path) { | |
375 | return ('images', $info->{volid}); | |
376 | } | |
377 | } | |
b6cf0a66 DM |
378 | } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) { |
379 | my $name = $1; | |
1a3459ac | 380 | return ('iso', "$sid:iso/$name"); |
b6cf0a66 DM |
381 | } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) { |
382 | my $name = $1; | |
383 | return ('vztmpl', "$sid:vztmpl/$name"); | |
1ac17c74 DM |
384 | } elsif ($path =~ m!^$privatedir/(\d+)$!) { |
385 | my $vmid = $1; | |
386 | return ('rootdir', "$sid:rootdir/$vmid"); | |
a22854e5 | 387 | } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) { |
568de3d1 | 388 | my $name = $1; |
1a3459ac | 389 | return ('iso', "$sid:backup/$name"); |
b6cf0a66 DM |
390 | } |
391 | } | |
392 | ||
393 | # can't map path to volume id | |
394 | return (''); | |
395 | } | |
396 | ||
397 | sub path { | |
207ea852 | 398 | my ($cfg, $volid, $snapname) = @_; |
b6cf0a66 | 399 | |
1dc01b9f | 400 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 | 401 | |
1dc01b9f | 402 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 | 403 | |
1dc01b9f | 404 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
207ea852 | 405 | my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname); |
2494896a | 406 | return wantarray ? ($path, $owner, $vtype) : $path; |
b6cf0a66 DM |
407 | } |
408 | ||
35fbb2e6 DM |
409 | sub abs_filesystem_path { |
410 | my ($cfg, $volid) = @_; | |
411 | ||
412 | my $path; | |
413 | if (PVE::Storage::parse_volume_id ($volid, 1)) { | |
414 | PVE::Storage::activate_volumes($cfg, [ $volid ]); | |
415 | $path = PVE::Storage::path($cfg, $volid); | |
416 | } else { | |
417 | if (-f $volid) { | |
418 | my $abspath = abs_path($volid); | |
419 | if ($abspath && $abspath =~ m|^(/.+)$|) { | |
420 | $path = $1; # untaint any path | |
421 | } | |
422 | } | |
423 | } | |
424 | ||
425 | die "can't find file '$volid'\n" if !($path && -f $path); | |
426 | ||
427 | return $path; | |
428 | } | |
429 | ||
b6cf0a66 DM |
430 | sub storage_migrate { |
431 | my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_; | |
432 | ||
6bf56298 | 433 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 DM |
434 | $target_volname = $volname if !$target_volname; |
435 | ||
6bf56298 | 436 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
437 | |
438 | # no need to migrate shared content | |
439 | return if $storeid eq $target_storeid && $scfg->{shared}; | |
440 | ||
6bf56298 | 441 | my $tcfg = storage_config($cfg, $target_storeid); |
b6cf0a66 DM |
442 | |
443 | my $target_volid = "${target_storeid}:${target_volname}"; | |
444 | ||
445 | my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'"; | |
446 | ||
45c2ee35 | 447 | my $sshoptions = "-o 'BatchMode=yes'"; |
b6cf0a66 DM |
448 | my $ssh = "/usr/bin/ssh $sshoptions"; |
449 | ||
450 | local $ENV{RSYNC_RSH} = $ssh; | |
451 | ||
1dc01b9f DM |
452 | # only implemented for file system based storage |
453 | if ($scfg->{path}) { | |
454 | if ($tcfg->{path}) { | |
b6cf0a66 | 455 | |
1dc01b9f DM |
456 | my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
457 | my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type}); | |
6bf56298 DM |
458 | my $src = $src_plugin->path($scfg, $volname, $storeid); |
459 | my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid); | |
b6cf0a66 | 460 | |
1dc01b9f | 461 | my $dirname = dirname($dst); |
b6cf0a66 DM |
462 | |
463 | if ($tcfg->{shared}) { # we can do a local copy | |
1a3459ac | 464 | |
f81372ac | 465 | run_command(['/bin/mkdir', '-p', $dirname]); |
b6cf0a66 | 466 | |
1dc01b9f | 467 | run_command(['/bin/cp', $src, $dst]); |
b6cf0a66 | 468 | |
1dc01b9f | 469 | } else { |
1a3459ac | 470 | run_command(['/usr/bin/ssh', "root\@${target_host}", |
1dc01b9f | 471 | '/bin/mkdir', '-p', $dirname]); |
b6cf0a66 | 472 | |
1dc01b9f DM |
473 | # we use rsync with --sparse, so we can't use --inplace, |
474 | # so we remove file on the target if it already exists to | |
475 | # save space | |
476 | my ($size, $format) = PVE::Storage::Plugin::file_size_info($src); | |
477 | if ($format && ($format eq 'raw') && $size) { | |
1a3459ac | 478 | run_command(['/usr/bin/ssh', "root\@${target_host}", |
1dc01b9f DM |
479 | 'rm', '-f', $dst], |
480 | outfunc => sub {}); | |
481 | } | |
b6cf0a66 | 482 | |
986303ca WL |
483 | my $cmd; |
484 | if ($format eq 'subvol') { | |
485 | $cmd = ['/usr/bin/rsync', '--progress', '-X', '-A', '--numeric-ids', | |
486 | '-aH', '--delete', '--no-whole-file', '--inplace', | |
487 | '--one-file-system', "$src/", "[root\@${target_host}]:$dst"]; | |
488 | } else { | |
489 | $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file', | |
490 | $src, "[root\@${target_host}]:$dst"]; | |
491 | } | |
b6cf0a66 | 492 | |
1dc01b9f | 493 | my $percent = -1; |
b6cf0a66 | 494 | |
1dc01b9f DM |
495 | run_command($cmd, outfunc => sub { |
496 | my $line = shift; | |
b6cf0a66 | 497 | |
1dc01b9f DM |
498 | if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) { |
499 | if ($2 > $percent) { | |
500 | $percent = $2; | |
501 | print "rsync status: $1\n"; | |
502 | *STDOUT->flush(); | |
503 | } | |
504 | } else { | |
505 | print "$line\n"; | |
506 | *STDOUT->flush(); | |
507 | } | |
508 | }); | |
509 | } | |
510 | } else { | |
511 | die "$errstr - target type '$tcfg->{type}' not implemented\n"; | |
512 | } | |
e0852ba7 | 513 | |
3d621977 WL |
514 | } elsif ($scfg->{type} eq 'zfspool') { |
515 | ||
e0852ba7 | 516 | if ($tcfg->{type} eq 'zfspool') { |
3d621977 | 517 | |
e0852ba7 DM |
518 | die "$errstr - pool on target has not same name as source!" |
519 | if $tcfg->{pool} ne $scfg->{pool}; | |
3d621977 | 520 | |
e0852ba7 | 521 | my (undef, $volname) = parse_volname($cfg, $volid); |
3d621977 WL |
522 | |
523 | my $zfspath = "$scfg->{pool}\/$volname"; | |
524 | ||
b650f029 | 525 | my $snap = ['zfs', 'snapshot', "$zfspath\@__migration__"]; |
3d621977 | 526 | |
b650f029 WL |
527 | my $send = [['zfs', 'send', '-Rpv', "$zfspath\@__migration__"], ['ssh', "root\@$target_host", |
528 | 'zfs', 'recv', $zfspath]]; | |
3d621977 | 529 | |
b650f029 | 530 | my $destroy_target = ['ssh', "root\@$target_host", 'zfs', 'destroy', "$zfspath\@__migration__"]; |
3d621977 | 531 | run_command($snap); |
e0852ba7 | 532 | eval{ |
3d621977 WL |
533 | run_command($send); |
534 | }; | |
535 | my $err; | |
536 | if ($err = $@){ | |
b650f029 | 537 | run_command(['zfs', 'destroy', "$zfspath\@__migration__"]); |
3d621977 | 538 | die $err; |
e0852ba7 DM |
539 | } |
540 | run_command($destroy_target); | |
3d621977 WL |
541 | |
542 | } else { | |
543 | die "$errstr - target type $tcfg->{type} is not valid\n"; | |
544 | } | |
1dc01b9f DM |
545 | } else { |
546 | die "$errstr - source type '$scfg->{type}' not implemented\n"; | |
b6cf0a66 DM |
547 | } |
548 | } | |
549 | ||
2502b33b | 550 | sub vdisk_clone { |
7bbc4004 | 551 | my ($cfg, $volid, $vmid, $snap) = @_; |
1a3459ac | 552 | |
2502b33b DM |
553 | my ($storeid, $volname) = parse_volume_id($volid); |
554 | ||
555 | my $scfg = storage_config($cfg, $storeid); | |
556 | ||
557 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 558 | |
2502b33b DM |
559 | activate_storage($cfg, $storeid); |
560 | ||
561 | # lock shared storage | |
562 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
7bbc4004 | 563 | my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap); |
2502b33b DM |
564 | return "$storeid:$volname"; |
565 | }); | |
566 | } | |
567 | ||
568 | sub vdisk_create_base { | |
569 | my ($cfg, $volid) = @_; | |
570 | ||
571 | my ($storeid, $volname) = parse_volume_id($volid); | |
572 | ||
573 | my $scfg = storage_config($cfg, $storeid); | |
574 | ||
575 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 576 | |
2502b33b DM |
577 | activate_storage($cfg, $storeid); |
578 | ||
579 | # lock shared storage | |
580 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
581 | my $volname = $plugin->create_base($storeid, $scfg, $volname); | |
582 | return "$storeid:$volname"; | |
583 | }); | |
584 | } | |
585 | ||
1dc01b9f DM |
586 | sub vdisk_alloc { |
587 | my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_; | |
b6cf0a66 | 588 | |
1dc01b9f | 589 | die "no storage id specified\n" if !$storeid; |
b6cf0a66 | 590 | |
1dc01b9f | 591 | PVE::JSONSchema::parse_storage_id($storeid); |
b6cf0a66 | 592 | |
1dc01b9f | 593 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 | 594 | |
1dc01b9f | 595 | die "no VMID specified\n" if !$vmid; |
b6cf0a66 | 596 | |
1dc01b9f | 597 | $vmid = parse_vmid($vmid); |
b6cf0a66 | 598 | |
1dc01b9f | 599 | my $defformat = PVE::Storage::Plugin::default_format($scfg); |
b6cf0a66 | 600 | |
1dc01b9f | 601 | $fmt = $defformat if !$fmt; |
b6cf0a66 | 602 | |
1dc01b9f | 603 | activate_storage($cfg, $storeid); |
3af60e62 | 604 | |
1dc01b9f | 605 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
b6cf0a66 | 606 | |
1dc01b9f DM |
607 | # lock shared storage |
608 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
afdfbe55 WB |
609 | my $old_umask = umask(umask|0037); |
610 | my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) }; | |
611 | my $err = $@; | |
612 | umask $old_umask; | |
613 | die $err if $err; | |
1dc01b9f DM |
614 | return "$storeid:$volname"; |
615 | }); | |
b6cf0a66 DM |
616 | } |
617 | ||
1dc01b9f DM |
618 | sub vdisk_free { |
619 | my ($cfg, $volid) = @_; | |
b6cf0a66 | 620 | |
1dc01b9f | 621 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 | 622 | |
1dc01b9f | 623 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 | 624 | |
1dc01b9f | 625 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
1a3459ac | 626 | |
1dc01b9f | 627 | activate_storage($cfg, $storeid); |
b6cf0a66 | 628 | |
1dc01b9f | 629 | my $cleanup_worker; |
b6cf0a66 | 630 | |
1dc01b9f DM |
631 | # lock shared storage |
632 | $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
32437ed2 | 633 | |
35533c68 | 634 | my ($vtype, $name, $vmid, undef, undef, $isBase, $format) = |
32437ed2 | 635 | $plugin->parse_volname($volname); |
32437ed2 DM |
636 | if ($isBase) { |
637 | my $vollist = $plugin->list_images($storeid, $scfg); | |
638 | foreach my $info (@$vollist) { | |
639 | my (undef, $tmpvolname) = parse_volume_id($info->{volid}); | |
f104d1dd AD |
640 | my $basename = undef; |
641 | my $basevmid = undef; | |
32437ed2 | 642 | |
f104d1dd | 643 | eval{ |
1a3459ac | 644 | (undef, undef, undef, $basename, $basevmid) = |
f104d1dd AD |
645 | $plugin->parse_volname($tmpvolname); |
646 | }; | |
32437ed2 | 647 | |
ff00afd7 | 648 | if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) { |
32437ed2 DM |
649 | die "base volume '$volname' is still in use " . |
650 | "(use by '$tmpvolname')\n"; | |
651 | } | |
652 | } | |
653 | } | |
35533c68 | 654 | $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format); |
1dc01b9f | 655 | }); |
b6cf0a66 | 656 | |
1dc01b9f | 657 | return if !$cleanup_worker; |
b6cf0a66 | 658 | |
1dc01b9f DM |
659 | my $rpcenv = PVE::RPCEnvironment::get(); |
660 | my $authuser = $rpcenv->get_user(); | |
b6cf0a66 | 661 | |
1dc01b9f | 662 | $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker); |
b6cf0a66 DM |
663 | } |
664 | ||
b6cf0a66 DM |
665 | #list iso or openvz template ($tt = <iso|vztmpl|backup>) |
666 | sub template_list { | |
667 | my ($cfg, $storeid, $tt) = @_; | |
668 | ||
1a3459ac DM |
669 | die "unknown template type '$tt'\n" |
670 | if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup'); | |
b6cf0a66 DM |
671 | |
672 | my $ids = $cfg->{ids}; | |
673 | ||
674 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
675 | ||
676 | my $res = {}; | |
677 | ||
678 | # query the storage | |
679 | ||
680 | foreach my $sid (keys %$ids) { | |
681 | next if $storeid && $storeid ne $sid; | |
682 | ||
683 | my $scfg = $ids->{$sid}; | |
684 | my $type = $scfg->{type}; | |
685 | ||
686 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
687 | ||
688 | next if $tt eq 'iso' && !$scfg->{content}->{iso}; | |
689 | next if $tt eq 'vztmpl' && !$scfg->{content}->{vztmpl}; | |
690 | next if $tt eq 'backup' && !$scfg->{content}->{backup}; | |
691 | ||
1dc01b9f | 692 | activate_storage($cfg, $sid); |
b6cf0a66 | 693 | |
1dc01b9f DM |
694 | if ($scfg->{path}) { |
695 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 696 | |
1dc01b9f | 697 | my $path = $plugin->get_subdir($scfg, $tt); |
b6cf0a66 DM |
698 | |
699 | foreach my $fn (<$path/*>) { | |
700 | ||
701 | my $info; | |
702 | ||
703 | if ($tt eq 'iso') { | |
704 | next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!; | |
705 | ||
706 | $info = { volid => "$sid:iso/$1", format => 'iso' }; | |
707 | ||
708 | } elsif ($tt eq 'vztmpl') { | |
13d2cb79 | 709 | next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!; |
b6cf0a66 | 710 | |
13d2cb79 | 711 | $info = { volid => "$sid:vztmpl/$1", format => "t$2" }; |
b6cf0a66 DM |
712 | |
713 | } elsif ($tt eq 'backup') { | |
a22854e5 | 714 | next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!; |
1a3459ac | 715 | |
b6cf0a66 DM |
716 | $info = { volid => "$sid:backup/$1", format => $2 }; |
717 | } | |
718 | ||
719 | $info->{size} = -s $fn; | |
720 | ||
721 | push @{$res->{$sid}}, $info; | |
722 | } | |
723 | ||
724 | } | |
725 | ||
726 | @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid}; | |
727 | } | |
728 | ||
729 | return $res; | |
730 | } | |
731 | ||
20ccac1b | 732 | |
b6cf0a66 DM |
733 | sub vdisk_list { |
734 | my ($cfg, $storeid, $vmid, $vollist) = @_; | |
735 | ||
736 | my $ids = $cfg->{ids}; | |
737 | ||
738 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
739 | ||
740 | my $res = {}; | |
741 | ||
742 | # prepare/activate/refresh all storages | |
743 | ||
b6cf0a66 DM |
744 | my $storage_list = []; |
745 | if ($vollist) { | |
746 | foreach my $volid (@$vollist) { | |
1dc01b9f DM |
747 | my ($sid, undef) = parse_volume_id($volid); |
748 | next if !defined($ids->{$sid}); | |
b6cf0a66 DM |
749 | next if !storage_check_enabled($cfg, $sid, undef, 1); |
750 | push @$storage_list, $sid; | |
b6cf0a66 DM |
751 | } |
752 | } else { | |
753 | foreach my $sid (keys %$ids) { | |
754 | next if $storeid && $storeid ne $sid; | |
755 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
756 | push @$storage_list, $sid; | |
b6cf0a66 DM |
757 | } |
758 | } | |
759 | ||
1dc01b9f | 760 | my $cache = {}; |
b6cf0a66 | 761 | |
1dc01b9f | 762 | activate_storage_list($cfg, $storage_list, $cache); |
b6cf0a66 DM |
763 | |
764 | foreach my $sid (keys %$ids) { | |
1dc01b9f DM |
765 | next if $storeid && $storeid ne $sid; |
766 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
b6cf0a66 | 767 | |
1dc01b9f DM |
768 | my $scfg = $ids->{$sid}; |
769 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
770 | $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache); | |
b6cf0a66 DM |
771 | @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid}; |
772 | } | |
773 | ||
774 | return $res; | |
775 | } | |
776 | ||
37ba0aea DM |
777 | sub volume_list { |
778 | my ($cfg, $storeid, $vmid, $content) = @_; | |
779 | ||
780 | my @ctypes = qw(images vztmpl iso backup); | |
781 | ||
782 | my $cts = $content ? [ $content ] : [ @ctypes ]; | |
783 | ||
784 | my $scfg = PVE::Storage::storage_config($cfg, $storeid); | |
785 | ||
786 | my $res = []; | |
787 | foreach my $ct (@$cts) { | |
788 | my $data; | |
789 | if ($ct eq 'images') { | |
790 | $data = vdisk_list($cfg, $storeid, $vmid); | |
791 | } elsif ($ct eq 'iso' && !defined($vmid)) { | |
792 | $data = template_list($cfg, $storeid, 'iso'); | |
793 | } elsif ($ct eq 'vztmpl'&& !defined($vmid)) { | |
794 | $data = template_list ($cfg, $storeid, 'vztmpl'); | |
795 | } elsif ($ct eq 'backup') { | |
796 | $data = template_list ($cfg, $storeid, 'backup'); | |
797 | foreach my $item (@{$data->{$storeid}}) { | |
798 | if (defined($vmid)) { | |
799 | @{$data->{$storeid}} = grep { $_->{volid} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}}; | |
800 | } | |
801 | } | |
802 | } | |
803 | ||
804 | next if !$data || !$data->{$storeid}; | |
805 | ||
806 | foreach my $item (@{$data->{$storeid}}) { | |
807 | $item->{content} = $ct; | |
808 | push @$res, $item; | |
809 | } | |
810 | } | |
811 | ||
812 | return $res; | |
813 | } | |
814 | ||
b6cf0a66 DM |
815 | sub uevent_seqnum { |
816 | ||
817 | my $filename = "/sys/kernel/uevent_seqnum"; | |
818 | ||
819 | my $seqnum = 0; | |
1dc01b9f | 820 | if (my $fh = IO::File->new($filename, "r")) { |
b6cf0a66 DM |
821 | my $line = <$fh>; |
822 | if ($line =~ m/^(\d+)$/) { | |
1dc01b9f | 823 | $seqnum = int($1); |
b6cf0a66 DM |
824 | } |
825 | close ($fh); | |
826 | } | |
827 | return $seqnum; | |
828 | } | |
829 | ||
f3d4ef46 | 830 | sub activate_storage { |
1dc01b9f | 831 | my ($cfg, $storeid, $cache) = @_; |
b6cf0a66 | 832 | |
f3d4ef46 DM |
833 | $cache = {} if !$cache; |
834 | ||
b6cf0a66 DM |
835 | my $scfg = storage_check_enabled($cfg, $storeid); |
836 | ||
1dc01b9f | 837 | return if $cache->{activated}->{$storeid}; |
b6cf0a66 | 838 | |
1dc01b9f | 839 | $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum}; |
b6cf0a66 | 840 | |
1dc01b9f | 841 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
b6cf0a66 | 842 | |
1dc01b9f DM |
843 | if ($scfg->{base}) { |
844 | my ($baseid, undef) = parse_volume_id ($scfg->{base}); | |
f3d4ef46 DM |
845 | activate_storage($cfg, $baseid, $cache); |
846 | } | |
847 | ||
848 | if (!$plugin->check_connection($storeid, $scfg)) { | |
849 | die "storage '$storeid' is not online\n"; | |
b6cf0a66 DM |
850 | } |
851 | ||
1dc01b9f DM |
852 | $plugin->activate_storage($storeid, $scfg, $cache); |
853 | ||
b6cf0a66 DM |
854 | my $newseq = uevent_seqnum (); |
855 | ||
856 | # only call udevsettle if there are events | |
1dc01b9f | 857 | if ($newseq > $cache->{uevent_seqnum}) { |
b6cf0a66 DM |
858 | my $timeout = 30; |
859 | system ("$UDEVADM settle --timeout=$timeout"); # ignore errors | |
1dc01b9f | 860 | $cache->{uevent_seqnum} = $newseq; |
b6cf0a66 DM |
861 | } |
862 | ||
1dc01b9f | 863 | $cache->{activated}->{$storeid} = 1; |
b6cf0a66 DM |
864 | } |
865 | ||
866 | sub activate_storage_list { | |
1dc01b9f | 867 | my ($cfg, $storeid_list, $cache) = @_; |
b6cf0a66 | 868 | |
1dc01b9f | 869 | $cache = {} if !$cache; |
b6cf0a66 DM |
870 | |
871 | foreach my $storeid (@$storeid_list) { | |
f3d4ef46 | 872 | activate_storage($cfg, $storeid, $cache); |
b6cf0a66 DM |
873 | } |
874 | } | |
875 | ||
1dc01b9f DM |
876 | sub deactivate_storage { |
877 | my ($cfg, $storeid) = @_; | |
878 | ||
879 | my $scfg = storage_config ($cfg, $storeid); | |
880 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 881 | |
1dc01b9f DM |
882 | my $cache = {}; |
883 | $plugin->deactivate_storage($storeid, $scfg, $cache); | |
b6cf0a66 DM |
884 | } |
885 | ||
886 | sub activate_volumes { | |
02e797b8 | 887 | my ($cfg, $vollist, $snapname) = @_; |
6703353b DM |
888 | |
889 | return if !($vollist && scalar(@$vollist)); | |
890 | ||
b6cf0a66 DM |
891 | my $storagehash = {}; |
892 | foreach my $volid (@$vollist) { | |
1dc01b9f | 893 | my ($storeid, undef) = parse_volume_id($volid); |
b6cf0a66 DM |
894 | $storagehash->{$storeid} = 1; |
895 | } | |
896 | ||
1dc01b9f DM |
897 | my $cache = {}; |
898 | ||
899 | activate_storage_list($cfg, [keys %$storagehash], $cache); | |
b6cf0a66 DM |
900 | |
901 | foreach my $volid (@$vollist) { | |
5521b580 | 902 | my ($storeid, $volname) = parse_volume_id($volid); |
1dc01b9f DM |
903 | my $scfg = storage_config($cfg, $storeid); |
904 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
02e797b8 | 905 | $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache); |
b6cf0a66 DM |
906 | } |
907 | } | |
908 | ||
909 | sub deactivate_volumes { | |
02e797b8 | 910 | my ($cfg, $vollist, $snapname) = @_; |
b6cf0a66 | 911 | |
6703353b DM |
912 | return if !($vollist && scalar(@$vollist)); |
913 | ||
1dc01b9f DM |
914 | my $cache = {}; |
915 | ||
6703353b | 916 | my @errlist = (); |
b6cf0a66 | 917 | foreach my $volid (@$vollist) { |
1dc01b9f | 918 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 | 919 | |
1dc01b9f DM |
920 | my $scfg = storage_config($cfg, $storeid); |
921 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 922 | |
1dc01b9f | 923 | eval { |
02e797b8 | 924 | $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache); |
1dc01b9f DM |
925 | }; |
926 | if (my $err = $@) { | |
927 | warn $err; | |
928 | push @errlist, $volid; | |
b6cf0a66 DM |
929 | } |
930 | } | |
6703353b DM |
931 | |
932 | die "volume deativation failed: " . join(' ', @errlist) | |
933 | if scalar(@errlist); | |
b6cf0a66 DM |
934 | } |
935 | ||
1a3459ac | 936 | sub storage_info { |
b6cf0a66 DM |
937 | my ($cfg, $content) = @_; |
938 | ||
939 | my $ids = $cfg->{ids}; | |
940 | ||
941 | my $info = {}; | |
583c2802 DM |
942 | |
943 | my @ctypes = PVE::Tools::split_list($content); | |
944 | ||
b6cf0a66 DM |
945 | my $slist = []; |
946 | foreach my $storeid (keys %$ids) { | |
947 | ||
b6cf0a66 DM |
948 | next if !storage_check_enabled($cfg, $storeid, undef, 1); |
949 | ||
d73060be DM |
950 | if (defined($content)) { |
951 | my $want_ctype = 0; | |
952 | foreach my $ctype (@ctypes) { | |
953 | if ($ids->{$storeid}->{content}->{$ctype}) { | |
954 | $want_ctype = 1; | |
955 | last; | |
956 | } | |
583c2802 | 957 | } |
d73060be | 958 | next if !$want_ctype; |
583c2802 | 959 | } |
d73060be | 960 | |
b6cf0a66 DM |
961 | my $type = $ids->{$storeid}->{type}; |
962 | ||
1a3459ac | 963 | $info->{$storeid} = { |
b6cf0a66 | 964 | type => $type, |
1a3459ac DM |
965 | total => 0, |
966 | avail => 0, | |
967 | used => 0, | |
04a2e4f3 | 968 | shared => $ids->{$storeid}->{shared} ? 1 : 0, |
1dc01b9f | 969 | content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}), |
b6cf0a66 DM |
970 | active => 0, |
971 | }; | |
972 | ||
b6cf0a66 DM |
973 | push @$slist, $storeid; |
974 | } | |
975 | ||
1dc01b9f | 976 | my $cache = {}; |
b6cf0a66 | 977 | |
b6cf0a66 DM |
978 | foreach my $storeid (keys %$ids) { |
979 | my $scfg = $ids->{$storeid}; | |
b6cf0a66 DM |
980 | next if !$info->{$storeid}; |
981 | ||
f3d4ef46 DM |
982 | eval { activate_storage($cfg, $storeid, $cache); }; |
983 | if (my $err = $@) { | |
984 | warn $err; | |
985 | next; | |
986 | } | |
987 | ||
1dc01b9f | 988 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
7028645e DM |
989 | my ($total, $avail, $used, $active); |
990 | eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); }; | |
991 | warn $@ if $@; | |
1dc01b9f | 992 | next if !$active; |
1a3459ac DM |
993 | $info->{$storeid}->{total} = $total; |
994 | $info->{$storeid}->{avail} = $avail; | |
995 | $info->{$storeid}->{used} = $used; | |
1dc01b9f | 996 | $info->{$storeid}->{active} = $active; |
b6cf0a66 DM |
997 | } |
998 | ||
999 | return $info; | |
1000 | } | |
1001 | ||
1002 | sub resolv_server { | |
1003 | my ($server) = @_; | |
1a3459ac | 1004 | |
c67daeac WB |
1005 | my ($packed_ip, $family); |
1006 | eval { | |
1007 | my @res = PVE::Tools::getaddrinfo_all($server); | |
1008 | $family = $res[0]->{family}; | |
1009 | $packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2]; | |
1010 | }; | |
b6cf0a66 | 1011 | if (defined $packed_ip) { |
ee302b1c | 1012 | return Socket::inet_ntop($family, $packed_ip); |
b6cf0a66 DM |
1013 | } |
1014 | return undef; | |
1015 | } | |
1016 | ||
1017 | sub scan_nfs { | |
1018 | my ($server_in) = @_; | |
1019 | ||
1020 | my $server; | |
1021 | if (!($server = resolv_server ($server_in))) { | |
1022 | die "unable to resolve address for server '${server_in}'\n"; | |
1023 | } | |
1024 | ||
1025 | my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server]; | |
1026 | ||
1027 | my $res = {}; | |
f81372ac | 1028 | run_command($cmd, outfunc => sub { |
b6cf0a66 DM |
1029 | my $line = shift; |
1030 | ||
1031 | # note: howto handle white spaces in export path?? | |
1032 | if ($line =~ m!^(/\S+)\s+(.+)$!) { | |
1033 | $res->{$1} = $2; | |
1034 | } | |
1035 | }); | |
1036 | ||
1037 | return $res; | |
1038 | } | |
1039 | ||
584d97f6 DM |
1040 | sub scan_zfs { |
1041 | ||
3932390b | 1042 | my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used']; |
584d97f6 DM |
1043 | |
1044 | my $res = []; | |
1045 | run_command($cmd, outfunc => sub { | |
1046 | my $line = shift; | |
1047 | ||
1048 | if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) { | |
3932390b | 1049 | my ($pool, $size_str, $used_str) = ($1, $2, $3); |
584d97f6 | 1050 | my $size = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($size_str); |
3932390b | 1051 | my $used = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($used_str); |
48e27f79 | 1052 | # ignore subvolumes generated by our ZFSPoolPlugin |
851658c3 WL |
1053 | return if $pool =~ m!/subvol-\d+-[^/]+$!; |
1054 | return if $pool =~ m!/basevol-\d+-[^/]+$!; | |
3932390b | 1055 | push @$res, { pool => $pool, size => $size, free => $size-$used }; |
584d97f6 DM |
1056 | } |
1057 | }); | |
1058 | ||
1059 | return $res; | |
1060 | } | |
1061 | ||
b6cf0a66 DM |
1062 | sub resolv_portal { |
1063 | my ($portal, $noerr) = @_; | |
1064 | ||
1689e627 WB |
1065 | my ($server, $port) = PVE::Tools::parse_host_and_port($portal); |
1066 | if ($server) { | |
b6cf0a66 DM |
1067 | if (my $ip = resolv_server($server)) { |
1068 | $server = $ip; | |
1689e627 | 1069 | $server = "[$server]" if $server =~ /^$IPV6RE$/; |
b6cf0a66 DM |
1070 | return $port ? "$server:$port" : $server; |
1071 | } | |
1072 | } | |
1073 | return undef if $noerr; | |
1074 | ||
1075 | raise_param_exc({ portal => "unable to resolve portal address '$portal'" }); | |
1076 | } | |
1077 | ||
1078 | # idea is from usbutils package (/usr/bin/usb-devices) script | |
1079 | sub __scan_usb_device { | |
1080 | my ($res, $devpath, $parent, $level) = @_; | |
1081 | ||
1082 | return if ! -d $devpath; | |
1083 | return if $level && $devpath !~ m/^.*[-.](\d+)$/; | |
1084 | my $port = $level ? int($1 - 1) : 0; | |
1085 | ||
1086 | my $busnum = int(file_read_firstline("$devpath/busnum")); | |
1087 | my $devnum = int(file_read_firstline("$devpath/devnum")); | |
1088 | ||
1089 | my $d = { | |
1090 | port => $port, | |
1091 | level => $level, | |
1092 | busnum => $busnum, | |
1093 | devnum => $devnum, | |
1094 | speed => file_read_firstline("$devpath/speed"), | |
1095 | class => hex(file_read_firstline("$devpath/bDeviceClass")), | |
1096 | vendid => file_read_firstline("$devpath/idVendor"), | |
1097 | prodid => file_read_firstline("$devpath/idProduct"), | |
1098 | }; | |
1099 | ||
1100 | if ($level) { | |
1101 | my $usbpath = $devpath; | |
1102 | $usbpath =~ s|^.*/\d+\-||; | |
1103 | $d->{usbpath} = $usbpath; | |
1104 | } | |
1105 | ||
1106 | my $product = file_read_firstline("$devpath/product"); | |
1107 | $d->{product} = $product if $product; | |
1a3459ac | 1108 | |
b6cf0a66 DM |
1109 | my $manu = file_read_firstline("$devpath/manufacturer"); |
1110 | $d->{manufacturer} = $manu if $manu; | |
1111 | ||
1112 | my $serial => file_read_firstline("$devpath/serial"); | |
1113 | $d->{serial} = $serial if $serial; | |
1114 | ||
1115 | push @$res, $d; | |
1116 | ||
1117 | foreach my $subdev (<$devpath/$busnum-*>) { | |
1118 | next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|; | |
1119 | __scan_usb_device($res, $subdev, $devnum, $level + 1); | |
1120 | } | |
1121 | ||
1122 | }; | |
1123 | ||
1124 | sub scan_usb { | |
1125 | ||
1126 | my $devlist = []; | |
1127 | ||
1128 | foreach my $device (</sys/bus/usb/devices/usb*>) { | |
1129 | __scan_usb_device($devlist, $device, 0, 0); | |
1130 | } | |
1131 | ||
1132 | return $devlist; | |
1133 | } | |
1134 | ||
1135 | sub scan_iscsi { | |
1136 | my ($portal_in) = @_; | |
1137 | ||
1138 | my $portal; | |
1dc01b9f | 1139 | if (!($portal = resolv_portal($portal_in))) { |
b6cf0a66 DM |
1140 | die "unable to parse/resolve portal address '${portal_in}'\n"; |
1141 | } | |
1142 | ||
1dc01b9f | 1143 | return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal); |
b6cf0a66 DM |
1144 | } |
1145 | ||
1146 | sub storage_default_format { | |
1147 | my ($cfg, $storeid) = @_; | |
1148 | ||
1149 | my $scfg = storage_config ($cfg, $storeid); | |
1150 | ||
1dc01b9f | 1151 | return PVE::Storage::Plugin::default_format($scfg); |
b6cf0a66 DM |
1152 | } |
1153 | ||
1154 | sub vgroup_is_used { | |
1155 | my ($cfg, $vgname) = @_; | |
1156 | ||
1157 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1158 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
1159 | if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) { |
1160 | return 1; | |
1161 | } | |
1162 | } | |
1163 | ||
1164 | return undef; | |
1165 | } | |
1166 | ||
1167 | sub target_is_used { | |
1168 | my ($cfg, $target) = @_; | |
1169 | ||
1170 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1171 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
1172 | if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) { |
1173 | return 1; | |
1174 | } | |
1175 | } | |
1176 | ||
1177 | return undef; | |
1178 | } | |
1179 | ||
1180 | sub volume_is_used { | |
1181 | my ($cfg, $volid) = @_; | |
1182 | ||
1183 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1184 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
1185 | if ($scfg->{base} && $scfg->{base} eq $volid) { |
1186 | return 1; | |
1187 | } | |
1188 | } | |
1189 | ||
1190 | return undef; | |
1191 | } | |
1192 | ||
1193 | sub storage_is_used { | |
1194 | my ($cfg, $storeid) = @_; | |
1195 | ||
1196 | foreach my $sid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1197 | my $scfg = storage_config($cfg, $sid); |
b6cf0a66 | 1198 | next if !$scfg->{base}; |
1dc01b9f | 1199 | my ($st) = parse_volume_id($scfg->{base}); |
b6cf0a66 DM |
1200 | return 1 if $st && $st eq $storeid; |
1201 | } | |
1202 | ||
1203 | return undef; | |
1204 | } | |
1205 | ||
1206 | sub foreach_volid { | |
1207 | my ($list, $func) = @_; | |
1208 | ||
1209 | return if !$list; | |
1210 | ||
1211 | foreach my $sid (keys %$list) { | |
1212 | foreach my $info (@{$list->{$sid}}) { | |
1213 | my $volid = $info->{volid}; | |
1dc01b9f | 1214 | my ($sid1, $volname) = parse_volume_id($volid, 1); |
b6cf0a66 DM |
1215 | if ($sid1 && $sid1 eq $sid) { |
1216 | &$func ($volid, $sid, $info); | |
1217 | } else { | |
1218 | warn "detected strange volid '$volid' in volume list for '$sid'\n"; | |
1219 | } | |
1220 | } | |
1221 | } | |
1222 | } | |
1223 | ||
8898dd7b FG |
1224 | sub extract_vzdump_config_tar { |
1225 | my ($archive, $conf_re) = @_; | |
1226 | ||
1227 | die "ERROR: file '$archive' does not exist\n" if ! -f $archive; | |
1228 | ||
1229 | my $pid = open(my $fh, '-|', 'tar', 'tf', $archive) || | |
1230 | die "unable to open file '$archive'\n"; | |
1231 | ||
1232 | my $file; | |
1233 | while (defined($file = <$fh>)) { | |
1234 | if ($file =~ m!$conf_re!) { | |
1235 | $file = $1; # untaint | |
1236 | last; | |
1237 | } | |
1238 | } | |
1239 | ||
1240 | kill 15, $pid; | |
1241 | waitpid $pid, 0; | |
1242 | close $fh; | |
1243 | ||
1244 | die "ERROR: archive contains no configuration file\n" if !$file; | |
1245 | chomp $file; | |
1246 | ||
1247 | my $raw = ''; | |
1248 | my $out = sub { | |
1249 | my $output = shift; | |
1250 | $raw .= "$output\n"; | |
1251 | }; | |
1252 | ||
1253 | PVE::Tools::run_command(['tar', '-xpOf', $archive, $file, '--occurrence'], outfunc => $out); | |
1254 | ||
1255 | return wantarray ? ($raw, $file) : $raw; | |
1256 | } | |
1257 | ||
1258 | sub extract_vzdump_config_vma { | |
1259 | my ($archive, $comp) = @_; | |
1260 | ||
1261 | my $cmd; | |
1262 | my $raw = ''; | |
1263 | my $out = sub { | |
1264 | my $output = shift; | |
1265 | $raw .= "$output\n"; | |
1266 | }; | |
1267 | ||
1268 | ||
1269 | if ($comp) { | |
1270 | my $uncomp; | |
1271 | if ($comp eq 'gz') { | |
1272 | $uncomp = ["zcat", $archive]; | |
1273 | } elsif ($comp eq 'lzo') { | |
1274 | $uncomp = ["lzop", "-d", "-c", $archive]; | |
1275 | } else { | |
1276 | die "unknown compression method '$comp'\n"; | |
1277 | } | |
1278 | $cmd = [$uncomp, ["vma", "config", "-"]]; | |
1279 | ||
1280 | # in some cases, lzop/zcat exits with 1 when its stdout pipe is | |
1281 | # closed early by vma, detect this and ignore the exit code later | |
1282 | my $broken_pipe; | |
1283 | my $errstring; | |
1284 | my $err = sub { | |
1285 | my $output = shift; | |
1286 | if ($output =~ m/lzop: Broken pipe: <stdout>/ || $output =~ m/gzip: stdout: Broken pipe/) { | |
1287 | $broken_pipe = 1; | |
1288 | } elsif (!defined ($errstring) && $output !~ m/^\s*$/) { | |
1289 | $errstring = "Failed to extract config from VMA archive: $output\n"; | |
1290 | } | |
1291 | }; | |
1292 | ||
1293 | # in other cases, the pipeline will exit with exit code 141 | |
1294 | # because of the broken pipe, handle / ignore this as well | |
1295 | my $rc; | |
1296 | eval { | |
1297 | $rc = PVE::Tools::run_command($cmd, outfunc => $out, errfunc => $err, noerr => 1); | |
1298 | }; | |
1299 | my $rerr = $@; | |
1300 | ||
1301 | # use exit code if no stderr output and not just broken pipe | |
1302 | if (!$errstring && !$broken_pipe && $rc > 0 && $rc != 141) { | |
1303 | die "$rerr\n" if $rerr; | |
1304 | die "config extraction failed with exit code $rc\n"; | |
1305 | } | |
1306 | die "$errstring\n" if $errstring; | |
1307 | } else { | |
1308 | # simple case without compression and weird piping behaviour | |
1309 | PVE::Tools::run_command(["vma", "config", $archive], outfunc => $out); | |
1310 | } | |
1311 | ||
1312 | return wantarray ? ($raw, undef) : $raw; | |
1313 | } | |
1314 | ||
1315 | sub extract_vzdump_config { | |
1316 | my ($cfg, $volid) = @_; | |
1317 | ||
1318 | my $archive = abs_filesystem_path($cfg, $volid); | |
1319 | ||
1320 | if ($volid =~ /\/vzdump-(lxc|openvz)-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|(tar(\.(gz|lzo))?))$/) { | |
1321 | return extract_vzdump_config_tar($archive,'^(\./etc/vzdump/(pct|vps)\.conf)$'); | |
1322 | } elsif ($volid =~ /\/vzdump-qemu-\d+-(\d{4})_(\d{2})_(\d{2})-(\d{2})_(\d{2})_(\d{2})\.(tgz|((tar|vma)(\.(gz|lzo))?))$/) { | |
1323 | my $format; | |
1324 | my $comp; | |
1325 | if ($7 eq 'tgz') { | |
1326 | $format = 'tar'; | |
1327 | $comp = 'gz'; | |
1328 | } else { | |
1329 | $format = $9; | |
1330 | $comp = $11 if defined($11); | |
1331 | } | |
1332 | ||
1333 | if ($format eq 'tar') { | |
1334 | return extract_vzdump_config_tar($archive, qr!\(\./qemu-server\.conf\)!); | |
1335 | } else { | |
1336 | return extract_vzdump_config_vma($archive, $comp); | |
1337 | } | |
1338 | } else { | |
1339 | die "cannot determine backup guest type for backup archive '$volid'\n"; | |
1340 | } | |
1341 | } | |
1342 | ||
f7621c01 DM |
1343 | # bash completion helper |
1344 | ||
1345 | sub complete_storage { | |
746e530f | 1346 | my ($cmdname, $pname, $cvalue) = @_; |
f7621c01 | 1347 | |
746e530f | 1348 | my $cfg = PVE::Storage::config(); |
180c8b02 | 1349 | |
746e530f | 1350 | return $cmdname eq 'add' ? [] : [ PVE::Storage::storage_ids($cfg) ]; |
f7621c01 DM |
1351 | } |
1352 | ||
1353 | sub complete_storage_enabled { | |
746e530f | 1354 | my ($cmdname, $pname, $cvalue) = @_; |
f7621c01 | 1355 | |
746e530f | 1356 | my $res = []; |
f7621c01 | 1357 | |
746e530f DM |
1358 | my $cfg = PVE::Storage::config(); |
1359 | foreach my $sid (keys %{$cfg->{ids}}) { | |
1360 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
1361 | push @$res, $sid; | |
1362 | } | |
1363 | return $res; | |
f7621c01 DM |
1364 | } |
1365 | ||
98437f4c DM |
1366 | sub complete_content_type { |
1367 | my ($cmdname, $pname, $cvalue) = @_; | |
1368 | ||
1369 | return [qw(rootdir images vztmpl iso backup)]; | |
1370 | } | |
1371 | ||
bf7aed26 DM |
1372 | sub complete_volume { |
1373 | my ($cmdname, $pname, $cvalue) = @_; | |
1374 | ||
1375 | my $cfg = config(); | |
1376 | ||
1377 | my $storage_list = complete_storage_enabled(); | |
1378 | ||
b70b0c58 DM |
1379 | if ($cvalue =~ m/^([^:]+):/) { |
1380 | $storage_list = [ $1 ]; | |
1381 | } else { | |
1382 | if (scalar(@$storage_list) > 1) { | |
1383 | # only list storage IDs to avoid large listings | |
1384 | my $res = []; | |
1385 | foreach my $storeid (@$storage_list) { | |
1386 | # Hack: simply return 2 artificial values, so that | |
1387 | # completions does not finish | |
1388 | push @$res, "$storeid:volname", "$storeid:..."; | |
1389 | } | |
1390 | return $res; | |
1391 | } | |
1392 | } | |
1393 | ||
bf7aed26 DM |
1394 | my $res = []; |
1395 | foreach my $storeid (@$storage_list) { | |
1396 | my $vollist = PVE::Storage::volume_list($cfg, $storeid); | |
1397 | ||
1398 | foreach my $item (@$vollist) { | |
1399 | push @$res, $item->{volid}; | |
1400 | } | |
1401 | } | |
1402 | ||
1403 | return $res; | |
1404 | } | |
1405 | ||
b6cf0a66 | 1406 | 1; |