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