]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage.pm
iscsidirect : add volume_snapshot_delete
[pve-storage.git] / PVE / Storage.pm
CommitLineData
b6cf0a66
DM
1package PVE::Storage;
2
3use strict;
4use POSIX;
5use IO::Select;
b6cf0a66 6use IO::File;
b6cf0a66
DM
7use File::Basename;
8use File::Path;
b6cf0a66 9use Cwd 'abs_path';
7a2d5c1a 10use Socket;
b6cf0a66 11
1dc01b9f
DM
12use PVE::Tools qw(run_command file_read_firstline);
13use PVE::Cluster qw(cfs_read_file cfs_lock_file);
b6cf0a66
DM
14use PVE::Exception qw(raise_param_exc);
15use PVE::JSONSchema;
16use PVE::INotify;
88c3abaf 17use PVE::RPCEnvironment;
b6cf0a66 18
1dc01b9f
DM
19use PVE::Storage::Plugin;
20use PVE::Storage::DirPlugin;
21use PVE::Storage::LVMPlugin;
22use PVE::Storage::NFSPlugin;
23use PVE::Storage::ISCSIPlugin;
0509010d 24use PVE::Storage::RBDPlugin;
caf1960c 25use PVE::Storage::SheepdogPlugin;
86616554
DM
26use PVE::Storage::ISCSIDirectPlugin;
27use PVE::Storage::NexentaPlugin;
b6cf0a66 28
1dc01b9f
DM
29# load and initialize all plugins
30PVE::Storage::DirPlugin->register();
31PVE::Storage::LVMPlugin->register();
32PVE::Storage::NFSPlugin->register();
33PVE::Storage::ISCSIPlugin->register();
0509010d 34PVE::Storage::RBDPlugin->register();
caf1960c 35PVE::Storage::SheepdogPlugin->register();
86616554
DM
36PVE::Storage::ISCSIDirectPlugin->register();
37PVE::Storage::NexentaPlugin->register();
1dc01b9f 38PVE::Storage::Plugin->init();
b6cf0a66 39
1dc01b9f 40my $UDEVADM = '/sbin/udevadm';
b6cf0a66 41
1dc01b9f 42# PVE::Storage utility functions
b6cf0a66
DM
43
44sub config {
45 return cfs_read_file("storage.cfg");
46}
47
b6cf0a66
DM
48sub lock_storage_config {
49 my ($code, $errmsg) = @_;
50
51 cfs_lock_file("storage.cfg", undef, $code);
52 my $err = $@;
53 if ($err) {
54 $errmsg ? die "$errmsg: $err" : die $err;
55 }
56}
57
b6cf0a66
DM
58sub storage_config {
59 my ($cfg, $storeid, $noerr) = @_;
60
61 die "no storage id specified\n" if !$storeid;
62
63 my $scfg = $cfg->{ids}->{$storeid};
64
65 die "storage '$storeid' does not exists\n" if (!$noerr && !$scfg);
66
67 return $scfg;
68}
69
70sub storage_check_node {
71 my ($cfg, $storeid, $node, $noerr) = @_;
72
1dc01b9f 73 my $scfg = storage_config($cfg, $storeid);
b6cf0a66
DM
74
75 if ($scfg->{nodes}) {
76 $node = PVE::INotify::nodename() if !$node || ($node eq 'localhost');
77 if (!$scfg->{nodes}->{$node}) {
da156fb3 78 die "storage '$storeid' is not available on node '$node'\n" if !$noerr;
b6cf0a66
DM
79 return undef;
80 }
81 }
82
83 return $scfg;
84}
85
86sub storage_check_enabled {
87 my ($cfg, $storeid, $node, $noerr) = @_;
88
1dc01b9f 89 my $scfg = storage_config($cfg, $storeid);
b6cf0a66
DM
90
91 if ($scfg->{disable}) {
92 die "storage '$storeid' is disabled\n" if !$noerr;
93 return undef;
94 }
95
96 return storage_check_node($cfg, $storeid, $node, $noerr);
97}
98
99sub storage_ids {
100 my ($cfg) = @_;
101
1dc01b9f 102 return keys %{$cfg->{ids}};
b6cf0a66
DM
103}
104
1dc01b9f
DM
105sub file_size_info {
106 my ($filename, $timeout) = @_;
b6cf0a66 107
1dc01b9f 108 return PVE::Storage::Plugin::file_size_info($filename, $timeout);
b6cf0a66
DM
109}
110
20ccac1b
AD
111sub volume_size_info {
112 my ($cfg, $volid, $timeout) = @_;
113
f18199e5
DM
114 my ($storeid, $volname) = parse_volume_id($volid, 1);
115 if ($storeid) {
116 my $scfg = storage_config($cfg, $storeid);
117 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
118 return $plugin->volume_size_info($scfg, $storeid, $volname, $timeout);
119 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
120 return file_size_info($volid, $timeout);
121 } else {
122 return 0;
123 }
20ccac1b
AD
124}
125
7e6c05dc
AD
126sub volume_resize {
127 my ($cfg, $volid, $size, $running) = @_;
128
129 my ($storeid, $volname) = parse_volume_id($volid, 1);
130 if ($storeid) {
131 my $scfg = storage_config($cfg, $storeid);
132 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
133 return $plugin->volume_resize($scfg, $storeid, $volname, $size, $running);
134 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
135 die "resize device is not possible";
136 } else {
137 die "can't resize";
138 }
139}
140
db60719c
AD
141sub volume_snapshot {
142 my ($cfg, $volid, $snap, $running) = @_;
143
144 my ($storeid, $volname) = parse_volume_id($volid, 1);
145 if ($storeid) {
146 my $scfg = storage_config($cfg, $storeid);
147 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
148 return $plugin->volume_snapshot($scfg, $storeid, $volname, $snap, $running);
149 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
150 die "snapshot device is not possible";
151 } else {
152 die "can't snapshot";
153 }
154}
155
22a2a633
AD
156sub volume_snapshot_rollback {
157 my ($cfg, $volid, $snap) = @_;
158
159 my ($storeid, $volname) = parse_volume_id($volid, 1);
160 if ($storeid) {
161 my $scfg = storage_config($cfg, $storeid);
162 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
163 return $plugin->volume_snapshot_rollback($scfg, $storeid, $volname, $snap);
164 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
165 die "snapshot rollback device is not possible";
166 } else {
167 die "can't snapshot";
168 }
169}
170
5753c9d1
AD
171sub volume_snapshot_delete {
172 my ($cfg, $volid, $snap, $running) = @_;
173
174 my ($storeid, $volname) = parse_volume_id($volid, 1);
175 if ($storeid) {
176 my $scfg = storage_config($cfg, $storeid);
177 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
178 return $plugin->volume_snapshot_rollback_delete($scfg, $storeid, $volname, $snap, $running);
179 } elsif ($volid =~ m|^(/.+)$| && -e $volid) {
180 die "snapshot delete device is not possible";
181 } else {
182 die "can't delete snapshot";
183 }
184}
185
1dc01b9f
DM
186sub get_image_dir {
187 my ($cfg, $storeid, $vmid) = @_;
b6cf0a66 188
1dc01b9f
DM
189 my $scfg = storage_config($cfg, $storeid);
190 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 191
1dc01b9f 192 my $path = $plugin->get_subdir($scfg, 'images');
b6cf0a66 193
1dc01b9f 194 return $vmid ? "$path/$vmid" : $path;
b6cf0a66
DM
195}
196
1dc01b9f 197sub get_private_dir {
b6cf0a66
DM
198 my ($cfg, $storeid, $vmid) = @_;
199
1dc01b9f
DM
200 my $scfg = storage_config($cfg, $storeid);
201 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 202
1dc01b9f 203 my $path = $plugin->get_subdir($scfg, 'rootdir');
d22a6133 204
1dc01b9f 205 return $vmid ? "$path/$vmid" : $path;
d22a6133
DM
206}
207
b6cf0a66
DM
208sub get_iso_dir {
209 my ($cfg, $storeid) = @_;
210
1dc01b9f
DM
211 my $scfg = storage_config($cfg, $storeid);
212 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 213
1dc01b9f 214 return $plugin->get_subdir($scfg, 'iso');
b6cf0a66
DM
215}
216
217sub get_vztmpl_dir {
218 my ($cfg, $storeid) = @_;
219
1dc01b9f
DM
220 my $scfg = storage_config($cfg, $storeid);
221 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 222
1dc01b9f 223 return $plugin->get_subdir($scfg, 'vztmpl');
b6cf0a66
DM
224}
225
568de3d1
DM
226sub get_backup_dir {
227 my ($cfg, $storeid) = @_;
228
1dc01b9f
DM
229 my $scfg = storage_config($cfg, $storeid);
230 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 231
1dc01b9f 232 return $plugin->get_subdir($scfg, 'backup');
b6cf0a66
DM
233}
234
235# library implementation
236
b6cf0a66
DM
237sub parse_vmid {
238 my $vmid = shift;
239
240 die "VMID '$vmid' contains illegal characters\n" if $vmid !~ m/^\d+$/;
241
242 return int($vmid);
243}
244
245PVE::JSONSchema::register_format('pve-volume-id', \&parse_volume_id);
246sub parse_volume_id {
247 my ($volid, $noerr) = @_;
248
249 if ($volid =~ m/^([a-z][a-z0-9\-\_\.]*[a-z0-9]):(.+)$/i) {
250 return wantarray ? ($1, $2) : $1;
251 }
252 return undef if $noerr;
253 die "unable to parse volume ID '$volid'\n";
254}
255
b6cf0a66
DM
256# try to map a filesystem path to a volume identifier
257sub path_to_volume_id {
258 my ($cfg, $path) = @_;
259
260 my $ids = $cfg->{ids};
261
1dc01b9f 262 my ($sid, $volname) = parse_volume_id($path, 1);
b6cf0a66 263 if ($sid) {
1dc01b9f
DM
264 if (my $scfg = $ids->{$sid}) {
265 if (my $path = $scfg->{path}) {
266 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
267 my ($vtype, $name, $vmid) = $plugin->parse_volname($volname);
b6cf0a66
DM
268 return ($vtype, $path);
269 }
270 }
271 return ('');
272 }
273
75d75990
DM
274 # Note: abs_path() return undef if $path doesn not exist
275 # for example when nfs storage is not mounted
276 $path = abs_path($path) || $path;
b6cf0a66
DM
277
278 foreach my $sid (keys %$ids) {
1dc01b9f
DM
279 my $scfg = $ids->{$sid};
280 next if !$scfg->{path};
281 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
282 my $imagedir = $plugin->get_subdir($scfg, 'images');
283 my $isodir = $plugin->get_subdir($scfg, 'iso');
284 my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl');
285 my $backupdir = $plugin->get_subdir($scfg, 'backup');
286 my $privatedir = $plugin->get_subdir($scfg, 'rootdir');
b6cf0a66
DM
287
288 if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) {
289 my $vmid = $1;
290 my $name = $2;
1dc01b9f 291 return ('images', "$sid:$vmid/$name");
b6cf0a66
DM
292 } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) {
293 my $name = $1;
294 return ('iso', "$sid:iso/$name");
295 } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) {
296 my $name = $1;
297 return ('vztmpl', "$sid:vztmpl/$name");
1ac17c74
DM
298 } elsif ($path =~ m!^$privatedir/(\d+)$!) {
299 my $vmid = $1;
300 return ('rootdir', "$sid:rootdir/$vmid");
88870923 301 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!) {
568de3d1
DM
302 my $name = $1;
303 return ('iso', "$sid:backup/$name");
b6cf0a66
DM
304 }
305 }
306
307 # can't map path to volume id
308 return ('');
309}
310
311sub path {
312 my ($cfg, $volid) = @_;
313
1dc01b9f 314 my ($storeid, $volname) = parse_volume_id($volid);
b6cf0a66 315
1dc01b9f 316 my $scfg = storage_config($cfg, $storeid);
b6cf0a66 317
1dc01b9f 318 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
e5427b00 319 my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid);
2494896a 320 return wantarray ? ($path, $owner, $vtype) : $path;
b6cf0a66
DM
321}
322
323sub storage_migrate {
324 my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_;
325
6bf56298 326 my ($storeid, $volname) = parse_volume_id($volid);
b6cf0a66
DM
327 $target_volname = $volname if !$target_volname;
328
6bf56298 329 my $scfg = storage_config($cfg, $storeid);
b6cf0a66
DM
330
331 # no need to migrate shared content
332 return if $storeid eq $target_storeid && $scfg->{shared};
333
6bf56298 334 my $tcfg = storage_config($cfg, $target_storeid);
b6cf0a66
DM
335
336 my $target_volid = "${target_storeid}:${target_volname}";
337
338 my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'";
339
340 # blowfish is a fast block cipher, much faster then 3des
341 my $sshoptions = "-c blowfish -o 'BatchMode=yes'";
342 my $ssh = "/usr/bin/ssh $sshoptions";
343
344 local $ENV{RSYNC_RSH} = $ssh;
345
1dc01b9f
DM
346 # only implemented for file system based storage
347 if ($scfg->{path}) {
348 if ($tcfg->{path}) {
b6cf0a66 349
1dc01b9f
DM
350 my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type});
351 my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type});
6bf56298
DM
352 my $src = $src_plugin->path($scfg, $volname, $storeid);
353 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
b6cf0a66 354
1dc01b9f 355 my $dirname = dirname($dst);
b6cf0a66
DM
356
357 if ($tcfg->{shared}) { # we can do a local copy
358
f81372ac 359 run_command(['/bin/mkdir', '-p', $dirname]);
b6cf0a66 360
1dc01b9f 361 run_command(['/bin/cp', $src, $dst]);
b6cf0a66 362
1dc01b9f 363 } else {
b6cf0a66 364
1dc01b9f
DM
365 run_command(['/usr/bin/ssh', "root\@${target_host}",
366 '/bin/mkdir', '-p', $dirname]);
b6cf0a66 367
1dc01b9f
DM
368 # we use rsync with --sparse, so we can't use --inplace,
369 # so we remove file on the target if it already exists to
370 # save space
371 my ($size, $format) = PVE::Storage::Plugin::file_size_info($src);
372 if ($format && ($format eq 'raw') && $size) {
373 run_command(['/usr/bin/ssh', "root\@${target_host}",
374 'rm', '-f', $dst],
375 outfunc => sub {});
376 }
b6cf0a66 377
1dc01b9f
DM
378 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
379 $src, "root\@${target_host}:$dst"];
b6cf0a66 380
1dc01b9f 381 my $percent = -1;
b6cf0a66 382
1dc01b9f
DM
383 run_command($cmd, outfunc => sub {
384 my $line = shift;
b6cf0a66 385
1dc01b9f
DM
386 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
387 if ($2 > $percent) {
388 $percent = $2;
389 print "rsync status: $1\n";
390 *STDOUT->flush();
391 }
392 } else {
393 print "$line\n";
394 *STDOUT->flush();
395 }
396 });
397 }
398 } else {
399 die "$errstr - target type '$tcfg->{type}' not implemented\n";
400 }
401 } else {
402 die "$errstr - source type '$scfg->{type}' not implemented\n";
b6cf0a66
DM
403 }
404}
405
1dc01b9f
DM
406sub vdisk_alloc {
407 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
b6cf0a66 408
1dc01b9f 409 die "no storage id specified\n" if !$storeid;
b6cf0a66 410
1dc01b9f 411 PVE::JSONSchema::parse_storage_id($storeid);
b6cf0a66 412
1dc01b9f 413 my $scfg = storage_config($cfg, $storeid);
b6cf0a66 414
1dc01b9f 415 die "no VMID specified\n" if !$vmid;
b6cf0a66 416
1dc01b9f 417 $vmid = parse_vmid($vmid);
b6cf0a66 418
1dc01b9f 419 my $defformat = PVE::Storage::Plugin::default_format($scfg);
b6cf0a66 420
1dc01b9f 421 $fmt = $defformat if !$fmt;
b6cf0a66 422
1dc01b9f 423 activate_storage($cfg, $storeid);
3af60e62 424
1dc01b9f 425 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 426
1dc01b9f
DM
427 # lock shared storage
428 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
429 my $volname = $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size);
430 return "$storeid:$volname";
431 });
b6cf0a66
DM
432}
433
1dc01b9f
DM
434sub vdisk_free {
435 my ($cfg, $volid) = @_;
b6cf0a66 436
1dc01b9f 437 my ($storeid, $volname) = parse_volume_id($volid);
b6cf0a66 438
1dc01b9f 439 my $scfg = storage_config($cfg, $storeid);
b6cf0a66 440
1dc01b9f
DM
441 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
442
443 activate_storage($cfg, $storeid);
b6cf0a66 444
1dc01b9f 445 my $cleanup_worker;
b6cf0a66 446
1dc01b9f
DM
447 # lock shared storage
448 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
449 my $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname);
450 });
b6cf0a66 451
1dc01b9f 452 return if !$cleanup_worker;
b6cf0a66 453
1dc01b9f
DM
454 my $rpcenv = PVE::RPCEnvironment::get();
455 my $authuser = $rpcenv->get_user();
b6cf0a66 456
1dc01b9f 457 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
b6cf0a66
DM
458}
459
b6cf0a66
DM
460#list iso or openvz template ($tt = <iso|vztmpl|backup>)
461sub template_list {
462 my ($cfg, $storeid, $tt) = @_;
463
1dc01b9f
DM
464 die "unknown template type '$tt'\n"
465 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
b6cf0a66
DM
466
467 my $ids = $cfg->{ids};
468
469 storage_check_enabled($cfg, $storeid) if ($storeid);
470
471 my $res = {};
472
473 # query the storage
474
475 foreach my $sid (keys %$ids) {
476 next if $storeid && $storeid ne $sid;
477
478 my $scfg = $ids->{$sid};
479 my $type = $scfg->{type};
480
481 next if !storage_check_enabled($cfg, $sid, undef, 1);
482
483 next if $tt eq 'iso' && !$scfg->{content}->{iso};
484 next if $tt eq 'vztmpl' && !$scfg->{content}->{vztmpl};
485 next if $tt eq 'backup' && !$scfg->{content}->{backup};
486
1dc01b9f 487 activate_storage($cfg, $sid);
b6cf0a66 488
1dc01b9f
DM
489 if ($scfg->{path}) {
490 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 491
1dc01b9f 492 my $path = $plugin->get_subdir($scfg, $tt);
b6cf0a66
DM
493
494 foreach my $fn (<$path/*>) {
495
496 my $info;
497
498 if ($tt eq 'iso') {
499 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
500
501 $info = { volid => "$sid:iso/$1", format => 'iso' };
502
503 } elsif ($tt eq 'vztmpl') {
504 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
505
506 $info = { volid => "$sid:vztmpl/$1", format => 'tgz' };
507
508 } elsif ($tt eq 'backup') {
88870923 509 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz))$!;
b6cf0a66
DM
510
511 $info = { volid => "$sid:backup/$1", format => $2 };
512 }
513
514 $info->{size} = -s $fn;
515
516 push @{$res->{$sid}}, $info;
517 }
518
519 }
520
521 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
522 }
523
524 return $res;
525}
526
20ccac1b 527
b6cf0a66
DM
528sub vdisk_list {
529 my ($cfg, $storeid, $vmid, $vollist) = @_;
530
531 my $ids = $cfg->{ids};
532
533 storage_check_enabled($cfg, $storeid) if ($storeid);
534
535 my $res = {};
536
537 # prepare/activate/refresh all storages
538
b6cf0a66
DM
539 my $storage_list = [];
540 if ($vollist) {
541 foreach my $volid (@$vollist) {
1dc01b9f
DM
542 my ($sid, undef) = parse_volume_id($volid);
543 next if !defined($ids->{$sid});
b6cf0a66
DM
544 next if !storage_check_enabled($cfg, $sid, undef, 1);
545 push @$storage_list, $sid;
b6cf0a66
DM
546 }
547 } else {
548 foreach my $sid (keys %$ids) {
549 next if $storeid && $storeid ne $sid;
550 next if !storage_check_enabled($cfg, $sid, undef, 1);
551 push @$storage_list, $sid;
b6cf0a66
DM
552 }
553 }
554
1dc01b9f 555 my $cache = {};
b6cf0a66 556
1dc01b9f 557 activate_storage_list($cfg, $storage_list, $cache);
b6cf0a66
DM
558
559 foreach my $sid (keys %$ids) {
1dc01b9f
DM
560 next if $storeid && $storeid ne $sid;
561 next if !storage_check_enabled($cfg, $sid, undef, 1);
b6cf0a66 562
1dc01b9f
DM
563 my $scfg = $ids->{$sid};
564 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
565 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
b6cf0a66
DM
566 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
567 }
568
569 return $res;
570}
571
b6cf0a66
DM
572sub uevent_seqnum {
573
574 my $filename = "/sys/kernel/uevent_seqnum";
575
576 my $seqnum = 0;
1dc01b9f 577 if (my $fh = IO::File->new($filename, "r")) {
b6cf0a66
DM
578 my $line = <$fh>;
579 if ($line =~ m/^(\d+)$/) {
1dc01b9f 580 $seqnum = int($1);
b6cf0a66
DM
581 }
582 close ($fh);
583 }
584 return $seqnum;
585}
586
f3d4ef46 587sub activate_storage {
1dc01b9f 588 my ($cfg, $storeid, $cache) = @_;
b6cf0a66 589
f3d4ef46
DM
590 $cache = {} if !$cache;
591
b6cf0a66
DM
592 my $scfg = storage_check_enabled($cfg, $storeid);
593
1dc01b9f 594 return if $cache->{activated}->{$storeid};
b6cf0a66 595
1dc01b9f 596 $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum};
b6cf0a66 597
1dc01b9f 598 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 599
1dc01b9f
DM
600 if ($scfg->{base}) {
601 my ($baseid, undef) = parse_volume_id ($scfg->{base});
f3d4ef46
DM
602 activate_storage($cfg, $baseid, $cache);
603 }
604
605 if (!$plugin->check_connection($storeid, $scfg)) {
606 die "storage '$storeid' is not online\n";
b6cf0a66
DM
607 }
608
1dc01b9f
DM
609 $plugin->activate_storage($storeid, $scfg, $cache);
610
b6cf0a66
DM
611 my $newseq = uevent_seqnum ();
612
613 # only call udevsettle if there are events
1dc01b9f 614 if ($newseq > $cache->{uevent_seqnum}) {
b6cf0a66
DM
615 my $timeout = 30;
616 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
1dc01b9f 617 $cache->{uevent_seqnum} = $newseq;
b6cf0a66
DM
618 }
619
1dc01b9f 620 $cache->{activated}->{$storeid} = 1;
b6cf0a66
DM
621}
622
623sub activate_storage_list {
1dc01b9f 624 my ($cfg, $storeid_list, $cache) = @_;
b6cf0a66 625
1dc01b9f 626 $cache = {} if !$cache;
b6cf0a66
DM
627
628 foreach my $storeid (@$storeid_list) {
f3d4ef46 629 activate_storage($cfg, $storeid, $cache);
b6cf0a66
DM
630 }
631}
632
1dc01b9f
DM
633sub deactivate_storage {
634 my ($cfg, $storeid) = @_;
635
636 my $scfg = storage_config ($cfg, $storeid);
637 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 638
1dc01b9f
DM
639 my $cache = {};
640 $plugin->deactivate_storage($storeid, $scfg, $cache);
b6cf0a66
DM
641}
642
643sub activate_volumes {
6703353b
DM
644 my ($cfg, $vollist, $exclusive) = @_;
645
646 return if !($vollist && scalar(@$vollist));
647
b6cf0a66
DM
648 my $storagehash = {};
649 foreach my $volid (@$vollist) {
1dc01b9f 650 my ($storeid, undef) = parse_volume_id($volid);
b6cf0a66
DM
651 $storagehash->{$storeid} = 1;
652 }
653
1dc01b9f
DM
654 my $cache = {};
655
656 activate_storage_list($cfg, [keys %$storagehash], $cache);
b6cf0a66
DM
657
658 foreach my $volid (@$vollist) {
5521b580 659 my ($storeid, $volname) = parse_volume_id($volid);
1dc01b9f
DM
660 my $scfg = storage_config($cfg, $storeid);
661 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
662 $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache);
b6cf0a66
DM
663 }
664}
665
666sub deactivate_volumes {
667 my ($cfg, $vollist) = @_;
668
6703353b
DM
669 return if !($vollist && scalar(@$vollist));
670
1dc01b9f
DM
671 my $cache = {};
672
6703353b 673 my @errlist = ();
b6cf0a66 674 foreach my $volid (@$vollist) {
1dc01b9f 675 my ($storeid, $volname) = parse_volume_id($volid);
b6cf0a66 676
1dc01b9f
DM
677 my $scfg = storage_config($cfg, $storeid);
678 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
679
680 eval {
681 $plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
682 };
683 if (my $err = $@) {
684 warn $err;
685 push @errlist, $volid;
b6cf0a66
DM
686 }
687 }
6703353b
DM
688
689 die "volume deativation failed: " . join(' ', @errlist)
690 if scalar(@errlist);
b6cf0a66
DM
691}
692
b6cf0a66
DM
693sub storage_info {
694 my ($cfg, $content) = @_;
695
696 my $ids = $cfg->{ids};
697
698 my $info = {};
b6cf0a66
DM
699
700 my $slist = [];
701 foreach my $storeid (keys %$ids) {
702
703 next if $content && !$ids->{$storeid}->{content}->{$content};
704
705 next if !storage_check_enabled($cfg, $storeid, undef, 1);
706
707 my $type = $ids->{$storeid}->{type};
708
709 $info->{$storeid} = {
710 type => $type,
711 total => 0,
712 avail => 0,
713 used => 0,
04a2e4f3 714 shared => $ids->{$storeid}->{shared} ? 1 : 0,
1dc01b9f 715 content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}),
b6cf0a66
DM
716 active => 0,
717 };
718
b6cf0a66
DM
719 push @$slist, $storeid;
720 }
721
1dc01b9f 722 my $cache = {};
b6cf0a66 723
b6cf0a66
DM
724 foreach my $storeid (keys %$ids) {
725 my $scfg = $ids->{$storeid};
b6cf0a66
DM
726 next if !$info->{$storeid};
727
f3d4ef46
DM
728 eval { activate_storage($cfg, $storeid, $cache); };
729 if (my $err = $@) {
730 warn $err;
731 next;
732 }
733
1dc01b9f 734 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
7028645e
DM
735 my ($total, $avail, $used, $active);
736 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
737 warn $@ if $@;
1dc01b9f
DM
738 next if !$active;
739 $info->{$storeid}->{total} = $total;
740 $info->{$storeid}->{avail} = $avail;
741 $info->{$storeid}->{used} = $used;
742 $info->{$storeid}->{active} = $active;
b6cf0a66
DM
743 }
744
745 return $info;
746}
747
748sub resolv_server {
749 my ($server) = @_;
750
751 my $packed_ip = gethostbyname($server);
752 if (defined $packed_ip) {
753 return inet_ntoa($packed_ip);
754 }
755 return undef;
756}
757
758sub scan_nfs {
759 my ($server_in) = @_;
760
761 my $server;
762 if (!($server = resolv_server ($server_in))) {
763 die "unable to resolve address for server '${server_in}'\n";
764 }
765
766 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
767
768 my $res = {};
f81372ac 769 run_command($cmd, outfunc => sub {
b6cf0a66
DM
770 my $line = shift;
771
772 # note: howto handle white spaces in export path??
773 if ($line =~ m!^(/\S+)\s+(.+)$!) {
774 $res->{$1} = $2;
775 }
776 });
777
778 return $res;
779}
780
781sub resolv_portal {
782 my ($portal, $noerr) = @_;
783
784 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
785 my $server = $1;
786 my $port = $3;
787
788 if (my $ip = resolv_server($server)) {
789 $server = $ip;
790 return $port ? "$server:$port" : $server;
791 }
792 }
793 return undef if $noerr;
794
795 raise_param_exc({ portal => "unable to resolve portal address '$portal'" });
796}
797
798# idea is from usbutils package (/usr/bin/usb-devices) script
799sub __scan_usb_device {
800 my ($res, $devpath, $parent, $level) = @_;
801
802 return if ! -d $devpath;
803 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
804 my $port = $level ? int($1 - 1) : 0;
805
806 my $busnum = int(file_read_firstline("$devpath/busnum"));
807 my $devnum = int(file_read_firstline("$devpath/devnum"));
808
809 my $d = {
810 port => $port,
811 level => $level,
812 busnum => $busnum,
813 devnum => $devnum,
814 speed => file_read_firstline("$devpath/speed"),
815 class => hex(file_read_firstline("$devpath/bDeviceClass")),
816 vendid => file_read_firstline("$devpath/idVendor"),
817 prodid => file_read_firstline("$devpath/idProduct"),
818 };
819
820 if ($level) {
821 my $usbpath = $devpath;
822 $usbpath =~ s|^.*/\d+\-||;
823 $d->{usbpath} = $usbpath;
824 }
825
826 my $product = file_read_firstline("$devpath/product");
827 $d->{product} = $product if $product;
828
829 my $manu = file_read_firstline("$devpath/manufacturer");
830 $d->{manufacturer} = $manu if $manu;
831
832 my $serial => file_read_firstline("$devpath/serial");
833 $d->{serial} = $serial if $serial;
834
835 push @$res, $d;
836
837 foreach my $subdev (<$devpath/$busnum-*>) {
838 next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|;
839 __scan_usb_device($res, $subdev, $devnum, $level + 1);
840 }
841
842};
843
844sub scan_usb {
845
846 my $devlist = [];
847
848 foreach my $device (</sys/bus/usb/devices/usb*>) {
849 __scan_usb_device($devlist, $device, 0, 0);
850 }
851
852 return $devlist;
853}
854
855sub scan_iscsi {
856 my ($portal_in) = @_;
857
858 my $portal;
1dc01b9f 859 if (!($portal = resolv_portal($portal_in))) {
b6cf0a66
DM
860 die "unable to parse/resolve portal address '${portal_in}'\n";
861 }
862
1dc01b9f 863 return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal);
b6cf0a66
DM
864}
865
866sub storage_default_format {
867 my ($cfg, $storeid) = @_;
868
869 my $scfg = storage_config ($cfg, $storeid);
870
1dc01b9f 871 return PVE::Storage::Plugin::default_format($scfg);
b6cf0a66
DM
872}
873
874sub vgroup_is_used {
875 my ($cfg, $vgname) = @_;
876
877 foreach my $storeid (keys %{$cfg->{ids}}) {
1dc01b9f 878 my $scfg = storage_config($cfg, $storeid);
b6cf0a66
DM
879 if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) {
880 return 1;
881 }
882 }
883
884 return undef;
885}
886
887sub target_is_used {
888 my ($cfg, $target) = @_;
889
890 foreach my $storeid (keys %{$cfg->{ids}}) {
1dc01b9f 891 my $scfg = storage_config($cfg, $storeid);
b6cf0a66
DM
892 if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) {
893 return 1;
894 }
895 }
896
897 return undef;
898}
899
900sub volume_is_used {
901 my ($cfg, $volid) = @_;
902
903 foreach my $storeid (keys %{$cfg->{ids}}) {
1dc01b9f 904 my $scfg = storage_config($cfg, $storeid);
b6cf0a66
DM
905 if ($scfg->{base} && $scfg->{base} eq $volid) {
906 return 1;
907 }
908 }
909
910 return undef;
911}
912
913sub storage_is_used {
914 my ($cfg, $storeid) = @_;
915
916 foreach my $sid (keys %{$cfg->{ids}}) {
1dc01b9f 917 my $scfg = storage_config($cfg, $sid);
b6cf0a66 918 next if !$scfg->{base};
1dc01b9f 919 my ($st) = parse_volume_id($scfg->{base});
b6cf0a66
DM
920 return 1 if $st && $st eq $storeid;
921 }
922
923 return undef;
924}
925
926sub foreach_volid {
927 my ($list, $func) = @_;
928
929 return if !$list;
930
931 foreach my $sid (keys %$list) {
932 foreach my $info (@{$list->{$sid}}) {
933 my $volid = $info->{volid};
1dc01b9f 934 my ($sid1, $volname) = parse_volume_id($volid, 1);
b6cf0a66
DM
935 if ($sid1 && $sid1 eq $sid) {
936 &$func ($volid, $sid, $info);
937 } else {
938 warn "detected strange volid '$volid' in volume list for '$sid'\n";
939 }
940 }
941 }
942}
943
9441;