2020-01-06 19:34:44 -01:00
|
|
|
#!/bin/bash
|
2020-04-21 12:33:42 +00:00
|
|
|
set -o pipefail
|
2020-01-06 19:34:44 -01:00
|
|
|
|
2019-12-31 14:28:42 -01:00
|
|
|
################################################
|
|
|
|
# Initialize script environment
|
|
|
|
|
|
|
|
# Find the directory this script is stored in. (from: http://stackoverflow.com/questions/59895)
|
|
|
|
get_source_dir() {
|
|
|
|
local source="${BASH_SOURCE[0]}"
|
2020-04-20 22:40:54 +00:00
|
|
|
while [[ -h $source ]]
|
2019-12-31 14:28:42 -01:00
|
|
|
do
|
|
|
|
local tmp="$(cd -P "$(dirname "${source}")" && pwd)"
|
|
|
|
source="$(readlink "${source}")"
|
2020-04-20 22:40:54 +00:00
|
|
|
[[ $source != /* ]] && source="${tmp}/${source}"
|
2019-12-31 14:28:42 -01:00
|
|
|
done
|
|
|
|
|
|
|
|
echo -n "$(realpath "$(dirname "${source}")")"
|
|
|
|
}
|
|
|
|
|
2020-01-08 16:21:01 -01:00
|
|
|
export GENTOO_INSTALL_REPO_DIR_ORIGINAL="$(dirname "$(get_source_dir)")"
|
|
|
|
export GENTOO_INSTALL_REPO_DIR="$GENTOO_INSTALL_REPO_DIR_ORIGINAL"
|
|
|
|
export GENTOO_INSTALL_REPO_SCRIPT_ACTIVE=true
|
|
|
|
export GENTOO_INSTALL_REPO_SCRIPT_PID=$$
|
2019-12-31 14:28:42 -01:00
|
|
|
|
|
|
|
umask 0077
|
|
|
|
|
2020-01-08 16:21:01 -01:00
|
|
|
source "$GENTOO_INSTALL_REPO_DIR/scripts/utils.sh"
|
|
|
|
source "$GENTOO_INSTALL_REPO_DIR/scripts/config.sh"
|
|
|
|
source "$GENTOO_INSTALL_REPO_DIR/scripts/functions.sh"
|
2019-12-31 14:28:42 -01:00
|
|
|
|
2020-01-05 22:35:15 -01:00
|
|
|
[[ $I_HAVE_READ_AND_EDITED_THE_CONFIG_PROPERLY == true ]] \
|
|
|
|
|| die "You have not properly read the config. Set I_HAVE_READ_AND_EDITED_THE_CONFIG_PROPERLY=true to continue."
|
|
|
|
|
2020-04-21 16:59:49 +00:00
|
|
|
preprocess_config
|
|
|
|
|
2020-01-05 22:35:15 -01:00
|
|
|
mkdir_or_die 0755 "$TMP_DIR"
|
2019-12-31 14:28:42 -01:00
|
|
|
[[ $EUID == 0 ]] \
|
|
|
|
|| die "Must be root"
|
|
|
|
|
|
|
|
|
|
|
|
################################################
|
|
|
|
# Functions
|
|
|
|
|
2020-01-02 21:28:16 -01:00
|
|
|
install_stage3() {
|
2019-12-31 14:28:42 -01:00
|
|
|
[[ $# == 0 ]] || die "Too many arguments"
|
|
|
|
|
|
|
|
prepare_installation_environment
|
2020-04-21 12:33:42 +00:00
|
|
|
apply_disk_configuration
|
2019-12-31 14:28:42 -01:00
|
|
|
download_stage3
|
|
|
|
extract_stage3
|
|
|
|
}
|
|
|
|
|
2020-01-13 20:56:16 -01:00
|
|
|
configure_base_system() {
|
2020-01-06 14:13:42 -01:00
|
|
|
# Set hostname
|
|
|
|
einfo "Selecting hostname"
|
|
|
|
sed -i "/hostname=/c\\hostname=\"$HOSTNAME\"" /etc/conf.d/hostname \
|
|
|
|
|| die "Could not sed replace in /etc/conf.d/hostname"
|
|
|
|
|
2020-01-03 21:48:49 -01:00
|
|
|
# Set timezone
|
|
|
|
einfo "Selecting timezone"
|
|
|
|
echo "$TIMEZONE" > /etc/timezone \
|
|
|
|
|| die "Could not write /etc/timezone"
|
2020-01-04 10:55:31 -01:00
|
|
|
try emerge -v --config sys-libs/timezone-data
|
2020-01-03 21:48:49 -01:00
|
|
|
|
2020-01-06 14:13:42 -01:00
|
|
|
# Set keymap
|
|
|
|
einfo "Selecting keymap"
|
|
|
|
sed -i "/keymap=/c\\keymap=\"$KEYMAP\"" /etc/conf.d/keymaps \
|
|
|
|
|| die "Could not sed replace in /etc/conf.d/keymaps"
|
|
|
|
|
2020-01-03 21:48:49 -01:00
|
|
|
# Set locale
|
|
|
|
einfo "Selecting locale"
|
|
|
|
echo "$LOCALES" > /etc/locale.gen \
|
|
|
|
|| die "Could not write /etc/locale.gen"
|
|
|
|
locale-gen \
|
|
|
|
|| die "Could not generate locales"
|
2020-01-04 10:55:31 -01:00
|
|
|
try eselect locale set "$LOCALE"
|
2020-01-03 21:48:49 -01:00
|
|
|
|
|
|
|
# Update environment
|
|
|
|
env_update
|
2020-01-13 20:56:16 -01:00
|
|
|
}
|
2019-12-31 14:28:42 -01:00
|
|
|
|
2020-01-13 20:56:16 -01:00
|
|
|
install_sshd() {
|
|
|
|
einfo "Installing sshd"
|
|
|
|
install -m0600 -o root -g root "$GENTOO_INSTALL_REPO_DIR/configs/sshd_config" /etc/ssh/sshd_config \
|
|
|
|
|| die "Could not install /etc/ssh/sshd_config"
|
|
|
|
rc-update add sshd default \
|
|
|
|
|| die "Could not add sshd to default services"
|
|
|
|
groupadd -r sshusers \
|
|
|
|
|| die "Could not create group 'sshusers'"
|
|
|
|
}
|
2020-01-04 10:55:31 -01:00
|
|
|
|
2020-04-21 18:48:23 +00:00
|
|
|
install_kernel_efi() {
|
|
|
|
try emerge --verbose sys-boot/efibootmgr
|
2020-01-05 22:35:15 -01:00
|
|
|
|
|
|
|
# Copy kernel to EFI
|
|
|
|
local kernel_version
|
2020-04-21 18:48:23 +00:00
|
|
|
kernel_version="$(find "/boot" -name "vmlinuz-*" -printf '%f\n' | sort -V | tail -n 1)" \
|
2020-01-05 22:35:15 -01:00
|
|
|
|| die "Could not list newest kernel file"
|
|
|
|
kernel_version="${kernel_version#vmlinuz-}" \
|
|
|
|
|| die "Could not find kernel version"
|
|
|
|
|
|
|
|
mkdir_or_die 0755 "/boot/efi/EFI"
|
|
|
|
cp "/boot/initramfs-$kernel_version"* "/boot/efi/EFI/initramfs.img" \
|
|
|
|
|| die "Could not copy initramfs to EFI partition"
|
|
|
|
cp "/boot/vmlinuz-$kernel_version"* "/boot/efi/EFI/vmlinuz.efi" \
|
|
|
|
|| die "Could not copy kernel to EFI partition"
|
|
|
|
|
|
|
|
# Create boot entry
|
|
|
|
einfo "Creating efi boot entry"
|
|
|
|
local linuxdev
|
2020-04-21 16:38:46 +00:00
|
|
|
linuxdev="$(get_device_by_partuuid "$PARTITION_UUID_ROOT")" \
|
|
|
|
|| die "Could not resolve partition UUID '$PARTITION_UUID_ROOT'"
|
2020-04-21 18:48:23 +00:00
|
|
|
local efipartdev
|
|
|
|
efipartdev="$(get_device_by_partuuid "$PARTITION_UUID_EFI")" \
|
2020-01-05 22:35:15 -01:00
|
|
|
|| die "Could not resolve partition UUID '$PARTITION_UUID_EFI'"
|
2020-04-21 18:48:23 +00:00
|
|
|
local efipartnum="${efipartdev: -1}"
|
|
|
|
local gptuuid="${DISK_PARTUUID_TO_GPT_UUID[$PARTITION_UUID_EFI]}"
|
|
|
|
local gptdev
|
2020-04-21 18:58:21 +00:00
|
|
|
gptdev="$(get_device_by_ptuuid "$gptuuid")" \
|
2020-04-21 18:48:23 +00:00
|
|
|
|| die "Could not resolve GPT UUID '$gptuuid'"
|
|
|
|
try efibootmgr --verbose --create --disk "$gptdev" --part "$efipartnum" --label "gentoo" --loader '\EFI\vmlinuz.efi' --unicode "root=$linuxdev initrd=\\EFI\\initramfs.img"
|
|
|
|
}
|
|
|
|
|
|
|
|
install_kernel_bios() {
|
|
|
|
try emerge --verbose sys-boot/syslinux
|
|
|
|
|
|
|
|
# Install syslinux MBR record
|
|
|
|
einfo "Copying syslinux MBR record"
|
2020-04-21 19:52:46 +00:00
|
|
|
local biosdev
|
|
|
|
biosdev="$(get_device_by_partuuid "$PARTITION_UUID_BIOS")" \
|
|
|
|
|| die "Could not resolve partition UUID '$PARTITION_UUID_BIOS'"
|
|
|
|
local gptuuid="${DISK_PARTUUID_TO_GPT_UUID[$PARTITION_UUID_BIOS]}"
|
2020-04-21 18:48:23 +00:00
|
|
|
local gptdev
|
2020-04-21 18:58:21 +00:00
|
|
|
gptdev="$(get_device_by_ptuuid "$gptuuid")" \
|
2020-04-21 18:48:23 +00:00
|
|
|
|| die "Could not resolve GPT UUID '$gptuuid'"
|
|
|
|
try dd bs=440 conv=notrunc count=1 if=/usr/share/syslinux/gptmbr.bin of="$gptdev"
|
|
|
|
|
|
|
|
# Install syslinux
|
|
|
|
einfo "Installing syslinux"
|
2020-04-21 19:52:46 +00:00
|
|
|
syslinux --install "$biosdev"
|
2020-04-21 18:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
install_kernel() {
|
|
|
|
# Install vanilla kernel
|
|
|
|
einfo "Installing binary vanilla kernel"
|
|
|
|
try emerge --verbose sys-kernel/vanilla-kernel-bin
|
|
|
|
|
|
|
|
if [[ $IS_EFI == "true" ]]; then
|
|
|
|
install_kernel_efi
|
|
|
|
else
|
|
|
|
install_kernel_bios
|
|
|
|
fi
|
2020-01-13 20:56:16 -01:00
|
|
|
}
|
|
|
|
|
|
|
|
install_ansible() {
|
|
|
|
einfo "Installing ansible"
|
|
|
|
try emerge --verbose app-admin/ansible
|
|
|
|
|
|
|
|
einfo "Creating ansible user"
|
|
|
|
useradd -r -d "$ANSIBLE_HOME" -s /bin/bash ansible \
|
|
|
|
|| die "Could not create user 'ansible'"
|
|
|
|
mkdir_or_die 0700 "$ANSIBLE_HOME"
|
|
|
|
mkdir_or_die 0700 "$ANSIBLE_HOME/.ssh"
|
|
|
|
|
2020-04-20 22:40:54 +00:00
|
|
|
if [[ -n $ANSIBLE_SSH_AUTHORIZED_KEYS ]]; then
|
2020-01-13 20:56:16 -01:00
|
|
|
einfo "Adding authorized keys for ansible"
|
|
|
|
touch_or_die 0600 "$ANSIBLE_HOME/.ssh/authorized_keys"
|
|
|
|
echo "$ANSIBLE_SSH_AUTHORIZED_KEYS" >> "$ANSIBLE_HOME/.ssh/authorized_keys" \
|
|
|
|
|| die "Could not add ssh key to authorized_keys"
|
|
|
|
fi
|
|
|
|
|
|
|
|
chown -R ansible: "$ANSIBLE_HOME" \
|
|
|
|
|| die "Could not change ownership of ansible home"
|
|
|
|
|
2020-01-17 16:15:12 -01:00
|
|
|
einfo "Adding ansible to some auxiliary groups"
|
|
|
|
usermod -a -G wheel,sshusers ansible \
|
|
|
|
|| die "Could not add ansible to auxiliary groups"
|
2020-01-13 20:56:16 -01:00
|
|
|
}
|
|
|
|
|
|
|
|
main_install_gentoo_in_chroot() {
|
|
|
|
[[ $# == 0 ]] || die "Too many arguments"
|
|
|
|
|
|
|
|
# Lock the root password, making the account unaccessible for the
|
|
|
|
# period of installation, except by chrooting
|
|
|
|
einfo "Locking root account"
|
|
|
|
passwd -l root \
|
|
|
|
|| die "Could not change root password"
|
|
|
|
|
2020-04-21 18:48:23 +00:00
|
|
|
if [[ $IS_EFI == "true" ]]; then
|
|
|
|
# Mount efi partition
|
|
|
|
mount_efivars
|
|
|
|
einfo "Mounting efi partition"
|
|
|
|
mount_by_partuuid "$PARTITION_UUID_EFI" "/boot/efi"
|
|
|
|
else
|
|
|
|
# Mount boot partition
|
|
|
|
einfo "Mounting boot partition"
|
2020-04-21 19:52:46 +00:00
|
|
|
mount_by_partuuid "$PARTITION_UUID_BIOS" "/boot"
|
2020-04-21 18:48:23 +00:00
|
|
|
fi
|
2020-01-13 20:56:16 -01:00
|
|
|
|
|
|
|
# Sync portage
|
|
|
|
einfo "Syncing portage tree"
|
|
|
|
try emerge-webrsync
|
|
|
|
|
|
|
|
# Configure basic system things like timezone, locale, ...
|
|
|
|
configure_base_system
|
|
|
|
|
|
|
|
# Prepare /etc/portage for autounmask
|
|
|
|
mkdir_or_die 0755 "/etc/portage/package.use"
|
|
|
|
touch_or_die 0644 "/etc/portage/package.use/zz-autounmask"
|
|
|
|
mkdir_or_die 0755 "/etc/portage/package.keywords"
|
|
|
|
touch_or_die 0644 "/etc/portage/package.keywords/zz-autounmask"
|
|
|
|
|
|
|
|
einfo "Temporarily installing mirrorselect"
|
|
|
|
try emerge --verbose --oneshot app-portage/mirrorselect
|
|
|
|
|
|
|
|
einfo "Selecting fastest portage mirrors"
|
|
|
|
try mirrorselect -s 4 -b 10 -D
|
|
|
|
|
|
|
|
einfo "Adding ~$GENTOO_ARCH to ACCEPT_KEYWORDS"
|
|
|
|
echo "ACCEPT_KEYWORDS=\"~$GENTOO_ARCH\"" >> /etc/portage/make.conf \
|
|
|
|
|| die "Could not modify /etc/portage/make.conf"
|
|
|
|
|
|
|
|
# Install git (for git portage overlays)
|
|
|
|
einfo "Installing git"
|
|
|
|
try emerge --verbose dev-vcs/git
|
|
|
|
|
|
|
|
install_kernel
|
2019-12-31 14:28:42 -01:00
|
|
|
|
2020-01-05 22:35:15 -01:00
|
|
|
# Generate a valid fstab file
|
|
|
|
einfo "Generating fstab"
|
2020-01-08 16:21:01 -01:00
|
|
|
install -m0644 -o root -g root "$GENTOO_INSTALL_REPO_DIR/configs/fstab" /etc/fstab \
|
2020-01-05 22:35:15 -01:00
|
|
|
|| die "Could not overwrite /etc/fstab"
|
2020-04-21 16:38:46 +00:00
|
|
|
echo "PARTUUID=$PARTITION_UUID_ROOT / ext4 defaults,noatime,errors=remount-ro,discard 0 1" >> /etc/fstab \
|
2020-01-05 22:35:15 -01:00
|
|
|
|| die "Could not append entry to fstab"
|
2020-04-21 18:48:23 +00:00
|
|
|
if [[ $IS_EFI == "true" ]]; then
|
|
|
|
echo "PARTUUID=$PARTITION_UUID_EFI /boot/efi vfat defaults,noatime,fmask=0022,dmask=0022,noexec,nodev,nosuid,discard 0 2" >> /etc/fstab \
|
|
|
|
|| die "Could not append entry to fstab"
|
|
|
|
else
|
2020-04-21 19:52:46 +00:00
|
|
|
echo "PARTUUID=$PARTITION_UUID_BIOS /boot vfat defaults,noatime,fmask=0022,dmask=0022,noexec,nodev,nosuid,discard 0 2" >> /etc/fstab \
|
2020-04-21 18:48:23 +00:00
|
|
|
|| die "Could not append entry to fstab"
|
|
|
|
fi
|
2020-04-21 16:38:46 +00:00
|
|
|
if [[ -v "PARTITION_UUID_SWAP" ]]; then
|
2020-01-05 22:35:15 -01:00
|
|
|
echo "PARTUUID=$PARTITION_UUID_SWAP none swap defaults,discard 0 0" >> /etc/fstab \
|
|
|
|
|| die "Could not append entry to fstab"
|
|
|
|
fi
|
|
|
|
|
2020-01-09 09:51:36 -01:00
|
|
|
# Install and enable dhcpcd
|
|
|
|
einfo "Installing gentoolkit"
|
|
|
|
try emerge --verbose app-portage/gentoolkit
|
|
|
|
|
2020-01-05 22:35:15 -01:00
|
|
|
# Install and enable sshd
|
2020-04-20 22:40:54 +00:00
|
|
|
if [[ $INSTALL_SSHD == true ]]; then
|
2020-01-13 20:56:16 -01:00
|
|
|
install_sshd
|
2020-01-06 19:34:44 -01:00
|
|
|
fi
|
2020-01-05 22:35:15 -01:00
|
|
|
|
|
|
|
# Install and enable dhcpcd
|
|
|
|
einfo "Installing dhcpcd"
|
2020-01-06 16:20:47 -01:00
|
|
|
try emerge --verbose net-misc/dhcpcd
|
2020-01-05 22:35:15 -01:00
|
|
|
rc-update add dhcpcd default \
|
|
|
|
|| die "Could not add dhcpcd to default services"
|
|
|
|
|
|
|
|
# Install ansible
|
2020-04-20 22:40:54 +00:00
|
|
|
if [[ $INSTALL_ANSIBLE == true ]]; then
|
2020-01-13 20:56:16 -01:00
|
|
|
install_ansible
|
2020-01-05 22:35:15 -01:00
|
|
|
fi
|
2019-12-31 14:28:42 -01:00
|
|
|
|
2020-01-06 19:34:44 -01:00
|
|
|
# Install additional packages, if any.
|
2020-04-20 22:40:54 +00:00
|
|
|
if [[ -n $ADDITIONAL_PACKAGES ]]; then
|
2020-01-06 19:34:44 -01:00
|
|
|
einfo "Installing additional packages"
|
2020-04-20 22:40:54 +00:00
|
|
|
# shellcheck disable=SC2086
|
2020-01-06 19:34:44 -01:00
|
|
|
try emerge --verbose --autounmask-continue=y -- $ADDITIONAL_PACKAGES
|
|
|
|
fi
|
|
|
|
|
2020-01-04 19:55:42 -01:00
|
|
|
if ask "Do you want to assign a root password now?"; then
|
2020-01-06 16:26:11 -01:00
|
|
|
try passwd root
|
2020-01-04 19:55:42 -01:00
|
|
|
einfo "Root password assigned"
|
|
|
|
else
|
2020-01-06 16:26:11 -01:00
|
|
|
try passwd -d root
|
2020-01-04 19:55:42 -01:00
|
|
|
ewarn "Root password cleared, set one as soon as possible!"
|
|
|
|
fi
|
|
|
|
|
|
|
|
einfo "Gentoo installation complete."
|
|
|
|
einfo "To chroot into the new system, simply execute the provided 'chroot' wrapper."
|
|
|
|
einfo "Otherwise, you may now reboot your system."
|
2019-12-31 14:28:42 -01:00
|
|
|
}
|
|
|
|
|
2020-01-02 21:28:16 -01:00
|
|
|
main_install() {
|
2019-12-31 14:28:42 -01:00
|
|
|
[[ $# == 0 ]] || die "Too many arguments"
|
|
|
|
|
2020-01-02 22:29:17 -01:00
|
|
|
gentoo_umount
|
2020-01-02 22:42:31 -01:00
|
|
|
install_stage3
|
2020-04-21 18:48:23 +00:00
|
|
|
|
|
|
|
[[ $IS_EFI == "true" ]] \
|
|
|
|
&& mount_efivars
|
2020-01-08 16:21:01 -01:00
|
|
|
gentoo_chroot "$GENTOO_INSTALL_REPO_BIND/scripts/main.sh" install_gentoo_in_chroot
|
2020-01-05 23:09:09 -01:00
|
|
|
gentoo_umount
|
2020-01-02 21:28:16 -01:00
|
|
|
}
|
|
|
|
|
|
|
|
main_chroot() {
|
2020-01-02 22:42:31 -01:00
|
|
|
gentoo_chroot "$@"
|
2020-01-02 21:28:16 -01:00
|
|
|
}
|
|
|
|
|
|
|
|
main_umount() {
|
|
|
|
gentoo_umount
|
2019-12-31 14:28:42 -01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
################################################
|
|
|
|
# Main dispatch
|
|
|
|
|
2020-01-02 22:29:17 -01:00
|
|
|
# Instantly kill when pressing ctrl-c
|
2020-01-08 16:21:01 -01:00
|
|
|
trap 'kill "$GENTOO_INSTALL_REPO_SCRIPT_PID"' INT
|
2020-01-02 22:29:17 -01:00
|
|
|
|
2019-12-31 14:28:42 -01:00
|
|
|
SCRIPT_ALIAS="$(basename "$0")"
|
2020-04-20 22:40:54 +00:00
|
|
|
if [[ $SCRIPT_ALIAS == main.sh ]]; then
|
2020-01-02 21:28:16 -01:00
|
|
|
SCRIPT_ALIAS="$1"
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
2019-12-31 14:28:42 -01:00
|
|
|
case "$SCRIPT_ALIAS" in
|
2020-01-02 21:28:16 -01:00
|
|
|
"chroot") main_chroot "$@" ;;
|
|
|
|
"install") main_install "$@" ;;
|
|
|
|
"install_gentoo_in_chroot") main_install_gentoo_in_chroot "$@" ;;
|
|
|
|
"umount") main_umount "$@" ;;
|
2019-12-31 14:28:42 -01:00
|
|
|
*) die "Invalid alias '$SCRIPT_ALIAS' was used to execute this script" ;;
|
|
|
|
esac
|