we harmoyos 论坛

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

启动qemu脚本 qemu_run.sh

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

    [LV.4]偶尔看看III

    48

    主题

    77

    帖子

    1007

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    1007
    发表于 2022-6-15 17:05:56 | 显示全部楼层 |阅读模式
    qemu_run.sh 是 qemu_small_system_demo 启动脚本, 位于 vendor\ohemu\qemu_small_system_demo\

    1. #!/bin/bash

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

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

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

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

    36.     Options:

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

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

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

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

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

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

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

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

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

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

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

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

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

    101.     # Format.
    102.     sudo losetup -o 1048576 /dev/loop1 /dev/loop0
    103.     sudo losetup -o 1074790400 /dev/loop2 /dev/loop0
    104.     sudo losetup -o 3222274048 /dev/loop3 /dev/loop0
    105.     sudo mkfs.vfat /dev/loop1 > /dev/null
    106.     sudo mkfs.vfat /dev/loop2 > /dev/null
    107.     sudo mkfs.vfat /dev/loop3 > /dev/null

    108.     # Clean.
    109.     sudo losetup -d /dev/loop3
    110.     sudo losetup -d /dev/loop2
    111.     sudo losetup -d /dev/loop1
    112.     sudo losetup -d /dev/loop0

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

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

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

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

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

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

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

    162. unsupported_parameters_check

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



    回复

    使用道具 举报

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

    本版积分规则

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

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

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

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