Skip to content

Instantly share code, notes, and snippets.

@maxux
Last active November 30, 2023 00:44
Show Gist options
  • Save maxux/3a30133f18c72f15254bcdbbf2d97d6d to your computer and use it in GitHub Desktop.
Save maxux/3a30133f18c72f15254bcdbbf2d97d6d to your computer and use it in GitHub Desktop.
Gentoo Base Install Scripts
#!/bin/bash
gentoo="/home/gentoo"
mirror="https://ftp.belnet.be/pub/rsync.gentoo.org/gentoo/releases/amd64/autobuilds"
latest=$(curl -s ${mirror}/latest-stage3-amd64-openrc.txt | grep openrc | awk '{ print $1 }')
baselat=$(basename $latest)
mkdir -p ${gentoo}
cd ${gentoo}
wget ${mirror}/${latest} -O ${baselat}
tar xpvf ${baselat} --xattrs-include='*.*' --numeric-owner
rm -f ${baselat}
makeconf="${gentoo}/etc/portage/make.conf"
cpucount=$(($(grep -c MHz /proc/cpuinfo) + 1))
cat << EOF >> ${makeconf}
USE="bindist syslog -multilib -X -gnome -kde iproute2 btrfs lzma -cups"
LINGUAS="en"
L10N="en"
GENTOO_MIRRORS="http://gentoo.mirrors.ovh.net/gentoo-distfiles/"
PORTAGE_NICENESS="19"
ACCEPT_LICENSE="linux-fw-redistributable"
MAKEOPTS="-j${cpucount}"
EOF
mkdir -p ${gentoo}/etc/portage/repos.conf
cp ${gentoo}/usr/share/portage/config/repos.conf ${gentoo}/etc/portage/repos.conf/gentoo.conf
# set resolver for the chroot
cp -L /etc/resolv.conf ${gentoo}/etc/
mount --types proc /proc ${gentoo}/proc
mount --rbind /sys ${gentoo}/sys
mount --rbind /dev ${gentoo}/dev
mount --bind /run ${gentoo}/run
#!/bin/bash
disk="/dev/vda"
gentoo="/mnt/gentoo"
mirror="https://ftp.belnet.be/pub/rsync.gentoo.org/gentoo/releases/amd64/autobuilds"
# create partitions
parted ${disk} mklabel msdos
parted -a optimal ${disk} mkpart primary 0% 768MB
parted -a optimal ${disk} mkpart primary 768MB 40GB
parted -a optimal ${disk} mkpart primary 41GB 80GB
parted ${disk} set 1 boot on
partprobe ${disk}
# create and mount filesystem
mkfs.ext2 ${disk}1
mkfs.ext4 ${disk}2
mkfs.ext4 ${disk}3
mount ${disk}2 ${gentoo}
mkdir ${gentoo}/boot
mkdir ${gentoo}/home
mount ${disk}1 ${gentoo}/boot
mount ${disk}3 ${gentoo}/home
# synchronize time
chronyd -q
# fetch latest stage3 version
latest=$(curl -s ${mirror}/latest-stage3-amd64-openrc.txt | grep openrc | awk '{ print $1 }')
baselat=$(basename $latest)
cd ${gentoo}
# download and extract stage3 on the root filesystem
wget ${mirror}/${latest} -O ${baselat}
tar xpvf ${baselat} --xattrs-include='*.*' --numeric-owner
rm -f ${baselat}
makeconf="${gentoo}/etc/portage/make.conf"
cpucount=$(($(grep -c MHz /proc/cpuinfo) + 1))
# customize make.conf with minimal features
cat << EOF >> ${makeconf}
USE="bindist syslog -multilib -X -gnome -kde iproute2 btrfs lzma -cups"
LINGUAS="en"
L10N="en"
GENTOO_MIRRORS="http://gentoo.mirrors.ovh.net/gentoo-distfiles/"
PORTAGE_NICENESS="19"
ACCEPT_LICENSE="linux-fw-redistributable"
ACCEPT_KEYWORDS="~amd64"
MAKEOPTS="-j${cpucount}"
EOF
# configure original gentoo repository
mkdir -p ${gentoo}/etc/portage/repos.conf
cp ${gentoo}/usr/share/portage/config/repos.conf ${gentoo}/etc/portage/repos.conf/gentoo.conf
# set resolver for the chroot
cp -L /etc/resolv.conf ${gentoo}/etc/
# mount base system from host
mount --types proc /proc ${gentoo}/proc
mount --rbind /sys ${gentoo}/sys
mount --rbind /dev ${gentoo}/dev
mount --bind /run ${gentoo}/run
# download next script
gist="3a30133f18c72f15254bcdbbf2d97d6d"
wget -O ${gentoo}/tmp/gentoo-chroot.sh https://gist.githubusercontent.com/maxux/${gist}/raw/gentoo-chroot.sh
# chroot ${gentoo} /bin/bash
# chroot ${gentoo} /bin/bash /tmp/gentoo-chroot.sh
## Execute next script (gentoo-chroot) inside the chroot
exit 1
umount ${gentoo}/boot
umount ${gentoo}/home
umount ${gentoo}/proc
umount ${gentoo}/run
umount --recursive ${gentoo}/dev
umount --recursive ${gentoo}/sys
umount ${gentoo}
# reboot
#!/bin/bash
source /etc/profile
# make visual difference with host prompt
export PS1="(chroot) ${PS1}"
# fast way to fetch portage tree
emerge-webrsync
# clean up a bit release news
eselect news read > /dev/null
# choose no-multilib profile, pure 64 bits environment
eselect profile set default/linux/amd64/17.1/no-multilib
# set single locale
sed -i "s/#en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/" /etc/locale.gen
# set timezone
echo "Europe/Brussels" > /etc/timezone
emerge --config sys-libs/timezone-data
# upgrade system to 'unstable' (upgrade packages)
emerge -DuNv world
# reload environment
telinit u
hash -r
# apply locale change
eselect locale set en_US.utf8
env-update
source /etc/profile
export PS1="(chroot) ${PS1}"
# install some tools
emerge -v gentoolkit
# fetching latest kernel 6.1 release available and downloading it
krnl=$(equery list -po sys-kernel/vanilla-sources | awk '{ print $1 }' | grep sources-6.1)
version=${krnl:27}
emerge -v =${krnl}
# configuring kernel
cd /usr/src
ln -s linux-${version} linux
cd linux
wget http://home.maxux.net/temp/jan-config.gz
zcat jan-config.gz > .config
yes "" | make config
# reload makeopts set earlier
eval $(grep MAKEOP /etc/portage/make.conf)
make $MAKEOPTS
make install
make modules_install
cd /boot
ln -s vmlinuz-${version} vmlinuz
ln -s initramfs-${version}.img initramfs
# install firmwares
emerge -v sys-kernel/linux-firmware
# initramfs tools
emerge -v dracut sys-apps/nvme-cli
dracut --kver ${version}
# installing bootloader
emerge -v syslinux
echo >> /etc/fstab
grep ' / ' /proc/mounts | sed "s/ /\t/g" >> /etc/fstab
grep ' /boot ' /proc/mounts | sed "s/ /\t/g" >> /etc/fstab
grep ' /home' /proc/mounts | sed "s/ /\t/g" >> /etc/fstab
rootpart=$(grep ' / ' /proc/mounts | awk '{ print $1 }')
rootdisk=/dev/$(lsblk -ndo pkname $(grep ' / ' /proc/mounts | awk '{ print $1 }'))
# install bootsector
dd bs=440 conv=notrunc count=1 if=/usr/share/syslinux/mbr.bin of=${rootdisk}
# install bootloader
mkdir /boot/extlinux
extlinux --install /boot/extlinux
cd /usr/share/syslinux
cp menu.c32 memdisk libcom32.c32 libutil.c32 /boot/extlinux/
cat << EOF > /boot/extlinux/extlinux.conf
DEFAULT gentoo
LABEL gentoo
LINUX /vmlinuz
INITRD /initramfs
APPEND root=${rootpart}
EOF
# installing basic management
emerge -v chrony cronie netifrc syslog-ng
# setting up root password
echo "root:root" | chpasswd
# Missing: /etc/hosts
# Missing: /etc/conf.d/net (for custom network)
# Missing: /etc/hostname and /etc/conf.d/hostname
# setup ssh
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# enable services on boot time
rc-update add sshd default
rc-update add dhcpcd default
rc-update add chronyd default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment