]> git.zerfleddert.de Git - m1-debian/blob - files/rc.local
e6f9c6d205651639807396e78ed46e35c6870c2b
[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 my $grubcfg = '/mnt/EFI/debian/grub.cfg';
16
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
33 sub
34 find_fs_uuid_of_device
35 {
36 my $dev = shift || die;
37 my $blkid_output = `blkid ${dev}`;
38
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");
62 if (-f $grubcfg) {
63 open(GRUBCFG, '<', $grubcfg) || die ("Can't open $grubcfg: $!");
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
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;
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 system("update-grub");
103 }
104
105 sub
106 update_wifi_firmware_if_necessary
107 {
108 return unless -f $firmware_tarball;
109
110 if (-f $firmware_manifest) {
111 system("sha256sum -c $firmware_manifest --quiet");
112 return if $? == 0;
113 }
114
115 system("sha256sum $firmware_tarball > $firmware_manifest");
116 system("tar -C /lib/firmware/ -xf $firmware_tarball");
117
118 system('rmmod brcmfmac');
119 system('rmmod brcmutil');
120 sleep(1);
121 system('modprobe brcmfmac');
122 sleep(1);
123 system('rmmod brcmfmac');
124 sleep(1);
125 system('modprobe brcmfmac');
126 }
127
128 my $root_block_device = undef;
129 my $root_fs_uuid = undef;
130 my $efi_block_device = undef;
131 my $efi_fs_uuid = undef;
132
133 unless (-f '/boot/grub/grub.cfg') {
134 $root_block_device = find_root_device();
135 system("resize2fs $root_block_device");
136 $root_fs_uuid = find_fs_uuid_of_device($root_block_device);
137 $efi_block_device = find_efi_parition($root_fs_uuid);
138 $efi_fs_uuid = find_fs_uuid_of_device($efi_block_device);
139 generate_fstab($root_fs_uuid, $efi_fs_uuid);
140 system('mount /boot/efi');
141 install_grub();
142 }
143
144 update_wifi_firmware_if_necessary();
Impressum, Datenschutz