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