]>
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 | ||
ad27ee3e DM |
271 | sub volume_is_base { |
272 | my ($cfg, $volid) = @_; | |
273 | ||
274 | my ($sid, $volname) = parse_volume_id($volid, 1); | |
275 | return 0 if !$sid; | |
276 | ||
277 | if (my $scfg = $cfg->{ids}->{$sid}) { | |
278 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
279 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
280 | $plugin->parse_volname($volname); | |
281 | return $isBase ? 1 : 0; | |
282 | } else { | |
283 | # stale volid with undefined storage - so we can just guess | |
284 | if ($volid =~ m/base-/) { | |
285 | return 1; | |
286 | } | |
287 | } | |
288 | ||
289 | return 0; | |
290 | } | |
291 | ||
b6cf0a66 DM |
292 | # try to map a filesystem path to a volume identifier |
293 | sub path_to_volume_id { | |
294 | my ($cfg, $path) = @_; | |
295 | ||
296 | my $ids = $cfg->{ids}; | |
297 | ||
1dc01b9f | 298 | my ($sid, $volname) = parse_volume_id($path, 1); |
b6cf0a66 | 299 | if ($sid) { |
1dc01b9f DM |
300 | if (my $scfg = $ids->{$sid}) { |
301 | if (my $path = $scfg->{path}) { | |
302 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
303 | my ($vtype, $name, $vmid) = $plugin->parse_volname($volname); | |
b6cf0a66 DM |
304 | return ($vtype, $path); |
305 | } | |
306 | } | |
307 | return (''); | |
308 | } | |
309 | ||
75d75990 DM |
310 | # Note: abs_path() return undef if $path doesn not exist |
311 | # for example when nfs storage is not mounted | |
312 | $path = abs_path($path) || $path; | |
b6cf0a66 DM |
313 | |
314 | foreach my $sid (keys %$ids) { | |
1dc01b9f DM |
315 | my $scfg = $ids->{$sid}; |
316 | next if !$scfg->{path}; | |
317 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
318 | my $imagedir = $plugin->get_subdir($scfg, 'images'); | |
319 | my $isodir = $plugin->get_subdir($scfg, 'iso'); | |
320 | my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl'); | |
321 | my $backupdir = $plugin->get_subdir($scfg, 'backup'); | |
322 | my $privatedir = $plugin->get_subdir($scfg, 'rootdir'); | |
b6cf0a66 DM |
323 | |
324 | if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) { | |
325 | my $vmid = $1; | |
326 | my $name = $2; | |
fcbec654 DM |
327 | |
328 | my $vollist = $plugin->list_images($sid, $scfg, $vmid); | |
329 | foreach my $info (@$vollist) { | |
330 | my ($storeid, $volname) = parse_volume_id($info->{volid}); | |
331 | my $volpath = $plugin->path($scfg, $volname, $storeid); | |
332 | if ($volpath eq $path) { | |
333 | return ('images', $info->{volid}); | |
334 | } | |
335 | } | |
b6cf0a66 DM |
336 | } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) { |
337 | my $name = $1; | |
338 | return ('iso', "$sid:iso/$name"); | |
339 | } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) { | |
340 | my $name = $1; | |
341 | return ('vztmpl', "$sid:vztmpl/$name"); | |
1ac17c74 DM |
342 | } elsif ($path =~ m!^$privatedir/(\d+)$!) { |
343 | my $vmid = $1; | |
344 | return ('rootdir', "$sid:rootdir/$vmid"); | |
a22854e5 | 345 | } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) { |
568de3d1 DM |
346 | my $name = $1; |
347 | return ('iso', "$sid:backup/$name"); | |
b6cf0a66 DM |
348 | } |
349 | } | |
350 | ||
351 | # can't map path to volume id | |
352 | return (''); | |
353 | } | |
354 | ||
355 | sub path { | |
356 | my ($cfg, $volid) = @_; | |
357 | ||
1dc01b9f | 358 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 | 359 | |
1dc01b9f | 360 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 | 361 | |
1dc01b9f | 362 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
e5427b00 | 363 | my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid); |
2494896a | 364 | return wantarray ? ($path, $owner, $vtype) : $path; |
b6cf0a66 DM |
365 | } |
366 | ||
367 | sub storage_migrate { | |
368 | my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_; | |
369 | ||
6bf56298 | 370 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 DM |
371 | $target_volname = $volname if !$target_volname; |
372 | ||
6bf56298 | 373 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
374 | |
375 | # no need to migrate shared content | |
376 | return if $storeid eq $target_storeid && $scfg->{shared}; | |
377 | ||
6bf56298 | 378 | my $tcfg = storage_config($cfg, $target_storeid); |
b6cf0a66 DM |
379 | |
380 | my $target_volid = "${target_storeid}:${target_volname}"; | |
381 | ||
382 | my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'"; | |
383 | ||
45c2ee35 | 384 | my $sshoptions = "-o 'BatchMode=yes'"; |
b6cf0a66 DM |
385 | my $ssh = "/usr/bin/ssh $sshoptions"; |
386 | ||
387 | local $ENV{RSYNC_RSH} = $ssh; | |
388 | ||
1dc01b9f DM |
389 | # only implemented for file system based storage |
390 | if ($scfg->{path}) { | |
391 | if ($tcfg->{path}) { | |
b6cf0a66 | 392 | |
1dc01b9f DM |
393 | my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
394 | my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type}); | |
6bf56298 DM |
395 | my $src = $src_plugin->path($scfg, $volname, $storeid); |
396 | my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid); | |
b6cf0a66 | 397 | |
1dc01b9f | 398 | my $dirname = dirname($dst); |
b6cf0a66 DM |
399 | |
400 | if ($tcfg->{shared}) { # we can do a local copy | |
401 | ||
f81372ac | 402 | run_command(['/bin/mkdir', '-p', $dirname]); |
b6cf0a66 | 403 | |
1dc01b9f | 404 | run_command(['/bin/cp', $src, $dst]); |
b6cf0a66 | 405 | |
1dc01b9f | 406 | } else { |
b6cf0a66 | 407 | |
1dc01b9f DM |
408 | run_command(['/usr/bin/ssh', "root\@${target_host}", |
409 | '/bin/mkdir', '-p', $dirname]); | |
b6cf0a66 | 410 | |
1dc01b9f DM |
411 | # we use rsync with --sparse, so we can't use --inplace, |
412 | # so we remove file on the target if it already exists to | |
413 | # save space | |
414 | my ($size, $format) = PVE::Storage::Plugin::file_size_info($src); | |
415 | if ($format && ($format eq 'raw') && $size) { | |
416 | run_command(['/usr/bin/ssh', "root\@${target_host}", | |
417 | 'rm', '-f', $dst], | |
418 | outfunc => sub {}); | |
419 | } | |
b6cf0a66 | 420 | |
1dc01b9f DM |
421 | my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file', |
422 | $src, "root\@${target_host}:$dst"]; | |
b6cf0a66 | 423 | |
1dc01b9f | 424 | my $percent = -1; |
b6cf0a66 | 425 | |
1dc01b9f DM |
426 | run_command($cmd, outfunc => sub { |
427 | my $line = shift; | |
b6cf0a66 | 428 | |
1dc01b9f DM |
429 | if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) { |
430 | if ($2 > $percent) { | |
431 | $percent = $2; | |
432 | print "rsync status: $1\n"; | |
433 | *STDOUT->flush(); | |
434 | } | |
435 | } else { | |
436 | print "$line\n"; | |
437 | *STDOUT->flush(); | |
438 | } | |
439 | }); | |
440 | } | |
441 | } else { | |
442 | die "$errstr - target type '$tcfg->{type}' not implemented\n"; | |
443 | } | |
444 | } else { | |
445 | die "$errstr - source type '$scfg->{type}' not implemented\n"; | |
b6cf0a66 DM |
446 | } |
447 | } | |
448 | ||
2502b33b DM |
449 | sub vdisk_clone { |
450 | my ($cfg, $volid, $vmid) = @_; | |
451 | ||
452 | my ($storeid, $volname) = parse_volume_id($volid); | |
453 | ||
454 | my $scfg = storage_config($cfg, $storeid); | |
455 | ||
456 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
457 | ||
458 | activate_storage($cfg, $storeid); | |
459 | ||
460 | # lock shared storage | |
461 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
462 | my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid); | |
463 | return "$storeid:$volname"; | |
464 | }); | |
465 | } | |
466 | ||
467 | sub vdisk_create_base { | |
468 | my ($cfg, $volid) = @_; | |
469 | ||
470 | my ($storeid, $volname) = parse_volume_id($volid); | |
471 | ||
472 | my $scfg = storage_config($cfg, $storeid); | |
473 | ||
474 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
475 | ||
476 | activate_storage($cfg, $storeid); | |
477 | ||
478 | # lock shared storage | |
479 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
480 | my $volname = $plugin->create_base($storeid, $scfg, $volname); | |
481 | return "$storeid:$volname"; | |
482 | }); | |
483 | } | |
484 | ||
1dc01b9f DM |
485 | sub vdisk_alloc { |
486 | my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_; | |
b6cf0a66 | 487 | |
1dc01b9f | 488 | die "no storage id specified\n" if !$storeid; |
b6cf0a66 | 489 | |
1dc01b9f | 490 | PVE::JSONSchema::parse_storage_id($storeid); |
b6cf0a66 | 491 | |
1dc01b9f | 492 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 | 493 | |
1dc01b9f | 494 | die "no VMID specified\n" if !$vmid; |
b6cf0a66 | 495 | |
1dc01b9f | 496 | $vmid = parse_vmid($vmid); |
b6cf0a66 | 497 | |
1dc01b9f | 498 | my $defformat = PVE::Storage::Plugin::default_format($scfg); |
b6cf0a66 | 499 | |
1dc01b9f | 500 | $fmt = $defformat if !$fmt; |
b6cf0a66 | 501 | |
1dc01b9f | 502 | activate_storage($cfg, $storeid); |
3af60e62 | 503 | |
1dc01b9f | 504 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
b6cf0a66 | 505 | |
1dc01b9f DM |
506 | # lock shared storage |
507 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
508 | my $volname = $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size); | |
509 | return "$storeid:$volname"; | |
510 | }); | |
b6cf0a66 DM |
511 | } |
512 | ||
1dc01b9f DM |
513 | sub vdisk_free { |
514 | my ($cfg, $volid) = @_; | |
b6cf0a66 | 515 | |
1dc01b9f | 516 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 | 517 | |
1dc01b9f | 518 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 | 519 | |
1dc01b9f DM |
520 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
521 | ||
522 | activate_storage($cfg, $storeid); | |
b6cf0a66 | 523 | |
1dc01b9f | 524 | my $cleanup_worker; |
b6cf0a66 | 525 | |
1dc01b9f DM |
526 | # lock shared storage |
527 | $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
528 | my $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname); | |
529 | }); | |
b6cf0a66 | 530 | |
1dc01b9f | 531 | return if !$cleanup_worker; |
b6cf0a66 | 532 | |
1dc01b9f DM |
533 | my $rpcenv = PVE::RPCEnvironment::get(); |
534 | my $authuser = $rpcenv->get_user(); | |
b6cf0a66 | 535 | |
1dc01b9f | 536 | $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker); |
b6cf0a66 DM |
537 | } |
538 | ||
b6cf0a66 DM |
539 | #list iso or openvz template ($tt = <iso|vztmpl|backup>) |
540 | sub template_list { | |
541 | my ($cfg, $storeid, $tt) = @_; | |
542 | ||
1dc01b9f DM |
543 | die "unknown template type '$tt'\n" |
544 | if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup'); | |
b6cf0a66 DM |
545 | |
546 | my $ids = $cfg->{ids}; | |
547 | ||
548 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
549 | ||
550 | my $res = {}; | |
551 | ||
552 | # query the storage | |
553 | ||
554 | foreach my $sid (keys %$ids) { | |
555 | next if $storeid && $storeid ne $sid; | |
556 | ||
557 | my $scfg = $ids->{$sid}; | |
558 | my $type = $scfg->{type}; | |
559 | ||
560 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
561 | ||
562 | next if $tt eq 'iso' && !$scfg->{content}->{iso}; | |
563 | next if $tt eq 'vztmpl' && !$scfg->{content}->{vztmpl}; | |
564 | next if $tt eq 'backup' && !$scfg->{content}->{backup}; | |
565 | ||
1dc01b9f | 566 | activate_storage($cfg, $sid); |
b6cf0a66 | 567 | |
1dc01b9f DM |
568 | if ($scfg->{path}) { |
569 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 570 | |
1dc01b9f | 571 | my $path = $plugin->get_subdir($scfg, $tt); |
b6cf0a66 DM |
572 | |
573 | foreach my $fn (<$path/*>) { | |
574 | ||
575 | my $info; | |
576 | ||
577 | if ($tt eq 'iso') { | |
578 | next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!; | |
579 | ||
580 | $info = { volid => "$sid:iso/$1", format => 'iso' }; | |
581 | ||
582 | } elsif ($tt eq 'vztmpl') { | |
583 | next if $fn !~ m!/([^/]+\.tar\.gz)$!; | |
584 | ||
585 | $info = { volid => "$sid:vztmpl/$1", format => 'tgz' }; | |
586 | ||
587 | } elsif ($tt eq 'backup') { | |
a22854e5 | 588 | next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!; |
b6cf0a66 DM |
589 | |
590 | $info = { volid => "$sid:backup/$1", format => $2 }; | |
591 | } | |
592 | ||
593 | $info->{size} = -s $fn; | |
594 | ||
595 | push @{$res->{$sid}}, $info; | |
596 | } | |
597 | ||
598 | } | |
599 | ||
600 | @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid}; | |
601 | } | |
602 | ||
603 | return $res; | |
604 | } | |
605 | ||
20ccac1b | 606 | |
b6cf0a66 DM |
607 | sub vdisk_list { |
608 | my ($cfg, $storeid, $vmid, $vollist) = @_; | |
609 | ||
610 | my $ids = $cfg->{ids}; | |
611 | ||
612 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
613 | ||
614 | my $res = {}; | |
615 | ||
616 | # prepare/activate/refresh all storages | |
617 | ||
b6cf0a66 DM |
618 | my $storage_list = []; |
619 | if ($vollist) { | |
620 | foreach my $volid (@$vollist) { | |
1dc01b9f DM |
621 | my ($sid, undef) = parse_volume_id($volid); |
622 | next if !defined($ids->{$sid}); | |
b6cf0a66 DM |
623 | next if !storage_check_enabled($cfg, $sid, undef, 1); |
624 | push @$storage_list, $sid; | |
b6cf0a66 DM |
625 | } |
626 | } else { | |
627 | foreach my $sid (keys %$ids) { | |
628 | next if $storeid && $storeid ne $sid; | |
629 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
630 | push @$storage_list, $sid; | |
b6cf0a66 DM |
631 | } |
632 | } | |
633 | ||
1dc01b9f | 634 | my $cache = {}; |
b6cf0a66 | 635 | |
1dc01b9f | 636 | activate_storage_list($cfg, $storage_list, $cache); |
b6cf0a66 DM |
637 | |
638 | foreach my $sid (keys %$ids) { | |
1dc01b9f DM |
639 | next if $storeid && $storeid ne $sid; |
640 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
b6cf0a66 | 641 | |
1dc01b9f DM |
642 | my $scfg = $ids->{$sid}; |
643 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
644 | $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache); | |
b6cf0a66 DM |
645 | @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid}; |
646 | } | |
647 | ||
648 | return $res; | |
649 | } | |
650 | ||
b6cf0a66 DM |
651 | sub uevent_seqnum { |
652 | ||
653 | my $filename = "/sys/kernel/uevent_seqnum"; | |
654 | ||
655 | my $seqnum = 0; | |
1dc01b9f | 656 | if (my $fh = IO::File->new($filename, "r")) { |
b6cf0a66 DM |
657 | my $line = <$fh>; |
658 | if ($line =~ m/^(\d+)$/) { | |
1dc01b9f | 659 | $seqnum = int($1); |
b6cf0a66 DM |
660 | } |
661 | close ($fh); | |
662 | } | |
663 | return $seqnum; | |
664 | } | |
665 | ||
f3d4ef46 | 666 | sub activate_storage { |
1dc01b9f | 667 | my ($cfg, $storeid, $cache) = @_; |
b6cf0a66 | 668 | |
f3d4ef46 DM |
669 | $cache = {} if !$cache; |
670 | ||
b6cf0a66 DM |
671 | my $scfg = storage_check_enabled($cfg, $storeid); |
672 | ||
1dc01b9f | 673 | return if $cache->{activated}->{$storeid}; |
b6cf0a66 | 674 | |
1dc01b9f | 675 | $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum}; |
b6cf0a66 | 676 | |
1dc01b9f | 677 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
b6cf0a66 | 678 | |
1dc01b9f DM |
679 | if ($scfg->{base}) { |
680 | my ($baseid, undef) = parse_volume_id ($scfg->{base}); | |
f3d4ef46 DM |
681 | activate_storage($cfg, $baseid, $cache); |
682 | } | |
683 | ||
684 | if (!$plugin->check_connection($storeid, $scfg)) { | |
685 | die "storage '$storeid' is not online\n"; | |
b6cf0a66 DM |
686 | } |
687 | ||
1dc01b9f DM |
688 | $plugin->activate_storage($storeid, $scfg, $cache); |
689 | ||
b6cf0a66 DM |
690 | my $newseq = uevent_seqnum (); |
691 | ||
692 | # only call udevsettle if there are events | |
1dc01b9f | 693 | if ($newseq > $cache->{uevent_seqnum}) { |
b6cf0a66 DM |
694 | my $timeout = 30; |
695 | system ("$UDEVADM settle --timeout=$timeout"); # ignore errors | |
1dc01b9f | 696 | $cache->{uevent_seqnum} = $newseq; |
b6cf0a66 DM |
697 | } |
698 | ||
1dc01b9f | 699 | $cache->{activated}->{$storeid} = 1; |
b6cf0a66 DM |
700 | } |
701 | ||
702 | sub activate_storage_list { | |
1dc01b9f | 703 | my ($cfg, $storeid_list, $cache) = @_; |
b6cf0a66 | 704 | |
1dc01b9f | 705 | $cache = {} if !$cache; |
b6cf0a66 DM |
706 | |
707 | foreach my $storeid (@$storeid_list) { | |
f3d4ef46 | 708 | activate_storage($cfg, $storeid, $cache); |
b6cf0a66 DM |
709 | } |
710 | } | |
711 | ||
1dc01b9f DM |
712 | sub deactivate_storage { |
713 | my ($cfg, $storeid) = @_; | |
714 | ||
715 | my $scfg = storage_config ($cfg, $storeid); | |
716 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 717 | |
1dc01b9f DM |
718 | my $cache = {}; |
719 | $plugin->deactivate_storage($storeid, $scfg, $cache); | |
b6cf0a66 DM |
720 | } |
721 | ||
722 | sub activate_volumes { | |
6703353b DM |
723 | my ($cfg, $vollist, $exclusive) = @_; |
724 | ||
725 | return if !($vollist && scalar(@$vollist)); | |
726 | ||
b6cf0a66 DM |
727 | my $storagehash = {}; |
728 | foreach my $volid (@$vollist) { | |
1dc01b9f | 729 | my ($storeid, undef) = parse_volume_id($volid); |
b6cf0a66 DM |
730 | $storagehash->{$storeid} = 1; |
731 | } | |
732 | ||
1dc01b9f DM |
733 | my $cache = {}; |
734 | ||
735 | activate_storage_list($cfg, [keys %$storagehash], $cache); | |
b6cf0a66 DM |
736 | |
737 | foreach my $volid (@$vollist) { | |
5521b580 | 738 | my ($storeid, $volname) = parse_volume_id($volid); |
1dc01b9f DM |
739 | my $scfg = storage_config($cfg, $storeid); |
740 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
741 | $plugin->activate_volume($storeid, $scfg, $volname, $exclusive, $cache); | |
b6cf0a66 DM |
742 | } |
743 | } | |
744 | ||
745 | sub deactivate_volumes { | |
746 | my ($cfg, $vollist) = @_; | |
747 | ||
6703353b DM |
748 | return if !($vollist && scalar(@$vollist)); |
749 | ||
1dc01b9f DM |
750 | my $cache = {}; |
751 | ||
6703353b | 752 | my @errlist = (); |
b6cf0a66 | 753 | foreach my $volid (@$vollist) { |
1dc01b9f | 754 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 | 755 | |
1dc01b9f DM |
756 | my $scfg = storage_config($cfg, $storeid); |
757 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
758 | ||
759 | eval { | |
760 | $plugin->deactivate_volume($storeid, $scfg, $volname, $cache); | |
761 | }; | |
762 | if (my $err = $@) { | |
763 | warn $err; | |
764 | push @errlist, $volid; | |
b6cf0a66 DM |
765 | } |
766 | } | |
6703353b DM |
767 | |
768 | die "volume deativation failed: " . join(' ', @errlist) | |
769 | if scalar(@errlist); | |
b6cf0a66 DM |
770 | } |
771 | ||
b6cf0a66 DM |
772 | sub storage_info { |
773 | my ($cfg, $content) = @_; | |
774 | ||
775 | my $ids = $cfg->{ids}; | |
776 | ||
777 | my $info = {}; | |
b6cf0a66 DM |
778 | |
779 | my $slist = []; | |
780 | foreach my $storeid (keys %$ids) { | |
781 | ||
782 | next if $content && !$ids->{$storeid}->{content}->{$content}; | |
783 | ||
784 | next if !storage_check_enabled($cfg, $storeid, undef, 1); | |
785 | ||
786 | my $type = $ids->{$storeid}->{type}; | |
787 | ||
788 | $info->{$storeid} = { | |
789 | type => $type, | |
790 | total => 0, | |
791 | avail => 0, | |
792 | used => 0, | |
04a2e4f3 | 793 | shared => $ids->{$storeid}->{shared} ? 1 : 0, |
1dc01b9f | 794 | content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}), |
b6cf0a66 DM |
795 | active => 0, |
796 | }; | |
797 | ||
b6cf0a66 DM |
798 | push @$slist, $storeid; |
799 | } | |
800 | ||
1dc01b9f | 801 | my $cache = {}; |
b6cf0a66 | 802 | |
b6cf0a66 DM |
803 | foreach my $storeid (keys %$ids) { |
804 | my $scfg = $ids->{$storeid}; | |
b6cf0a66 DM |
805 | next if !$info->{$storeid}; |
806 | ||
f3d4ef46 DM |
807 | eval { activate_storage($cfg, $storeid, $cache); }; |
808 | if (my $err = $@) { | |
809 | warn $err; | |
810 | next; | |
811 | } | |
812 | ||
1dc01b9f | 813 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
7028645e DM |
814 | my ($total, $avail, $used, $active); |
815 | eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); }; | |
816 | warn $@ if $@; | |
1dc01b9f DM |
817 | next if !$active; |
818 | $info->{$storeid}->{total} = $total; | |
819 | $info->{$storeid}->{avail} = $avail; | |
820 | $info->{$storeid}->{used} = $used; | |
821 | $info->{$storeid}->{active} = $active; | |
b6cf0a66 DM |
822 | } |
823 | ||
824 | return $info; | |
825 | } | |
826 | ||
827 | sub resolv_server { | |
828 | my ($server) = @_; | |
829 | ||
830 | my $packed_ip = gethostbyname($server); | |
831 | if (defined $packed_ip) { | |
832 | return inet_ntoa($packed_ip); | |
833 | } | |
834 | return undef; | |
835 | } | |
836 | ||
837 | sub scan_nfs { | |
838 | my ($server_in) = @_; | |
839 | ||
840 | my $server; | |
841 | if (!($server = resolv_server ($server_in))) { | |
842 | die "unable to resolve address for server '${server_in}'\n"; | |
843 | } | |
844 | ||
845 | my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server]; | |
846 | ||
847 | my $res = {}; | |
f81372ac | 848 | run_command($cmd, outfunc => sub { |
b6cf0a66 DM |
849 | my $line = shift; |
850 | ||
851 | # note: howto handle white spaces in export path?? | |
852 | if ($line =~ m!^(/\S+)\s+(.+)$!) { | |
853 | $res->{$1} = $2; | |
854 | } | |
855 | }); | |
856 | ||
857 | return $res; | |
858 | } | |
859 | ||
860 | sub resolv_portal { | |
861 | my ($portal, $noerr) = @_; | |
862 | ||
863 | if ($portal =~ m/^([^:]+)(:(\d+))?$/) { | |
864 | my $server = $1; | |
865 | my $port = $3; | |
866 | ||
867 | if (my $ip = resolv_server($server)) { | |
868 | $server = $ip; | |
869 | return $port ? "$server:$port" : $server; | |
870 | } | |
871 | } | |
872 | return undef if $noerr; | |
873 | ||
874 | raise_param_exc({ portal => "unable to resolve portal address '$portal'" }); | |
875 | } | |
876 | ||
877 | # idea is from usbutils package (/usr/bin/usb-devices) script | |
878 | sub __scan_usb_device { | |
879 | my ($res, $devpath, $parent, $level) = @_; | |
880 | ||
881 | return if ! -d $devpath; | |
882 | return if $level && $devpath !~ m/^.*[-.](\d+)$/; | |
883 | my $port = $level ? int($1 - 1) : 0; | |
884 | ||
885 | my $busnum = int(file_read_firstline("$devpath/busnum")); | |
886 | my $devnum = int(file_read_firstline("$devpath/devnum")); | |
887 | ||
888 | my $d = { | |
889 | port => $port, | |
890 | level => $level, | |
891 | busnum => $busnum, | |
892 | devnum => $devnum, | |
893 | speed => file_read_firstline("$devpath/speed"), | |
894 | class => hex(file_read_firstline("$devpath/bDeviceClass")), | |
895 | vendid => file_read_firstline("$devpath/idVendor"), | |
896 | prodid => file_read_firstline("$devpath/idProduct"), | |
897 | }; | |
898 | ||
899 | if ($level) { | |
900 | my $usbpath = $devpath; | |
901 | $usbpath =~ s|^.*/\d+\-||; | |
902 | $d->{usbpath} = $usbpath; | |
903 | } | |
904 | ||
905 | my $product = file_read_firstline("$devpath/product"); | |
906 | $d->{product} = $product if $product; | |
907 | ||
908 | my $manu = file_read_firstline("$devpath/manufacturer"); | |
909 | $d->{manufacturer} = $manu if $manu; | |
910 | ||
911 | my $serial => file_read_firstline("$devpath/serial"); | |
912 | $d->{serial} = $serial if $serial; | |
913 | ||
914 | push @$res, $d; | |
915 | ||
916 | foreach my $subdev (<$devpath/$busnum-*>) { | |
917 | next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|; | |
918 | __scan_usb_device($res, $subdev, $devnum, $level + 1); | |
919 | } | |
920 | ||
921 | }; | |
922 | ||
923 | sub scan_usb { | |
924 | ||
925 | my $devlist = []; | |
926 | ||
927 | foreach my $device (</sys/bus/usb/devices/usb*>) { | |
928 | __scan_usb_device($devlist, $device, 0, 0); | |
929 | } | |
930 | ||
931 | return $devlist; | |
932 | } | |
933 | ||
934 | sub scan_iscsi { | |
935 | my ($portal_in) = @_; | |
936 | ||
937 | my $portal; | |
1dc01b9f | 938 | if (!($portal = resolv_portal($portal_in))) { |
b6cf0a66 DM |
939 | die "unable to parse/resolve portal address '${portal_in}'\n"; |
940 | } | |
941 | ||
1dc01b9f | 942 | return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal); |
b6cf0a66 DM |
943 | } |
944 | ||
945 | sub storage_default_format { | |
946 | my ($cfg, $storeid) = @_; | |
947 | ||
948 | my $scfg = storage_config ($cfg, $storeid); | |
949 | ||
1dc01b9f | 950 | return PVE::Storage::Plugin::default_format($scfg); |
b6cf0a66 DM |
951 | } |
952 | ||
953 | sub vgroup_is_used { | |
954 | my ($cfg, $vgname) = @_; | |
955 | ||
956 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 957 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
958 | if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) { |
959 | return 1; | |
960 | } | |
961 | } | |
962 | ||
963 | return undef; | |
964 | } | |
965 | ||
966 | sub target_is_used { | |
967 | my ($cfg, $target) = @_; | |
968 | ||
969 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 970 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
971 | if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) { |
972 | return 1; | |
973 | } | |
974 | } | |
975 | ||
976 | return undef; | |
977 | } | |
978 | ||
979 | sub volume_is_used { | |
980 | my ($cfg, $volid) = @_; | |
981 | ||
982 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 983 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
984 | if ($scfg->{base} && $scfg->{base} eq $volid) { |
985 | return 1; | |
986 | } | |
987 | } | |
988 | ||
989 | return undef; | |
990 | } | |
991 | ||
992 | sub storage_is_used { | |
993 | my ($cfg, $storeid) = @_; | |
994 | ||
995 | foreach my $sid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 996 | my $scfg = storage_config($cfg, $sid); |
b6cf0a66 | 997 | next if !$scfg->{base}; |
1dc01b9f | 998 | my ($st) = parse_volume_id($scfg->{base}); |
b6cf0a66 DM |
999 | return 1 if $st && $st eq $storeid; |
1000 | } | |
1001 | ||
1002 | return undef; | |
1003 | } | |
1004 | ||
1005 | sub foreach_volid { | |
1006 | my ($list, $func) = @_; | |
1007 | ||
1008 | return if !$list; | |
1009 | ||
1010 | foreach my $sid (keys %$list) { | |
1011 | foreach my $info (@{$list->{$sid}}) { | |
1012 | my $volid = $info->{volid}; | |
1dc01b9f | 1013 | my ($sid1, $volname) = parse_volume_id($volid, 1); |
b6cf0a66 DM |
1014 | if ($sid1 && $sid1 eq $sid) { |
1015 | &$func ($volid, $sid, $info); | |
1016 | } else { | |
1017 | warn "detected strange volid '$volid' in volume list for '$sid'\n"; | |
1018 | } | |
1019 | } | |
1020 | } | |
1021 | } | |
1022 | ||
1023 | 1; |