root-enter: upgrade to 7

This commit is contained in:
ptrcnull 2022-07-19 05:31:00 +02:00
parent db660a39c9
commit 2555cc55b0
2 changed files with 79 additions and 42 deletions

View file

@ -1,13 +1,15 @@
# Contributor: Patrycja Rosa <alpine@ptrcnull.me>
# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=root-enter
pkgver=6
pkgver=7
pkgrel=0
pkgdesc="enter chroot with mounts"
url="https://git.ddd.rip/ptrcnull/ptrcports"
arch="noarch"
license="BSD-2-Clause"
depends="execline"
source="enter"
builddir="$srcdir"
options="!check"
package() {
@ -15,5 +17,5 @@ package() {
}
sha512sums="
6e458ca9f00ce8a804bb1ff07e639a682546d3cb4e963f7fbad262625e0af74db7b94d11c2ad2fd16f7d229f4d936a80bd334216c0f1cafde4b3c650a34bf3ae enter
695da33b6a8eeca6e77e7afb6ea36f14b66d53afbb5408911905fbfaf93ef6e54f12819ef0af267919707f9e3c867f63fb905092c6909dd5fc68805e20849f2f enter
"

View file

@ -1,48 +1,83 @@
#!/bin/sh
#!/bin/execlineb
set -e
elgetopt "be:u:c:"
[ "$(id -u)" = 0 ] || DOAS="${DOAS:-doas}"
[ "$unshared" = 1 ] || exec $DOAS env unshared=1 unshare -m $0 "$@"
multisubstitute {
importas -D "0" build ELGETOPT_b
importas -D "" extra_mounts ELGETOPT_e
importas -D "" user_arg ELGETOPT_u
importas -D "" command_arg ELGETOPT_c
importas -D "" dest 1
importas -i USER USER
importas -i HOME HOME
}
bindpoints="/etc/resolv.conf"
user="patrycja"
command="ash -l"
shift
elgetpositionals
emptyenv -oP
while getopts "be:u:c:" opt; do
case $opt in
'b') bindpoints="
$bindpoints
/home/patrycja/aports
/home/patrycja/packages
/home/patrycja/.abuild
" ;;
'e') bindpoints="$bindpoints $OPTARG" ;;
'u') user=$OPTARG ;;
'c') command=$OPTARG ;;
*) echo "unknown $opt" ;;
esac
done
shift $(( $OPTIND - 1 ))
backtick -D "ash -l" command {
ifelse { test -n $command_arg } {
echo $command_arg
}
if [ ! -d "$1" ]; then
echo "no such directory: $1"
ifelse { test $# -gt 0 } {
echo $@
}
echo ash -l
}
importas -ui command command
# TODO: add -s to that when not using `su` later
backtick -D "" -E user { ifelse { test -n $user_arg } { echo $user_arg } echo $USER }
backtick -D "" -E build_mounts {
if { test $build = 1 }
echo "
${HOME}/aports
${HOME}/packages
${HOME}/.abuild
"
}
define -s -C bindpoints "/etc/resolv.conf ${build_mounts} ${extra_mounts}"
ifelse -n { test -d $dest } {
foreground { echo "no such file or directory: $dest" }
exit 1
fi
}
mount -t devtmpfs devtmpfs $1/dev
mount -t tmpfs shm $1/dev/shm
mount -t devpts devpts $1/dev/pts
mount -t sysfs sysfs $1/sys
mount -t proc proc $1/proc
mount -t tmpfs tmpfs $1/tmp
for point in $bindpoints; do
if [ -f $point ]; then
touch $1$point
else
mkdir -p $1$point
fi
mount --bind $point $1$point
done
ifelse -n { test -x ${dest}/bin/sh } {
foreground { echo "$dest does not contain executable /bin/sh" }
exit 1
}
chroot $1 /bin/sh -c "su $user -c '$command'"
backtick -E uid { id -u }
backtick -E -D "exec" sucmd { if { test $uid != 0 } echo doas }
$sucmd
unshare -m
foreground { mount -t devtmpfs devtmpfs ${dest}/dev }
foreground { mount -t tmpfs shm ${dest}/dev/shm }
foreground { mount -t devpts devpts ${dest}/dev/pts }
foreground { mount -t sysfs sysfs ${dest}/sys }
foreground { mount -t proc proc ${dest}/proc }
foreground { mount -t tmpfs tmpfs ${dest}/tmp }
foreground {
forx -E point { $bindpoints }
# ensure $point exists in destination
ifthenelse { test -f $point } {
touch ${dest}${point}
} {
mkdir -p ${dest}${point}
}
mount --bind $point ${dest}${point}
}
chroot ${dest}
su - $user -c "$command"