2020-11-29 21:39:44 -06:00
|
|
|
#! /bin/bash
|
|
|
|
#
|
|
|
|
# gentoostrap
|
|
|
|
# Build a Gentoo system in /mnt
|
|
|
|
# Copyright (C) 2020 Vintage Salt <rehashedsalt@cock.li>
|
|
|
|
#
|
|
|
|
# Distributed under terms of the MIT license.
|
|
|
|
#
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# Read-only set-once variables
|
|
|
|
declare -r _name="$(basename -- "$0")"
|
|
|
|
# Options
|
|
|
|
declare -i _optchroot
|
|
|
|
declare _optdest="/mnt"
|
2020-12-02 19:05:41 -06:00
|
|
|
declare _optprofile="desktop"
|
2020-11-29 21:39:44 -06:00
|
|
|
declare -i _opthelp
|
|
|
|
declare -i _optverbose
|
|
|
|
# Working variables
|
|
|
|
declare -a _args
|
|
|
|
declare _return
|
|
|
|
|
|
|
|
# Helper functions
|
|
|
|
log() {
|
|
|
|
# Print a line to the terminal if _optverbose is greater than $2
|
|
|
|
# $2 defaults to 0
|
|
|
|
# loglevel 0: Daily-use messages
|
|
|
|
# loglevel 1: Detailed but not quite debugging
|
|
|
|
# loglevel 2: Definitely debugging
|
|
|
|
[ -z "$1" ] && return 1
|
|
|
|
if (( _optverbose >= ${2:-0} )); then
|
|
|
|
printf "%s\\n" "$1"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
warn() {
|
|
|
|
# Print a yellow line to the terminal, respecting _optverbose
|
|
|
|
[ -z "$1" ] && return 1
|
|
|
|
if (( _optverbose >= ${2:-0} )); then
|
|
|
|
if [ -t 1 ]; then
|
|
|
|
printf "\\e[33m%s\\e[0m\\n" "$1"
|
|
|
|
else
|
|
|
|
printf "WARN: %s\\n" "$1"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
error() {
|
|
|
|
# Print a red line to the terminal, exit if $2 is specified
|
|
|
|
[ -z "$1" ] && return 1
|
|
|
|
if [ -t 2 ]; then
|
|
|
|
printf "\\e[31m%s\\e[0m\\n" "$1" 1>&2
|
|
|
|
else
|
|
|
|
printf "ERROR: %s\\n" "$1" 1>&2
|
|
|
|
fi
|
|
|
|
[ -z "$2" ] && return
|
|
|
|
exit "${2:-1}"
|
|
|
|
}
|
|
|
|
has() {
|
|
|
|
# Parse out all arguments and try to find them in path
|
|
|
|
# If an argument cannot be found, set _return and fail
|
|
|
|
for prog in "$@"; do
|
|
|
|
if ! command -v "$prog" > /dev/null 2>&1; then
|
|
|
|
_return="$prog"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-11-29 21:43:52 -06:00
|
|
|
# Helper functions
|
|
|
|
cleanup() {
|
|
|
|
[ -z "$1" ] && warn "No argument passed to cleanup"
|
|
|
|
rm -rf "$1" || warn "Failed to remove $1: $?"
|
|
|
|
}
|
|
|
|
|
2020-11-29 21:39:44 -06:00
|
|
|
# Core program functions
|
|
|
|
printhelp() {
|
|
|
|
cat << EOF
|
|
|
|
Usage: $_name [OPTION]...
|
|
|
|
|
|
|
|
-h Print this help text
|
|
|
|
-c Perform the Gentoo half of installation. Must be invoked from a chroot
|
2020-12-04 01:58:47 -06:00
|
|
|
or a systemd-nspawn container
|
|
|
|
-d Perform the second Gentoo half of installation from a chroot
|
2020-11-29 21:39:44 -06:00
|
|
|
-v Print more status messages. Stacks
|
|
|
|
|
|
|
|
Copyright (c) 2020 rehashedsalt@cock.li
|
|
|
|
Licensed under the MIT license
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
build-gentoo() {
|
|
|
|
# Build Gentoo
|
|
|
|
|
|
|
|
# First, get a link to a stage3 tarball
|
|
|
|
stage3="http://distfiles.gentoo.org/releases/amd64/autobuilds/current-stage3-amd64-systemd/$(
|
|
|
|
curl -s http://distfiles.gentoo.org/releases/amd64/autobuilds/current-stage3-amd64-systemd/ \
|
|
|
|
| grep -oe 'stage3-amd64-systemd-20.*\.tar\.xz' \
|
|
|
|
| sed 's/^stage3.*">//g' \
|
|
|
|
| sort \
|
2020-11-29 22:06:29 -06:00
|
|
|
| uniq \
|
2020-11-29 21:39:44 -06:00
|
|
|
| head -n 1
|
|
|
|
)"
|
2020-12-02 19:16:17 -06:00
|
|
|
stage3file="/tmp/tmp.$USER.$_name.stage3.tar.xz"
|
2020-12-02 19:20:45 -06:00
|
|
|
stage3sig="/tmp/tmp.$USER.$_name.stage3.tar.xz.DIGESTS"
|
2020-12-02 19:16:17 -06:00
|
|
|
for file in "$stage3file" "$stage3sig"; do
|
|
|
|
touch "$file"
|
|
|
|
chmod 0600 "$file"
|
|
|
|
done
|
2020-12-02 19:20:45 -06:00
|
|
|
# We grab the checksums first to verify the integrity of any tarball leftover from a previous run
|
2020-12-03 04:46:42 -06:00
|
|
|
local shouldextract=1
|
|
|
|
[ -f "$_optdest/bin/bash" ] && unset shouldextract
|
2020-12-02 19:16:17 -06:00
|
|
|
log "Downloading stage3 signature"
|
2020-12-02 19:20:45 -06:00
|
|
|
curl -s "$stage3.DIGESTS" -o "$stage3sig"
|
2020-12-02 19:16:17 -06:00
|
|
|
if ! [ -f "$stage3file" ]; then
|
|
|
|
log "Downloading stage3"
|
|
|
|
curl -s "$stage3" -o "$stage3file"
|
2020-12-02 19:27:50 -06:00
|
|
|
elif ! grep "$stage3sig" -e "$(sha512sum "$stage3file" | awk '{print $1}')" > /dev/null 2>&1; then
|
2020-12-02 19:20:45 -06:00
|
|
|
log "Checksum verification failed; downloading new stage3"
|
2020-12-02 19:16:17 -06:00
|
|
|
curl -s "$stage3" -o "$stage3file"
|
|
|
|
fi
|
2020-12-02 19:27:50 -06:00
|
|
|
# At this point, we should have a new stage3 that matches our signature; die if not
|
|
|
|
if ! grep "$stage3sig" -e "$(sha512sum "$stage3file" | awk '{print $1}')" > /dev/null 2>&1; then
|
2020-12-02 19:20:45 -06:00
|
|
|
error "stage3 checksum verification failed" 50
|
2020-12-02 19:16:17 -06:00
|
|
|
fi
|
2020-12-03 04:46:42 -06:00
|
|
|
if [ -n "$shouldextract" ]; then
|
|
|
|
log "Decompressing tarball; this will prompt for root privileges"
|
|
|
|
sudo tar xf "$stage3file" -C "$_optdest"
|
|
|
|
fi
|
|
|
|
unset shouldextract
|
2020-11-29 21:39:44 -06:00
|
|
|
|
|
|
|
# Now we do some pre-chroot configuration
|
|
|
|
pushd "$_optdest" > /dev/null 2>&1
|
|
|
|
# These is a very generic make.conf to get the system up and running
|
|
|
|
# We use march of x86-64 to ensure compatibility if we're bootstrapping on a host other than the target
|
2020-12-04 01:49:17 -06:00
|
|
|
log "Building make.conf"
|
2020-11-29 21:39:44 -06:00
|
|
|
cat <<-EOF > "$_optdest/etc/portage/make.conf"
|
2020-12-02 19:31:02 -06:00
|
|
|
# ABI_X86 is used to control multilib support
|
|
|
|
ABI_X86="64 32"
|
2020-12-03 04:41:05 -06:00
|
|
|
ACCEPT_LICENSE="*"
|
2020-11-29 21:39:44 -06:00
|
|
|
COMMON_FLAGS="-march=x86-64 -mtune=generic -O2 -pipe"
|
|
|
|
CFLAGS="\${COMMON_FLAGS}"
|
|
|
|
CXXFLAGS="\${COMMON_FLAGS}"
|
2020-12-02 20:13:32 -06:00
|
|
|
FEATURES="-network-sandbox"
|
2020-11-29 21:39:44 -06:00
|
|
|
GENTOO_MIRRORS="http://distfiles.gentoo.org"
|
2020-12-03 22:48:56 -06:00
|
|
|
GRUB_PLATFORMS="emu efi-32 efi-64 pc"
|
2020-12-02 20:15:37 -06:00
|
|
|
MAKEOPTS="-j$(nproc)"
|
2020-12-02 21:00:00 -06:00
|
|
|
USE="networkmanager systemd -elogind -test"
|
2020-12-02 20:06:49 -06:00
|
|
|
EOF
|
|
|
|
# This is required on >=17.1 to unroll some circular dependencies
|
2020-12-04 01:49:17 -06:00
|
|
|
log "Setting package.use settings for gentoostrap"
|
2020-12-02 20:06:49 -06:00
|
|
|
cat <<-EOF > "$_optdest/etc/portage/package.use/gentoostrap"
|
2020-12-02 20:08:41 -06:00
|
|
|
# These solve circular dependency issues
|
2020-12-02 20:10:58 -06:00
|
|
|
dev-lang/python -bluetooth -sqlite
|
2020-12-02 21:00:00 -06:00
|
|
|
net-misc/networkmanager dhclient
|
2020-12-02 20:08:41 -06:00
|
|
|
sys-libs/ncurses -gpm
|
2020-11-29 21:39:44 -06:00
|
|
|
EOF
|
|
|
|
# Configure default ebuild repositories
|
2020-12-04 01:49:17 -06:00
|
|
|
log "Syncing ebuild repos"
|
2020-11-29 21:39:44 -06:00
|
|
|
mkdir -p "$_optdest/etc/portage/repos.conf"
|
|
|
|
cp "$_optdest/usr/share/portage/config/repos.conf" \
|
|
|
|
"$_optdest/etc/portage/repos.conf/gentoo.conf"
|
|
|
|
# DNS
|
2020-12-04 01:49:17 -06:00
|
|
|
log "Configuring DNS"
|
2020-11-29 21:39:44 -06:00
|
|
|
cat <<-EOF > "$_optdest/etc/resolv.conf"
|
|
|
|
nameserver 8.8.8.8
|
|
|
|
nameserver 8.8.4.4
|
|
|
|
EOF
|
2020-12-02 21:00:00 -06:00
|
|
|
# Hostname
|
2020-12-04 01:49:17 -06:00
|
|
|
read -p "Enter system hostname: " hostname
|
|
|
|
echo "$hostname" > "$_optdest/etc/conf.d/hostname"
|
2020-12-02 21:00:00 -06:00
|
|
|
# hosts
|
2020-12-04 01:49:17 -06:00
|
|
|
log "Configuring hosts"
|
|
|
|
cat <<-EOF > "$_optdest/etc/hosts"
|
|
|
|
127.0.0.1 $hostname
|
|
|
|
::1 $hostname
|
|
|
|
EOF
|
2020-11-29 22:06:29 -06:00
|
|
|
# Timezone
|
2020-12-04 01:49:17 -06:00
|
|
|
log "Configuring timezone"
|
2020-11-29 22:06:29 -06:00
|
|
|
cp /etc/timezone "$_optdest/etc/timezone"
|
|
|
|
# Locale
|
2020-12-04 01:49:17 -06:00
|
|
|
log "Configuring locale"
|
2020-11-29 22:06:29 -06:00
|
|
|
cat <<-EOF > "$_optdest/etc/locale.gen"
|
|
|
|
en_US ISO-8859-1
|
|
|
|
en_US.UTF-8 UTF-8
|
|
|
|
EOF
|
2020-12-02 21:00:00 -06:00
|
|
|
# fstab
|
2020-12-04 01:49:17 -06:00
|
|
|
log "Dropping to editor to configure fstab"
|
2020-12-02 21:01:27 -06:00
|
|
|
cat <<-EOF > "$_optdest/etc/fstab"
|
2020-12-02 21:00:00 -06:00
|
|
|
# fstab - Filesystem table
|
|
|
|
# See fstab(5) for syntax and configuration information
|
|
|
|
# Example:
|
|
|
|
#LABEL=gentoo / ext4 defaults,noatime,errors=remount-ro 0 1
|
|
|
|
EOF
|
|
|
|
"${EDITOR:-vim}" "$_optdest/etc/fstab"
|
2020-11-29 21:39:44 -06:00
|
|
|
|
|
|
|
# Now we need to pivot into a chroot and finish configuration natively
|
|
|
|
# Buuuut... since we're using systemd, we get to just spawn a systemd-nspawn container
|
|
|
|
popd
|
2020-12-04 01:49:17 -06:00
|
|
|
log "Pivoting to container for more setup"
|
2020-11-29 21:51:29 -06:00
|
|
|
cp "$0" "$_optdest/gentoostrap.sh"
|
|
|
|
sudo systemd-nspawn --directory="$_optdest" /gentoostrap.sh -c
|
2020-12-04 01:45:01 -06:00
|
|
|
|
|
|
|
# The nspawn container sadly cannot manipulate the environment entirely, so we need to now do an ACTUAL chroot to install GRUB
|
2020-12-04 01:58:47 -06:00
|
|
|
mount --types proc /proc "$_optdest/proc"
|
|
|
|
mount --rbind /sys "$_optdest/sys"
|
|
|
|
mount --make-rslave "$_optdest/sys"
|
|
|
|
mount --rbind /dev "$_optdest/dev"
|
|
|
|
mount --make-rslave "$_optdest/dev"
|
|
|
|
chroot "$_optdest" /gentoostrap.sh -d
|
|
|
|
log "Configuration complete! You should be good to reboot now."
|
2020-11-29 21:39:44 -06:00
|
|
|
}
|
|
|
|
build-gentoo-chroot() {
|
|
|
|
# Build Gentoo, but from within the chroot environment
|
2020-11-29 22:06:29 -06:00
|
|
|
log "Building Gentoo from within chroot"
|
|
|
|
|
|
|
|
# Sync repositories
|
|
|
|
log "Getting ebuild repositories"
|
2020-12-04 02:13:35 -06:00
|
|
|
if ! grep /etc/portage/repos.conf/gentoo.conf -ie 'git'; then
|
|
|
|
# Only do this rsync if we're actually rsyncing
|
|
|
|
# As part of installation, we switch to git, so this prevents us from hammering upstream, too
|
|
|
|
emerge-webrsync
|
|
|
|
fi
|
2020-11-29 22:06:29 -06:00
|
|
|
|
|
|
|
# Get the stable plasma/systemd profile and select it
|
|
|
|
profile=$(
|
|
|
|
eselect profile list \
|
|
|
|
| grep -ie 'stable' \
|
2020-12-02 19:46:26 -06:00
|
|
|
| grep -ie "$_optprofile" \
|
2020-11-29 22:06:29 -06:00
|
|
|
| head -n 1 \
|
|
|
|
| grep -oP '\[\K[^\]]+'
|
2020-12-02 19:05:41 -06:00
|
|
|
) || error "Error finding profile: $profile" 51
|
|
|
|
[ -z "$profile" ] && error "Could not find profile: $profile" 52
|
2020-11-29 22:06:29 -06:00
|
|
|
log "Setting profile $profile"
|
|
|
|
eselect profile set "$profile"
|
|
|
|
|
|
|
|
# BEHOLD!
|
2020-12-04 01:52:29 -06:00
|
|
|
emerge -DNUu @world --jobs --quiet-build y --with-bdeps y
|
2020-11-29 22:06:29 -06:00
|
|
|
# Configure timezone
|
|
|
|
emerge --config sys-libs/timezone-data
|
|
|
|
# Locales
|
|
|
|
locale-gen
|
|
|
|
eselect locale set en_US.utf8
|
|
|
|
env-update
|
|
|
|
. /etc/profile
|
|
|
|
|
2020-12-02 21:00:00 -06:00
|
|
|
# Extra packages
|
|
|
|
emerge --jobs --quiet-build y \
|
2020-12-04 01:45:01 -06:00
|
|
|
app-admin/sudo \
|
|
|
|
app-editors/vim \
|
2020-12-04 02:09:30 -06:00
|
|
|
dev-vcs/git \
|
2020-12-04 01:58:47 -06:00
|
|
|
sys-boot/grub:2 \
|
2020-12-02 21:00:00 -06:00
|
|
|
sys-fs/{e2fs,xfs}progs \
|
|
|
|
sys-process/cronie
|
|
|
|
|
2020-12-04 01:45:01 -06:00
|
|
|
# Configure sudoers
|
|
|
|
cat <<-EOF >> "/etc/sudoers"
|
|
|
|
%wheel ALL=(ALL) ALL
|
|
|
|
ansible ALL=(ALL) NOPASSWD:ALL
|
|
|
|
EOF
|
|
|
|
|
2020-12-02 21:00:00 -06:00
|
|
|
# Unnghhh... COLONEL...
|
|
|
|
# Dude distribution kernels are awesome
|
|
|
|
emerge --jobs --quiet-build y sys-kernel/installkernel-gentoo sys-kernel/linux-firmware
|
|
|
|
emerge --jobs --quiet-build y sys-kernel/gentoo-kernel
|
|
|
|
# Rebuilding modules shouldn't be necessary since the kernel was our last step
|
|
|
|
#emerge --jobs --quiet-build y @module-rebuild
|
|
|
|
|
2020-12-04 02:09:30 -06:00
|
|
|
# Reconfigure repo to clone over git from here on out
|
|
|
|
log "Configuring gentoo repo"
|
|
|
|
cat <<-EOF > "/etc/portage/repos.conf/gentoo.conf"
|
|
|
|
[DEFAULT]
|
|
|
|
main-repo = gentoo
|
|
|
|
|
|
|
|
[gentoo]
|
|
|
|
location = /usr/portage
|
|
|
|
#sync-depth = 1
|
|
|
|
sync-type = git
|
|
|
|
sync-uri = https://github.com/gentoo-mirror/gentoo.git
|
|
|
|
sync-git-verify-commit-signature = true
|
|
|
|
auto-sync = yes
|
|
|
|
EOF
|
|
|
|
|
2020-12-02 21:00:00 -06:00
|
|
|
# Install the bootloader
|
|
|
|
mkdir -p /boot/efi
|
|
|
|
chmod 0700 /boot/efi
|
|
|
|
|
2020-12-04 01:45:01 -06:00
|
|
|
# Configure a new user, add him to sudoers
|
2020-12-04 01:45:31 -06:00
|
|
|
if ! id salt > /dev/null 2>&1; then
|
|
|
|
useradd salt -m -G wheel -s /bin/bash -u 1000 -U
|
|
|
|
passwd salt
|
|
|
|
fi
|
2020-12-04 01:45:01 -06:00
|
|
|
|
2020-12-02 21:00:00 -06:00
|
|
|
# We've made it this far and now the user has to do just a liiiiiitle bit of configuration
|
|
|
|
clear
|
|
|
|
cat <<-EOF
|
|
|
|
Initial system configuration is complete. There are now just a few tasks left:
|
|
|
|
* Mount the ESP under /boot/efi
|
2020-12-04 01:58:47 -06:00
|
|
|
* Make sure everything looks good before we install GRUB
|
2020-12-02 21:00:00 -06:00
|
|
|
|
|
|
|
Dumping to a shell now. Have fun!
|
|
|
|
EOF
|
|
|
|
exec bash
|
2020-11-29 21:39:44 -06:00
|
|
|
}
|
2020-12-04 01:58:47 -06:00
|
|
|
build-gentoo-chroot2() {
|
|
|
|
# Last few chroot steps, mostly involving GRUB
|
|
|
|
log "Configuring GRUB"
|
|
|
|
grub-mkconfig -o /boot/grub
|
|
|
|
log "Installing GRUB"
|
|
|
|
grub-install
|
|
|
|
cat <<-EOF
|
|
|
|
EOF
|
|
|
|
}
|
2020-11-29 21:39:44 -06:00
|
|
|
|
|
|
|
# Main
|
|
|
|
main() {
|
|
|
|
# Parse out arguments
|
|
|
|
while [ -n "$1" ]; do
|
|
|
|
# Parse out flags
|
|
|
|
while getopts ":chv" opt; do
|
|
|
|
case $opt in
|
2020-12-04 01:58:47 -06:00
|
|
|
d)
|
|
|
|
_optchroot2=1
|
|
|
|
;;
|
2020-11-29 21:39:44 -06:00
|
|
|
c)
|
|
|
|
_optchroot=1
|
|
|
|
;;
|
|
|
|
h)
|
|
|
|
_opthelp=1
|
|
|
|
;;
|
|
|
|
v)
|
|
|
|
_optverbose+=1
|
|
|
|
;;
|
|
|
|
:)
|
|
|
|
error "Option requires argument: -$OPTARG" 2
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
error "Invalid option: -$OPTARG" 2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
# Store arguments
|
|
|
|
shift $((OPTIND - 1))
|
|
|
|
if [ -n "$1" ]; then
|
|
|
|
_args+=("$1")
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
unset OPTIND
|
|
|
|
done
|
|
|
|
# Early hook for help
|
|
|
|
[ -n "$_opthelp" ] && printhelp && exit 0
|
|
|
|
# Validate critical options
|
|
|
|
# TODO: That
|
|
|
|
# Validate core program dependencies
|
|
|
|
log "Validating dependencies" 2
|
2020-12-04 01:58:47 -06:00
|
|
|
if ! has basename chroot curl sha512sum systemd-nspawn tar; then
|
2020-11-29 21:39:44 -06:00
|
|
|
error "Failed to find program: $_return" 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Do the do
|
2020-12-04 01:58:47 -06:00
|
|
|
if [ -n "$_optchroot2" ]; then
|
|
|
|
build-gentoo-chroot2
|
|
|
|
elif [ -n "$_optchroot" ]; then
|
2020-11-29 21:39:44 -06:00
|
|
|
build-gentoo-chroot
|
2020-12-04 01:58:47 -06:00
|
|
|
else
|
|
|
|
build-gentoo
|
2020-11-29 21:39:44 -06:00
|
|
|
fi
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
main "$@"
|
|
|
|
|