1d6aed566Sopenharmony_ci# Qemu x86_64 Virt 标准系统教程
2d6aed566Sopenharmony_ci
3d6aed566Sopenharmony_ci## 1. 简介
4d6aed566Sopenharmony_ci
5d6aed566Sopenharmony_ci`x86_64_virt/linux` 子目录包含部分Qemu x86_64虚拟化平台验证的Linux kernel的适配代码,含驱动配置、板端配置等。
6d6aed566Sopenharmony_ci
7d6aed566Sopenharmony_cix86_64 虚拟化平台是一个 `qemu-system-x86_64` 的目标设备,通过它来模拟一个通用的、基于x86_64架构的单板。
8d6aed566Sopenharmony_ciQemu中machine为 **microvim** 的单板就是这种可配置的,例如:选择核的类型、核的个数、内存的大小和安全特性等,单板设备的配置。
9d6aed566Sopenharmony_ci
10d6aed566Sopenharmony_ci提示: 系统内存硬编码为1024MB。
11d6aed566Sopenharmony_ci
12d6aed566Sopenharmony_ci## 2. 环境搭建
13d6aed566Sopenharmony_ci
14d6aed566Sopenharmony_ci参考链接: [环境搭建](https://gitee.com/openharmony/docs/blob/master/zh-cn/device-dev/porting/Readme-CN.md)
15d6aed566Sopenharmony_ci
16d6aed566Sopenharmony_ci## 3. 获取源码
17d6aed566Sopenharmony_ci
18d6aed566Sopenharmony_ci参考链接: [代码获取](https://gitee.com/openharmony/docs/blob/HEAD/zh-cn/device-dev/get-code/sourcecode-acquire.md)
19d6aed566Sopenharmony_ci
20d6aed566Sopenharmony_ci## 4. 源码构建
21d6aed566Sopenharmony_ci
22d6aed566Sopenharmony_ci在已经获取的源码根目录,请输入:
23d6aed566Sopenharmony_ci
24d6aed566Sopenharmony_ci```
25d6aed566Sopenharmony_ci./build.sh --product-name qemu-x86_64-linux-min --ccache --jobs 4
26d6aed566Sopenharmony_ci```
27d6aed566Sopenharmony_ci
28d6aed566Sopenharmony_ci在构建完成之后,对应的镜像文件在out/qemu-x86_64-linux/packages/phone/images/目录下。
29d6aed566Sopenharmony_ciqemu-x86_64-linux-min表示部件最小集合的产品。
30d6aed566Sopenharmony_ciqemu-x86_64-linux-headless表示在最小集合基础上,支持无屏幕的用户程序框架部件集合的产品。
31d6aed566Sopenharmony_ci
32d6aed566Sopenharmony_ci
33d6aed566Sopenharmony_ci## 5. 在Qemu中运行镜像
34d6aed566Sopenharmony_ci
35d6aed566Sopenharmony_cia) 如果没有安装 `qemu-system-x86_64` ,安装请参考链接 [Qemu installation](https://gitee.com/openharmony/device_qemu/blob/HEAD/README_zh.md)
36d6aed566Sopenharmony_ci
37d6aed566Sopenharmony_ci提示:当前引入的功能在virt-5.1的目标machine已经完成测试,不保证所有的Qemu版本都能够运行成功,因此需要保证你的qemu-system-x86_64
38d6aed566Sopenharmony_ci版本尽可能在5.1及以上。
39d6aed566Sopenharmony_ci
40d6aed566Sopenharmony_cib) 运行镜像
41d6aed566Sopenharmony_ci
42d6aed566Sopenharmony_ci执行`./vendor/ohemu/qemu_x86_64_linux_min/qemu_run.sh`即可运行步骤4生成的镜像。
43d6aed566Sopenharmony_ci
44d6aed566Sopenharmony_cic) 退出qemu环境
45d6aed566Sopenharmony_ci
46d6aed566Sopenharmony_ci按下`Ctrl-A + x`可退出qemu虚拟环境。
47d6aed566Sopenharmony_ci
48d6aed566Sopenharmony_ci### 5.1 qemu_run.sh使用说明
49d6aed566Sopenharmony_ci
50d6aed566Sopenharmony_ci```C
51d6aed566Sopenharmony_ciUsage: qemu-run [OPTION]...
52d6aed566Sopenharmony_ciRun a OHOS image in qemu according to the options.
53d6aed566Sopenharmony_ci    -e,  --exec image_path    images path, including: zImage-dtb, ramdisk.img, system.img, vendor.img, userdata.img
54d6aed566Sopenharmony_ci    -g,  --gdb                enable gdb for kernel.
55d6aed566Sopenharmony_ci    -n,  --network            auto setup network for qemu (sudo required).
56d6aed566Sopenharmony_ci    -i,  --instance id        start qemu images with specified instance id (from 01 to 99).
57d6aed566Sopenharmony_ci                              it will also setup network when running in multiple instance mode.
58d6aed566Sopenharmony_ci         -f                   force override instance image with a new copy.
59d6aed566Sopenharmony_ci    -h,  --help               print this help info.
60d6aed566Sopenharmony_ci
61d6aed566Sopenharmony_ci    If no image_path specified, it will find OHOS image in current working directory; then try .
62d6aed566Sopenharmony_ci
63d6aed566Sopenharmony_ci    When setting up network, it will create br0 on the host PC with the following information:
64d6aed566Sopenharmony_ci        IP address: 192.168.100.1
65d6aed566Sopenharmony_ci        netmask: 255.255.255.0
66d6aed566Sopenharmony_ci
67d6aed566Sopenharmony_ci    The default qemu device MAC address is [00:22:33:44:55:66], default serial number is [0023456789].
68d6aed566Sopenharmony_ci    When running in multiple instances mode, the MAC address and serial number will increase with specified instance ID as follow:
69d6aed566Sopenharmony_ci        MAC address:    {instanceID}:22:33:44:55:66
70d6aed566Sopenharmony_ci        Serial number:  {instanceID}23456789
71d6aed566Sopenharmony_ci```
72d6aed566Sopenharmony_ci
73d6aed566Sopenharmony_ciqemu_run.sh默认会启动当前工作目录或out/qemu-x86_64-linux/packages/phone/images目录下的系统镜像。
74d6aed566Sopenharmony_ci
75d6aed566Sopenharmony_ci默认的qemu虚拟机没有网络连接。如果需要使能网络连接,可以添加-n选项。此选项会完成以下几个事物:
76d6aed566Sopenharmony_ci
77d6aed566Sopenharmony_ci- 主机侧创建虚拟网桥
78d6aed566Sopenharmony_ci
79d6aed566Sopenharmony_ci  主机侧会创建br0的网桥,用于与qemu ram虚拟机设备进行网络通信。
80d6aed566Sopenharmony_ci
81d6aed566Sopenharmony_ci  br0默认IP地址为192.168.100.1,子网掩码为255.255.255.0。
82d6aed566Sopenharmony_ci
83d6aed566Sopenharmony_ci  由于主机侧创建虚拟网桥需要管理员权限,因此,需要通过sudo命令执行qemu_run.sh84d6aed566Sopenharmony_ci
85d6aed566Sopenharmony_ci
86d6aed566Sopenharmony_ci- 虚拟机侧创建虚拟网卡
87d6aed566Sopenharmony_ci
88d6aed566Sopenharmony_ci  启动qemu x86_64虚拟机时会为虚拟机设备创建一个网卡,该网卡的默认MAC地址是12:22:33:44:55:66,在虚拟机的网络接口名称是eth0。
89d6aed566Sopenharmony_ci
90d6aed566Sopenharmony_ci  虚拟机启动后,可以通过ifconfig eth0 192.168.100.2给虚拟机设置上IP地址(其它IP地址都可以,只要在同一个网段内)。
91d6aed566Sopenharmony_ci
92d6aed566Sopenharmony_ci  通过此设置,主机就可以和虚拟机进行网络通信。可以使用hdc与虚拟机交互。
93d6aed566Sopenharmony_ci
94d6aed566Sopenharmony_ci正常的OHOS设备都有一个唯一的设备序列号,该序列号会用于各个业务进行设备唯一标识。使用qemu虚拟机启动时,默认的序列号是0123456789。
95d6aed566Sopenharmony_ci
96d6aed566Sopenharmony_ci### 5.2 qemu_run.sh多实例运行
97d6aed566Sopenharmony_ci
98d6aed566Sopenharmony_ci有时需要运行多个qemu x86_64虚拟机实例,用于验证分布式组网场景。此时,可以在调用qemu_run.sh时传入-i指定不同实例号。每个不同实例号运行多虚拟机设备都有不同的MAC地址和SN号,以此来模拟多个虚拟设备。MAC地址和SN号的分配规则如下:
99d6aed566Sopenharmony_ci
100d6aed566Sopenharmony_ci        MAC address:    {instanceID}:22:33:44:55:66
101d6aed566Sopenharmony_ci        Serial number:  {instanceID}23456789
102d6aed566Sopenharmony_ci
103d6aed566Sopenharmony_ciinstanceID的取值格式为两个数字字符:范围为01到99。
104d6aed566Sopenharmony_ci
105d6aed566Sopenharmony_ci当镜像输出目录中已存在该instanceID的实例时,默认不重新拷贝镜像。若要重新拷贝镜像可在调用qemu_run.sh时传入-f选项将原镜像覆盖。
106