README.md
1# QEMU Arm Virt Tutorial - liteos_a
2
3## 1. Overview
4
5The `arm_virt/` directory contains code that has been verified on the QEMU Arm Virt platform for adapting to OpenHarmony kernel\_liteos\_a. The code includes the driver and board configurations.
6
7The Arm Virt platform is a `qemu-system-arm` target device that simulates a general-purpose board running on the Arm architecture.
8The board whose **machine** is **virt** in QEMU is configurable. For example, you can select the core type and quantity, memory size, and security extensions when configuring the board.
9
10This tutorial guides you through the configuration of a board based on the Cortex-A7 architecture, with one CPU, extended secure features, Generic Interrupt Controller versions 2 (GICv2), and 1 GB memory.
11The system memory is hardcoded to 64 MB.
12
13## 2. Setting Up the Environment
14
15For details, see [Environment Setup](https://gitee.com/openharmony/docs/blob/HEAD/en/device-dev/quick-start/quickstart-lite-env-setup.md).
16
17## 3. Obtaining the Source Code
18
19For details, see [Source Code Acquisition](https://gitee.com/openharmony/docs/blob/HEAD/en/device-dev/get-code/sourcecode-acquire.md).
20
21## 4. Building the Source Code
22
23In the root directory of the obtained source code, run the following command:
24
25```
26hb set
27```
28
29Select `qemu_small_system_demo` under **ohemu**.
30
31Run the following build command:
32
33```
34hb build
35```
36
37After this command is executed, the image files `OHOS_Image.bin`, `rootfs_jffs2.img`, and `userfs_jffs2.img` are generated in out/arm_virt/qemu_small_system_demo/ directory.
38
39## 5. Running an Image in QEMU
40
41a) If `qemu-system-arm` has not been installed, install it. For details, see [Qemu Installation](https://gitee.com/openharmony/device_qemu/blob/HEAD/README.md).
42
43Note: The introduced functions have been tested on the target machine of virt-5.1, but are not available for all QEMU versions. Therefore, you must ensure that the qemu-system-arm version is 5.1 or later.
44
45b) Create and run an image.
46
47After the source code is built, the **qemu-run** script is generated in the root directory of the code. You can run the script to create and run an image as prompted.
48
49Run the `./qemu-run --help` command. The following information is displayed:
50
51```
52Usage: ./qemu-run [OPTION]...
53Make a qemu image(flash.img) for OHOS, and run the image in qemu according
54to the options.
55
56 Options:
57
58 -f, --force rebuild flash.img
59 -n, --net-enable enable net
60 -l, --local-desktop no VNC
61 -b, --bootargs additional boot arguments(-bk1=v1,k2=v2...)
62 -g, --gdb enable gdb for kernel
63 -h, --help print help info
64
65 By default, flash.img will not be rebuilt if exists, and net will not
66 be enabled, gpu enabled and waiting for VNC connection at port 5920.
67```
68
69Simulated network interface is wireless card wlan0, but has no real wifi functions. By default, the network will not be automatically configured if no parameter is specified. If the root directory image file **flash.img** exists, the image will not be re-created.
70
71Note: On the first run, script will also create a MMC image mainly for system and user data files. The 1st partition will be mounted at /sdcard, 2nd at /userdata, and 3rd is reserved. It is stored at OHOS source tree 'out' sub-directory, named 'smallmmc.img'. Whenever it exists, script will never try to touch it again. More information please refer to `vendor/ohemu/qemu_small_system_demo/qemu_run.sh`.
72
73c) Exit QEMU.
74
75Press `Ctrl-A + x` to exit the QEMU virtual environment.
76
77## 6. gdb debug
78
79Install `gdb-multiarch` package:
80```
81sudo apt install gdb-multiarch
82```
83Then,
84```
85cd ohos/vendor/ohemu/qemu_small_system_demo/kernel_configs
86vim debug.config
87```
88
89Modify `LOSCFG_CC_STACKPROTECTOR_ALL=y` to:
90
91```
92# LOSCFG_CC_STACKPROTECTOR_ALL is not set
93LOSCFG_COMPILE_DEBUG=y
94```
95
96Save and exit, recompile under OHOS root directory:
97
98```
99hb build -f
100```
101
102In a window to enter the command:
103
104```
105./qemu-run -g
106```
107
108In another window to enter the command:
109
110```
111gdb-multiarch out/arm_virt/qemu_small_system_demo/OHOS_Image
112(gdb) target remote localhost:1234
113```
114
115More GDB related debugging can refer to [GDB instruction manual](https://sourceware.org/gdb/current/onlinedocs/gdb).
116
117## 7. Example
118
119- [Transferring Parameters to the Kernel](example.md#sectiondebug)
120
121- [Transferring Files Using MMC Images](example.md#sectionfatfs)
122
123- [Adding a Hello World Program](example.md#addhelloworld)
124
125- [Running the Graphic Demo](example.md#simple_ui_demo)
126
127- [Observe dsoftbus Discovery](example.md#dsoftbus_discover)
128
129- [Hack A Sample Desktop](example.md#desktop)
130
131## FAQ:
1321. How do I locate a network configuration problem?
133
134 Manually configure a host network bridge. For Linux, run the following commands:
135
136 ```
137 sudo modprobe tun tap
138 sudo ip link add br0 type bridge
139 sudo ip address add 10.0.2.2/24 dev br0
140 sudo ip link set dev br0 up
141
142 # The following commands can be commented out after being executed:
143 sudo mkdir -p /etc/qemu
144 echo 'allow br0' | sudo tee -a /etc/qemu/bridge.conf
145 ```
146
147 Run the **ip addr** command to check the configuration. If **br0** does not exist or the value in the angle brackets is **DOWN**, check the configuration commands again.
148
149 ```
150 5: br0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
151 link/ether 2e:52:52:0e:21:44 brd ff:ff:ff:ff:ff:ff
152 inet 10.0.2.2/24 scope global br0
153 valid_lft forever preferred_lft forever
154 ```
155
156 If software such as Docker has been installed in the system, the system firewall may prevent the bridge from accessing the system. Run the following command:
157
158 `cat /proc/sys/net/bridge/bridge-nf-call-iptables`
159
160 **1** is displayed. Run the following command to allow the access from the network bridge:
161
162 ```
163 echo 0 | sudo tee /proc/sys/net/bridge/bridge-nf-call-iptables
164 ```
165
166 Note: The system is hardcoded to **10.0.2.0/24** for the sub-network, **10.0.2.2** for the gateway, and random IP address. Use different MAC addresses, IP addresses, and flash, MMC images for different client instances. The MAC address can be transferred using the QEMU command line. The IP address can be adjusted in the OHOS command line, for example, using `ifconfig wlan0 inet 10.0.2.30` or other methods.
167
1682. How do I troubleshoot the error when running `qemu-system-arm`?
169
170 The commands and parameters in the **qemu-run** script are as follows:
171
172 ```
173 qemu-system-arm -M virt,gic-version=2,secure=on -cpu cortex-a7 -smp cpus=1 -m 1G \
174 -drive if=pflash,file=flash.img,format=raw \
175 -drive if=none,file=./out/smallmmc.img,format=qcow2,id=mmc
176 -device virtio-blk-device,drive=mmc \
177 -netdev bridge,id=net0 \
178 -device virtio-net-device,netdev=net0,mac=12:22:33:44:55:66 \
179 -device virtio-gpu-device,xres=960,yres=480 \
180 -device virtio-tablet-device \
181 -device virtio-rng-device \
182 -vnc :20 \
183 -s -S \
184 -global virtio-mmio.force-legacy=false
185 ```
186
187 ```
188 -M Virtual machine type, ARM virt, GICv2, and extended security features
189 -cpu CPU model
190 -smp SMP setting, single core
191 -m Maximum memory size that can be used by the virtual machines
192 -drive if=pflash CFI flash drive setting
193 -drive if=none Block device image setting
194 -netdev [optional] NIC bridge type
195 -device virtio-blk-device Block device
196 -device virtio-net-device [optional] NIC device
197 -device virtio-gpu-device GPU device
198 -device virtio-tablet-device Input device
199 -device virtio-rng-device Random generator device
200 -vnc: 20 [recommend] Remote desktop connection, port 5920
201 -s -S [optional] gdb single step debug
202 -global QEMU configuration parameter, which cannot be changed
203 ```
204
205 If the error message "failed to parse default acl file" is displayed when **qemu-run** is executed:
206
207 The error may be caused by the QEMU configuration file path, which varies with the QEMU installation mode. The default QEMU configuration file path is:
208
209 - **/usr/local/qemu/etc/qemu** if QEMU is installed from the source code.
210
211 - **/ect/qemu/** if QEMU is installed by using a Linux distribution installation tool.
212
213 Determine the configuration file path and run the following command:
214
215 ```
216 echo 'allow br0' | sudo tee -a <Configuration file path>
217 ```
218
219
2203. What do I do if there is no output after QEMU of LTS 1.1.0 is executed?
221
222 The LTS code has a kernel startup defect. You can try to resolve the problem by referring to the following:
223
224 https://gitee.com/openharmony/kernel_liteos_a/pulls/324
225
226
2274. VNC window has no cursor?
228
229 Virtio-tablet is an emulated tablet input device. Neither QEMU captures nor the virtual machine displays it. The cursor is displayed by VNC client. Please check VNC client options.
README_zh.md
1# Qemu ARM Virt 教程 - liteos_a
2
3## 1. 简介
4
5`arm_virt/` 子目录包含部分Qemu ARM虚拟化平台验证的OpenHarmony kernel\_liteos\_a的适配代码,含驱动配置、板端配置等。
6
7ARM 虚拟化平台是一个 `qemu-system-arm` 的目标设备,通过它来模拟一个通用的、基于ARM架构的单板。
8Qemu中machine为 **virt** 的单板就是这种可配置的,例如:选择核的类型、核的个数、内存的大小和安全特性等,单板设备的配置。
9
10这次模拟的配置是:Cortex-A7架构,1个CPU,带安全扩展,GICv2,1G内存。
11提示: 系统内存硬编码为64MB。
12
13## 2. 环境搭建
14
15参考链接: [环境搭建](https://gitee.com/openharmony/docs/blob/HEAD/zh-cn/device-dev/quick-start/quickstart-lite-env-setup.md)
16
17## 3. 获取源码
18
19参考链接: [代码获取](https://gitee.com/openharmony/docs/blob/HEAD/zh-cn/device-dev/get-code/sourcecode-acquire.md)
20
21## 4. 源码构建
22
23在已经获取的源码根目录,请输入:
24
25```
26hb set
27```
28
29选择ohemu下的`qemu_small_system_demo`选项。
30
31
32然后执行构建命令如下:
33
34```
35hb build
36```
37
38这个命令构建会产生 `OHOS_Image.bin`、`rootfs_jffs2.img` 和 `userfs_jffs2.img` 的镜像文件。
39
40在构建完成之后,对应的镜像文件在out/arm_virt/qemu_small_system_demo/目录。
41
42
43## 5. 在Qemu中运行镜像
44
45a) 如果没有安装 `qemu-system-arm` ,安装请参考链接 [Qemu installation](https://gitee.com/openharmony/device_qemu/blob/HEAD/README_zh.md)
46
47提示:当前引入的功能在virt-5.1的目标machine已经完成测试,不保证所有的Qemu版本都能够运行成功,因此需要保证你的qemu-system-arm
48版本尽可能在5.1及以上。
49
50b) 制作以及运行镜像
51
52在代码根目录下,编译后会生成qemu-run脚本,可直接运行该脚本,根据脚本提示制作、运行镜像。
53
54执行`./qemu-run --help`提示如下:
55
56```
57Usage: ./qemu-run [OPTION]...
58Make a qemu image(flash.img) for OHOS, and run the image in qemu according
59to the options.
60
61 Options:
62
63 -f, --force rebuild flash.img
64 -n, --net-enable enable net
65 -l, --local-desktop no VNC
66 -b, --bootargs additional boot arguments(-bk1=v1,k2=v2...)
67 -g, --gdb enable gdb for kernel
68 -h, --help print help info
69
70 By default, flash.img will not be rebuilt if exists, and net will not
71 be enabled, gpu enabled and waiting for VNC connection at port 5920.
72```
73
74网卡模拟的是无线网卡wlan0,但无真的wifi功能;默认不加参数的情况下,网络不会自动配置。当根目录镜像文件flash.img存在时,镜像不会被重新制作。
75
76提示:初次运行脚本时,系统还会生成MMC镜像,主要内容为系统和用户数据文件,第1个分区将安装在/sdcard目录,第2个分区安装在/userdata目录,第3个分区保留。镜像存放在OHOS源码树的out目录下,文件名为smallmmc.img。只要不被删除,后续就不再重新制作该镜像。具体请见vendor/ohemu/qemu_small_system_demo/qemu_run.sh。
77
78c) 退出qemu环境
79
80按下`Ctrl-A + x`可退出qemu虚拟环境。
81
82## 6. gdb调试
83
84安装`gdb-multiarch`工具包:
85```
86sudo apt install gdb-multiarch
87```
88然后,
89```
90cd ohos/vendor/ohemu/qemu_small_system_demo/kernel_configs
91vim debug.config
92```
93
94将 `LOSCFG_CC_STACKPROTECTOR_ALL=y` 修改为:
95
96```
97# LOSCFG_CC_STACKPROTECTOR_ALL is not set
98LOSCFG_COMPILE_DEBUG=y
99```
100
101保存并退出,在OHOS根目录重新编译:
102
103```
104hb build -f
105```
106
107在一个窗口中输入命令:
108
109```
110./qemu-run -g
111```
112
113在另一个窗口中输入命令:
114
115```
116gdb-multiarch out/arm_virt/qemu_small_system_demo/OHOS_Image
117(gdb) target remote localhost:1234
118```
119
120更多gdb相关的调试可以查阅:[gdb指导手册](https://sourceware.org/gdb/current/onlinedocs/gdb)。
121
122## 7. 用法示例
123
124- [向内核传递参数](example.md#sectiondebug)
125
126- [用MMC映像传递文件](example.md#sectionfatfs)
127
128- [添加一个Hello World程序](example.md#addhelloworld)
129
130- [运行图形demo](example.md#simple_ui_demo)
131
132- [观察dsoftbus组网发现](example.md#dsoftbus_discover)
133
134- [Hack图形桌面](example.md#desktop)
135
136## FAQ:
1371. 当网络配置出现问题时,如何排查问题?
138
139 手动配置主机网桥设备。Linux系统参考以下命令:
140
141 ```
142 sudo modprobe tun tap
143 sudo ip link add br0 type bridge
144 sudo ip address add 10.0.2.2/24 dev br0
145 sudo ip link set dev br0 up
146
147 # 以下命令执行一次后即可注释掉
148 sudo mkdir -p /etc/qemu
149 echo 'allow br0' | sudo tee -a /etc/qemu/bridge.conf
150 ```
151
152 配置完成后,用ip addr检查应有如下类似显示。当br0不存在或尖括号中为DOWN时,请重新检查配置命令。
153
154 ```
155 5: br0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
156 link/ether 2e:52:52:0e:21:44 brd ff:ff:ff:ff:ff:ff
157 inet 10.0.2.2/24 scope global br0
158 valid_lft forever preferred_lft forever
159 ```
160
161 当系统安装有docker等软件时,系统防火墙可能阻止网桥访问。
162
163 `cat /proc/sys/net/bridge/bridge-nf-call-iptables`会显示结果:1
164
165 这时,可用如下命令打开访问许可:
166
167 ```
168 echo 0 | sudo tee /proc/sys/net/bridge/bridge-nf-call-iptables
169 ```
170
171 提示:系统网络硬编码为10.0.2.0/24,网关10.0.2.2,网址随机选取。不同的客户机实例应使用不同的MAC和IP地址(flash、MMC映像文件也应不同),MAC地址可通过QEMU命令行传递,IP地址可在OHOS命令行中调整,如`ifconfig wlan0 inet 10.0.2.30`,或使用其它方法。
172
1732. qemu-run提示`qemu-system-arm`运行出错时,如何排查问题?
174
175 qemu-run脚本中,完整的执行命令及参数解释如下:
176
177 ```
178 qemu-system-arm -M virt,gic-version=2,secure=on -cpu cortex-a7 -smp cpus=1 -m 1G \
179 -drive if=pflash,file=flash.img,format=raw \
180 -drive if=none,file=./out/smallmmc.img,format=qcow2,id=mmc
181 -device virtio-blk-device,drive=mmc \
182 -netdev bridge,id=net0 \
183 -device virtio-net-device,netdev=net0,mac=12:22:33:44:55:66 \
184 -device virtio-gpu-device,xres=960,yres=480 \
185 -device virtio-tablet-device \
186 -device virtio-rng-device \
187 -vnc :20 \
188 -s -S \
189 -global virtio-mmio.force-legacy=false
190 ```
191
192 ```
193 -M 虚拟机类型,ARM virt,GIC 2中断控制器,有安全扩展
194 -cpu CPU型号
195 -smp SMP设置,单核
196 -m 虚拟机可使用的内存限制
197 -drive if=pflash CFI闪存盘设置
198 -drive if=none 块设备映像文件设置
199 -netdev [可选]网卡后端设置,桥接类型
200 -device virtio-blk-device 块存储设备
201 -device virtio-net-device [可选]网卡设备
202 -device virtio-gpu-device GPU设备
203 -device virtio-tablet-device 输入设备
204 -device virtio-rng-device 随机数设备
205 -vnc :20 [推荐]远程桌面连接,端口5920
206 -s -S [可选]gdb单步调试
207 -global QEMU配置参数,不可调整
208 ```
209
210 运行时,qemu-run遇到报错如下报错: failed to parse default acl file
211
212 可能是qemu安装方式不同,导致qemu配置文件路径存在一定差异:
213
214 - 使用源码安装默认在/usr/local/qemu/etc/qemu
215
216 - 使用部分Linux发行版安装工具进行安装时,默认在/ect/qemu/目录下
217
218 可根据实际情况,确认具体配置目录,并进行如下修改:
219
220 ```
221 echo 'allow br0' | sudo tee -a <配置文件路径>
222 ```
223
224
2253. 1.1.0LTS版本qemu运行无输出?
226
227 LTS的代码存在一个内核启动缺陷,可以参考如下PR尝试解决问题:
228
229 https://gitee.com/openharmony/kernel_liteos_a/pulls/324
230
231
2324. VNC窗口不显示光标?
233
234 virtio-tablet是个模拟平板输入的设备,QEMU不捕获设备,虚拟机不显示光标,由VNC客户端自行处理光标显示。请查看VNC客户端选项设置。