转载
https://blog.csdn.net/u012124304/article/details/80955753?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-3.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-3.control
一般情况下,我们是无法在KVM虚拟机里面再去创建虚拟机的,因为我们的KVM虚拟机的CPU默认情况下并不支持虚拟化功能,对于Intel的CPU来说,如果要支持虚拟化功能,必须要有一个叫vmx的特性,执行cat /proc/cpuinfo |grep vmx查看,在CPU特性(flags)中包含了vmx这个特性,说明这台物理机是支持虚拟化功能的,也就是说我们可以在这台物理机上创建虚拟机。如下图
再在这台物理机创建的虚机上查看cpu特性,如果不包含vmx特性的话,是无法在虚机上创建虚机的。如果要让创建出来的虚拟机的CPU也带有vmx特性,就要用到所谓的嵌套虚拟化的功能,当物理机开启嵌套虚拟化功能时,我们在该物理机上创建的虚拟机的CPU也可以获得vmx特性,从而可以在该虚拟机中再去创建虚拟机。
centos和ubuntu开启/关闭嵌套虚拟化的方法如下
一、查看kvm嵌套虚拟化开启与否
1、Intel 处理器:
cat /sys/module/kvm_intel/parameters/nested
2、AMD 处理器:
cat /sys/module/kvm_amd/parameters/nested
3、systool命令
systool -m kvm_intel -v | grep -i nested
二、卸载kvm模块
1、关闭所有虚拟机
virsh destroy *
2、关闭libvirtd服务
systemctl stop libvirtd
3、卸载kvm
modprobe -r kvm_intel
modprobe -r kvm
三、开启与禁止kvm嵌套
1、ubuntu系统
通过修改内核参数的方式:
vi /etc/default/grub
在kernel 那一行的末端加上 “kvm-intel.nested=1”(设置GRUB_CMDLINE_LINUX=“kvm-intel.nested=1”)
grub-mkconfig -o /boot/grub/grub.cfg
reboot
2、centos系统
卸载kvm内核模块之后
echo ‘options kvm_intel nested=1’ >/etc/modprobe.d/kvm-nested.conf
modprode kvm_intel
cat /sys/module/kvm_intel/parameters/nested
nested=1或Y表示开启kvm嵌套虚拟化
nested=0或N表示关闭kvm嵌套虚拟化
当有了支持KVM嵌套虚拟化的物理机后,可以使用libvirt来创建虚拟机
virsh define vm01.xml
virsh start vm01
vm01.xml文件参考如下
<domain type = 'kvm'>
<name>vm01</name>
<memory>2096576</memory>
<vcpu>2</vcpu>
<os>
<type arch = 'x86_64' machine = 'rhel6.0.0'>hvm</type>
<boot dev = 'hd'/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<cpu mode='host-model'> <!--使虚拟机也支持虚拟化 -->
<model fallback='allow'/>
<topology sockets='1' cores='2' threads='1'/>
</cpu>
<clock offset = 'localtime'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type = 'file' device = 'disk'>
<driver name = 'qemu' type = 'qcow2'/>
<source file = '/var/lib/libvirt/images/CentOS7.2-20180708.qcow2'/>
<target dev = 'hda' bus = 'ide'/>
</disk>
<interface type='bridge'>
<source bridge='virbr0'/> <!--宿主机的网桥,virbr0为kvm模块提供给连接在该网桥上面的guest使用NAT方式访问外网 -->
<target dev='vnet001'/> <!-- 在virbr0网桥上增加的网口 -->
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<input type ='tablet' bus='usb'/>
<input type = 'mouse' bus = 'ps2'/>
<graphics type = 'vnc' port = '-1' listen = '0.0.0.0' autoport = 'yes'/>
<console type='pty'> <!-- 加上这行设置,否则无法使用virsh console连上虚拟机-->
<target port='0'/>
</console>
</devices>
</domain>
创建好之后进入虚机,查看cpu是否支持vmx特性,此时应该是支持的,然后就可以用相同方法在虚机中再创建虚机