Skip to content

Instantly share code, notes, and snippets.

@quiloos39
Last active December 24, 2020 17:12
Show Gist options
  • Save quiloos39/9319ed27803eee41d0ad5901a4f23429 to your computer and use it in GitHub Desktop.
Save quiloos39/9319ed27803eee41d0ad5901a4f23429 to your computer and use it in GitHub Desktop.
#!/bin/bash
# MIT License
#
# Copyright (c) 2019, Necdet Efe
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
MIRROR="https://ftp.halifax.rwth-aachen.de/gentoo/"
STAGE_PATH="releases/amd64/autobuilds/current-stage3-amd64/"
STAGE_BALL="stage3-amd64-20190911T214502Z.tar.xz"
PORTAGE_PATH="snapshots/"
PORTAGE_SNAPSHOT="portage-latest.tar.xz"
PROFILE_SELECTION="default/linux/amd64/17.0"
LOCALE_SELECTION="en_US.utf8"
#MAKEOPTS="-j1"
echo "Looking for mounted devices."
for device in $(df | grep -E "sda|gentoo" | cut -d ' ' -f1); do
echo "Unmounting \"$device\"."
umount -l $device
done
echo "Formatting block device."
parted -a optimal /dev/sda <<EOF
mklabel gpt
unit mib
mkpart primary 1 3
name 1 grub
set 1 bios_grub on
mkpart primary 3 131
name 2 boot
mkpart primary 131 643
name 3 swap
mkpart primary 643 -1
name 4 rootfs
set 2 boot on
print
EOF
echo "Making file system."
yes | mkfs.ext2 -T small /dev/sda2
yes | mkfs.ext4 /dev/sda4
echo "Mounting \"/mnt/gentoo\"."
mount /dev/sda4 /mnt/gentoo
echo "Setting date."
ntpd -q -g
echo "Looking for stage ball."
if [ ! -f $STAGE_BALL ]; then
echo "Stage is not found, attempting to download it."
wget ${MIRROR}${STAGE_PATH}${STAGE_BALL}
fi
echo "Unpacking stage ball (might take while)."
tar xpf $STAGE_BALL -C /mnt/gentoo
echo "Looking for portage snapshot."
if [ ! -f $PORTAGE_SNAPSHOT ]; then
echo "Portage snapshot is not found, attempting to download it."
wget ${MIRROR}${PORTAGE_PATH}${PORTAGE_SNAPSHOT}
fi
echo "Unpacking portage snapshot (might take while)."
tar xpf $PORTAGE_SNAPSHOT -C /mnt/gentoo/usr
if [ -f "/mnt/gentoo/etc/portage/make.conf.copy" ]; then
rm "/mnt/gentoo/etc/portage/make.conf"
cp "/mnt/gentoo/etc/portage/make.conf.copy" "/mnt/gentoo/etc/portage/make.conf"
fi
cp "/mnt/gentoo/etc/portage/make.conf" "/mnt/gentoo/etc/portage/make.conf.copy"
#printf "MAKEOPTS=\"%s\"\n" $MAKEOPTS >> "/mnt/gentoo/etc/portage/make.conf"
printf "GENTOO_MIRRORS=\"%s\"\n" $MIRROR >> "/mnt/gentoo/etc/portage/make.conf"
cp --dereference /etc/resolv.conf /mnt/gentoo/etc/
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
echo "Entering chroot."
chroot /mnt/gentoo <<EOF
source /etc/profile
export PS1="(chroot) ${PS1}"
mount /dev/sda2 /boot
echo "Syncing with Gentoo ebuild repositorys (might take while)."
emerge-webrsync --quiet
eselect profile list
eselect profile set $PROFILE_SELECTION
echo "Updating @world set (might take while)."
emerge --quiet --update --deep --newuse @world
echo "Adding locale"
echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
locale-gen
eselect locale set $LOCALE_SELECTION
env-update && source /etc/profile && export PS1="(chroot) ${PS1}"
echo "Emerging gentoo-sources."
emerge --quiet sys-kernel/gentoo-sources
printf "/dev/sda2\t/boot\text2\tdefaults,noatime\t0 2\n" >> /etc/fstab
echo "Emerging genkernel."
ACCEPT_LICENSE="*" USE="static-libs" emerge --quiet sys-kernel/genkernel
echo "Configuring kernel (takes a lot of time)."
genkernel all
printf "/dev/sda3\tnone\tswap\tsw\t0 0\n" >> /etc/fstab
printf "/dev/sda4\t/\text4\tnoatime\t0 1\n" >> etc/fstab
echo "hostname=\"gentoo-nerd\"" > /etc/conf.d/hostname
echo "dns_domain_lo=\"homenetwork\"" > /etc/conf.d/net
echo "This is \n. (\s \m \r) \t" > /etc/issue
emerge --noreplace net-misc/netifrc
echo "config_eth0=\"dhcp\"" >> /etc/conf.d/net
echo -e "gentoo\ngentoo" | passwd root
useradd "gentoo"
echo -e "gentoo\ngentoo" | passwd gentoo
emerge app-admin/sysklogd
rc-update add sysklogd default
echo "Emerging DHCPD."
emerge net-misc/dhcpcd
echo "Emerging grub."
emerge --quiet sys-boot/grub:2
echo "Installing and configuring grub."
grub-install /dev/sda
grub-mkconfig -o /boot/grub/grub.cfg
echo "Exitting chroot"
EOF
echo "Unmounting devices."
cd
umount -l /mnt/gentoo/dev{/shm,/pts,}
umount -lR /mnt/gentoo
echo "Rebooting in 3 seconds."
sleep 3
reboot
@tokenwastaken
Copy link

nice one

@bobbbay
Copy link

bobbbay commented Dec 24, 2020

Sexy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment