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