#!/usr/bin/env bash # # Script to build Kogaion Linux packages in a clean squashfs + overlayfs chroot # Using predefined targets : kernel.target, libs.target, x11.target, sound.target, artwork.target etc # Targets are passed as arguments, and will remain private...for now (not all of them are stable yet) # Author : V3n3RiX @ RogentOS # Deps : squashfs + overlayfs kernel support # Todo : add squashfs portage tree && overlays sync && build.git sync && profile setup # Todo : auto push built packages to repos (maybe, really needed?!?) export local sqfsroot="kogaiondevelx64.squashfs" export local sqfsroothash="fb2afc5f3b387412a0954f242df54927" export local rodir="rodir" export local rwdir="rwdir" export local overlaydir="overlaydir" export local envuser="root" export local envtargets="$@" export local envbuildcmd="emerge -av "$envtargets"" envintegrity() { # check if our squashfs chroot somehow got corrupted, abort if so while : true ; do if [[ "$(md5sum "$sqfsroot" | awk {'print $1'})" = "$sqfsroothash" ]]; then break else exit 1 fi done } envstart () { # mount ro squashfs + add rw overlayfs layer to enable clean package building mount -t squashfs "$sqfsroot" "$rodir" mount -t overlayfs -o lowerdir="$rodir",upperdir="$rwdir" overlayfs "$overlaydir" mount -o bind /proc "$overlaydir"/proc mount -o bind /sys "$overlaydir"/sys mount -o bind /dev "$overlaydir"/dev mount -o bind /dev/pts "$overlaydir"/dev/pts mount -o bind /dev/shm "$overlaydir"/dev/shm mount -o bind /tmp "$overlaydir"/tmp } envstop () { # umount squashfs + overlayfs chroot umount -l "$overlaydir"/proc umount -l "$overlaydir"/sys umount -l "$overlaydir"/dev/pts umount -l "$overlaydir"/dev/shm umount -l "$overlaydir"/dev umount -l "$overlaydir"/tmp umount -l "$overlaydir" umount -l "$rodir" } envbuild () { # build packages in squashfs + overlayfs chroot chroot "$overlaydir" su - "$envuser" -c "$envbuildcmd" } envchroot() { # enter squashfs + overlayfs chroot to push packages, or debug build errors echo -e "" echo -e "#################################################################" echo -e "# ENTERING CHROOT ENV FOR YOU TO PUSH BUILT PACKAGES #" echo -e "# OR TO FIX EVENTUAL BUILD ERRORS #" echo -e "#################################################################" echo -e "# !!! WARNING !!! WARNING !!! WARNING !!! #" echo -e "#################################################################" echo -e "# NEXT RUN OF THIS SCRIPT WILL DESTROY ALL YOUR WORK #" echo -e "# DO NOT EXIT CHROOT UNTIL ALL PACKAGES ARE PUSHED TO REPOS #" echo -e "# OR, IN CASE OF BUILD FAILURES, UNTIL ALL FIXES ARE COMMITED #" echo -e "#################################################################" echo -e "# !!! WARNING !!! WARNING !!! WARNING !!! #" echo -e "#################################################################" echo -e "" echo -e "" chroot "$overlaydir" su - "$envuser" } envprepare () { # first let's check our environment for sanity # if safe, trigger start && break the loop to build packages # else trigger stop && cleanup && check again while : true ; do if [[ ! -d "$rodir" && ! -d "$rwdir" && ! -d "$overlaydir" ]]; then for i in "$rodir" "$rwdir" "$overlaydir" ; do mkdir "$i" done envstart break elif [[ -d "$overlaydir" && -f "$overlaydir/usr/bin/emerge" ]] ; then envstop for i in "$rodir" "$rwdir" "$overlaydir" ; do rm -rf "$i" done continue fi done } main () { envintegrity envprepare envbuild envchroot } main exit 0