# 快速入门
以下教程将引导您了解并使用QingTian Enclave特性，包括如何启动QingTian Enclave父虚拟机，如何构建QingTian Enclave镜像文件，如何查询正在运行的QingTian Enclave，以及停止QingTian Enclave。
1. 购买一台弹性云服务器作为QingTian Enclave的父虚拟机，"操作系统"选择Linux类型系统镜像，并勾选"Enclave"。 详细内容，请参见[自定义购买ECS](https://support.huaweicloud.com/usermanual-ecs/ecs_03_7002.html)。建议选择Huawei Cloud EulerOS 2.0镜像。
   
2. 连接到QingTian Enclave父虚拟机，详细操作，请参见[Linux ECS登录方式概述](https://support.huaweicloud.com/usermanual-ecs/zh-cn_topic_0013771089.html)。
3. 配置QingTian Enclave父虚拟机。
   **若选择Huawei Cloud EulerOS 2.0镜像：**
   1. 在父虚拟机中安装**qt CLI** 工具和必要外围组件。
      使用qt CLI工具需要安装必要的python模块，详情请参见[安装qt CLI](https://support.huaweicloud.com/usermanual-ecs/ecs_03_1417.html)。
      ```
      yum install qt-enclave-bootstrap
      yum install virtio-qtbox
      yum install qingtian-tool
      ```
      其中，
      - virtio-qtbox：安装enclave相关驱动
      
      - qt-enclave-bootstrap：包含enclave镜像文件制作的必要文件
      
      - qingtian-tools：用于管理enclave生命周期
       
   
   2. 在**qt-enclave-env.conf** 配置文件中按需配置隔离参数，详情请参见[qt-enclave-env工具介绍](https://support.huaweicloud.com/usermanual-ecs/ecs_03_1418.html)。
      建议一次性隔离出较大内存资源预留给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
      ```
      
      
   
   3. 启动资源隔离服务。
      ```
      systemctl start qt-enclave-env
      ```
      如果出现隔离服务失败，无法申请到足够内存，可能存在内存碎片，建议先重启虚拟机，再执行隔离服务。
      
    
   **若选择Ubuntu 22.04镜像：**
   1. 下载华为云**QingTian Enclave** 代码。
      目前，支持Ubuntu 22.04镜像的分支为sync，代码仓为<https://gitee.com/HuaweiCloudDeveloper/huawei-qingtian.git>。
      ```
      cd /home
      git clone -b sync https://gitee.com/HuaweiCloudDeveloper/huawei-qingtian.git
      cd /home/huawei-qingtian
      ```
      
   
   2. 安装所需依赖。
      ```
      apt-get update -y
      apt install libgnutls28-dev libcjson-dev libglib2.0-dev -y
      ```
      
   
   3. 编译**virtio-qtbox** 。
      ```
      cd /home/huawei-qingtian/virtio-qtbox
      make
      make install
      ```
      
   
   4. 编译**qingtian-tools** 工具包。
      ```
      cd /home/huawei-qingtian/qingtian-tools
      make
      make install
      ```
      
   
   5. 编译**qt-proxy** 包。
      ```
      cd /home/huawei-qingtian/qingtian-tools/qt-proxy
      make
      make install
      ```
      
   
   6. 安装**qt-enclave-bootstrap** 包。
      因需要编译内核，若无特殊修改需要，可直接使用Huawei Cloud EulerOS的**qt-enclave-bootstrap**包，解压后通过alien转换rpm格式为Ubuntu系统的deb格式即可。
      ```
      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
      ```
      
     
4. 为QingTian Enclave父虚拟机安装必要的Python包。
   ```
   pip3 install docker knack
   ```
   图1安装结果   
   ![](https://support.huaweicloud.com/usermanual-ecs/zh-cn_image_0000002315056405.png "点击放大") 
5. 在父虚拟机中安装Docker程序，我们推荐使用二进制的方式进行Docker安装，可以在Docker官方网站下载所需版本。
   1. 以27.0.1版本为例下载Docker：
      ```
      wget https://download.docker.com/linux/static/stable/x86_64/docker-27.0.1.tgz
      ```
      
   
   2. 对下载的压缩包进行解压操作：
      ```
      tar zxf docker-27.0.1.tgz
      ```
      
   
   3. 解压完成后将docker目录下所有文件复制到/usr/bin目录下：
      ```
      cp docker/* /usr/bin
      ```
      
   
   4. 启动docker服务，并将日志等级设置为error等级：
      ```
      dockerd -l error --config-file ${/path/to/your/daemon.json} &
      ```
      ![](https://support.huaweicloud.com/usermanual-ecs/public_sys-resources/note_3.0-zh-cn.png)
      在启动dockerd时需要配置额外配置文件：${/path/to/your/daemon.json}
      详细内容，请参考[设置镜像加速器](https://support.huaweicloud.com/usermanual-swr/swr_01_0045.html)。
      
   
   5. 验证Docker版本：
      ```
      docker version
      ```
      
   
   6. 运行hello-world容器，查看Docker是否安装正确：
      ```
      docker run hello-world
      ```
      
    
6. 构建QingTian Enclave镜像。
   1. 使用以下**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日志转发工具](https://support.huaweicloud.com/usermanual-ecs/ecs_03_1426.html)。
      例如，将QingTian Enclave中的/tmp/hello_enclave_output导出到父虚拟机的指定路径中：
      ```
      CMD ["/root/hello_enclave.sh > /tmp/hello_enclave_output"]
      ```
      
   
   2. 确认脚本有可执行权限：
      ```
      chmod +x hello_enclave.sh
      ```
      
   
   3. 构建一个名为hello-enclave的Docker镜像：
      ```
      docker build -f Dockerfile -t hello-enclave .
      ```
      
   
   4. 使用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镜像构建时产生的测量值，它们通常作为预期的测量值（相对于证明文档中所包含的启动时测量值而言）。
      
   
   5. 通过以下命令生成证书对：
      ```
      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
      ```
      
   
   6. 通过生成的证书和密钥生成对应的EIF文件：
      ```
      qt enclave make-img --docker-uri hello-enclave --eif hello-enclave.eif --private-key private-key.pem --signing-certificate server.pem
      ```
      
    
7. 运行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子命令介绍](https://support.huaweicloud.com/usermanual-ecs/ecs_03_1419.html)。
   您会看到以下输出：
   ```
   # 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地址。
   
8. 查询正在运行的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!。
   
9. 停止正在运行的QingTian Enclave实例 如果您想停止运行以上的QingTian Enclave实例，可以使用以下命令：
   ```
   # qt enclave stop --enclave-id 0
   stop enclave 0 successfully
   {
       "EnclaveID":    0
   }
   ```
   
   
 
