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