PiFlash/MediaWriter.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« PiFlash/Hook.pm ↑ Summary PiFlash.pm »
1 # PiFlash::MediaWriter - write to Raspberry Pi SD card installation with scriptable customization
2 # by Ian Kluft
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use v5.14.0; # require 2011 or newer version of Perl
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use PiFlash::State;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use PiFlash::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use PiFlash::Inspector;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use PiFlash::Hook;
11  
12 package PiFlash::MediaWriter;
13 $PiFlash::MediaWriter::VERSION = '0.4.3';
14 use autodie; # report errors instead of silently continuing ("die" actions are used as exceptions - caught & reported)
15 use Try::Tiny;
16 use File::Basename;
17 use File::Slurp qw(slurp);
18  
19 # ABSTRACT: write to Raspberry Pi SD card installation with scriptable customization
20  
21  
22 # generate random hex digits
23 sub random_hex
24 {
25 my $length = shift;
26 my $hex = "";
27 while ($length > 0) {
28 my $chunk = ($length > 4) ? 4 : $length;
29 $length -= $chunk;
30 $hex .= sprintf "%0*x", $chunk, int(rand(16**$chunk));
31 }
32 return $hex;
33 }
34  
35 # generate a random UUID
36 # 128 bits/32 hexadecimal digits, used to set a probably-unique UUID on an ext2/3/4 filesystem we created
37 sub random_uuid
38 {
39 my $uuid;
40  
41 # start with our own contrived prefix for our UUIDs
42 $uuid .= "314decaf-"; # "314" first digits of pi (as in RasPi), and "decaf" among few words from hex digits
43  
44 # next 4 digits are from lower 4 hex digits of current time (rolls over every 18 days)
45 $uuid .= sprintf "%04x-", (time & 0xffff);
46  
47 # next 4 digits are the UUID format version (4 for random) and 3 random hex digits
48 $uuid .= "4".random_hex(3)."-";
49  
50 # next 4 digits are a UUID variant digit and 3 random hex digits
51 $uuid .= (sprintf "%x", 8+int(rand(4))).random_hex(3)."-";
52
53 # conclude with 8 random hex digits
54 $uuid .= random_hex(12);
55  
56 return $uuid;
57 }
58  
59 # generate a random label string
60 # 11 characters, used to set a probably-unique label on a VFAT/ExFAT filesystem we created
61 sub random_label
62 {
63 my $label = "RPI";
64 for (my $i=0; $i<8; $i++) {
65 my $num = int(rand(36));
66 if ($num <= 9) {
67 $label .= chr(ord('0')+$num);
68 } else {
69 $label .= chr(ord('A')+$num-10);
70 }
71 }
72 return $label;
73 }
74  
75 # reread partition table, with retries if necessary
76 sub reread_pt
77 {
78 my $reason = shift;
79  
80 # re-read partition table, use multiple tries if necessary
81 my $tries = 10;
82 while (1) {
83 try {
84 PiFlash::Command::cmd("reread partition table for $reason", PiFlash::Command::prog("sudo"),
85 PiFlash::Command::prog("blockdev"), "--rereadpt", PiFlash::State::output("path"));
86 };
87  
88 # check for errors, retry if possible
89 if ($@) {
90 if (ref $@) {
91 # reference means unrecognized error - rethrow the exception
92 die $@;
93 } elsif ($@ =~ /exited with value 1/) {
94 # exit status 1 means retry
95 $tries--;
96 if ($tries > 0) {
97 # wait a second and try again - sync may need to settle
98 sleep 1;
99 next;
100 }
101 # otherwise fail for repeated failed retries
102 die $@;
103 } else {
104 # other unrecognized error - rethrow the exception
105 die $@;
106 }
107 }
108  
109 # got through without an error - done
110 last;
111 }
112 }
113  
114 # look up boot and root partition & filesystem info
115 # save data in PiFlash::State::output
116 sub get_sd_partitions
117 {
118 my $output = PiFlash::State::output();
119 (exists $output->{partitions}) and return;
120 my @partitions = grep {/part\s*$/} PiFlash::Command::cmd2str("lsblk - find partitions",
121 PiFlash::Command::prog("lsblk"), "--list", PiFlash::State::output("path"));
122  
123 if (@partitions) {
124 for (my $i=0; $i<scalar @partitions; $i++) {
125 $partitions[$i] =~ s/^([^\s]+)\s.*/$1/;
126 }
127 my $part_boot = $partitions[0];
128 my $num_root = scalar @partitions;
129 my $part_root = $partitions[$num_root-1];
130 PiFlash::State::output("num_boot", 0);
131 PiFlash::State::output("part_boot", $part_boot);
132 PiFlash::State::output("fstype_boot", PiFlash::Inspector::get_fstype("/dev/$part_boot"));
133 PiFlash::State::output("num_root", $num_root);
134 PiFlash::State::output("part_root", $part_root);
135 PiFlash::State::output("fstype_root", PiFlash::Inspector::get_fstype("/dev/$part_root"));
136 }
137 PiFlash::State::output("partitions", \@partitions);
138  
139 if (PiFlash::State::verbose()) {
140 print "get_sd_partitions: ";
141 for my $key (qw(num_boot part_boot fstype_boot num_root part_root fstype_root)) {
142 print "$key=".(PiFlash::State::output($key) // "undef")." ";
143 }
144 print "\n";
145 }
146 }
147  
148 # flash the output device from the input file
149 sub flash_device
150 {
151 # flash the device
152 if (PiFlash::State::has_input("imgfile")) {
153 # if we know an embedded image file name, use it in the start message
154 say "flashing ".PiFlash::State::input("path")." / ".PiFlash::State::input("imgfile")." -> "
155 .PiFlash::State::output("path");
156 } else {
157 # print a start message with source and destination
158 say "flashing ".PiFlash::State::input("path")." -> ".PiFlash::State::output("path");
159 }
160 say "wait for it to finish - this takes a while, progress not always indicated";
161 my $dd_args = "bs=4M oflag=sync status=progress";
162 if (PiFlash::State::input("type") eq "img") {
163 PiFlash::Command::cmd("dd flash", PiFlash::Command::prog("sudo")." ".PiFlash::Command::prog("dd")
164 ." if=\"".PiFlash::State::input("path")."\" of=\""
165 .PiFlash::State::output("path")."\" $dd_args" );
166 } elsif (PiFlash::State::input("type") eq "zip") {
167 if (PiFlash::State::has_input("NOOBS")) {
168 # format SD and copy NOOBS archive to it
169 my $label = random_label();
170 PiFlash::State::output("label", $label);
171 my $fstype = PiFlash::State::system("primary_fs");
172 if ($fstype ne "vfat") {
173 PiFlash::State->error("NOOBS requires VFAT filesystem, not in this kernel - need to load a module?");
174 }
175 say "formatting $fstype filesystem for Raspberry Pi NOOBS system...";
176 PiFlash::Command::cmd("write partition table", PiFlash::Command::prog("echo"), "type=c", "|",
177 PiFlash::Command::prog("sudo"), PiFlash::Command::prog("sfdisk"), PiFlash::State::output("path"));
178 my @partitions = grep {/part\s*$/} PiFlash::Command::cmd2str("lsblk - find partitions",
179 PiFlash::Command::prog("lsblk"), "--list", PiFlash::State::output("path"));
180 $partitions[0] =~ /^([^\s]+)\s/;
181 my $partition = "/dev/".$1;
182 PiFlash::Command::cmd("format sd card", PiFlash::Command::prog("sudo"),
183 PiFlash::Command::prog("mkfs.$fstype"), "-n", $label, $partition);
184 my $mntdir = PiFlash::State::system("media_dir")."/piflash/sdcard";
185 PiFlash::Command::cmd("reread partition table for NOOBS", PiFlash::Command::prog("sudo"),
186 PiFlash::Command::prog("blockdev"), "--rereadpt", PiFlash::State::output("path"));
187 PiFlash::Command::cmd("create mount point", PiFlash::Command::prog("sudo"),
188 PiFlash::Command::prog("mkdir"), "-p", $mntdir );
189 PiFlash::Command::cmd("mount SD card", PiFlash::Command::prog("sudo"), PiFlash::Command::prog("mount"),
190 "-t", $fstype, "LABEL=$label", $mntdir);
191 PiFlash::Command::cmd("unzip NOOBS contents", PiFlash::Command::prog("sudo"),
192 PiFlash::Command::prog("unzip"), "-d", $mntdir, PiFlash::State::input("path"));
193 PiFlash::Command::cmd("unmount SD card", PiFlash::Command::prog("sudo"), PiFlash::Command::prog("umount"),
194 $mntdir);
195 } else {
196 # flash zip archive to SD
197 PiFlash::Command::cmd("unzip/dd flash", PiFlash::Command::prog("unzip")." -p \""
198 .PiFlash::State::input("path")."\" \"".PiFlash::State::input("imgfile")."\" | "
199 .PiFlash::Command::prog("sudo")." ".PiFlash::Command::prog("dd")." of=\""
200 .PiFlash::State::output("path")."\" $dd_args");
201 }
202 } elsif (PiFlash::State::input("type") eq "gz") {
203 # flash gzip-compressed image file to SD
204 PiFlash::Command::cmd("gunzip/dd flash", PiFlash::Command::prog("gunzip")." --stdout \""
205 .PiFlash::State::input("path")."\" | ".PiFlash::Command::prog("sudo")." ".PiFlash::Command::prog("dd")
206 ." of=\"".PiFlash::State::output("path")."\" $dd_args");
207 } elsif (PiFlash::State::input("type") eq "xz") {
208 # flash xz-compressed image file to SD
209 PiFlash::Command::cmd("xz/dd flash", PiFlash::Command::prog("xz")." --decompress --stdout \""
210 .PiFlash::State::input("path")."\" | ".PiFlash::Command::prog("sudo")." ".PiFlash::Command::prog("dd")
211 ." of=\"".PiFlash::State::output("path")."\" $dd_args");
212 }
213 say "- synchronizing buffers";
214 PiFlash::Command::cmd("sync", PiFlash::Command::prog("sync"));
215 reread_pt("post-sync"); # re-read partition table, use multiple tries if necessary
216 get_sd_partitions();
217 my @partitions = PiFlash::State::output("partitions");
218  
219 # check if there are any partitions before partition-dependent processing
220 # protects from scenario (such as RISCOS) where whole-device filesystem has no partition table
221 if (@partitions) {
222 my $sd_name = basename(PiFlash::State::output("path"));
223 my $num_boot = PiFlash::State::output("num_boot");
224 my $part_boot = PiFlash::State::output("part_boot");
225 my $num_root = PiFlash::State::output("num_root");
226 my $part_root = PiFlash::State::output("part_root");
227 my $fstype_root = PiFlash::State::output("fstype_root");
228  
229 # resize root filesystem if command-line flag is set
230 # resize flag is silently ignored for NOOBS images because it will re-image and resize
231 if (PiFlash::State::has_cli_opt("resize") and not PiFlash::State::has_input("NOOBS")) {
232 say "- resizing the partition";
233  
234 if ((defined $fstype_root) and $fstype_root =~ /^ext[234]/ ) {
235 # ext2/3/4 filesystem can be resized
236 my @sfdisk_resize_input = ( ", +" );
237 PiFlash::Command::cmd2str(\@sfdisk_resize_input, "resize partition",
238 PiFlash::Command::prog("sudo"), PiFlash::Command::prog("sfdisk"), "--quiet", "--no-reread", "-N",
239 $num_root, PiFlash::State::output("path"));
240 say "- checking the filesystem";
241 PiFlash::Command::cmd2str("filesystem check", PiFlash::Command::prog("sudo"),
242 PiFlash::Command::prog("e2fsck"), "-fy", "/dev/$part_root");
243 say "- resizing the filesystem";
244 PiFlash::Command::cmd2str("resize filesystem", PiFlash::Command::prog("sudo"),
245 PiFlash::Command::prog("resize2fs"), "/dev/$part_root");
246 } else {
247 warn "unrecognized filesystem type ".($fstype_root // "")." - resize not attempted";
248 }
249 }
250  
251 # check if any hooks are registered for filesystem access
252 if (PiFlash::Hook::has("fs_mount")) {
253 reread_pt("filesystem hooks"); # re-read partition table, use multiple tries if necessary
254 get_sd_partitions();
255 my @partitions = PiFlash::State::output("partitions");
256 my $fstype_boot = PiFlash::State::output("fstype_boot");
257 my $dev_boot = "/dev/".PiFlash::State::output("part_boot");
258 my $fstype_root = PiFlash::State::output("fstype_root");
259 my $dev_root = "/dev/".PiFlash::State::output("part_root");
260 my $mntdir = PiFlash::State::system("media_dir")."/piflash/sdcard";
261 my $mnt_boot = $mntdir."/boot";
262 my $mnt_root = $mntdir."/root";
263 PiFlash::Command::cmd("create mount point for boot fs", PiFlash::Command::prog("sudo"),
264 PiFlash::Command::prog("mkdir"), "-p", $mnt_boot );
265 PiFlash::Command::cmd("create mount point for root fs", PiFlash::Command::prog("sudo"),
266 PiFlash::Command::prog("mkdir"), "-p", $mnt_root );
267 PiFlash::Command::cmd("mount boot fs", PiFlash::Command::prog("sudo"), PiFlash::Command::prog("mount"),
268 "-t", $fstype_boot, $dev_boot, $mnt_boot);
269 PiFlash::Command::cmd("mount root fs", PiFlash::Command::prog("sudo"), PiFlash::Command::prog("mount"),
270 "-t", $fstype_root, $dev_root, $mnt_root);
271 PiFlash::Hook::fs_mount({boot => $mnt_boot, root => $mnt_root});
272 PiFlash::Command::cmd("unmount root fs", PiFlash::Command::prog("sudo"), PiFlash::Command::prog("umount"),
273 $mnt_root);
274 PiFlash::Command::cmd("unmount boot fs", PiFlash::Command::prog("sudo"), PiFlash::Command::prog("umount"),
275 $mnt_boot);
276 }
277 }
278  
279 # call hooks for optional post-install tweaks
280 PiFlash::Hook::post_install();
281  
282 # report that it's done
283 say "done - it is safe to remove the SD card";
284 }
285  
286 1;
287  
288 __END__
« PiFlash/Hook.pm ↑ Summary PiFlash.pm »