PiFlash/Inspector.pm

filter: Statementstmt 93.1% 27 / 29 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 94.8% 37 / 39 CC 1 SCAR 0.3
CC
1
Cov
93%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« PiFlash.pm ↑ Summary
1 # PiFlash::Inspector - inspection of the Linux system configuration including identifying SD card devices
2 # by Ian Kluft
3  
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use v5.14.0; # require 2011 or newer version of Perl
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use PiFlash::State;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use PiFlash::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package PiFlash::Inspector;
11 $PiFlash::Inspector::VERSION = '0.4.3';
12 5 ▶use autodie; # report errors instead of silently continuing ("die" actions are used as exceptions - caught & reported)
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use File::Slurp qw(slurp);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 0 ▶use File::LibMagic; # rpm: "dnf install perl-File-LibMagic", deb: "apt-get install libfile-libmagic-perl"
Subroutine BEGIN: called CC=1 SCAR=2.6
17  
18 # ABSTRACT: PiFlash functions to inspect Linux system devices to flash an SD card for Raspberry Pi
19  
20  
21 #
22 # class-global variables
23 #
24  
25 # recognized file suffixes which SD cards can be flashed from
26 our @known_suffixes = qw(gz zip xz img);
27  
28 # block device parameters to collect via lsblk
29 our @blkdev_params = ( "MOUNTPOINT", "FSTYPE", "SIZE", "SUBSYSTEMS", "TYPE", "MODEL", "RO", "RM", "HOTPLUG", "PHY-SEC");
30  
31 # collect data about the system: kernel specs, program locations
32 sub collect_system_info
33 {
34 my $system = PiFlash::State::system();
35  
36 # Make sure we're on a Linux system - this program uses Linux-only features
37 ($system->{sysname}, $system->{nodename}, $system->{release}, $system->{version},
38 $system->{machine}) = POSIX::uname();
39 if ($system->{sysname} ne "Linux") {
40 PiFlash::State->error("This depends on features of Linux. Found $system->{sysname} kernel - cannot continue.");
41 }
42  
43 # hard-code known-secure locations of programs here if you need to override any on your system
44 # $prog{name} = "/path/to/name";
45  
46 # loop through needed programs and record locations from environment variable or system directories
47 $system->{prog} = {};
48  
49 # set PATH in environment as a precaution - we don't intend to use it but mkfs does
50 # search paths in standard Unix PATH order
51 my @path;
52 for my $path ("/sbin", "/usr/sbin", "/bin", "/usr/bin") {
53 # include in PATH standard Unix directories which exist on this system
54 if (-d $path) {
55 push @path, $path;
56 }
57 }
58 ## no critic (RequireLocalizedPunctuationVars])
59 $ENV{PATH} = join ":", @path;
60 ## use critic
61 $system->{PATH} = $ENV{PATH};
62  
63 # find filesystems supported by this kernel (for formatting SD card)
64 my %fs_pref = (vfat => 1, ext4 => 2, ext3 => 3, ext2 => 4, exfat => 5, other => 6); # fs preference order
65 my @filesystems = grep {! /^nodev\s/} slurp("/proc/filesystems");
66 chomp @filesystems;
67 for (my $i=0; $i<=$#filesystems; $i++) {
68 # remove leading and trailing whitespace;
69 $filesystems[$i] =~ s/^\s*//;
70 $filesystems[$i] =~ s/\s*$//;
71 }
72 # sort list by decreasing preference (increasing numbers)
73 $system->{filesystems} = [ sort {($fs_pref{$a} // $fs_pref{other})
74 <=> ($fs_pref{$b} // $fs_pref{other})} @filesystems ];
75 $system->{primary_fs} = $system->{filesystems}[0];
76  
77  
78 # find locations where we can put mount points
79 foreach my $dir ( qw(/run/media /media /mnt) ) {
80 if ( -d $dir ) {
81 PiFlash::State::system("media_dir", $dir); # use the first one
82 last;
83 }
84 }
85 }
86  
87 # collect input file info
88 # verify existence, deduce file type from contents, get size, check for raw filesystem image or NOOBS archive
89 sub collect_file_info
90 {
91 my $input = PiFlash::State::input();
92  
93 # verify input file exists
94 if (! -e $input->{path}) {
95 PiFlash::State->error("input ".$input->{path}." does not exist");
96 }
97 if (! -f $input->{path}) {
98 PiFlash::State->error("input ".$input->{path}." is not a regular file");
99 }
100  
101 # use libmagic/file to collect file data
102 # it is collected even if type will be determined by suffix so we can later inspect data further
103 {
104 my $magic = File::LibMagic->new();
105 $input->{info} = $magic->info_from_filename($input->{path});
106 if ($input->{info}{mime_type} eq "application/gzip"
107 or $input->{info}{mime_type} eq "application/x-xz")
108 {
109 my $uncompress_magic = File::LibMagic->new(uncompress => 1);
110 $input->{info}{uncompress} = $uncompress_magic->info_from_filename($input->{path});
111 }
112 }
113  
114 # parse the file name
115 $input->{parse} = {};
116 ($input->{parse}{name}, $input->{parse}{path}, $input->{parse}{suffix})
117 = fileparse($input->{path}, map {".".$_} @known_suffixes);
118  
119 # use libmagic/file to determine file type from contents
120 say "input file is a ".$input->{info}{description};
121 if ($input->{info}{description} =~ /^Zip archive data/i) {
122 $input->{type} = "zip";
123 } elsif ($input->{info}{description} =~ /^gzip compressed data/i) {
124 $input->{type} = "gz";
125 } elsif ($input->{info}{description} =~ /^XZ compressed data/i) {
126 $input->{type} = "xz";
127 } elsif ($input->{info}{description} =~ /^DOS\/MBR boot sector/i) {
128 $input->{type} = "img";
129 }
130 if (!exists $input->{type}) {
131 PiFlash::State->error("collect_file_info(): file type not recognized on $input->{path}");
132 }
133  
134 # get file size - start with raw file size, update later if it's compressed/archive
135 $input->{size} = -s $input->{path};
136  
137 # find embedded image in archived/compressed files (either *.img or a NOOBS image)
138 if ($input->{type} eq "zip") {
139 # process zip archives
140 my @zip_content = PiFlash::Command::cmd2str("unzip - list contents", PiFlash::Command::prog("unzip"),
141 "-l", $input->{path});
142 chomp @zip_content;
143 my $found_build_data = 0;
144 my @imgfiles;
145 my $zip_lastline = pop @zip_content; # last line contains total size
146 $zip_lastline =~ /^\s*(\d+)/;
147 $input->{size} = $1;
148 foreach my $zc_entry (@zip_content) {
149 if ($zc_entry =~ /\sBUILD-DATA$/) {
150 $found_build_data = 1;
151 } elsif ($zc_entry =~ /^\s*(\d+)\s.*\s([^\s]*)$/) {
152 push @imgfiles, [$2, $1];
153 }
154 }
155  
156 # detect if the zip archive contains Raspberry Pi NOOBS (New Out Of the Box System)
157 if ($found_build_data) {
158 my @noobs_version = grep {/^NOOBS Version:/} PiFlash::Command::cmd2str("unzip - check for NOOBS",
159 PiFlash::Command::prog("unzip"), "-p", $input->{path}, "BUILD-DATA");
160 chomp @noobs_version;
161 if (scalar @noobs_version > 0) {
162 if ($noobs_version[0] =~ /^NOOBS Version: (.*)/) {
163 $input->{NOOBS} = $1;
164 }
165 }
166 }
167  
168 # if NOOBS system was not found, look for a *.img file
169 if (!exists $input->{NOOBS}) {
170 if (scalar @imgfiles == 0) {
171 PiFlash::State->error("input file is a zip archive but does not contain a *.img file or NOOBS system");
172 }
173 $input->{imgfile} = $imgfiles[0][0];
174 $input->{size} = $imgfiles[0][1];
175 }
176 } elsif ($input->{type} eq "gz") {
177 # process gzip compressed files
178 my @gunzip_out = PiFlash::Command::cmd2str("gunzip - list contents", PiFlash::Command::prog("gunzip"),
179 "--list", "--quiet", $input->{path});
180 chomp @gunzip_out;
181 $gunzip_out[0] =~ /^\s+\d+\s+(\d+)\s+[\d.]+%\s+(.*)/;
182 $input->{size} = $1;
183 $input->{imgfile} = $2;
184 } elsif ($input->{type} eq "xz") {
185 # process xz compressed files
186 if ($input->{path} =~ /^.*\/([^\/]*\.img)\.xz/) {
187 $input->{imgfile} = $1;
188 }
189 my @xz_out = PiFlash::Command::cmd2str("xz - list contents", PiFlash::Command::prog("xz"), "--robot",
190 "--list", $input->{path});
191 chomp @xz_out;
192 foreach my $xz_line (@xz_out) {
193 if ($xz_line =~ /^file\s+\d+\s+\d+\s+\d+\s+(\d+)/) {
194 $input->{size} = $1;
195 last;
196 }
197 }
198 }
199 }
200  
201 # collect output device info
202 sub collect_device_info
203 {
204 my $output = PiFlash::State::output();
205  
206 # check that device exists
207 if (! -e $output->{path}) {
208 PiFlash::State->error("output device ".$output->{path}." does not exist");
209 }
210 if (! -b $output->{path}) {
211 PiFlash::State->error("output device ".$output->{path}." is not a block device");
212 }
213  
214 # check block device parameters
215  
216 # load block device info into %output
217 blkparam(@blkdev_params);
218 if ($output->{mountpoint} ne "") {
219 PiFlash::State->error("output device is mounted - this operation would erase it");
220 }
221 if (!(exists $output->{fstype}) or $output->{fstype} =~ /^\s*$/) {
222 # workaround for apparent bug in lsblk in util-linux which omits requested FSTYPE data
223 $output->{fstype} = get_fstype($output->{path}) // "";
224 }
225 if ($output->{fstype} eq "swap") {
226 PiFlash::State->error("output device is a swap device - this operation would erase it");
227 }
228 if ($output->{type} eq "part") {
229 PiFlash::State->error("output device is a partition - Raspberry Pi flash needs whole SD device");
230 }
231  
232 # check for SD/MMC card via USB or PCI bus interfaces
233 if (!is_sd()) {
234 PiFlash::State->error("output device is not an SD card - this operation would erase it");
235 }
236 }
237  
238 # blkparam function: get device information with lsblk command
239 # usage: blkparam(\%output, param-name, ...)
240 # output: reference to hash with output device parameter strings
241 # param-name: list of parameter names to read into output hash
242 sub blkparam
243 {
244 # use PiFlash::State::output device unless another hash is provided
245 my $blkdev;
246 if (ref($_[0]) eq "HASH") {
247 $blkdev = shift @_;
248 } else {
249 $blkdev = PiFlash::State::output();
250 }
251  
252 # get the device's path
253 # throw an exception if the device's hash data doesn't have it
254 if (!exists $blkdev->{path}) {
255 PiFlash::State::error("blkparam: device hash does not contain path to device");
256 }
257 my $path = $blkdev->{path};
258  
259 # loop through the requested parameters and get each one for the device with lsblk
260 foreach my $paramname (@_) {
261 if (exists $blkdev->{lc $paramname}) {
262 # skip names of existing data to avoid overwriting
263 say STDERR "blkparam(): skipped collection of parameter $paramname to avoid overwriting existing data";
264 next;
265 }
266 my $value = PiFlash::Command::cmd2str("lsblk lookup of $paramname", PiFlash::Command::prog("lsblk"),
267 "--bytes", "--nodeps", "--noheadings", "--output", $paramname, $path);
268 if ($? == -1) {
269 PiFlash::State->error("blkparam($paramname): failed to execute lsblk: $!");
270 } elsif ($? & 127) {
271 PiFlash::State->error(sprintf "blkparam($paramname): lsblk died with signal %d, %s coredump",
272 ($? & 127), ($? & 128) ? 'with' : 'without');
273 } elsif ($? != 0) {
274 PiFlash::State->error(sprintf "blkparam($paramname): lsblk exited with value %d", $? >> 8);
275 }
276 chomp $value;
277 $value =~ s/^\s*//; # remove leading whitespace
278 $value =~ s/\s*$//; # remove trailing whitespace
279 $blkdev->{lc $paramname} = $value;
280 }
281 }
282  
283 # check if a device is an SD card
284 sub is_sd
285 {
286 # use PiFlash::State::output device unless another hash is provided
287 my $blkdev;
288 if (ref($_[0]) eq "HASH") {
289 $blkdev = shift @_;
290 } else {
291 $blkdev = PiFlash::State::output();
292 }
293  
294 # check for SD/MMC card via USB or PCI bus interfaces
295 if ($blkdev->{model} eq "SD/MMC") {
296 # detected SD card via USB adapter
297 PiFlash::State::verbose() and say "output device ".$blkdev->{path}." is an SD card via USB adapter";
298 return 1;
299 }
300  
301 # check if the SD card driver operates this device
302 my $found_mmc = 0;
303 my $found_usb = 0;
304 my @subsystems = split /:/, $blkdev->{subsystems};
305 foreach my $subsystem (@subsystems) {
306 if ($subsystem eq "mmc_host" or $subsystem eq "mmc") {
307 $found_mmc = 1;
308 }
309 if ($subsystem eq "usb") {
310 $found_usb = 1;
311 }
312 }
313 if ($found_mmc) {
314 # verify that the MMC device is actually an SD card
315 my $sysfs_devtype_path = "/sys/block/".basename($blkdev->{path})."/device/type";
316 if (! -f $sysfs_devtype_path) {
317 PiFlash::State->error("cannot find output device ".$blkdev->{path}." type - Linux kernel "
318 .PiFlash::State::system("release")." may be too old");
319 }
320 my $sysfs_devtype = slurp($sysfs_devtype_path);
321 chomp $sysfs_devtype;
322 PiFlash::State::verbose() and say "output device ".$blkdev->{path}." is a $sysfs_devtype";
323 if ($sysfs_devtype eq "SD") {
324 return 1;
325 }
326 }
327  
328 # allow USB writable/hotplug/removable drives with physical sector size 512
329 # this is imprecise because some other non-SD flash devices will be accepted as SD
330 # it will still avoid allowing hard drives to be erased
331 if ($found_usb) {
332 if($blkdev->{ro}==0 and $blkdev->{rm}==1 and $blkdev->{hotplug}==1 and $blkdev->{"phy-sec"}==512) {
333 PiFlash::State::verbose() and say "output device ".$blkdev->{path}." close enough: USB removable writable hotplug ps=512";
334 return 1;
335 }
336 }
337
338 PiFlash::State::verbose() and say "output device ".$blkdev->{path}." rejected as SD card";
339 return 0;
340 }
341  
342 # search for and print names of SD card devices
343 sub sd_search
344 {
345 # add block devices to system info
346 my $system = PiFlash::State::system();
347 $system->{blkdev} = {};
348  
349 # loop through available devices - collect info and print list of available SD cards
350 my @blkdev = PiFlash::Command::cmd2str("lsblk - find block devices", PiFlash::Command::prog("lsblk"),
351 "--nodeps", "--noheadings", "--list", "--output", "NAME");
352 my @sdcard;
353 foreach my $blkdevname (@blkdev) {
354 $system->{blkdev}{$blkdevname} = {};
355 my $blkdev = $system->{blkdev}{$blkdevname};
356 $blkdev->{path} = "/dev/$blkdevname";
357 blkparam($blkdev, @blkdev_params);
358 if (is_sd($blkdev)) {
359 push @sdcard, $blkdev->{path};
360 }
361 }
362  
363 # print results of SD search
364 if (scalar @sdcard == 0) {
365 say "no SD cards found on system";
366 } else {
367 say "SD cards found: ".join(" ", @sdcard);
368 }
369 }
370  
371 # base function: get basename from a file path
372 sub base
373 {
374 my $path = shift;
375 my $filename = File::Basename::fileparse($path, ());
376 return $filename;
377 }
378  
379 # get filesystem type info
380 # workaround for apparent bug in lsblk (from util-linux) which omits requested FSTYPE data when in the background
381 # use blkid or libmagic if it fails
382 sub get_fstype
383 {
384 my $devpath = shift;
385 my $fstype;
386 try {
387 $fstype = PiFlash::Command::cmd2str( "use lsblk to get fs type for $devpath", PiFlash::Command::prog("sudo"),
388 PiFlash::Command::prog("lsblk"), "--nodeps", "--noheadings", "--output", "FSTYPE", $devpath);
389 };
390  
391 # fallback: use blkid
392 if ((!defined $fstype) or $fstype =~ /^\s*$/) {
393 $fstype = PiFlash::Command::cmd2str( "use blkid to get fs type for $devpath", PiFlash::Command::prog("sudo"),
394 PiFlash::Command::prog("blkid"), "--probe", "--output=value", "--match-tag=TYPE", $devpath);
395  
396 # fallback: use File::LibMagic as backup filesystem type lookup
397 if ((!defined $fstype) or $fstype =~ /^\s*$/) {
398 my $magic = File::LibMagic->new();
399 $fstype = undef;
400 $magic->{flags} |= File::LibMagic::MAGIC_DEVICES; # undocumented trick for equivalent of "file -s" on device
401 my $magic_data = $magic->info_from_filename($devpath);
402 if (PiFlash::State::verbose()) {
403 for my $key (keys %$magic_data) {
404 say "get_fstype: magic_data/$key = ".$magic_data->{$key};
405 }
406 }
407 if ($magic_data->{description} =~ /^Linux rev \d+.\d+ (ext[234]) filesystem data,/) {
408 $fstype=$1;
409 } elsif ($magic_data->{description} =~ /^DOS\/MBR boot sector, .*, OEM-ID "mkfs.fat",.*, FAT (32 bit),/) {
410 $fstype="vfat";
411 } elsif ($magic_data->{description} =~ /^Linux\/\w+ swap file/) {
412 $fstype="swap";
413 } elsif ($magic_data->{description} =~ /\s+(\w+)\sfilesystem/i) {
414 $fstype=lc $1;
415 }
416 }
417 }
418  
419 # lookup failure if we get here
420 defined $fstype and chomp $fstype;
421 PiFlash::State::verbose() and say "get_fstype($devpath) = ".($fstype // "undef");
422 return $fstype;
423 }
424  
425 1;
426  
427 __END__
« PiFlash.pm ↑ Summary