LVM2/Utils.pm

filter: Statementstmt 56.8% 50 / 88 Branchbran 14.2% 4 / 28 Conditioncond 28.5% 2 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 85.7% 12 / 14 Podpod 100% 4 / 4 totaltotal 49.6% 72 / 145 CC 20 SCAR 30.3
CC
20
Cov
46%
translate_devmapper_name
58.0
find_devicemapper_major
30.0
translate_lvm_name
6.1
CRAP 20.8
« LVM2.pm ↑ Summary LVM2/Snapshot.pm »
1 package Linux::LVM2::Utils;
2 {
3 4 $Linux::LVM2::Utils::VERSION = '0.14';
4 }
5 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 $Linux::LVM2::Utils::AUTHORITY = 'cpan:TEX';
7 }
8 # ABSTRACT: Linux LVM2 helper
9  
10 4 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # use IO::Handle;
18 # use autodie;
19  
20 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use POSIX qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # translate_devmapper_name
25 # inspired by translate_devicemapper_name from the munin diskstats plugin
26 # which was written by Michael Renner <michael.renner@amd.co.at>
27 0 ▶sub translate_devmapper_name {
Subroutine translate_devmapper_name: not called CC=8 SCAR=40.6
Pod documented
28 0 my $device = shift;
29  
30 0 my $want_minor;
31  
32 0 ▶ if($device =~ m/^dm-(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($device =~ /^dm-(\d+)$/) 0 0 0
33 0 $want_minor = $1;
34 }
35  
36 0 ▶ if(!$want_minor) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($want_minor) 0 0 0
37 0 carp "No devicemapper id found\n";
38 0 return;
39 }
40  
41 0 my $dm_major = find_dm_major();
42  
43 0 ▶ if(!$dm_major) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dm_major) 0 0 0
44 0 carp "No devicemapper major id found\n";
45 0 return;
46 }
47  
48 0 ▶ foreach my $dirent ( glob('/dev/mapper/*')) {
Subroutine BEGIN: called CC=1 SCAR=0.2
49 0 my $rdev = (stat($dirent))[6];
50 0 my $major = POSIX::floor($rdev / 256 );
51 0 my $minor = $rdev % 256;
52  
53 0 ▶ if( $major == $dm_major && $minor == $want_minor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($major == $dm_major and $minor == $want_minor) 0 0 0
Condition0%0/3
$major == $dm_major and $minor == $want_minor
!ll&&!rl&&r
000
MC/DC0%0/2
$major == $dm_major and $minor == $want_minor
$major == $dm_major $minor == $want_minor
Truth table0%0/3
A and B
A= $major == $dm_majorB= $minor == $want_minor
ABresult
0X0
100
111
54 0 my $display_name = translate_lvm_name($dirent);
55 0 $dirent =~ s#/dev/##;
56  
57 0 ▶ if(defined($display_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $display_name) { } 0 0 0
58 0 return $display_name;
59 } else {
60 0 return $dirent;
61 }
62 }
63 }
64  
65 # fallback in case our search was fruitless
66 0 return $device;
67 }
68  
69 # translate_lvm_name
70 # inspired by translate_lvm_name from the munin diskstats plugin
71 # which was written by Michael Renner <michael.renner@amd.co.at>
72 2 ▶sub translate_lvm_name {
Subroutine translate_lvm_name: called CC=5 SCAR=18.0
Pod documented
73 2 my $device = shift;
74 2 ▶ my $no_stat = shift || 0;
Condition50%1/2
shift() || 0
l!l
20
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
75  
76 2 my $device_name = File::Basename::basename($device);
77  
78 # search for single dashes as this suggests for a lvm devmapper dev
79 2 ▶ if ( $device_name =~ m/(?<!-)-(?!-)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($device_name =~ /(?<!-)-(?!-)/) 2 0 2
80 # split into vg and lv parts
81 2 my ( $vg, $lv ) = split /(?<!-)-(?!-)/, $device_name, 2;
82  
83 # remove superflous dashes from vg and lv names
84 2 $vg =~ s/--/-/g;
85 2 $lv =~ s/--/-/g;
86  
87 2 $device_name = '/dev/'.$vg.'/'.$lv;
88  
89 # assert that the assembled device name actually exists
90 2 ▶ if($no_stat) {
Branch: 2 branches
BranchTrueFalseTotal
if ($no_stat) { } 2 0 2
elsif (stat $device_name) { } 0 0 0
91 2 return $device_name;
92 }
93 elsif( stat($device_name)) {
94 0 return $device_name;
95 }
96 }
97  
98 0 return;
99 }
100  
101 # translate_mapper_name
102 2 ▶sub translate_mapper_name {
Subroutine translate_mapper_name: called CC=5 SCAR=18.0
Pod documented
103 2 my $entry = shift;
104 2 ▶ my $no_stat = shift || 0;
Condition50%1/2
shift() || 0
l!l
20
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
105  
106 2 ▶ if ( $entry =~ m#^/dev/([^/]+)/(.*)$# ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ m[^/dev/([^/]+)/(.*)$]) 2 0 2
107 2 my $vg = $1;
108 2 my $lv = $2;
109  
110 # add extraneous dashes to vg and lv names
111 2 $vg =~ s/-/--/g;
112 2 $lv =~ s/-/--/g;
113  
114 2 my $device_name = '/dev/mapper/'.$vg.'-'.$lv;
115  
116 # Sanity check - does the constructed device name exist?
117 # Breaks unless we are root.
118 2 ▶ if( $no_stat) {
Branch: 2 branches
BranchTrueFalseTotal
if ($no_stat) { } 2 0 2
elsif (stat $device_name) { } 0 0 0
119 2 return $device_name;
120 }
121 elsif ( stat($device_name) ) {
122 0 return $device_name;
123 }
124 }
125  
126 0 return $entry;
127 }
128  
129 # find_devicemapper_major
130 # inspired by find_devicemapper_major from the munin diskstats plugin
131 # which was written by Michael Renner <michael.renner@amd.co.at>
132 0 ▶sub find_devicemapper_major {
Subroutine find_devicemapper_major: not called CC=5 SCAR=34.0
Pod documented
133 0 my $dm_major;
134  
135 0 ▶ if(open(my $fh, '<', '/proc/devices')) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", "/proc/devices") 0 0 0
136 0 my @lines = <$fh>;
137 ## no critic (RequireCheckedClose)
138 0 close($fh);
139 ## use critic
140 0 chomp(@lines);
141 0 foreach my $line ( @lines ) {
142  
143 0 my ($major, $name ) = split /\s+/, $line, 2;
144  
145 0 ▶ next unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 0 0
146  
147 0 ▶ if ( $name eq 'device-mapper' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "device-mapper") 0 0 0
148 0 $dm_major = $major;
149 0 last;
150 }
151 }
152 }
153  
154 0 return $dm_major;
155 }
156  
157 4 1;
158  
159 __END__
« LVM2.pm ↑ Summary LVM2/Snapshot.pm »