3 # [x] resize root filesystem
4 # [x] find root fs uuid
5 # [x] find boot partition
9 # [x] extract wifi firmware
10 # [ ] on life system skip everything but wifi firmware
11 # [x] change uuid of ext4
13 my $firmware_tarball = '/boot/efi/linux-firmware.tar';
14 my $firmware_manifest = '/lib/firmware/ASAHI_FIRMWARE_MANIFEST';
15 my $grubcfg = '/mnt/EFI/debian/grub.cfg';
20 open(MOUNT
, '<', '/proc/mounts') || die ("Can not open /proc/mounts for reading: $!");
25 if (/^([\S]+)+ \/ /) {
30 die("Could not find root device");
34 find_fs_uuid_of_device
36 my $dev = shift || die;
37 my $blkid_output = `blkid ${dev}`;
39 if ($blkid_output =~ /UUID="([^"]+)"/) {
43 die("Could not find fs uuid of $dev");
49 my $uuid_in_grub_cfg = shift || die;
52 my $efi_parition = undef;
55 if (/^([\S]+):.*TYPE="vfat"/) {
56 push(@candidates, $1);
60 for my $dev (@candidates) {
61 system("mount -o ro $dev /mnt");
63 open(GRUBCFG
, '<', $grubcfg) || die ("Can't open $grubcfg: $!");
64 my @lines = <GRUBCFG
>;
66 if (/${uuid_in_grub_cfg}/) {
72 system("umount /mnt");
73 last if defined $efi_parition;
76 die ("No efi parition found") unless defined $efi_parition;
84 my $root_fs_uuid = shift || die;
85 my $efi_fs_uuid = shift || die;
87 open(FSTAB
, '>', '/etc/fstab') || die ("Can not open fstab");
89 UUID="$root_fs_uuid" / ext4 defaults 0 0
90 UUID="$efi_fs_uuid" /boot/efi vfat defaults 0 0
98 system('rm -rf /boot/efi/*');
99 system('apt-get install -y grub-efi-arm64-signed-');
100 system("echo 'grub-efi-arm64 grub2/update_nvram boolean false' | debconf-set-selections");
101 system("echo 'grub-efi-arm64 grub2/force_efi_extra_removable boolean true' | debconf-set-selections");
102 system("dpkg-reconfigure -fnoninteractive grub-efi-arm64");
103 system("update-grub");
107 update_wifi_firmware_if_necessary
109 return unless -f
$firmware_tarball;
111 if (-f
$firmware_manifest) {
112 system("sha256sum -c $firmware_manifest --quiet");
116 system("sha256sum $firmware_tarball > $firmware_manifest");
117 system("tar -C /lib/firmware/ -xf $firmware_tarball");
119 system('rmmod brcmfmac');
120 system('rmmod brcmutil');
122 system('modprobe brcmfmac');
124 system('rmmod brcmfmac');
126 system('modprobe brcmfmac');
129 my $root_block_device = undef;
130 my $initial_root_fs_uuid = undef;
131 my $final_root_fs_uuid = undef;
132 my $efi_block_device = undef;
133 my $efi_fs_uuid = undef;
135 unless (-f
'/boot/grub/grub.cfg') {
136 $root_block_device = find_root_device
();
137 system("resize2fs $root_block_device");
138 $initial_root_fs_uuid = find_fs_uuid_of_device
($root_block_device);
139 $efi_block_device = find_efi_parition
($initial_root_fs_uuid);
140 system("mlabel -s -n :: -i $efi_block_device");
141 system("tune2fs -U random ${root_block_device}");
142 $efi_fs_uuid = find_fs_uuid_of_device
($efi_block_device);
143 $final_root_fs_uuid = find_fs_uuid_of_device
($root_block_device);
144 generate_fstab
($final_root_fs_uuid, $efi_fs_uuid);
145 system('mount /boot/efi');
149 update_wifi_firmware_if_necessary
();