个人站点:landery小站 原文地址:blog.landery.cn/895.html
编译qemu运行
为了方便后期对qemu源码修改,需要讲yum安装的qemu包,自己编译替换
这里qemu我们使用6.0的
安装开发工具
yum groupinstall "Development Tools"
先安装ninja,需要先安装re2c,下载地址:http://re2c.org/index.html
tar -xvf re2c-3.1.tar.xz
cd re2c-3.1/
autoreconf -i -W all
./configure
make
make install
安装ninja,
cd /data
git clone https://github.com/ninja-build/ninja.git
cd ninja
./configure.py --bootstrap
cp ./ninja /usr/bin
ninja --version
编译安装qemu
cd /data
git clone -b stable-6.0 https://github.com/qemu/qemu.git
cd qemu
mkdir build
cd build
../configure --prefix=/usr --enable-virtfs --enable-kvm --enable-sdl --enable-debug --enable-tpm --enable-vnc --target-list=aarch64-softmmu,arm-softmmu
make -j4
make install //此操作会替换qemu-img等软件包,无所谓就直接执行
因为我们要用virsh创建虚拟机,需要替换/usr/bin/qemu-kvm和/usr/libexec/qemu-kvm
rm -rf /usr/bin/qemu-kvm
ln -s /usr/bin/qemu-system-aarch64 /usr/bin/qemu-kvm
mv /usr/libexec/qemu-kvm /usr/libexec/qemu-kvm-bak #备份一下
ln -s /usr/bin/qemu-system-aarch64 /usr/libexec/qemu-kvm
测试一下是否能够正常启动虚拟机
架构:鲲鹏920ARM+openEuler 22.03
虚拟机:ubuntu22.04 server ARM版
我们只能使用virsh创建虚拟机,因此下面给出对应的配置文件xml,这是虚拟机配置 (openeuler.org),官网配置,注释需要删除
<domain type='kvm'>
<name>ubuntu2204</name>
<memory unit='GiB'>8</memory>
<vcpu>4</vcpu>
<os>
<type arch='aarch64' machine='virt'>hvm</type>
<loader readonly='yes' type='pflash'>/usr/share/edk2/aarch64/QEMU_EFI-pflash.raw</loader> #这里使用的是默认的UEFI固件
<nvram>/var/lib/libvirt/qemu/nvram/openEulerVM.fd</nvram> #UEFI变量
</os>
<features>
<acpi/>
<gic version='3'/>
</features>
<cpu mode='host-passthrough'>
<topology sockets='2' cores='2' threads='1'/>
</cpu>
<iothreads>1</iothreads>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator> #这里使用qemu-kvm有已经被我们替换成我们编译的了
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' iothread="1"/>
<source file='/data/lyy/vm/ubuntu2204.qcow2'/>
<target dev='vda' bus='virtio'/>
<boot order='1'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='/data/iso/ubuntu-22.04-live-server-arm64.iso'/>
<readonly/>
<target dev='sdb' bus='scsi'/>
<boot order='2'/>
</disk>
<interface type='bridge'>
<source bridge='br0'/>
<model type='virtio'/>
</interface>
<console type='pty'/>
<video>
<model type='virtio'/>
</video>
<controller type='scsi' index='0' model='virtio-scsi'/>
<controller type='usb' model='ehci'/>
<input type='tablet' bus='usb'/>
<input type='keyboard' bus='usb'/>
<graphics type='vnc' listen='0.0.0.0' passwd='123456'/> #这里配置vnc监听端口和密码
</devices>
<seclabel type='dynamic' model='dac' relabel='yes'/>
</domain>
virsh define ubuntu2204.xml
virsh start ubuntu2204
这里有可能报错:libvirtError: Unable to add bridge br0 port vnet0: Operation not supported
参考之前的文章:blog.landery.cn/864.html
define定义了虚拟机以后,虚拟机的配置文件就在/etc/libvirt/qemu/目录下
我们需要使用以下命令,实时修改虚拟机配置。
virsh edit ubuntu2204
这里备份一下ubuntu2204的配置。
<domain type='kvm'>
<name>ubuntu2204</name>
<uuid>606763c0-eefe-4816-acca-e21be114cfff</uuid>
<memory unit='KiB'>8388608</memory>
<currentMemory unit='KiB'>8388608</currentMemory>
<vcpu placement='static'>4</vcpu>
<iothreads>1</iothreads>
<os>
<type arch='aarch64' machine='virt-6.0'>hvm</type>
<loader readonly='yes' type='pflash'>/usr/share/edk2/aarch64/QEMU_EFI-pflash.raw</loader>
<nvram>/var/lib/libvirt/qemu/nvram/openEulerVM.fd</nvram>
</os>
<features>
<acpi/>
<gic version='3'/>
</features>
<cpu mode='host-passthrough' check='none'>
<topology sockets='2' dies='1' clusters='1' cores='2' threads='1'/>
</cpu>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' iothread='1'/>
<source file='/data/lyy/vm/ubuntu2204.qcow2'/>
<target dev='vda' bus='virtio'/>
<boot order='1'/>
<address type='pci' domain='0x0000' bus='0x05' slot='0x00' function='0x0'/>
</disk>
<controller type='scsi' index='0' model='virtio-scsi'>
<address type='pci' domain='0x0000' bus='0x03' slot='0x00' function='0x0'/>
</controller>
<controller type='usb' index='0' model='ehci'>
<address type='pci' domain='0x0000' bus='0x04' slot='0x01' function='0x0'/>
</controller>
<controller type='pci' index='0' model='pcie-root'/>
<controller type='pci' index='1' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='1' port='0x8'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x0' multifunction='on'/>
</controller>
<controller type='pci' index='2' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='2' port='0x9'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<controller type='pci' index='3' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='3' port='0xa'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
</controller>
<controller type='pci' index='4' model='pcie-to-pci-bridge'>
<model name='pcie-pci-bridge'/>
<address type='pci' domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>
</controller>
<controller type='pci' index='5' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='5' port='0xb'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x3'/>
</controller>
<controller type='pci' index='6' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='6' port='0xc'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x4'/>
</controller>
<controller type='pci' index='7' model='pcie-root-port'>
<model name='pcie-root-port'/>
<target chassis='7' port='0xd'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x5'/>
</controller>
<interface type='bridge'>
<mac address='52:54:00:4a:d1:10'/>
<source bridge='br0'/>
<virtualport type='openvswitch'>
<parameters interfaceid='da2f33a2-2b12-4710-9199-b8e46cc39df6'/>
</virtualport>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x02' slot='0x00' function='0x0'/>
</interface>
<serial type='pty'>
<target type='system-serial' port='0'>
<model name='pl011'/>
</target>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<input type='tablet' bus='usb'>
<address type='usb' bus='0' port='1'/>
</input>
<input type='keyboard' bus='usb'>
<address type='usb' bus='0' port='2'/>
</input>
<graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0' passwd='123456'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<video>
<model type='virtio' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x06' slot='0x00' function='0x0'/>
</video>
</devices>
<seclabel type='dynamic' model='dac' relabel='yes'/>
</domain>
然后启动就行。
virsh start ubuntu2204
注:如果修改了配置文件edit之后,需要先关机在启动,配置文件才会生效。
virsh shudown ubuntu2204 virsh start ubuntu2204 #新的配置文件生效
查看vnc端口
virsh vncdisplay ubuntu2204
使用vncviewer等软件连接。
记得开放端口
firewall-cmd --add-port 5901/tcp --permanent firewall-cmd --reload
成功进入系统。