Ubuntu Server 16.04开启KVM并使用桥接网络

本次使用家庭NAS服务器,IP与MAC绑定,分配其内网地址为192.168.1.88。

0x00 安装cpu-check来查看CPU是否支持虚拟化

sudo apt install cpu-checker

确认支持KVM技术

0x01 安装软件和依赖

sudo apt install qemu qemu-kvm libvirt-bin  bridge-utils  virt-manager

0x02 修改网络类型为桥接,请注意,这里要将原有的网卡连接删除或注释
修改/etc/network/interfaces

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.1.88
gateway 192.168.1.1
netmask 255.255.255.0
dns-nameservers 192.168.1.1

bridge_ports enp3s0 
bridge_stp off
bridge_fd 0
bridge_maxwait 0

sudo service networking restart

0x03 创建磁盘镜像

qemu-img create -f raw ubuntu.raw 80G
或者
qemu-img create -f qcow2 ubuntu.qcow2 80G

关于这两种虚拟磁盘镜像格式,有一些区别。

0x04 创建启动描述文件 ubuntukvm.xml

<domain type='kvm'>
  <name>ubuntukvm</name> //虚拟机名称
  <memory>1048576</memory> //最大内存,单位k
  <currentMemory>1048576</currentMemory> //可用内存,单位k
  <vcpu>1</vcpu> //虚拟cpu个数
<os>
  <type arch='x86_64' machine='pc'>hvm</type>
  <boot dev='cdrom'/> //光盘启动
</os>
<features>
  <acpi/>
  <apic/>
  <pae/>
</features>
<clock offset='localtime'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<devices>
  <emulator>/usr/bin/qemu-system-x86_64</emulator>
  <interface type='bridge'>
    <mac address='52:54:00:84:e9:e1'/>
    <source bridge='br0'/>
    <model type='rtl8139'/>
    <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
  </interface>
  <disk type='file' device='disk'>
    <driver name='qemu' type='raw'/>
    <source file='/home/disk/.kvm/image/ubuntu.raw'/>
    <target dev='hda' bus='ide'/>
  </disk>
  <disk type='file' device='cdrom'>
    <source file='/home/disk/iso/ubuntu-16.04.4-server-amd64.iso'/> //光盘镜像路径
    <target dev='hdb' bus='ide'/>
  </disk>
  <input type='mouse' bus='ps2'/>
  <graphics type='vnc' port='-1' listen = '0.0.0.0' keymap='en-us'/>
</devices>
</domain>

其中cdrom在安装完成镜像后需要删除。

发表回复

您的电子邮箱地址不会被公开。