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