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