Initial commit.

This commit is contained in:
Xavier Moffett 2023-01-18 14:26:34 -05:00
commit 8d7b73d70c
4 changed files with 506 additions and 0 deletions

170
sbcreate Executable file
View file

@ -0,0 +1,170 @@
#!/bin/bash
#
# BSD-3-Clause
#
# Sandbox Creation Utility Script (SBCreate) Copyright 2023 Xavier (sapphirus@azorium.net)
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (
# INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
main () {
parse_args "$@"
if [[ $SANDBOX == "" ]]; then
echo "Sandbox not specified."
exit 1;
fi
PACMAN_CONFIG=pacman.base.conf
MACHINE_ID=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 32 | head -n 1)
BASE_ROOT=$SANDBOX_BASE/root/base
INSTANCE_LINK=$SANDBOX_BASE/hldb/$SANDBOX
INSTANCE_HOME=$SANDBOX_BASE/home/$SANDBOX
INSTANCE_ROOT=$SANDBOX_BASE/root/$SANDBOX
if [[ $SANDBOX == "base" ]]; then
PACMAN_CONFIG=pacman.conf
fi
if [[ -d $INSTANCE_ROOT ]] || [[ -d $INSTANCE_HOME ]]; then
echo "Root or home directory for $SANDBOX exists. Aborting."
exit 1
fi
echo "Pre-initialising structures.."
PARAMS="--sandbox=$SANDBOX --root --exec"
init_struct
if [[ ! -d $INSTANCE_ROOT ]] || [[ ! -d $INSTANCE_HOME ]]; then
echo "Directory creation failed."
exit 1
fi
echo "Initialising chroot.."
init_chroot
if [[ $SANDBOX != "base" ]]; then
echo "Initialising linker and link database.."
init_link
fi
echo "Finalising chroot.."
finalise
echo "Cleaning up.."
cleanup
echo "Process complete! Use sbexecute to configure $SANDBOX."
}
init_struct () {
mkdir -p $INSTANCE_ROOT $INSTANCE_HOME \
$INSTANCE_ROOT/etc \
$INSTANCE_ROOT/var/lib/pacman \
$INSTANCE_ROOT/etc/pacman.d/
cp $SANDBOX_BASE/etc/pacman.conf $INSTANCE_ROOT/etc/pacman.conf
cp $SANDBOX_BASE/etc/pacman.base.conf $INSTANCE_ROOT/etc/pacman.base.conf
cp /etc/pacman.d/mirrorlist $INSTANCE_ROOT/etc/pacman.d/mirrorlist
echo "en_CA.UTF-8 UTF-8" > $INSTANCE_ROOT/etc/locale.gen
echo "LANG=en_CA.UTF-8" > $INSTANCE_ROOT/etc/locale.conf
echo 'PS1="'$SANDBOX'> "' > $INSTANCE_HOME/.bashrc
cp $XDG_DATA_HOME/sbscripts/template $XDG_DATA_HOME/sbscripts/$SANDBOX
echo "$MACHINE_ID" > $INSTANCE_ROOT/etc/machine-id
mkdir -p $INSTANCE_LINK
}
init_chroot () {
fakechroot fakeroot pacman -Syu \
--root $INSTANCE_ROOT \
--dbpath $INSTANCE_ROOT/var/lib/pacman \
--config $INSTANCE_ROOT/etc/$PACMAN_CONFIG \
--cache /var/lib/cache/pacman/pkg \
base --noconfirm 2>/dev/null
}
init_link() {
link_create "$BASE_ROOT/usr/lib" "$INSTANCE_ROOT/usr/" > $INSTANCE_LINK/lib
link_create "$BASE_ROOT/usr/bin" "$INSTANCE_ROOT/usr/" > $INSTANCE_LINK/bin
link_create "$BASE_ROOT/usr/share" "$INSTANCE_ROOT/usr/" > $INSTANCE_LINK/share
local dir=$PWD
cd $INSTANCE_LINK
sha256sum lib > $INSTANCE_LINK/lib.sha256
sha256sum bin > $INSTANCE_LINK/bin.sha256
sha256sum share > $INSTANCE_LINK/share.sha256
cd $dir
}
link_create() {
local files=$(rsync -av --link-dest="$1" "$1" "$2" | head -n -3 | tail -n +3 | tr -d ' ')
for f in $files; do
local file=${f%->*}
local filepath=$BASE_ROOT/usr/$file
if [[ -f $filepath ]] || [[ -L $filepath ]]; then
echo $file
fi
done
}
finalise () {
sbexecute $PARAMS pacman-key --init
sbexecute $PARAMS pacman-key --populate
sbexecute $PARAMS locale-gen
sbexecute $PARAMS update-ca-trust
}
cleanup () {
rm -v -r \
$INSTANCE_ROOT/boot \
$INSTANCE_ROOT/mnt \
$INSTANCE_ROOT/root \
$INSTANCE_ROOT/srv \
$INSTANCE_ROOT/sys \
$INSTANCE_ROOT/opt \
$INSTANCE_ROOT/run \
$INSTANCE_ROOT/dev
}
parse_args () {
ArGS=()
for var in "$@"; do
if [[ "$var" != *-* ]]; then
SANDBOX=$var
fi
done
}
main $@

115
sbexecute Executable file
View file

@ -0,0 +1,115 @@
#!/bin/bash
#
# BSD-3-Clause
#
# Sandbox Invocation Script - Copyright 2022-2023 Xavier (sapphirus@azorium.net)
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (
# INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
main () {
parse_args "$@"
if [[ $SANDBOX == "" ]]; then
echo "Sandbox not specified."
exit 1
fi
INSTANCE_USER=user
INSTANCE_ROOT=$SANDBOX_BASE/root/$SANDBOX
INSTANCE_HOME=$SANDBOX_BASE/home/$SANDBOX
INSTANCE_HOME_MOUNT=/home/$INSTANCE_USER
if [[ ! -d $INSTANCE_ROOT ]] || [[ ! -d $INSTANCE_HOME ]] || [[ ! -d $SANDBOX_BASE ]]; then
echo "Sandbox not found."
exit 1
fi
if [[ $SWITCH == *r* ]]; then
SANDBOX=fakeroot
fi
SANDBOX_SCRIPT="$XDG_DATA_HOME/sbscripts/$SANDBOX.sh"
if [[ ! -f $SANDBOX_SCRIPT ]]; then
echo "Script $SANDBOX_SCRIPT not found."
exit 1
fi
case $SWITCH in
ep)
nvim "$INSTANCE_ROOT/etc/pacman.conf"
;;
*rc*|*cr*)
execute_sandbox fakeroot "${ARGS[@]}"
;;
*rs*|*sr*)
execute_sandbox fakeroot bash
;;
s)
execute_sandbox bash
;;
*)
execute_sandbox "${ARGS[@]}"
;;
esac
}
parse_args () {
ARGS=()
for var in "$@"; do
case $var in
--sandbox=*)
SANDBOX=`echo $var | cut -c 11-`
;;
--root|-r)
SWITCH=r$SWITCH
;;
-s|--shell)
SWITCH=s$SWITCH
;;
--pacman)
SWITCH=p$SWITCH
;;
--exec)
SWITCH=rc$SWITCH
;;
--edit-pacman|-ep)
SWITCH=ep
;;
*)
ARGS+=("$var")
;;
esac
done
}
execute_sandbox () {
source $SANDBOX_SCRIPT
}
main $@

3
sbshell Executable file
View file

@ -0,0 +1,3 @@
#!/bin/bash
echo "Invoking root shell on $1."
sbexecute --sandbox=$1 --shell --root

218
sbupdate Executable file
View file

@ -0,0 +1,218 @@
#!/bin/bash
#
# BSD-3-Clause
#
# Sandbox Update - Copyright 2022-2023 Xavier (sapphirus@azorium.net)
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (
# INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
SANDBOX_ROOT=$SANDBOX_BASE/root
BOLD=$(tput bold)
RED=$(tput setaf 1)
RESET=$(tput sgr0)
BAR="$BOLD$RED::$RESET$BOLD"
PARAMS="--root --exec --sandbox="
main () {
if [[ ! -d $SANDBOX_ROOT ]]; then
echo "$BOLD$RED Sandbox root is either missing or an environmental variable is misconfigured."
exit
fi
BASE_LINK_LIST=$SANDBOX_BASE/hldb/list
local list=$@
if [[ ! $list ]]; then
list=`ls -1F $SANDBOX_ROOT | grep -i "/" | tr -d "/"`
fi
update $list
if [ ! "$(query_confirm "Invoke update on base dependency")" ]; then
invoke_update base
update_base $list
update_link_db
update_link $list
fi
}
log () {
echo -e "$BAR $@ $RESET"
}
update_link_db() {
local dir=$PWD
local source=$SANDBOX_BASE/root/base
local target=$SANDBOX_BASE/hldb/base
log "Synchronizing base link database..."
link_update $source/usr/share /tmp/usr/ > $target/share
link_update $source/usr/bin /tmp/usr/ > $target/bin
link_update $source/usr/lib /tmp/usr/ > $target/lib
cd $target
sha256sum lib > $target/lib.sha256
sha256sum bin > $target/bin.sha256
sha256sum share > $target/share.sha256
cd $dir
}
invoke_link() {
local dir=$PWD
local source=$1
local target=$2
log "Synchronizing links for $2 in $3 sandbox..."
link_create $source/usr/$target $root/usr/ $root > $link/$target
cd $link
sha256sum $target > $link/$target.sha256
cd $dir
}
link_update() {
local source=$source
parse_file_list $(rsync -av --dry-run --link-dest="$1" "$1" "$2" | head -n -3 | tail -n +3 | tr -d ' ')
}
link_create() {
local source=$source
parse_file_list $(rsync -av --link-dest="$1" "$1" "$2" | head -n -3 | tail -n +3 | tr -d ' ')
}
parse_file_list() {
local files=$@
for f in ${files[@]}; do
local file=${f%->*}
local filepath=$source/usr/$file
if [[ -f $filepath ]] || [[ -L $filepath ]]; then
echo $file
fi
done
}
parse_delete_list() {
local fail=$1
local files=$(cat $link/$fail)
for f in ${files[@]}; do
local file=${f%->*}
local filepath=$root/usr/$file
if [[ -f $filepath ]] || [[ -L $filepath ]]; then
echo $filepath
fi
done
}
checksum_link_files() {
local dir=$PWD
cd $link
local linkfiles=$(ls *.sha256)
for file in $linkfiles; do
$(cat $SANDBOX_BASE/hldb/base/$file | sha256sum --check --status);
local return=$?
if [[ $return == 1 ]]; then
echo -e "${file%.*} "
fi
done
cd $dir
}
update_link() {
for sandbox in "$@"; do
root=$SANDBOX_BASE/root/$sandbox
link=$SANDBOX_BASE/hldb/$sandbox
checksum_fail_list=$(checksum_link_files);
if [[ $checksum_fail_list == "" ]]; then
log "Link database is up-to-date for $sandbox sandbox!"
continue
fi
log "Preparing to resynchronize $sandbox's filesystem..."
for item in ${checksum_fail_list[@]}; do
local files=`parse_delete_list $item`
#for file in $files; do
rm -f $files
#done
done
for item in ${checksum_fail_list[@]}; do
invoke_link $SANDBOX_BASE/root/base $item $sandbox
done
done
}
update_base() {
for var in "$@"; do
([[ $var == *.* ]] || [[ $var == "base" ]]) && continue
invoke_update $var --config=/etc/pacman.base.conf
done
}
update () {
for var in "$@"; do
([[ $var == *.* ]] || [[ $var == "base" ]]) && continue
invoke_update $var
done
PACMAN_SYNC=1
}
invoke_update() {
if [[ ! $PACMAN_SYNC ]]; then
sbexecute $PARAMS$1 pacman -Sy
fi
local result=$(sbexecute pacman -Qu $PARAMS$@)
if [[ ! $result ]]; then
log "Packages are up-to-date for $1 sandbox!"
return
fi
echo -e "$BAR Packages to be installed or upgraded: $RESET \n\n$result\n"
[ "$(query_confirm "Confirm update on sandbox $var")" != "" ] && return
sbexecute pacman -Su $PARAMS$@ --noconfirm 2>/dev/null
}
query_confirm () {
read -rp "$BAR $@ [Y/n]$RESET " input
if [[ "$input" != "Y" ]] &&
[[ "$input" != "y" ]] &&
[[ "$input" != "" ]]; then
echo 0
fi
}
main $@