]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage.pm
fix typo
[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});
27cc55d4 178 return $plugin->volume_snapshot_delete($scfg, $storeid, $volname, $snap, $running);
5753c9d1
AD
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");
a22854e5 301 } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) {
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
45c2ee35 340 my $sshoptions = "-o 'BatchMode=yes'";
b6cf0a66
DM
341 my $ssh = "/usr/bin/ssh $sshoptions";
342
343 local $ENV{RSYNC_RSH} = $ssh;
344
1dc01b9f
DM
345 # only implemented for file system based storage
346 if ($scfg->{path}) {
347 if ($tcfg->{path}) {
b6cf0a66 348
1dc01b9f
DM
349 my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type});
350 my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type});
6bf56298
DM
351 my $src = $src_plugin->path($scfg, $volname, $storeid);
352 my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid);
b6cf0a66 353
1dc01b9f 354 my $dirname = dirname($dst);
b6cf0a66
DM
355
356 if ($tcfg->{shared}) { # we can do a local copy
357
f81372ac 358 run_command(['/bin/mkdir', '-p', $dirname]);
b6cf0a66 359
1dc01b9f 360 run_command(['/bin/cp', $src, $dst]);
b6cf0a66 361
1dc01b9f 362 } else {
b6cf0a66 363
1dc01b9f
DM
364 run_command(['/usr/bin/ssh', "root\@${target_host}",
365 '/bin/mkdir', '-p', $dirname]);
b6cf0a66 366
1dc01b9f
DM
367 # we use rsync with --sparse, so we can't use --inplace,
368 # so we remove file on the target if it already exists to
369 # save space
370 my ($size, $format) = PVE::Storage::Plugin::file_size_info($src);
371 if ($format && ($format eq 'raw') && $size) {
372 run_command(['/usr/bin/ssh', "root\@${target_host}",
373 'rm', '-f', $dst],
374 outfunc => sub {});
375 }
b6cf0a66 376
1dc01b9f
DM
377 my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file',
378 $src, "root\@${target_host}:$dst"];
b6cf0a66 379
1dc01b9f 380 my $percent = -1;
b6cf0a66 381
1dc01b9f
DM
382 run_command($cmd, outfunc => sub {
383 my $line = shift;
b6cf0a66 384
1dc01b9f
DM
385 if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) {
386 if ($2 > $percent) {
387 $percent = $2;
388 print "rsync status: $1\n";
389 *STDOUT->flush();
390 }
391 } else {
392 print "$line\n";
393 *STDOUT->flush();
394 }
395 });
396 }
397 } else {
398 die "$errstr - target type '$tcfg->{type}' not implemented\n";
399 }
400 } else {
401 die "$errstr - source type '$scfg->{type}' not implemented\n";
b6cf0a66
DM
402 }
403}
404
1dc01b9f
DM
405sub vdisk_alloc {
406 my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_;
b6cf0a66 407
1dc01b9f 408 die "no storage id specified\n" if !$storeid;
b6cf0a66 409
1dc01b9f 410 PVE::JSONSchema::parse_storage_id($storeid);
b6cf0a66 411
1dc01b9f 412 my $scfg = storage_config($cfg, $storeid);
b6cf0a66 413
1dc01b9f 414 die "no VMID specified\n" if !$vmid;
b6cf0a66 415
1dc01b9f 416 $vmid = parse_vmid($vmid);
b6cf0a66 417
1dc01b9f 418 my $defformat = PVE::Storage::Plugin::default_format($scfg);
b6cf0a66 419
1dc01b9f 420 $fmt = $defformat if !$fmt;
b6cf0a66 421
1dc01b9f 422 activate_storage($cfg, $storeid);
3af60e62 423
1dc01b9f 424 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 425
1dc01b9f
DM
426 # lock shared storage
427 return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
428 my $volname = $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size);
429 return "$storeid:$volname";
430 });
b6cf0a66
DM
431}
432
1dc01b9f
DM
433sub vdisk_free {
434 my ($cfg, $volid) = @_;
b6cf0a66 435
1dc01b9f 436 my ($storeid, $volname) = parse_volume_id($volid);
b6cf0a66 437
1dc01b9f 438 my $scfg = storage_config($cfg, $storeid);
b6cf0a66 439
1dc01b9f
DM
440 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
441
442 activate_storage($cfg, $storeid);
b6cf0a66 443
1dc01b9f 444 my $cleanup_worker;
b6cf0a66 445
1dc01b9f
DM
446 # lock shared storage
447 $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
448 my $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname);
449 });
b6cf0a66 450
1dc01b9f 451 return if !$cleanup_worker;
b6cf0a66 452
1dc01b9f
DM
453 my $rpcenv = PVE::RPCEnvironment::get();
454 my $authuser = $rpcenv->get_user();
b6cf0a66 455
1dc01b9f 456 $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker);
b6cf0a66
DM
457}
458
b6cf0a66
DM
459#list iso or openvz template ($tt = <iso|vztmpl|backup>)
460sub template_list {
461 my ($cfg, $storeid, $tt) = @_;
462
1dc01b9f
DM
463 die "unknown template type '$tt'\n"
464 if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup');
b6cf0a66
DM
465
466 my $ids = $cfg->{ids};
467
468 storage_check_enabled($cfg, $storeid) if ($storeid);
469
470 my $res = {};
471
472 # query the storage
473
474 foreach my $sid (keys %$ids) {
475 next if $storeid && $storeid ne $sid;
476
477 my $scfg = $ids->{$sid};
478 my $type = $scfg->{type};
479
480 next if !storage_check_enabled($cfg, $sid, undef, 1);
481
482 next if $tt eq 'iso' && !$scfg->{content}->{iso};
483 next if $tt eq 'vztmpl' && !$scfg->{content}->{vztmpl};
484 next if $tt eq 'backup' && !$scfg->{content}->{backup};
485
1dc01b9f 486 activate_storage($cfg, $sid);
b6cf0a66 487
1dc01b9f
DM
488 if ($scfg->{path}) {
489 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 490
1dc01b9f 491 my $path = $plugin->get_subdir($scfg, $tt);
b6cf0a66
DM
492
493 foreach my $fn (<$path/*>) {
494
495 my $info;
496
497 if ($tt eq 'iso') {
498 next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!;
499
500 $info = { volid => "$sid:iso/$1", format => 'iso' };
501
502 } elsif ($tt eq 'vztmpl') {
503 next if $fn !~ m!/([^/]+\.tar\.gz)$!;
504
505 $info = { volid => "$sid:vztmpl/$1", format => 'tgz' };
506
507 } elsif ($tt eq 'backup') {
a22854e5 508 next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!;
b6cf0a66
DM
509
510 $info = { volid => "$sid:backup/$1", format => $2 };
511 }
512
513 $info->{size} = -s $fn;
514
515 push @{$res->{$sid}}, $info;
516 }
517
518 }
519
520 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
521 }
522
523 return $res;
524}
525
20ccac1b 526
b6cf0a66
DM
527sub vdisk_list {
528 my ($cfg, $storeid, $vmid, $vollist) = @_;
529
530 my $ids = $cfg->{ids};
531
532 storage_check_enabled($cfg, $storeid) if ($storeid);
533
534 my $res = {};
535
536 # prepare/activate/refresh all storages
537
b6cf0a66
DM
538 my $storage_list = [];
539 if ($vollist) {
540 foreach my $volid (@$vollist) {
1dc01b9f
DM
541 my ($sid, undef) = parse_volume_id($volid);
542 next if !defined($ids->{$sid});
b6cf0a66
DM
543 next if !storage_check_enabled($cfg, $sid, undef, 1);
544 push @$storage_list, $sid;
b6cf0a66
DM
545 }
546 } else {
547 foreach my $sid (keys %$ids) {
548 next if $storeid && $storeid ne $sid;
549 next if !storage_check_enabled($cfg, $sid, undef, 1);
550 push @$storage_list, $sid;
b6cf0a66
DM
551 }
552 }
553
1dc01b9f 554 my $cache = {};
b6cf0a66 555
1dc01b9f 556 activate_storage_list($cfg, $storage_list, $cache);
b6cf0a66
DM
557
558 foreach my $sid (keys %$ids) {
1dc01b9f
DM
559 next if $storeid && $storeid ne $sid;
560 next if !storage_check_enabled($cfg, $sid, undef, 1);
b6cf0a66 561
1dc01b9f
DM
562 my $scfg = $ids->{$sid};
563 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
564 $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache);
b6cf0a66
DM
565 @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid};
566 }
567
568 return $res;
569}
570
b6cf0a66
DM
571sub uevent_seqnum {
572
573 my $filename = "/sys/kernel/uevent_seqnum";
574
575 my $seqnum = 0;
1dc01b9f 576 if (my $fh = IO::File->new($filename, "r")) {
b6cf0a66
DM
577 my $line = <$fh>;
578 if ($line =~ m/^(\d+)$/) {
1dc01b9f 579 $seqnum = int($1);
b6cf0a66
DM
580 }
581 close ($fh);
582 }
583 return $seqnum;
584}
585
f3d4ef46 586sub activate_storage {
1dc01b9f 587 my ($cfg, $storeid, $cache) = @_;
b6cf0a66 588
f3d4ef46
DM
589 $cache = {} if !$cache;
590
b6cf0a66
DM
591 my $scfg = storage_check_enabled($cfg, $storeid);
592
1dc01b9f 593 return if $cache->{activated}->{$storeid};
b6cf0a66 594
1dc01b9f 595 $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum};
b6cf0a66 596
1dc01b9f 597 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 598
1dc01b9f
DM
599 if ($scfg->{base}) {
600 my ($baseid, undef) = parse_volume_id ($scfg->{base});
f3d4ef46
DM
601 activate_storage($cfg, $baseid, $cache);
602 }
603
604 if (!$plugin->check_connection($storeid, $scfg)) {
605 die "storage '$storeid' is not online\n";
b6cf0a66
DM
606 }
607
1dc01b9f
DM
608 $plugin->activate_storage($storeid, $scfg, $cache);
609
b6cf0a66
DM
610 my $newseq = uevent_seqnum ();
611
612 # only call udevsettle if there are events
1dc01b9f 613 if ($newseq > $cache->{uevent_seqnum}) {
b6cf0a66
DM
614 my $timeout = 30;
615 system ("$UDEVADM settle --timeout=$timeout"); # ignore errors
1dc01b9f 616 $cache->{uevent_seqnum} = $newseq;
b6cf0a66
DM
617 }
618
1dc01b9f 619 $cache->{activated}->{$storeid} = 1;
b6cf0a66
DM
620}
621
622sub activate_storage_list {
1dc01b9f 623 my ($cfg, $storeid_list, $cache) = @_;
b6cf0a66 624
1dc01b9f 625 $cache = {} if !$cache;
b6cf0a66
DM
626
627 foreach my $storeid (@$storeid_list) {
f3d4ef46 628 activate_storage($cfg, $storeid, $cache);
b6cf0a66
DM
629 }
630}
631
1dc01b9f
DM
632sub deactivate_storage {
633 my ($cfg, $storeid) = @_;
634
635 my $scfg = storage_config ($cfg, $storeid);
636 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
b6cf0a66 637
1dc01b9f
DM
638 my $cache = {};
639 $plugin->deactivate_storage($storeid, $scfg, $cache);
b6cf0a66
DM
640}
641
642sub activate_volumes {
6703353b
DM
643 my ($cfg, $vollist, $exclusive) = @_;
644
645 return if !($vollist && scalar(@$vollist));
646
b6cf0a66
DM
647 my $storagehash = {};
648 foreach my $volid (@$vollist) {
1dc01b9f 649 my ($storeid, undef) = parse_volume_id($volid);
b6cf0a66
DM
650 $storagehash->{$storeid} = 1;
651 }
652
1dc01b9f
DM
653 my $cache = {};
654
655 activate_storage_list($cfg, [keys %$storagehash], $cache);
b6cf0a66
DM
656
657 foreach my $volid (@$vollist) {
5521b580 658 my ($storeid, $volname) = parse_volume_id($volid);
1dc01b9f
DM
659 my $scfg = storage_config($cfg, $storeid);
660 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
661 $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache);
b6cf0a66
DM
662 }
663}
664
665sub deactivate_volumes {
666 my ($cfg, $vollist) = @_;
667
6703353b
DM
668 return if !($vollist && scalar(@$vollist));
669
1dc01b9f
DM
670 my $cache = {};
671
6703353b 672 my @errlist = ();
b6cf0a66 673 foreach my $volid (@$vollist) {
1dc01b9f 674 my ($storeid, $volname) = parse_volume_id($volid);
b6cf0a66 675
1dc01b9f
DM
676 my $scfg = storage_config($cfg, $storeid);
677 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
678
679 eval {
680 $plugin->deactivate_volume($storeid, $scfg, $volname, $cache);
681 };
682 if (my $err = $@) {
683 warn $err;
684 push @errlist, $volid;
b6cf0a66
DM
685 }
686 }
6703353b
DM
687
688 die "volume deativation failed: " . join(' ', @errlist)
689 if scalar(@errlist);
b6cf0a66
DM
690}
691
b6cf0a66
DM
692sub storage_info {
693 my ($cfg, $content) = @_;
694
695 my $ids = $cfg->{ids};
696
697 my $info = {};
b6cf0a66
DM
698
699 my $slist = [];
700 foreach my $storeid (keys %$ids) {
701
702 next if $content && !$ids->{$storeid}->{content}->{$content};
703
704 next if !storage_check_enabled($cfg, $storeid, undef, 1);
705
706 my $type = $ids->{$storeid}->{type};
707
708 $info->{$storeid} = {
709 type => $type,
710 total => 0,
711 avail => 0,
712 used => 0,
04a2e4f3 713 shared => $ids->{$storeid}->{shared} ? 1 : 0,
1dc01b9f 714 content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}),
b6cf0a66
DM
715 active => 0,
716 };
717
b6cf0a66
DM
718 push @$slist, $storeid;
719 }
720
1dc01b9f 721 my $cache = {};
b6cf0a66 722
b6cf0a66
DM
723 foreach my $storeid (keys %$ids) {
724 my $scfg = $ids->{$storeid};
b6cf0a66
DM
725 next if !$info->{$storeid};
726
f3d4ef46
DM
727 eval { activate_storage($cfg, $storeid, $cache); };
728 if (my $err = $@) {
729 warn $err;
730 next;
731 }
732
1dc01b9f 733 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
7028645e
DM
734 my ($total, $avail, $used, $active);
735 eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); };
736 warn $@ if $@;
1dc01b9f
DM
737 next if !$active;
738 $info->{$storeid}->{total} = $total;
739 $info->{$storeid}->{avail} = $avail;
740 $info->{$storeid}->{used} = $used;
741 $info->{$storeid}->{active} = $active;
b6cf0a66
DM
742 }
743
744 return $info;
745}
746
747sub resolv_server {
748 my ($server) = @_;
749
750 my $packed_ip = gethostbyname($server);
751 if (defined $packed_ip) {
752 return inet_ntoa($packed_ip);
753 }
754 return undef;
755}
756
757sub scan_nfs {
758 my ($server_in) = @_;
759
760 my $server;
761 if (!($server = resolv_server ($server_in))) {
762 die "unable to resolve address for server '${server_in}'\n";
763 }
764
765 my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server];
766
767 my $res = {};
f81372ac 768 run_command($cmd, outfunc => sub {
b6cf0a66
DM
769 my $line = shift;
770
771 # note: howto handle white spaces in export path??
772 if ($line =~ m!^(/\S+)\s+(.+)$!) {
773 $res->{$1} = $2;
774 }
775 });
776
777 return $res;
778}
779
780sub resolv_portal {
781 my ($portal, $noerr) = @_;
782
783 if ($portal =~ m/^([^:]+)(:(\d+))?$/) {
784 my $server = $1;
785 my $port = $3;
786
787 if (my $ip = resolv_server($server)) {
788 $server = $ip;
789 return $port ? "$server:$port" : $server;
790 }
791 }
792 return undef if $noerr;
793
794 raise_param_exc({ portal => "unable to resolve portal address '$portal'" });
795}
796
797# idea is from usbutils package (/usr/bin/usb-devices) script
798sub __scan_usb_device {
799 my ($res, $devpath, $parent, $level) = @_;
800
801 return if ! -d $devpath;
802 return if $level && $devpath !~ m/^.*[-.](\d+)$/;
803 my $port = $level ? int($1 - 1) : 0;
804
805 my $busnum = int(file_read_firstline("$devpath/busnum"));
806 my $devnum = int(file_read_firstline("$devpath/devnum"));
807
808 my $d = {
809 port => $port,
810 level => $level,
811 busnum => $busnum,
812 devnum => $devnum,
813 speed => file_read_firstline("$devpath/speed"),
814 class => hex(file_read_firstline("$devpath/bDeviceClass")),
815 vendid => file_read_firstline("$devpath/idVendor"),
816 prodid => file_read_firstline("$devpath/idProduct"),
817 };
818
819 if ($level) {
820 my $usbpath = $devpath;
821 $usbpath =~ s|^.*/\d+\-||;
822 $d->{usbpath} = $usbpath;
823 }
824
825 my $product = file_read_firstline("$devpath/product");
826 $d->{product} = $product if $product;
827
828 my $manu = file_read_firstline("$devpath/manufacturer");
829 $d->{manufacturer} = $manu if $manu;
830
831 my $serial => file_read_firstline("$devpath/serial");
832 $d->{serial} = $serial if $serial;
833
834 push @$res, $d;
835
836 foreach my $subdev (<$devpath/$busnum-*>) {
837 next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|;
838 __scan_usb_device($res, $subdev, $devnum, $level + 1);
839 }
840
841};
842
843sub scan_usb {
844
845 my $devlist = [];
846
847 foreach my $device (</sys/bus/usb/devices/usb*>) {
848 __scan_usb_device($devlist, $device, 0, 0);
849 }
850
851 return $devlist;
852}
853
854sub scan_iscsi {
855 my ($portal_in) = @_;
856
857 my $portal;
1dc01b9f 858 if (!($portal = resolv_portal($portal_in))) {
b6cf0a66
DM
859 die "unable to parse/resolve portal address '${portal_in}'\n";
860 }
861
1dc01b9f 862 return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal);
b6cf0a66
DM
863}
864
865sub storage_default_format {
866 my ($cfg, $storeid) = @_;
867
868 my $scfg = storage_config ($cfg, $storeid);
869
1dc01b9f 870 return PVE::Storage::Plugin::default_format($scfg);
b6cf0a66
DM
871}
872
873sub vgroup_is_used {
874 my ($cfg, $vgname) = @_;
875
876 foreach my $storeid (keys %{$cfg->{ids}}) {
1dc01b9f 877 my $scfg = storage_config($cfg, $storeid);
b6cf0a66
DM
878 if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) {
879 return 1;
880 }
881 }
882
883 return undef;
884}
885
886sub target_is_used {
887 my ($cfg, $target) = @_;
888
889 foreach my $storeid (keys %{$cfg->{ids}}) {
1dc01b9f 890 my $scfg = storage_config($cfg, $storeid);
b6cf0a66
DM
891 if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) {
892 return 1;
893 }
894 }
895
896 return undef;
897}
898
899sub volume_is_used {
900 my ($cfg, $volid) = @_;
901
902 foreach my $storeid (keys %{$cfg->{ids}}) {
1dc01b9f 903 my $scfg = storage_config($cfg, $storeid);
b6cf0a66
DM
904 if ($scfg->{base} && $scfg->{base} eq $volid) {
905 return 1;
906 }
907 }
908
909 return undef;
910}
911
912sub storage_is_used {
913 my ($cfg, $storeid) = @_;
914
915 foreach my $sid (keys %{$cfg->{ids}}) {
1dc01b9f 916 my $scfg = storage_config($cfg, $sid);
b6cf0a66 917 next if !$scfg->{base};
1dc01b9f 918 my ($st) = parse_volume_id($scfg->{base});
b6cf0a66
DM
919 return 1 if $st && $st eq $storeid;
920 }
921
922 return undef;
923}
924
925sub foreach_volid {
926 my ($list, $func) = @_;
927
928 return if !$list;
929
930 foreach my $sid (keys %$list) {
931 foreach my $info (@{$list->{$sid}}) {
932 my $volid = $info->{volid};
1dc01b9f 933 my ($sid1, $volname) = parse_volume_id($volid, 1);
b6cf0a66
DM
934 if ($sid1 && $sid1 eq $sid) {
935 &$func ($volid, $sid, $info);
936 } else {
937 warn "detected strange volid '$volid' in volume list for '$sid'\n";
938 }
939 }
940 }
941}
942
9431;