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