lib/Mogura/Scanner/Disk.pm

filter: Statementstmt 74.0% 208 / 281 Branchbran 60.0% 60 / 100 Conditioncond 69.3% 68 / 98 MC/DCmcdc 45.6% 21 / 46 Subroutinesub 77.7% 28 / 36 Podpod 0.0% 0 / 14 totaltotal 66.9% 385 / 575 CC 113 SCAR 27.7
CC
113
Cov
70%
resolve
45.5
_exclude_active_swap
42.0
unmount
30.0
CRAP 16.0
« blib/lib/Mogura/Scanner/Config.pm ↑ Summary lib/Mogura/Scanner/Command.pm »
1 package Mogura::Scanner::Disk;
2 4 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Mojo::JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Digest::SHA qw(sha256_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Fcntl qw(O_RDONLY O_NONBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Mogura::Scanner::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Linux amd64: _IOR(0x12, 128, __u64), available since Linux 5.15.
12 4 ▶use constant BLKGETDISKSEQ => 0x80081280;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use constant BIOS_BOOT_PARTITION_TYPE => '21686148-6449-6e6f-744e-656564454649';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 has 'config';
16 0 ▶has command => sub { Mogura::Scanner::Command->new };
Subroutine __ANON__: not called CC=1 SCAR=1.2
17 0 ▶has app_root => sub { abs_path(__FILE__) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
18 4 has sysfs_root => '/sys/dev/block';
19  
20 0 ▶sub device_number {
Subroutine device_number: not called CC=2 SCAR=17.9
Pod undocumented
21 0 my ($self, $file) = @_;
22 0 ▶ die "Not a block device: $file" unless -b $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -b $file 0 0 0
23  
24 # Linux dev_t stores the major/minor fields in separate bit ranges.
25 0 my $device_id = (stat $file)[6];
26 0 my $major = (($device_id >> 8) & 0xfff) | (($device_id >> 32) & 0xfffff000);
27 0 my $minor = ($device_id & 0xff) | (($device_id >> 12) & 0xffffff00);
28 0 return "$major:$minor";
29 }
30  
31 1 ▶sub mount_table {
Subroutine mount_table: called CC=4 SCAR=14.0
Pod undocumented
32 1 my ($self) = @_;
33  
34 1 my @mounts;
35 1 for my $line (split /\n/, path('/proc/self/mountinfo')->slurp) {
36  
37 # Optional mount fields vary in length; the separator anchors the
38 # filesystem fields without assuming a fixed number of columns.
39 24 my ($mount_description, $filesystem_description) = split / - /, $line, 2;
40 24 ▶ die 'Cannot parse mount table' unless defined $filesystem_description;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filesystem_description 0 24 24
41 24 my @mount_fields = split / /, $mount_description;
42 24 my @filesystem_fields = split / /, $filesystem_description;
43  
44 # The kernel octal-escapes whitespace and backslashes in these paths.
45 24 my ($root, $mountpoint, $source) = (@mount_fields[3, 4], $filesystem_fields[1]);
46 24 for my $value ($root, $mountpoint, $source) {
47 0 $value =~ s/\\([0-7]{3})/chr(oct($1))/ge;
48 }
49 24 push @mounts,
50 {
51 number => $mount_fields[2],
52 root => $root,
53 target => $mountpoint,
54 source => $source,
55 fstype => $filesystem_fields[0],
56 options => "$mount_fields[5],$filesystem_fields[2]",
57 };
58 }
59 1 return \@mounts;
60 }
61  
62 4 ▶sub inventory {
Subroutine inventory: called CC=4 SCAR=13.9
Pod undocumented
63 4 my ($self) = @_;
64  
65 4 my ($nodes, $parents) = $self->_read_block_devices;
66 4 my %busy;
67 my @errors;
68  
69 # Host use is collected separately from supported guest layouts. A disk
70 # must pass both checks before any of its partitions can be selected.
71 4 $self->_read_sysfs($nodes, $parents, \%busy);
72 4 for my $mount (@{$self->mount_table}) {
73 8 $busy{$mount->{number}} = 'Mounted filesystem';
74 }
75 4 $self->_protect_scanner_storage($nodes, \%busy, \@errors);
76 4 $self->_exclude_active_swap($nodes, \%busy);
77  
78 4 my $boot_id = path('/proc/sys/kernel/random/boot_id')->slurp;
79 4 my $inventory = $self->classify($nodes, $parents, \%busy, \@errors, $boot_id);
80 4 $inventory->{host} = $self->host_target;
81  
82 # Display the live root on its physical disks, including every backing
83 # disk of LVM / RAID. This annotation never changes clone eligibility.
84 4 my %root_devices;
85 4 $self->_exclude_ancestors($inventory->{host}{identity}{partition}{'maj:min'},
86 1, $parents, \%root_devices, {});
87 4 for my $disk (@{$inventory->{disks}}) {
88 8 ▶ $disk->{system_root} = $root_devices{$disk->{'maj:min'}} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$root_devices{$disk->{'maj:min'}} ? : 4 4 8
89 }
90 4 return $inventory;
91 }
92  
93 9 ▶sub host_target {
Subroutine host_target: called CC=2 SCAR=7.0
Pod undocumented
94 9 my ($self) = @_;
95  
96 9 my ($root) = grep { $_->{target} eq '/' } @{$self->mount_table};
97 9 ▶ die 'Cannot identify the running root filesystem' unless $root;
Branch: 1 branch
BranchTrueFalseTotal
unless $root 0 9 9
98 my $identity = {
99 kind => 'host',
100 boot_id => path('/proc/sys/kernel/random/boot_id')->slurp,
101 disk => {name => $root->{source}},
102 partition => {name => '/', fstype => $root->{fstype}, 'maj:min' => $root->{number}},
103 9 };
104 return {
105 9 kind => 'host',
106 selectable => 1,
107 identity => $identity,
108 token => sha256_hex(JSON::PP->new->canonical->encode($identity)),
109 };
110 }
111  
112 1 ▶sub host_exclusions {
Subroutine host_exclusions: called CC=1 SCAR=0.0
Pod undocumented
113 1 my ($self) = @_;
114  
115 # Limit live scans to the root filesystem and avoid scanning our own
116 # growing output and virtual filesystems. Software in temporary or cache
117 # directories is still evidence and must remain searchable.
118 8 my %paths = map { $_ => 1 } (
119 '/dev', '/proc', '/sys', '/run', $self->config->state,
120 $self->config->runtime,
121 1 map { $_->{target} } @{$self->mount_table},
122 );
123 1 delete $paths{'/'};
124 1 return [sort keys %paths];
125 }
126  
127 1 ▶sub _read_block_devices {
Subroutine _read_block_devices: called CC=3 SCAR=11.0
128 1 my ($self) = @_;
129  
130 1 my @columns = qw(
131 NAME KNAME TYPE SIZE RO MAJ:MIN PKNAME FSTYPE UUID PARTUUID
132 PTTYPE PARTTYPE SERIAL WWN MODEL MOUNTPOINTS
133 );
134 1 my $json = $self->command->run(
135 [
136 '/usr/bin/lsblk', '--json', '--bytes', '--paths',
137 '--tree', '--output', join(',', @columns),
138 ]
139 );
140 1 my $devices = decode_json($json)->{blockdevices};
141 1 ▶ die 'Missing block device inventory' unless ref $devices eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $devices eq "ARRAY" 0 1 1
142  
143 1 my (%nodes, %parents);
144 1 for my $device (@$devices) {
145 1 $self->_index_device($device, undef, \%nodes, \%parents);
146 }
147 1 return (\%nodes, \%parents);
148 }
149  
150 3 ▶sub _index_device {
Subroutine _index_device: called CC=6 SCAR=18.0
151 3 my ($self, $device, $parent_number, $nodes, $parents) = @_;
152  
153 3 my $number = $device->{'maj:min'};
154 3 ▶ die 'Unidentifiable block device' unless ($number // '') =~ /\A\d+:\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless ($number // "") =~ /\A\d+:\d+\z/u 0 3 3
Condition50%1/2
$number // ""
l!l
30
MC/DC0%0/1
$number // ""
$number
Truth table50%1/2
A
A= $number
Aresult
11
00
155 3 $nodes->{$number} = {%$device};
156 3 ▶ $parents->{$number}{$parent_number} = 1 if defined $parent_number;
Branch: 1 branch
BranchTrueFalseTotal
if defined $parent_number 2 1 3
157  
158 3 ▶ for my $child (@{$device->{children} // []}) {
Condition100%2/2
$device->{"children"} // []
l!l
12
MC/DC100%1/1
$device->{"children"} // []
$device->{"children"}
Truth table100%2/2
A
A= $device->{"children"}
Aresult
11
00
159 2 $self->_index_device($child, $number, $nodes, $parents);
160 }
161 }
162  
163 4 ▶sub _read_sysfs {
Subroutine _read_sysfs: called CC=8 SCAR=21.5
164 4 my ($self, $nodes, $parents, $busy) = @_;
165  
166 # lsblk supplies partition relationships; sysfs adds dependencies beneath
167 # device-mapper and RAID so host LVM never conceals its physical disks.
168 4 for my $number (keys %$nodes) {
169 12 my $device_directory = path($self->sysfs_root, $number);
170 12 ▶ die "Missing sysfs device $number" unless -d $device_directory;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $device_directory 0 12 12
171  
172 # Ubuntu 22.04's lsblk has no START column. sysfs exposes the same
173 # offset in 512-byte sectors; missing partition geometry must fail closed.
174 12 ▶ if ($nodes->{$number}{type} eq 'part') {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes->{$number}{"type"} eq "part") 8 4 12
175 8 my $start = $device_directory->child('start')->slurp;
176 7 $start =~ s/\s+\z//;
177 7 ▶ die "Invalid partition start for $number" unless $start =~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $start =~ /\A\d+\z/u 1 6 7
178 6 $nodes->{$number}{start} = 0 + $start;
179 }
180  
181 10 for my $slave ($device_directory->child('slaves')->list->each) {
182 0 my $backing_number = path($slave, 'dev')->slurp;
183 0 chomp $backing_number;
184 0 $parents->{$number}{$backing_number} = 1;
185 }
186 10 ▶ if ($device_directory->child('holders')->list->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($device_directory->child("holders")->list->size) 0 10 10
187 0 $busy->{$number} = 'Referenced by device-mapper / RAID or another block device';
188 }
189  
190 10 my $sequence_file = $device_directory->child('diskseq');
191 10 ▶ my $sequence = -f $sequence_file ? $sequence_file->slurp : '';
Branch: 1 branch
BranchTrueFalseTotal
-f $sequence_file ? : 4 6 10
192 10 $sequence =~ s/\s+\z//;
193 10 $nodes->{$number}{diskseq} = $sequence;
194 }
195 }
196  
197 0 ▶sub _protect_scanner_storage {
Subroutine _protect_scanner_storage: not called CC=4 SCAR=30.0
198 0 my ($self, $nodes, $busy, $errors) = @_;
199  
200 # Incomplete host topology must fail closed, even if no mounted device in
201 # lsblk was recognized as belonging to the scanner.
202 0 for my $protected_path ('/', $self->app_root, $self->config->state) {
203 0 my $json = $self->command->run(
204 [
205 '/usr/bin/findmnt', '--json',
206 '--target', $protected_path,
207 '--output', 'MAJ:MIN,FSTYPE,SOURCE',
208 ]
209 );
210 0 my $mount = decode_json($json)->{filesystems}[0];
211 0 ▶ my $number = $mount->{'maj:min'} // '';
Condition0%0/2
$mount->{"maj:min"} // ""
l!l
00
MC/DC0%0/1
$mount->{"maj:min"} // ""
$mount->{"maj:min"}
Truth table0%0/2
A
A= $mount->{"maj:min"}
Aresult
11
00
212 0 ▶ if ($nodes->{$number}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes->{$number}) { } 0 0 0
213 0 $busy->{$number} = "Scanner storage: $protected_path";
214 }
215 else {
216 0 push @$errors, "Cannot resolve scanner storage: $protected_path";
217 }
218 }
219 }
220  
221 0 ▶sub _exclude_active_swap {
Subroutine _exclude_active_swap: not called CC=6 SCAR=37.4
222 0 my ($self, $nodes, $busy) = @_;
223  
224 0 my @swap_entries = split /\n/, path('/proc/swaps')->slurp;
225 0 shift @swap_entries; # /proc/swaps begins with a column header.
226  
227 0 for my $entry (@swap_entries) {
228 0 my ($file) = split /\s+/, $entry;
229 0 $file =~ s/\\([0-7]{3})/chr(oct($1))/ge;
230  
231 0 my $number;
232 0 ▶ if (-b $file) {
Branch: 2 branches
BranchTrueFalseTotal
if (-b $file) { } 0 0 0
elsif (-f $file) { } 0 0 0
233 0 $number = $self->device_number($file);
234 }
235 elsif (-f $file) {
236  
237 # A swap file protects the filesystem containing it, including
238 # every physical ancestor of an LVM or RAID backing device.
239 0 my $json = $self->command->run(
240 ['/usr/bin/findmnt', '--json', '--target', $file, '--output', 'MAJ:MIN',]);
241 0 $number = decode_json($json)->{filesystems}[0]{'maj:min'};
242 }
243 0 ▶ die 'Cannot identify active swap backing device' unless $number && $nodes->{$number};
Branch: 1 branch
BranchTrueFalseTotal
unless $number and $nodes->{$number} 0 0 0
Condition0%0/3
$number and $nodes->{$number}
!ll&&!rl&&r
000
MC/DC0%0/2
$number and $nodes->{$number}
$number $nodes->{$number}
Truth table0%0/3
A and B
A= $numberB= $nodes->{$number}
ABresult
0X0
100
111
244 0 $busy->{$number} = 'Active swap';
245 }
246 }
247  
248 48 ▶sub classify {
Subroutine classify: called CC=8 SCAR=20.8
Pod undocumented
249 48 my ($self, $nodes, $parents, $busy, $errors, $boot_id) = @_;
250  
251 48 my %excluded = %$busy;
252 48 for my $number (keys %$busy) {
253 43 $self->_exclude_ancestors($number, $busy->{$number}, $parents, \%excluded, {});
254 }
255  
256 41 my @physical_disks = sort { $a->{name} cmp $b->{name} }
257 48 grep { $_->{type} eq 'disk' } values %$nodes;
258 48 my @disks;
259 48 for my $disk (@physical_disks) {
260 89 my $number = $disk->{'maj:min'};
261 31 ▶ my @partitions = sort { ($a->{start} // 0) <=> ($b->{start} // 0) }
Condition50%2/4
$Mogura::Scanner::Disk::a->{"start"} // 0
l!l
310
$Mogura::Scanner::Disk::b->{"start"} // 0
l!l
310
MC/DC0%0/1
$Mogura::Scanner::Disk::a->{"start"} // 0
$Mogura::Scanner::Disk::a->{"start"}
MC/DC0%0/1
$Mogura::Scanner::Disk::b->{"start"} // 0
$Mogura::Scanner::Disk::b->{"start"}
Truth table50%1/2
A
A= $Mogura::Scanner::Disk::a->{"start"}
Aresult
11
00
Truth table50%1/2
A
A= $Mogura::Scanner::Disk::b->{"start"}
Aresult
11
00
262 89 ▶ grep { ($parents->{$_->{'maj:min'}} // {})->{$number} } values %$nodes;
Condition100%2/2
$$parents{$_->{"maj:min"}} // {}
l!l
302171
MC/DC100%1/1
$$parents{$_->{"maj:min"}} // {}
$$parents{$_->{"maj:min"}}
Truth table100%2/2
A
A= $$parents{$_->{"maj:min"}}
Aresult
11
00
263 89 my $reasons = $self->_disk_exclusion_reasons($disk, \@partitions, \%excluded, $errors);
264  
265 # A filesystem may occupy either a partition or the complete disk.
266 # Every candidate inherits exclusions affecting any part of its disk.
267 89 ▶ my @filesystems = @partitions ? @partitions : ($disk);
Branch: 1 branch
BranchTrueFalseTotal
@partitions ? : 87 2 89
268 89 my @targets;
269 89 for my $filesystem (@filesystems) {
270 120 push @targets, $self->_build_target($disk, $filesystem, $reasons, $boot_id);
271 }
272 89 push @disks, {%$disk, targets => \@targets, reasons => $reasons};
273 }
274 48 return {updated_at => time, disks => \@disks, errors => $errors};
275 }
276  
277 127 ▶sub _exclude_ancestors {
Subroutine _exclude_ancestors: called CC=5 SCAR=16.2
278 127 my ($self, $number, $reason, $parents, $excluded, $visited) = @_;
279  
280 127 ▶ return if $visited->{$number};
Branch: 1 branch
BranchTrueFalseTotal
if $visited->{$number} 0 127 127
281 127 $visited->{$number} = 1;
282 127 ▶ $excluded->{$number} //= $reason;
Condition66%2/3
$excluded->{$number} //= $reason
l!l&&r!l&&!r
46810
MC/DC0%0/2
$excluded->{$number} //= $reason
$excluded->{$number} $reason
Truth table66%2/3
A //= B
A= $excluded->{$number}B= $reason
ABresult
1X1
011
000
283  
284 127 ▶ for my $parent (keys %{$parents->{$number} // {}}) {
Condition100%2/2
$parents->{$number} // {}
l!l
7849
MC/DC100%1/1
$parents->{$number} // {}
$parents->{$number}
Truth table100%2/2
A
A= $parents->{$number}
Aresult
11
00
285 80 $self->_exclude_ancestors($parent, $reason, $parents, $excluded, $visited);
286 }
287 }
288  
289 89 ▶sub _disk_exclusion_reasons {
Subroutine _disk_exclusion_reasons: called CC=28 SCAR=34.0
290 89 my ($self, $disk, $partitions, $excluded, $errors) = @_;
291  
292 89 my @reasons = @$errors;
293 89 my $number = $disk->{'maj:min'};
294 89 ▶ push @reasons, $excluded->{$number} if $excluded->{$number};
Branch: 1 branch
BranchTrueFalseTotal
if $excluded->{$number} 41 48 89
295  
296 89 ▶ my $hardware_id = ($disk->{serial} // '') || ($disk->{wwn} // '');
Condition57%4/7
($disk->{'serial'} // '') || ($disk->{'wwn'} // '')
l!l&&r!l&&!r
8702
$disk->{"serial"} // ""
l!l
890
$disk->{"wwn"} // ""
l!l
02
MC/DC0%0/2
($disk->{'serial'} // '') || ($disk->{'wwn'} // '')
$disk->{"serial"} $disk->{"wwn"}
Truth table33%1/3
A || B
A= $disk->{"serial"}B= $disk->{"wwn"}
ABresult
1X1
011
000
297 89 ▶ my $has_sequence = ($disk->{diskseq} // '') =~ /\A\d+\z/;
Condition50%1/2
$disk->{"diskseq"} // ""
l!l
890
MC/DC0%0/1
$disk->{"diskseq"} // ""
$disk->{"diskseq"}
Truth table50%1/2
A
A= $disk->{"diskseq"}
Aresult
11
00
298 89 ▶ unless (length($hardware_id) && $has_sequence) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $hardware_id and $has_sequence) 2 87 89
Condition66%2/3
length $hardware_id and $has_sequence
!ll&&!rl&&r
2087
MC/DC50%1/2
length $hardware_id and $has_sequence
length $hardware_id $has_sequence
Truth table66%2/3
A and B
A= length $hardware_idB= $has_sequence
ABresult
0X0
100
111
299 2 push @reasons, 'No hardware serial / WWN or kernel disk sequence';
300 }
301  
302 89 ▶ if (@$partitions) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$partitions) { } 87 2 89
elsif (length($disk->{'pttype'} // '')) { } 0 2 2
303 push @reasons, 'Unsupported partition table: ' . ($disk->{pttype} || '<empty>')
304 87 ▶ unless ($disk->{pttype} // '') =~ /\A(?:gpt|dos)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless ($disk->{"pttype"} // "") =~ /\A(?:gpt|dos)\z/u 3 84 87
Condition75%3/4
$disk->{"pttype"} // ""
l!l
870
$disk->{'pttype'} || '<empty>'
l!l
12
MC/DC0%0/1
$disk->{"pttype"} // ""
$disk->{"pttype"}
MC/DC100%1/1
$disk->{'pttype'} || '<empty>'
$disk->{'pttype'}
Truth table50%1/2
A
A= $disk->{"pttype"}
Aresult
11
00
Truth table100%2/2
A
A= $disk->{'pttype'}
Aresult
11
00
305 push @reasons, 'Ambiguous filesystem and partition table'
306 87 ▶ if length($disk->{fstype} // '');
Branch: 1 branch
BranchTrueFalseTotal
if length($disk->{"fstype"} // "") 0 87 87
Condition50%1/2
$disk->{"fstype"} // ""
l!l
087
MC/DC0%0/1
$disk->{"fstype"} // ""
$disk->{"fstype"}
Truth table50%1/2
A
A= $disk->{"fstype"}
Aresult
11
00
307 }
308 ▶ elsif (length($disk->{pttype} // '')) {
Condition100%2/2
$disk->{"pttype"} // ""
l!l
11
MC/DC100%1/1
$disk->{"pttype"} // ""
$disk->{"pttype"}
Truth table100%2/2
A
A= $disk->{"pttype"}
Aresult
11
00
309 0 push @reasons, 'Partition table present but no readable partitions';
310 }
311  
312 89 for my $partition (@$partitions) {
313 118 ▶ my $filesystem = $partition->{fstype} // '';
Condition100%2/2
$partition->{"fstype"} // ""
l!l
1162
MC/DC100%1/1
$partition->{"fstype"} // ""
$partition->{"fstype"}
Truth table100%2/2
A
A= $partition->{"fstype"}
Aresult
11
00
314 my $is_efi =
315 118 ▶ lc($partition->{parttype} // '') =~ /\A(?:c12a7328-f81f-11d2-ba4b-00a0c93ec93b|0xef)\z/;
Condition100%2/2
$partition->{"parttype"} // ""
l!l
3088
MC/DC100%1/1
$partition->{"parttype"} // ""
$partition->{"parttype"}
Truth table100%2/2
A
A= $partition->{"parttype"}
Aresult
11
00
316 118 ▶ my $is_bios_boot_type = lc($partition->{parttype} // '') eq BIOS_BOOT_PARTITION_TYPE;
Condition100%2/2
$partition->{"parttype"} // ""
l!l
3088
MC/DC100%1/1
$partition->{"parttype"} // ""
$partition->{"parttype"}
Truth table100%2/2
A
A= $partition->{"parttype"}
Aresult
11
00
317 118 ▶ my $supported =
Branch: 1 branch
BranchTrueFalseTotal
$is_bios_boot_type ? : 20 98 118
Condition83%5/6
$filesystem =~ /\A(?:ext4|xfs|swap)\z/u or $is_efi and $filesystem eq "vfat"
l!l&&r!l&&!r
42155
$is_efi and $filesystem eq "vfat"
!ll&&!rl&&r
5501
MC/DC66%2/3
$filesystem =~ /\A(?:ext4|xfs|swap)\z/u or $is_efi and $filesystem eq "vfat"
$filesystem =~ /\A(?:ext4|xfs|swap)\z/u $is_efi $filesystem eq "vfat"
Truth table75%3/4
A or B and C
A= $filesystem =~ /\A(?:ext4|xfs|swap)\z/uB= $is_efiC= $filesystem eq "vfat"
ABCresult
1XX1
0111
00X0
0100
318 $is_bios_boot_type
319 ? $self->_is_bios_boot_partition($disk, $partition)
320 : $filesystem =~ /\A(?:ext4|xfs|swap)\z/ || ($is_efi && $filesystem eq 'vfat');
321 118 ▶ unless ($partition->{type} eq 'part' && $supported) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($partition->{"type"} eq "part" and $supported) 63 55 118
Condition100%3/3
$partition->{"type"} eq "part" and $supported
!ll&&!rl&&r
16255
MC/DC100%2/2
$partition->{"type"} eq "part" and $supported
$partition->{"type"} eq "part" $supported
Truth table100%3/3
A and B
A= $partition->{"type"} eq "part"B= $supported
ABresult
0X0
100
111
322 my $details = join(', ',
323 'PTTYPE=' . ($disk->{pttype} || '<empty>'),
324 63 ▶ map { uc($_) . '=' . ($partition->{$_} || '<empty>') } qw(type parttype fstype));
Condition100%4/4
$disk->{'pttype'} || '<empty>'
l!l
603
$partition->{$_} || '<empty>'
l!l
12960
MC/DC100%1/1
$disk->{'pttype'} || '<empty>'
$disk->{'pttype'}
MC/DC100%1/1
$partition->{$_} || '<empty>'
$partition->{$_}
Truth table100%2/2
A
A= $disk->{'pttype'}
Aresult
11
00
Truth table100%2/2
A
A= $partition->{$_}
Aresult
11
00
325 63 push @reasons, "Unsupported or unknown partition: $partition->{name} ($details)";
326 }
327 118 ▶ if ($excluded->{$partition->{'maj:min'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$excluded{$partition->{"maj:min"}}) 41 77 118
328 41 push @reasons, "Referenced partition: $partition->{name}";
329 }
330 }
331 89 return \@reasons;
332 }
333  
334 140 ▶sub _is_bios_boot_partition {
Subroutine _is_bios_boot_partition: called CC=8 SCAR=20.8
335 140 my ($self, $disk, $partition) = @_;
336  
337 # GRUB stores bootloader data here without a filesystem. Only this exact
338 # GPT layout may coexist with scan targets; host-use checks still apply.
339 return
340 ($disk->{pttype} // '') eq 'gpt'
341 && ($partition->{type} // '') eq 'part'
342 && lc($partition->{parttype} // '') eq BIOS_BOOT_PARTITION_TYPE
343 140 ▶ && !length($partition->{fstype} // '');
Condition94%16/17
($disk->{'pttype'} // '') eq 'gpt' && ($partition->{'type'} // '') eq 'part' && lc($partition->{'parttype'} // '') eq '21686148-6449-6e6f-744e-656564454649' && !length($partition->{'fstype'} // '')
!ll&&!rl&&r
1061024
($disk->{"pttype"} // "") eq "gpt" and ($partition->{"type"} // "") eq "part" and lc($partition->{"parttype"} // "") eq "21686148-6449-6e6f-744e-656564454649"
!ll&&!rl&&r
169034
($disk->{"pttype"} // "") eq "gpt" and ($partition->{"type"} // "") eq "part"
!ll&&!rl&&r
142124
$disk->{"pttype"} // ""
l!l
1391
$partition->{"type"} // ""
l!l
1260
$partition->{"parttype"} // ""
l!l
4084
$partition->{"fstype"} // ""
l!l
304
MC/DC75%3/4
($disk->{'pttype'} // '') eq 'gpt' && ($partition->{'type'} // '') eq 'part' && lc($partition->{'parttype'} // '') eq '21686148-6449-6e6f-744e-656564454649' && !length($partition->{'fstype'} // '')
$disk->{"pttype"} $partition->{"type"} $partition->{"parttype"} $partition->{"fstype"}
Truth table80%4/5
A and B and C && not(D)
A= $disk->{"pttype"}B= $partition->{"type"}C= $partition->{"parttype"}D= $partition->{"fstype"}
ABCDresult
0XXX0
10XX0
110X0
11110
11101
344 }
345  
346 120 ▶sub _build_target {
Subroutine _build_target: called CC=5 SCAR=16.1
347 120 my ($self, $disk, $filesystem, $disk_reasons, $boot_id) = @_;
348  
349 120 my @reasons = @$disk_reasons;
350 120 ▶ if ($self->_is_bios_boot_partition($disk, $filesystem)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_bios_boot_partition($disk, $filesystem)) { } 12 108 120
elsif (not +($filesystem->{'fstype'} // '') =~ /\A(?:ext4|xfs)\z/u) { } 62 46 108
351 12 push @reasons, 'BIOS boot partition: bootloader only, not scanned';
352 }
353 ▶ elsif (($filesystem->{fstype} // '') !~ /\A(?:ext4|xfs)\z/) {
Condition50%1/2
$filesystem->{"fstype"} // ""
l!l
1080
MC/DC0%0/1
$filesystem->{"fstype"} // ""
$filesystem->{"fstype"}
Truth table50%1/2
A
A= $filesystem->{"fstype"}
Aresult
11
00
354 62 push @reasons, 'Only ext4 / XFS root filesystems are supported';
355 }
356  
357 # Serial/WWN identify the hardware; boot ID and disk sequence distinguish
358 # attachments even when /dev names, partition UUIDs, and sizes are reused.
359 my $identity = {
360 boot_id => $boot_id,
361 840 disk => {map { $_ => $disk->{$_} } qw(name maj:min size serial wwn diskseq pttype)},
362 partition =>
363 120 {map { $_ => $filesystem->{$_} } qw(name maj:min size start fstype uuid partuuid)},
364 };
365 120 my $canonical_identity = JSON::PP->new->canonical->encode($identity);
366 return {
367 120 ▶ %$filesystem,
Branch: 1 branch
BranchTrueFalseTotal
@reasons ? : 102 18 120
368 reasons => \@reasons,
369 identity => $identity,
370 token => sha256_hex($canonical_identity),
371 selectable => @reasons ? 0 : 1,
372 };
373 }
374  
375 2 ▶sub resolve {
Subroutine resolve: called CC=11 SCAR=38.2
Pod undocumented
376 2 my ($self, $token) = @_;
377 2 ▶ die 'Invalid target token' unless ($token // '') =~ /\A[0-9a-f]{64}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless ($token // "") =~ /\A[0-9a-f]{64}\z/u 0 2 2
Condition50%1/2
$token // ""
l!l
20
MC/DC0%0/1
$token // ""
$token
Truth table50%1/2
A
A= $token
Aresult
11
00
378 2 my $host = $self->host_target;
379 2 ▶ return $host if $host->{token} eq $token;
Branch: 1 branch
BranchTrueFalseTotal
if $host->{"token"} eq $token 1 1 2
380  
381 1 my @matches;
382 1 for my $disk (@{$self->inventory->{disks}}) {
383 0 ▶ push @matches, grep { $_->{token} eq $token && $_->{selectable} } @{$disk->{targets}};
Condition0%0/3
$_->{'token'} eq $token && $_->{'selectable'}
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'token'} eq $token && $_->{'selectable'}
$_->{'token'} eq $token $_->{'selectable'}
Truth table0%0/3
A && B
A= $_->{'token'} eq $tokenB= $_->{'selectable'}
ABresult
0X0
100
111
384 }
385 1 ▶ die 'Disk missing, replaced, busy, unsupported, or no longer identifiable' unless @matches == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @matches == 1 1 0 1
386 0 my $target = $matches[0];
387 0 for my $item ($target->{identity}{disk}, $target->{identity}{partition}) {
388 die 'Device identity changed'
389 0 ▶ unless $self->device_number($item->{name}) eq $item->{'maj:min'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->device_number($item->{"name"}) eq $item->{"maj:min"} 0 0 0
390 0 ▶ sysopen my $handle, $item->{name}, O_RDONLY | O_NONBLOCK or die "Open verified device: $!";
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $handle, $item->{"name"}, 2048 or die "Open verified device: $!" 0 0 0
391  
392 # Keep the device open throughout mounting. External commands resolve
393 # these descriptors instead of a /dev name that hotplug can replace.
394 0 my $descriptor = '/proc/' . $$ . '/fd/' . fileno($handle);
395 die 'Opened device identity changed'
396 0 ▶ unless $self->device_number($descriptor) eq $item->{'maj:min'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->device_number($descriptor) eq $item->{"maj:min"} 0 0 0
397 0 push @{$target->{handles}}, $handle;
398 }
399 0 $self->verify_sequence($target);
400 0 return $target;
401 }
402  
403 0 ▶sub device_path {
Subroutine device_path: not called CC=2 SCAR=17.9
Pod undocumented
404 0 my ($self, $target, $index) = @_;
405 0 ▶ my $handle = $target->{handles}[$index] or die 'Verified device descriptor unavailable';
Branch: 1 branch
BranchTrueFalseTotal
my $handle = $target->{"handles"}[$index] or die "Verified device descriptor unavailable" 0 0 0
406 0 return '/proc/' . $$ . '/fd/' . fileno($handle);
407 }
408  
409 4 ▶sub verify_sequence {
Subroutine verify_sequence: called CC=4 SCAR=13.9
Pod undocumented
410 4 my ($self, $target) = @_;
411  
412 # Handle 0 is the whole disk; handle 1 is the selected filesystem. A
413 # partition reports its parent disk's sequence, so both must still agree.
414 4 for my $index (0, 1) {
415 6 ▶ my $handle = $target->{handles}[$index] or die 'Verified device descriptor unavailable';
Branch: 1 branch
BranchTrueFalseTotal
my $handle = $target->{"handles"}[$index] or die "Verified device descriptor unavailable" 5 1 6
416 5 my $actual = $self->_disk_sequence($handle);
417 5 ▶ die 'Kernel disk sequence changed' unless $actual eq $target->{identity}{disk}{diskseq};
Branch: 1 branch
BranchTrueFalseTotal
unless $actual eq $target->{"identity"}{"disk"}{"diskseq"} 2 3 5
418 }
419 }
420  
421 1 ▶sub _disk_sequence {
Subroutine _disk_sequence: called CC=2 SCAR=7.6
422 1 my ($self, $handle) = @_;
423  
424 # Ubuntu 22.04's blockdev lacks --getdiskseq. Query the held descriptor
425 # directly so hotplug cannot redirect this check to a replacement device.
426 1 my $sequence = pack('Q', 0);
427 1 ▶ ioctl($handle, BLKGETDISKSEQ, $sequence) or die "Read kernel disk sequence: $!";
Branch: 1 branch
BranchTrueFalseTotal
ioctl $handle, 2148012672, $sequence or die "Read kernel disk sequence: $!" 0 1 1
428 0 return unpack('Q', $sequence);
429 }
430  
431 2 ▶sub make_readonly {
Subroutine make_readonly: called CC=3 SCAR=20.3
Pod undocumented
432 2 my ($self, $target) = @_;
433 2 $self->verify_sequence($target);
434 0 for my $index (0, 1) {
435 0 my $device = $self->device_path($target, $index);
436 0 $self->command->run(['/usr/sbin/blockdev', '--setro', $device]);
437 0 my $result = $self->command->run(['/usr/sbin/blockdev', '--getro', $device]);
438 0 ▶ die "Block read-only verification failed: $device" unless $result =~ /\A1\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $result =~ /\A1\s*\z/u 0 0 0
439 }
440  
441 # Re-enumeration catches replacements or new host use during setro.
442 0 return $self->resolve($target->{token});
443 }
444  
445 4 ▶sub options {
Subroutine options: called CC=3 SCAR=11.4
Pod undocumented
446 4 my ($self, $filesystem) = @_;
447  
448 # Read-only alone can still replay a journal or log. Suppress recovery
449 # explicitly, without modifying UUIDs or repairing the guest filesystem.
450 4 ▶ return 'ro,noload,nodev,nosuid,noexec' if $filesystem eq 'ext4';
Branch: 1 branch
BranchTrueFalseTotal
if $filesystem eq "ext4" 3 1 4
451 1 ▶ return 'ro,norecovery,nouuid,nodev,nosuid,noexec' if $filesystem eq 'xfs';
Branch: 1 branch
BranchTrueFalseTotal
if $filesystem eq "xfs" 1 0 1
452 0 die 'Unsupported filesystem';
453 }
454  
455 0 ▶sub mount {
Subroutine mount: not called CC=1 SCAR=6.9
Pod undocumented
456 0 my ($self, $target, $mountpoint) = @_;
457 $self->command->run(
458 [
459 '/usr/bin/mount', '--no-canonicalize',
460 '-t', $target->{fstype},
461 0 '-o', $self->options($target->{fstype}),
462 '--', $self->device_path($target, 1),
463 "$mountpoint"
464 ]
465 );
466 0 $self->verify_mount($target, $mountpoint);
467 }
468  
469 3 ▶sub verify_mount {
Subroutine verify_mount: called CC=11 SCAR=24.3
Pod undocumented
470 3 my ($self, $target, $mountpoint) = @_;
471  
472 # A successful mount command does not prove its source or options. Check
473 # kernel-reported state before allowing a cataloger to read guest files.
474 3 my @mounts = grep { $_->{target} eq "$mountpoint" } @{$self->mount_table};
475 die 'Mount source verification failed'
476 unless @mounts == 1
477 && $mounts[0]{root} eq '/'
478 && $mounts[0]{number} eq $target->{'maj:min'}
479 3 ▶ && $mounts[0]{fstype} eq $target->{fstype};
Branch: 1 branch
BranchTrueFalseTotal
unless @mounts == 1 and $mounts[0]{"root"} eq "/" and $mounts[0]{"number"} eq $target->{"maj:min"} and $mounts[0]{"fstype"} eq $target->{"fstype"} 1 2 3
Condition55%5/9
@mounts == 1 and $mounts[0]{"root"} eq "/" and $mounts[0]{"number"} eq $target->{"maj:min"} and $mounts[0]{"fstype"} eq $target->{"fstype"}
!ll&&!rl&&r
102
@mounts == 1 and $mounts[0]{"root"} eq "/" and $mounts[0]{"number"} eq $target->{"maj:min"}
!ll&&!rl&&r
012
@mounts == 1 and $mounts[0]{"root"} eq "/"
!ll&&!rl&&r
003
MC/DC25%1/4
@mounts == 1 and $mounts[0]{"root"} eq "/" and $mounts[0]{"number"} eq $target->{"maj:min"} and $mounts[0]{"fstype"} eq $target->{"fstype"}
@mounts == 1 $mounts[0]{"root"} eq "/" $mounts[0]{"number"} eq $target->{"maj:min"} $mounts[0]{"fstype"} eq $target->{"fstype"}
Truth table40%2/5
A and B and C and D
A= @mounts == 1B= $mounts[0]{"root"} eq "/"C= $mounts[0]{"number"} eq $target->{"maj:min"}D= $mounts[0]{"fstype"} eq $target->{"fstype"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
480 2 my %actual = map { $_ => 1 } split /,/, $mounts[0]{options};
481 2 for my $required (split /,/, $self->options($target->{fstype})) {
482  
483 # The kernel reports ext4 noload as norecovery.
484 7 ▶ next if $required eq 'noload' && $actual{norecovery};
Branch: 1 branch
BranchTrueFalseTotal
if $required eq "noload" and $actual{"norecovery"} 1 6 7
Condition100%3/3
$required eq "noload" and $actual{"norecovery"}
!ll&&!rl&&r
511
MC/DC100%2/2
$required eq "noload" and $actual{"norecovery"}
$required eq "noload" $actual{"norecovery"}
Truth table100%3/3
A and B
A= $required eq "noload"B= $actual{"norecovery"}
ABresult
0X0
100
111
485 6 ▶ die "Missing mount safety option: $required" unless $actual{$required};
Branch: 1 branch
BranchTrueFalseTotal
unless $actual{$required} 1 5 6
486 }
487 1 $self->verify_sequence($target);
488 1 for my $index (0, 1) {
489 2 my $value = $self->command->run(
490 ['/usr/sbin/blockdev', '--getro', $self->device_path($target, $index)]);
491 2 ▶ die 'Block device is no longer read-only' unless $value =~ /\A1\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A1\s*\z/u 0 2 2
492 }
493 }
494  
495 0 ▶sub unmount {
Subroutine unmount: not called CC=5 SCAR=34.0
Pod undocumented
496 0 my ($self, $target, $mountpoint) = @_;
497  
498 # Never detach an unexpected filesystem that appeared at our mountpoint.
499 # Only a normal unmount followed by absence in mountinfo counts as cleanup.
500 0 my @mounts = grep { $_->{target} eq "$mountpoint" } @{$self->mount_table};
501 0 ▶ return unless @mounts;
Branch: 1 branch
BranchTrueFalseTotal
unless @mounts 0 0 0
502 die 'Refusing cleanup: unexpected mount source'
503 unless @mounts == 1
504 0 ▶ && $mounts[0]{number} eq $target->{'maj:min'};
Branch: 1 branch
BranchTrueFalseTotal
unless @mounts == 1 and $mounts[0]{"number"} eq $target->{"maj:min"} 0 0 0
Condition0%0/3
@mounts == 1 and $mounts[0]{"number"} eq $target->{"maj:min"}
!ll&&!rl&&r
000
MC/DC0%0/2
@mounts == 1 and $mounts[0]{"number"} eq $target->{"maj:min"}
@mounts == 1 $mounts[0]{"number"} eq $target->{"maj:min"}
Truth table0%0/3
A and B
A= @mounts == 1B= $mounts[0]{"number"} eq $target->{"maj:min"}
ABresult
0X0
100
111
505 0 $self->command->run(['/usr/bin/umount', '--', "$mountpoint"]);
506 die 'Mount remains after umount'
507 0 ▶ if grep { $_->{target} eq "$mountpoint" } @{$self->mount_table};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->{"target"} eq "$mountpoint";} @{$self->mount_table;} 0 0 0
508 }
509  
510 4 1;
« blib/lib/Mogura/Scanner/Config.pm ↑ Summary lib/Mogura/Scanner/Command.pm »