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