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