# debain安装kvm

# 安装命令

sudo apt install qemu-kvm libvirt-clients libvirt-daemon-system bridge-utils libguestfs-tools genisoimage virtinst libosinfo-bin

# 配置桥接网络

vi /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto enp3s0
iface enp3s0 inet manual

auto br0
iface br0 inet static
address 192.168.3.106
network 192.168.3.0
netmask 255.255.255.0
broadcast 192.168.3.255
gateway 192.168.3.1
dns-nameservers 192.168.3.1
bridge_ports enp3s0
bridge_stp off
bridge_fd 0
bridge_maxwait 0
virsh net-list --all
vi /root/bridged.xml
<network>
  <name>br0</name>
  <forward mode="bridge"/>
  <bridge name="br0"/>
</network>
virsh net-define --file /root/bridged.xml
virsh net-autostart br0
virsh net-start br0
service network restart

# 构建虚拟机

virt-builder --list | more
virt-builder --list | grep x86_64
virt-builder centos-7.5 \
--size=40G \
--format qcow2 -o /var/lib/libvirt/images/centos7vm1.qcow2 \
--hostname centos7vm1 \
--network \
--timezone Asia/Shanghai
sudo virt-install --import --name centos7vm1 \
--ram 2048 \
--vcpu 2 \
--disk path=/var/lib/libvirt/images/centos7vm1.qcow2,format=qcow2 \
--os-variant centos7.0 \
--network=bridge=br0,model=virtio \
--noautoconsole

# KVM虚拟机克隆

virt-clone --original=centos7vm1 \
               --name=centos7vm2 \
               --file=/var/lib/libvirt/images/centos7vm2.qcow2

# kvm安装windows

准备虚拟硬盘文件

qemu-img create /var/lib/libvirt/images/win7vm1.img 40G

# 准备和编写配置文件。

  • 虚拟机cpu数量(非必要, 默认是2)
  • 虚拟机内存大小(非必要, 默认是2G)
  • 虚拟硬盘文件位置。
  • win7安装镜像的位置。
  • 虚拟网络实例的名字( 非必要, /etc/network/interfaces 里新加的br0 如果你用了别的名字的话)
  • vnc 端口(默认5900,除非你这个端口被占用否则不要改)
  • 默认目录/etc/libvirt/qemu
<domain type='kvm'>  //如果是Xen,则type=‘xen’
  <name>vm0</name> //虚拟机名称,同一物理机唯一
  <uuid>fd3535db-2558-43e9-b067-314f48211343</uuid>  //同一物理机唯一,可用uuidgen生成
  <memory>2048000</memory>
  <currentMemory>2048000</currentMemory>  //memory这两个值最好设成一样
  <vcpu>2</vcpu>            //虚拟机可使用的cpu个数,查看物理机可用CPU个数:cat /proc/cpuinfo |grep processor | wc -l
  <os>
   <type arch='x86_64' machine='q35'>hvm</type> //arch指出系统架构类型,machine 则是机器类型,查看机器类型:qemu-system-x86_64 -M ?
   <boot dev='cdrom'/>  //启动介质,第一次需要装系统可以选择cdrom光盘启动
   <bootmenu enable='yes'/>  //表示启动按F12进入启动菜单
  </os>
  <features>
   <acpi/>  //Advanced Configuration and Power Interface,高级配置与电源接口
   <apic/>  //Advanced Programmable Interrupt Controller,高级可编程中断控制器
   <pae/>   //Physical Address Extension,物理地址扩展
  </features>
  <clock offset='localtime'/>  //虚拟机时钟设置,这里表示本地本机时间
  <on_poweroff>destroy</on_poweroff>  //突发事件动作
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>   //设备配置/
   <input type=’tablet’ bus=’usb’/>//鼠标同步问题
   <emulator>/usr/bin/kvm</emulator> //如果是Xen则是/usr/lib/xen/binqemu-dm
   <disk type='file' device='disk'> //硬盘
      <driver name='qemu' type='raw'/>
      <source file='/home/gateman/data/vmDisk/kvm0.img'/>
      <target dev='vda' bus='virtio'/>  // if windows must ide   else virtio

   </disk>
   <disk type='file' device='disk'>
      <driver name='qemu' type='raw'/>
      <source file='/home/gateman/data/vmDisk/kvm1.img'/>
      <target dev='vdb' bus='virtio'/>
   </disk>
   <disk type='file' device='cdrom'>//光盘
      <driver name='qemu' type='raw'/>
      <source file='/home/gateman/mobiledisk/iso/cn_windows_7_professional_with_sp1_vl_build_x64_dvd_u_incl_virtio-140506-homemade-by-Jetso.iso'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
   </disk>
   /* 利用Linux网桥连接网络 */
   <interface type='bridge'>
      <mac address='fa:92:01:33:d4:fa'/>
      <source bridge='virbr0'/>  //配置的网桥网卡名称
      <target dev='vnet0'/>     //同一网桥下相同
      <alias name='net0'/>      //别名,同一网桥下相同
   </interface>

  <graphics type='vnc' port='5900' autoport='yes' listen='0.0.0.0' keymap='en-us'>  //配置vnc,windows下可以使用vncviewer登录,获取vnc端口号:virsh vncdisplay vm0
   <listen type='address' address='0.0.0.0'/>
  </graphics>
  </devices>
</domain>

实例

<domain type='kvm'> 
  <name>win7vm1</name>
  <uuid>fd3535db-2558-43e9-b067-314f48211343</uuid>
  <memory>4096000</memory>
  <currentMemory>4096000</currentMemory>
  <vcpu>2</vcpu>
  <os>
   <type arch='x86_64' machine='pc-i440fx-2.8'>hvm</type>
   <boot dev='cdrom'/>
   <bootmenu enable='yes'/>
  </os>
  <features>
   <acpi/>
   <apic/>
   <pae/>
  </features>
  <clock offset='localtime'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
   <emulator>/usr/bin/kvm</emulator>
   <input type='tablet' bus='usb'/>
   <disk type='file' device='disk'> 
      <driver name='qemu' type='raw'/>
      <source file='/var/lib/libvirt/images/win7vm1.img'/>
      <target dev='vda' bus='virtio'/>

   </disk>
   <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/opt/virtio_cn_windows_7_ultimate_with_sp1_x64_dvd_u_677408.iso'/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
   </disk>
    <interface type='bridge'>
      <mac address='52:54:00:08:a1:81'/>
      <source bridge='br0'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>

  <graphics type='vnc' port='5901' autoport='yes' listen='0.0.0.0' keymap='en-us'> 
   <listen type='address' address='0.0.0.0'/>
  </graphics>
  </devices>
</domain>

之后将文件保存为win7vm1.xml

基于这个配置文件创建1个虚拟机

virsh create win7vm1.xml

基于这个配置文件持久化创建和更新配置虚拟机

virsh define win7vm1.xml

# 常用命令

# 查看所有虚拟机列表:
virsh list --all

# KVM 通过virsh console连入虚拟机:
virsh console  centos7vm1

# 关闭一个名为centos7vm1的虚拟机:
virsh shutdown centos7vm1

# 设置虚拟机随着宿主主机开机自动启动:
virsh autostart centos7vm1

# 开机自动启动的虚拟机配置文件会自动在目录/etc/libvirt/qemu/autostart/目录下生成,禁用开机自动启动:
virsh autostart --disable centos7vm1

# 强制关闭虚拟机电源:
virsh destroy centos7

# 移除虚拟机定义,即从虚拟机列表中移除虚拟机:
virsh undefine centos7vm1

# 通过虚拟机配置文件重新定义虚拟机:
virsh define centos7vm1.xml

# 查看vnc端口
virsh vncdisplay + 虚机的Name或者Id

# 查看qemu端口
netstat -lntp|grep qemu

# 遇到的问题

  1. 桥接网络ping不通网关的情况:

禁用网络过滤器,向文件/etc/sysctl.conf添加以下代码:

net.bridge.bridge-nf-call-ip6tables = 0

net.bridge.bridge-nf-call-iptables = 0

net.bridge.bridge-nf-call-arptables = 0

重新加载kernel参数:

sysctl -p 

输出:

net.ipv4.ip_forward = 0 
...
net.bridge.bridge-nf-call-ip6tables = 0 
net.bridge.bridge-nf-call-iptables = 0 
net.bridge.bridge-nf-call-arptables = 0
Last Updated: 11/12/2019, 2:38:04 PM