User:Daggystyle/Nfs based binhost setup

From Gentoo Wiki
Jump to:navigation Jump to:search

NFS based Server/Client BinHost setup

the following tutorial will show how to create a binhost server and client setup for gentoo. the base assumption are follows:

  • both server and client are of the same arch, in my case x86_64.
  • there is an network connection between both setups.

Build Chroot Creation

the below steps will result with a basic build chroot for the binhost server.

img and env preperation

first, create the proper tree by running the following cmd:

root #mkdir -p /mnt/gentoo_binhost/{chroot,binpkgs/portage-env}

next, allocate an image file on the server with a size which will accommodate the new chroot. my client runs from a 16GB usb thumb drive, so I used that exactly. the cmd is:

root #dd if=/dev/zero of=/mnt/gentoo_binhost/chroot.img.raw bs=1M count=16384

the next step will be to load the image so it can be used, the cmd is rather simple:

root #losetup -P $(losetup -f) /mnt/gentoo_binhost/chroot.img.raw

this will bind /mnt/gentoo_binhost/chroot.img.raw to the first free loop device found on the system. in order to see which loop device was used, run:

root #losetup --all | grep /mnt/gentoo_binhost/chroot.img.raw

and examine the output.

from now on, is it the same as the install handbook, e.g. create the proper partition table on the image Handbook:AMD64/Installation/Disks (I've decided to go with one full gpt partition.).
next, preform the stage unpack Handbook:AMD64/Installation/Stage#Installing_a_stage_tarball and don't forget to mount the root folder.

configuration

root #mount /dev/<llop device>p<num of root partition> /mnt/gentoo_binhost/chroot

now it is time to setup the system, edit make.conf as follows:

root #nano -w /mnt/gentoo_binhost/chroot/etc/portage/make.conf

and populate it as follows:

FILE /mnt/gentoo_binhost/chroot/etc/portage/make.conf
These settings were set by the catalyst build script that automatically
# built this stage.
# Please consult /usr/share/portage/config/make.conf.example for a more
# detailed example.

# NOTE: This stage was built with the bindist Use flag enabled
PORTDIR="/usr/portage"
DISTDIR="/var/cache/distfiles"
PKGDIR="/opt/binpkgs"

# This sets the language of build output to English.
# Please keep this setting intact when reporting bugs.
LC_MESSAGES=C

GENTOO_MIRRORS="ftp://ftp.gtlib.gatech.edu/pub/gentoo ftp://ftp.ussg.iu.edu/pub/linux/gentoo"

EMERGE_DEFAULT_OPTS="--buildpkg"
FEATURES="collision-protect parallel-fetch -userfetch clean-logs -xattr buildpkg"
PORTAGE_COMPRESS="xz"

create the bin pkgs target folder:

root #mkdir -p /mnt/gentoo_binhost/chroot/opt/binpkgs

the last step is to edit /mnt/gentoo_binhost/chroot/etc/profile:

root #nano -w /mnt/gentoo_binhost/chroot/etc/profile

and add the following to the end:

FILE /mnt/gentoo_binhost/chroot/etc/profile
if [ -e /opt/binpkgs/portage-env/make.conf.defs ]; then
        . /opt/binpkgs/portage-env/make.conf.defs
fi

if you need to use package.{use|mask|unmask}, you can do the following: create the missing folder under /mnt/gentoo_binhost/binpkgs/portage-env, for example:

root #mkdir -p /mnt/gentoo_binhost/binpkgs/portage-env/package.mask

then, edit the a file inside, for example:

FILE mnt/gentoo_binhost/binpkgs/portage-env/package.mask/pkgs
dev-lang/python:3.8

Chroot Utils Creation

this section will introduce the needed utils which will allow easy pkg management.

bh_emerge.sh

bh_emerge.sh is short for binhost emerge, this script will generate the required pkgs for the client to use. execute the following cmd:

root #nano -w /mnt/gentoo_binhost/chroot/usr/local/bin/bh_emerge.sh

and update the file as follows:

FILE /mnt/gentoo_binhost/chroot/usr/local/bin/bh_emerge.sh
#!/bin/bash
kernel_build=0

echo "updating repos cache..."
eix-update || exit 1

eix --installed --upgrade | egrep -q "/gentoo-sources|/linux-firmware"
if [ $? -eq 0 ]; then
        kernel_build=1
fi

echo "preforming upgrade..."
emerge -atvuDN @world | tee /var/log/world_update.log
ret_val=$?
emerge @preserved-rebuild | tee /var/log/world_update.log
emerge --depclean | tee /var/log/world_update.log

if [ ${kernel_build} -eq 1 ]; then
        echo "building new kernel..."
        build_kernel.sh
fi

if [ ${ret_val} -eq 0 ]; then
        echo "done."
else
        echo "upgrade failed, please attend to it."
fi

exit ${ret_val}

don't forget to set the proper permissions as follows:

root #chmod 755 /mnt/gentoo_binhost/chroot/usr/local/bin/bh_emerge.sh

build_kernel.sh

build_kernel.sh is the script that will compile and pack the kernel. create the file as follows:

root #nano -w /mnt/gentoo_binhost/chroot/usr/local/bin/build_kernel.sh

and update the file as follows:

FILE /mnt/gentoo_binhost/chroot/usr/local/bin/build_kernel.sh
#!/bin/bash

cfg_path="$(realpath /usr/src/linux/.config)"
new_kernel_path=$(eselect kernel list | grep linux | awk '{print $2}' | sort | tail -1)
eselect kernel set ${new_kernel_path} || exit 1
cd /usr/src/linux || exit 1
curr_cfg_sum=$(sha512sum $(basename ${cfg_path}) 2>/dev/null | awk '{print $1}')
new_cfg_sum=$(sha512sum ${cfg_path} 2>/dev/null | awk '{print $1}')

if [ "${new_cfg_sum}" != "${curr_cfg_sum}" ]; then
        cp ${cfg_path} . || exit 1
fi

make oldconfig || exit 1

if [ ! -z "${RUN_MENUCONFIG}" ]; then
        make menuconfig || exit 1
fi

make ${MAKEOPTS} || exit 1
make ${MAKEOPTS} modules_install || exit 1
version=$(file arch/x86/boot/bzImage | tr ' ' '\n' | grep -A1 version | tail -1)
(find arch/x86/boot/bzImage /lib/modules/${version} -not -type l -print0; echo -n ${cfg_path}) | tar -c -a -f /opt/binpkgs/kernels/kernel-${version}.tar.lzma --null -T - || exit 1

don't forget to set the proper permissions as follows:

root #chmod 755 /mnt/gentoo_binhost/chroot/usr/local/bin/build_kernel.sh

binhost configuration

this section will configure the binhost.

NFS Server configuration

install nfs-utils Nfs-utils#Installation. next, configure the export by executing:

root #nano -w /etc/exports

and add the following to it:

FILE /etc/exports
/usr/portage                    <network ip range>/24(ro,nohide,insecure,no_subtree_check)
/mnt/gentoo_binhost/binpkgs     <network ip range>/24(ro,nohide,insecure,no_subtree_check)

save and exit and execute:

root #exportfs -rv

verify there aren't any errors.

make.conf.defs

now it is time to configure the make.conf.

setting COMMON_FLAGS

the main issue with COMMON_FLAGS is -march, if the serer and client cpus are the same, -march=native can be used. if not, use -march=x86-64. using a more fitting compile opts will be added at later stage.

root #nano -w /mnt/gentoo_binhost/binpkgs/portage-env/make.conf.defs
FILE /mnt/gentoo_binhost/binpkgs/portage-env/make.conf.defs
export CFLAGS="-O2 -pipe -march=x86-64"
export CXXFLAGS="${CFLAGS}"
export CPPFLAGS="${CFLAGS}"
export FCFLAGS="${CFLAGS}"
export FFLAGS="${CFLAGS}"

export USE="<enter your useflags here>"
export FEATURES="collision-protect"
export ACCEPT_LICENSE="linux-fw-redistributable no-source-code"

export CPU_FLAGS_X86="<cpu cflags>"

and populate it as follows:

CPU_FLAGS_X86 generation will be covered in a later stage.

additional portage env config

if you need to use package.{use|mask|unmask}, you'll need to follow the relevant steps the chroot config section. afterwards, create the relevant link, for example:

root #ln -s /opt/binpkgs/portage-env/package.mask /etc/portage/package.mask

chroot handling utils

all the above is nice to have but abit hard to use for simple maintainment. for this there is /mnt/gentoo_binhost/manage_chroot.sh.

run

root #nano -w /mnt/gentoo_binhost/manage_chroot.sh

and populate it as follows:

FILE /mnt/gentoo_binhost/manage_chroot.sh
#!/bin/bash

dir=$(dirname $(realpath $0))
name=$(basename $0)
base_cmd="-c \"env-update && . /etc/profile && export PS1=\\\"(chroot) \\\$PS1\\\";"
cmd=

case ${name} in
        chroot_emerge)
                cmd="${base_cmd} emerge $*\""
        ;;

        chroot_run_menu_cfg)
                cmd="${base_cmd} RUN_MENUCONFIG=1 build_kernel.sh\""
        ;;

        chroot_shell)
        ;;

        chroot_update)
                cmd="${base_cmd} bh_emerge.sh\""
        ;;
 
        *)
                echo "Error: unsupported cmd ${name}"
                exit 1
        ;;
esac

cd ${dir}
MNT_POINT="chroot"
IMG="chroot.img.raw"
LOOP_POINT=$(losetup -f)
losetup -P ${LOOP_POINT} ${IMG}
mount ${LOOP_POINT}p<num of root partition> ${MNT_POINT}
mount -o rbind /dev ${MNT_POINT}/dev > /dev/null
mount -t proc none ${MNT_POINT}/proc > /dev/null
mount -o bind /sys ${MNT_POINT}/sys > /dev/null
mount -o bind /tmp ${MNT_POINT}/tmp > /dev/null
mount -o ro,bind $(realpath /usr/portage/) ${MNT_POINT}/usr/portage > /dev/null
mount -o rw,bind binpkgs ${MNT_POINT}/opt/binpkgs > /dev/null

eval $(echo chroot ${MNT_POINT} /bin/bash ${cmd})

umount $(mount | egrep "/$(basename ${MNT_POINT})[/ ]" | awk '{print $3}' | sort -r | xargs) || exit 1
losetup -d ${LOOP_POINT}

save it and execute:

root #chmod 755 /mnt/gentoo_binhost/manage_chroot.sh

to allow execution.

last but not least, create the proper links so the relevant folder content will be as follows:

root #ls -l /mnt/gentoo_binhost/*chroot*
lrwxrwxrwx 1 root root   16 Oct 18 17:04 /mnt/gentoo_binhost/chroot_emerge -> manage_chroot.sh
lrwxrwxrwx 1 root root   16 Oct 18 17:04 /mnt/gentoo_binhost/chroot_run_menu_cfg -> manage_chroot.sh
lrwxrwxrwx 1 root root   16 Oct 18 17:04 /mnt/gentoo_binhost/chroot_shell -> manage_chroot.sh
lrwxrwxrwx 1 root root   16 Oct 18 17:04 /mnt/gentoo_binhost/chroot_update -> manage_chroot.sh
-rwxr-xr-x 1 dagg dagg 1123 Oct 18 18:15 /mnt/gentoo_binhost/manage_chroot.sh

this concludes the binhost setup and configuration.

bin client configuration

this section will configure the bin client.

basic installation

for this stage, you will need the same basic requirements as a normal installation with one restriction, the installation env must support nfs mount. the gentoo net install cd will do just fine.

follow the guide until the configure the compile opts step (Handbook:AMD64/Installation/Stage#Configuring_the_compile_options)

first thing in order to update make.conf, run

root #nano -w /mnt/gentoo/etc/portage/make.conf

and populate it with these settings:

FILE /mnt/gentoo/etc/portage/make.conf
#These settings were set by the catalyst build script that automatically
#built this stage.
#Please consult /usr/share/portage/config/make.conf.example for a more
#detailed example.

#NOTE: This stage was built with the bindist Use flag enabled
PORTDIR="/usr/portage"
DISTDIR="/var/cache/distfiles"
PKGDIR="/opt/binpkgs"

#This sets the language of build output to English.
#Please keep this setting intact when reporting bugs.
LC_MESSAGES=C

EMERGE_DEFAULT_OPTS="--usepkgonly --rebuilt-binaries --usepkg-exclude 'sys-kernel/gentoo-sources'"

continue with the installation guide until the mounting part (Handbook:AMD64/Installation/Base#Mounting_the_necessary_filesystems) be sure to skip the Gentoo ebuild repository config part as it isn't needed.

after the pre chroot mounts described in the handbook were preforemed, there is a need to monunt the portage nfs and the binpkgs mounts. run the below cmds:

root #mount -tnfs <binhost ip>:/usr/portage /usr/portage
root #mount -tnfs <binhost ip>:/mnt/gentoo_binhost/binpkgs /opt/binpkgs

continue with the chroot from the handbook but skip the emerge sync. like in the binhost configuration step.

root #nano -w /etc/profile

and add the following to the end:

FILE /etc/profile
if [ -e /opt/binpkgs/portage-env/make.conf.defs ]; then
        . /opt/binpkgs/portage-env/make.conf.defs
fi

and apply theses changes on the current chroot by executing

root #. /opt/binpkgs/portage-env/make.conf.defs