-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathbuild_rootfs.sh
189 lines (148 loc) · 5.78 KB
/
build_rootfs.sh
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
help(){
echo "Usage:
build_rootfs.sh <rootfs_path> <shim> <board_recovery_image> [options]
build_rootfs.sh -h | --help
Options:
--no-xfce Skip installing xfce and related packages
--no-kill-frecon Don't kill frecon during boot"
}
die() {
echo -e "\x1b[31m${1}\x1b[0m" >&2
exit 1
}
die_help() {
echo -e "\x1b[31m${1}\x1b[0m" >&2
help
exit 1
}
has_arg(){
#example: has_arg "--help" "$@"
check=$1
shift
for arg in "$@"; do
if [ $arg == $check ]; then
return 0
fi
done
return 1
}
if has_arg "--help" "$@" || has_arg "-h" "$@"; then
help
exit 0
fi
if [ ${EUID} -ne 0 ]; then
die "this script must be run as root!"
fi
if [ $# -le 0 ]; then
die_help "you must pass an output rootfs path"
fi
if [ $# -le 1 ]; then
die_help "you must pass an input RMA shim"
fi
if [ $# -le 2 ]; then
die_help "you must pass an input board recovery image"
fi
if ! which pacstrap >/dev/null 2>/dev/null; then
die "this program requires pacstrap from arch-install-scripts"
fi
if ! which arch-chroot >/dev/null 2>/dev/null; then
die "this program requires arch-chroot from arch-install-scripts"
fi
if ! which lsof >/dev/null 2>/dev/null; then
die "this program requires lsof"
fi
if ! which xargs >/dev/null 2>/dev/null; then
die "this program requires xargs"
fi
if ! which wget >/dev/null 2>/dev/null; then
die "this program requires wget"
fi
if test ! -f "${2}"; then
die "${2}: No such file"
fi
# fix for people not removing the old rootfs before re-creating it
rm -rf "${1}"
mkdir "${1}"
if test -d mnt; then rm -r mnt; fi
PACKAGES="base networkmanager pulseaudio pavucontrol alsa-utils mesa-amber which sudo vim neofetch base-devel cloud-utils util-linux"
if ! has_arg "--no-xfce" "$@"; then
PACKAGES="${PACKAGES} network-manager-applet xfce4 xfce4-goodies lightdm-gtk-greeter firefox noto-fonts"
fi
pacstrap -McK "${1}" $PACKAGES || die "failed to bootstrap rootfs"
wget -O- https://archlinux.org/mirrorlist/all/ | sed "s/^#//" | tee "${1}/etc/pacman.d/mirrorlist" || die "failed to get mirrorlist"
cp *.pkg.tar.zst "${1}/" || die "failed to copy packages to root"
mount --bind "${1}" "${1}" || die "failed to bindmount root"
arch-chroot "${1}" pacman --noconfirm -Rdd systemd systemd-libs systemd-sysvcompat || die "failed to remove systemd"
arch-chroot "${1}" bash -c 'pacman --noconfirm -U *.pkg.tar.zst' || die "failed to install packages"
arch-chroot "${1}" bash -c 'rm *.pkg.tar.zst' || die "failed to remove packages"
arch-chroot "${1}" bash -c 'rm /var/cache/pacman/pkg/*' || die "failed to remove package cache"
arch-chroot "${1}" useradd -m terraos || die "failed to add terraos user"
arch-chroot "${1}" bash -c 'echo -e "terraos\nterraos" | passwd terraos' || die "failed to set terraos user password"
arch-chroot "${1}" bash -c "echo 'terraos ALL=(ALL:ALL) NOPASSWD:ALL' >> /etc/sudoers" || die "failed to add terraos to sudoers"
arch-chroot "${1}" bash -c "ln -sf /usr/share/zoneinfo/America/Los_Angeles /etc/localtime" || die "failed to set time"
sed -i "s/#en_US.UTF-8/en_US.UTF-8/" "${1}/etc/locale.gen" || die "failed to set locale"
arch-chroot "${1}" locale-gen || die "failed to generate locale files"
echo "LANG=en_US.UTF-8" > "${1}/etc/locale.conf"
echo "terraos" > "${1}/etc/hostname"
cat <<"EOT" > "${1}/usr/local/bin/expand-root.sh"
set -xe
PART=$(findmnt -no SOURCE /)
DEV=$(lsblk -npo PKNAME ${PART})
echo "${DEV} ${PART} ${PART#${DEV}}"
echo "w" | fdisk ${DEV}
growpart ${DEV} ${PART#${DEV}}
resize2fs ${PART}
EOT
cat <<EOT > "${1}/etc/systemd/system/expand-root.service"
[Unit]
Description=Resize root on first boot
ConditionPathExists=!/usr/local/bin/expand-root.completed
[Service]
Type=simple
ExecStart=/usr/bin/bash /usr/local/bin/expand-root.sh
ExecStartPost=/usr/bin/touch /usr/local/bin/expand-root.completed
[Install]
WantedBy=basic.target
EOT
arch-chroot "${1}" systemctl enable expand-root || die "failed to enable expand-root service"
if ! has_arg "--no-kill-frecon" "$@"; then
cat <<EOT > "${1}/etc/systemd/system/kill-frecon.service"
[Unit]
Description=Tell frecon to kill itself
[Service]
Type=simple
ExecStart=/usr/bin/killall frecon-lite
[Install]
WantedBy=basic.target
EOT
arch-chroot "${1}" systemctl enable kill-frecon || die "failed to enable kill-frecon service"
fi
if ! has_arg "--no-xfce" "$@"; then
arch-chroot "${1}" systemctl enable lightdm || die "failed to enable lightdm service"
fi
arch-chroot "${1}" systemctl enable NetworkManager || die "failed to enable services"
lsof -t +D "${1}" 2>/dev/null | xargs kill -9
rm -rf "${1}"/etc/pacman.d/gnupg/S.*
umount -R "${1}" || die "failed to unmount root bindmount"
SHIM_DEV=$(losetup -Pf --show "${2}")
mkdir mnt || die "failed to create temporary mountpoint"
mount "${SHIM_DEV}p3" -o ro mnt || die "failed to mount shim"
cp -a mnt/lib/modules "${1}/lib/" || die "failed to copy modules"
cp -a mnt/lib/firmware "${1}/lib/" || die "failed to copy firmware from shim"
sync
umount -l mnt || die "failed to unmount shim"
losetup -d ${SHIM_DEV} || die "failed to remove shim loop device"
RECO_DEV=$(losetup -Pf --show "${3}")
mount "${RECO_DEV}p3" -o ro mnt || die "failed to mount recovery image"
cp -a mnt/etc/modprobe.d/alsa* "${1}/etc/modprobe.d/" || die "failed to copy alsa drivers"
sync
umount -l mnt || die "failed to unmount recovery image"
git clone https://chromium.googlesource.com/chromiumos/third_party/linux-firmware fw --depth=1 -b master || die "failed to clone firmware"
cp -r fw/* "${1}/lib/firmware/" || die "failed to copy firmware"
rm -r fw || die "failed to remove firmware"
losetup -d ${RECO_DEV} || die "failed to remove recovery image loop device"
rm -r mnt || die "failed to remove temporary mountpoint"
# it may already exist
mkdir -p "${1}/etc/modules-load.d" || true
echo "iwlmvm" >> "${1}/etc/modules-load.d/dedede-wifi.conf"
echo "ccm" >> "${1}/etc/modules-load.d/dedede-wifi.conf"