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