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