QingTian Enclave:docker 容器中管理Enclave的生命周期
背景
当前项目需要在容器里对QingTian Enclave的生命周期进行管理。
准备工作
参考: https://bbs.huaweicloud.com/blogs/451408 购买c7t
虚拟机,在虚拟机上安装必要的外围包组件,以HCE操作系统为例:
yum install qingtian-tool virtio-qtbox qt-enclave-bootstrap -y
执行隔离服务
隔离必要资源预留给QingTian Enclave使用,当前只能在父虚拟机上操作,以配置4 vCPU、8G资源预留为例子:
vim /etc/qingtian/enclave/qt-enclave-env.conf
配置文件为:
# qingtian enclave configuration file.
# Which hugepage size to reserve for qingtian enclave
# can only configure it by 2 (2MB hugepage size) or 1024 (1GB hugepage size)
hugepage_size:1024
# How much memory to allocate for qingtian enclave (in MiB).
memory_mib:8192
# User can use cpu_count to set how many CPUs need to be reserved.
# Or cpu_list to set which CPUs need to be reserved.
#
# cpu_count and cpu_list conflict with each other. Only use exactly one of them.
#
# How many CPUs to reserve for qingtian enclave.
#cpu_count:4
# Which CPUs to reserve for qingtian enclave. You can configure it like below:
# 2,3 means reserving CPUs 2, 3, and you can also use 2-5 to reserve 2 through 5.
cpu_list:2,3,6,7
启动隔离服务:
systemctl start qt-enclave-env
Note:如果隔离失败,可能原因有:
- 确认是否开启Enclave服务
- 确认虚拟机规格是否满足要求,最少预留2 vCPU给父虚拟机
- 可能是系统长时间运行,无法隔离连续内存,需要在console页面重启虚拟机
制作业务EIF镜像
这里制作的是需要在enclav中运行的业务程序,以循环打印helloworld
为例,在/home/test
目录下:
mkdir -p /home/test
cd /home/test
写一个helloworld.sh
脚本:
#!/bin/bash
while true
do
echo "hello world"
sleep 5
done
修改其权限:chmod 777 helloworld
写一个简单的Dockerfile_eif
:
FROM ubuntu:22.04
COPY helloworld.sh /root/helloworld.sh
CMD "/root/helloworld.sh"
制作Docker image
docker build -f Dockerfile_eif -t helloworld .
制作EIF镜像:
qt enclave make-img --docker-uri helloworld --eif helloworld.eif
制作管理enclave的容器镜像
在这个容器镜像中,会安装管理QingTian Enclave生命周期的包:qingtian-tool
,以及他的相关依赖glib2.0
,python
和cjson
,以及python的模块docker
和knack
;
并且会把上一步的业务Enclave镜像(helloworld.eif
)放在容器镜像中。
- 先下载qingtian-tool的rpm包
yum download qingtian-tool
在写这篇博客的时候,qingtian-tool
版本为1.0-54
;所以会在/home/test
目录下看到这样一个rpm包:
qingtian-tool-1.0-54.hce2.x86_64.rpm
- 编写一个
Dockerfile
:
FROM ubuntu:22.04
# 将下载的qingtian-tool rpm包拷贝到容器镜像中
COPY ./qingtian-tool-1.0-54.hce2.x86_64.rpm /root/qingtian-tool-1.0-54.hce2.x86_64.rpm
# 拷贝业务EIF镜像
COPY ./helloworld.eif /root/helloworld.eif
# 安装必要依赖
RUN apt-get update -y && \
apt-get install python3 pip alien libglib2.0-dev libcjson-dev -y
RUN cd /root/ && \
alien -d qingtian-tool-1.0-54.hce2.x86_64.rpm && \
dpkg -i *.deb && \
pip install docker knack &&\
ln -s /usr/bin/python3 /usr/bin/python # ubuntu中需要建立软连接
制作容器镜像:
docker build -f Dockerfile_eif -t container-qt .
启动容器,并对QingTian Enclave的生命周期进行管理
QingTian Enclave的生命周期管理依赖宿主机上的一个设备/dev/qtbox_service0
,所以在启动容器的时候,需要将该设备映射到容器中。
方法一:使用–device参数
通过--device
参数将必要设备/dev/qtbox_service0
映射到容器中
docker run --device=/dev/qtbox_service0:/dev/qtbox_service0 --rm -it container-qt /bin/bash
在容器中检查是否设备已映射:
ls /dev/ | grep qt
可以看到:
测试管理Enclave生命周期命令
-
启动
qt enclave start --cpus 2 --mem 1024 --eif /root/helloworld.eif --cid 4
可以看到enclave启动成功:
-
查询Enclave
qt enclave query
-
停止Enclave
qt enclave stop --enclave-id 0
Note: 在这种方式下,qt enclave console
命令会失效,也就是无法通过console
获取以debug-mode
启动的Enclave中内部打印,用于开发调测。为了解决这个问题,可以参考方案二。
方案二:使用–privileged方式启动容器,并通过-v /dev:/dev挂载/dev目录
使用privileged
方式启动容器,会让容器具备访问宿主机上所有设备的权限,在安全上不推荐使用,只可用于非生产环境的调测工作。
docker run --privileged -v /dev:/dev -it container-qt /bin/bash
容器内检查设备:
ls /dev | grep qt
测试enclave生命周期
-
启动QingTian Enclave
qt enclave start --cpus 2 --mem 1024 --eif /root/helloworld.eif --cid 4
-
查询QingTian Enclave
qt enclave query
-
停止QingTian Enclave
qt enclave stop --enclave-id 0
-
以debug-mode启动QingTian Enclave,并使用console命令获取Enclave内部打印
qt enclave start --cpus 2 --mem 1024 --eif /root/helloworld.eif --cid 4 --debug-mode
使用console查询:
qt enclave console --enclave-id 0
Note: 这里的主要原因是debug模式下,会挂载一个/dev/sandbox-log-0
设备到父虚拟机内,而console命令依赖这个设备
总结
从容器内管理Enclave的生命周期可行,但需要注意挂载必要设备,与aws nitro enclave一致:
- 需要在容器镜像中安装必要依赖包
- 隔离服务需要提前在父虚拟机内启动
- 在容器里拉起的enclave,宿主机中无法通过qt enclave query的命令查询到
- debug模式启动的enclave需要更新容器内的/dev设备目录,才能通过console命令获取enclave内部打印
- 点赞
- 收藏
- 关注作者
评论(0)