Skip to content

Instantly share code, notes, and snippets.

@Luv2C0d3
Last active March 28, 2021 14:36
Show Gist options
  • Save Luv2C0d3/60fdee0db8330cb7701f7ac5a7adbac0 to your computer and use it in GitHub Desktop.
Save Luv2C0d3/60fdee0db8330cb7701f7ac5a7adbac0 to your computer and use it in GitHub Desktop.
# Create a virtual machine, with 4GB + 4cpus. This such that compilation
# goes fast and you can pass for jobs (-j4) to make.
# Mark the checkbox in System / Motherboard / Enable EFI (special OSes only)
#
# Choose VBoxSvga as the display adapter IF you want guest auto-resize to work.
# This is because the autoresize is triggered by fooling the guest that the
# monitor changed by injecting a new serial#. This can't be done under VMSVGA.
# See this link -> https://wiki.gentoo.org/wiki/VirtualBox#Resizing_doesn.27t_work
#
# Use any live disk to drive the install. I used the archlinux boot disk because
# it works and has the genfstab utility which makes it easy to create the mount table.
#
# For cases where VM is booting into high dpi display.
# Not needed if you scale the VM to 200% from the VirtualBox UI
# Set large font
setfont latarcyrheb-sun32
#
# Set root password and start sshd to connect from
# terminal in host
#
passwd
systemctl start sshd
#
# Get VM ip address
#
ip a
#
# Now, from host connect via ssh to root@address returned above.
# Has the advantage that you can copy/paste commands into console.
#
# Sync clock
timedatectl set-ntp true
#
# Create 4 partitions:
# Create GRUB boot, boot, swap and root primary partitions
# For partitioning, follow instructions at
# https://wiki.gentoo.org/wiki/Handbook:AMD64/Installation/Disks
#
parted -a optimal /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt
(parted) mkpart primary 1 3
(parted) name 1 grub
(parted) set 1 bios_grub on
(parted) mkpart primary 3 131
(parted) name 2 boot
(parted) mkpart primary 131 4227
(parted) name 3 swap
(parted) mkpart pribary 4227 -1
(parted) name 4 rootfs
(parted) set 2 boot on
(parted) print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sda: 32.2GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 3146kB 2097kB grub bios_grub
2 3146kB 131MB 128MB boot boot, esp
3 131MB 4227MB 4096MB swap
4 4227MB 32.2GB 28.0GB rootfs
(parted)
#
# Format boot partition as vfat as we have UEFI.
#
mkfs.fat -F 32 /dev/sda2
#
# Format and mount the swap area
#
mkswap /dev/sda3
swapon /dev/sda3
#
# Create the btrfs partitions
#
mkfs.btrfs /dev/sda4
#
# Create btrfs subvolumes
#
mkdir /mnt/gentoo
mount -t btrfs /dev/sda4 /mnt/gentoo
btrfs subvolume create /mnt/gentoo/@root
btrfs subvolume create /mnt/gentoo/@var
btrfs subvolume create /mnt/gentoo/@home
btrfs subvolume create /mnt/gentoo/@snapshots
#
# dismount btrfs
#
umount /mnt/gentoo
#
# Mount btrfs as subvolumes
#
mkdir -p /mnt/gentoo
mount -o subvol=@root /dev/sda4 /mnt/gentoo
mkdir /mnt/gentoo/{var,home,.snapshots}
mount -o subvol=@var /dev/sda4 /mnt/gentoo/var
mount -o subvol=@home /dev/sda4 /mnt/gentoo/home
mount -o subvol=@snapshots /dev/sda4 /mnt/gentoo/.snapshots
#
# Mount boot / EFI partition
#
mkdir -p /mnt/gentoo/boot
mount /dev/sda2 /mnt/gentoo/boot
#
# Pull the stage 3 tarball and install it
#
cd /mnt/gentoo
#
# Somehow, get the tarball to /mnt/gentoo
#
links https://www.gentoo.org/downloads/mirrors/
#
# Unpack the tarball
#
tar xpvf stage3-*.tar.xz --xattrs-include='*.*' --numeric-owner
#
# Let's use Arch's genfstab to generate the /etc/fstab
# We do it here as the tarball has an /etc/fstab in it.
#
genfstab -L /mnt/gentoo >> /mnt/gentoo/etc/fstab
#
# Edit /etc/portage/make.conf and include COMMON_FLAGS="-march=native..." and
# MAKEOPTS="-j4"
#
nano /mnt/gentoo/etc/portage/make.conf
#
# Copy dns info into root partition for chrooted network to function properly
#
cp --dereference /etc/resolv.conf /mnt/gentoo/etc/
#
# Preparing additional mounts for chroot
# The make-rslave commands are for systemd
#
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
#
# chroot into the mounted area and continue in the
# disk.
#
SHELL= chroot /mnt/gentoo /bin/bash
#
# Note that all the bash history from here is recorded in root user
# on root partition
#
source /etc/profile
export PS1="(chroot) $PS1"
#
# Installing a Gentoo ebuild repository snapshot from the web
#
emerge-webrsync
#
# Verify default profile
#
eselect profile list
# Possibly not needed next one as it takes about 1h and
# the systemd tarball has all the goods.
# NOTE: The two lines below are just temporary due to circular dependencies
# problems with Python.
#
# time emerge --ask --verbose --update --deep --newuse --exclude python:3.9 @world
# time emerge --ask --verbose --update --deep --newuse @world
#
# Get kernel sources
#
emerge --ask sys-kernel/gentoo-sources
#
# config + build kernel
#
cd /usr/src/linux
#
# Create a default /usr/src/linux/.config
#
#make defconfig
make menuconfig
#
# Tweak things. I needed to add btrfs support and several mandatory things
# which did not seem to be included in the omnibus make option systemd
# THE UEFI options need something special, otherwise I get a blank screen.
# CONFIG_FRAMEBUFFER_CONSOLE=y
# CONFIG_FRAMEBUFFER_CONSOLE_DETECT_PRIMARY=y
#
# Device Drivers --->
# Generic Driver Options --->
# [*] Maintain a devtmpfs filesystem to mount at /dev
# [*] Automount devtmpfs at /dev, after the kernel mounted the rootfs
# Device Drivers --->
# SCSI device support --->
# <*> SCSI disk support
# File systems --->
# <*> Second extended fs support <--
# <*> The Extended 3 (ext3) filesystem <--
# <*> The Extended 4 (ext4) filesystem
# <*> Reiserfs support
# <*> JFS filesystem support
# <*> XFS filesystem support
# <*> Btrfs filesystem support <--
# DOS/FAT/NT Filesystems --->
# <*> MSDOS fs support
# <*> VFAT (Windows-95) fs support
Pseudo Filesystems --->
[*] /proc file system support
[*] Tmpfs virtual memory file system support (former shm fs)
Processor type and features --->
[*] Symmetric multi-processing support
Device Drivers --->
HID support --->
-*- HID bus support
<*> Generic HID driver
[*] Battery level reporting for HID devices <--
USB HID support --->
<*> USB HID transport layer
[*] USB support --->
<*> xHCI HCD (USB 3.0) support
<*> EHCI HCD (USB 2.0) support
<*> OHCI HCD (USB 1.1) support
-*- Enable the block layer --->
Partition Types --->
[*] Advanced partition selection
[*] EFI GUID Partition support
Processor type and features --->
[*] EFI runtime service support
[*] EFI stub support
[*] EFI mixed-mode support
Firmware Drivers --->
EFI (Extensible Firmware Interface) Support --->
<*> EFI Variable Support via sysfs
# Gentoo Linux --->
# Support for init systems, system and service managers --->
# [*] systemd
#
# The options I needed to add were:
#
# General setup --->
# [*] Control Group support --->
# [*] Support for eBPF programs attached to cgroup <-- This one only showed up when I added the
# omnibus choice systemd above.
# [ ] Enable deprecated sysfs features to support old userspace tools
# [*] Configure standard kernel features (expert users) ---> <--
# [*] open by fhandle syscalls
# [*] Enable eventpoll support
# [*] Enable signalfd() system call
# [*] Enable timerfd() system call
#
# Also needed to add the UEFI options:
# [*] Enable the block layer ---> <--
# Partition Types --->
# [*] Advanced partition selection
# [*] EFI GUID Partition support
# Processor type and features --->
# [*] EFI runtime service support
# Firmware Drivers --->
# EFI (Extensible Firmware Interface) Support -->
# <*> EFI Variable Support via sysfs
#
time make -j4
make modules_install
make install
#
# Just look at the modules installed above.
#
find /lib/modules/5.4.92-gentoo/ -type f -iname '*.o' -or -iname '*.ko'
#
# Let's get grub 2
#
emerge --ask --verbose sys-boot/grub:2
#
# Install grub boot loader to /dev/sda
#
grub-install --target=x86_64-efi --efi-directory=/boot
#
# Add systemd and dobtrfs parameters to command line. Will be used
# when creating the grub configuration next.
#
echo 'GRUB_CMDLINE_LINUX="init=/usr/lib/systemd/systemd dobtrfs"' >> /etc/default/grub
#
# Create a grub configuration
#
grub-mkconfig -o /boot/grub/grub.cfg
#
# Create systemd machine id
# Note: The Gentoo instructions say to use this next command
# systemd-machine-id-setup
# But, it seems it does not re-generate /etc/machine-id with a unique UUID
# which is what is needed, to avoid two different gentoo vms with bridged
# NIC from getting the same ip address.
# See this: https://unix.stackexchange.com/a/403054/312273
#
# Note 2: /etc/machine-id is also symlinked from /var/lib/dbus/machine-id
#
rm -f /etc/machine-id
dbus-uuidgen --ensure=/etc/machine-id
#
# Set root passwd, BEFORE we reboot so that we don't need to hook up again
# a live dvd just to set this password.
#
passwd
#
# At this point, reboot machine, login via console and finish setup
# this because dbus is not properly set up for hostnamectl / localectl to work
#
hostnamectl set-hostname <your-hostname>
#
# Missing something in the next one, possibly needs building the locales via localegen
# prior to issuing the command.
#
localectl set-locale LANG=en_US.utf8
localectl set-keymap dvorak
env-update && source /etc/profile
timedatectl set-timezone America/Vancouver
#
# Create network configuration
#
cat > /etc/systemd/network/50-dhcp.network <<eof
[Match]
Name=en*
[Network]
DHCP=yes
eof
systemctl enable systemd-networkd
systemctl start systemd-networkd
systemctl enable systemd-resolved
systemctl start systemd-resolved
systemctl enable sshd
systemctl start sshd
#
# Install sudo
#
emerge sudo
#
# Set root passwd
#
passwd
#
# Add a user. Replace 'user1' by your non-root login user.
useradd -m -g users -G wheel -s /bin/bash user1
passwd user1
echo 'user1 ALL=(ALL) ALL' > /etc/sudoers.d/user1
#
# Now, let's add bash-completions
#
emerge -a app-shells/bash-completion
#
# btrfs system tools
#
emerge -a sys-fs/btrfsmaintenance
#
# Verify all packages installed
#
cat /var/lib/portage/world
#
# Adding Virtualbox VM additions
# Reference: https://wiki.gentoo.org/wiki/VirtualBox#Linux_Guests
#
# 1st. Make sure the kernel is set according to instructions in link above.
#
echo 'VIDEO_CARDS="vmware virtualbox"' >> /etc/portage/make.conf
emerge --ask app-emulation/virtualbox-guest-additions
#
# enable guest additions and start them
#
systemctl enable virtualbox-guest-additions
#
# After the next command, kernel modules vboxguest and vboxsf should be loaded
#
systemctl start virtualbox-guest-additions
#
# lsmod
# Module Size Used by
# vboxsf 81920 0
# vboxguest 282624 2 vboxsf
#
# ----------------------------------------------------------------------------
# From here on is necessary only for adding X support
#
# Select a profile that has systemd + some desktop, which should have been
# compiled already with the X use flag.
eselect profile list
Available profile symlink targets:
[1] default/linux/amd64/17.1 (stable)
[2] default/linux/amd64/17.1/selinux (stable)
[3] default/linux/amd64/17.1/hardened (stable)
[4] default/linux/amd64/17.1/hardened/selinux (stable)
[5] default/linux/amd64/17.1/desktop (stable)
[6] default/linux/amd64/17.1/desktop/gnome (stable)
[7] default/linux/amd64/17.1/desktop/gnome/systemd (stable)
[8] default/linux/amd64/17.1/desktop/plasma (stable)
[9] default/linux/amd64/17.1/desktop/plasma/systemd (stable) *
[10] default/linux/amd64/17.1/desktop/systemd (dev)
#
#
emerge --ask x11-base/xorg-drivers
emerge --ask x11-base/xorg-server
#
# The following ones added only for testing, then can be deleted.
#
echo XSESSION="twm" > /etc/env.d/90xsession
emerge -a x11-wm/twm
emerge -a x11-apps/xinit
emerge -a x11-terms/xterm
#
# This next section working with my user, though possibly it's not strictly necessary
#
#
# Configure xterm such that the default terminal has black background
#
$ cat > XTerm <<eof
XTerm*Background: black
XTerm*Foreground: white
XTerm*font: 9x15
XTerm*saveLines: 1000
; XTerm*HiForeColor: black
; XTerm*HiBackColor: #c06077
; XTerm*geometry: +50+100
eof
#
# Once the above is complete, can start X via startx
#
startx
#
# Uninstall the previous test utilities
#
cat /var/lib/portage/world
emerge --deselect x11-terms/xterm
emerge --deselect x11-wm/twm
emerge -C x11-wm/twm
emerge -vC x11-terms/xterm
#
# xfce
#
emerge --ask xfce-base/xfwm4 xfce-base/xfce4-panel
emerge --ask xfce-base/xfce4-meta
emerge xfce-extra/xfce4-whiskermenu-plugin
#
# Adjust resolution of screen
# For now manual mode
# References:
# https://unix.stackexchange.com/a/363588/312273
# https://unix.stackexchange.com/a/225010/312273
# list monitors, assuming you obtained Virtual1
xrandr --listmonitors
...
0: +*Virtual1 ... Virtual1
...
# figure modes and check whether the mode I need is in there.
# `xrandr -d :0` or just `xrandr` will do
xrandr
# If mode is not there, then figure out the parameters it will
# take to add one mode to my monitor
cvt 1920 1080
Modeline "1920x1080_60.00" ....rest of chunk...
# output from cvt above is added as new mode and monitor reset to that
xrandr --newmode "1920x1080_60.00" ....rest of chunk...
xrandr --addmode Virtual1 "1920x1080_60.00"
xrandr --output Virtual1 --mode "1920x1080_60.00"
#
# Firefox
#
emerge -a www-client/firefox-bin
#
# vscode bin
# These instructions from https://github.com/jorgicio/jorgicio-gentoo-overlay
# and "safe" masking hint from here -> http://davydm.blogspot.com/2017/02/gentoo-adventures-overlays-are-great.html
# Add tool to manage repositories
emerge -a eselect-repository
# Add jorgicio repo
emaint sync -r jorgicio
#
# Mask everything from jorgicio, later I will unmask only vscode
#
echo '*/*::jordicio' > /etc/portage/package.mask/jordicio
#
# First run of emerge below will reveal the masked packages
# that I will need to unmask, accept licenses of
#
emerge -a app-editors/vscode-bin
#
# Now, unmask + accept license for package.
# Note: emerge may help with some of this configuration,
# but it's still good to know it.
#
mkdir -p /etc/portage/package.unmask
# unmask the vscode package
cat > /etc/portage/package.unmask/vscode-bin <<eof
### jorgicio
app-editors/vscode-bin
eof
#
# Add keywords, such that amd64 is good for this package
#
echo 'app-editors/vscode-bin ~amd64' /etc/portage/package.accept_keywords
#
# Now mark the MS license as accepted
#
echo '>=app-editors/vscode-bin-1.53.2 MS-vscode' >> /etc/portage/package.license
#
# Now emerge the package
#
emerge -a app-editors/vscode-bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment