Ubuntu 24.04 上 x86_64 虚拟机的 libvirt 模板
XML 配置
libvirt_template.xml
<domain type='kvm'>
<name>myvm</name>
<memory unit='MiB'>4096</memory>
<vcpu placement='static'>4</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-jammy'>hvm</type>
<boot dev='hd'/>
<boot dev='cdrom'/>
</os>
<features>
<acpi/>
<apic/>
<vmport state='off'/>
</features>
<cpu mode='host-passthrough'/>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
<controller type='pci' model='pci-root'/>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/opt/myvm.qcow2'/>
<target dev='vda' bus='virtio'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='/tmp/ubuntu.iso'/>
<target dev='hda' bus='ide'/>
<readonly/>
</disk>
<interface type='network'>
<mac address='4c:68:29:d5:aa:55'/>
<source network='default'/>
<model type='virtio'/>
</interface>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<input type='keyboard' bus='virtio'/>
<input type='mouse' bus='virtio'/>
<serial type='pty'>
<target port='0'/>
</serial>
<graphics type='vnc' port='-1' listen='0.0.0.0' autoport='yes'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<filesystem type='mount' accessmode='mapped'>
<source dir='/home/uli/vm-data'/>
<target dir='data'/>
</filesystem>
<memballoon model='virtio'/>
</devices>
</domain>需要修改的内容
<mac>标签中的 MAC 地址。<source file='...'>标签中磁盘镜像的路径。
如何创建磁盘镜像
create_disk.sh
qemu-img create -f qcow2 /opt/myvm.qcow2 100G如何启动虚拟机
确保在 /tmp/ubuntu.iso 有可用的 DVD 镜像用于启动,然后运行:
start_vm.sh
virsh define /path/to/your/xml/file.xml
virsh start myvm要通过 VNC 查看虚拟机的桌面,运行
vncdisplay.sh
virsh vncdisplay myvmCheck out similar posts by category:
Virtualization, Libvirt
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow