-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgentoo-install.sh
executable file
·279 lines (238 loc) · 8.71 KB
/
gentoo-install.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#! /bin/bash
CONFIG_FILE="inc.config.sh"
if [ "`whoami`" != "root" ]; then
echo Must be run as root!
exit 1
fi
if ! grep -qs /mnt/gentoo /proc/mounts; then
echo "Target /mnt/gentoo not mounted!"
exit 1
fi
no_gpg_validation=""
if [ "$1" == "--no-gpg-validation" ]; then
no_gpg_validation="1"
shift
fi
if [ "$1" != "" ]; then
CONFIG_FILE="$1"
fi
if [ ! -f "$CONFIG_FILE" ]; then
echo "Configuration file $CONFIG_FILE does not exist!"
exit 1
fi
. "$CONFIG_FILE"
# Some configuration checks
if [ "$USE_KERNEL_CONFIG" != "" ] && [ ! -f "$USE_KERNEL_CONFIG" ]; then
echo "Kernel configuration file $USE_KERNEL_CONFIG not found!"
exit 1
fi
if [ "$TARGET_HOSTNAME" == "" ]; then
echo "Hostname in configuration blank, cannot continue!"
exit 1
fi
if [ "$ROOT_PASSWORD" == "" ] && [ "$SUDO_WHEEL_ALL" != "1" ]; then
echo "Either root password or SUDO_WHEEL_ALL=1 must be specified!"
exit 1
fi
MACHINE="`uname -m`"
case $MACHINE in
i686) GENTOO_ARCH=x86 ; GENTOO_SUBARCH=i686-openrc ;;
x86_64) GENTOO_ARCH=amd64 ; GENTOO_SUBARCH=amd64-openrc ;;
*)
echo "Unknown / unsupported machine type $MACHINE!"
exit 1
;;
esac
echo === Installing Gentoo GNU/Linux for $GENTOO_ARCH
if command -v chronyd > /dev/null; then
echo -- Setting the date and time
chronyd -q
elif command -v ntpd > /dev/null; then
echo -- Setting the date and time
timeout 60s ntpd -q -g
fi
if [ "$GENTOO_MIRROR" == "" ]; then
echo --- Choosing Gentoo mirror for stage download
GENTOO_MIRROR="$(mirrorselect --ftp --deep --output --quiet | grep -Eo "ftp://[^\"]+")"
fi
cd /mnt/gentoo
rm -f stage3-*.tar*
if [ "$LOCAL_STAGE3" != "" ] && [ -f "$LOCAL_STAGE3" ]; then
echo --- Using local stage3 copy
cp "$LOCAL_STAGE3" /mnt/gentoo/
else
echo --- Downloading stage3
while true; do
# Specify 10 sec. timeout to faster things up, not all Gentoo mirrors host an FTP server.
wget --connect-timeout=10 "$GENTOO_MIRROR/releases/$GENTOO_ARCH/autobuilds/current-stage3-$GENTOO_SUBARCH/stage3-$GENTOO_SUBARCH-????????T??????Z.tar*" && break
done
if [ ! -f stage3-$GENTOO_SUBARCH-????????T??????Z.tar.bz2 ] && [ ! -f stage3-$GENTOO_SUBARCH-????????T??????Z.tar.xz ]; then
echo "Download failed"
exit 1
fi
if [ "$no_gpg_validation" != "1" ]; then
echo --- Verifying and validating
wget -O - https://qa-reports.gentoo.org/output/service-keys.gpg | gpg --import
gpg --verify stage3-$GENTOO_SUBARCH-????????T??????Z.tar*.asc || exit 1
gpg --verify stage3-$GENTOO_SUBARCH-????????T??????Z.tar*.DIGESTS || exit 1
gpg --verify stage3-$GENTOO_SUBARCH-????????T??????Z.tar*.sha256 || exit 1
fi
for hashalgo in blake2b512 sha512; do
if ! grep -qs $(openssl dgst -$hashalgo stage3-$GENTOO_SUBARCH-????????T??????Z.tar.{bz2,xz} 2> /dev/null | grep -Eo "[0-9a-z]{128,}") stage3-$GENTOO_SUBARCH-????????T??????Z.tar*.DIGESTS; then
echo "stage3 $hashalgo checksum mismatch"
exit 1
fi
done
if ! sha256sum --check stage3-$GENTOO_SUBARCH-????????T??????Z.tar*.sha256; then
echo "stage3 sha256 checksum mismatch"
exit 1
fi
fi
echo --- Unpacking the stage tarball
if [ -f stage3-*.tar.xz ]; then
tar xpf stage3-*.tar.xz --xattrs-include='*.*' --numeric-owner || exit 1
elif [ -f stage3-*.tar.bz2 ]; then
tar xpf stage3-*.tar.bz2 --xattrs-include='*.*' --numeric-owner || exit 1
else
echo "FATAL: Unknown stage3 tarball filename!"
exit 1
fi
if dmesg | grep -qs Xen; then
echo --- Creating Xen device nodes
mknod -m 600 /mnt/gentoo/dev/hvc0 c 229 0
# Create xvd* device nodes for all /dev/[sh]d[ab] devices, others not supported yet
mknod -m 660 /mnt/gentoo/dev/xvda b 202 0
mknod -m 660 /mnt/gentoo/dev/xvdb b 202 16
for i in `seq 1 15`; do
mknod -m 660 /mnt/gentoo/dev/xvda$i b 202 $i
mknod -m 660 /mnt/gentoo/dev/xvdb$i b 202 $((i + 16))
done
fi
echo --- Configuring the compile options
make_conf="/mnt/gentoo/etc/portage/make.conf"
cp $make_conf ${make_conf}.dist
if [ "$COMMON_FLAGS" != "" ]; then
sed -i "s/COMMON_FLAGS=.*/COMMON_FLAGS=\"$COMMON_FLAGS\"/" $make_conf
fi
if [ "$CFLAGS" != "" ]; then
sed -i "s/CFLAGS=.*/CFLAGS=\"$CFLAGS\"/" $make_conf
fi
if [ "$CXXFLAGS" != "" ]; then
sed -i "s/CXXFLAGS=.*/CXXFLAGS=\"$CXXFLAGS\"/" $make_conf
fi
if grep -qs "USE=.*" $make_conf; then
sed -i "s/USE=.*/USE=\"$USE\"/" $make_conf
else
echo -e "\nUSE=\"$USE\"" >> $make_conf
fi
if [ "$GENTOO_ARCH" == "amd64" ] || [ "$GENTOO_ARCH" == "x86" ]; then
if [ "$CPU_FLAGS_X86" != "" ]; then
echo "CPU_FLAGS_X86=\"$CPU_FLAGS_X86\"" >> $make_conf
fi
fi
num_cores="`nproc`"
mem_gigs="$(( `grep MemTotal /proc/meminfo | sed 's/\s\+/\t/g' | cut -f 2` / 1024 / 1024 ))"
if [ "$mem_gigs" == "0" ]; then
echo "MAKEOPTS=\"-j1\"" >> $make_conf
elif [ "$num_cores" -lt "$mem_gigs" ]; then
echo "MAKEOPTS=\"-j$num_cores\"" >> $make_conf
else
echo "MAKEOPTS=\"-j$mem_gigs\"" >> $make_conf
fi
if [ "$http_proxy" != "" ]; then
echo "http_proxy=\"$http_proxy\"" >> $make_conf
fi
if [ "$FEATURES" != "" ]; then
echo "FEATURES=\"$FEATURES\"" >> $make_conf
fi
if [ "$INPUT_DEVICES" != "" ]; then
echo "INPUT_DEVICES=\"$INPUT_DEVICES\"" >> $make_conf
fi
if [ "$VIDEO_CARDS" != "" ]; then
echo "VIDEO_CARDS=\"$VIDEO_CARDS\"" >> $make_conf
fi
if [ "$LINGUAS" != "" ]; then
echo "LINGUAS=\"$LINGUAS\"" >> $make_conf
fi
if [ "$L10N" != "" ]; then
echo "L10N=\"$L10N\"" >> $make_conf
fi
if [ "$ACCEPT_LICENSE" != "" ]; then
echo "ACCEPT_LICENSE=\"$ACCEPT_LICENSE\"" >> $make_conf
fi
if [ "$EMERGE_DEFAULT_OPTS" != "" ]; then
echo "EMERGE_DEFAULT_OPTS=\"$EMERGE_DEFAULT_OPTS\"" >> $make_conf
fi
if [ "$PORTAGE_ELOG_MAILURI" != "" ]; then
echo "PORTAGE_ELOG_MAILURI=\"$PORTAGE_ELOG_MAILURI\"" >> $make_conf
fi
# Package specific make.conf options
if [ "$LIBREOFFICE_EXTENSIONS" != "" ]; then
echo "LIBREOFFICE_EXTENSIONS=\"$LIBREOFFICE_EXTENSIONS\"" >> $make_conf
fi
if [ "$NGINX_MODULES_HTTP" != "" ]; then
echo "NGINX_MODULES_HTTP=\"$NGINX_MODULES_HTTP\"" >> $make_conf
fi
if [ "$NGINX_MODULES_MAIL" != "" ]; then
echo "NGINX_MODULES_MAIL=\"$NGINX_MODULES_MAIL\"" >> $make_conf
fi
if [ "$QEMU_SOFTMMU_TARGETS" != "" ]; then
echo "QEMU_SOFTMMU_TARGETS=\"$QEMU_SOFTMMU_TARGETS\"" >> $make_conf
fi
if [ "$QEMU_USER_TARGETS" != "" ]; then
echo "QEMU_USER_TARGETS=\"$QEMU_USER_TARGETS\"" >> $make_conf
fi
echo --- Checking for DHCP
touch /mnt/gentoo/use_dhcpcd.txt
if pgrep dhcpcd > /dev/null; then
echo "1" > /mnt/gentoo/use_dhcpcd.txt
fi
echo --- Checking for WPA
touch /mnt/gentoo/use_wpa.txt
if pgrep wpa_supplicant > /dev/null; then
echo "1" > /mnt/gentoo/use_wpa.txt
mkdir -p /mnt/gentoo/etc/wpa_supplicant/
cp -L /etc/wpa_supplicant/wpa_supplicant.conf /mnt/gentoo/etc/wpa_supplicant/wpa_supplicant.conf
fi
echo --- Checking for system type
dmidecode -s system-product-name > /mnt/gentoo/system-product-name.txt
lspci | grep Virtio > /mnt/gentoo/has-virtio.txt
if [ "$RC_LOCAL_SCRIPTS" != "" ]; then
echo --- Copying local.d scripts
while read locald_script; do
if [ "$locald_script" != "" ]; then
echo "------ $locald_script"
cp "$locald_script" /mnt/gentoo/etc/local.d/
fi
done <<< "$RC_LOCAL_SCRIPTS"
chmod +x /mnt/gentoo/etc/local.d/*.start
chmod +x /mnt/gentoo/etc/local.d/*.stop
echo "rc_verbose=yes" > /mnt/gentoo/etc/conf.d/local
fi
echo --- Chrooting
mkdir -p /mnt/gentoo/etc/portage/repos.conf
cp /mnt/gentoo/usr/share/portage/config/repos.conf /mnt/gentoo/etc/portage/repos.conf/gentoo.conf
cp -L /etc/resolv.conf /mnt/gentoo/etc/
cd - > /dev/null
cp "$CONFIG_FILE" /mnt/gentoo/etc/inc.config.sh
# we remove first three lines which contains error message and exit, as guard
# against direct launching of chroot-part script.
tail -n+4 chroot-part.sh > /mnt/gentoo/chroot-part.sh
if [ "$USE_KERNEL_CONFIG" != "" ]; then
cp "$USE_KERNEL_CONFIG" /mnt/gentoo/usr/src/use_kernel_config || exit 1
fi
grep "\s/mnt/gentoo" /proc/mounts > /mnt/gentoo/mounts.txt
mkdir -p /mnt/gentoo/usr/src/initramfs/{bin,dev,etc,lib,lib64,mnt/root,proc,root,sbin,sys}
cp initramfs-init.sh /mnt/gentoo/usr/src/initramfs/init
mount --types proc proc /mnt/gentoo/proc
mount --rbind /sys /mnt/gentoo/sys
mount --make-rslave /mnt/gentoo/sys
mount --rbind /dev /mnt/gentoo/dev
mount --make-rslave /mnt/gentoo/dev
mount --bind /run /mnt/gentoo/run
mount --make-rslave /mnt/gentoo/run
# Be sure to always umount, if user presses Ctrl+C.
# Two traps are needed to guard against executing commands twice.
trap "umount -l /mnt/gentoo/dev; umount -l /mnt/gentoo/sys; umount -l /mnt/gentoo/proc" EXIT
trap "exit 1" SIGINT SIGTERM
chroot /mnt/gentoo /bin/bash /chroot-part.sh