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