]> git.zerfleddert.de Git - m1-debian/blob - files/rc.local
47dde6aac034e75a09473edaab7561cd63b49ab4
[m1-debian] / files / rc.local
1 #!/usr/bin/perl
2
3 # [x] resize root filesystem
4 # [x] find root fs uuid
5 # [x] find boot partition
6 # [x] generate fstab
7 # [x] mount /boot/efi
8 # [x] install grub
9 # [x] extract wifi firmware
10 # [ ] on life system skip everything but wifi firmware
11 # [ ] In order to change the uuid of the root filesystem, bootstrap.sh must remember it in a file within the mounted disk image. And the initrd needs to change it.
12
13 my $firmware_tarball = '/boot/efi/linux-firmware.tar';
14 my $firmware_manifest = '/lib/firmware/ASAHI_FIRMWARE_MANIFEST';
15
16 sub
17 find_root_device
18 {
19 open(MOUNT, '<', '/proc/mounts') || die ("Can not open /proc/mounts for reading: $!");
20 my @lines = <MOUNT>;
21 close(MOUNT);
22
23 for (@lines) {
24 if (/^([\S]+)+ \/ /) {
25 return $1;
26 }
27 }
28
29 die("Could not find root device");
30 }
31
32 sub
33 find_fs_uuid_of_device
34 {
35 my $dev = shift || die;
36 my $blkid_output = `blkid ${dev}`;
37
38 if ($blkid_output =~ /UUID="([^"]+)"/) {
39 return $1;
40 }
41
42 die("Could not find fs uuid of $dev");
43 }
44
45 sub
46 find_efi_parition
47 {
48 my $uuid_in_grub_cfg = shift || die;
49 my @candidates;
50
51 my $efi_parition = undef;
52
53 for (`blkid`) {
54 if (/^([\S]+):.*TYPE="vfat"/) {
55 push(@candidates, $1);
56 }
57 }
58
59 for my $dev (@candidates) {
60 system("mount -o ro $dev /mnt");
61 if (-f '/mnt/EFI/boot/grub.cfg') {
62 open(GRUBCFG, '<', '/mnt/EFI/boot/grub.cfg') || die ("Can't open /mnt/EFI/boot/grub.cfg: $!");
63 my @lines = <GRUBCFG>;
64 for (@lines) {
65 if (/${uuid_in_grub_cfg}/) {
66 $efi_parition = $dev;
67 }
68 }
69 close(GRUBCFG);
70 }
71 system("umount /mnt");
72 last if defined $efi_parition;
73 }
74
75 die ("No efi parition found") unless defined $efi_parition;
76
77 return $efi_parition;
78 }
79
80 sub
81 generate_fstab
82 {
83 my $root_fs_uuid = shift || die;
84 my $efi_fs_uuid = shift || die;
85
86 open(FSTAB, '>', '/etc/fstab') || die ("Can not open fstab");
87 print FSTAB <<EOF;
88
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 }
94
95 sub
96 install_grub
97 {
98 system('apt-get install -y grub-efi-arm64-signed-');
99 system("echo 'grub-efi-arm64 grub2/update_nvram boolean false' | debconf-set-selections");
100 system("echo 'grub-efi-arm64 grub2/force_efi_extra_removable boolean true' | debconf-set-selections");
101 system("dpkg-reconfigure -fnoninteractive grub-efi-arm64");
102 }
103
104 sub
105 update_wifi_firmware_if_necessary
106 {
107 return unless -f $firmware_tarball;
108
109 if (-f $firmware_manifest) {
110 system("sha256sum -c $firmware_manifest --quiet");
111 return if $? == 0;
112 }
113
114 system("sha256sum $firmware_tarball > $firmware_manifest");
115 system("tar -C /lib/firmware/ -xf $firmware_tarball");
116
117 system('rmmod brcmfmac');
118 system('rmmod brcmutil');
119 sleep(1);
120 system('modprobe brcmfmac');
121 sleep(1);
122 system('rmmod brcmfmac');
123 sleep(1);
124 system('modprobe brcmfmac');
125 }
126
127 my $root_block_device = undef;
128 my $root_fs_uuid = undef;
129 my $efi_block_device = undef;
130 my $efi_fs_uuid = undef;
131
132 unless (-f '/etc/fstab') {
133 $root_block_device = find_root_device();
134 system("resize2fs $root_block_device");
135 $root_fs_uuid = find_fs_uuid_of_device($root_block_device);
136 $efi_block_device = find_efi_parition($root_fs_uuid);
137 $efi_fs_uuid = find_fs_uuid_of_device($efi_block_device);
138 generate_fstab($root_fs_uuid, $efi_fs_uuid);
139 system('mount /boot/efi');
140 install_grub();
141 }
142
143 update_wifi_firmware_if_necessary();
Impressum, Datenschutz