we harmoyos 论坛

 找回密码
 立即注册
查看: 2949|回复: 0

查看 qemu_run.sh

[复制链接]
  • TA的每日心情
    开心
    2024-1-19 14:48
  • 签到天数: 17 天

    [LV.4]偶尔看看III

    48

    主题

    77

    帖子

    1007

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    1007
    发表于 2022-6-20 17:56:42 | 显示全部楼层 |阅读模式



    启动qemu过程坑很多, 留下文件,方便找问题

    1. root@turingkuang:/home/openharmony/code-v3.1.1-Release/OpenHarmony/vendor/ohemu/qemu_small_system_demo# cat qemu_run.sh
    2. #!/bin/bash

    3. #Copyright (c) 2020-2021 Huawei Device Co., Ltd.
    4. #Licensed under the Apache License, Version 2.0 (the "License");
    5. #you may not use this file except in compliance with the License.
    6. #You may obtain a copy of the License at
    7. #
    8. #    http://www.apache.org/licenses/LICENSE-2.0
    9. #
    10. #Unless required by applicable law or agreed to in writing, software
    11. #distributed under the License is distributed on an "AS IS" BASIS,
    12. #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13. #See the License for the specific language governing permissions and
    14. #limitations under the License.

    15. set -e
    16. flash_name=flash.img
    17. vnc="-vnc :20  -serial mon:stdio"
    18. src_dir=out/arm_virt/qemu_small_system_demo/
    19. bootargs=$(cat <<-END
    20. bootargs=root=cfi-flash fstype=jffs2 rootaddr=10M rootsize=27M useraddr=37M usersize=27M
    21. END
    22. )

    23. elf_file=$1
    24. rebuild_image=$2
    25. vnc_enable=$3
    26. add_boot_args=$4
    27. boot_args=$5
    28. net_enable=$6
    29. gdb_enable=$7
    30. qemu_test=$8
    31. test_file=$9
    32. qemu_help=${10}

    33. help_info=$(cat <<-END
    34. Usage: qemu-run [OPTION]...
    35. Make a qemu image($flash_name) for OHOS, and run the image in qemu according
    36. to the options.

    37.     Options:

    38.     -f, --force                    rebuild ${flash_name}
    39.     -n, --net-enable               enable net
    40.     -l, --local-desktop            no VNC
    41.     -b, --bootargs boot_arguments  additional boot arguments(-bk1=v1,k2=v2...)
    42.     -g,  --gdb                     enable gdb for kernel
    43.     -h, --help                     print help info

    44.     By default, ${flash_name} will not be rebuilt if exists, and net will not
    45.     be enabled, gpu enabled and waiting for VNC connection at port 5920.
    46. END
    47. )

    48. if [ "$qemu_help" = "yes" ]; then
    49.     echo "${help_info}"
    50.     exit 0
    51. fi

    52. if [ "$vnc_enable" = "no" ]; then
    53.     vnc=""
    54. fi

    55. if [ "$add_boot_args" = "yes" ]; then
    56.     bootargs+=" "${boot_args//","/" "}
    57. fi

    58. if [ "$gdb_enable" = "yes" ]; then
    59.     qemu_option+="-s -S"
    60. fi

    61. function unsupported_parameters_check(){
    62.     if [ "$qemu_test" = "test" ]; then
    63.         echo "Error: The -t|--test option is not supported !"
    64.         echo "${help_info}"
    65.         exit 1
    66.     fi
    67. }

    68. function make_flash(){
    69.     echo -e "\nStart making ${flash_name}..."
    70.     echo -ne "${bootargs}"'\0' > ${src_dir}/bootargs
    71.     dd if=/dev/zero of=${flash_name} bs=64M count=1
    72.     dd if=${src_dir}/OHOS_Image.bin of=${flash_name} conv=notrunc seek=0 oflag=seek_bytes
    73.     dd if=${src_dir}/bootargs of=${flash_name} conv=notrunc seek=9984k oflag=seek_bytes
    74.     dd if=${src_dir}/rootfs_jffs2.img of=${flash_name} conv=notrunc seek=10M oflag=seek_bytes
    75.     dd if=${src_dir}/userfs_jffs2.img of=${flash_name} conv=notrunc seek=37M oflag=seek_bytes
    76.     echo -e "Succeed making ${flash_name}.\n"
    77. }

    78. function check_mmc_tools(){
    79.     modprobe -n nbd > /dev/null 2>&1
    80.     if [ $? != 0 ]; then
    81.         echo "Failed: need kernel module 'nbd'"
    82.         exit 1
    83.     fi

    84.     type qemu-img qemu-nbd > /dev/null 2>&1
    85.     if [ $? != 0 ]; then
    86.         echo "Failed: need qemu-utils 'qemu-img' and 'qemu-nbd'"
    87.         exit 1
    88.     fi

    89.     type parted > /dev/null 2>&1
    90.     if [ $? != 0 ]; then
    91.         echo "Failed: need tool 'parted'"
    92.         exit 1
    93.     fi
    94. }

    95. function make_mmc(){
    96.     echo -ne "\nStart making out/smallmmc.img..."

    97.     # Create raw "disk" with 1G, 2G, 5G partitions, all type 0C (FAT32 LBA).
    98.     qemu-img create -f raw out/smallmmc.raw 8G > /dev/null
    99.     sudo losetup /dev/loop100 out/smallmmc.raw
    100.     sudo parted -s /dev/loop100 -- mklabel msdos mkpart primary fat32 2048s 1025MiB \
    101.             mkpart primary fat32 1025MiB 3073MiB mkpart primary fat32 3073MiB -1s

    102.     # Format.
    103.     sudo losetup -o 1048576 /dev/loop101 /dev/loop100
    104.     sudo losetup -o 1074790400 /dev/loop102 /dev/loop100
    105.     sudo losetup -o 3222274048 /dev/loop103 /dev/loop100
    106.     sudo mkfs.vfat /dev/loop101 > /dev/null
    107.     sudo mkfs.vfat /dev/loop102 > /dev/null
    108.     sudo mkfs.vfat /dev/loop103 > /dev/null

    109.     # Clean.
    110.     sudo losetup -d /dev/loop103
    111.     sudo losetup -d /dev/loop102
    112.     sudo losetup -d /dev/loop101
    113.     sudo losetup -d /dev/loop100

    114.     # Convert to qcow2 format.
    115.     qemu-img convert -f raw out/smallmmc.raw -O qcow2 out/smallmmc.img
    116.     rm out/smallmmc.raw

    117.     # Mount.
    118.     sudo modprobe nbd
    119.     sudo qemu-nbd --connect=/dev/nbd0 out/smallmmc.img
    120.     sudo mount /dev/nbd0p1 /mnt   # 1st partition

    121.     # Copy necessary files.
    122.     sudo mkdir /mnt/data
    123.     sudo cp out/arm_virt/qemu_small_system_demo/data/line_cj.brk /mnt/data/
    124.     sudo cp out/arm_virt/qemu_small_system_demo/data/SourceHanSansSC-Regular.otf /mnt/data

    125.     # Unmount.
    126.     sudo umount /mnt
    127.     sudo qemu-nbd -d /dev/nbd0 > /dev/null
    128.     sudo modprobe -r nbd
    129.     sync out/smallmmc.img   # avoid 'Failed to get "write" lock' error

    130.     echo -e "done.\n"
    131. }

    132. function net_config(){
    133.     echo "Network config..."
    134.     set +e
    135.     sudo modprobe tun tap
    136.     sudo ip link add br0 type bridge
    137.     sudo ip address add 10.0.2.2/24 dev br0
    138.     sudo ip link set dev br0 up
    139.     set -e
    140. }

    141. function start_qemu(){
    142.     net_enable=${1}
    143.     if [[ "${vnc}" == "-vnc "* ]]; then
    144.         echo -e "Waiting VNC connection on: 5920 ...(Ctrl-C exit)"
    145.     fi
    146.     if [ ${net_enable} = yes ]
    147.     then
    148.         net_config 2>/dev/null
    149.         sudo `which qemu-system-arm` -M virt,gic-version=2,secure=on -cpu cortex-a7 -smp cpus=1 -m 1G -drive \
    150.         if=pflash,file=./${flash_name},format=raw -global virtio-mmio.force-legacy=false -netdev bridge,id=net0 \
    151.         -device virtio-net-device,netdev=net0,mac=12:22:33:44:55:66 \
    152.         -device virtio-gpu-device,xres=800,yres=480 -device virtio-tablet-device ${vnc} $qemu_option \
    153.         -drive if=none,file=./out/smallmmc.img,format=qcow2,id=mmc -device virtio-blk-device,drive=mmc \
    154.         -device virtio-rng-device
    155.     else
    156.         `which qemu-system-arm` -M virt,gic-version=2,secure=on -cpu cortex-a7 -smp cpus=1 -m 1G -drive \
    157.         if=pflash,file=./${flash_name},format=raw -global virtio-mmio.force-legacy=false \
    158.         -device virtio-gpu-device,xres=800,yres=480 -device virtio-tablet-device ${vnc} $qemu_option \
    159.         -drive if=none,file=./out/smallmmc.img,format=qcow2,id=mmc -device virtio-blk-device,drive=mmc \
    160.         -device virtio-rng-device
    161.     fi
    162. }

    163. unsupported_parameters_check

    164. if [ ! -f "${flash_name}" ] || [ ${rebuild_image} = yes ]; then
    165.     make_flash ${flash_name} "${bootargs}" ${src_dir}
    166. elif [ ${add_boot_args} = yes ]; then
    167.     echo "Update bootargs..."
    168.     echo -e "${bootargs}"'\0' > ${src_dir}/bootargs
    169.     dd if=${src_dir}/bootargs of=${flash_name} conv=notrunc seek=9984k oflag=seek_bytes
    170. fi
    171. if [ ! -f "out/smallmmc.img" ]; then
    172.     check_mmc_tools
    173.     make_mmc
    174. fi
    175. start_qemu ${net_enable}
    复制代码


    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|we harmonyos 论坛 ( 粤ICP备 2022126345号 )

    GMT+8, 2024-4-29 08:34 , Processed in 0.047628 second(s), 24 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表