forked from AsahiLinux/asahi-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst-boot
executable file
·109 lines (96 loc) · 4.01 KB
/
first-boot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/sh
# SPDX-License-Identifier: MIT
if [ -e "$(dirname "$0")"/functions.sh ]; then
. "$(dirname "$0")"/functions.sh
else
. /usr/share/asahi-scripts/functions.sh
fi
mount_sys_esp /efi
root_dev=$(findmnt -n -o SOURCE /)
efi_dev=$(findmnt -n -o SOURCE /efi)
if [ -e "$root_dev" ]; then
echo "Resizing root filesystem ..."
resize2fs "$root_dev"
echo "Randomizing root filesystem UUID..."
tune2fs -U random "$root_dev"
# root_uuid="$(blkid -c /dev/null "$root_dev" -o export | grep '^UUID=')"
# Use eval to evaluate the output of blkid:
eval "$(blkid -c /dev/null "$root_dev" -o export)"
root_uuid="$UUID"
echo "Root filesystem: $root_uuid"
echo
fi
if [ -e "$efi_dev" ] && \
blkid "$efi_dev" | grep -q 'TYPE="vfat"'; then
echo "Randomizing EFI system partition UUID..."
# Ugly... why isn't there a command to do this?
# Yes of course, fatlabel(8) actually provides such feature, but the UUID
# should be privided instead of generating it automatically.
# Generate a 32-bit long UUID
efi_uuid=$(dd if=/dev/random bs=4 count=1 status=none | hexdump -e '"%04x"')
# Set the above as the new volume ID for the EFI partiton
fatlabel -i "$efi_dev" "$efi_uuid"
eval "$(blkid -c /dev/null "$efi_dev" -o export)"
efi_uuid="$UUID"
echo "EFI partition: $efi_uuid"
echo
fi
echo "Determining the size of the swapfile ..."
# Swapfile size should be a multiple of page size, because it should.
# And we already have systems running in different page sizes, e.g. Apple
# M1 Macs which runs a kernel compiled for 16KiB page size.
# The file size can be not dividable by page size, and it does not cause
# the file to malfunction, besides wasting a few kiB of space.
# We just simply round it to 1MiB, as it is faster to allocate using dd,
# and it is a multiple of both 4KiB and 16KiB, or 64KiB.
mem_kb=$(grep -- MemTotal /proc/meminfo | awk '{ print $2 }')
# Avoid using bash's builtin test as integer overflow can occur.
# Make sure we process the numbers using bc.
# Modern bash does use the 64-bit integer, but it is signed. It means that
# it will cease functioning if memory is larger than 8EiB - 1, which will
# never going to happen in the forseeable future.
mem_less_than_1gb=$(echo "$mem_kb <= 1048576" | bc)
if [ "x$mem_less_than_1gb" == "x1" ] ; then
# If less than 1GB, the swapfile would be double the amount of RAM.
# We round it up to a multiple of the page size.
swapsize=$(echo "scale=0;m=$mem_kb;p=1024;p*((m+p-1)/p)/1024" | bc)
else
# What aoscdk-rs does is:
# swap = mem + sqrt(mem)
# It will be too large if memory itself is large enough.
swapsize=$(echo "scale=0;m=$mem_kb;p=1024;a=m+sqrt(m);p*((a+p-1)/p)/1024" | bc)
fi
echo "The size of the swap file will be $swapsize MiB."
echo "Checking if the root partition is large enough..."
# Generate a script for bc to process, e.g. "69801*4096/1048576"
# The reason for *4096 is that the block size is 4KiB. The block size is
# formatted using %S. Available blocks is formatted using %a.
# Avoid parsing the output of df(1).
rootavail=$(stat -f -c '%a*%S/1048576' / | bc)
insufficient_space=$(echo "$swapsize >= $rootavail")
if [ "x$insufficient_space" == "x1" ] ; then
echo "Not enough space to allocate a swapfile with this size. Skipping."
else
echo "Allocating swapfile ..."
dd if=/dev/zero of=/swapfile bs=1MiB count=$swapsize
chmod 000 /swapfile
mkswap /swapfile
echo "Enabling swapfile..."
swapon /swapfile
fi
if [ ! -z "$root_uuid" ] && [ ! -z "$efi_uuid" ]; then
echo "Regenerating /etc/fstab..."
genfstab -p -U / > /etc/fstab
fi
grub-install --efi-directory=/efi --bootloader-id="AOSC OS" --removable
update-grub
# FIXME: For some reason, without running update-grub twice, the sysroot UUID
# does not updated according with the new randomised UUID.
update-grub
echo "All done, self-destroying ..."
rm -v /usr/lib/systemd/system/multi-user.target.wants/first-boot.service
rm -v /usr/lib/systemd/system/first-boot.service
rm -v "$0"
echo "Rebooting ..."
sync
reboot -f