]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage.pm
update changelog
[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 {
b6cf0a66 464
1a3459ac 465 run_command(['/usr/bin/ssh', "root\@${target_host}",
1dc01b9f 466 '/bin/mkdir', '-p', $dirname]);
b6cf0a66 467
1dc01b9f
DM
468 # we use rsync with --sparse, so we can't use --inplace,
469 # so we remove file on the target if it already exists to
470 # save space
471 my ($size, $format) = PVE::Storage::Plugin::file_size_info($src);
472 if ($format && ($format eq 'raw') && $size) {
1a3459ac 473 run_command(['/usr/bin/ssh', "root\@${target_host}",
1dc01b9f
DM
474 'rm', '-f', $dst],
475 outfunc => sub {});
476 }
b6cf0a66 477
1a3459ac 478 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
2d080bb8 479 $src, "[root\@${target_host}]:$dst"];
b6cf0a66 480
1dc01b9f 481 my $percent = -1;
b6cf0a66 482
1dc01b9f
DM
483 run_command($cmd, outfunc => sub {
484 my $line = shift;
b6cf0a66 485
1dc01b9f
DM
486 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
487 if ($2 > $percent) {
488 $percent = $2;
489 print "rsync status: $1\n";
490 *STDOUT->flush();
491 }
492 } else {
493 print "$line\n";
494 *STDOUT->flush();
495 }
496 });
497 }
498 } else {
499 die "$errstr - target type '$tcfg->{type}' not implemented\n";
500 }
e0852ba7 501
3d621977
WL
502 } elsif ($scfg->{type} eq 'zfspool') {
503
e0852ba7 504 if ($tcfg->{type} eq 'zfspool') {
3d621977 505
e0852ba7
DM
506 die "$errstr - pool on target has not same name as source!"
507 if $tcfg->{pool} ne $scfg->{pool};
3d621977 508
e0852ba7 509 my (undef, $volname) = parse_volname($cfg, $volid);
3d621977
WL
510
511 my $zfspath = "$scfg->{pool}\/$volname";
512
b650f029 513 my $snap = ['zfs', 'snapshot', "$zfspath\@__migration__"];
3d621977 514
b650f029
WL
515 my $send = [['zfs', 'send', '-Rpv', "$zfspath\@__migration__"], ['ssh', "root\@$target_host",
516 'zfs', 'recv', $zfspath]];
3d621977 517
b650f029 518 my $destroy_target = ['ssh', "root\@$target_host", 'zfs', 'destroy', "$zfspath\@__migration__"];
3d621977 519 run_command($snap);
e0852ba7 520 eval{
3d621977
WL
521 run_command($send);
522 };
523 my $err;
524 if ($err = $@){
b650f029 525 run_command(['zfs', 'destroy', "$zfspath\@__migration__"]);
3d621977 526 die $err;
e0852ba7
DM
527 }
528 run_command($destroy_target);
3d621977
WL
529
530 } else {
531 die "$errstr - target type $tcfg->{type} is not valid\n";
532 }
1dc01b9f
DM
533 } else {
534 die "$errstr - source type '$scfg->{type}' not implemented\n";
b6cf0a66
DM
535 }
536}
537
2502b33b 538sub vdisk_clone {
7bbc4004 539 my ($cfg, $volid, $vmid, $snap) = @_;
1a3459ac 540
2502b33b
DM
541 my ($storeid, $volname) = parse_volume_id($volid);
542
543 my $scfg = storage_config($cfg, $storeid);
544
545 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1a3459ac 546
2502b33b
DM
547 activate_storage($cfg, $storeid);
548
549 # lock shared storage
550 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
7bbc4004 551 my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap);
2502b33b
DM
552 return "$storeid:$volname";
553 });
554}
555
556sub vdisk_create_base {
557 my ($cfg, $volid) = @_;
558
559 my ($storeid, $volname) = parse_volume_id($volid);
560
561 my $scfg = storage_config($cfg, $storeid);
562
563 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1a3459ac 564
2502b33b
DM
565 activate_storage($cfg, $storeid);
566
567 # lock shared storage
568 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
569 my $volname = $plugin->create_base($storeid, $scfg, $volname);
570 return "$storeid:$volname";
571 });
572}
573
1dc01b9f
DM
574sub vdisk_alloc {
575 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
b6cf0a66 576
1dc01b9f 577 die "no storage id specified\n" if !$storeid;
b6cf0a66 578
1dc01b9f 579 PVE::JSONSchema::parse_storage_id($storeid);
b6cf0a66 580
1dc01b9f 581 my $scfg = storage_config($cfg, $storeid);
b6cf0a66 582
1dc01b9f 583 die "no VMID specified\n" if !$vmid;
b6cf0a66 584
1dc01b9f 585 $vmid = parse_vmid($vmid);
b6cf0a66 586
1dc01b9f 587 my $defformat = PVE::Storage::Plugin::default_format($scfg);
b6cf0a66 588
1dc01b9f 589 $fmt = $defformat if !$fmt;
b6cf0a66 590
1dc01b9f 591 activate_storage($cfg, $storeid);
3af60e62 592
1dc01b9f 593 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 594
1dc01b9f
DM
595 # lock shared storage
596 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
afdfbe55
WB
597 my $old_umask = umask(umask|0037);
598 my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) };
599 my $err = $@;
600 umask $old_umask;
601 die $err if $err;
1dc01b9f
DM
602 return "$storeid:$volname";
603 });
b6cf0a66
DM
604}
605
1dc01b9f
DM
606sub vdisk_free {
607 my ($cfg, $volid) = @_;
b6cf0a66 608
1dc01b9f 609 my ($storeid, $volname) = parse_volume_id($volid);
b6cf0a66 610
1dc01b9f 611 my $scfg = storage_config($cfg, $storeid);
b6cf0a66 612
1dc01b9f 613 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1a3459ac 614
1dc01b9f 615 activate_storage($cfg, $storeid);
b6cf0a66 616
1dc01b9f 617 my $cleanup_worker;
b6cf0a66 618
1dc01b9f
DM
619 # lock shared storage
620 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
32437ed2 621
35533c68 622 my ($vtype, $name, $vmid, undef, undef, $isBase, $format) =
32437ed2 623 $plugin->parse_volname($volname);
32437ed2
DM
624 if ($isBase) {
625 my $vollist = $plugin->list_images($storeid, $scfg);
626 foreach my $info (@$vollist) {
627 my (undef, $tmpvolname) = parse_volume_id($info->{volid});
f104d1dd
AD
628 my $basename = undef;
629 my $basevmid = undef;
32437ed2 630
f104d1dd 631 eval{
1a3459ac 632 (undef, undef, undef, $basename, $basevmid) =
f104d1dd
AD
633 $plugin->parse_volname($tmpvolname);
634 };
32437ed2 635
ff00afd7 636 if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) {
32437ed2
DM
637 die "base volume '$volname' is still in use " .
638 "(use by '$tmpvolname')\n";
639 }
640 }
641 }
35533c68 642 $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format);
1dc01b9f 643 });
b6cf0a66 644
1dc01b9f 645 return if !$cleanup_worker;
b6cf0a66 646
1dc01b9f
DM
647 my $rpcenv = PVE::RPCEnvironment::get();
648 my $authuser = $rpcenv->get_user();
b6cf0a66 649
1dc01b9f 650 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
b6cf0a66
DM
651}
652
b6cf0a66
DM
653#list iso or openvz template ($tt = <iso|vztmpl|backup>)
654sub template_list {
655 my ($cfg, $storeid, $tt) = @_;
656
1a3459ac
DM
657 die "unknown template type '$tt'\n"
658 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
b6cf0a66
DM
659
660 my $ids = $cfg->{ids};
661
662 storage_check_enabled($cfg, $storeid) if ($storeid);
663
664 my $res = {};
665
666 # query the storage
667
668 foreach my $sid (keys %$ids) {
669 next if $storeid && $storeid ne $sid;
670
671 my $scfg = $ids->{$sid};
672 my $type = $scfg->{type};
673
674 next if !storage_check_enabled($cfg, $sid, undef, 1);
675
676 next if $tt eq 'iso' && !$scfg->{content}->{iso};
677 next if $tt eq 'vztmpl' && !$scfg->{content}->{vztmpl};
678 next if $tt eq 'backup' && !$scfg->{content}->{backup};
679
1dc01b9f 680 activate_storage($cfg, $sid);
b6cf0a66 681
1dc01b9f
DM
682 if ($scfg->{path}) {
683 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 684
1dc01b9f 685 my $path = $plugin->get_subdir($scfg, $tt);
b6cf0a66
DM
686
687 foreach my $fn (<$path/*>) {
688
689 my $info;
690
691 if ($tt eq 'iso') {
692 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
693
694 $info = { volid => "$sid:iso/$1", format => 'iso' };
695
696 } elsif ($tt eq 'vztmpl') {
13d2cb79 697 next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!;
b6cf0a66 698
13d2cb79 699 $info = { volid => "$sid:vztmpl/$1", format => "t$2" };
b6cf0a66
DM
700
701 } elsif ($tt eq 'backup') {
a22854e5 702 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
1a3459ac 703
b6cf0a66
DM
704 $info = { volid => "$sid:backup/$1", format => $2 };
705 }
706
707 $info->{size} = -s $fn;
708
709 push @{$res->{$sid}}, $info;
710 }
711
712 }
713
714 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
715 }
716
717 return $res;
718}
719
20ccac1b 720
b6cf0a66
DM
721sub vdisk_list {
722 my ($cfg, $storeid, $vmid, $vollist) = @_;
723
724 my $ids = $cfg->{ids};
725
726 storage_check_enabled($cfg, $storeid) if ($storeid);
727
728 my $res = {};
729
730 # prepare/activate/refresh all storages
731
b6cf0a66
DM
732 my $storage_list = [];
733 if ($vollist) {
734 foreach my $volid (@$vollist) {
1dc01b9f
DM
735 my ($sid, undef) = parse_volume_id($volid);
736 next if !defined($ids->{$sid});
b6cf0a66
DM
737 next if !storage_check_enabled($cfg, $sid, undef, 1);
738 push @$storage_list, $sid;
b6cf0a66
DM
739 }
740 } else {
741 foreach my $sid (keys %$ids) {
742 next if $storeid && $storeid ne $sid;
743 next if !storage_check_enabled($cfg, $sid, undef, 1);
744 push @$storage_list, $sid;
b6cf0a66
DM
745 }
746 }
747
1dc01b9f 748 my $cache = {};
b6cf0a66 749
1dc01b9f 750 activate_storage_list($cfg, $storage_list, $cache);
b6cf0a66
DM
751
752 foreach my $sid (keys %$ids) {
1dc01b9f
DM
753 next if $storeid && $storeid ne $sid;
754 next if !storage_check_enabled($cfg, $sid, undef, 1);
b6cf0a66 755
1dc01b9f
DM
756 my $scfg = $ids->{$sid};
757 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
758 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
b6cf0a66
DM
759 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
760 }
761
762 return $res;
763}
764
37ba0aea
DM
765sub volume_list {
766 my ($cfg, $storeid, $vmid, $content) = @_;
767
768 my @ctypes = qw(images vztmpl iso backup);
769
770 my $cts = $content ? [ $content ] : [ @ctypes ];
771
772 my $scfg = PVE::Storage::storage_config($cfg, $storeid);
773
774 my $res = [];
775 foreach my $ct (@$cts) {
776 my $data;
777 if ($ct eq 'images') {
778 $data = vdisk_list($cfg, $storeid, $vmid);
779 } elsif ($ct eq 'iso' && !defined($vmid)) {
780 $data = template_list($cfg, $storeid, 'iso');
781 } elsif ($ct eq 'vztmpl'&& !defined($vmid)) {
782 $data = template_list ($cfg, $storeid, 'vztmpl');
783 } elsif ($ct eq 'backup') {
784 $data = template_list ($cfg, $storeid, 'backup');
785 foreach my $item (@{$data->{$storeid}}) {
786 if (defined($vmid)) {
787 @{$data->{$storeid}} = grep { $_->{volid} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}};
788 }
789 }
790 }
791
792 next if !$data || !$data->{$storeid};
793
794 foreach my $item (@{$data->{$storeid}}) {
795 $item->{content} = $ct;
796 push @$res, $item;
797 }
798 }
799
800 return $res;
801}
802
b6cf0a66
DM
803sub uevent_seqnum {
804
805 my $filename = "/sys/kernel/uevent_seqnum";
806
807 my $seqnum = 0;
1dc01b9f 808 if (my $fh = IO::File->new($filename, "r")) {
b6cf0a66
DM
809 my $line = <$fh>;
810 if ($line =~ m/^(\d+)$/) {
1dc01b9f 811 $seqnum = int($1);
b6cf0a66
DM
812 }
813 close ($fh);
814 }
815 return $seqnum;
816}
817
f3d4ef46 818sub activate_storage {
1dc01b9f 819 my ($cfg, $storeid, $cache) = @_;
b6cf0a66 820
f3d4ef46
DM
821 $cache = {} if !$cache;
822
b6cf0a66
DM
823 my $scfg = storage_check_enabled($cfg, $storeid);
824
1dc01b9f 825 return if $cache->{activated}->{$storeid};
b6cf0a66 826
1dc01b9f 827 $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum};
b6cf0a66 828
1dc01b9f 829 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 830
1dc01b9f
DM
831 if ($scfg->{base}) {
832 my ($baseid, undef) = parse_volume_id ($scfg->{base});
f3d4ef46
DM
833 activate_storage($cfg, $baseid, $cache);
834 }
835
836 if (!$plugin->check_connection($storeid, $scfg)) {
837 die "storage '$storeid' is not online\n";
b6cf0a66
DM
838 }
839
1dc01b9f
DM
840 $plugin->activate_storage($storeid, $scfg, $cache);
841
b6cf0a66
DM
842 my $newseq = uevent_seqnum ();
843
844 # only call udevsettle if there are events
1dc01b9f 845 if ($newseq > $cache->{uevent_seqnum}) {
b6cf0a66
DM
846 my $timeout = 30;
847 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
1dc01b9f 848 $cache->{uevent_seqnum} = $newseq;
b6cf0a66
DM
849 }
850
1dc01b9f 851 $cache->{activated}->{$storeid} = 1;
b6cf0a66
DM
852}
853
854sub activate_storage_list {
1dc01b9f 855 my ($cfg, $storeid_list, $cache) = @_;
b6cf0a66 856
1dc01b9f 857 $cache = {} if !$cache;
b6cf0a66
DM
858
859 foreach my $storeid (@$storeid_list) {
f3d4ef46 860 activate_storage($cfg, $storeid, $cache);
b6cf0a66
DM
861 }
862}
863
1dc01b9f
DM
864sub deactivate_storage {
865 my ($cfg, $storeid) = @_;
866
867 my $scfg = storage_config ($cfg, $storeid);
868 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 869
1dc01b9f
DM
870 my $cache = {};
871 $plugin->deactivate_storage($storeid, $scfg, $cache);
b6cf0a66
DM
872}
873
874sub activate_volumes {
02e797b8 875 my ($cfg, $vollist, $snapname) = @_;
6703353b
DM
876
877 return if !($vollist && scalar(@$vollist));
878
b6cf0a66
DM
879 my $storagehash = {};
880 foreach my $volid (@$vollist) {
1dc01b9f 881 my ($storeid, undef) = parse_volume_id($volid);
b6cf0a66
DM
882 $storagehash->{$storeid} = 1;
883 }
884
1dc01b9f
DM
885 my $cache = {};
886
887 activate_storage_list($cfg, [keys %$storagehash], $cache);
b6cf0a66
DM
888
889 foreach my $volid (@$vollist) {
5521b580 890 my ($storeid, $volname) = parse_volume_id($volid);
1dc01b9f
DM
891 my $scfg = storage_config($cfg, $storeid);
892 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
02e797b8 893 $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache);
b6cf0a66
DM
894 }
895}
896
897sub deactivate_volumes {
02e797b8 898 my ($cfg, $vollist, $snapname) = @_;
b6cf0a66 899
6703353b
DM
900 return if !($vollist && scalar(@$vollist));
901
1dc01b9f
DM
902 my $cache = {};
903
6703353b 904 my @errlist = ();
b6cf0a66 905 foreach my $volid (@$vollist) {
1dc01b9f 906 my ($storeid, $volname) = parse_volume_id($volid);
b6cf0a66 907
1dc01b9f
DM
908 my $scfg = storage_config($cfg, $storeid);
909 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
1a3459ac 910
1dc01b9f 911 eval {
02e797b8 912 $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache);
1dc01b9f
DM
913 };
914 if (my $err = $@) {
915 warn $err;
916 push @errlist, $volid;
b6cf0a66
DM
917 }
918 }
6703353b
DM
919
920 die "volume deativation failed: " . join(' ', @errlist)
921 if scalar(@errlist);
b6cf0a66
DM
922}
923
1a3459ac 924sub storage_info {
b6cf0a66
DM
925 my ($cfg, $content) = @_;
926
927 my $ids = $cfg->{ids};
928
929 my $info = {};
583c2802
DM
930
931 my @ctypes = PVE::Tools::split_list($content);
932
b6cf0a66
DM
933 my $slist = [];
934 foreach my $storeid (keys %$ids) {
935
b6cf0a66
DM
936 next if !storage_check_enabled($cfg, $storeid, undef, 1);
937
d73060be
DM
938 if (defined($content)) {
939 my $want_ctype = 0;
940 foreach my $ctype (@ctypes) {
941 if ($ids->{$storeid}->{content}->{$ctype}) {
942 $want_ctype = 1;
943 last;
944 }
583c2802 945 }
d73060be 946 next if !$want_ctype;
583c2802 947 }
d73060be 948
b6cf0a66
DM
949 my $type = $ids->{$storeid}->{type};
950
1a3459ac 951 $info->{$storeid} = {
b6cf0a66 952 type => $type,
1a3459ac
DM
953 total => 0,
954 avail => 0,
955 used => 0,
04a2e4f3 956 shared => $ids->{$storeid}->{shared} ? 1 : 0,
1dc01b9f 957 content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}),
b6cf0a66
DM
958 active => 0,
959 };
960
b6cf0a66
DM
961 push @$slist, $storeid;
962 }
963
1dc01b9f 964 my $cache = {};
b6cf0a66 965
b6cf0a66
DM
966 foreach my $storeid (keys %$ids) {
967 my $scfg = $ids->{$storeid};
b6cf0a66
DM
968 next if !$info->{$storeid};
969
f3d4ef46
DM
970 eval { activate_storage($cfg, $storeid, $cache); };
971 if (my $err = $@) {
972 warn $err;
973 next;
974 }
975
1dc01b9f 976 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
7028645e
DM
977 my ($total, $avail, $used, $active);
978 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
979 warn $@ if $@;
1dc01b9f 980 next if !$active;
1a3459ac
DM
981 $info->{$storeid}->{total} = $total;
982 $info->{$storeid}->{avail} = $avail;
983 $info->{$storeid}->{used} = $used;
1dc01b9f 984 $info->{$storeid}->{active} = $active;
b6cf0a66
DM
985 }
986
987 return $info;
988}
989
990sub resolv_server {
991 my ($server) = @_;
1a3459ac 992
c67daeac
WB
993 my ($packed_ip, $family);
994 eval {
995 my @res = PVE::Tools::getaddrinfo_all($server);
996 $family = $res[0]->{family};
997 $packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2];
998 };
b6cf0a66 999 if (defined $packed_ip) {
ee302b1c 1000 return Socket::inet_ntop($family, $packed_ip);
b6cf0a66
DM
1001 }
1002 return undef;
1003}
1004
1005sub scan_nfs {
1006 my ($server_in) = @_;
1007
1008 my $server;
1009 if (!($server = resolv_server ($server_in))) {
1010 die "unable to resolve address for server '${server_in}'\n";
1011 }
1012
1013 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
1014
1015 my $res = {};
f81372ac 1016 run_command($cmd, outfunc => sub {
b6cf0a66
DM
1017 my $line = shift;
1018
1019 # note: howto handle white spaces in export path??
1020 if ($line =~ m!^(/\S+)\s+(.+)$!) {
1021 $res->{$1} = $2;
1022 }
1023 });
1024
1025 return $res;
1026}
1027
584d97f6
DM
1028sub scan_zfs {
1029
3932390b 1030 my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used'];
584d97f6
DM
1031
1032 my $res = [];
1033 run_command($cmd, outfunc => sub {
1034 my $line = shift;
1035
1036 if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) {
3932390b 1037 my ($pool, $size_str, $used_str) = ($1, $2, $3);
584d97f6 1038 my $size = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($size_str);
3932390b 1039 my $used = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($used_str);
48e27f79
DM
1040 # ignore subvolumes generated by our ZFSPoolPlugin
1041 return if $pool =~ m!/subvol-\d+-[^/]+$!;
3932390b 1042 push @$res, { pool => $pool, size => $size, free => $size-$used };
584d97f6
DM
1043 }
1044 });
1045
1046 return $res;
1047}
1048
b6cf0a66
DM
1049sub resolv_portal {
1050 my ($portal, $noerr) = @_;
1051
1689e627
WB
1052 my ($server, $port) = PVE::Tools::parse_host_and_port($portal);
1053 if ($server) {
b6cf0a66
DM
1054 if (my $ip = resolv_server($server)) {
1055 $server = $ip;
1689e627 1056 $server = "[$server]" if $server =~ /^$IPV6RE$/;
b6cf0a66
DM
1057 return $port ? "$server:$port" : $server;
1058 }
1059 }
1060 return undef if $noerr;
1061
1062 raise_param_exc({ portal => "unable to resolve portal address '$portal'" });
1063}
1064
1065# idea is from usbutils package (/usr/bin/usb-devices) script
1066sub __scan_usb_device {
1067 my ($res, $devpath, $parent, $level) = @_;
1068
1069 return if ! -d $devpath;
1070 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
1071 my $port = $level ? int($1 - 1) : 0;
1072
1073 my $busnum = int(file_read_firstline("$devpath/busnum"));
1074 my $devnum = int(file_read_firstline("$devpath/devnum"));
1075
1076 my $d = {
1077 port => $port,
1078 level => $level,
1079 busnum => $busnum,
1080 devnum => $devnum,
1081 speed => file_read_firstline("$devpath/speed"),
1082 class => hex(file_read_firstline("$devpath/bDeviceClass")),
1083 vendid => file_read_firstline("$devpath/idVendor"),
1084 prodid => file_read_firstline("$devpath/idProduct"),
1085 };
1086
1087 if ($level) {
1088 my $usbpath = $devpath;
1089 $usbpath =~ s|^.*/\d+\-||;
1090 $d->{usbpath} = $usbpath;
1091 }
1092
1093 my $product = file_read_firstline("$devpath/product");
1094 $d->{product} = $product if $product;
1a3459ac 1095
b6cf0a66
DM
1096 my $manu = file_read_firstline("$devpath/manufacturer");
1097 $d->{manufacturer} = $manu if $manu;
1098
1099 my $serial => file_read_firstline("$devpath/serial");
1100 $d->{serial} = $serial if $serial;
1101
1102 push @$res, $d;
1103
1104 foreach my $subdev (<$devpath/$busnum-*>) {
1105 next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|;
1106 __scan_usb_device($res, $subdev, $devnum, $level + 1);
1107 }
1108
1109};
1110
1111sub scan_usb {
1112
1113 my $devlist = [];
1114
1115 foreach my $device (</sys/bus/usb/devices/usb*>) {
1116 __scan_usb_device($devlist, $device, 0, 0);
1117 }
1118
1119 return $devlist;
1120}
1121
1122sub scan_iscsi {
1123 my ($portal_in) = @_;
1124
1125 my $portal;
1dc01b9f 1126 if (!($portal = resolv_portal($portal_in))) {
b6cf0a66
DM
1127 die "unable to parse/resolve portal address '${portal_in}'\n";
1128 }
1129
1dc01b9f 1130 return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal);
b6cf0a66
DM
1131}
1132
1133sub storage_default_format {
1134 my ($cfg, $storeid) = @_;
1135
1136 my $scfg = storage_config ($cfg, $storeid);
1137
1dc01b9f 1138 return PVE::Storage::Plugin::default_format($scfg);
b6cf0a66
DM
1139}
1140
1141sub vgroup_is_used {
1142 my ($cfg, $vgname) = @_;
1143
1144 foreach my $storeid (keys %{$cfg->{ids}}) {
1dc01b9f 1145 my $scfg = storage_config($cfg, $storeid);
b6cf0a66
DM
1146 if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) {
1147 return 1;
1148 }
1149 }
1150
1151 return undef;
1152}
1153
1154sub target_is_used {
1155 my ($cfg, $target) = @_;
1156
1157 foreach my $storeid (keys %{$cfg->{ids}}) {
1dc01b9f 1158 my $scfg = storage_config($cfg, $storeid);
b6cf0a66
DM
1159 if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) {
1160 return 1;
1161 }
1162 }
1163
1164 return undef;
1165}
1166
1167sub volume_is_used {
1168 my ($cfg, $volid) = @_;
1169
1170 foreach my $storeid (keys %{$cfg->{ids}}) {
1dc01b9f 1171 my $scfg = storage_config($cfg, $storeid);
b6cf0a66
DM
1172 if ($scfg->{base} && $scfg->{base} eq $volid) {
1173 return 1;
1174 }
1175 }
1176
1177 return undef;
1178}
1179
1180sub storage_is_used {
1181 my ($cfg, $storeid) = @_;
1182
1183 foreach my $sid (keys %{$cfg->{ids}}) {
1dc01b9f 1184 my $scfg = storage_config($cfg, $sid);
b6cf0a66 1185 next if !$scfg->{base};
1dc01b9f 1186 my ($st) = parse_volume_id($scfg->{base});
b6cf0a66
DM
1187 return 1 if $st && $st eq $storeid;
1188 }
1189
1190 return undef;
1191}
1192
1193sub foreach_volid {
1194 my ($list, $func) = @_;
1195
1196 return if !$list;
1197
1198 foreach my $sid (keys %$list) {
1199 foreach my $info (@{$list->{$sid}}) {
1200 my $volid = $info->{volid};
1dc01b9f 1201 my ($sid1, $volname) = parse_volume_id($volid, 1);
b6cf0a66
DM
1202 if ($sid1 && $sid1 eq $sid) {
1203 &$func ($volid, $sid, $info);
1204 } else {
1205 warn "detected strange volid '$volid' in volume list for '$sid'\n";
1206 }
1207 }
1208 }
1209}
1210
f7621c01
DM
1211# bash completion helper
1212
1213sub complete_storage {
746e530f 1214 my ($cmdname, $pname, $cvalue) = @_;
f7621c01 1215
746e530f 1216 my $cfg = PVE::Storage::config();
180c8b02 1217
746e530f 1218 return $cmdname eq 'add' ? [] : [ PVE::Storage::storage_ids($cfg) ];
f7621c01
DM
1219}
1220
1221sub complete_storage_enabled {
746e530f 1222 my ($cmdname, $pname, $cvalue) = @_;
f7621c01 1223
746e530f 1224 my $res = [];
f7621c01 1225
746e530f
DM
1226 my $cfg = PVE::Storage::config();
1227 foreach my $sid (keys %{$cfg->{ids}}) {
1228 next if !storage_check_enabled($cfg, $sid, undef, 1);
1229 push @$res, $sid;
1230 }
1231 return $res;
f7621c01
DM
1232}
1233
98437f4c
DM
1234sub complete_content_type {
1235 my ($cmdname, $pname, $cvalue) = @_;
1236
1237 return [qw(rootdir images vztmpl iso backup)];
1238}
1239
bf7aed26
DM
1240sub complete_volume {
1241 my ($cmdname, $pname, $cvalue) = @_;
1242
1243 my $cfg = config();
1244
1245 my $storage_list = complete_storage_enabled();
1246
b70b0c58
DM
1247 if ($cvalue =~ m/^([^:]+):/) {
1248 $storage_list = [ $1 ];
1249 } else {
1250 if (scalar(@$storage_list) > 1) {
1251 # only list storage IDs to avoid large listings
1252 my $res = [];
1253 foreach my $storeid (@$storage_list) {
1254 # Hack: simply return 2 artificial values, so that
1255 # completions does not finish
1256 push @$res, "$storeid:volname", "$storeid:...";
1257 }
1258 return $res;
1259 }
1260 }
1261
bf7aed26
DM
1262 my $res = [];
1263 foreach my $storeid (@$storage_list) {
1264 my $vollist = PVE::Storage::volume_list($cfg, $storeid);
1265
1266 foreach my $item (@$vollist) {
1267 push @$res, $item->{volid};
1268 }
1269 }
1270
1271 return $res;
1272}
1273
b6cf0a66 12741;