2023-02-25 06:41:32 +00:00
|
|
|
#!/bin/sh
|
|
|
|
set -eu
|
|
|
|
|
2024-04-09 18:55:01 +00:00
|
|
|
USERNAME="${USERNAME:-$(id -u -n)}"
|
|
|
|
UNSHARE_CMD="${UNSHARE_CMD:-unshare}"
|
|
|
|
|
2023-02-25 06:41:32 +00:00
|
|
|
# make sure we're root
|
|
|
|
if [ "$(id -u)" != 0 ]; then
|
|
|
|
sucmd="su -c"
|
|
|
|
if command -v doas >/dev/null; then
|
|
|
|
sucmd="doas"
|
|
|
|
elif command -v sudo >/dev/null; then
|
|
|
|
sucmd="sudo"
|
|
|
|
fi
|
|
|
|
|
2024-04-09 18:55:01 +00:00
|
|
|
exec $sucmd \
|
|
|
|
env \
|
|
|
|
HOME="$HOME" \
|
|
|
|
USERNAME="$USERNAME" \
|
|
|
|
UNSHARE_CMD="$UNSHARE_CMD" \
|
|
|
|
WAYLAND_DISPLAY="$WAYLAND_DISPLAY" \
|
|
|
|
DISPLAY="$DISPLAY" \
|
|
|
|
XDG_RUNTIME_DIR="$XDG_RUNTIME_DIR" \
|
|
|
|
DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS" \
|
|
|
|
"$0" "$@"
|
2023-02-25 06:41:32 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# and make sure we're in a separate mount namespace
|
|
|
|
if [ "$(readlink /proc/$$/ns/mnt | cut -d: -f2)" = "$(readlink /proc/1/ns/mnt | cut -d: -f2)" ]; then
|
2024-04-09 18:55:01 +00:00
|
|
|
exec $UNSHARE_CMD --keep-caps -m "$0" "$@"
|
2023-02-25 06:41:32 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
bindpoints="/etc/resolv.conf"
|
|
|
|
command=""
|
|
|
|
user="$USERNAME"
|
|
|
|
|
2024-04-09 18:55:01 +00:00
|
|
|
while getopts "bde:u:c:" opt; do
|
2023-02-25 06:41:32 +00:00
|
|
|
case $opt in
|
|
|
|
'b') bindpoints="
|
|
|
|
$bindpoints
|
2023-04-28 03:05:24 +00:00
|
|
|
/var/cache/distfiles
|
2023-02-25 06:41:32 +00:00
|
|
|
$HOME/aports
|
|
|
|
$HOME/packages
|
|
|
|
$HOME/.abuild
|
|
|
|
" ;;
|
2024-04-09 18:55:01 +00:00
|
|
|
'd') bindpoints="
|
|
|
|
$bindpoints
|
|
|
|
$XDG_RUNTIME_DIR
|
|
|
|
/tmp
|
|
|
|
" ;;
|
2023-02-25 06:41:32 +00:00
|
|
|
'e') bindpoints="$bindpoints $OPTARG" ;;
|
|
|
|
'u') user="$OPTARG" ;;
|
|
|
|
'c') command="$OPTARG" ;;
|
|
|
|
*) echo "unknown $opt" ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $(( $OPTIND - 1 ))
|
|
|
|
|
|
|
|
dest="$1"
|
2022-07-19 03:31:00 +00:00
|
|
|
shift
|
|
|
|
|
2023-02-25 06:41:32 +00:00
|
|
|
if [ ! -d "$dest" ]; then
|
|
|
|
echo "no such directory: $dest"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2024-04-09 18:55:01 +00:00
|
|
|
if ! [ -x "$dest"/bin/sh -o -L "$dest"/bin/sh ]; then
|
2023-02-25 06:41:32 +00:00
|
|
|
echo "$dest does not contain executable /bin/sh"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2024-04-09 18:55:01 +00:00
|
|
|
# do stupid path fixup
|
|
|
|
export PATH="/bin:/sbin:/usr/bin:/usr/sbin:$PATH"
|
|
|
|
|
2023-02-25 06:41:32 +00:00
|
|
|
mount -t proc proc "$dest"/proc
|
|
|
|
mount -t sysfs sysfs "$dest"/sys
|
|
|
|
mount -t tmpfs tmpfs "$dest"/tmp
|
|
|
|
# some mini kernels don't have devtmpfs
|
|
|
|
if grep -q devtmpfs /proc/filesystems; then
|
|
|
|
mount -t devtmpfs devtmpfs "$dest"/dev
|
|
|
|
else
|
|
|
|
mount -t tmpfs tmpfs "$dest"/dev
|
|
|
|
|
2024-04-09 18:55:01 +00:00
|
|
|
if [ -x "$dest"/sbin/mdev -o -L "$dest"/sbin/mdev ]; then
|
2023-02-25 06:41:32 +00:00
|
|
|
echo "devtmpfs not supported - running 'mdev -s' instead"
|
2024-04-09 18:55:01 +00:00
|
|
|
chroot "$dest" /sbin/mdev -sv
|
2023-02-25 06:41:32 +00:00
|
|
|
else
|
|
|
|
echo "devtmpfs not supported - devices need to be created manually"
|
|
|
|
fi
|
|
|
|
fi
|
2024-04-09 18:55:01 +00:00
|
|
|
mkdir -p "$dest"/dev/pts "$dest"/dev/shm
|
2023-02-25 06:41:32 +00:00
|
|
|
mount -t devpts devpts "$dest"/dev/pts
|
|
|
|
mount -t tmpfs tmpfs "$dest"/dev/shm
|
|
|
|
|
2024-04-09 18:55:01 +00:00
|
|
|
# workaround for shitty android-based stuff
|
|
|
|
if ! [ -f /etc/resolv.conf ]; then
|
|
|
|
# remove /etc/resolv.conf from bindpoints
|
|
|
|
bindpoints="${bindpoints#/etc/resolv.conf}"
|
|
|
|
fi
|
|
|
|
|
2023-02-25 06:41:32 +00:00
|
|
|
for bindpoint in $bindpoints; do
|
|
|
|
if [ -f "$bindpoint" ]; then
|
|
|
|
touch "$dest"/"$bindpoint"
|
|
|
|
else
|
|
|
|
mkdir -p "$dest"/"$bindpoint"
|
|
|
|
fi
|
|
|
|
|
2024-04-09 18:55:01 +00:00
|
|
|
mount -o bind "$bindpoint" "$dest"/"$bindpoint"
|
2023-02-25 06:41:32 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
# if running on chromeos, fixup symlink exec
|
|
|
|
# https://goo.gl/8xICW6
|
|
|
|
# https://github.com/dnschneid/crouton/commit/5cb7ad05
|
|
|
|
if [ -e "/run/chrome" ]; then
|
|
|
|
if mount -n -t securityfs -o nodev,noexec,nosuid securityfs /sys/kernel/security; then
|
|
|
|
policies=/sys/kernel/security/chromiumos/inode_security_policies
|
|
|
|
if [ -d "$policies" ]; then
|
|
|
|
for policy in "$policies/allow_"*; do
|
|
|
|
printf "$(realpath "$dest")" > "$policy"
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
umount /sys/kernel/security
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2024-04-09 18:55:01 +00:00
|
|
|
mount -o bind "$dest" /mnt
|
|
|
|
pivot_root /mnt /mnt/mnt
|
|
|
|
|
2023-02-25 06:41:32 +00:00
|
|
|
if [ "$command" ]; then
|
2024-04-09 18:55:01 +00:00
|
|
|
exec su $user -c "$command"
|
2023-02-25 06:41:32 +00:00
|
|
|
elif [ "$#" -gt 0 ]; then
|
2024-04-09 18:55:01 +00:00
|
|
|
exec su $user -c "$*"
|
2023-02-25 06:41:32 +00:00
|
|
|
else
|
2024-04-09 18:55:01 +00:00
|
|
|
exec login -p -f $user
|
2023-02-25 06:41:32 +00:00
|
|
|
fi
|