配置网桥
命令如下,记得把ens3换成自己的网卡号。
nmcli con add type bridge ifname br0
nmcli con modify bridge-br0 bridge.stp no
nmcli con add type bridge-slave ifname ens3 master bridge-br0
nmcli connection up bridge-br0
将虚拟机导入virsh
这里假设domain的名字设置成centos8。先写一个centos8.xml
,定义虚拟机的基本属性:
<domain type='kvm'>
<name>centos8</name>
<memory unit='GiB'>2</memory>
<vcpu>4</vcpu>
// 如果要让虚拟机可以迁移到其他宿主机上,要把这个删掉
<cpu mode='host-passthrough' migratable='off'>
<cache mode='passthrough'/>
<feature policy='disable' name='lahf_lm'/>
</cpu>
<os>
<type arch='x86_64'>hvm</type>
</os>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
// 这里的type注意不要搞错了。可以用qemu-img info查看镜像的格式
<driver name='qemu' type='qcow2'/>
<source file='/home/searchstar/vm/centos.img'/>
<target dev='vda' bus='virtio'/>
</disk>
// 将宿主机上的/dev/sdb作为虚拟机的/dev/vdb。如果不需要可以删掉这部分。
<disk type="block" device="disk">
<driver name='qemu' type='raw' cache='none'/>
<source dev='/dev/sdb'/>
<target dev='vdb' bus='virtio'/>
</disk>
// 下面的是为了console访问
<serial type='pty'>
<source path='/dev/pts/0'/>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
<alias name='serial0'/>
</serial>
<console type='pty' tty='/dev/pts/0'>
<source path='/dev/pts/0'/>
<target type='serial' port='0'/>
<alias name='serial0'/>
</console>
// 联网
<interface type='bridge'>
<source bridge='br0'/>
<model type='virtio'/>
</interface>
</devices>
</domain>
导入虚拟机:
virsh define centos8.xml
如果要删除虚拟机的话:
virsh undefine centos8
查看和编辑导入后的虚拟机配置:
virsh edit centos8
访问虚拟机
启动虚拟机:
virsh start centos8
强行关闭虚拟机:
virsh destroy centos8
终端访问虚拟机(要先start):
virsh console centos8
// 退出按ctrl+]
如果没有反应,说明虚拟机里没有配置console,要看这篇文章进行配置:https://blog.csdn.net/qq_41961459/article/details/119108333
奇怪的是有时候console里shutdown之后不会自动destroy。这时要ctrl+]
退出console,然后手动virsh destroy
一下。
注意,普通用户的virsh域和root的virsh域是不互通的。
常见错误
Could not open ‘/dev/sdb’: Permission denied
将/dev/sdb
的owner和group改成自己:
sudo chown $USER /dev/sdb
理论上把自己加入到disk组也可以,因为/dev/sdb的组是disk,但是我这里没用,不知道为什么。
error: failed to connect to the hypervisor
error: failed to connect to the hypervisor
error: Failed to connect socket to '/run/user/1000/libvirt/libvirt-sock': 没有那个文件或目录
解决方法:
sudo apt install libvirt-daemon
libvirtd -d
来源:https://www.cnblogs.com/fang888/p/8496562.html
qemu-bridge-helper: failed to create tun device: Operation not permitted
https://blog.csdn.net/qq_41961459/article/details/119520468
错误:域管理的保存映像存在时拒绝取消定义
我进行如下操作之后就正常undefine了,原因不明。
virsh start centos8
virsh console centos8
virsh destroy centos8
virsh undefine centos8
参考文献
https://libvirt.org/formatdomain.html
Centos8关于kvm-qemu、libvirt和nmcli创建桥网络的使用和理解
CentOS8创建网桥
如何在 Linux 里使用 nmcli 添加网桥 | Linux 中国
How to enable KVM virsh console access
利用virsh和xml文件创建虚拟机
kvm常见故障及解决方案
给KVM虚拟机增加硬盘
https://www.linuxquestions.org/questions/linux-virtualization-and-cloud-90/qemu-kvm-permission-for-partition-disk-4175531592/
如何使KVM虚拟机的CPU和物理CPU一模一样?