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