Skip to content

Instantly share code, notes, and snippets.

@renich
Last active May 10, 2024 10:16
Show Gist options
  • Save renich/90e0a5bed8c7c0de40d40ac9ccac6dfd to your computer and use it in GitHub Desktop.
Save renich/90e0a5bed8c7c0de40d40ac9ccac6dfd to your computer and use it in GitHub Desktop.
Install Gentoo on BtrFS subvolumes + UEFI
#!/usr/bin/env bash
# WARNING!!
# This will obliterate all the data in your partition!! (not actually true, but act as if it was)
# Do NOT execute this script if you don't fully understand it!
# a few vars
amount_of_swap=$( free --si -g | grep Mem: | gawk '{ print $2 + 1}' )
# create directories
mkdir -p /mnt/gentoo
# create partitions
# I would add your amount of RAM + something (1 GB maybe?) to the swap partition (#3) instead of 9.
sgdisk -Z /dev/vda
sgdisk -o /dev/vda
sgdisk -n 1::+1024M -t 1:ef02 /dev/vda
sgdisk -n 2::+1G -t 2:8300 /dev/vda
sgdisk -n 3::+${amount_of_swap}G -t 3:8200 /dev/vda
sgdisk -n 4:: -t 4:8300 /dev/vda
# create filesystems
mkfs -t vfat -F 32 /dev/vda1
mkfs -t btrfs -L boot /dev/vda2
mkfs -t btrfs -L btrfsroot /dev/vda4
mkswap /dev/vda3
# mount
## root
mount /dev/vda4 /mnt/gentoo
mkdir -p /mnt/gentoo/boot
## boot
mount /dev/vda2 /mnt/gentoo/boot
mkdir -p /mnt/gentoo/boot/efi
# create subvols
cd /mnt/gentoo
btrfs subvol create root
btrfs subvol create home
btrfs subvol create srv
btrfs subvol create var
# unmount
cd ..
umount -l gentoo
#!/usr/bin/env bash
# This is a recipe; not a script. You should try to run it chunk by chunck.
# The shebang is only for syntax highlighting
# Chroot
## Please, run this right after running script 00
mount -o defaults,relatime,compress=lzo,autodefrag,subvol=root /dev/vda4 /mnt/gentoo
## create dirs for mounts
cd /mnt/gentoo
mkdir srv home root var boot
## mount
mount -o defaults,relatime,compress=lzo,autodefrag,subvol=home /dev/vda4 /mnt/gentoo/home
mount -o defaults,relatime,compress=lzo,autodefrag,subvol=srv /dev/vda4 /mnt/gentoo/srv
mount -o defaults,relatime,compress=lzo,autodefrag,subvol=var /dev/vda4 /mnt/gentoo/var
mount -o defaults,relatime /dev/vda2 /mnt/gentoo/boot
### efi partition
mount -o defaults,noatime /dev/vda1 /mnt/gentoo/boot/efi
## get gentoo stage3
curl -LO https://bouncer.gentoo.org/fetch/root/all/releases/amd64/autobuilds/20211121T170545Z/stage3-amd64-nomultilib-systemd-20211121T170545Z.tar.xz
## uncompress
tar -xapf stage3-amd64-nomultilib-systemd-20211121T170545Z.tar.xz
rm -f $_
## mount proc, sys and dev
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
## activate swap
swapon /dev/vda3
## get dns
cp -u /etc/resolv.conf /mnt/gentoo/etc/
## chroot
env -i HOME=/root TERM=$TERM chroot . bash -l
## environment
env-update
source /etc/profile
export PS1="(chroot) $PS1"
# emerge
## sync repo first
emaint -A sync
## update portage
emerge --oneshot portage
# portage (git)
# ref: https://wiki.gentoo.org/wiki/Portage_with_Git
## install dependencies for emerge with git
emerge -Dju app-eselect/eselect-repository dev-vcs/git
## setup portage to use git instead of rsync
eselect repository remove gentoo
eselect repository add gentoo git https://github.com/gentoo-mirror/gentoo.git
rm -r /var/db/repos/gentoo
## sync
emaint sync -r gentoo
# Setup
## fstab
cat << 'EOF' > /etc/fstab
# <fs> <mountpoint> <type> <opts> <dump/pass>
shm /dev/shm tmpfs nodev,nosuid,noexec 0 0
/dev/vda4 / btrfs rw,relatime,compress=zstd:1,autodefrag,subvol=root 0 0
/dev/vda4 /home btrfs rw,relatime,compress=zstd:1,autodefrag,subvol=home 0 0
/dev/vda4 /srv btrfs rw,relatime,compress=zstd:1,autodefrag,subvol=srv 0 0
/dev/vda4 /var btrfs rw,relatime,compress=zstd:1,autodefrag,subvol=var 0 0
/dev/vda3 none swap sw 0 0
/dev/vda2 /boot btrfs rw,relatime 1 2
/dev/vda1 /boot/efi vfat umask=0077,shortname=winnt 0 2
EOF
## local time in MX
ln -sf /usr/share/zoneinfo/Etc/UTC /etc/localtime
## add cpu flags
emerge app-portage/cpuid2cpuflags
cpu_flags=$( cpuid2cpuflags | cut -d ' ' -f '2-' )
## append to my make.conf
use_remove='-accessibility -altivec -apache2 -aqua -big-endian -bindist -boundschecking -bsf -canna -clamav -connman -coreaudio -custom-cflags -debug -dedicated -emacs -handbook -ibm -infiniband -iwmmxt -kde -kontact -libav -libedit -libressl -libsamplerate -mono -mule -neon -oci8 -oci8-instant-client -oracle -oss -pch -pcmcia -plasma -qmail-spp -qt4 -qt5 -static -syslog -sysvipc -tcpd -xemacs -yahoo -zsh-completion'
use_add='symlink unicode vim-syntax'
make_opts="-j$(( $( nproc ) + 1 ))"
cat << EOF > /etc/portage/make.conf
CFLAGS="-mtune=native -O2 -pipe"
CXXFLAGS=\${CFLAGS}
CHOST="x86_64-pc-linux-gnu"
CPU_FLAGS_X86="${cpu_flags}"
GRUB_PLATFORMS="efi-64"
# enable this if you like living on the edge
#ACCEPT_KEYWORDS="~amd64"
MAKEOPTS="${make_opts}"
ADD="${use_add}"
REMOVE="${use_remove}"
USE="\$REMOVE \$ADD"
# Portage Opts
FEATURES="parallel-fetch parallel-install ebuild-locks"
EMERGE_DEFAULT_OPTS="--with-bdeps=y"
AUTOCLEAN="yes"
EOF
## set profiles
eselect profile set default/linux/amd64/17.1/no-multilib/systemd/merged-usr
## update everything and cleanup
emerge -DNju @world
emerge -c
## configure os-prober to mount grub
cat << 'EOF' > /etc/portage/package.use/os-prober
>=sys-boot/grub-2.06-r7 mount
EOF
## install useful apps
emerge -DNju vim bash-completion btrfs-progs
emerge -DNju app-portage/eix app-portage/gentoolkit sys-process/htop sys-process/lsof sys-boot/os-prober
## install vanilla sources and genkernel-next
mkdir -p /etc/portage/package.license
cat << 'EOF' > /etc/portage/package.license/linux-firmware
sys-kernel/linux-firmware linux-fw-redistributable no-source-code
EOF
emerge -DNju genkernel sys-kernel/gentoo-sources sys-kernel/linux-firmware
## Remember to enable:
## * all virtio devices; at least: virtio_pci and virtio_blk
## * btrfs support
genkernel --menuconfig --btrfs --virtio all
# Configure real root and stuff
# Do not forget to append systemd to the kernel command line: GRUB_CMDLINE_LINUX="real_init=/lib/systemd/systemd quiet"
vim /etc/default/grub
## install grub
grub-install --target=x86_64-efi --efi-directory=/boot/efi
# update grub
grub-mkconfig -o /boot/grub/grub.cfg
# systemd
## Setup machine ID to activate journaling
systemd-machine-id-setup
## DHCP
cat << 'EOF' > /etc/systemd/network/20-default.network
[Match]
Name = enp*
[Network]
DHCP = yes
EOF
systemctl enable systemd-networkd.socket
# set root password
passwd
# reboot (and pray, think wishfully, cross your fingers or whatever you do to influence reality... not!)
reboot
# after reboot
## set hostname
# hostnamectl set-hostname my.example.tld
## set locale (after reboot)
# localectl set-locale LANG=<LOCALE>
## set time and date
# timedatectl --help
@X-method
Copy link

well done, super helpful

@renich
Copy link
Author

renich commented Jun 11, 2020

Glad to know it was useful! :D

@oicydwa
Copy link

oicydwa commented Nov 24, 2021

This seems to be missing information regarding EFI. I don't see you mounting your EFI partition at any point. I'm not a noob, but I'm not an expert, but /dev/sda1 should be mounted on the efi directory under /boot BEFORE you can install grub. Unless I'm overlooking something?

EDIT: Otherwise very helpful, Thank you for this. Helped since I know what I need to do, just not always the order or specific commands by memory and I don't always want to dig through the Gentoo Handbook.

@renich
Copy link
Author

renich commented Nov 24, 2021

@oicydwa you're correct! I overlooked that part. Thank you for pointing it out. :D

I'll see if I can work on it during the weekend. If you can give it a try and figure it out, please, report it back so we can improve this one. :)

@oicydwa
Copy link

oicydwa commented Nov 24, 2021

I'll see if I can work on it during the weekend. If you can give it a try and figure it out, please, report it back so we can improve this one. :)

Really all that's needed is to mount (according to your example) /dev/sda1 (the efi partition) to /boot/efi.
So basically insert below anytime before grub-install:

mkdir /boot/efi
mount /dev/sda1 /boot/efi

Or if before chroot:

mkdir /mnt/gentoo/boot/efi
mount /dev/sda1 /mnt/gentoo/boot/efi

@renich
Copy link
Author

renich commented Nov 25, 2021

@oicydwa it took more than that. Had to add something to make.conf and the grub install command changes.

@renich
Copy link
Author

renich commented Nov 25, 2021

OK, updated at Thu Nov 25, 2021. It works at the time of update! ;D

@renich
Copy link
Author

renich commented Aug 27, 2023

Fixed fstab's /boot/efi entry.

@renich
Copy link
Author

renich commented Aug 27, 2023

Finished updating it.

Added:

  • portage using git (much faster syncs and as many as you like during the day)
  • some random fixes.
  • relatime where possible.
  • merged-usr.

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