From 60b1e2c28d5419b715d8b03a9b4c5cdb161134fc Mon Sep 17 00:00:00 2001 From: "Tate, Hongliang Tian" Date: Thu, 26 Mar 2020 08:08:03 +0000 Subject: [PATCH] Make the command line tool more robust The CLI tool is robust in the sense that it can handle the execution of init/build/run/gdb commands in any order (as long as the commands are invoked sequentially, not concurrently). --- tools/occlum | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/tools/occlum b/tools/occlum index a8bb2e72..5758b225 100755 --- a/tools/occlum +++ b/tools/occlum @@ -76,10 +76,30 @@ parse_occlum_user_space_size() { numfmt --from=iec ${size_with_unit::-1} } +check_has_init() { + if [ ! -d "$context_dir" ]; then + echo "Error: the current working directory is not initialized as an Occlum context. Need to run \"occlum init\" first." + exit 1 + fi +} + +check_has_built() { + check_has_init + + if [ ! -d "$context_dir/run/mount/root" ]; then + echo "Error: the Occlum image and enclave are not built yet. Need to run \"occlum build\" first." + exit 1 + fi +} + cmd_init() { - cd "$working_dir" - mkdir -p .occlum + if [ -d "$context_dir" ]; then + echo "Error: the current working directory has been initialized as an Occlum context" + exit 1 + fi + mkdir "$context_dir" + cd "$context_dir" echo "initialized" > status @@ -108,6 +128,8 @@ cmd_init() { } cmd_build() { + check_has_init + build_dir=build while [ -n "$1" ]; do @@ -135,6 +157,7 @@ cmd_build() { echo "building" > status rm -rf build + rm -rf run mkdir -p build/bin cp "$occlum_dir/$build_dir/bin/occlum-run" build/bin/ @@ -188,18 +211,20 @@ cmd_build() { cd "$context_dir" echo "built" > status - mkdir -p "$context_dir/run/mount/root" - if [[ -n $SGX_MODE && "$SGX_MODE" != "HW" ]]; then echo "SIM" > .sgx_mode else echo "HW" > .sgx_mode fi + mkdir -p "$context_dir/run/mount/root" + echo "Built the Occlum image and enclave successfully" } cmd_run() { + check_has_built + SGX_MODE=$(cat $context_dir/.sgx_mode) if [[ -n $SGX_MODE && "$SGX_MODE" != "HW" ]]; then export LD_LIBRARY_PATH="$context_dir/build/lib:$SGX_SDK/sdk_libs/" @@ -216,6 +241,8 @@ cmd_run() { } cmd_gdb() { + check_has_built + SGX_MODE=$(cat $context_dir/.sgx_mode) if [[ -n $SGX_MODE && "$SGX_MODE" != "HW" ]]; then export LD_LIBRARY_PATH="$context_dir/build/lib:$SGX_SDK/sdk_libs/"