]>
Commit | Line | Data |
---|---|---|
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 | # [ ] extract wifi firmware | |
10 | # [ ] 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. | |
11 | ||
12 | sub | |
13 | find_root_device | |
14 | { | |
15 | open(MOUNT, '<', '/proc/mounts') || die ("Can not open /proc/mounts for reading: $!"); | |
16 | my @lines = <MOUNT>; | |
17 | close(MOUNT); | |
18 | ||
19 | for (@lines) { | |
20 | if (/^([\S]+)+ \/ /) { | |
21 | return $1; | |
22 | } | |
23 | } | |
24 | ||
25 | die("Could not find root device"); | |
26 | } | |
27 | ||
28 | sub | |
29 | find_fs_uuid_of_device | |
30 | { | |
31 | my $dev = shift || die; | |
32 | my $blkid_output = `blkid ${dev}`; | |
33 | # /dev/nvme0n1p5: LABEL="/" UUID="fe9c5ac8-edf4-442e-a09e-e0451606898e" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="primary" PARTUUID="03378b79-346d-42f9-b404-44b22bc6798f" | |
34 | if ($blkid_output =~ /UUID="([^"]+)"/) { | |
35 | return $1; | |
36 | } | |
37 | ||
38 | die("Could not find fs uuid of $dev"); | |
39 | } | |
40 | ||
41 | sub | |
42 | find_efi_parition | |
43 | { | |
44 | my $uuid_in_grub_cfg = shift || die; | |
45 | my @candidates; | |
46 | ||
47 | my $efi_parition = undef; | |
48 | ||
49 | for (`blkid`) { | |
50 | if (/^([\S]+):.*TYPE="vfat"/) { | |
51 | push(@candidates, $1); | |
52 | } | |
53 | } | |
54 | ||
55 | for my $dev (@candidates) { | |
56 | system("mount -o ro $dev /mnt"); | |
57 | if (-f '/mnt/EFI/boot/grub.cfg') { | |
58 | open(GRUBCFG, '<', '/mnt/EFI/boot/grub.cfg') || die ("Can't open /mnt/EFI/boot/grub.cfg: $!"); | |
59 | my @lines = <GRUBCFG>; | |
60 | for (@lines) { | |
61 | if (/${uuid_in_grub_cfg}/) { | |
62 | $efi_parition = $dev; | |
63 | } | |
64 | } | |
65 | close(GRUBCFG); | |
66 | } | |
67 | system("umount /mnt"); | |
68 | last if defined $efi_parition; | |
69 | } | |
70 | ||
71 | die ("No efi parition found") unless defined $efi_parition; | |
72 | ||
73 | return $efi_parition; | |
74 | } | |
75 | ||
76 | sub | |
77 | generate_fstab | |
78 | { | |
79 | my $root_fs_uuid = shift || die; | |
80 | my $efi_fs_uuid = shift || die; | |
81 | ||
82 | open(FSTAB, '>', '/etc/fstab') || die ("Can not open fstab"); | |
83 | print FSTAB <<EOF; | |
84 | ||
85 | UUID="$root_fs_uuid" / ext4 defaults 0 0 | |
86 | UUID="$efi_fs_uuid" /boot/efi vfat defaults 0 0 | |
87 | EOF | |
88 | close(FSTAB); | |
89 | } | |
90 | ||
91 | sub | |
92 | install_grub | |
93 | { | |
94 | system('apt-get install -y grub-efi-arm64-signed-'); | |
95 | system('grub-install --target=arm64-efi --efi-directory=/boot/efi --removable'); | |
96 | system('update-grub'); | |
97 | system("echo 'grub-efi-arm64 grub2/update_nvram boolean false' | debconf-set-selections"); | |
98 | system("echo 'grub-efi-arm64 grub2/force_efi_extra_removable boolean true' | debconf-set-selections"); | |
99 | system("dpkg-reconfigure -fnoninteractive grub-efi-arm64"); | |
100 | } | |
101 | ||
102 | my $root_block_device = undef; | |
103 | my $root_fs_uuid = undef; | |
104 | my $efi_block_device = undef; | |
105 | my $efi_fs_uuid = undef; | |
106 | ||
107 | unless (-f '/etc/fstab') { | |
108 | $root_block_device = find_root_device(); | |
109 | system("resize2fs $root_block_device"); | |
110 | $root_fs_uuid = find_fs_uuid_of_device($root_block_device); | |
111 | $efi_block_device = find_efi_parition($root_fs_uuid); | |
112 | $efi_fs_uuid = find_fs_uuid_of_device($efi_block_device); | |
113 | generate_fstab($root_fs_uuid, $efi_fs_uuid); | |
114 | system('mount /boot/efi'); | |
115 | install_grub(); | |
116 | } |