320 lines
8.6 KiB
Bash
Executable File
320 lines
8.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
this_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
|
|
occlum_dir="$( cd "$( dirname "$this_dir/../../../" )" >/dev/null 2>&1 && pwd )"
|
|
build_makefile=$occlum_dir/build/bin/occlum_build.mk
|
|
|
|
if [[ "$occlum_dir" == "/opt/occlum" ]]; then
|
|
version_header=$occlum_dir/include/occlum_version.h
|
|
else
|
|
version_header=$occlum_dir/src/pal/include/occlum_version.h
|
|
fi
|
|
|
|
major_ver=`grep '\#define OCCLUM_MAJOR_VERSION' $version_header | awk '{print $3}'`
|
|
minor_ver=`grep '\#define OCCLUM_MINOR_VERSION' $version_header | awk '{print $3}'`
|
|
patch_ver=`grep '\#define OCCLUM_PATCH_VERSION' $version_header | awk '{print $3}'`
|
|
occlum_version="$major_ver.$minor_ver.$patch_ver"
|
|
|
|
instance_dir=`pwd`
|
|
|
|
status_file=$instance_dir/.__occlum_status
|
|
|
|
SGX_SDK="${SGX_SDK:-/opt/intel/sgxsdk}"
|
|
SGX_GDB="$SGX_SDK/bin/sgx-gdb"
|
|
ENCLAVE_SIGN_TOOL="$SGX_SDK/bin/x64/sgx_sign"
|
|
ENCLAVE_SIGN_KEY="$occlum_dir/etc/template/Enclave.pem"
|
|
|
|
exit_error() {
|
|
echo "Error: $@" >&2
|
|
exit 1
|
|
}
|
|
|
|
report_arg_error() {
|
|
echo $1 >&2
|
|
echo ""
|
|
cat <<EOF
|
|
Usage:
|
|
occlum new <path>
|
|
Create a new directory at <path> and initialize as the Occlum instance.
|
|
|
|
occlum init
|
|
Initialize a directory as the Occlum instance.
|
|
|
|
occlum build [--sign-key <key_path>] [--sign-tool <tool_path>] [-f/--force]
|
|
Build and sign an Occlum SGX enclave (.so) and generate its associated secure FS image
|
|
according to the user-provided image directory and Occlum.json config file.
|
|
The whole building process is incremental: the building artifacts are built only when needed.
|
|
To force rebuilding all artifacts, give the [-f/--force] flag.
|
|
|
|
occlum run <program_name> <program_args>
|
|
Run the user program inside an SGX enclave.
|
|
To run the enclave in SGX hardware release mode, use:
|
|
OCCLUM_RELEASE_ENCLAVE=1 occlum run <program_name> <program_args>
|
|
|
|
occlum gdb <program_name> <program_args>
|
|
Debug the program running inside an SGX enclave with GDB.
|
|
EOF
|
|
}
|
|
|
|
check_has_init() {
|
|
if [ ! -f "$status_file" ]; then
|
|
echo "Error: the current working directory is not initialized as an Occlum instance. Need to run \"occlum init\" first."
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
check_has_built() {
|
|
check_has_init
|
|
|
|
if [ ! -d "$instance_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_new() {
|
|
if [ -z $@ ]; then
|
|
echo "Error: target directory is not set"
|
|
exit 1
|
|
fi
|
|
|
|
dir_path="$@"
|
|
if [[ "$dir_path" != "/"* ]]; then
|
|
dir_path="$instance_dir/$@"
|
|
fi
|
|
|
|
if [[ -e "$dir_path" ]]; then
|
|
echo "Error: destination \"$dir_path\" already exists"
|
|
exit 1
|
|
fi
|
|
|
|
mkdir -p $dir_path
|
|
instance_dir=$dir_path
|
|
status_file=$instance_dir/.__occlum_status
|
|
cd $dir_path && cmd_init
|
|
}
|
|
|
|
cmd_init() {
|
|
if [ -f "$status_file" ]; then
|
|
echo "Error: the current working directory has been initialized as an Occlum instance"
|
|
exit 1
|
|
fi
|
|
|
|
echo "initialized" > $status_file
|
|
|
|
cd "$instance_dir"
|
|
mkdir -p image
|
|
mkdir -p image/bin
|
|
mkdir -p image/lib
|
|
mkdir -p image/root
|
|
mkdir -p image/host
|
|
mkdir -p image/tmp
|
|
|
|
local occlum_gcc_lib=/usr/local/occlum/x86_64-linux-musl/lib
|
|
cp -t image/lib/ \
|
|
/lib/ld-musl-x86_64.so.1 \
|
|
"$occlum_gcc_lib/libc.so" \
|
|
"$occlum_gcc_lib/libstdc++.so.6" \
|
|
"$occlum_gcc_lib/libgcc_s.so.1" \
|
|
"$occlum_gcc_lib/libgomp.so.1"
|
|
|
|
cp "$occlum_dir"/etc/template/Occlum.json "$instance_dir"/
|
|
chmod 644 "$instance_dir"/Occlum.json
|
|
|
|
echo "$instance_dir initialized as an Occlum instance"
|
|
}
|
|
|
|
cmd_build() {
|
|
check_has_init
|
|
|
|
pal_lib=libocclum-pal.so
|
|
libos_lib=libocclum-libos.so
|
|
|
|
while [ -n "$1" ]; do
|
|
case "$1" in
|
|
--sign-key) [ -n "$2" ] && ENCLAVE_SIGN_KEY=$2 ; shift 2 || exit_error "empty signing key path" ;;
|
|
--sign-tool) [ -n "$2" ] && ENCLAVE_SIGN_TOOL=$2 ; shift 2 || exit_error "empty signing tool path" ;;
|
|
--sgx-mode) [[ -n "$2" && "$2" != "HW" ]] && export SGX_MODE=SIM ; shift 2 || exit_error "empty sgx mode";;
|
|
--force | -f) MAKE_OPTION="--always-make" ; shift ;;
|
|
*) exit_error "Unknown option: $1" ;;
|
|
esac
|
|
done
|
|
[ -e "$ENCLAVE_SIGN_KEY" ] || exit_error "invalid signing key path: $ENCLAVE_SIGN_KEY"
|
|
[ -e "$ENCLAVE_SIGN_TOOL" ] || exit_error "invalid signing tool path: $ENCLAVE_SIGN_TOOL"
|
|
echo "Enclave sign-tool: $ENCLAVE_SIGN_TOOL"
|
|
echo "Enclave sign-key: $ENCLAVE_SIGN_KEY"
|
|
|
|
if [[ -n $SGX_MODE && "$SGX_MODE" != "HW" ]]; then
|
|
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$SGX_SDK/sdk_libs
|
|
pal_lib=libocclum-pal_sim.so
|
|
libos_lib=libocclum-libos_sim.so
|
|
echo "SGX mode: $SGX_MODE"
|
|
else
|
|
echo "SGX mode: HW"
|
|
fi
|
|
|
|
# If sgx mode is changed, build thoroughly again
|
|
if [[ -n $SGX_MODE && "$SGX_MODE" != "HW" ]]; then
|
|
if [ "$(cat $instance_dir/.sgx_mode 2>/dev/null)" != "SIM" ]; then
|
|
MAKE_OPTION="--always-make"
|
|
fi
|
|
else
|
|
#HW mode
|
|
if [ "$(cat $instance_dir/.sgx_mode 2>/dev/null)" != "HW" ]; then
|
|
MAKE_OPTION="--always-make"
|
|
fi
|
|
fi
|
|
|
|
rm -rf "$instance_dir/run"
|
|
|
|
occlum_dir=$occlum_dir instance_dir=$instance_dir pal_lib=$pal_lib major_ver=$major_ver \
|
|
occlum_version=$occlum_version libos_lib=$libos_lib ENCLAVE_SIGN_KEY=$ENCLAVE_SIGN_KEY \
|
|
ENCLAVE_SIGN_TOOL=$ENCLAVE_SIGN_TOOL \
|
|
make -f $build_makefile $MAKE_OPTION
|
|
|
|
cd "$instance_dir"
|
|
echo "built" > $status_file
|
|
|
|
if [[ -n $SGX_MODE && "$SGX_MODE" != "HW" ]]; then
|
|
echo "SIM" > .sgx_mode
|
|
else
|
|
echo "HW" > .sgx_mode
|
|
fi
|
|
|
|
mkdir -p "$instance_dir/run/mount/__ROOT"
|
|
mkdir -p "$instance_dir/run/mount/tmp"
|
|
|
|
echo "Built the Occlum image and enclave successfully"
|
|
}
|
|
|
|
cmd_run() {
|
|
check_has_built
|
|
|
|
SGX_MODE=$(cat $instance_dir/.sgx_mode)
|
|
if [[ -n $SGX_MODE && "$SGX_MODE" != "HW" ]]; then
|
|
export LD_LIBRARY_PATH="$instance_dir/build/lib:$SGX_SDK/sdk_libs/"
|
|
else
|
|
export LD_LIBRARY_PATH="$instance_dir/build/lib"
|
|
fi
|
|
|
|
echo "running" > $status_file
|
|
|
|
RUST_BACKTRACE=1 "$instance_dir/build/bin/occlum-run" "$@"
|
|
|
|
echo "built" > $status_file
|
|
}
|
|
|
|
cmd_start() {
|
|
check_has_built
|
|
|
|
SGX_MODE=$(cat $instance_dir/.sgx_mode)
|
|
if [[ -n $SGX_MODE && "$SGX_MODE" != "HW" ]]; then
|
|
export LD_LIBRARY_PATH="$instance_dir/build/lib:$SGX_SDK/sdk_libs/"
|
|
else
|
|
export LD_LIBRARY_PATH="$instance_dir/build/lib"
|
|
fi
|
|
|
|
echo "running" > $status_file
|
|
|
|
RUST_BACKTRACE=1 "$instance_dir/build/bin/occlum_exec_client" start
|
|
|
|
echo "built" > $status_file
|
|
}
|
|
|
|
cmd_exec() {
|
|
check_has_built
|
|
|
|
SGX_MODE=$(cat $instance_dir/.sgx_mode)
|
|
if [[ -n $SGX_MODE && "$SGX_MODE" != "HW" ]]; then
|
|
export LD_LIBRARY_PATH="$instance_dir/build/lib:$SGX_SDK/sdk_libs/"
|
|
else
|
|
export LD_LIBRARY_PATH="$instance_dir/build/lib"
|
|
fi
|
|
|
|
echo "running" > "$status_file"
|
|
|
|
RUST_BACKTRACE=1 "$instance_dir/build/bin/occlum_exec_client" exec -- "$@"
|
|
|
|
echo "built" > "$status_file"
|
|
}
|
|
|
|
cmd_stop() {
|
|
check_has_built
|
|
|
|
SGX_MODE=$(cat $instance_dir/.sgx_mode)
|
|
if [[ -n $SGX_MODE && "$SGX_MODE" != "HW" ]]; then
|
|
export LD_LIBRARY_PATH="$instance_dir/build/lib:$SGX_SDK/sdk_libs/"
|
|
else
|
|
export LD_LIBRARY_PATH="$instance_dir/build/lib"
|
|
fi
|
|
|
|
echo "running" > "$status_file"
|
|
|
|
RUST_BACKTRACE=1 "$instance_dir/build/bin/occlum_exec_client" stop -t 0
|
|
|
|
echo "built" > "$status_file"
|
|
}
|
|
|
|
cmd_gdb() {
|
|
check_has_built
|
|
|
|
SGX_MODE=$(cat $instance_dir/.sgx_mode)
|
|
if [[ -n $SGX_MODE && "$SGX_MODE" != "HW" ]]; then
|
|
export LD_LIBRARY_PATH="$instance_dir/build/lib:$SGX_SDK/sdk_libs/"
|
|
else
|
|
export LD_LIBRARY_PATH="$instance_dir/build/lib"
|
|
fi
|
|
|
|
echo "debugging" > "$status_file"
|
|
|
|
OCCLUM_GDB=1 $SGX_GDB --args "$instance_dir/build/bin/occlum-run" "$@"
|
|
|
|
echo "built" > "$status_file"
|
|
}
|
|
|
|
cmd_status() {
|
|
cat "$status_file"
|
|
}
|
|
|
|
set -e
|
|
|
|
if [[ ( "$#" < 1 ) ]] ; then
|
|
report_arg_error "Error: no sub-command is given"
|
|
exit 1
|
|
fi
|
|
|
|
cmd=$1
|
|
case "$cmd" in
|
|
new)
|
|
cmd_new "${@:2:1}"
|
|
;;
|
|
init)
|
|
cmd_init
|
|
;;
|
|
build)
|
|
cmd_build "${@:2}"
|
|
;;
|
|
run)
|
|
cmd_run "${@:2}"
|
|
;;
|
|
start)
|
|
cmd_start
|
|
;;
|
|
exec)
|
|
cmd_exec "${@:2}"
|
|
;;
|
|
stop)
|
|
cmd_stop
|
|
;;
|
|
gdb)
|
|
cmd_gdb "${@:2}"
|
|
;;
|
|
status)
|
|
cmd_status
|
|
;;
|
|
*)
|
|
report_arg_error "Error: unknown sub-command $cmd"
|
|
exit 1
|
|
esac
|