-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgentoo-install.sh
executable file
·433 lines (318 loc) · 12.7 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/bin/bash -e
# Copyright (c) 2012, Michael Mol <[email protected]>
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
################################################################################
# #
# Configuration Variables #
# #
################################################################################
# The script assumes the /boot and /home partitions already exist, are
# formatted, and may be mounted by specifying the filesystems' UUID.
# It also presumes that there's an HTTP proxy server available.
# This is all very rough at the moment, and it's strictly a "works for my
# systems, on mt network" thing. Hopefully things improve and are generalized.
# Patches very welcome.
#Mirror for portage snapshot and stage3 tarball
MIRROR=http://lug.mtu.edu/gentoo/
#stage 3 relative path
STAGE_PATH=releases/amd64/autobuilds/current-stage3-amd64/
#portage snapshot relative path
PORTAGE_PATH=snapshots/
#Stage3 tarball
STAGE_BALL=stage3-amd64-20180715T214502Z.tar.xz
#Portage snapshot tarball
PORTAGE_SNAPSHOT=portage-latest.tar.xz
#Root filesystem device
ROOTDEV=/dev/sda4
FS_BOOT_UUID=3c2398d1-c84a-425d-b35b-63841188ff01
FS_SWAP_UUID=cf048d96-2455-4dbd-bda1-5a0931897a6f
#assigned later
FS_ROOT_UUID=""
ETC_CONFD_HOSTNAME="saffron"
ETC_TIMEZONE="America/Detroit"
KERNEL_SOURCES="sys-kernel/gentoo-sources"
PROFILE='desktop/plasma/systemd (stable)'
ETC_CONFD_NET_FILE_CONTENT=$(cat <<'EOF'
config_eth0="dhcp"
EOF
)
#make.conf
SYS_CPU_TGT="3"
MAKE_CONF=$(cat <<EOF
CFLAGS="-O2 -pipe -march=native -ggdb"
CXXFLAGS="\${CFLAGS}"
MAKEOPTS="--jobs=${SYS_CPU_TGT}"
EMERGE_DEFAULT_OPTS="--jobs=${SYS_CPU_TGT} --verbose --tree --keep-going --with-bdeps=y"
FEATURES="splitdebug"
LINGUAS="en"
USE="mmx sse sse2 sse3 ssse3 posix nptl smp avahi curl ipv6 acpi dbus hddtemp libnotify lm_sensors pam readline syslog udev unicode usb -gnome -oss -static"
GENTOO_MIRRORS="http://chi-10g-1-mirror.fastsoft.net/pub/linux/gentoo/gentoo-distfiles/ http://mirrors.cs.wmich.edu/gentoo http://gentoo.mirrors.tds.net/gentoo"
VIDEO_CARDS="intel"
INPUT_DEVICES="evdev"
ALSA_CARDS=""
#PKGDIR="/mnt/r5/pkgdir"
#PORTAGE_TMPDIR="/mnt/r5/portage_tmp"
CHOST="x86_64-pc-linux-gnu"
EOF
)
logger "Gentoo install: Grabbing release and portage tarballs"
STAGEFILEPATH="$MIRROR$STAGE_PATH$STAGE_BALL"
if [ ! -f $STAGE_BALL ]; then
wget "$STAGEFILEPATH"
fi
unset STAGEFILEPATH
PORTAGEFILEPATH="$MIRROR$PORTAGE_PATH$PORTAGE_SNAPSHOT"
if [ ! -f $PORTAGE_SNAPSHOT ]; then
wget "$PORTAGEFILEPATH"
fi
unset PORTAGEFILEPATH
unset ROOTPATH
logger "Gentoo install: Creating the filesystem"
#Create the filesystem
mkfs.ext4 -F -E discard -O sparse_super2 "$ROOTDEV"
logger "Gentoo install: Extracting the root filesystem's UUID."
FS_ROOT_UUID=$(tune2fs -l "$ROOTDEV"|grep "Filesystem UUID"|cut -f2 -d:|sed -e 's/ \+//')
logger "Gentoo install: Mounting the filesystem"
# mount the root filesystem. We're going to play fast and loose with integrity,
# but it'll be OK as long as things don't crash before the script finishes. And
# if they do, we just run the script again.
mount "$ROOTDEV" -o nobarrier,noatime,discard,max_batch_time=100000,data=writeback /mnt/gentoo
# Here, we deviate from the handbook; we'll mount /boot once we're chrooted.
# Instead, we go ahead and unpack our tarballs.
logger "Gentoo Install: Unpacking the stage tarball"
tar xpf "$STAGE_BALL" -C /mnt/gentoo
logger "Gentoo install: Unpacking the portage snapshot."
tar xpf "$PORTAGE_SNAPSHOT" -C /mnt/gentoo/usr
# Another deviation. Rather than assemble make.conf the Handbook way, we'll
# use a lump make.conf I already use.
logger "Gentoo install: Unpacking make.conf."
echo "$MAKE_CONF" > /mnt/gentoo/etc/portage/make.conf
logger "Gentoo install: Writing timezone configuration"
echo "$ETC_TIMEZONE" > /mnt/gentoo/etc/timezone
cp "/mnt/gentoo/usr/share/zoneinfo/$ETC_TIMEZONE" /mnt/gentoo/etc/localtime
logger "Gentoo install: Copying autodiscovered DNS details"
cp -L /etc/resolv.conf /mnt/gentoo/etc/resolv.conf
logger "Gentoo install: Mounting dev, proc, etc in target environment"
mount -t proc none /mnt/gentoo/proc
mount --rbind /dev /mnt/gentoo/dev/
if [ ! mountpoint -q /mnt/gentoo/dev/shm ] ; then
mount -t tmpfs none -o rw,nosuid,nodev,mode=1777 /dev/shm
fi
if [ ! mountpoint -q /mnt/gentoo/dev/pts ] ; then
mount -t devpts none -o rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 /mnt/gentoo/dev/pts
fi
# And that's everything we do *outside* the chroot.
# we still want automation inside the chroot. So we build a second script to
# run in there.
INNER_SCRIPT=$(cat <<INNERSCRIPT
env-update
source /etc/profile
export PS1="(autochroot) \$PS1" # Not that the user will see this.
# Is there any reason the handbook specifies anything but emerges to be done
# _after_ the chroot?
script_fail() {
logger "Gentoo install: Failing out"
umount -l /dev
umount -l /proc
exit 1
}
script_check_fail() {
if [ \$? -ne 0 ]; then
script_fail;
else
echo "Gentoo install: Cmd Succeeded"
fi
}
script_em_sync() {
logger "Syncing portage"
emerge --sync
script_check_fail
}
script_env_update() {
logger "Gentoo install: Updating environment"
env-update
script_check_fail
logger "Gentoo install: sourcing environment"
source /etc/profile
}
script_write_fstab() {
logger "Gentoo install: Writing fstab"
# Clear out what's already there, first.
echo "" > /etc/fstab
echo "UUID=$FS_BOOT_UUID\t/boot\text4\tdefaults,noatime,discard\t1\t2" >> /etc/fstab
echo "UUID=$FS_SWAP_UUID\tnone\tswap\tdefaults,discard\t0\t0" >> /etc/fstab
echo "UUID=$FS_ROOT_UUID\t/\text4\tnoatime,discard\t0\t1" >> /etc/fstab
echo "/dev/cdrom\t/mnt/cdrom\tauto\tuser,noauto\t0\t0" >> /etc/fstab
}
script_conf_hostname() {
logger "Gentoo install: setting hostname"
# Set the system hostname
echo "hostname=\"$ETC_CONFD_HOSTNAME\"" > /etc/conf.d/hostname
}
script_conf_net() {
logger "Configuring network"
# Write the etc/conf.d/net file.
echo "$ETC_CONFD_NET_FILE_CONTENT" > /etc/conf.d/net
}
script_conf_locale_gen_write() {
logger "Writing and generating locales"
# Clear out initial file.
echo '' > /etc/locales.gen
echo "en_US ISO-8859-1" >> /etc/locales.gen
echo "en_US.UTF-8 UTF-8" >> /etc/locales.gen
}
script_conf_locales_select() {
logger "Configuring environment locales"
echo '' > /etc/env.d/02locale
echo 'LANG="en_US.UTF-8"' >> /etc/env.d/02locale
echo 'LC_COLLATE="C"' >> /etc/env.d/02locale
}
script_conf_locales() {
script_conf_locale_gen_write
locale-gen
script_check_fail
script_conf_locales_select
script_env_update
}
script_emerge_post() {
logger "Gentoo install: beginning script_emerge_post."
# It's possible for some critical stuff to have been changed, and these
# commands _should_ be harmless if run when not needed. And since this
# is a largely unattended script, this shouldn't be wasting much time.
logger "Gentoo install: hash -r"
hash -r
script_env_update
# These commands _may_ be harmful if run at the wrong time...but I've
# tried to order them in a minimal-risk fashion.
hash perl-cleaner 2> /dev/null
if [ \$? -eq 0 ]; then
logger "Gentoo install: perl updater"
perl-cleaner --reallyall
fi
logger "Gentoo install: revdep-rebuild"
revdep-rebuild
# Yes, there's a risk of an infinite recursive loop here.
script_emerge_retry
# Update configuration files. This part might not be unattended...
dispatch-conf
}
script_emerge_retry() {
# Keep trying until we've got it!
SER=0
while test \$? -ne 0; do
logger "Gentoo install: emerge failed. Retry."
emerge --resume
SER=1
done
logger "Gentoo install: emerge succeeded. Continuing"
if [ \$SER -ne 0 ]; then
# Don't let our SER interfere with deeper SERs.
# We're done with it, anyhow.
unset SER
# So nice, we do it twice.
script_emerge_post
script_emerge_post
# Really, though, since it may trigger emerges, we may need to clean up
# after it.
else
unset SER
fi
}
script_emerge_portage_update() {
logger "Gentoo install: Updating portage"
emerge --update --deep --newuse sys-apps/portage
script_emerge_retry
}
script_emerge_update_world() {
logger "Gentoo install: updating @world"
emerge --update --deep --newuse @world
script_emerge_retry
}
script_emerge_rebuild_world() {
# Rebuild the whole thing with our latest compiler, binutils...
logger "Gentoo install: rebuilding world"
emerge -e @world
script_emerge_retry
}
script_emerge() {
logger "Gentoo install: emerging \$*"
emerge \$*
script_emerge_retry
}
# We need to finish the base configuration. After that, we can go on and try
# and update.
# We're going to skip over configuring and installing grub and the kernel. I'm
# assuming this has already been done, and that grub and the built kernel both
# comfortably reside under /boot. Why? Because doing so has saved me a ton of
# time on my own setup, this week.
# Write out configuration items.
script_write_fstab
# Real quick, enable swap.
swapon -a
script_conf_hostname
script_conf_net
script_conf_locales
script_em_sync
# We need these for post-emerge steps, but they're normally pulled in as
# dependencies of other things. For whatever reason, they're not in
# the stage 3 tarball. We'll oneshot-install them so we have them. Later,
# they'll either be scooped up as dependencies of other packages, or
# they'll be cleaned up as part of an emerge --depclean. Either way, we're
# not putting them in our world file.
logger "Gentoo install: One-shotting perl-cleaner"
emerge -1 app-admin/perl-cleaner
# We need this for revdep-rebuild, which we'll want _immediately_ after
# updating portage.
script_emerge app-portage/gentoolkit
script_emerge_portage_update
logger "Gentoo install: Installing kernel-sources"
emerge $KERNEL_SOURCES
script_emerge_update_world
# Now let's set the profile we want
profile_no=\$(eselect profile list | grep '$PROFILE' | tail -n1 | cut -d'[' -f2 | cut -d']' -f1)
eselect profile set \$profile_no
# Since we're rebasing the system with new CFLAGS, and _then_ rebuilding
# the _entire_ system twice to make sure we got everything, it's almost
# certainly best to go ahead and do this before we install any more packages.
# So nice, we do it twice.
script_emerge_rebuild_world
script_emerge_rebuild_world
# Really, though, we do it twice to pick up any two-step stragglers.
# OK, now on to the necessary system tools.
script_emerge app-admin/syslog-ng sys-process/vixie-cron net-misc/openssh net-misc/dhcpcd sys-apps/mlocate
rc-update add syslog-ng default
rc-update add vixie-cron default
rc-update add sshd default
# And not-so-necessary-but-oh-so-nice tools.
script_emerge app-portage/genlop sys-process/htop app-portage/eix
echo "SUCCESS!"
INNERSCRIPT
)
echo "Preparing chroot script"
# Write the script.
echo "$INNER_SCRIPT" > /mnt/gentoo/chroot_inner_script.sh
echo "Running chroot script"
# and run it. Wish us luck!
chroot /mnt/gentoo/ /bin/bash /chroot_inner_script.sh "$FS_ROOT_UUID" "$FS_BOOT_UUID" "$FS_SWAP_UUID" "$FS_HOME_UUID" "$ETC_CONFD_HOSTNAME" "$ETC_CONFD_NET_FILE_CONTENT" "$http_proxy" "$KERNEL_SOURCES"
if [ $? -ne 0 ]; then
echo "chroot install script failed. Read output, collect logs, submit bugs..."
echo "Which nobody bothered to do for six years. I guess we're bug free!"
fi