power shell 脚本编写

power shell 脚本编写

  • 需求
  • 一、准备工作
  • 二、代码编写
  • 三、结果展示
  • 四、总结
  • 参考


需求

windows power shell 运行.ps1脚本启动qemu模拟器加载openharmony镜像。


一、准备工作

1.windows安装qemu:qemu-w64-setup-20221230.exe。
2.管理员启动power shell:win + x。
power shell 脚本编写_第1张图片
3.新建文本文档,扩展名改为.ps1。
4.确保命令行运行qemu命令加载镜像正常启动。
power shell 直接运行命令:
在这里插入图片描述
启动命令如下:

C:/program/qemu/qemu-system-riscv64.exe -machine virt -m 4096 -smp 4 -drive if=none,file=updater.img,format=raw,id=updater,index=3 -device virtio-blk-device,drive=updater -drive if=none,file=./system.img,format=raw,id=system,index=2 -device virtio-blk-device,drive=system -drive if=none,file=./vendor.img,format=raw,id=vendor,index=1 -device virtio-blk-device,drive=vendor -drive if=none,file=./userdata.img,format=raw,id=userdata,index=0 -device virtio-blk-device,drive=userdata -append “ip=dhcp loglevel=7 console=ttyS0,115200 init=init root=/dev/ram0 rw rootwait ohos.boot.hardware=visionfive default_boot_device=10007000.virtio_mmio sn=8823456789 ohos.required_mount.system=/dev/block/vdb@/usr@ext4@ro,barrier=1@wait,required ohos.required_mount.vendor=/dev/block/vdc@/vendor@ext4@ro,barrier=1@wait,required ohos.required_mount.data=/dev/block/vdd@/data@ext4@nosuid,nodev,noatime,barrier=1,data=ordered,noauto_da_alloc@wait,reservedsize=104857600” -kernel ./Image -initrd ./ramdisk.img -nographic -vga none -device virtio-gpu-pci,xres=320,yres=640,max_outputs=1,addr=08.0 -vnc :20 -device virtio-mouse-pci -device virtio-keyboard-pci -k en-us -display sdl,gl=off

所需镜像文件如下:(忽略.bat和.ps1,镜像文件在linux系统中编译openharmony项目代码成功后的out子目录获取)
power shell 脚本编写_第2张图片
运行结果展示:
power shell 脚本编写_第3张图片
5.进行终端配置
power shell 执行命令:Set-ExecutionPolicy AllSigned

二、代码编写

#.\qemu_make_run_riscv64.ps1 -updater updater.img -system ./system.img -vendor ./vendor.img -userdata ./userdata.img -kernel ./Image -initrd ./ramdisk.img -qemupath C:/Users/msys64/bn/ -msys2path C:/msys64/
param 
(
    $updater   = $(throw "updater image is required. eg:./updater.img"),
    $system    = $(throw "system image is required. eg:./system.img"),
    $vendor    = $(throw "vendor image is required. eg:./vendor.img"),
    $userdata  = $(throw "userdata image is required. eg:./userdata.img"),
    $kernel    = $(throw "kernel is required. eg:./Image"),
    $initrd    = $(throw "initrd image is required. eg:./ramdisk.img"),
    $qemupath  = $(),
    $msys2path = $()
)


if($qemupath -eq $null)
{
    $exeFileExists = Test-Path -Path "C:/Users/msys64/bn/qemu-system-riscv64.exe"
    if($exeFileExists)
    {
        $cmd = "C:/Users/msys64/bn/qemu-system-riscv64.exe"
    }
    else
    {
        echo "default 'C:/Users/msys64/bn/qemu-system-riscv64.exe' file not exists, please use -qemupath to certain the 'qemu-system-riscv64.exe' file path."
        Exit
    }
}
else
{
    $exeFilePath ="$qemupath" + "qemu-system-riscv64.exe"
    $exeFileExists = Test-Path -Path $exeFilePath
    if($exeFileExists)
    {
        $cmd = $exeFilePath
    }
    else
    {
        echo "'$exeFilePath' file not exists, please check."
        Exit
    }
}

$cmdbody = " -machine virt -m 4096 -smp 4 " +
    "-drive if=none,file=$updater,format=raw,id=updater,index=3 -device virtio-blk-device,drive=updater " +
    "-drive if=none,file=$system,format=raw,id=system,index=2 -device virtio-blk-device,drive=system " +
    "-drive if=none,file=$vendor,format=raw,id=vendor,index=1 -device virtio-blk-device,drive=vendor " +
    "-drive if=none,file=$userdata,format=raw,id=userdata,index=0 -device virtio-blk-device,drive=userdata " +
    "-append `'ip=dhcp loglevel=7 console=ttyS0,115200 init=init root=/dev/ram0 rw rootwait ohos.boot.hardware=riscv64_virt default_boot_device=10007000.virtio_mmio sn=8823456789 ohos.required_mount.system=/dev/block/vdb@/usr@ext4@ro,barrier=1@wait,required ohos.required_mount.vendor=/dev/block/vdc@/vendor@ext4@ro,barrier=1@wait,required ohos.required_mount.data=/dev/block/vdd@/data@ext4@nosuid,nodev,noatime,barrier=1,data=ordered,noauto_da_alloc@wait,reservedsize=104857600`' " +
    "-kernel $kernel " +
    "-initrd $initrd " +
    "-nographic -vga none -device virtio-gpu-pci,xres=320,yres=640,max_outputs=1,addr=08.0 " +
    "-vnc :20 -device virtio-mouse-pci -device virtio-keyboard-pci " +
    "-k en-us -display gtk,gl=off"

$cmd = $cmd + $cmdbody

if($msys2path -eq $null)
{
    $cmdFileExists = Test-Path -Path "C:/msys64/msys2_shell.cmd"

    if($cmdFileExists)
    {
        $cmdhead = "C:/msys64/msys2_shell.cmd -defterm -here -no-start -mingw64 -shell bash -c `"$cmd`""
    }
    else
    {
        echo "default C:/msys64/msys2_shell.cmd file not exists, please use -msys2path to certain the 'msys2_shell.cmd' file path."
        Exit
    }
}
else
{
    $cmdFilePath = $msys2path + "msys2_shell.cmd"
    $cmdFileExists = Test-Path -Path $cmdFilePath

    if($cmdFileExists)
    {
        $cmdhead = "$msys2path" + "msys2_shell.cmd -defterm -here -no-start -mingw64 -shell bash -c `"$cmd`""
    }
    else
    {
        echo "$cmdFilePath not exists, please check."
        Exit
    }
}

#$cmdhead
#$cmd | cmd
$cmdhead | powershell

1.脚本运行方式:powershell 直接执行

.\qemu_run_riscv64.ps1 -updater updater.img -system ./system.img
-vendor ./vendor.img -userdata ./userdata.img -kernel ./Image -initrd ./ramdisk.img -qemupath C:/Users/msys64/bn/ -msys2path C:/msys64/

即可。
2.param()为运行脚本命令需要传入的参数,若没有参数则提示报错。
3.由于运行qumu的命令过长,分行展示更加清晰,所以按字符串的形式以“+”进行拼接。
但需要注意的是不能随意增加空格,否则会导致命令运行不起来,比如:

-drive if=none,file=$system,format=raw,id=system,index=2 -device virtio-blk-device,drive=system

当时为了对齐,逗号前后增加空格结果运行报错:

C:\program\qemu\qemu-system-riscv64.exe: file=./system.img: drive with bus=0, unit=0 (index=0) exists

4.传入的参数在命令中直接替代原位置即可,例如:

-drive if=none,file=userdata.img,format=raw,id=userdata,index=0 -device virtio-blk-device,drive=userdata

改为

-drive if=none,file=$userdata,format=raw,id=userdata,index=0 -device virtio-blk-device,drive=userdata

5.power shell中直接将字符串当作命令运行的方式没有百度到,所以直接利用管道运行字符串命令

$cmd | cmd 或者 $cmd | powershell

6.打印字符串的方式:直接调用变量$cmd即可在power shell 控制台中显示
7.使用“#”进行注释

三、结果展示

power shell 脚本编写_第4张图片

四、总结

安装qemu最好添加至系统环境变量中,不然只能像本文截图中那样指定qumu安装目录运行qemu对应CPU类型的应用程序。
这个脚本只是最初步的结果,之后会不断更新增加功能并进行优化。
敬请期待。

参考

https://blog.csdn.net/weixin_39871788/article/details/123250595
https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_scripts?view=powershell-7.3
https://www.red-gate.com/simple-talk/sysadmin/powershell/how-to-use-parameters-in-powershell/

你可能感兴趣的:(openharmony,harmonyos)