]>
Commit | Line | Data |
---|---|---|
e1af00a3 TG |
1 | #!/usr/bin/perl |
2 | ||
3 | # [x] resize root filesystem | |
e323215d | 4 | # [x] find root fs uuid |
ccc0e1bb TG |
5 | # [x] find boot partition |
6 | # [x] generate fstab | |
7 | # [x] mount /boot/efi | |
d72e8ddd | 8 | # [x] install grub |
de764601 TG |
9 | # [x] extract wifi firmware |
10 | # [ ] on life system skip everything but wifi firmware | |
fb2efba4 | 11 | # [x] change uuid of ext4 |
e323215d | 12 | |
de764601 TG |
13 | my $firmware_tarball = '/boot/efi/linux-firmware.tar'; |
14 | my $firmware_manifest = '/lib/firmware/ASAHI_FIRMWARE_MANIFEST'; | |
60b2ebc1 | 15 | my $grubcfg = '/mnt/EFI/debian/grub.cfg'; |
de764601 | 16 | |
e1af00a3 TG |
17 | sub |
18 | find_root_device | |
19 | { | |
20 | open(MOUNT, '<', '/proc/mounts') || die ("Can not open /proc/mounts for reading: $!"); | |
21 | my @lines = <MOUNT>; | |
22 | close(MOUNT); | |
23 | ||
24 | for (@lines) { | |
25 | if (/^([\S]+)+ \/ /) { | |
26 | return $1; | |
27 | } | |
28 | } | |
29 | ||
30 | die("Could not find root device"); | |
31 | } | |
32 | ||
e323215d TG |
33 | sub |
34 | find_fs_uuid_of_device | |
35 | { | |
36 | my $dev = shift || die; | |
37 | my $blkid_output = `blkid ${dev}`; | |
de764601 | 38 | |
e323215d TG |
39 | if ($blkid_output =~ /UUID="([^"]+)"/) { |
40 | return $1; | |
41 | } | |
42 | ||
43 | die("Could not find fs uuid of $dev"); | |
44 | } | |
45 | ||
46 | sub | |
47 | find_efi_parition | |
48 | { | |
49 | my $uuid_in_grub_cfg = shift || die; | |
50 | my @candidates; | |
51 | ||
52 | my $efi_parition = undef; | |
53 | ||
54 | for (`blkid`) { | |
55 | if (/^([\S]+):.*TYPE="vfat"/) { | |
56 | push(@candidates, $1); | |
57 | } | |
58 | } | |
59 | ||
60 | for my $dev (@candidates) { | |
61 | system("mount -o ro $dev /mnt"); | |
60b2ebc1 TG |
62 | if (-f $grubcfg) { |
63 | open(GRUBCFG, '<', $grubcfg) || die ("Can't open $grubcfg: $!"); | |
e323215d TG |
64 | my @lines = <GRUBCFG>; |
65 | for (@lines) { | |
66 | if (/${uuid_in_grub_cfg}/) { | |
67 | $efi_parition = $dev; | |
68 | } | |
69 | } | |
70 | close(GRUBCFG); | |
71 | } | |
72 | system("umount /mnt"); | |
73 | last if defined $efi_parition; | |
74 | } | |
75 | ||
76 | die ("No efi parition found") unless defined $efi_parition; | |
77 | ||
78 | return $efi_parition; | |
79 | } | |
80 | ||
ccc0e1bb TG |
81 | sub |
82 | generate_fstab | |
83 | { | |
84 | my $root_fs_uuid = shift || die; | |
85 | my $efi_fs_uuid = shift || die; | |
86 | ||
87 | open(FSTAB, '>', '/etc/fstab') || die ("Can not open fstab"); | |
88 | print FSTAB <<EOF; | |
ccc0e1bb TG |
89 | UUID="$root_fs_uuid" / ext4 defaults 0 0 |
90 | UUID="$efi_fs_uuid" /boot/efi vfat defaults 0 0 | |
91 | EOF | |
92 | close(FSTAB); | |
93 | } | |
e1af00a3 | 94 | |
d72e8ddd TG |
95 | sub |
96 | install_grub | |
97 | { | |
73dfd698 | 98 | system('rm -rf /boot/efi/*'); |
d72e8ddd | 99 | system('apt-get install -y grub-efi-arm64-signed-'); |
d72e8ddd TG |
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"); | |
60b2ebc1 | 103 | system("update-grub"); |
d72e8ddd TG |
104 | } |
105 | ||
de764601 TG |
106 | sub |
107 | update_wifi_firmware_if_necessary | |
108 | { | |
109 | return unless -f $firmware_tarball; | |
110 | ||
111 | if (-f $firmware_manifest) { | |
112 | system("sha256sum -c $firmware_manifest --quiet"); | |
113 | return if $? == 0; | |
114 | } | |
115 | ||
116 | system("sha256sum $firmware_tarball > $firmware_manifest"); | |
117 | system("tar -C /lib/firmware/ -xf $firmware_tarball"); | |
118 | ||
119 | system('rmmod brcmfmac'); | |
120 | system('rmmod brcmutil'); | |
121 | sleep(1); | |
122 | system('modprobe brcmfmac'); | |
123 | sleep(1); | |
124 | system('rmmod brcmfmac'); | |
125 | sleep(1); | |
126 | system('modprobe brcmfmac'); | |
127 | } | |
128 | ||
ccc0e1bb | 129 | my $root_block_device = undef; |
fb2efba4 TG |
130 | my $initial_root_fs_uuid = undef; |
131 | my $final_root_fs_uuid = undef; | |
ccc0e1bb TG |
132 | my $efi_block_device = undef; |
133 | my $efi_fs_uuid = undef; | |
134 | ||
5bd8c45b | 135 | unless (-f '/boot/grub/grub.cfg') { |
ccc0e1bb TG |
136 | $root_block_device = find_root_device(); |
137 | system("resize2fs $root_block_device"); | |
fb2efba4 TG |
138 | $initial_root_fs_uuid = find_fs_uuid_of_device($root_block_device); |
139 | $efi_block_device = find_efi_parition($initial_root_fs_uuid); | |
076a2682 | 140 | system("mlabel -s -n :: -i $efi_block_device"); |
fb2efba4 | 141 | system("tune2fs -U random ${root_block_device}"); |
076a2682 | 142 | $efi_fs_uuid = find_fs_uuid_of_device($efi_block_device); |
fb2efba4 TG |
143 | $final_root_fs_uuid = find_fs_uuid_of_device($root_block_device); |
144 | generate_fstab($final_root_fs_uuid, $efi_fs_uuid); | |
ccc0e1bb | 145 | system('mount /boot/efi'); |
d72e8ddd | 146 | install_grub(); |
ccc0e1bb | 147 | } |
de764601 TG |
148 | |
149 | update_wifi_firmware_if_necessary(); |