Skip to content

KVM虚拟化

Kervin edited this page Jul 10, 2020 · 4 revisions

1. 检查主机是否支持虚拟化

egrep '(vmx|svm)' /proc/cpuinfo
#如果有输出信息则支持
apt install -y cpu-checker
kvm-ok

2. 查看系统内核KVM模块

lsmod | grep kvm

3. Ubuntu下安装

apt-get install -y qemu qemu-kvm
apt-get install -y libvirt-daemon-system  libvirt-daemon  bridge-utils
apt-get install -y virtinst virt-manager  virt-viewer

kvm安装成功后会有/dev/kvm,再来查看下KVM是否安装成功,执行:

virsh list --all
brctl show

4. libvirtd后台启动

systemctl enable libvirtd
systemctl restart libvirtd
systemctl status libvirtd

5. 配置网络

  • 物理机网络
network:
    version: 2
    ethernets:
        eno1:
            dhcp4: false
        eno2:
            dhcp4: true
        eno3:
            dhcp4: true
        eno4:
            dhcp4: true
    bridges:
         virbr1:
            dhcp4: true
         vbr0:
            interfaces: [eno2]
            addresses:
                - 172.21.32.59/24
            gateway4: 172.21.32.254
            nameservers:
                addresses: [172.21.100.15, 172.21.100.11]
         vbr128:
            interfaces: [eno3]
            addresses:
                - 128.0.0.210/16
            routes:
                - to: 128.0.0.0/16
                  via: 128.0.0.254
         vbr129:
            interfaces: [eno4]
            addresses:
                - 129.0.0.210/16
            routes:
                - to: 129.0.0.0/16
                  via: 129.0.0.254
  • 虚拟机网络
network:
    version: 2
    ethernets:
        ens3:
            dhcp4: true
            addresses: [172.21.32.160/24]
            gateway4: 172.21.32.254
            nameservers:
                addresses: [172.21.100.15,172.21.100.11]
            routes:
                - to: 172.21.0.0/16
                  via: 172.21.32.254
        ens4:
            dhcp4: false
            addresses:
                - 128.0.0.160/16
            routes:
                - to: 128.0.0.0/16
                  via: 128.0.0.254
        ens5:
            dhcp4: false
            addresses:
                - 129.0.0.160/16
            routes:
                - to: 129.0.0.0/16
                  via: 129.0.0.254
        ens6:
            dhcp4: false
            addresses: [192.168.122.160/24]
            gateway4: 192.168.122.1
            nameservers:
                    addresses: [192.168.122.1]
        ens11:
            dhcp4: false
            addresses: [192.168.32.160/24]
            gateway4: 192.168.32.254
            nameservers:
                    addresses: [192.168.32.254]
    bonds:
        bond0:
            addresses: [10.32.0.160/24]
            gateway4: 10.32.0.254
            nameservers:
                    addresses: [10.32.0.254]
            interfaces: [ens6,ens11]

6. 视图操作

export DISPLAY=172.21.12.2:0.0
xhost +
virt-manager

注:IP为PC机的IP,后面的数字则为开始配置的Display number

7. 创建硬盘

创建KVM目录并上传Ubuntu镜像

qemu-img create -f qcow2 /home/kvm/disk/ics-server160.img 500G
qemu-img create -f qcow2 /home/kvm/disk/ics-server161.img 500G
qemu-img create -f qcow2 /home/kvm/disk/ics-server162.img 500G

磁盘映像格式,如raw、qcow2、vmdk等

8. 创建虚机

virt-install --name=ics-server160 \
--virt-type=kvm \
--vcpus=6 \
--memory=16384 \
--disk path=/home/kvm/disk/ics-server160.img,bus=virtio \
--cdrom=/home/kvm/ubuntu-19.10-live-server-amd64.iso \
--network bridge=vbr0 \
--network bridge=vbr128 \
--network bridge=vbr129 \
--network bridge=virbr0 \
--graphics vnc,listen=0.0.0.0 \
--console pty,target_type=virtio \
-v

注:关键点有网络代理、更改数据源、不能用—noautoconsole

9. 删除虚机

virsh shutdown ics-server160
virsh destroy ics-server160
virsh undefine ics-server160
rm -rf /home/kvm/disk/ics-server160.img

10. 克隆虚机

virt-clone -o ics-server  \
-n ics-server163  \
-f /home/kvm/disk/ics-server163.img

12. 其他命令

 启动虚拟机

virsh start ics-server160

 挂起虚拟机

virsh suspend ics-server160

 恢复被挂起的虚拟机

virsh resume ics-server160

 开机启动虚拟机

virsh autostart ics-server160

#注:在虚拟机服务(libvirt)启动的时候,就启动虚拟机。

 关闭开机启动虚拟机

virsh autostart --disable ics-server160

 关闭虚拟机

virsh shutdown ics-server160
#注:(需要ACPID服务的支持,ACPI是Advanced Configuration and PowerInterface缩写,高级配置和电源管理接口)

 强制关闭虚拟机

virsh destory ics-server160
#注:这种方式是从virsh list列表中将虚拟机删除,仍然可以start起来

 启动默认的网络

virsh net-start default

 自启动默认网络

virsh net-autostart default