Occlum安装使用


Occlum是一个Intel SGX内存安全、多进程库操作系统(LibOS)。作为一个libOS,它使得合法应用程序在SGX上运行,几乎不需要修改源代码,从而透明地保护用户工作负载的机密性和完整性。

一、Docker部署

1.1 硬件支持

首先,请确保裸机或虚拟机支持SGX。否则,用户只能尝试SW仿真模式。
为了更好地用户体验,要求带有FLC的SGX1或SGX2,用户可以使用cpuid命令检测硬件是否满足Occlum要求。

#SGX2
cpuid | grep SGX2
#FLC
cpuid | grep SGX_LC

注:暂无硬件支持,我们以仿真模式为例。

1.2 环境

实验环境:Ubuntu 22.04.3 LTS
Docker version 20.10.10, build b4856D36

1.3 拉取镜像创建虚机

# 1. Create softlinks on host
mkdir -p /dev/sgx
ln -sf ../sgx_enclave /dev/sgx/enclave
ln -sf ../sgx_provision /dev/sgx/provision
# 2. Create container in two methods:
# (1) With privileged mode特权模式
docker run -it --privileged -v /dev/sgx:/dev/sgx occlum/occlum:latest-ubuntu20.04
# (2) With non-privileged mode非特权模式
docker run -it --device /dev/sgx/enclave --device /dev/sgx/provision occlum/occlum:latest-ubuntu20.04

1.4 简单验证

SGX SDK提供了简单的样例来简单验证SGX是否正常工作。

cd /opt/intel/sgxsdk/SampleCode/SampleEnclave 

仿真模式下,需要将该目录下的Makefile中第35行SGX_MODE修改为SIM。

#Makefile中
35 SGX_MODE ?= SIM
make && ./app

在这里插入图片描述
到此,验证ok。Occlum自带的demo位置在/root/demos。

二、Occlum中gcc编译

以Hello World为例,展示如何在Occlum中编译SGX APP。

#include <stdio.h>

int main() {
    printf("Hello World\n");
    return 0;
}

2.1 交叉编译

occlum-gcc -o hello_world hello_world.c
./hello_world

使用occlum-gcc编译时,生成的二进制文件将基于musl-libc库。
请注意,Occlum工具链并非传统意义上的交叉编译:Occlum工具链构建的二进制文件也可在Linux上运行。此属性使得编译、调试和测试用于Occlum的用户程序变得更加方便。

2.2 初始化Occlum实例

mkdir occlum_instance && cd occlum_instance
occlum init

或者

occlum new occlum_instance

occlum init命令在当前工作目录中创建Occlum的编译时和运行时状态。
occlum new命令基本上做了同样的事情,但是在一个新的实例目录中。
每个Occlum实例目录应用于应用程序的单个实例;多个应用程序或单个应用程序的不同实例应使用不同的Occlum实例。

2.3 Occlum构建

cp ../hello_world image/bin/
#仿真模式需要加参数 --sgx-mode SIM
occlum build --sgx-mode SIM

image目录的内容由occlum init命令初始化。镜像目录的结构模仿普通的UNIX FS,包含/bin、/lib、/root、/tmp等目录。将用户程序hello_world复制到镜像/bin/后,通过occlum build命令打包镜像目录,以生成安全的Occlum FS镜像以及Occlum SGX enclave。

2.4 运行

occlum run /bin/hello_world

occlum run命令启动一个Occlum SGX enclave,在幕后验证并加载相关的Occlum FS镜像,产生一个新的LibOS进程来执行/bin/hello_world,最终打印出消息。

三、Occlum中go编译

以Hello World为例,展示如何在Occlum中编译SGX APP。我们需要go_hello.go(源码)、go_hello.yaml(配置文件)和run_go_hello.sh(运行脚本)。

go_hello.go

package main

import (
        "fmt"
        _ "go-sqlite3"
)

func main() {
        fmt.Println("Hello World")
}

go_hello.yaml

includes:
  - base.yaml
targets:
  - target: /bin
    copy:
      - files:
        - ../go_hello

run_go_hello.sh

#!/bin/bash
set -e

BLUE='\033[1;34m'
NC='\033[0m'

rm -f go.mod  && occlum-go mod init go_hello_instance

occlum-go build -o go_hello go_hello.go

# Init Occlum Workspace
rm -rf go_hello_instance && mkdir go_hello_instance
cd go_hello_instance
occlum init
new_json="$(jq '.resource_limits.user_space_size = "2560MB" |
        .resource_limits.kernel_space_heap_size="320MB" |
        .resource_limits.kernel_space_stack_size="10MB" |
        .process.default_stack_size = "40MB" |
        .process.default_heap_size = "320MB" ' Occlum.json)" && \
echo "${new_json}" > Occlum.json

# Copy program into Occlum Workspace and build
rm -rf image && \
copy_bom -f ../go_hello.yaml --root image --include-dir /opt/occlum/etc/template && \
occlum build --sgx-mode SIM

echo -e "${BLUE}occlum run /bin/go_hello${NC}"
time occlum run /bin/go_hello                              

运行脚本之后,效果如下:
在这里插入图片描述
在这里插入图片描述
注:go-sqlite3这个包需要导入。

三、Occlum中python编译

以Hello World为例,展示如何在Occlum中编译SGX APP。我们需要
hello.py

#!/usr/bin/python
# -*- coding: UTF-8 -*-

print("hello world!")

python-hello.yaml

includes:
  - base.yaml
targets:
  - target: /bin
    createlinks:
      - src: /opt/python-occlum/bin/python3
        linkname: python3
  # python packages
  - target: /opt
    copy:
      - dirs:
          - ../python-occlum
  # below are python code and data
  - target: /
    copy:
      - from: ..
        files:
          - hello.py                

run_python_hello.sh

#!/bin/bash
set -e

BLUE='\033[1;34m'
NC='\033[0m'

script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}"  )" >/dev/null 2>&1 && pwd )"
python_dir="$script_dir/occlum_instance/image/opt/python-occlum"

[ -d occlum_instance ] || occlum new occlum_instance

cd occlum_instance && rm -rf image
copy_bom -f ../python-hello.yaml --root image --include-dir /opt/occlum/etc/template

if [ ! -d $python_dir ];then
    echo "Error: cannot stat '$python_dir' directory"
    exit 1
fi

new_json="$(jq '.resource_limits.user_space_size = "640MB" |
        .resource_limits.kernel_space_heap_size = "300MB" |
        .env.default += ["PYTHONHOME=/opt/python-occlum"]' Occlum.json)" && \
echo "${new_json}" > Occlum.json
occlum build --sgx-mode SIM

# Run the python demo
echo -e "${BLUE}occlum run /bin/python3 hello.py${NC}"
occlum run /bin/python3 hello.py

执行脚本之后,效果如下:
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Occlum 是一个支持运行 Linux 应用程序的轻量级容器化解决方案,它可以在安全的环境中运行 Linux 应用程序。下面是 occlum 使用教程: ## 安装 Occlum 首先需要安装 Occlum,官方提供了一键安装脚本,可以在任何支持 bash 的 Linux 系统上安装 Occlum: ``` $ wget https://github.com/occlum/occlum/releases/download/v0.20.0/occlum_0.20.0-ubuntu18.04_amd64.deb $ sudo dpkg -i occlum_0.20.0-ubuntu18.04_amd64.deb ``` 安装完成后,可以使用 occlum 命令来验证是否安装成功。 ## 创建 Occlum 工作区 使用 Occlum 需要先创建一个工作区,这个工作区包含所有需要在 Occlum 中运行的文件和配置。 可以使用以下命令创建一个默认的 Occlum 工作区: ``` $ occlum init ``` 执行此命令后,会在当前目录下创建一个名为 `occlum_instance` 的文件夹,该文件夹包含一个默认配置文件 `config.json`,和一些必要的文件夹和文件。 ## 运行应用程序 在创建了 Occlum 工作区后,就可以在其中运行 Linux 应用程序了。 以下是在 Occlum 中运行应用程序的步骤: 1. 将应用程序及其依赖项复制到 Occlum 工作区中。 ``` $ mkdir -p occlum_instance/rootfs $ cp /path/to/your/app occlum_instance/rootfs/ $ sudo occlum build ``` 2. 在创建的 Occlum 工作区中启动应用程序。 ``` $ occlum run /your/app ``` 以上就是使用 Occlum 的基本步骤,当然还可以通过修改配置文件来设置更多的参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值