在openwrt上运行qemu

1.编译的时候需要选择包:

kmod-tun qemu-bridge-helper qemu-x86_64-softmmu kmod-kvm-intel

2.进入系统以后挂载光盘

root@horee:/srv/iso# mkdir /mnt/cdrom
root@horee:/srv/iso# mount archlinux-2022.01.01-x86_64.iso /mnt/cdrom

3.拷贝启动文件

root@horee:/srv/iso# cp -r /mnt/cdrom/arch/boot/x86_64 ./
root@horee:/srv/iso# mv x86_64 boot

4.创建磁盘镜像

root@horee:/srv/iso# qemu-img create -f qcow2 /srv/sys/vmSrv/vmSrv.img 10G

5.启动安装光盘

root@horee:/srv/iso# qemu-system-x86_64 -enable-kvm -cpu host -smp 2 -m 4G \
    -drive file=/srv/sys/vmSrv/vmSrv.img,format=qcow2,if=virtio \
    -device virtio-net-pci,mac=E2:F2:6A:01:9D:C9,netdev=br0 \
    -netdev bridge,br=br-lan,id=br0 \
    -cdrom archlinux-2022.01.01-x86_64.iso \
    -boot order=d \
    -curses \
    -nographic

6.按照archlinux的安装流程进行安装

进入安装菜单使用tab进去启动编辑命令,然后追加下面的配置

console=ttyS0,115200

———————————-

7.运行安装好的OS

qemu-system-x86_64 -enable-kvm -cpu host -smp 4 -m 6G \
    -drive file=/srv/sys/vmSrv/vmSrv.img,format=qcow2,if=virtio \
    -drive file=/dev/sda,cache=none,if=virtio,format=raw \
    -device virtio-net-pci,mac=E2:F2:6A:01:9D:C9,netdev=br0 \
    -netdev bridge,br=br-lan,id=br0 \
    -boot order=d \
    -nographic

8.增加grub的console输出转发

nano -w /etc/default/grub

加上这个: consoleblank=0 console=tty1 console=ttyS0,115200n8

linux   /boot/vmlinuz-linux root=UUID=1a9a7eb1-4973-4098-bfec-31a2cf905efc rw consoleblank=0 console=tty1 console=ttyS0,115200n8 loglevel=3 quiet

9.启动脚本

#!/bin/sh /etc/rc.common

START=99
STOP=1

qemu_pidfile="/var/run/qemu.pid"

boot() {
    return 0
}

start() {
    qemu-system-x86_64 -enable-kvm -cpu host -smp 4 -m 6G \
        -drive file=/srv/sys/vmSrv/vmSrv.img,format=qcow2,if=virtio \
        -drive file=/dev/sda,cache=none,if=virtio,format=raw \
        -device virtio-net-pci,mac=E2:F2:6A:01:9D:C9,netdev=br0 \
        -netdev bridge,br=br-lan,id=br0 \
        -qmp tcp:0.0.0.0:4488,server,nowait \
        -boot order=d \
        -daemonize &> /var/log/qemu.log

    /usr/bin/pgrep qemu-system-x86_64 > $qemu_pidfile
    echo "QEMU: Started VM with PID $(cat $qemu_pidfile)."
}

stop() {
    echo "QEMU: Sending 'system_powerdown' to VM with PID $(cat $qemu_pidfile)."
    nc 172.16.1.1 4488 <<QMP
    { "execute": "qmp_capabilities" }
    { "execute": "system_powerdown" }
QMP

    if [ -e $qemu_pidfile ]; then
                if [ -e /proc/$(cat $qemu_pidfile) ]; then
                        echo "QEMU: Waiting for VM shutdown."
                        while [ -e /proc/$(cat $qemu_pidfile) ]; do sleep 1s; done
                        echo "QEMU: VM Process $(cat $qemu_pidfile) finished."
                else
                        echo "QEMU: Error: No VM with PID $(cat $qemu_pidfile) running."
                fi
                rm -f $qemu_pidfile
    else
                echo "QEMU: Error: $qemu_pidfile doesn't exist."
    fi
}

关于Zeno Chen

本人涉及的领域较多,杂而不精 程序设计语言: Perl, Java, PHP, Python; 数据库系统: MySQL,Oracle; 偶尔做做电路板的开发,主攻STM32单片机
此条目发表在Linux分类目录。将固定链接加入收藏夹。