From 0a1d6e1cb33ba407f15c680aeac1963154f3a807 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Fri, 7 Feb 2020 21:25:10 +0100 Subject: [PATCH] scripts: dev_cli: Fix build directory permisions All our tests must be run as root and thus the build directory is owned by root after we run any of them. Start another container to fix all permissions whenever we're done with our tests. Signed-off-by: Samuel Ortiz --- scripts/dev_cli.sh | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/scripts/dev_cli.sh b/scripts/dev_cli.sh index 8441e3f40..71a9cf757 100755 --- a/scripts/dev_cli.sh +++ b/scripts/dev_cli.sh @@ -23,7 +23,8 @@ CLH_INTEGRATION_WORKLOADS="${HOME}/workloads" # Container paths CTR_CLH_ROOT_DIR="/cloud-hypervisor" -CTR_CLH_CARGO_TARGET="${CTR_CLH_ROOT_DIR}/build/cargo_target" +CTR_CLH_CARGO_BUILT_DIR="${CTR_CLH_ROOT_DIR}/build" +CTR_CLH_CARGO_TARGET="${CTR_CLH_CARGO_BUILT_DIR}/cargo_target" CTR_CLH_INTEGRATION_WORKLOADS="/root/workloads" # Cargo paths @@ -112,6 +113,25 @@ ensure_build_dir() { done } +# Fix build/ dir permissions after a container ran as root +# Since the container ran as root, any files it creates will be owned by root. +# This fixes that by recursively changing the ownership of build/ to the +# current user. +# +fix_build_dir_perms() { + # Yes, running Docker to get elevated privileges, just to chown some files + # is a dirty hack. + $DOCKER_RUNTIME run \ + --workdir "$CTR_CLH_ROOT_DIR" \ + --rm \ + --volume /dev:/dev \ + --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ + "$CTR_IMAGE" \ + chown -R "$(id -u):$(id -g)" "$CTR_CLH_CARGO_BUILT_DIR" + + return $1 +} + cmd_help() { echo "" echo "Cloud Hypervisor $(basename $0)" @@ -226,18 +246,17 @@ cmd_tests() { --volume /dev:/dev \ --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ "$CTR_IMAGE" \ - ./scripts/run_unit_tests.sh "$@" || exit $? + ./scripts/run_unit_tests.sh "$@" || fix_build_dir_perms $? || exit $? fi if [ "$cargo" = true ] ; then say "Running cargo tests..." $DOCKER_RUNTIME run \ - --user "$(id -u):$(id -g)" \ --workdir "$CTR_CLH_ROOT_DIR" \ --rm \ --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ "$CTR_IMAGE" \ - ./scripts/run_cargo_tests.sh || exit $? + ./scripts/run_cargo_tests.sh || fix_build_dir_perms $? || exit $? fi if [ "$integration" = true ] ; then @@ -251,8 +270,10 @@ cmd_tests() { --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ "$CTR_IMAGE" \ - ./scripts/run_integration_tests.sh "$@" || exit $? + ./scripts/run_integration_tests.sh "$@" || fix_build_dir_perms $? || exit $? fi + + fix_build_dir_perms $? } cmd_build-container() {