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