mirror of
https://github.com/fsquillace/junest.git
synced 2026-08-02 15:52:32 +00:00
Issue #93: Improve help and add test for proot access
This commit is contained in:
parent
438eeafb63
commit
e2b14e9bff
4 changed files with 58 additions and 66 deletions
25
bin/junest
25
bin/junest
|
|
@ -29,16 +29,23 @@ source "${JUNEST_BASE}/lib/core.sh"
|
|||
usage() {
|
||||
echo -e "$NAME: $DESCRIPTION"
|
||||
echo -e "Usage: $CMD [options] [--] [command]"
|
||||
echo -e "Options:"
|
||||
echo
|
||||
echo -e "Setup options:"
|
||||
echo -e "-i, --setup-from-file <image> Setup the $NAME image in ${JUNEST_HOME}"
|
||||
echo -e "-a, --arch <arch> $NAME architecture to download (x86_64, x86, arm)."
|
||||
echo -e " Defaults to the host architecture ($ARCH)"
|
||||
echo -e "-d, --delete Delete $NAME from ${JUNEST_HOME}"
|
||||
echo
|
||||
echo -e "Access options:"
|
||||
echo -e "-f, --fakeroot Run $NAME with fakeroot privileges"
|
||||
echo -e "-r, --root Run $NAME with root privileges"
|
||||
echo -e "-p, --proot-args Proot arguments"
|
||||
echo -e "-a, --arch $NAME architecture to use (x86_64, x86, arm)."
|
||||
echo -e " Defaults to the host architecture ($ARCH)"
|
||||
echo -e "-p, --proot-args <args> Proot arguments"
|
||||
echo
|
||||
echo -e "Building options:"
|
||||
echo -e "-b, --build-image Build a $NAME image (must run in ArchLinux)"
|
||||
echo -e "-n, --disable-validation Disable the $NAME image validation"
|
||||
echo -e "-d, --delete Delete $NAME from ${JUNEST_HOME}"
|
||||
echo
|
||||
echo -e "General options:"
|
||||
echo -e "-h, --help Show this help message"
|
||||
echo -e "-v, --version Show the $NAME version"
|
||||
}
|
||||
|
|
@ -173,17 +180,21 @@ function execute_operation(){
|
|||
setup_env_from_file $IMAGE_FILE
|
||||
else
|
||||
setup_env $ARCH_ARG
|
||||
unset ARCH_ARG
|
||||
fi
|
||||
elif $OPT_SETUP_FROM_FILE; then
|
||||
die "Error: The image cannot be installed since $JUNEST_HOME is not empty."
|
||||
fi
|
||||
|
||||
[ -z "${ARCH_ARG}" ] || \
|
||||
die "The option --arch cannot be specified since JuNest has already been downloaded in $JUNEST_HOME"
|
||||
|
||||
if $OPT_FAKEROOT; then
|
||||
run_env_as_fakeroot "${ARCH_ARG}" "${PROOT_ARGS}" "${ARGS[@]}"
|
||||
run_env_as_fakeroot "${PROOT_ARGS}" "${ARGS[@]}"
|
||||
elif $OPT_ROOT; then
|
||||
run_env_as_root "${ARGS[@]}"
|
||||
else
|
||||
run_env_as_user "${ARCH_ARG}" "${PROOT_ARGS}" "${ARGS[@]}"
|
||||
run_env_as_user "${PROOT_ARGS}" "${ARGS[@]}"
|
||||
fi
|
||||
}
|
||||
|
||||
|
|
|
|||
31
lib/core.sh
31
lib/core.sh
|
|
@ -238,23 +238,22 @@ function _run_env_with_proot(){
|
|||
}
|
||||
|
||||
function _run_env_with_qemu(){
|
||||
local proot_args="$2"
|
||||
if [ "$1" != "" ] && [ "$1" != "$ARCH" ]
|
||||
then
|
||||
local qemu_bin="/tmp/qemu-$1-static-$ARCH-$RANDOM"
|
||||
trap - QUIT EXIT ABRT KILL TERM INT
|
||||
trap "[ -e ${qemu_bin} ] && rm_cmd -f ${qemu_bin}" EXIT QUIT ABRT KILL TERM INT
|
||||
local proot_args="$1"
|
||||
source ${JUNEST_HOME}/etc/junest/info
|
||||
|
||||
if [ "$JUNEST_ARCH" != "$ARCH" ]
|
||||
then
|
||||
local qemu_bin="qemu-$JUNEST_ARCH-static-$ARCH"
|
||||
local qemu_symlink="/tmp/${qemu_bin}-$RANDOM"
|
||||
trap - QUIT EXIT ABRT KILL TERM INT
|
||||
trap "[ -e ${qemu_symlink} ] && rm_cmd -f ${qemu_symlink}" EXIT QUIT ABRT KILL TERM INT
|
||||
|
||||
contains_element $1 "${ARCH_LIST[@]}" || \
|
||||
die "The architecture is not one of: ${ARCH_LIST[@]}"
|
||||
[ -e "${JUNEST_HOME}/opt/qemu/qemu-$1-static-$ARCH" ] || \
|
||||
die "The JuNest image in ${JUNEST_HOME} is not an $1 architecture"
|
||||
warn "Emulating $NAME via QEMU..."
|
||||
[ -e ${qemu_bin} ] || \
|
||||
ln_cmd -s ${JUNEST_HOME}/opt/qemu/qemu-$1-static-$ARCH ${qemu_bin}
|
||||
proot_args="-q ${qemu_bin} $2"
|
||||
[ -e ${qemu_symlink} ] || \
|
||||
ln_cmd -s ${JUNEST_HOME}/opt/qemu/${qemu_bin} ${qemu_symlink}
|
||||
proot_args="-q ${qemu_symlink} $proot_args"
|
||||
fi
|
||||
shift 2
|
||||
shift
|
||||
_run_env_with_proot "$proot_args" "${@}"
|
||||
}
|
||||
|
||||
|
|
@ -262,14 +261,14 @@ function run_env_as_fakeroot(){
|
|||
(( EUID == 0 )) && \
|
||||
die "You cannot access with root privileges. Use --root option instead."
|
||||
[ ! -e ${JUNEST_HOME}/etc/mtab ] && ln_cmd -s /proc/self/mounts ${JUNEST_HOME}/etc/mtab
|
||||
_run_env_with_qemu "$1" "-S ${JUNEST_HOME} $2" "${@:3}"
|
||||
_run_env_with_qemu "-S ${JUNEST_HOME} $1" "${@:2}"
|
||||
}
|
||||
|
||||
function run_env_as_user(){
|
||||
(( EUID == 0 )) && \
|
||||
die "You cannot access with root privileges. Use --root option instead."
|
||||
[ -e ${JUNEST_HOME}/etc/mtab ] && rm_cmd -f ${JUNEST_HOME}/etc/mtab
|
||||
_run_env_with_qemu "$1" "-R ${JUNEST_HOME} $2" "${@:3}"
|
||||
_run_env_with_qemu "-R ${JUNEST_HOME} $1" "${@:2}"
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -27,21 +27,17 @@ function setup_env(){
|
|||
echo "setup_env"
|
||||
}
|
||||
function run_env_as_fakeroot(){
|
||||
local arch_arg="$1"
|
||||
local proot_args="$2"
|
||||
local proot_args="$1"
|
||||
shift
|
||||
shift
|
||||
echo "run_env_as_fakeroot($arch_arg,$proot_args,$@)"
|
||||
echo "run_env_as_fakeroot($proot_args,$@)"
|
||||
}
|
||||
function run_env_as_root(){
|
||||
echo "run_env_as_root $@"
|
||||
}
|
||||
function run_env_as_user(){
|
||||
local arch_arg="$1"
|
||||
local proot_args="$2"
|
||||
local proot_args="$1"
|
||||
shift
|
||||
shift
|
||||
echo "run_env_as_user($arch_arg,$proot_args,$@)"
|
||||
echo "run_env_as_user($proot_args,$@)"
|
||||
}
|
||||
|
||||
function wrap_env(){
|
||||
|
|
@ -76,31 +72,31 @@ function test_delete_env(){
|
|||
}
|
||||
function test_run_env_as_fakeroot(){
|
||||
local output=$(wrap_env -f)
|
||||
assertEquals $output "run_env_as_fakeroot(,,)"
|
||||
assertEquals $output "run_env_as_fakeroot(,)"
|
||||
local output=$(wrap_env --fakeroot)
|
||||
assertEquals $output "run_env_as_fakeroot(,,)"
|
||||
assertEquals $output "run_env_as_fakeroot(,)"
|
||||
|
||||
local output=$(wrap_env -f -p "-b arg")
|
||||
assertEquals "${output[@]}" "run_env_as_fakeroot(,-b arg,)"
|
||||
assertEquals "${output[@]}" "run_env_as_fakeroot(-b arg,)"
|
||||
local output=$(wrap_env -f -p "-b arg" -- command -kv)
|
||||
assertEquals "${output[@]}" "run_env_as_fakeroot(,-b arg,command -kv)"
|
||||
assertEquals "${output[@]}" "run_env_as_fakeroot(-b arg,command -kv)"
|
||||
local output=$(wrap_env -f command --as)
|
||||
assertEquals "${output[@]}" "run_env_as_fakeroot(,,command --as)"
|
||||
local output=$(wrap_env -a "myarch" -f command --as)
|
||||
assertEquals "${output[@]}" "run_env_as_fakeroot(myarch,,command --as)"
|
||||
assertEquals "${output[@]}" "run_env_as_fakeroot(,command --as)"
|
||||
$(wrap_env -a "myarch" -f command --as 2> /dev/null)
|
||||
assertEquals 1 $?
|
||||
}
|
||||
function test_run_env_as_user(){
|
||||
local output=$(wrap_env)
|
||||
assertEquals $output "run_env_as_user(,,)"
|
||||
assertEquals $output "run_env_as_user(,)"
|
||||
|
||||
local output=$(wrap_env -p "-b arg")
|
||||
assertEquals "$output" "run_env_as_user(,-b arg,)"
|
||||
assertEquals "$output" "run_env_as_user(-b arg,)"
|
||||
local output=$(wrap_env -p "-b arg" -- command -ll)
|
||||
assertEquals "$output" "run_env_as_user(,-b arg,command -ll)"
|
||||
assertEquals "$output" "run_env_as_user(-b arg,command -ll)"
|
||||
local output=$(wrap_env command -ls)
|
||||
assertEquals "$output" "run_env_as_user(,,command -ls)"
|
||||
local output=$(wrap_env -a "myarch" -- command -ls)
|
||||
assertEquals "$output" "run_env_as_user(myarch,,command -ls)"
|
||||
assertEquals "$output" "run_env_as_user(,command -ls)"
|
||||
$(wrap_env -a "myarch" -- command -ls 2> /dev/null)
|
||||
assertEquals 1 $?
|
||||
}
|
||||
function test_run_env_as_root(){
|
||||
local output=$(wrap_env -r)
|
||||
|
|
|
|||
|
|
@ -249,28 +249,21 @@ function test_run_env_as_junest_root(){
|
|||
|
||||
function test_run_env_as_user(){
|
||||
install_mini_env
|
||||
local output=$(run_env_as_user "" "-k 3.10" "/usr/bin/mkdir" "-v" "/newdir2" | awk -F: '{print $1}')
|
||||
local output=$(run_env_as_user "-k 3.10" "/usr/bin/mkdir" "-v" "/newdir2" | awk -F: '{print $1}')
|
||||
assertEquals "$output" "/usr/bin/mkdir"
|
||||
assertTrue "[ -e $JUNEST_HOME/newdir2 ]"
|
||||
|
||||
SH=("/usr/bin/mkdir" "-v" "/newdir")
|
||||
local output=$(run_env_as_user "" "-k 3.10" | awk -F: '{print $1}')
|
||||
local output=$(run_env_as_user "-k 3.10" | awk -F: '{print $1}')
|
||||
assertEquals "$output" "/usr/bin/mkdir"
|
||||
assertTrue "[ -e $JUNEST_HOME/newdir ]"
|
||||
|
||||
$(run_env_as_user "noarch" "-k 3.10" "mycommand" 2> /dev/null)
|
||||
assertEquals 1 $?
|
||||
|
||||
local different_arch=(${ARCH_LIST[@]/$ARCH})
|
||||
$(run_env_as_user "${different_arch[0]}" "-k 3.10" "mycommand" 2> /dev/null)
|
||||
assertEquals 1 $?
|
||||
}
|
||||
|
||||
function test_run_env_as_proot_mtab(){
|
||||
install_mini_env
|
||||
$(run_env_as_fakeroot "" "-k 3.10" "echo")
|
||||
$(run_env_as_fakeroot "-k 3.10" "echo")
|
||||
assertTrue "[ -e $JUNEST_HOME/etc/mtab ]"
|
||||
$(run_env_as_user "" "-k 3.10" "echo")
|
||||
$(run_env_as_user "-k 3.10" "echo")
|
||||
assertTrue "[ ! -e $JUNEST_HOME/etc/mtab ]"
|
||||
}
|
||||
|
||||
|
|
@ -287,19 +280,19 @@ function test_run_env_as_root_mtab(){
|
|||
|
||||
function test_run_env_with_quotes(){
|
||||
install_mini_env
|
||||
local output=$(run_env_as_user "" "-k 3.10" "bash" "-c" "/usr/bin/mkdir -v /newdir2" | awk -F: '{print $1}')
|
||||
local output=$(run_env_as_user "-k 3.10" "bash" "-c" "/usr/bin/mkdir -v /newdir2" | awk -F: '{print $1}')
|
||||
assertEquals "/usr/bin/mkdir" "$output"
|
||||
assertTrue "[ -e $JUNEST_HOME/newdir2 ]"
|
||||
}
|
||||
|
||||
function test_run_env_as_user_proot_args(){
|
||||
install_mini_env
|
||||
run_env_as_user "" "--help" "" &> /dev/null
|
||||
run_env_as_user "--help" "" &> /dev/null
|
||||
assertEquals 0 $?
|
||||
|
||||
mkdir $JUNEST_TEMPDIR/newdir
|
||||
touch $JUNEST_TEMPDIR/newdir/newfile
|
||||
run_env_as_user "" "-b $JUNEST_TEMPDIR/newdir:/newdir -k 3.10" "ls" "-l" "/newdir/newfile" &> /dev/null
|
||||
run_env_as_user "-b $JUNEST_TEMPDIR/newdir:/newdir -k 3.10" "ls" "-l" "/newdir/newfile" &> /dev/null
|
||||
assertEquals 0 $?
|
||||
|
||||
$(_run_env_with_proot --helps 2> /dev/null)
|
||||
|
|
@ -341,15 +334,8 @@ function test_run_proot_seccomp(){
|
|||
|
||||
function test_run_env_as_fakeroot(){
|
||||
install_mini_env
|
||||
local output=$(run_env_as_fakeroot "" "-k 3.10" "id" | awk '{print $1}')
|
||||
local output=$(run_env_as_fakeroot "-k 3.10" "id" | awk '{print $1}')
|
||||
assertEquals "uid=0(root)" "$output"
|
||||
|
||||
$(run_env_as_fakeroot "noarch" "-k 3.10" "mycommand" 2> /dev/null)
|
||||
assertEquals 1 $?
|
||||
|
||||
local different_arch=(${ARCH_LIST[@]/$ARCH})
|
||||
$(run_env_as_fakeroot "${different_arch[0]}" "-k 3.10" "mycommand" 2> /dev/null)
|
||||
assertEquals 1 $?
|
||||
}
|
||||
|
||||
function test_delete_env(){
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue