This script installs Gentoo for you. Yes I am making this and it takes hours help~
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

583 lines
14 KiB

#!/bin/bash
if [ "$(uname -m)" != "x86_64" ]; then
echo "You must use a arm64 system for this installer to work for sure. Also, it's 2021, fr?"
exit 1
fi
if ! dialog > /dev/null ; then
echo "This installer requires dialog installed. Are you on the live installer?"
exit 1
fi
# Terminal colors:
BLACK="\033[0;30m"
BLUE="\033[0;34m"
GREEN="\033[0;32m"
CYAN="\033[0;36m"
RED="\033[0;31m"
PURPLE="\033[0;35m"
BROWN="\033[0;33m"
BLUE="\033[0;34m"
GREEN="\033[0;32m"
CYAN="\033[0;36m"
RED="\033[0;31m"
PURPLE="\033[0;35m"
BROWN="\033[0;33m"
RESET="\033[0m"
DIALOG_BACKTITLE="xSlendiX's Gentoo installer"
DIALOG=${DIALOG=dialog --clear}
DIALOG_SIZE="20 61"
DIALOG_SIZE_BIG="24 80"
DIALOG_SIZE_S="7 60"
DIALOG_SIZE_TEXT="10 60"
EFI_INSTALL=0
NETWORK_INTERFACES=()
NETWORK_INTERFACES_DIALOG=()
WIFI_NETWORKS=()
WIFI_SETUP=0
DISK_INST="/dev/sda"
BOOT_PART="${DISK_INST}1"
ROOT_PART="${DISK_INST}2"
_CHROOT="chroot /mnt/gentoo /bin/bash"
SOFTWARE="app-admin/syslog-ng"
SERVICES="syslog-ng sshd"
CORES=4
setup_wifi_networks() {
clear
while true; do
$DIALOG --backtitle "$DIALOG_BACKTITLE" \
--title "Select wireless interface" \
--radiolist "Select the wireless interface you wish to use in your Gentoo system. If you do not wish to use WiFi, you can either press ESC or Cancel." \
$DIALOG_SIZE 5\
"${NETWORK_INTERFACES[@]}" 2> /tmp/choice_setup
response=$?
case $response in
0)
interface_selected="$(cat /tmp/choice_setup)"
if [[ -n "$interface_selected" ]]; then
WIFI_SETUP=1
break
fi
;;
*)
WIFI_SETUP=0
break
;;
esac
done
interface_selected="$(cat /tmp/choice_setup)"
if [[ -z "$interface_selected" ]]; then
WIFI_SETUP=0
fi
if grep -qi "enp" /tmp/choice_setup; then
WIFI_SETUP=0
$DIALOG --backtitle "$DIALOG_BACKTITLE" \
--title "!!!" \
--msgbox "Cancelled WiFi setup! Reason: ethernet interface selected." $DIALOG_SIZE_S
fi
for _ in once; do
if [[ "$WIFI_SETUP" -eq 1 ]]; then
while true; do
$DIALOG --backtitle "$DIALOG_BACKTITLE" \
--title "Country code" \
--inputbox "Enter your country code (lower-case). Example: us, ro, td, cz etc." \
$DIALOG_SIZE_TEXT 2> /tmp/choice_setup
status=$?
if [[ "$status" -ne 0 ]]; then
WIFI_SETUP=0
break
fi
country_code=$(cat /tmp/choice_setup)
chrlen=${#country_code}
if [[ "$chrlen" -ne 2 ]]; then
$DIALOG --backtitle "$DIALOG_BACKTITLE" \
--title "Uh oh!" \
--msgbox "Invalid country code! Try again." $DIALOG_SIZE_S
else
break
fi
done
if [[ "$WIFI_SETUP" -eq 0 ]]; then
break
fi
WIFI_NETWORKS=()
wifi_networks=()
update_networks=0
reset_shit=1
while true; do
if [[ "$reset_shit" -eq 1 ]]; then
cat > /tmp/wpa_supplicant.conf <<EOF
country=$(cat /tmp/choice_setup)
ctrl_interface=/var/run/wpa_supplicant
update_config=1
EOF
WIFI_NETWORKS=()
wifi_networks=()
reset_shit=0
fi
$DIALOG --backtitle "$DIALOG_BACKTITLE" \
--title "Add WiFi networks" \
--menu "Press Cancel or any wireless network added to apply changes. Press ESC to reset." $DIALOG_SIZE 6 \
"New" "Add new wifi network" \
"${wifi_networks[@]}" 2> /tmp/choice_setup
ret=$?
choice=$(cat /tmp/choice_setup)
case $ret in
0)
if [[ "$choice" == "New" ]]; then
ssid=""
password=""
exec 3>&1
# TODO: Implement
result=$($DIALOG --backtitle "$DIALOG_BACKTITLE" \
--title "Create new WiFi network" \
--form "Enter the information below. Currently, password cannot be empty and cannot contain spaces." 0 0 0 \
"SSID:" 1 1 "$ssid" 1 16 30 0 \
"Password:" 3 1 "$password" 3 16 30 0\
2>&1 1>&3)
exec 3>&-
if [[ "$?" -eq 0 ]]; then
ssid=$(echo $result | cut -d' ' -f1)
password=$(echo $result | cut -d' ' -f2)
wifi_networks+=("$ssid" "")
WIFI_NETWORKS+=("$ssid" "$password")
wpa_passphrase "$ssid" "$password" >> /tmp/wpa_supplicant.conf
fi
else
break
fi
;;
*)
reset_shit=1
break
;;
esac
done
fi
done
}
detect_hardware() {
clear
printf "$BLUE ::$RESET Detecting hardware...\n"
CORES=$(($(nproc) + 1))
for iface in $(ifconfig | cut -d ' ' -f1| tr ':' '\n' | awk NF); do
NETWORK_INTERFACES+=("$iface")
NETWORK_INTERFACES_DIALOG+=("$iface" "")
done
if ip l | grep -iq 'wlp\|wlan'; then
WIFI_AVAILABLE=1
echo "$PURPLE ::$RESET Detected WiFi adapter(s)\n"
else
WIFI_AVAILABLE=0
fi
}
confirmation() {
$DIALOG --backtitle "$DIALOG_BACKTITLE" \
--title "Confirmation" \
--yesno "Are you sure you want to continue?\nIf you do, $DISK_INST will be cleared!" $DIALOG_SIZE_S
response=$?
if [[ "$response" -ne 0 ]]; then
clear
exit 1
fi
}
ask_questions() {
$DIALOG --backtitle "$DIALOG_BACKTITLE" \
--title "Question" --yesno "Is this an EFI install?" $DIALOG_SIZE_S
response=$?
case $response in
0)
EFI_INSTALL=1
;;
*)
$DIALOG --backtitle "$DIALOG_BACKTITLE" \
--msgbox "EFI install is highly recommended, especially for support for disks bigger than 1TB." $DIALOG_SIZE_S
EFI_INSTALL=0
;;
esac
while true; do
$DIALOG --backtitle "$DIALOG_BACKTITLE" \
--title "Disk" \
--inputbox "On what disk do you wish to install Gentoo? (ex: /dev/sda)" $DIALOG_SIZE_TEXT 2> /tmp/choice_setup
response=$?
retval=$(cat /tmp/choice_setup)
case $response in
0)
if [[ -n "$retval" ]]; then
DISK_INST="$retval"
break
fi
;;
*)
;;
esac
done
if [[ "$WIFI_AVAILABLE" -eq 1 ]]; then
setup_wifi_networks
fi
areas=(Africa America Antarctica Arctic Asia Atlantic Australia Europe Indian Pacific)
while (IFS='|'; DIALOG ${area:+--default-item|"$area"} --title " Select area " --menu "$MENULABEL" 19 51 19 $(printf '%s||' "${areas[@]}")); do
area=$(cat $ANSWER)
read -a locations -d '\n' < <(find /usr/share/zoneinfo/$area -type f -printf '%P\n' | sort)
if (IFS='|'; $DIALOG --backtitle "$DIALOG_BACKTITLE" --title "Select location (${area})" --menu "$MENULABEL" 19 51 19 $(printf '%s||' "${locations[@]//_/ }")); then
location=$(tr ' ' '_' < $ANSWER)
TIMEZONE="$area/$location"
break
fi
done
while true; do
$DIALOG --backtitle "$DIALOG_BACKTITLE" \
--title "Hostname" \
--inputbox "What do you wish to be this machine's hostname?" $DIALOG_SIZE_TEXT 2> /tmp/choice_setup
response=$?
retval=$(cat /tmp/choice_setup)
case $response in
0)
if [[ -n "$retval" ]]; then
HOSTNAME="$retval"
break
fi
;;
*)
;;
esac
done
while true; do
$DIALOG --backtitle "$DIALOG_BACKTITLE" --insecure --passwordbox "Enter root password" $DIALOG_SIZE_TEXT 2> /tmp/choice_setup
retval=$?
ANSWER=$(cat /tmp/choice_setup)
if [[ "$retval" -ne 0 ]]; then
continue
fi
$DIALOG --backtitle "$DIALOG_BACKTITLE" --insecure --passwordbox "Enter root password again" $DIALOG_SIZE_TEXT 2> /tmp/choice_setup
retval=$?
ANSWER2=$(cat /tmp/choice_setup)
if [[ "$retval" -ne 0 ]]; then
continue
fi
if [[ "$ANSWER" = "$ANSWER2" ]]; then
ROOT_PASSWORD="$ANSWER"
break;
else
$DIALOG --backtitle "$DIALOG_BACKTITLE" \
--title "Oops!" \
--msgbox "Passwords do not match! Please try again." $DIALOG_SIZE_S
continue
fi
done
DIST_MIRROR=$(mirrorselect -i -o)
confirmation
}
empty_disk() {
cat > /tmp/temp_fdisk <<EOF
d
1
d
1
d
1
d
1
d
1
d
1
w
EOF
cat /tmp/temp_fdisk | fdisk ${DISK_INST} | \
$DIALOG --backtitle "$DIALOG_BACKTITLE" \
--title "Deleting all partitions on disk" \
--programbox $DIALOG_SIZE_BIG
}
prepare_disk() {
# empty_disk
# if [[ "$EFI_INSTALL" -eq 1 ]]; then
# printf "g\nn\n1\n2048\nt\n1\n1\nw" | fdisk ${DISK_INST} | \
# $DIALOG --backtitle "$DIALOG_BACKTITLE" \
# --title "Creating EFI boot partition" \
# --programbox $DIALOG_SIZE_BIG
# else
# printf "o\nn\n1\n2048\na\n1\nw" | fdisk ${DISK_INST} | \
# $DIALOG --backtitle "$DIALOG_BACKTITLE" \
# --title "Creating BIOS boot partition" \
# --programbox $DIALOG_SIZE_BIG
# fi
#
# printf "n\n2\n\nw\n" | fdisk ${DISK_INST} | \
# $DIALOG --backtitle "$DIALOG_BACKTITLE" \
# --title "Creating root partition" \
# --programbox $DIALOG_SIZE_BIG
clear
echo "Welp, this is awkward, you need to make the partitions yourself. Sorry."
read
cfdisk $DISK_INST
BOOT_PART="${DISK_INST}1"
ROOT_PART="${DISK_INST}2"
printf "${BLUE} ::$RESET Creating boot partition\n"
if [[ "EFI_INSTALL" -eq 1 ]]; then
mkfs.vfat -F32 $BOOT_PART
else
mkfs.ext4 $BOOT_PART
fi
printf "${BLUE} ::$RESET Creating root partition\n"
mkfs.ext4 $ROOT_PART
}
install_system() {
mount $ROOT_PART /mnt/gentoo
cd /mnt/gentoo
$DIALOG --backtitle "$DIALOG_BACKTITLE" \
--title "Stage3 download" \
--msgbox "Now you need to download the hardened stage3 tarball.\nSince I don't know how to detect mirror links, you have to download it yourself." $DIALOG_SIZE_S
links gentoo.org/downloads/mirrors
tar xpvf stage3-*.tar.xz --xattrs-include='*.*' --numeric-owner | dialog --backtitle "$DIALOG_BACKTITLE" \
--title "Extracting stage3 tarball" \
--no-ok --no-cancel \
--programbox "This can take a while so sit back and relax." 24 80
rm stage3-*.tar.xz
clear
cat > /mnt/gentoo/etc/portage/make.conf << EOF
CFLAGS="-march=native -O2 -pipe"
CXXFLAGS="${CFLAGS}"
LDFLAGS="-Wl,-O1 -Wl,--sort-common -Wl,--as-needed"
MAKEOPTS="--jobs=$CORES"
EMERGE_DEFAULT_OPTS="--jobs=$CORES"
USE="-gnome threads xinerama git subversion pulseaudio alsa alsa-lib glib -oss \
-selinux dbus openssl X -gtk -qt4 -qt5 -kde elogind -systemd \
-ios -ipod -emacs -xemacs -smartcard -quicktime -css -3dfx opengl config-file"
PORTAGE_COMPRESS="xz"
PORTAGE_COMPRESS_FLAGS="-9"
ACCEPT_LICENSE="*"
LC_MESSAGES=C
$DIST_MIRROR
EOF
printf "${BLUE} ::$RESET Copying DNS info\n"
cp -L /etc/resolv.conf /mnt/gentoo/etc/
printf "${BLUE} ::$RESET Preparing chroot\n"
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
printf "${BLUE} ::$RESET Reading all news\n"
echo 'eselect news read --quiet all' | $_CHROOT
printf "${BLUE} ::$RESET emerge-webrsync\n"
echo 'emerge-webrsync' | $_CHROOT
printf "${BLUE} ::$RESET emerge sync\n"
echo 'emerge --sync' | $_CHROOT
}
do_configuration() {
printf "${BLUE} ::$RESET Setting timezone\n"
echo $TIMEZONE > /mnt/gentoo/etc/timezone
echo 'emerge --config sys-libs/timezone-data' | $_CHROOT > /dev/null
printf "${BLUE} ::$RESET Setting locale\n"
cat > /mnt/gentoo/etc/locale.gen << EOF
en_US ISO-8859-1
en_US.UTF-8 UTF-8
EOF
echo 'locale-gen' | $_CHROOT
printf "${BLUE} ::$RESET Configuring fstab\n"
if [[ "$EFI_INSTALL" -eq 1 ]]; then
cat > /mnt/gentoo/etc/fstab << EOF
# <fs> <mountpoint> <type> <opts> <dump/pass>
/dev/sda1 /boot vfat noauto,noatime 0 2
EOF
else
cat > /mnt/gentoo/etc/fstab << EOF
# <fs> <mountpoint> <type> <opts> <dump/pass>
/dev/sda1 /boot ext4 noauto,noatime 0 2
EOF
fi
cat >> /mnt/gentoo/etc/fstab << EOF
/dev/sda2 / ext4 defaults,noatime 0 1
EOF
printf "${BLUE} ::$RESET Setting hostname\n"
cat > /mnt/gentoo/etc/conf.d/hostname << EOF
hostname="$HOSTNAME"
EOF
printf "${BLUE} ::$RESET Configuring network adapters\n"
ETH0=$(ifconfig | head -1 | awk '{print $1}' | sed 's/://')
cat > /mnt/gentoo/etc/conf.d/net << EOF
config_$ETH0="dhcp"
EOF
$_CHROOT << EOF
cd /etc/init.d
ln -s net.lo net.$ETH0
rc-update add net.$ETH0 default
EOF
if [[ "$WIFI_SETUP" -eq 1 ]]; then
printf "${BLUE} ::$RESET Setting up WiFi\n"
cp /tmp/wpa_supplicant.conf /mnt/gentoo/etc/wpa_supplicant/wpa_supplicant.conf
cat >> /mnt/gentoo/etc/conf.d/net << EOF
modules_${interface_selected}="wpa_supplicant"
config_${interface_selected}="dhcp"
EOF
cat > /mnt/gentoo/etc/conf.d/wpa_supplicant << EOF
wpa_supplicant_args="-B -i${interface_selected} -c/etc/wpa_supplicant/wpa_supplicant.conf"
EOF
chmod 600 /etc/wpa_supplicant/wpa_supplicant.conf
fi
printf "${BLUE} ::$RESET Setting root password\n"
$_CHROOT > /dev/null << EOF
passwd root
$ROOT_PASSWORD
$ROOT_PASSWORD
EOF
printf "$BLUE ::$RESET Downloading post_installer.sh"
$_CHROOT > /dev/null << EOF
cd
wget https://git.xslendi.xyz/xslendix/gentoo_installer/raw/branch/master/post_installer.sh
chmod +x post_installer
EOF
}
do_kernel() {
printf "${BLUE} ::$RESET Getting kernel, genkernel and grub\n"
echo "emerge sys-kernel/gentoo-sources sys-kernel/genkernel sys-boot/grub" | $_CHROOT
printf "${BLUE} ::$RESET Compiling kernel...\n"
$_CHROOT << EOF
cd /usr/src/linux
genkernel all --makeopts=-j${CORES} --compress-initramfs --zfs
EOF
if [[ "EFI_INSTALL" -eq 0 ]]; then
$_CHROOT << EOF
mount ${BOOT_PART} /boot
grub-install /dev/sda
grub-mkconfig -o /boot/grub/grub.cfg
EOF
else
$_CHROOT << EOF
mkdir -p /boot/efi
mount ${BOOT_PART} /boot/efi
mount -o remount,rw /boot/efi
grub-install --target=x86_64-efi --efi-directory=/boot/efi --removable
grub-mkconfig -o /boot/grub/grub.cfg
EOF
fi
}
do_software() {
printf "${BLUE} ::$RESET Installing software\n"
echo "emerge $SOFTWARE" | $_CHROOT
printf "$BLUE ::$RESET Enabling services\n"
for i in $SERVICES; do
echo "rc-update add $i default" | $_CHROOT
done
echo "emerge app-portage/gentoolkit" | $_CHROOT
echo "emerge -vUDN @world" | $_CHROOT
echo "emerge --depclean" | $_CHROOT
echo "revdep-rebuild" | $_CHROOT
echo "rm -rf /var/tmp/portage/*" | $_CHROOT
echo "rm -rf /usr/portage/distfiles/*" | $_CHROOT
}
finish() {
cd /
umount -l /mnt/gentoo/dev{/shm,/pts,} || true
umount -l /mnt/gentoo/proc || true
if [[ "EFI_INSTALL" -eq 1 ]]; then
umount -l /mnt/gentoo/boot/efi || true
else
umount -l /mnt/gentoo/boot|| true
fi
umount -l /mnt/gentoo || true
printf "$BLUE ::$RESET System installed. You should reboot\n"
}
detect_hardware
ask_questions
prepare_disk
install_system
do_configuration
do_kernel
do_software
finish