| 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
| |||||||||||||||||||||||||||||
| 33 | 0 | $want_minor = $1; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | if(!$want_minor) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$major == $dm_major and $minor == $want_minor
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
| |||||||||||||||||||||||||||||
| 54 | 0 | my $display_name = translate_lvm_name($dirent); | |||||||||||||||||||||||||||
| 55 | 0 | $dirent =~ s#/dev/##; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | if(defined($display_name)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 2 | ▶ | if ( $entry =~ m#^/dev/([^/]+)/(.*)$# ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | if ( $name eq 'device-mapper' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||