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