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