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