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