blib/lib/StorageDisplay.pm

filter: Statementstmt 87.7% 316 / 360 Branchbran 69.7% 53 / 76 Conditioncond 70.7% 29 / 41 MC/DCmcdc 40.9% 9 / 22 Subroutinesub 94.7% 54 / 57 Podpod 0.0% 0 / 29 totaltotal 78.8% 461 / 585 CC 80 SCAR 14.8
CC
80
Cov
83%
createPartitionTable
10.3
removeVMsPartitions
10.0
__ANON__
7.2
CRAP 4.4
« blib/lib/StorageDisplay/Data/Root.pm ↑ Summary blib/lib/StorageDisplay/Block.pm »
1 #
2 # This file is part of StorageDisplay
3 #
4 # This software is copyright (c) 2014-2023 by Vincent Danjean.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use 5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 package StorageDisplay;
14 # ABSTRACT: Collect and display storages on linux machines
15  
16 9 our $VERSION = '2.06'; # VERSION
17  
18 ## Main object
19  
20 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶use namespace::sweep;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 9 ▶use StorageDisplay::Block;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 9 ▶use StorageDisplay::Data::Root;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 9 ▶use StorageDisplay::Data::Partition;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 9 ▶use StorageDisplay::Data::LVM;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 9 ▶use StorageDisplay::Data::RAID;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 9 ▶use StorageDisplay::Data::LUKS;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 9 ▶use StorageDisplay::Data::FS;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 9 ▶use StorageDisplay::Data::Libvirt;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 9 ▶use StorageDisplay::Data::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 has 'blocks' => (
34 is => 'ro',
35 isa => 'HashRef[StorageDisplay::Block]',
36 traits => [ 'Hash' ],
37 7 ▶ default => sub { return {}; },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 9 lazy => 1,
39 handles => {
40 'addBlock' => 'set',
41 'has_block' => 'exists',
42 '_block' => 'get',
43 'allBlocks' => 'values'
44 },
45 );
46  
47 9 has 'blocksRoot' => (
48 is => 'ro',
49 isa => 'StorageDisplay::BlockTreeElement',
50 lazy => 1,
51 builder => '_loadAllBlocks',
52 );
53  
54 9 has 'infos' => (
55 is => 'ro',
56 isa => 'HashRef',
57 required => 1,
58 traits => [ 'Hash' ],
59 # handles => {
60 # 'get_info' => 'get',
61 # }
62 );
63  
64 1072 ▶sub get_info {
Subroutine get_info: called CC=3 SCAR=11.0
Pod undocumented
65 1072 my $self = shift ;
66 1072 my @keys=@_;
67  
68 1072 my $infos=$self->infos;
69  
70 1072 ▶ while (defined(my $k = shift @keys)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $k = shift @keys)) 2347 1031 3378
71 2347 ▶ return if not defined($infos->{$k});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $infos->{$k} 41 2306 2347
72 2306 $infos = $infos->{$k};
73 }
74 1031 return $infos;
75 }
76  
77 #has 'connect' => (
78 # is => 'ro',
79 # isa => 'StorageDisplay::Connect',
80 # required => 0,
81 # );
82  
83 1328 ▶sub _allocateBlock {
Subroutine _allocateBlock: called CC=4 SCAR=13.9
84 1328 my $self=shift;
85 1328 my $name=shift;
86 1328 my $alloc=shift;
87  
88 1328 ▶ if (! $self->has_block($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_block($name)) 721 607 1328
89 721 my $block=$alloc->();
90 721 foreach my $n ($block->names_str()) {
91 1677 ▶ if ($self->has_block($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_block($n)) { } 6 1671 1677
92 6 print STDERR "W: duplicate block name '$n' for ".$block->name.
93 " and ".$self->_block($n)->name."\n";
94 } else {
95 #print STDERR "I: in $self Registering block name '$n' for ".$block->name."\n";
96 }
97 1677 $self->addBlock($n, $block);
98 }
99 }
100 1328 return $self->_block($name);
101 }
102  
103 336 ▶sub systemBlock {
Subroutine systemBlock: called CC=1 SCAR=0.0
Pod undocumented
104 336 my $self=shift;
105 336 my $name=shift;
106  
107 return $self->_allocateBlock(
108 233 ▶ $name, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
109 233 return StorageDisplay::Block::System->new(
110 $name,
111 $self);
112 336 });
113 }
114  
115 992 ▶sub block {
Subroutine block: called CC=2 SCAR=6.9
Pod undocumented
116 992 my $self=shift;
117 992 my $name=shift;
118  
119 992 ▶ if ($name =~m,^/dev/(.*)$,) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ m[^/dev/(.*)$]u) 208 784 992
120 208 $name=$1;
121 }
122 return $self->_allocateBlock(
123 488 ▶ $name, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
124 488 return StorageDisplay::Block::NoSystem->new(
125 'name' => $name,
126 );
127 992 });
128 }
129  
130 4 ▶sub blockBySerial {
Subroutine blockBySerial: called CC=6 SCAR=18.0
Pod undocumented
131 4 my $self=shift;
132 4 my $serial=shift;
133  
134 4 foreach my $block ($self->allBlocks()) {
135 #print STDERR " Testing ", ($block->name), "\n";
136 271 ▶ if (($block->blk_info('SERIAL')//'') eq $serial) {
Branch: 1 branch
BranchTrueFalseTotal
if (($block->blk_info("SERIAL") // "") eq $serial) 2 269 271
Condition100%2/2
$block->blk_info("SERIAL") // ""
l!l
38233
MC/DC100%1/1
$block->blk_info("SERIAL") // ""
$block->blk_info("SERIAL")
Truth table100%2/2
A
A= $block->blk_info("SERIAL")
Aresult
11
00
137 2 return $block;
138 }
139 269 ▶ if (($block->udev_info('ID_SCSI_SERIAL')//'') eq $serial) {
Branch: 1 branch
BranchTrueFalseTotal
if (($block->udev_info("ID_SCSI_SERIAL") // "") eq $serial) 0 269 269
Condition100%2/2
$block->udev_info("ID_SCSI_SERIAL") // ""
l!l
55214
MC/DC100%1/1
$block->udev_info("ID_SCSI_SERIAL") // ""
$block->udev_info("ID_SCSI_SERIAL")
Truth table100%2/2
A
A= $block->udev_info("ID_SCSI_SERIAL")
Aresult
11
00
140 0 return $block;
141 }
142 # WWN is not always unique :-(
143 #$serial =~ s/^0x//;
144 #if (($block->blk_info('WWN')//'') eq $serial) {
145 # return $block;
146 #}
147 #if (($block->blk_info('WWN')//'') eq '0x'.$serial) {
148 # return $block;
149 #}
150 }
151 #print STDERR "$serial not found in $self\n";
152 2 return;
153 }
154  
155 7 ▶sub _loadAllBlocks {
Subroutine _loadAllBlocks: called CC=2 SCAR=6.9
156 7 my $self=shift;
157  
158 7 my $blocks=$self->get_info('lsblk-hierarchy');
159  
160 7 my $handle_bloc;
161 336 ▶ $handle_bloc = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
162 336 my $jcur = shift;
163 336 my $bparent = shift;
164 336 ▶ my @children = (@{$jcur->{'children'}//[]});
Condition100%2/2
$jcur->{"children"} // []
l!l
96240
MC/DC100%1/1
$jcur->{"children"} // []
$jcur->{"children"}
Truth table100%2/2
A
A= $jcur->{"children"}
Aresult
11
00
165 #print STDERR Dumper($jcur);
166 336 my $bcur = $self->systemBlock($jcur->{'kname'});
167 336 $bparent->addChild($bcur);
168 336 foreach my $jchild (@children) {
169 262 my $bchild = $handle_bloc->($jchild, $bcur);
170 }
171 336 return $bcur;
172 7 };
173  
174 7 my $root=StorageDisplay::BlockTreeElement->new('name' => 'Root');
175  
176 7 foreach my $b (values %$blocks) {
177 74 $handle_bloc->($b, $root);
178 }
179 7 return $root;
180 }
181  
182 0 ▶sub dumpBlocks {
Subroutine dumpBlocks: not called CC=2 SCAR=17.9
Pod undocumented
183 0 my $self = shift;
184  
185 0 foreach my $b ($self->allBlocks) {
186 0 print $b->name, "\n";
187 }
188 }
189  
190 481 ▶sub _log {
Subroutine _log: called CC=3 SCAR=11.0
191 481 my $self = shift;
192 481 my $opts = shift;
193 481 my $info = shift;
194  
195 481 ▶ if (ref($info) =~ /^HASH/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($info) =~ /^HASH/u) 391 90 481
196 391 $opts = { %{$opts}, %{$info} };
197 391 $info = shift;
198 }
199  
200 481 print STDERR $opts->{type}, ': ', (' 'x$opts->{level}), $info, "\n";
201 481 foreach my $line (@_) {
202 0 print STDERR ' ', (' 'x$opts->{level}), $line, "\n";
203 }
204 }
205  
206  
207 481 ▶sub log {
Subroutine log: called CC=1 SCAR=0.0
Pod undocumented
208 481 my $self = shift;
209  
210 481 return $self->_log(
211 {
212 'level' => 0,
213 'type' => 'I',
214 'verbose' => 1,
215 }, @_);
216 }
217  
218 0 ▶sub warn {
Subroutine warn: not called CC=1 SCAR=6.9
Pod undocumented
219 0 my $self = shift;
220  
221 0 return $self->_log(
222 {
223 'level' => 0,
224 'type' => 'W',
225 'verbose' => 1,
226 }, @_);
227 }
228  
229 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod undocumented
230 0 my $self = shift;
231  
232 0 return $self->_log(
233 {
234 'level' => 0,
235 'type' => 'E',
236 'verbose' => 1,
237 }, @_);
238 }
239  
240 ###################
241 has '_providedBlocks' => (
242 is => 'ro',
243 isa => 'HashRef[StorageDisplay::Data::Elem]',
244 traits => [ 'Hash' ],
245 2 ▶ default => sub { return {}; },
Subroutine __ANON__: called CC=1 SCAR=0.0
246 9 lazy => 1,
247 handles => {
248 '_addProvidedBlock' => 'set',
249 '_provideBlock' => 'exists',
250 }
251 );
252  
253 has 'elemsRoot' => (
254 is => 'ro',
255 isa => 'StorageDisplay::Data::Root',
256 7 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
257 7 my $self = shift;
258 7 return StorageDisplay::Data::Root->new(
259 $self->get_info('hostname'));
260 },
261 9 lazy => 1,
262 );
263  
264 62 ▶sub _registerElement {
Subroutine _registerElement: called CC=4 SCAR=14.2
265 62 my $self = shift;
266 62 my $elem = shift;
267 my @providedBlockNames = map {
268 62 StorageDisplay::Block::asname($_)
269 } $elem->allProvidedBlocks;
270  
271 62 foreach my $bn (@providedBlockNames) {
272 3 ▶ if ($self->provide($bn)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->provide($bn)) 0 3 3
273 0 carp "Duplicate provider for $bn";
274 0 return 0;
275 }
276 }
277 62 foreach my $bn (@providedBlockNames) {
278 3 $self->_addProvidedBlock($bn, $elem);
279 }
280 #use Data::Dumper;
281 #print STDERR Dumper($elem);
282 #print STDERR $elem->isa("StorageDisplay::Data::Elem"), " DONE\n";
283 62 $self->elemsRoot->addChild($elem);
284 62 return 1;
285 }
286  
287 3 ▶sub provide {
Subroutine provide: called CC=1 SCAR=0.0
Pod undocumented
288 3 my $self = shift;
289 3 my $block = shift;
290 3 my $blockname = StorageDisplay::Block::asname($block);
291  
292 3 return $self->_provideBlock($blockname);
293 }
294  
295 7 ▶sub createElems {
Subroutine createElems: called CC=1 SCAR=0.0
Pod undocumented
296 7 my $self = shift;
297 7 $self->blocksRoot();
298 7 my $root=$self->elemsRoot;
299 7 $self->removeVMsPartitions;
300 7 $self->createPartitionTables($root);
301 7 $self->createLVMs($root);
302 7 $self->createLUKSs($root);
303 7 $self->createMDs($root);
304 7 $self->createLSIMegaclis($root);
305 7 $self->createLSISASIrcus($root);
306 7 $self->createFSs($root);
307 7 $self->createVMs($root);
308 7 $self->createLoops($root);
309 # Must be last, to avoid to create already existing disks
310 7 $self->createEmptyDisks($root);
311 7 $self->computeUsedBlocks;
312 }
313  
314 7 ▶sub removeVMsPartitions {
Subroutine removeVMsPartitions: called CC=10 SCAR=23.1
Pod undocumented
315 7 my $self = shift;
316 7 ▶ my $partitions = $self->get_info('partitions')//{};
Condition50%1/2
$self->get_info("partitions") // {}
l!l
70
MC/DC0%0/1
$self->get_info("partitions") // {}
$self->get_info("partitions")
Truth table50%1/2
A
A= $self->get_info("partitions")
Aresult
11
00
317 7 ▶ my $vms = $self->get_info('libvirt')//{};
Condition100%2/2
$self->get_info("libvirt") // {}
l!l
61
MC/DC100%1/1
$self->get_info("libvirt") // {}
$self->get_info("libvirt")
Truth table100%2/2
A
A= $self->get_info("libvirt")
Aresult
11
00
318 7 my $vmblocks={};
319 7 $self->log("Removing partitions of virtual machines disks");
320 7 foreach my $vm (values %$vms) {
321 35 ▶ foreach my $bname (keys %{$vm->{blocks}//{}}) {
Condition100%2/2
$vm->{"blocks"} // {}
l!l
332
MC/DC100%1/1
$vm->{"blocks"} // {}
$vm->{"blocks"}
Truth table100%2/2
A
A= $vm->{"blocks"}
Aresult
11
00
322 58 my $b = $self->block($bname);
323 58 foreach my $n ($b->names_str) {
324 222 ▶ $vmblocks->{$n} = $vm->{name}//1;
Condition50%1/2
$vm->{"name"} // 1
l!l
2220
MC/DC0%0/1
$vm->{"name"} // 1
$vm->{"name"}
Truth table50%1/2
A
A= $vm->{"name"}
Aresult
11
00
325 }
326 }
327 }
328 7 foreach my $p (keys %$partitions) {
329 50 my $b = $self->block($p);
330 50 ▶ if (exists($vmblocks->{$b->name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$vmblocks{$b->name}) 29 21 50
331 29 $self->log({level=>1}, "Removing ".$b->dname." (in VM ".$vmblocks->{$b->name}.")");
332 29 delete($partitions->{$p});
333 }
334 }
335 }
336  
337 7 ▶sub createPartitionTables {
Subroutine createPartitionTables: called CC=4 SCAR=14.0
Pod undocumented
338 7 my $self = shift;
339 7 my $root = shift;
340 7 $self->log("Creating partition tables");
341 7 ▶ if (defined($self->get_info('partitions'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->get_info("partitions")) 7 0 7
342 7 foreach my $p (sort keys %{$self->get_info('partitions')}) {
343 21 ▶ next if defined($self->get_info('partitions', $p, 'dos-extended'));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->get_info("partitions", $p, "dos-extended") 0 21 21
344 21 $self->createPartitionTable($root, $p);
345 }
346 }
347 }
348  
349 21 ▶sub createPartitionTable {
Subroutine createPartitionTable: called CC=7 SCAR=23.3
Pod undocumented
350 21 my $self = shift;
351 21 my $root = shift;
352 21 my $dev = shift;
353  
354 21 my $block = $self->block($dev);
355 21 my $elem;
356  
357 21 my $pttype = $block->blk_info("PTTYPE");
358 21 ▶ $pttype //= $self->get_info('partitions', $dev, 'type');
Condition33%1/3
$pttype //= $self->get_info("partitions", $dev, "type")
l!l&&r!l&&!r
2100
MC/DC0%0/2
$pttype //= $self->get_info("partitions", $dev, "type")
$pttype $self->get_info("partitions", $dev, "type")
Truth table33%1/3
A //= B
A= $pttypeB= $self->get_info("partitions", $dev, "type")
ABresult
1X1
011
000
359 21 ▶ if (! defined($pttype)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pttype) 0 21 21
360 0 $self->error("Unkown partition table for ".$block->name);
361 0 return;
362 }
363  
364 21 ▶ if ($pttype eq "gpt") {
Branch: 2 branches
BranchTrueFalseTotal
if ($pttype eq 'gpt') { } 19 2 21
elsif ($pttype eq 'dos' or $pttype eq 'msdos') { } 2 0 2
365 19 $elem = $root->newChild('Partition::GPT', $block, $self, @_);
366 ▶ } elsif ($pttype eq "dos" || $pttype eq "msdos") {
Condition33%1/3
$pttype eq "dos" or $pttype eq "msdos"
l!l&&r!l&&!r
200
MC/DC0%0/2
$pttype eq "dos" or $pttype eq "msdos"
$pttype eq "dos" $pttype eq "msdos"
Truth table33%1/3
A or B
A= $pttype eq "dos"B= $pttype eq "msdos"
ABresult
1X1
011
000
367 2 $elem = $root->newChild('Partition::MSDOS', $block, $self, @_);
368 } else {
369 0 $self->warn("Unknown partition type ".$pttype." for ".$block->name);
370 0 return;
371 }
372 21 ▶ if (!$self->_registerElement($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_registerElement($elem)) 0 21 21
373 0 $self->error("Cannot register partition table for ".$block->name);
374 0 return;
375 }
376 }
377  
378 7 ▶sub createEmptyDisks {
Subroutine createEmptyDisks: called CC=3 SCAR=11.0
Pod undocumented
379 7 my $self = shift;
380 7 my $root = shift;
381  
382 7 $self->log("Creating disks without partitions");
383 7 ▶ if (defined($self->get_info('disks-no-part'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->get_info("disks-no-part")) 5 2 7
384 5 foreach my $p (sort keys %{$self->get_info('disks-no-part')}) {
385 16 $self->createEmptyDisk($root, $p);
386 }
387 }
388 }
389  
390 16 ▶sub createEmptyDisk {
Subroutine createEmptyDisk: called CC=3 SCAR=11.2
Pod undocumented
391 16 my $self = shift;
392 16 my $root = shift;
393 16 my $dev = shift;
394  
395 16 my $block = $self->block($dev);
396 16 my $elem;
397  
398 16 ▶ if ($block->provided) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block->provided) 13 3 16
399 13 $self->log(" skipping $dev already created");
400 13 return;
401 }
402  
403 3 $elem = $root->newChild('Partition::None', $block, $self, @_);
404 3 ▶ if (!$self->_registerElement($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_registerElement($elem)) 0 3 3
405 0 $self->error("Cannot register empty disk for ".$block->name);
406 0 return;
407 }
408 }
409  
410 7 ▶sub createLVMs {
Subroutine createLVMs: called CC=5 SCAR=17.8
Pod undocumented
411 7 my $self = shift;
412 7 my $root = shift;
413  
414 7 $self->log('Creating LVM volume groups');
415 7 ▶ for my $vgname (sort keys %{$self->get_info('lvm') // {}}) {
Condition50%1/2
$self->get_info("lvm") // {}
l!l
70
MC/DC0%0/1
$self->get_info("lvm") // {}
$self->get_info("lvm")
Truth table50%1/2
A
A= $self->get_info("lvm")
Aresult
11
00
416 9 my $elem;
417 9 ▶ if ($vgname eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($vgname eq '') { } 0 9 9
418 0 $elem = $root->newChild('LVM::UnassignedPVs', $vgname, $self);
419 } else {
420 9 $elem = $root->newChild('LVM::VG', $vgname, $self);
421 }
422 9 ▶ if (!$self->_registerElement($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_registerElement($elem)) 0 9 9
423 0 $self->error("Cannot register LVM vg ".$vgname);
424 0 return;
425 }
426 }
427 }
428  
429 7 ▶sub createLUKSs {
Subroutine createLUKSs: called CC=4 SCAR=14.3
Pod undocumented
430 7 my $self = shift;
431 7 my $root = shift;
432  
433 7 $self->log("Creating LUKS devices");
434 7 ▶ for my $devname (sort keys %{$self->get_info('luks') // {}}) {
Condition100%2/2
$self->get_info("luks") // {}
l!l
52
MC/DC100%1/1
$self->get_info("luks") // {}
$self->get_info("luks")
Truth table100%2/2
A
A= $self->get_info("luks")
Aresult
11
00
435 5 my $elem = $root->newChild('LUKS', $devname, $self);
436 5 ▶ if (!$self->_registerElement($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_registerElement($elem)) 0 5 5
437 0 $self->error("Cannot register LUKS device ".$devname);
438 0 return;
439 }
440 }
441 }
442  
443 7 ▶sub createMDs {
Subroutine createMDs: called CC=6 SCAR=19.4
Pod undocumented
444 7 my $self = shift;
445 7 my $root = shift;
446  
447 7 $self->log("Creating MD devices");
448 7 ▶ for my $devname (sort keys %{$self->get_info('md') // {}}) {
Condition100%2/2
$self->get_info("md") // {}
l!l
52
MC/DC100%1/1
$self->get_info("md") // {}
$self->get_info("md")
Truth table100%2/2
A
A= $self->get_info("md")
Aresult
11
00
449 8 my $elem;
450 8 ▶ if ($self->get_info('md')->{$devname}->{'raid-container'} // 0 eq 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_info('md')->{$devname}{'raid-container'} // !1) { } 0 8 8
Condition50%1/2
$self->get_info("md")->{$devname}{"raid-container"} //
l!l
08
MC/DC0%0/1
$self->get_info("md")->{$devname}{"raid-container"} //
$self->get_info("md")->{$devname}{"raid-container"}
Truth table50%1/2
A
A= $self->get_info("md")->{$devname}{"raid-container"}
Aresult
11
00
451 0 $elem = $root->newChild('RAID::MD::Container', $devname, $self);
452 } else {
453 8 $elem = $root->newChild('RAID::MD', $devname, $self);
454 }
455 8 ▶ if (!$self->_registerElement($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_registerElement($elem)) 0 8 8
456 0 $self->error("Cannot register MD device ".$devname);
457 0 return;
458 }
459 }
460 }
461  
462 7 ▶sub createLSIMegaclis {
Subroutine createLSIMegaclis: called CC=4 SCAR=14.3
Pod undocumented
463 7 my $self = shift;
464 7 my $root = shift;
465  
466 7 $self->log("Creating Megacli controllers");
467 7 ▶ for my $cnum (sort keys %{$self->get_info('lsi-megacli') // {}}) {
Condition100%2/2
$self->get_info("lsi-megacli") // {}
l!l
16
MC/DC100%1/1
$self->get_info("lsi-megacli") // {}
$self->get_info("lsi-megacli")
Truth table100%2/2
A
A= $self->get_info("lsi-megacli")
Aresult
11
00
468 1 my $elem = $root->newChild('RAID::LSI::Megacli', $cnum, $self);
469 1 ▶ if (!$self->_registerElement($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_registerElement($elem)) 0 1 1
470 0 $self->error("Cannot register Megacli controller #".$cnum);
471 0 return;
472 }
473 }
474 }
475  
476 7 ▶sub createLSISASIrcus {
Subroutine createLSISASIrcus: called CC=4 SCAR=14.3
Pod undocumented
477 7 my $self = shift;
478 7 my $root = shift;
479  
480 7 $self->log("Creating SAS LSI controllers");
481 7 ▶ for my $cnum (sort keys %{$self->get_info('lsi-sas-ircu') // {}}) {
Condition100%2/2
$self->get_info("lsi-sas-ircu") // {}
l!l
16
MC/DC100%1/1
$self->get_info("lsi-sas-ircu") // {}
$self->get_info("lsi-sas-ircu")
Truth table100%2/2
A
A= $self->get_info("lsi-sas-ircu")
Aresult
11
00
482 1 my $elem = $root->newChild('RAID::LSI::SASIrcu', $cnum, $self);
483 1 ▶ if (!$self->_registerElement($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_registerElement($elem)) 0 1 1
484 0 $self->error("Cannot register SAS LSI controller #".$cnum);
485 0 return;
486 }
487 }
488 }
489  
490 7 ▶sub createFSs {
Subroutine createFSs: called CC=2 SCAR=7.6
Pod undocumented
491 7 my $self = shift;
492 7 my $root = shift;
493  
494 7 my $elem = $root->newChild('FS', $self);
495 7 ▶ if (!$self->_registerElement($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_registerElement($elem)) 0 7 7
496 0 print STDERR "Cannot register FS\n";
497 0 return;
498 }
499 7 return $elem;
500 }
501  
502 7 ▶sub createVMs {
Subroutine createVMs: called CC=2 SCAR=7.6
Pod undocumented
503 7 my $self = shift;
504 7 my $root = shift;
505  
506 7 my $elem = $root->newChild('Libvirt', $self);
507 7 ▶ if (!$self->_registerElement($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_registerElement($elem)) 0 7 7
508 0 print STDERR "Cannot register Libvirt\n";
509 0 return;
510 }
511 7 return $elem;
512 }
513  
514 7 ▶sub createLoops {
Subroutine createLoops: called CC=4 SCAR=18.7
Pod undocumented
515 7 my $self = shift;
516 7 my $root = shift;
517  
518 7 $self->log("Creating Loop devices");
519 7 ▶ for my $loop (sort keys %{$self->get_info('loops') // {}}) {
Condition50%1/2
$self->get_info("loops") // {}
l!l
70
MC/DC0%0/1
$self->get_info("loops") // {}
$self->get_info("loops")
Truth table50%1/2
A
A= $self->get_info("loops")
Aresult
11
00
520 0 my $elem = $root->newChild('Loop', $loop, $self);
521 0 ▶ if (!$self->_registerElement($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_registerElement($elem)) 0 0 0
522 0 $self->error("Cannot register loop device ".$loop);
523 0 return;
524 }
525 }
526 }
527  
528 7 ▶sub computeUsedBlocks {
Subroutine computeUsedBlocks: called CC=4 SCAR=13.9
Pod undocumented
529 7 my $self = shift;
530  
531 7 my $it = $self->elemsRoot->iterator(recurse => 1);
532 7 ▶ while (defined(my $e=$it->next)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $it->next)) 550 7 557
533 my @blocks = grep {
534 550 $_->provided
535 } $e->consumedBlocks;
536  
537 550 ▶ if (scalar(@blocks)>0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @blocks > 0) 147 403 550
538 147 foreach my $block (@blocks) {
539 147 $block->state("used");
540 #print STDERR "Block ", $block->name, " used due to ", $e->name, "\n";
541 }
542 }
543 #else {
544 # print STDERR "No providers for ",
545 # join(",",
546 # (map { $_->name } $e->consumedBlocks)), "\n";
547 #}
548 }
549  
550 }
551  
552 7 ▶sub display {
Subroutine display: called CC=1 SCAR=0.0
Pod undocumented
553 7 my $self = shift;
554 7 print join("\n", $self->dotNode), "\n";
555 }
556  
557 9 has '_mountpoints' => (
558 is => 'ro',
559 isa => 'HashRef[Num]',
560 traits => [ 'Hash' ],
561 required => 1,
562 lazy => 1,
563 builder => '_compute_mp',
564 handles => {
565 'has_mp' => 'exists',
566 'fs_mountpoint_blockname' => 'get',
567 'fs_mountpoint_id' => 'get',
568 },
569 );
570  
571 7 ▶sub _compute_mp {
Subroutine _compute_mp: called CC=1 SCAR=0.0
572 7 my $self = shift;
573  
574 7 my $st = $self;
575 7 my $allfs = $st->get_info('fs', 'hierarchy');
576 7 my $flatfs = $st->get_info('fs', 'flatfull');
577 7 my $mp = {};
578 7 my $comp_rec;
579 272 ▶ $comp_rec = sub {
Subroutine __ANON__: called CC=7 SCAR=19.8
580 272 my $node = shift;
581 272 my $parent = shift;
582 272 ▶ if (not defined($flatfs->{$node->{id}})) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $$flatfs{$node->{'id'}}) { } 0 272 272
elsif (not defined $$flatfs{$node->{'id'}}{'parent'}) { } 229 43 272
elsif ($$flatfs{$node->{'id'}}{'parent'} != $parent) { } 0 43 43
583 0 $self->warn($node->{id}.' not defined');
584 } elsif (not defined($flatfs->{$node->{id}}->{parent})) {
585 229 $flatfs->{$node->{id}}->{parent} = $parent;
586 } elsif ($flatfs->{$node->{id}}->{parent} != $parent) {
587 $self->warn('FS: wrong parent for '.$node->{target}
588 .' got: '.$flatfs->{$node->{id}}->{parent}
589 0 .' expects: '.$parent);
590 }
591 272 ▶ if ($parent != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent != 1) 265 7 272
592 265 push @{$flatfs->{$parent}->{children}}, $node->{id};
593 }
594 272 $mp->{$node->{target}} = $node->{id};
595 272 ▶ if (exists($node->{children})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{"children"}) 62 210 272
596 62 for my $child (@{$node->{children}}) {
597 265 $comp_rec->($child, $node->{id});
598 }
599 }
600 7 };
601 7 $comp_rec->($allfs, 1);
602 7 return $mp;
603 }
604  
605 9 ▶around 'fs_mountpoint_blockname' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.5
606 9 my $orig = shift;
607 9 my $self = shift;
608 9 my $mountpoint = shift;
609  
610 # must return an unique (per machine) fake blockname
611 # for the provided mount point
612 9 ▶ if (not $self->has_mp($mountpoint)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_mp($mountpoint)) 0 9 9
613 0 $self->warn("No mountpoint for $mountpoint");
614 0 return 'FS@-1@'.$mountpoint;
615 }
616 9 my $id = $self->$orig($mountpoint, @_);
617  
618 9 return $self->fs_mountpoint_blockname_by_id($id, $mountpoint);
619 9 };
620  
621 313 ▶sub fs_mountpoint_blockname_by_id {
Subroutine fs_mountpoint_blockname_by_id: called CC=3 SCAR=11.4
Pod undocumented
622 313 my $self = shift;
623 313 my $id = shift;
624 313 my $mp = shift;
625  
626 313 my $target = $self->get_info('fs', 'flatfull', $id)->{target};
627  
628 313 ▶ if (defined($mp) and not ($target eq $mp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mp and not $target eq $mp) 0 313 313
Condition66%2/3
defined $mp and not $target eq $mp
!ll&&!rl&&r
322810
MC/DC0%0/2
defined $mp and not $target eq $mp
defined $mp not $target eq $mp
Truth table66%2/3
A and not(B)
A= defined $mpB= not $target eq $mp
ABresult
0X0
100
111
629 0 $self->warn("Bad mountpoint: got $mp, expects $target");
630 }
631  
632 313 return 'FS@'.$id.'@'.$target;
633 }
634  
635 5 ▶sub fs_swap_blockname {
Subroutine fs_swap_blockname: called CC=1 SCAR=0.0
Pod undocumented
636 # must return an unique (per machine) fake blockname
637 # for the provided device/file swap
638 5 my $self = shift;
639 5 my $swappath = shift;
640  
641 5 return 'FS@SWAP@'.$swappath;
642 }
643  
644 # FIXME: to remove when StorageDisplay will be a StorageDisplay::Data::Elem
645 7 ▶sub pushDotText {
Subroutine pushDotText: called CC=2 SCAR=7.0
Pod undocumented
646 7 my $self = shift;
647 7 my $text = shift;
648 7 ▶ my $t = shift // "\t";
Condition50%1/2
shift() // "\t"
l!l
70
MC/DC0%0/1
shift() // "\t"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
649  
650 7 my @pushed = map { $t.$_ } @_;
651 7 push @{$text}, @pushed;
652 }
653  
654 7 ▶sub dotNode {
Subroutine dotNode: called CC=2 SCAR=7.0
Pod undocumented
655 7 my $self = shift;
656 7 ▶ my $t = shift // "\t";
Condition50%1/2
shift() // "\t"
l!l
07
MC/DC0%0/1
shift() // "\t"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
657 7 my @text = map { $_." // HEADER: MACHINE"} (
658 'digraph "'.$self->elemsRoot->host.'"{',
659 $t."rankdir=LR;",
660 );
661 7 $self->pushDotText(\@text, $t, $self->elemsRoot->dotNode("$t"));
662 7 push @text, "} // FOOTER: MACHINE";
663  
664 7 return @text;
665 }
666  
667 9 1;
668  
669 __END__
« blib/lib/StorageDisplay/Data/Root.pm ↑ Summary blib/lib/StorageDisplay/Block.pm »