]>
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 | |
1689e627 | 15 | use PVE::Tools qw(run_command file_read_firstline $IPV6RE); |
1dc01b9f | 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}); | |
a6f12626 DM |
293 | |
294 | # returns ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) | |
295 | ||
ec4b0dc7 AD |
296 | return $plugin->parse_volname($volname); |
297 | } | |
298 | ||
b6cf0a66 DM |
299 | sub parse_volume_id { |
300 | my ($volid, $noerr) = @_; | |
301 | ||
a7f3d909 | 302 | return PVE::Storage::Plugin::parse_volume_id($volid, $noerr); |
b6cf0a66 DM |
303 | } |
304 | ||
ad27ee3e DM |
305 | sub volume_is_base { |
306 | my ($cfg, $volid) = @_; | |
307 | ||
308 | my ($sid, $volname) = parse_volume_id($volid, 1); | |
309 | return 0 if !$sid; | |
310 | ||
311 | if (my $scfg = $cfg->{ids}->{$sid}) { | |
312 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 313 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = |
ad27ee3e DM |
314 | $plugin->parse_volname($volname); |
315 | return $isBase ? 1 : 0; | |
316 | } else { | |
317 | # stale volid with undefined storage - so we can just guess | |
318 | if ($volid =~ m/base-/) { | |
319 | return 1; | |
320 | } | |
321 | } | |
322 | ||
323 | return 0; | |
324 | } | |
325 | ||
b6cf0a66 DM |
326 | # try to map a filesystem path to a volume identifier |
327 | sub path_to_volume_id { | |
328 | my ($cfg, $path) = @_; | |
329 | ||
330 | my $ids = $cfg->{ids}; | |
331 | ||
1dc01b9f | 332 | my ($sid, $volname) = parse_volume_id($path, 1); |
b6cf0a66 | 333 | if ($sid) { |
1dc01b9f | 334 | if (my $scfg = $ids->{$sid}) { |
188aca38 | 335 | if ($scfg->{path}) { |
1dc01b9f DM |
336 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
337 | my ($vtype, $name, $vmid) = $plugin->parse_volname($volname); | |
b6cf0a66 DM |
338 | return ($vtype, $path); |
339 | } | |
340 | } | |
341 | return (''); | |
342 | } | |
343 | ||
1a3459ac | 344 | # Note: abs_path() return undef if $path doesn not exist |
75d75990 DM |
345 | # for example when nfs storage is not mounted |
346 | $path = abs_path($path) || $path; | |
b6cf0a66 DM |
347 | |
348 | foreach my $sid (keys %$ids) { | |
1dc01b9f DM |
349 | my $scfg = $ids->{$sid}; |
350 | next if !$scfg->{path}; | |
351 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
352 | my $imagedir = $plugin->get_subdir($scfg, 'images'); | |
353 | my $isodir = $plugin->get_subdir($scfg, 'iso'); | |
354 | my $tmpldir = $plugin->get_subdir($scfg, 'vztmpl'); | |
355 | my $backupdir = $plugin->get_subdir($scfg, 'backup'); | |
356 | my $privatedir = $plugin->get_subdir($scfg, 'rootdir'); | |
b6cf0a66 DM |
357 | |
358 | if ($path =~ m!^$imagedir/(\d+)/([^/\s]+)$!) { | |
359 | my $vmid = $1; | |
360 | my $name = $2; | |
fcbec654 DM |
361 | |
362 | my $vollist = $plugin->list_images($sid, $scfg, $vmid); | |
363 | foreach my $info (@$vollist) { | |
364 | my ($storeid, $volname) = parse_volume_id($info->{volid}); | |
365 | my $volpath = $plugin->path($scfg, $volname, $storeid); | |
366 | if ($volpath eq $path) { | |
367 | return ('images', $info->{volid}); | |
368 | } | |
369 | } | |
b6cf0a66 DM |
370 | } elsif ($path =~ m!^$isodir/([^/]+\.[Ii][Ss][Oo])$!) { |
371 | my $name = $1; | |
1a3459ac | 372 | return ('iso', "$sid:iso/$name"); |
b6cf0a66 DM |
373 | } elsif ($path =~ m!^$tmpldir/([^/]+\.tar\.gz)$!) { |
374 | my $name = $1; | |
375 | return ('vztmpl', "$sid:vztmpl/$name"); | |
1ac17c74 DM |
376 | } elsif ($path =~ m!^$privatedir/(\d+)$!) { |
377 | my $vmid = $1; | |
378 | return ('rootdir', "$sid:rootdir/$vmid"); | |
a22854e5 | 379 | } elsif ($path =~ m!^$backupdir/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!) { |
568de3d1 | 380 | my $name = $1; |
1a3459ac | 381 | return ('iso', "$sid:backup/$name"); |
b6cf0a66 DM |
382 | } |
383 | } | |
384 | ||
385 | # can't map path to volume id | |
386 | return (''); | |
387 | } | |
388 | ||
389 | sub path { | |
207ea852 | 390 | my ($cfg, $volid, $snapname) = @_; |
b6cf0a66 | 391 | |
1dc01b9f | 392 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 | 393 | |
1dc01b9f | 394 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 | 395 | |
1dc01b9f | 396 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
207ea852 | 397 | my ($path, $owner, $vtype) = $plugin->path($scfg, $volname, $storeid, $snapname); |
2494896a | 398 | return wantarray ? ($path, $owner, $vtype) : $path; |
b6cf0a66 DM |
399 | } |
400 | ||
35fbb2e6 DM |
401 | sub abs_filesystem_path { |
402 | my ($cfg, $volid) = @_; | |
403 | ||
404 | my $path; | |
405 | if (PVE::Storage::parse_volume_id ($volid, 1)) { | |
406 | PVE::Storage::activate_volumes($cfg, [ $volid ]); | |
407 | $path = PVE::Storage::path($cfg, $volid); | |
408 | } else { | |
409 | if (-f $volid) { | |
410 | my $abspath = abs_path($volid); | |
411 | if ($abspath && $abspath =~ m|^(/.+)$|) { | |
412 | $path = $1; # untaint any path | |
413 | } | |
414 | } | |
415 | } | |
416 | ||
417 | die "can't find file '$volid'\n" if !($path && -f $path); | |
418 | ||
419 | return $path; | |
420 | } | |
421 | ||
b6cf0a66 DM |
422 | sub storage_migrate { |
423 | my ($cfg, $volid, $target_host, $target_storeid, $target_volname) = @_; | |
424 | ||
6bf56298 | 425 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 DM |
426 | $target_volname = $volname if !$target_volname; |
427 | ||
6bf56298 | 428 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
429 | |
430 | # no need to migrate shared content | |
431 | return if $storeid eq $target_storeid && $scfg->{shared}; | |
432 | ||
6bf56298 | 433 | my $tcfg = storage_config($cfg, $target_storeid); |
b6cf0a66 DM |
434 | |
435 | my $target_volid = "${target_storeid}:${target_volname}"; | |
436 | ||
437 | my $errstr = "unable to migrate '$volid' to '${target_volid}' on host '$target_host'"; | |
438 | ||
45c2ee35 | 439 | my $sshoptions = "-o 'BatchMode=yes'"; |
b6cf0a66 DM |
440 | my $ssh = "/usr/bin/ssh $sshoptions"; |
441 | ||
442 | local $ENV{RSYNC_RSH} = $ssh; | |
443 | ||
1dc01b9f DM |
444 | # only implemented for file system based storage |
445 | if ($scfg->{path}) { | |
446 | if ($tcfg->{path}) { | |
b6cf0a66 | 447 | |
1dc01b9f DM |
448 | my $src_plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
449 | my $dst_plugin = PVE::Storage::Plugin->lookup($tcfg->{type}); | |
6bf56298 DM |
450 | my $src = $src_plugin->path($scfg, $volname, $storeid); |
451 | my $dst = $dst_plugin->path($tcfg, $target_volname, $target_storeid); | |
b6cf0a66 | 452 | |
1dc01b9f | 453 | my $dirname = dirname($dst); |
b6cf0a66 DM |
454 | |
455 | if ($tcfg->{shared}) { # we can do a local copy | |
1a3459ac | 456 | |
f81372ac | 457 | run_command(['/bin/mkdir', '-p', $dirname]); |
b6cf0a66 | 458 | |
1dc01b9f | 459 | run_command(['/bin/cp', $src, $dst]); |
b6cf0a66 | 460 | |
1dc01b9f | 461 | } else { |
b6cf0a66 | 462 | |
1a3459ac | 463 | run_command(['/usr/bin/ssh', "root\@${target_host}", |
1dc01b9f | 464 | '/bin/mkdir', '-p', $dirname]); |
b6cf0a66 | 465 | |
1dc01b9f DM |
466 | # we use rsync with --sparse, so we can't use --inplace, |
467 | # so we remove file on the target if it already exists to | |
468 | # save space | |
469 | my ($size, $format) = PVE::Storage::Plugin::file_size_info($src); | |
470 | if ($format && ($format eq 'raw') && $size) { | |
1a3459ac | 471 | run_command(['/usr/bin/ssh', "root\@${target_host}", |
1dc01b9f DM |
472 | 'rm', '-f', $dst], |
473 | outfunc => sub {}); | |
474 | } | |
b6cf0a66 | 475 | |
1a3459ac | 476 | my $cmd = ['/usr/bin/rsync', '--progress', '--sparse', '--whole-file', |
1dc01b9f | 477 | $src, "root\@${target_host}:$dst"]; |
b6cf0a66 | 478 | |
1dc01b9f | 479 | my $percent = -1; |
b6cf0a66 | 480 | |
1dc01b9f DM |
481 | run_command($cmd, outfunc => sub { |
482 | my $line = shift; | |
b6cf0a66 | 483 | |
1dc01b9f DM |
484 | if ($line =~ m/^\s*(\d+\s+(\d+)%\s.*)$/) { |
485 | if ($2 > $percent) { | |
486 | $percent = $2; | |
487 | print "rsync status: $1\n"; | |
488 | *STDOUT->flush(); | |
489 | } | |
490 | } else { | |
491 | print "$line\n"; | |
492 | *STDOUT->flush(); | |
493 | } | |
494 | }); | |
495 | } | |
496 | } else { | |
497 | die "$errstr - target type '$tcfg->{type}' not implemented\n"; | |
498 | } | |
e0852ba7 | 499 | |
3d621977 WL |
500 | } elsif ($scfg->{type} eq 'zfspool') { |
501 | ||
e0852ba7 | 502 | if ($tcfg->{type} eq 'zfspool') { |
3d621977 | 503 | |
e0852ba7 DM |
504 | die "$errstr - pool on target has not same name as source!" |
505 | if $tcfg->{pool} ne $scfg->{pool}; | |
3d621977 | 506 | |
e0852ba7 | 507 | my (undef, $volname) = parse_volname($cfg, $volid); |
3d621977 WL |
508 | |
509 | my $zfspath = "$scfg->{pool}\/$volname"; | |
510 | ||
e0852ba7 | 511 | my $snap = "zfs snapshot $zfspath\@__migration__"; |
3d621977 | 512 | |
323ab5ba | 513 | my $send = "zfs send -pv $zfspath\@__migration__ \| ssh root\@$target_host zfs recv $zfspath"; |
3d621977 | 514 | |
e0852ba7 | 515 | my $destroy_target = "ssh root\@$target_host zfs destroy $zfspath\@__migration__"; |
3d621977 | 516 | run_command($snap); |
e0852ba7 | 517 | eval{ |
3d621977 WL |
518 | run_command($send); |
519 | }; | |
520 | my $err; | |
521 | if ($err = $@){ | |
522 | run_command("zfs destroy $zfspath\@__migration__"); | |
523 | die $err; | |
e0852ba7 DM |
524 | } |
525 | run_command($destroy_target); | |
3d621977 WL |
526 | |
527 | } else { | |
528 | die "$errstr - target type $tcfg->{type} is not valid\n"; | |
529 | } | |
1dc01b9f DM |
530 | } else { |
531 | die "$errstr - source type '$scfg->{type}' not implemented\n"; | |
b6cf0a66 DM |
532 | } |
533 | } | |
534 | ||
2502b33b | 535 | sub vdisk_clone { |
7bbc4004 | 536 | my ($cfg, $volid, $vmid, $snap) = @_; |
1a3459ac | 537 | |
2502b33b DM |
538 | my ($storeid, $volname) = parse_volume_id($volid); |
539 | ||
540 | my $scfg = storage_config($cfg, $storeid); | |
541 | ||
542 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 543 | |
2502b33b DM |
544 | activate_storage($cfg, $storeid); |
545 | ||
546 | # lock shared storage | |
547 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
7bbc4004 | 548 | my $volname = $plugin->clone_image($scfg, $storeid, $volname, $vmid, $snap); |
2502b33b DM |
549 | return "$storeid:$volname"; |
550 | }); | |
551 | } | |
552 | ||
553 | sub vdisk_create_base { | |
554 | my ($cfg, $volid) = @_; | |
555 | ||
556 | my ($storeid, $volname) = parse_volume_id($volid); | |
557 | ||
558 | my $scfg = storage_config($cfg, $storeid); | |
559 | ||
560 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 561 | |
2502b33b DM |
562 | activate_storage($cfg, $storeid); |
563 | ||
564 | # lock shared storage | |
565 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
566 | my $volname = $plugin->create_base($storeid, $scfg, $volname); | |
567 | return "$storeid:$volname"; | |
568 | }); | |
569 | } | |
570 | ||
1dc01b9f DM |
571 | sub vdisk_alloc { |
572 | my ($cfg, $storeid, $vmid, $fmt, $name, $size) = @_; | |
b6cf0a66 | 573 | |
1dc01b9f | 574 | die "no storage id specified\n" if !$storeid; |
b6cf0a66 | 575 | |
1dc01b9f | 576 | PVE::JSONSchema::parse_storage_id($storeid); |
b6cf0a66 | 577 | |
1dc01b9f | 578 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 | 579 | |
1dc01b9f | 580 | die "no VMID specified\n" if !$vmid; |
b6cf0a66 | 581 | |
1dc01b9f | 582 | $vmid = parse_vmid($vmid); |
b6cf0a66 | 583 | |
1dc01b9f | 584 | my $defformat = PVE::Storage::Plugin::default_format($scfg); |
b6cf0a66 | 585 | |
1dc01b9f | 586 | $fmt = $defformat if !$fmt; |
b6cf0a66 | 587 | |
1dc01b9f | 588 | activate_storage($cfg, $storeid); |
3af60e62 | 589 | |
1dc01b9f | 590 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
b6cf0a66 | 591 | |
1dc01b9f DM |
592 | # lock shared storage |
593 | return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
afdfbe55 WB |
594 | my $old_umask = umask(umask|0037); |
595 | my $volname = eval { $plugin->alloc_image($storeid, $scfg, $vmid, $fmt, $name, $size) }; | |
596 | my $err = $@; | |
597 | umask $old_umask; | |
598 | die $err if $err; | |
1dc01b9f DM |
599 | return "$storeid:$volname"; |
600 | }); | |
b6cf0a66 DM |
601 | } |
602 | ||
1dc01b9f DM |
603 | sub vdisk_free { |
604 | my ($cfg, $volid) = @_; | |
b6cf0a66 | 605 | |
1dc01b9f | 606 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 | 607 | |
1dc01b9f | 608 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 | 609 | |
1dc01b9f | 610 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
1a3459ac | 611 | |
1dc01b9f | 612 | activate_storage($cfg, $storeid); |
b6cf0a66 | 613 | |
1dc01b9f | 614 | my $cleanup_worker; |
b6cf0a66 | 615 | |
1dc01b9f DM |
616 | # lock shared storage |
617 | $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
32437ed2 | 618 | |
35533c68 | 619 | my ($vtype, $name, $vmid, undef, undef, $isBase, $format) = |
32437ed2 | 620 | $plugin->parse_volname($volname); |
32437ed2 DM |
621 | if ($isBase) { |
622 | my $vollist = $plugin->list_images($storeid, $scfg); | |
623 | foreach my $info (@$vollist) { | |
624 | my (undef, $tmpvolname) = parse_volume_id($info->{volid}); | |
f104d1dd AD |
625 | my $basename = undef; |
626 | my $basevmid = undef; | |
32437ed2 | 627 | |
f104d1dd | 628 | eval{ |
1a3459ac | 629 | (undef, undef, undef, $basename, $basevmid) = |
f104d1dd AD |
630 | $plugin->parse_volname($tmpvolname); |
631 | }; | |
32437ed2 | 632 | |
ff00afd7 | 633 | if ($basename && defined($basevmid) && $basevmid == $vmid && $basename eq $name) { |
32437ed2 DM |
634 | die "base volume '$volname' is still in use " . |
635 | "(use by '$tmpvolname')\n"; | |
636 | } | |
637 | } | |
638 | } | |
35533c68 | 639 | $cleanup_worker = $plugin->free_image($storeid, $scfg, $volname, $isBase, $format); |
1dc01b9f | 640 | }); |
b6cf0a66 | 641 | |
1dc01b9f | 642 | return if !$cleanup_worker; |
b6cf0a66 | 643 | |
1dc01b9f DM |
644 | my $rpcenv = PVE::RPCEnvironment::get(); |
645 | my $authuser = $rpcenv->get_user(); | |
b6cf0a66 | 646 | |
1dc01b9f | 647 | $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker); |
b6cf0a66 DM |
648 | } |
649 | ||
b6cf0a66 DM |
650 | #list iso or openvz template ($tt = <iso|vztmpl|backup>) |
651 | sub template_list { | |
652 | my ($cfg, $storeid, $tt) = @_; | |
653 | ||
1a3459ac DM |
654 | die "unknown template type '$tt'\n" |
655 | if !($tt eq 'iso' || $tt eq 'vztmpl' || $tt eq 'backup'); | |
b6cf0a66 DM |
656 | |
657 | my $ids = $cfg->{ids}; | |
658 | ||
659 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
660 | ||
661 | my $res = {}; | |
662 | ||
663 | # query the storage | |
664 | ||
665 | foreach my $sid (keys %$ids) { | |
666 | next if $storeid && $storeid ne $sid; | |
667 | ||
668 | my $scfg = $ids->{$sid}; | |
669 | my $type = $scfg->{type}; | |
670 | ||
671 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
672 | ||
673 | next if $tt eq 'iso' && !$scfg->{content}->{iso}; | |
674 | next if $tt eq 'vztmpl' && !$scfg->{content}->{vztmpl}; | |
675 | next if $tt eq 'backup' && !$scfg->{content}->{backup}; | |
676 | ||
1dc01b9f | 677 | activate_storage($cfg, $sid); |
b6cf0a66 | 678 | |
1dc01b9f DM |
679 | if ($scfg->{path}) { |
680 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 681 | |
1dc01b9f | 682 | my $path = $plugin->get_subdir($scfg, $tt); |
b6cf0a66 DM |
683 | |
684 | foreach my $fn (<$path/*>) { | |
685 | ||
686 | my $info; | |
687 | ||
688 | if ($tt eq 'iso') { | |
689 | next if $fn !~ m!/([^/]+\.[Ii][Ss][Oo])$!; | |
690 | ||
691 | $info = { volid => "$sid:iso/$1", format => 'iso' }; | |
692 | ||
693 | } elsif ($tt eq 'vztmpl') { | |
13d2cb79 | 694 | next if $fn !~ m!/([^/]+\.tar\.([gx]z))$!; |
b6cf0a66 | 695 | |
13d2cb79 | 696 | $info = { volid => "$sid:vztmpl/$1", format => "t$2" }; |
b6cf0a66 DM |
697 | |
698 | } elsif ($tt eq 'backup') { | |
a22854e5 | 699 | next if $fn !~ m!/([^/]+\.(tar|tar\.gz|tar\.lzo|tgz|vma|vma\.gz|vma\.lzo))$!; |
1a3459ac | 700 | |
b6cf0a66 DM |
701 | $info = { volid => "$sid:backup/$1", format => $2 }; |
702 | } | |
703 | ||
704 | $info->{size} = -s $fn; | |
705 | ||
706 | push @{$res->{$sid}}, $info; | |
707 | } | |
708 | ||
709 | } | |
710 | ||
711 | @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid}; | |
712 | } | |
713 | ||
714 | return $res; | |
715 | } | |
716 | ||
20ccac1b | 717 | |
b6cf0a66 DM |
718 | sub vdisk_list { |
719 | my ($cfg, $storeid, $vmid, $vollist) = @_; | |
720 | ||
721 | my $ids = $cfg->{ids}; | |
722 | ||
723 | storage_check_enabled($cfg, $storeid) if ($storeid); | |
724 | ||
725 | my $res = {}; | |
726 | ||
727 | # prepare/activate/refresh all storages | |
728 | ||
b6cf0a66 DM |
729 | my $storage_list = []; |
730 | if ($vollist) { | |
731 | foreach my $volid (@$vollist) { | |
1dc01b9f DM |
732 | my ($sid, undef) = parse_volume_id($volid); |
733 | next if !defined($ids->{$sid}); | |
b6cf0a66 DM |
734 | next if !storage_check_enabled($cfg, $sid, undef, 1); |
735 | push @$storage_list, $sid; | |
b6cf0a66 DM |
736 | } |
737 | } else { | |
738 | foreach my $sid (keys %$ids) { | |
739 | next if $storeid && $storeid ne $sid; | |
740 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
741 | push @$storage_list, $sid; | |
b6cf0a66 DM |
742 | } |
743 | } | |
744 | ||
1dc01b9f | 745 | my $cache = {}; |
b6cf0a66 | 746 | |
1dc01b9f | 747 | activate_storage_list($cfg, $storage_list, $cache); |
b6cf0a66 DM |
748 | |
749 | foreach my $sid (keys %$ids) { | |
1dc01b9f DM |
750 | next if $storeid && $storeid ne $sid; |
751 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
b6cf0a66 | 752 | |
1dc01b9f DM |
753 | my $scfg = $ids->{$sid}; |
754 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
755 | $res->{$sid} = $plugin->list_images($sid, $scfg, $vmid, $vollist, $cache); | |
b6cf0a66 DM |
756 | @{$res->{$sid}} = sort {lc($a->{volid}) cmp lc ($b->{volid}) } @{$res->{$sid}} if $res->{$sid}; |
757 | } | |
758 | ||
759 | return $res; | |
760 | } | |
761 | ||
37ba0aea DM |
762 | sub volume_list { |
763 | my ($cfg, $storeid, $vmid, $content) = @_; | |
764 | ||
765 | my @ctypes = qw(images vztmpl iso backup); | |
766 | ||
767 | my $cts = $content ? [ $content ] : [ @ctypes ]; | |
768 | ||
769 | my $scfg = PVE::Storage::storage_config($cfg, $storeid); | |
770 | ||
771 | my $res = []; | |
772 | foreach my $ct (@$cts) { | |
773 | my $data; | |
774 | if ($ct eq 'images') { | |
775 | $data = vdisk_list($cfg, $storeid, $vmid); | |
776 | } elsif ($ct eq 'iso' && !defined($vmid)) { | |
777 | $data = template_list($cfg, $storeid, 'iso'); | |
778 | } elsif ($ct eq 'vztmpl'&& !defined($vmid)) { | |
779 | $data = template_list ($cfg, $storeid, 'vztmpl'); | |
780 | } elsif ($ct eq 'backup') { | |
781 | $data = template_list ($cfg, $storeid, 'backup'); | |
782 | foreach my $item (@{$data->{$storeid}}) { | |
783 | if (defined($vmid)) { | |
784 | @{$data->{$storeid}} = grep { $_->{volid} =~ m/\S+-$vmid-\S+/ } @{$data->{$storeid}}; | |
785 | } | |
786 | } | |
787 | } | |
788 | ||
789 | next if !$data || !$data->{$storeid}; | |
790 | ||
791 | foreach my $item (@{$data->{$storeid}}) { | |
792 | $item->{content} = $ct; | |
793 | push @$res, $item; | |
794 | } | |
795 | } | |
796 | ||
797 | return $res; | |
798 | } | |
799 | ||
b6cf0a66 DM |
800 | sub uevent_seqnum { |
801 | ||
802 | my $filename = "/sys/kernel/uevent_seqnum"; | |
803 | ||
804 | my $seqnum = 0; | |
1dc01b9f | 805 | if (my $fh = IO::File->new($filename, "r")) { |
b6cf0a66 DM |
806 | my $line = <$fh>; |
807 | if ($line =~ m/^(\d+)$/) { | |
1dc01b9f | 808 | $seqnum = int($1); |
b6cf0a66 DM |
809 | } |
810 | close ($fh); | |
811 | } | |
812 | return $seqnum; | |
813 | } | |
814 | ||
f3d4ef46 | 815 | sub activate_storage { |
1dc01b9f | 816 | my ($cfg, $storeid, $cache) = @_; |
b6cf0a66 | 817 | |
f3d4ef46 DM |
818 | $cache = {} if !$cache; |
819 | ||
b6cf0a66 DM |
820 | my $scfg = storage_check_enabled($cfg, $storeid); |
821 | ||
1dc01b9f | 822 | return if $cache->{activated}->{$storeid}; |
b6cf0a66 | 823 | |
1dc01b9f | 824 | $cache->{uevent_seqnum} = uevent_seqnum() if !$cache->{uevent_seqnum}; |
b6cf0a66 | 825 | |
1dc01b9f | 826 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
b6cf0a66 | 827 | |
1dc01b9f DM |
828 | if ($scfg->{base}) { |
829 | my ($baseid, undef) = parse_volume_id ($scfg->{base}); | |
f3d4ef46 DM |
830 | activate_storage($cfg, $baseid, $cache); |
831 | } | |
832 | ||
833 | if (!$plugin->check_connection($storeid, $scfg)) { | |
834 | die "storage '$storeid' is not online\n"; | |
b6cf0a66 DM |
835 | } |
836 | ||
1dc01b9f DM |
837 | $plugin->activate_storage($storeid, $scfg, $cache); |
838 | ||
b6cf0a66 DM |
839 | my $newseq = uevent_seqnum (); |
840 | ||
841 | # only call udevsettle if there are events | |
1dc01b9f | 842 | if ($newseq > $cache->{uevent_seqnum}) { |
b6cf0a66 DM |
843 | my $timeout = 30; |
844 | system ("$UDEVADM settle --timeout=$timeout"); # ignore errors | |
1dc01b9f | 845 | $cache->{uevent_seqnum} = $newseq; |
b6cf0a66 DM |
846 | } |
847 | ||
1dc01b9f | 848 | $cache->{activated}->{$storeid} = 1; |
b6cf0a66 DM |
849 | } |
850 | ||
851 | sub activate_storage_list { | |
1dc01b9f | 852 | my ($cfg, $storeid_list, $cache) = @_; |
b6cf0a66 | 853 | |
1dc01b9f | 854 | $cache = {} if !$cache; |
b6cf0a66 DM |
855 | |
856 | foreach my $storeid (@$storeid_list) { | |
f3d4ef46 | 857 | activate_storage($cfg, $storeid, $cache); |
b6cf0a66 DM |
858 | } |
859 | } | |
860 | ||
1dc01b9f DM |
861 | sub deactivate_storage { |
862 | my ($cfg, $storeid) = @_; | |
863 | ||
864 | my $scfg = storage_config ($cfg, $storeid); | |
865 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
b6cf0a66 | 866 | |
1dc01b9f DM |
867 | my $cache = {}; |
868 | $plugin->deactivate_storage($storeid, $scfg, $cache); | |
b6cf0a66 DM |
869 | } |
870 | ||
871 | sub activate_volumes { | |
02e797b8 | 872 | my ($cfg, $vollist, $snapname) = @_; |
6703353b DM |
873 | |
874 | return if !($vollist && scalar(@$vollist)); | |
875 | ||
b6cf0a66 DM |
876 | my $storagehash = {}; |
877 | foreach my $volid (@$vollist) { | |
1dc01b9f | 878 | my ($storeid, undef) = parse_volume_id($volid); |
b6cf0a66 DM |
879 | $storagehash->{$storeid} = 1; |
880 | } | |
881 | ||
1dc01b9f DM |
882 | my $cache = {}; |
883 | ||
884 | activate_storage_list($cfg, [keys %$storagehash], $cache); | |
b6cf0a66 DM |
885 | |
886 | foreach my $volid (@$vollist) { | |
5521b580 | 887 | my ($storeid, $volname) = parse_volume_id($volid); |
1dc01b9f DM |
888 | my $scfg = storage_config($cfg, $storeid); |
889 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
02e797b8 | 890 | $plugin->activate_volume($storeid, $scfg, $volname, $snapname, $cache); |
b6cf0a66 DM |
891 | } |
892 | } | |
893 | ||
894 | sub deactivate_volumes { | |
02e797b8 | 895 | my ($cfg, $vollist, $snapname) = @_; |
b6cf0a66 | 896 | |
6703353b DM |
897 | return if !($vollist && scalar(@$vollist)); |
898 | ||
1dc01b9f DM |
899 | my $cache = {}; |
900 | ||
6703353b | 901 | my @errlist = (); |
b6cf0a66 | 902 | foreach my $volid (@$vollist) { |
1dc01b9f | 903 | my ($storeid, $volname) = parse_volume_id($volid); |
b6cf0a66 | 904 | |
1dc01b9f DM |
905 | my $scfg = storage_config($cfg, $storeid); |
906 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); | |
1a3459ac | 907 | |
1dc01b9f | 908 | eval { |
02e797b8 | 909 | $plugin->deactivate_volume($storeid, $scfg, $volname, $snapname, $cache); |
1dc01b9f DM |
910 | }; |
911 | if (my $err = $@) { | |
912 | warn $err; | |
913 | push @errlist, $volid; | |
b6cf0a66 DM |
914 | } |
915 | } | |
6703353b DM |
916 | |
917 | die "volume deativation failed: " . join(' ', @errlist) | |
918 | if scalar(@errlist); | |
b6cf0a66 DM |
919 | } |
920 | ||
1a3459ac | 921 | sub storage_info { |
b6cf0a66 DM |
922 | my ($cfg, $content) = @_; |
923 | ||
924 | my $ids = $cfg->{ids}; | |
925 | ||
926 | my $info = {}; | |
583c2802 DM |
927 | |
928 | my @ctypes = PVE::Tools::split_list($content); | |
929 | ||
b6cf0a66 DM |
930 | my $slist = []; |
931 | foreach my $storeid (keys %$ids) { | |
932 | ||
b6cf0a66 DM |
933 | next if !storage_check_enabled($cfg, $storeid, undef, 1); |
934 | ||
d73060be DM |
935 | if (defined($content)) { |
936 | my $want_ctype = 0; | |
937 | foreach my $ctype (@ctypes) { | |
938 | if ($ids->{$storeid}->{content}->{$ctype}) { | |
939 | $want_ctype = 1; | |
940 | last; | |
941 | } | |
583c2802 | 942 | } |
d73060be | 943 | next if !$want_ctype; |
583c2802 | 944 | } |
d73060be | 945 | |
b6cf0a66 DM |
946 | my $type = $ids->{$storeid}->{type}; |
947 | ||
1a3459ac | 948 | $info->{$storeid} = { |
b6cf0a66 | 949 | type => $type, |
1a3459ac DM |
950 | total => 0, |
951 | avail => 0, | |
952 | used => 0, | |
04a2e4f3 | 953 | shared => $ids->{$storeid}->{shared} ? 1 : 0, |
1dc01b9f | 954 | content => PVE::Storage::Plugin::content_hash_to_string($ids->{$storeid}->{content}), |
b6cf0a66 DM |
955 | active => 0, |
956 | }; | |
957 | ||
b6cf0a66 DM |
958 | push @$slist, $storeid; |
959 | } | |
960 | ||
1dc01b9f | 961 | my $cache = {}; |
b6cf0a66 | 962 | |
b6cf0a66 DM |
963 | foreach my $storeid (keys %$ids) { |
964 | my $scfg = $ids->{$storeid}; | |
b6cf0a66 DM |
965 | next if !$info->{$storeid}; |
966 | ||
f3d4ef46 DM |
967 | eval { activate_storage($cfg, $storeid, $cache); }; |
968 | if (my $err = $@) { | |
969 | warn $err; | |
970 | next; | |
971 | } | |
972 | ||
1dc01b9f | 973 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); |
7028645e DM |
974 | my ($total, $avail, $used, $active); |
975 | eval { ($total, $avail, $used, $active) = $plugin->status($storeid, $scfg, $cache); }; | |
976 | warn $@ if $@; | |
1dc01b9f | 977 | next if !$active; |
1a3459ac DM |
978 | $info->{$storeid}->{total} = $total; |
979 | $info->{$storeid}->{avail} = $avail; | |
980 | $info->{$storeid}->{used} = $used; | |
1dc01b9f | 981 | $info->{$storeid}->{active} = $active; |
b6cf0a66 DM |
982 | } |
983 | ||
984 | return $info; | |
985 | } | |
986 | ||
987 | sub resolv_server { | |
988 | my ($server) = @_; | |
1a3459ac | 989 | |
c67daeac WB |
990 | my ($packed_ip, $family); |
991 | eval { | |
992 | my @res = PVE::Tools::getaddrinfo_all($server); | |
993 | $family = $res[0]->{family}; | |
994 | $packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2]; | |
995 | }; | |
b6cf0a66 | 996 | if (defined $packed_ip) { |
ee302b1c | 997 | return Socket::inet_ntop($family, $packed_ip); |
b6cf0a66 DM |
998 | } |
999 | return undef; | |
1000 | } | |
1001 | ||
1002 | sub scan_nfs { | |
1003 | my ($server_in) = @_; | |
1004 | ||
1005 | my $server; | |
1006 | if (!($server = resolv_server ($server_in))) { | |
1007 | die "unable to resolve address for server '${server_in}'\n"; | |
1008 | } | |
1009 | ||
1010 | my $cmd = ['/sbin/showmount', '--no-headers', '--exports', $server]; | |
1011 | ||
1012 | my $res = {}; | |
f81372ac | 1013 | run_command($cmd, outfunc => sub { |
b6cf0a66 DM |
1014 | my $line = shift; |
1015 | ||
1016 | # note: howto handle white spaces in export path?? | |
1017 | if ($line =~ m!^(/\S+)\s+(.+)$!) { | |
1018 | $res->{$1} = $2; | |
1019 | } | |
1020 | }); | |
1021 | ||
1022 | return $res; | |
1023 | } | |
1024 | ||
584d97f6 DM |
1025 | sub scan_zfs { |
1026 | ||
3932390b | 1027 | my $cmd = ['zfs', 'list', '-t', 'filesystem', '-H', '-o', 'name,avail,used']; |
584d97f6 DM |
1028 | |
1029 | my $res = []; | |
1030 | run_command($cmd, outfunc => sub { | |
1031 | my $line = shift; | |
1032 | ||
1033 | if ($line =~m/^(\S+)\s+(\S+)\s+(\S+)$/) { | |
3932390b | 1034 | my ($pool, $size_str, $used_str) = ($1, $2, $3); |
584d97f6 | 1035 | my $size = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($size_str); |
3932390b | 1036 | my $used = PVE::Storage::ZFSPoolPlugin::zfs_parse_size($used_str); |
48e27f79 DM |
1037 | # ignore subvolumes generated by our ZFSPoolPlugin |
1038 | return if $pool =~ m!/subvol-\d+-[^/]+$!; | |
3932390b | 1039 | push @$res, { pool => $pool, size => $size, free => $size-$used }; |
584d97f6 DM |
1040 | } |
1041 | }); | |
1042 | ||
1043 | return $res; | |
1044 | } | |
1045 | ||
b6cf0a66 DM |
1046 | sub resolv_portal { |
1047 | my ($portal, $noerr) = @_; | |
1048 | ||
1689e627 WB |
1049 | my ($server, $port) = PVE::Tools::parse_host_and_port($portal); |
1050 | if ($server) { | |
b6cf0a66 DM |
1051 | if (my $ip = resolv_server($server)) { |
1052 | $server = $ip; | |
1689e627 | 1053 | $server = "[$server]" if $server =~ /^$IPV6RE$/; |
b6cf0a66 DM |
1054 | return $port ? "$server:$port" : $server; |
1055 | } | |
1056 | } | |
1057 | return undef if $noerr; | |
1058 | ||
1059 | raise_param_exc({ portal => "unable to resolve portal address '$portal'" }); | |
1060 | } | |
1061 | ||
1062 | # idea is from usbutils package (/usr/bin/usb-devices) script | |
1063 | sub __scan_usb_device { | |
1064 | my ($res, $devpath, $parent, $level) = @_; | |
1065 | ||
1066 | return if ! -d $devpath; | |
1067 | return if $level && $devpath !~ m/^.*[-.](\d+)$/; | |
1068 | my $port = $level ? int($1 - 1) : 0; | |
1069 | ||
1070 | my $busnum = int(file_read_firstline("$devpath/busnum")); | |
1071 | my $devnum = int(file_read_firstline("$devpath/devnum")); | |
1072 | ||
1073 | my $d = { | |
1074 | port => $port, | |
1075 | level => $level, | |
1076 | busnum => $busnum, | |
1077 | devnum => $devnum, | |
1078 | speed => file_read_firstline("$devpath/speed"), | |
1079 | class => hex(file_read_firstline("$devpath/bDeviceClass")), | |
1080 | vendid => file_read_firstline("$devpath/idVendor"), | |
1081 | prodid => file_read_firstline("$devpath/idProduct"), | |
1082 | }; | |
1083 | ||
1084 | if ($level) { | |
1085 | my $usbpath = $devpath; | |
1086 | $usbpath =~ s|^.*/\d+\-||; | |
1087 | $d->{usbpath} = $usbpath; | |
1088 | } | |
1089 | ||
1090 | my $product = file_read_firstline("$devpath/product"); | |
1091 | $d->{product} = $product if $product; | |
1a3459ac | 1092 | |
b6cf0a66 DM |
1093 | my $manu = file_read_firstline("$devpath/manufacturer"); |
1094 | $d->{manufacturer} = $manu if $manu; | |
1095 | ||
1096 | my $serial => file_read_firstline("$devpath/serial"); | |
1097 | $d->{serial} = $serial if $serial; | |
1098 | ||
1099 | push @$res, $d; | |
1100 | ||
1101 | foreach my $subdev (<$devpath/$busnum-*>) { | |
1102 | next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|; | |
1103 | __scan_usb_device($res, $subdev, $devnum, $level + 1); | |
1104 | } | |
1105 | ||
1106 | }; | |
1107 | ||
1108 | sub scan_usb { | |
1109 | ||
1110 | my $devlist = []; | |
1111 | ||
1112 | foreach my $device (</sys/bus/usb/devices/usb*>) { | |
1113 | __scan_usb_device($devlist, $device, 0, 0); | |
1114 | } | |
1115 | ||
1116 | return $devlist; | |
1117 | } | |
1118 | ||
1119 | sub scan_iscsi { | |
1120 | my ($portal_in) = @_; | |
1121 | ||
1122 | my $portal; | |
1dc01b9f | 1123 | if (!($portal = resolv_portal($portal_in))) { |
b6cf0a66 DM |
1124 | die "unable to parse/resolve portal address '${portal_in}'\n"; |
1125 | } | |
1126 | ||
1dc01b9f | 1127 | return PVE::Storage::ISCSIPlugin::iscsi_discovery($portal); |
b6cf0a66 DM |
1128 | } |
1129 | ||
1130 | sub storage_default_format { | |
1131 | my ($cfg, $storeid) = @_; | |
1132 | ||
1133 | my $scfg = storage_config ($cfg, $storeid); | |
1134 | ||
1dc01b9f | 1135 | return PVE::Storage::Plugin::default_format($scfg); |
b6cf0a66 DM |
1136 | } |
1137 | ||
1138 | sub vgroup_is_used { | |
1139 | my ($cfg, $vgname) = @_; | |
1140 | ||
1141 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1142 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
1143 | if ($scfg->{type} eq 'lvm' && $scfg->{vgname} eq $vgname) { |
1144 | return 1; | |
1145 | } | |
1146 | } | |
1147 | ||
1148 | return undef; | |
1149 | } | |
1150 | ||
1151 | sub target_is_used { | |
1152 | my ($cfg, $target) = @_; | |
1153 | ||
1154 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1155 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
1156 | if ($scfg->{type} eq 'iscsi' && $scfg->{target} eq $target) { |
1157 | return 1; | |
1158 | } | |
1159 | } | |
1160 | ||
1161 | return undef; | |
1162 | } | |
1163 | ||
1164 | sub volume_is_used { | |
1165 | my ($cfg, $volid) = @_; | |
1166 | ||
1167 | foreach my $storeid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1168 | my $scfg = storage_config($cfg, $storeid); |
b6cf0a66 DM |
1169 | if ($scfg->{base} && $scfg->{base} eq $volid) { |
1170 | return 1; | |
1171 | } | |
1172 | } | |
1173 | ||
1174 | return undef; | |
1175 | } | |
1176 | ||
1177 | sub storage_is_used { | |
1178 | my ($cfg, $storeid) = @_; | |
1179 | ||
1180 | foreach my $sid (keys %{$cfg->{ids}}) { | |
1dc01b9f | 1181 | my $scfg = storage_config($cfg, $sid); |
b6cf0a66 | 1182 | next if !$scfg->{base}; |
1dc01b9f | 1183 | my ($st) = parse_volume_id($scfg->{base}); |
b6cf0a66 DM |
1184 | return 1 if $st && $st eq $storeid; |
1185 | } | |
1186 | ||
1187 | return undef; | |
1188 | } | |
1189 | ||
1190 | sub foreach_volid { | |
1191 | my ($list, $func) = @_; | |
1192 | ||
1193 | return if !$list; | |
1194 | ||
1195 | foreach my $sid (keys %$list) { | |
1196 | foreach my $info (@{$list->{$sid}}) { | |
1197 | my $volid = $info->{volid}; | |
1dc01b9f | 1198 | my ($sid1, $volname) = parse_volume_id($volid, 1); |
b6cf0a66 DM |
1199 | if ($sid1 && $sid1 eq $sid) { |
1200 | &$func ($volid, $sid, $info); | |
1201 | } else { | |
1202 | warn "detected strange volid '$volid' in volume list for '$sid'\n"; | |
1203 | } | |
1204 | } | |
1205 | } | |
1206 | } | |
1207 | ||
f7621c01 DM |
1208 | # bash completion helper |
1209 | ||
1210 | sub complete_storage { | |
746e530f | 1211 | my ($cmdname, $pname, $cvalue) = @_; |
f7621c01 | 1212 | |
746e530f | 1213 | my $cfg = PVE::Storage::config(); |
180c8b02 | 1214 | |
746e530f | 1215 | return $cmdname eq 'add' ? [] : [ PVE::Storage::storage_ids($cfg) ]; |
f7621c01 DM |
1216 | } |
1217 | ||
1218 | sub complete_storage_enabled { | |
746e530f | 1219 | my ($cmdname, $pname, $cvalue) = @_; |
f7621c01 | 1220 | |
746e530f | 1221 | my $res = []; |
f7621c01 | 1222 | |
746e530f DM |
1223 | my $cfg = PVE::Storage::config(); |
1224 | foreach my $sid (keys %{$cfg->{ids}}) { | |
1225 | next if !storage_check_enabled($cfg, $sid, undef, 1); | |
1226 | push @$res, $sid; | |
1227 | } | |
1228 | return $res; | |
f7621c01 DM |
1229 | } |
1230 | ||
98437f4c DM |
1231 | sub complete_content_type { |
1232 | my ($cmdname, $pname, $cvalue) = @_; | |
1233 | ||
1234 | return [qw(rootdir images vztmpl iso backup)]; | |
1235 | } | |
1236 | ||
bf7aed26 DM |
1237 | sub complete_volume { |
1238 | my ($cmdname, $pname, $cvalue) = @_; | |
1239 | ||
1240 | my $cfg = config(); | |
1241 | ||
1242 | my $storage_list = complete_storage_enabled(); | |
1243 | ||
b70b0c58 DM |
1244 | if ($cvalue =~ m/^([^:]+):/) { |
1245 | $storage_list = [ $1 ]; | |
1246 | } else { | |
1247 | if (scalar(@$storage_list) > 1) { | |
1248 | # only list storage IDs to avoid large listings | |
1249 | my $res = []; | |
1250 | foreach my $storeid (@$storage_list) { | |
1251 | # Hack: simply return 2 artificial values, so that | |
1252 | # completions does not finish | |
1253 | push @$res, "$storeid:volname", "$storeid:..."; | |
1254 | } | |
1255 | return $res; | |
1256 | } | |
1257 | } | |
1258 | ||
bf7aed26 DM |
1259 | my $res = []; |
1260 | foreach my $storeid (@$storage_list) { | |
1261 | my $vollist = PVE::Storage::volume_list($cfg, $storeid); | |
1262 | ||
1263 | foreach my $item (@$vollist) { | |
1264 | push @$res, $item->{volid}; | |
1265 | } | |
1266 | } | |
1267 | ||
1268 | return $res; | |
1269 | } | |
1270 | ||
b6cf0a66 | 1271 | 1; |