void

- enter the void 🪐
git clone git://git.acid.vegas/archlinux.git
Log | Files | Refs | Archive

enterthezoid (6535B)

      1 #!/bin/bash
      2 # enter the zoid (zfs on root with zraid) - developed by acidvegas (https://git.acid.vegas/void)
      3 #      boot: https://github.com/leahneukirchen/hrmpf
      4 # reference: https://docs.zfsbootmenu.org/en/v2.3.x/guides/void-linux/uefi.html (do we need to make any updates?)
      5 
      6 set -xev
      7 
      8 # Configuration
      9 export HOSTNAME=blackhole
     10 export BOOT_DRIVE=/dev/sde # Use the internal USB drive for the boot partition
     11 export BOOT_METHOD=direct # Use direct or refind
     12 export POOL_DRIVES="/dev/sda /dev/sdb /dev/sdc /dev/sdd" # Verify these with lsblk before running
     13 export RAIDZ_PARITY="1" # Number of drives to use for the RAID-Z parity (must be 1 or greater otherwise why are you using ZFS?)
     14 
     15 
     16 checks() {
     17 	# Check if the system is using UEFI or BIOS
     18 	if [ ! -d /sys/firmware/efi ]; then
     19 		echo "System must be using UEFI"
     20 		exit 1
     21 	fi
     22 
     23 	# Check if all drives exist and are valid
     24 	for d in $BOOT_DRIVE $POOL_DRIVES; do
     25 		if [ ! -b $d ]; then
     26 			echo "Drive $d does not exist"
     27 			exit 1
     28 		fi
     29 	done
     30 
     31 	# Check if the boot method is valid
     32 	if [ $BOOT_METHOD != "direct" ] && [ $BOOT_METHOD != "refind" ]; then
     33         echo "Boot method must be direct or refind"
     34         exit 1
     35     fi
     36 
     37 	# Check if the RAID-Z parity is valid
     38 	if [ $RAIDZ_PARITY -lt 1 ]; then
     39 		echo "RAID-Z parity must be 1 or greater"
     40 		exit 1
     41 	fi
     42 }
     43 
     44 
     45 setup_zfs() {
     46     # Validation
     47     check
     48 
     49     # Generate the hostid
     50 	source /etc/os-release
     51 	export ID
     52 	zgenhostid -f 0x00bab10c
     53 
     54 	# Prepare the boot drive
     55 	wipefs -a $BOOT_DRIVE
     56 	sgdisk --zap-all $BOOT_DRIVE
     57 	sgdisk -n "1:1m:+1g" -t "1:ef00" $BOOT_DRIVE
     58 
     59 	# Prepare the ZFS pool drives
     60 	for d in $POOL_DRIVES; do
     61 		wipefs -a $d
     62 		sgdisk --zap-all $d
     63 		sgdisk -n "1:0:-10m" -t "1:bf00" "$d"
     64 		if zdb -l "$d" &> /dev/null; then
     65 			zpool labelclear -f "$d"
     66 		fi
     67 	done
     68 
     69 	# Create the ZFS pool (should we use noatime=on instead of relatime=on?)
     70 	ZFS_POOL_DRIVES=$(echo $(for dev in $POOL_DRIVES; do find /dev/disk/by-id/ -samefile $(readlink -f "$dev") ! -name "*-part*" -print -quit; done))
     71 	zpool create -f -o ashift=12 -O compression=lz4 -O acltype=posixacl -O xattr=sa -O relatime=on -o autotrim=on -o compatibility=openzfs-2.1-linux -m none zroot raidz${RAIDZ_PARITY} $ZFS_POOL_DRIVES
     72 
     73 	# Create the ZFS datasets
     74 	zfs create -o mountpoint=none zroot/ROOT
     75 	zfs create -o mountpoint=/ -o canmount=noauto zroot/ROOT/$ID
     76 	zfs create -o mountpoint=/home zroot/home
     77 	zpool set bootfs=zroot/ROOT/$ID zroot
     78 
     79 	# Export and import the ZFS pool
     80 	zpool export zroot
     81 	zpool import -N -R /mnt zroot
     82 	zfs mount zroot/ROOT/$ID
     83 	zfs mount zroot/home
     84 
     85 	# Trigger udev
     86 	udevadm trigger
     87 
     88 	# Install base system
     89 	XBPS_ARCH=x86_64 xbps-install -S -R https://mirrors.servercentral.com/voidlinux/current -r /mnt base-system
     90 
     91 	# Copy the hostid into the new system
     92 	cp /etc/hostid /mnt/etc
     93 
     94 	# Chroot into the new system
     95 	echo "entering the void..."
     96 	xchroot /mnt
     97 }
     98 
     99 
    100 setup_chroot() {
    101     # Set the root password
    102 	echo "root:root" | chpasswd
    103 
    104 	# Update the package manager
    105 	xbps-install -Suy
    106 
    107 	# Install the non-free repository
    108 	xbps-install -y void-repo-nonfree
    109 	xbps-install -Suy
    110 
    111 	# Install & enable the intel microcode service
    112 	xbps-install -y intel-ucode
    113 	ln -sf /etc/sv/intel-ucode /etc/runit/runsvdir/default/
    114 
    115 	# Set the timezone & hardware clock
    116 	ln -sf /usr/share/zoneinfo/America/New_York /etc/localtime
    117 	hwclock --systohc
    118 
    119 	# Set the hostname
    120 	echo "$HOSTNAME" > /etc/hostname
    121 
    122 	# Set the rc.conf variables
    123 	printf "HOSTNAME=\"$HOSTNAME\"\nHARDWARECLOCK=\"UTC\"\nTIMEZONE=\"America/New_York\"\nKEYMAP=us\n" > /etc/rc.conf
    124 
    125 	# Set the locales
    126 	printf "en_US.UTF-8 UTF-8\nen_US ISO-8859-1\n" > /etc/default/libc-locales
    127 	xbps-reconfigure -f glibc-locales
    128 
    129 	# Set the dracut configuration
    130 	printf "nofsck=\"yes\"\nadd_dracutmodules+=\" zfs \"\nomit_dracutmodules+=\" btrfs \"\n" > /etc/dracut.conf.d/zol.conf
    131 
    132 	# Install the zfs package
    133 	xbps-install -y zfs
    134 
    135 	# Set the zfsbootmenu command line options
    136 	zfs set org.zfsbootmenu:commandline="quiet loglevel=4" zroot/ROOT
    137 
    138 	# Setup & mount the boot partition
    139 	mkfs.vfat -F32 ${BOOT_DRIVE}1
    140 	BOOT_UUID=$(blkid -s UUID -o value ${BOOT_DRIVE}1)
    141 	echo "UUID=$BOOT_UUID /boot/efi vfat defaults 0 0" > /etc/fstab
    142 	mkdir -p /boot/efi
    143 	mount /boot/efi
    144 
    145 	# Install and setup zfsbootmenu
    146 	xbps-install -S zfsbootmenu gummiboot-efistub yq
    147 	yq -iy '.Global.ManageImages=true | .Global.BootMountPoint="/boot/efi" | .Components.Enabled=false | .EFI.ImageDir="/boot/efi/EFI/zbm" | .EFI.Versions=false | .EFI.Enabled=true | .Kernel.CommandLine="quiet loglevel=0"' /etc/zfsbootmenu/config.yaml
    148 	generate-zbm
    149 
    150 	# Apply boot method
    151 	# Note      : Some systems can have issues with EFI boot entries, you might need to use a well-known EFI file name.
    152 	# Reference : https://docs.zfsbootmenu.org/en/v2.3.x/general/portable.html
    153 	if [ $BOOT_METHOD == "direct" ]; then
    154 	    xbps-install efibootmgr
    155 		efibootmgr -c -d "$BOOT_DRIVE" -p "1" -L "ZFSBootMenu (Backup)" -l '\EFI\ZBM\VMLINUZ-BACKUP.EFI'
    156 		efibootmgr -c -d "$BOOT_DRIVE" -p "1" -L "ZFSBootMenu" -l '\EFI\ZBM\VMLINUZ.EFI'
    157 	elif [ $BOOT_METHOD == "refind" ]; then
    158     	xbps-install -y refind
    159         refind-install
    160     	rm /boot/refind_linux.conf
    161     	printf "\"Boot default\"  \"quiet loglevel=0 zbm.skip\"\n\"Boot to menu\"  \"quiet loglevel=0 zbm.show\"\n" > /boot/efi/EFI/ZBM/refind_linux.conf
    162     	# Everything below this line is a "hacky" solution to a problem I was having with the zfsbootmenu package
    163     	# https://github.com/zbm-dev/zfsbootmenu/issues/293
    164     	# The developers of zfsbootmenu are rude and unhelpful, so I had to figure this out on my own:
    165     	#	12:39 -- Mode #zfsbootmenu [+b *!*@big.dick.acid.vegas] by zdykstra
    166     	#	12:39 ◀▬▬ zdykstra has kicked acidvegas (acidvegas)
    167     	#	12:39 -- #zfsbootmenu: Cannot join channel (+b) - you are banned
    168     	mkdir -p /boot/efi/EFI/BOOT
    169     	mvrefind /boot/efi/EFI/refind /boot/efi/EFI/BOOT
    170     	temp=$(mktemp -d)
    171     	wget -O $temp/latest.tar.gz https://get.zfsbootmenu.org/latest.tar.gz
    172     	tar xvf $temp/latest.tar.gz -C $temp/
    173     	rm $temp/latest.tar.gz
    174     	mv $temp/zfs*/* /boot/efi/EFI/ZBM/
    175     	rm /boot/efi/EFI/ZBM/vmlinuz.efi
    176     	xbps-remove zfsbootmenu
    177     fi
    178 
    179 	# Reconfigure the system
    180 	xbps-reconfigure -fa
    181 
    182 	# Exit the chroot environment
    183 	echo "exiting the void..."
    184 	exit
    185 }
    186 
    187 
    188 
    189 # Check the command
    190 if [ "$#" -ne 1 ]; then
    191 	echo "usage: $0 [zfs|chroot|final]"
    192 	exit 1
    193 fi
    194 
    195 # Execute the command
    196 case "$1" in
    197 	zfs)    setup_zfs ;;
    198 	chroot) setup_chroot ;;
    199 	final)  umount -n -R /mnt; zpool export zroot; reboot ;;
    200 	*)      echo "usage: $0 [zfs|chroot|final]" && exit 1 ;;
    201 esac