Setting up a VoidLinux chroot

Published on November 28, 2021

Installation

We will first create a directory for our VoidLinux installation:

mkdir -p ~/chroot/voidlinux

Then download the glibc flavour of the rootfs tarball containing the VoidLinux root filesystem for the x86-64 architecture (check the download page for the download link):

wget https://alpha.de.repo.voidlinux.org/live/current/void-x86_64-ROOTFS-20210930.tar.xz

Extract the tarball:

tar xf void-x86_64-ROOTFS-20210930.tar.xz -C ~/chroot/voidlinux

chroot scripts

We will now set up a script that mounts /dev/, /proc/, /sys and /tmp, copies resolv.conf for networking and then either executes the command that we pass as the first argument or otherwise defaults to /bin/bash:

cat >~/chroot/root-voidlinux.sh << 'EOF'
#!/bin/sh
CHROOT_PATH="/home/$USER/chroot/voidlinux"
cd $CHROOT_PATH
mount | grep $CHROOT_PATH/dev >/dev/null || sudo mount --rbind /dev $CHROOT_PATH/dev && sudo mount --make-rslave $CHROOT_PATH/dev
mount | grep $CHROOT_PATH/proc >/dev/null || sudo mount -t proc /proc $CHROOT_PATH/proc
mount | grep $CHROOT_PATH/sys >/dev/null || sudo mount --rbind /sys $CHROOT_PATH/sys && sudo mount --make-rslave $CHROOT_PATH/sys
mount | grep $CHROOT_PATH/tmp >/dev/null || sudo mount --rbind /tmp $CHROOT_PATH/tmp
cp /etc/resolv.conf $CHROOT_PATH/etc/resolv.conf
sudo chroot $CHROOT_PATH ${1:-/bin/bash}
EOF

In addition, we will set up a script that simply lets us run commands as the same user as on our host system:

cat >~/chroot/voidlinux.sh << 'EOF'
#!/bin/sh
CHROOT_PATH="/home/$USER/chroot/voidlinux"
cd $CHROOT_PATH
mount | grep $CHROOT_PATH/dev >/dev/null || sudo mount --rbind /dev $CHROOT_PATH/dev && sudo mount --make-rslave $CHROOT_PATH/dev
mount | grep $CHROOT_PATH/proc >/dev/null || sudo mount -t proc /proc $CHROOT_PATH/proc
mount | grep $CHROOT_PATH/sys >/dev/null || sudo mount --rbind /sys $CHROOT_PATH/sys && sudo mount --make-rslave $CHROOT_PATH/sys
mount | grep $CHROOT_PATH/tmp >/dev/null || sudo mount --rbind /tmp $CHROOT_PATH/tmp
cp /etc/resolv.conf $CHROOT_PATH/etc/resolv.conf
sudo chroot --userspec=$USER:users $CHROOT_PATH ${1:-/bin/bash}
EOF

In addition, we will set up a script to undo the mounts:

cat >~/chroot/cleanup-voidlinux.sh << 'EOF'
#!/bin/sh
CHROOT_PATH="/home/$USER/chroot/voidlinux"
mount | grep $CHROOT_PATH/dev >/dev/null && sudo umount -R $CHROOT_PATH/dev
mount | grep $CHROOT_PATH/proc >/dev/null && sudo umount -R $CHROOT_PATH/proc
mount | grep $CHROOT_PATH/sys >/dev/null && sudo umount -R $CHROOT_PATH/sys
mount | grep $CHROOT_PATH/tmp >/dev/null && sudo umount -R $CHROOT_PATH/tmp
EOF

Ensure the scripts are executable:

chmod +x ~/chroot/voidlinux.sh ~/chroot/root-voidlinux.sh ~/chroot/cleanup-voidlinux.sh

We can then add a user with the same name, uid and gid as the user on our host system as follows:

./root-voidlinux.sh "useradd -mU $USER -u $UID -g $GID"

We can now simply test our chroot setup by entering the VoidLinux chroot as follows:

./voidlinux.sh

Exit the shell and run the cleanup script as follows:

./cleanup-voidlinux.sh

Updating VoidLinux

Enter the VoidLinux chroot and run the following commands to update the system:

./root-voidlinux.sh "xbps-install -Su xbps"
./root-voidlinux.sh "xbps-install -u"
./root-voidlinux.sh "xbps-install base-system"
./root-voidlinux.sh "xbps-remove base-voidstrap"

You can also create a script to update the VoidLinux installation:

cat >~/chroot/update-voidlinux.sh << 'EOF'
#!/bin/sh
CHROOT_PATH="/home/$USER/chroot/voidlinux"
cd $CHROOT_PATH
mount | grep $CHROOT_PATH/dev >/dev/null || sudo mount --rbind /dev $CHROOT_PATH/dev && sudo mount --make-rslave $CHROOT_PATH/dev
mount | grep $CHROOT_PATH/proc >/dev/null || sudo mount -t proc /proc $CHROOT_PATH/proc
mount | grep $CHROOT_PATH/sys >/dev/null || sudo mount --rbind /sys $CHROOT_PATH/sys && sudo mount --make-rslave $CHROOT_PATH/sys
mount | grep $CHROOT_PATH/tmp >/dev/null || sudo mount --rbind /tmp $CHROOT_PATH/tmp
cp /etc/resolv.conf $CHROOT_PATH/etc/resolv.conf
sudo chroot $CHROOT_PATH xbps-install -Su xbps
sudo chroot $CHROOT_PATH xbps-install -u
sudo chroot $CHROOT_PATH xbps-install base-system
sudo chroot $CHROOT_PATH xbps-remove base-voidstrap
EOF

Ensure the script is executable:

chmod +x ~/chroot/update-voidlinux.sh

Edit ~/chroot/voidlinux/etc/default/libc-locales and uncomment the line containing "#en_US.UTF-8 UTF-8" by removing the "#", if it is not already uncommented. Update the locales as follows:

./root-voidlinux.sh "xbps-reconfigure -f glibc-locales"

[ #alpine ]


If you like my work or if my work has been useful to you in any way, then feel free to donate me a cup of coffee. Any donation is much appreciated!