快速入门
以下教程将引导您了解并使用QingTian Enclave特性,包括如何启动QingTian Enclave父虚拟机,如何构建QingTian Enclave镜像文件,如何查询正在运行的QingTian Enclave,以及停止QingTian Enclave。
- 购买一台弹性云服务器作为QingTian Enclave的父虚拟机,“操作系统”选择Linux类型系统镜像,并勾选“Enclave”。
详细内容,请参见自定义购买ECS。建议选择Huawei Cloud EulerOS 2.0镜像。
- 连接到QingTian Enclave父虚拟机,详细操作,请参见Linux ECS登录方式概述。
- 配置QingTian Enclave父虚拟机。
- 若选择Huawei Cloud EulerOS 2.0镜像:
- 在父虚拟机中安装qt CLI工具和必要外围组件。
使用qt CLI工具需要安装必要的python模块,详情请参见安装qt CLI。
yum install qt-enclave-bootstrap yum install virtio-qtbox yum install qingtian-tool
其中,
- virtio-qtbox:安装enclave相关驱动
- qt-enclave-bootstrap:包含enclave镜像文件制作的必要文件
- qingtian-tools:用于管理enclave生命周期
- 在qt-enclave-env.conf配置文件中按需配置隔离参数,详情请参见qt-enclave-env工具介绍。
建议一次性隔离出较大内存资源预留给QingTian Enclave使用,避免反复隔离,造成系统内存碎片化,隔离服务重启失败。
vim /etc/qingtian/enclave/qt-enclave-env.conf
将“memory_mib”设置为“8192”,“cpu_count”设置为“2”,详细内容如下:
# 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:2 # 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
- 启动资源隔离服务。
systemctl start qt-enclave-env
如果出现隔离服务失败,无法申请到足够内存,可能存在内存碎片,建议先重启虚拟机,再执行隔离服务。
- 在父虚拟机中安装qt CLI工具和必要外围组件。
- 若选择Ubuntu 22.04镜像:
- 下载华为云QingTian Enclave代码。
- 目前,支持Ubuntu 22.04镜像的分支为:https://gitee.com/HuaweiCloudDeveloper/huawei-qingtian.git。
cd /home git clone https://gitee.com/HuaweiCloudDeveloper/huawei-qingtian.git cd /home/huawei-qingtian
- 安装所需依赖。
apt-get update -y apt install libgnutls28-dev libcjson-dev libglib2.0-dev -y
- 编译virtio-qtbox。
cd /home/huawei-qingtian/virtio-qtbox make make install
- 编译qingtian-tools工具包。
cd /home/huawei-qingtian/qingtian-tools make make install
- 编译qt-proxy包。
cd /home/huawei-qingtian/qingtian-tools/qt-proxy make make install
- 安装qt-enclave-bootstrap包。
因为需要编译内核,若无特殊需要,可以将Huawei Cloud EulerOS的qt-enclave-bootstrap包解压后至对应路径。
apt install alien -y cd /home wget https://repo.huaweicloud.com/hce/2.0/updates/x86_64/Packages/qt-enclave-bootstrap-1.0-34.hce2.x86_64.rpm alien -d qt-enclave-bootstrap-1.0-34.hce2.x86_64.rpm dpkg -i qt-enclave-bootstrap_1.0-35_amd64.deb
- 若选择Huawei Cloud EulerOS 2.0镜像:
- 为QingTian Enclave父虚拟机安装必要的Python包。
pip3 install docker knack
图1 安装结果 - 在父虚拟机中安装Docker程序,我们推荐使用二进制的方式进行Docker安装,可以在Docker官方网站下载需要版本。
- 以27.0.1版本为例下载Docker:
wget https://download.docker.com/linux/static/stable/x86_64/docker-27.0.1.tgz
- 对下载的压缩包进行解压操作:
tar zxf docker-27.0.1.tgz
- 解压完成后将docker目录下所有文件复制到/usr/bin目录下:
cp docker/* /usr/bin
- 启动docker服务,并将日志等级设置为error等级:
dockerd -l error --config-file ${/path/to/your/daemon.json} &
- 验证Docker版本:
docker version
- 运行hello-world容器,查看Docker是否安装正确:
docker run hello-world
- 以27.0.1版本为例下载Docker:
- 构建QingTian Enclave镜像。
- 使用以下hello_enclave.sh脚本作为QingTian Enclave应用程序:
#!/bin/bash while true do echo "hello enclave!" sleep 2 done
Dockerfile内容如下:
FROM ubuntu:latest COPY hello_enclave.sh /root/hello_enclave.sh CMD ["/root/hello_enclave.sh"]
上述示例是直接向虚拟终端或者串口设备打印业务信息,仅适用于在调试(debug)模式下运行。在日常运维时,为了保证系统安全,建议您将业务信息重定向到文件,然后再转发到父虚拟机中,详情请参见QingTian Enclave日志转发工具。
例如,将QingTian Enclave中的/tmp/hello_enclave_output导出到父虚拟机的指定路径中:
CMD ["/root/hello_enclave.sh > /tmp/hello_enclave_output"]
- 确认脚本有可执行权限:
chmod +x hello_enclave.sh
- 构建一个名为hello-enclave的Docker镜像:
docker build -f Dockerfile -t hello-enclave .
- 使用qt enclave make-img命令将Docker镜像转换为名为hello-enclave.eif的QingTian Enclave镜像:
qt enclave make-img --docker-uri hello-enclave --eif hello-enclave.eif
输出为:
# qt enclave make-img --docker-uri hello-enclave --eif hello-enclave.eif { "digest": "SHA384", "PCR0": "63bf78ece7d2388ff773d0cad2ebc9a3070359db46d567ba271ff8adfb8b0b091be4ff4d5dda3f1c83109096e3656f3b", "PCR8": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" }
现在已经构建了名为hello-enclave.eif的QingTian Enclave镜像,命令包含了一系列PCR值,包括PCR0和PCR8(该例子中制作镜像时并没有指定证书和密钥,所以PCR8为0)。这些哈希值是在Enclave镜像构建时产生的测量值,它们通常作为预期的测量值(相对于证明文档中所包含的启动时测量值而言)。
- 通过以下命令生成证书对:
openssl ecparam -out private-key.pem -name secp384r1 -genkey openssl req -new -key private-key.pem -out ssl.csr openssl x509 -req -days 365 -in ssl.csr -signkey private-key.pem -out server.pem
- 通过生成的证书和密钥生成对应的EIF文件:
qt enclave make-img --docker-uri hello-enclave --eif hello-enclave.eif --private-key private-key.pem --signing-certificate server.pem
- 使用以下hello_enclave.sh脚本作为QingTian Enclave应用程序:
- 运行QingTian Enclave实例。
现在可以使用以下命令来运行QingTian Enclave镜像:
qt enclave start --mem 1024 --cpus 2 --eif hello-enclave.eif --cid 4 --debug-mode
该实例运行在debug-mode,关于debug-mode详情可见qt enclave子命令介绍。
您会看到以下输出:
# qt enclave start --cpus 2 --mem 1024 --eif hello-enclave.eif --cid 4 --debug-mode Started enclave with EnclaveID : 0, EnclaveCID : 4, NumberOfCPUs : 2, MemoryMiB : 1024 { "EnclaveID": 0, "EnclaveCID": 4, "NumberOfCPUs": 2, "MemoryMiB": 1024, "LaunchMode": "debug" }
在该教程中,我们为该QingTian Enclave实例分配了2个vCPU和1024M内存,并指定其cid为4,该cid可作为QingTian Enclave实例与父虚拟机实例之间的本地套接字的IP地址。
- 查询正在运行的QingTian Enclave
按以上步骤创建完QingTian Enclave实例后,可以以下命令查询他是否在运行:
qt enclave query --enclave-id 0 # qt enclave query --enclave-id 0 [{ "EnclaveID": 0, "ProcessID": 29990, "EnclaveCID": 4, "NumberOfCPUs": 2, "MemoryMiB": 1024, "LaunchMode": "debug" }]
该命令可以用于查询QingTian Enclave相关信息,包括EnclaveID、ProcessID、EnclaveCID、vCPU数量、内存数量以及其运行模式。因为该实例是以debug模式启动的,所以可以用qt enclave console 命令查看QingTian Enclave的只读控制台输出。
hello enclave! hello enclave! hello enclave! hello enclave!
您将观察到终端每隔2秒打印一次hello enclave!。
- 停止正在运行的QingTian Enclave实例
如果您想停止运行以上的QingTian Enclave实例,可以使用以下命令:
# qt enclave stop --enclave-id 0 stop enclave 0 successfully { "EnclaveID": 0 }