CentOS7、Ubuntu平台部署kvm

Ubuntu上部署kvm
apt-get install qemu-kvm
apt-get install qemu
apt-get install virt-manager
apt-get install virt-viewer
apt-get install libvirt-bin
apt-get install bridge-utils

验证是否安装成功
root@jun-pc:~# kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used
root@ubuntu-pc:~# apt install qemu-kvm
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 gsettings-desktop-schemas : Breaks: mutter (< 3.31.4) but 3.28.4+git20200505-0ubuntu18.04.2 is to be installed
E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.

报上述错误解决办法:
apt install gsettings-desktop-schemas
View Code

CentOS7上部署
[root@bogon ~]# systemctl stop firewalld   #停止防火墙
[root@bogon ~]# systemctl disable firewalld   #关闭防火墙开机启动
[root@bogon ~]# yum -y install qemu-kvm qemu-kvm-tools bridge-utils libvirt virt-install net-tools  #安装依赖包
[root@bogon ~]# yum install -y virt-* libvirt bridge-utils qemu-img qemu-kvm
[root@bogon ~]# yum -y install virt-manager   #图形管理端(如没有装桌面环境则不用安装)
[root@bogon ~]# modprobe kvm
[root@bogon ~]# lsmod |grep kvm   #如显示的不是以下信息则说明BIOS里没有开启虚拟化
kvm_intel        162153    0
kvm                525259    1   kvm_intel
[root@bogon ~]# systemctl start libvirtd
[root@bogon ~]# cp /etc/libvirt/qemu/networks/default.xml /etc/libvirt/qemu/networks/network01.xml  #复制网卡配置文件
[root@bogon ~]# virsh net-destroy default    #停止默认虚拟网卡
[root@bogon ~]# virsh net-undefine default   #删除默认虚拟网卡的配置文件
[root@bogon ~]# vi /etc/libvirt/qemu/networks/network01.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 net-edit network01
or other application using the libvirt API.
需要修改的地方有网卡名,uuid和mac地址
-->

<network>
  <name>network01</name>
  <uuid>7a3d4101-b275-4c43-b5bc-291a9c019f18</uuid>
  <forward mode='nat'/>
  <bridge name='network01' stp='on' delay='0'/>
  <mac address='52:54:00:b5:5c:2f'/>
  <ip address='192.168.10.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.10.100' end='192.168.10.200'/>
    </dhcp>
  </ip>
</network>

[root@bogon ~]# virsh net-define /etc/libvirt/qemu/networks/network01.xml  #导入网卡配置文件
[root@bogon ~]# virsh net-list --all
Name         State        Autostart    Persistent
-----------------------------------------------------------
network01  inactive         no              yes
[root@bogon ~]# virsh net-start network01   #启用虚拟网卡
[root@bogon ~]# dd if=/dev/sr0 of=/opt/CentOS7.iso   #把系统镜像从光驱导入到本地的opt目录下
[root@bogon ~]# qemu-img create -f qcow2 /opt/Centos7.qcow2 40G   #创建磁盘文件
[root@bogon ~]# virt-install --virt-type=kvm --name=Centos7 --vcpus=1 -r 1024 --cdrom=/opt/CentOS7.iso --boot cdrom --network bridge=br0 --network network=network01 --graphics vnc,listen=0.0.0.0 --noautoconsole --os-type=linux --disk path=/opt/Centos7.qcow2,size=40,format=qcow2   #创建虚拟机
[root@bogon ~]# virsh list   #查看正在运行的虚拟机
[root@bogon ~]# virsh list --all   #查看所有虚拟机
[root@bogon ~]# virsh dominfo Centos7   #查看虚拟机的配置信息
[root@bogon ~]# virsh dumpxml Centos7   #查看kvm虚拟机配置文件
[root@bogon ~]# virsh start Centos7   #启动kvm虚拟机
[root@bogon ~]# virsh suspend Centos7   #挂起,终止
[root@bogon ~]# virsh resume Centos7   #恢复挂起状态
[root@bogon ~]# virsh shutdown Centos7   #正常关机
[root@bogon ~]# virsh destroy Centos7   #非正常关机(相当于物理机直接拔掉电源)
[root@bogon ~]# virsh undefine Centos7  #彻底删除虚拟机,找不回来了,如果想找回来,需要备份/etc/libvirt/qemu目录下的xml文件
[root@bogon ~]# virsh dumpxml Centos7 >Centos7-1.xml   #导出配置文件
[root@bogon ~]# virsh define Centos7-1.xml   #使用配置文件导入虚拟机
[root@bogon ~]# qemu-img convert -c -f raw -O qcow2 new.raw new.qcow2   #虚拟磁盘格式转换
[root@bogon ~]# virsh snapshot-create-as c72-clone c72-clone-kuaizhao1   #创建快照
[root@bogon ~]# virsh snapshot-list c72-clone   #查看快照
[root@bogon ~]# virsh snapshot-revert c72-clone c72-clone-kuaizhao1   #还原快照
[root@bogon ~]# virsh snapshot-delete c72-clone c72-clone-kuaizhao1   #删除快照
[root@bogon ~]# localectl set-locale LANG=zh_CN.UTF-8   #修改为中文字符集
[root@bogon ~]# virsh edit Centos7   #编辑虚拟机的配置文件
[root@bogon ~]# cat /etc/libvirt/qemu/Centos7.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 debian02
or other application using the libvirt API.
-->

<domain type='kvm'>
  <name>debian02</name>
  <uuid>995bfc93-2b7c-44f0-8fb9-56c5e2e721b1</uuid>
  <memory unit='KiB'>1048576</memory>
  <currentMemory unit='KiB'>1048576</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <os>
    <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
    <!-- 
    修改开机启动项dev='cdrom'表示为从光驱启动,dev='hd'表示从硬盘启动
    如改为cdrom还要在disk cdrom标签中加上ISO文件的路径:
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/opt/CentOS7.iso'/>
      <target dev='hdb' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='0' target='0' unit='1'/>
    </disk> 
    注意:
修改配置文件的方式为virsh edit Centos7修改完保存后才能生效,直接编辑配置文件不会生效
--> <boot dev='cdrom'/> </os> <features> <acpi/> <apic/> </features> <cpu mode='custom' match='exact' check='partial'> <model fallback='allow'>Broadwell</model> </cpu> <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>destroy</on_crash> <pm> <suspend-to-mem enabled='no'/> <suspend-to-disk enabled='no'/> </pm> <devices> <emulator>/usr/libexec/qemu-kvm</emulator> <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/opt/Centos7.qcow2'/> <target dev='hda' bus='ide'/> <address type='drive' controller='0' bus='0' target='0' unit='0'/> </disk> <disk type='file' device='cdrom'> <driver name='qemu' type='raw'/> <source file='/opt/CentOS7.iso'/> <target dev='hdb' bus='ide'/> <readonly/> <address type='drive' controller='0' bus='0' target='0' unit='1'/> </disk> <controller type='usb' index='0' model='ich9-ehci1'> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x7'/> </controller> <controller type='usb' index='0' model='ich9-uhci1'> <master startport='0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/> </controller> <controller type='usb' index='0' model='ich9-uhci2'> <master startport='2'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/> </controller> <controller type='usb' index='0' model='ich9-uhci3'> <master startport='4'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/> </controller> <controller type='pci' index='0' model='pci-root'/> <controller type='ide' index='0'> <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/> </controller> <interface type='bridge'> <mac address='52:54:00:aa:7b:55'/> <source bridge='br0'/> <model type='rtl8139'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> <interface type='network'> <mac address='52:54:00:de:f2:7b'/> <source network='network01'/> <model type='rtl8139'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/> </interface> <serial type='pty'> <target port='0'/> </serial> <console type='pty'> <target type='serial' port='0'/> </console> <input type='mouse' bus='ps2'/> <input type='keyboard' bus='ps2'/> <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0' password='123456'> #使用vnc连接需要密码验证时需要配置password属性 <listen type='address' address='0.0.0.0'/> </graphics> <video> <model type='cirrus' vram='16384' heads='1' primary='yes'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/> </video> <memballoon model='virtio'> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </memballoon> </devices> </domain>

 

 注意:

      导出配置文件后配置文件需要修改的地方有:
      1、主机名(name);2、UUID,随机生成UUID命令uuidgen;3、磁盘文件路径;4、网卡MAC地址
      2、使用virsh shutdown vmhost发现虚拟机没有关闭,命令没有起作用。只能使用virsh destroy vmhost来强制关闭虚拟机
           在vmhost虚拟机里面安装acpid服务(yum -y install acpid),然后reboot,因为shutdown通过该服务给虚拟机发送关机指令。
            然后就可以正常使用virsh shutdown vmhost来关闭虚拟机了

虚拟机配置示例文件

<!--
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 ubuntu18.04
or other application using the libvirt API.
-->

<domain type='kvm'>
  <name>ubuntu18.04</name>
  <uuid>30209a92-f6b2-4b9b-b94d-912c0fb35a4c</uuid>
  <metadata>
    <libosinfo:libosinfo xmlns:libosinfo="http://libosinfo.org/xmlns/libvirt/domain/1.0">
      <libosinfo:os id="http://ubuntu.com/ubuntu/18.04"/>
    </libosinfo:libosinfo>
  </metadata>
  <memory unit='KiB'>2097152</memory>
  <currentMemory unit='KiB'>2097152</currentMemory>
  <vcpu placement='static'>2</vcpu>
  <os>
    <type arch='x86_64' machine='pc-q35-4.2'>hvm</type>
    <bootmenu enable='no'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <vmport state='off'/>
  </features>
  <cpu mode='host-model' check='partial'/>
  <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>destroy</on_crash>
  <pm>
    <suspend-to-mem enabled='no'/>
    <suspend-to-disk enabled='no'/>
  </pm>
  <devices>
    <emulator>/usr/bin/qemu-system-x86_64</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/ubuntu18.04-1.qcow2'/>
      <target dev='vdb' bus='virtio'/>
      <boot order='1'/>   #设置启动顺序
      <address type='pci' domain='0x0000' bus='0x06' slot='0x00' function='0x0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/sata/system-iso/ubuntu-20.04.3-desktop-amd64.iso'/>
      <target dev='sda' bus='sata'/>
      <readonly/>
      <boot order='2'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>
    </disk>
    <controller type='usb' index='0' model='ich9-ehci1'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x1d' function='0x7'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci1'>
      <master startport='0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x1d' function='0x0' multifunction='on'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci2'>
      <master startport='2'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x1d' function='0x1'/>
    </controller>
    <controller type='usb' index='0' model='ich9-uhci3'>
      <master startport='4'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x1d' function='0x2'/>
    </controller>
    <controller type='sata' index='0'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x1f' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pcie-root'/>
    <controller type='pci' index='1' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='1' port='0x10'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0' multifunction='on'/>
    </controller>
    <controller type='pci' index='2' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='2' port='0x11'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x1'/>
    </controller>
    <controller type='pci' index='3' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='3' port='0x12'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x2'/>
    </controller>
    <controller type='pci' index='4' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='4' port='0x13'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x3'/>
    </controller>
    <controller type='pci' index='5' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='5' port='0x14'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x4'/>
    </controller>
    <controller type='pci' index='6' model='pcie-root-port'>
      <model name='pcie-root-port'/>
      <target chassis='6' port='0x15'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x5'/>
    </controller>
    <controller type='virtio-serial' index='0'>
      <address type='pci' domain='0x0000' bus='0x02' slot='0x00' function='0x0'/>
    </controller>
    <interface type='direct'>
      <mac address='52:54:00:25:ac:48'/>
      <source dev='eth0' mode='bridge'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>
    </interface>
    <serial type='pty'>
      <target type='isa-serial' port='0'>
        <model name='isa-serial'/>
      </target>
    </serial>
    <console type='pty'>
      <target type='serial' port='0'/>
    </console>
    <channel type='unix'>
      <target type='virtio' name='org.qemu.guest_agent.0'/>
      <address type='virtio-serial' controller='0' bus='0' port='1'/>
    </channel>
    <channel type='spicevmc'>
      <target type='virtio' name='com.redhat.spice.0'/>
      <address type='virtio-serial' controller='0' bus='0' port='2'/>
    </channel>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <sound model='ich9'>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x1b' function='0x0'/>
    </sound>
    <video>
      <model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x0'/>
    </video>
    <redirdev bus='usb' type='spicevmc'>
      <address type='usb' bus='0' port='2'/>
    </redirdev>
    <memballoon model='virtio'>
      <address type='pci' domain='0x0000' bus='0x04' slot='0x00' function='0x0'/>
    </memballoon>
    <rng model='virtio'>
      <backend model='random'>/dev/urandom</backend>
      <address type='pci' domain='0x0000' bus='0x05' slot='0x00' function='0x0'/>
    </rng>
  </devices>
</domain>
View Code

参考链接:
      http://blog.oldboyedu.com/kvm/
      http://www.cnblogs.com/feisky/archive/2012/07/03/2575167.html     #虚拟磁盘文件讲解
      https://github.com/retspen/webvirtcloud
      https://github.com/retspen/webvirtmgr
      https://docs.openstack.org/ocata/zh_CN/install-guide-debian/
      https://docs.openstack.org/ocata/zh_CN/install-guide-rdo/
      https://www.toutiao.com/article/7449384242095587877/        # 在rockylinux9上搭建kvm虚拟化
      https://www.toutiao.com/article/7449414423892116018/        # 使用cockpit来管理kvm虚拟机

 

posted @ 2018-04-13 13:51  風£飛  阅读(47)  评论(0)    收藏  举报