使用xml文件创建虚拟机
一、安装相关软件
yum install virt-manager qemu libvirt -y
启动libvirt服务
systemctl start libvirtd.service
systemctl status libvirtd.service
下载虚拟机相关iso并上传至/var/lib/libvirt/images/目录下
二、xml文件编写
编写一个node1.xml文件并放在/etc/libvirt/qemu目录下
xml文件参考:
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:virsh edit node1
or other application using the libvirt API.
--><domain type='kvm'><name>node1</name><memory unit='KiB'>5023744</memory><currentMemory unit='KiB'>5023744</currentMemory><vcpu placement='static'>4</vcpu><os firmware='efi'><type arch='loongarch64' machine='virt'>hvm</type></os><features><acpi/></features><clock offset='utc'/><on_poweroff>destroy</on_poweroff><on_reboot>restart</on_reboot><on_crash>destroy</on_crash><devices><emulator>/usr/bin/qemu-system-loongarch64</emulator><disk type='file' device='disk'><driver name='qemu' type='qcow2' discard='unmap'/><source file='/var/lib/libvirt/images/node1.qcow2'/><target dev='vda' bus='virtio'/><boot order='2'/></disk><disk type='file' device='cdrom'><driver name='qemu' type='raw'/><source file='/var/lib/libvirt/images/loongnix-23.1-GA-loongarch64-dvd.iso'/><target dev='sda' bus='scsi'/><readonly/><boot order='1'/></disk><interface type='network'><source network='default'/><model type='virtio'/></interface><serial type='pty'><target type='system-serial' port='0'><model name='16550a'/></target></serial><console type='pty'><target type='serial' port='0'/></console><channel type='unix'><target type='virtio' name='org.qemu.guest_agent.0'/></channel><input type='keyboard' bus='usb'><address type='usb' bus='0' port='1'/></input><input type='tablet' bus='usb'><address type='usb' bus='0' port='2'/></input><graphics type='vnc' port='-1' autoport='yes'><listen type='address'/></graphics></devices>
</domain>
注释:
需要加入keyboard和tablet/mouse的设备,否则在虚拟机安装启动过程中,鼠标和键盘无法正常使用
三、创建磁盘映像
qemu-img create -f qcow2 /var/lib/libvirt/images/node1.qcow2 20G
/var/lib/libvirt/images/为文件存放的路径(跟xml文件中的设置保持一致)
20G为磁盘映像镜像的大小
四、创建启动虚拟机
1、从xml配置文件定义一个虚拟机
virsh define node1.xml
- 在执行该命令后,需要将node1.xml文件中的scsi的控制器model从lsilogic,修改为virtio-scsi,否则在安装启动虚拟机时,无法正常进入grub
scsi控制器示例如下:
<controller type='scsi' index='0' model='lsilogic'><address type='pci' domain='0x0000' bus='0x04' slot='0x01' function='0x0'/></controller>
2、启动虚拟机
virsh start node1
virsh reboot node1
node1为xml文件中定义的虚拟机名称
3、安装虚拟机
在终端启动virt-manager图形管理工具进行虚拟机安装
virt-manager
4、删除虚拟机
virsh shutdown node1 #停止虚拟机(通过发送ACPI关机信号)
virsh undefine node1 #删除虚拟机(仅删除虚拟机定义,不删除磁盘文件)
virsh destroy node1 #强制关闭虚拟机
rm -rf node1.qcow2 #删除磁盘映像
rm -rf loongnix-23.1-GA-loongarch64-dvd.iso #删除iso