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