KVM虚拟机使用virsh命令创建带操作系统image
说明:以下整个过程均实测可行,如配完不能正常启动镜像操作系统,核查虚拟机配置xml文件是否存在问题,网络配置是否存在问题。
1、检测CPU是否支持虚拟化
vmx为Intel系列虚拟化,采用Intel-VT技术,svm为AMD系列,采用AMD-V技术
注意:请务必开启以下虚拟化选项,否则无法加载kvm
--Windows:BIOS需要开启 virtualization technology enabled
--虚拟机需要开启:虚拟机-->设置-->硬件-->处理器-->选择 虚拟化xxx或 VT-X/AMD-v -->重启
[email protected]:~# egrep -o "(vmx|svm)" /proc/cpuinfo
vmx
vmx
vmx
vmx
#如开启后仍不行,重启机器。
2、安装KVM并启用KVM内核模块
1)安装
[email protected]:~# apt-get install qemu-kvm ubuntu-vm-builder bridge-utils
2)启用KVM内核模块
[email protected]:~# modprobe kvm
[email protected]:~# modprobe kvm_intel
[email protected]:~# modprobe kvm_amd
3)检测
[email protected]:~# kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used
4)添加用户root到kvm组
[email protected]:~# adduser root kvm
正在添加用户"root"到"kvm"组...
Adding user root to group kvm
完成。
3、安装libvirt
建议直接apt-get 安装,源码安装需要解决一系列依赖关系和配置问题,如不能安装,先 apt-get update
1)、安装 libvirt-bin qemu virt-manager
[email protected]:~# apt-get install libvirt-bin qemu virt-manager
2)、启动服务并确认
[email protected]:~# service libvirtd restart
[email protected]:~# ps -ef|grep libvirt
libvirt+ 1743 1 0 14:40 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper
root 1744 1743 0 14:40 ? 00:00:00 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/lib/libvirt/libvirt_leaseshelper
root 5284 1 2 14:47 ? 00:00:00 /usr/sbin/libvirtd
root 5355 1910 0 14:47 pts/0 00:00:00 grep --color=auto libvirt
[email protected]:~# virsh list --all
Id 名称 状态
----------------------------------------------------
3)、virt-manager 进入管理界面
注意:需要先 ssh –X 否则无法进入
[email protected]:~$ ssh -X localhost
The authenticity of host 'localhost (::1)' can't be established.
ECDSA key fingerprint is SHA256:24GN5wsSd6oXuBIjpltZJV6F17qmuxhlDQKvyM8GSMI.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
[email protected]'s password:
Welcome to Ubuntu 16.04.6 LTS (GNU/Linux 4.4.0-142-generic i686)
Last login: Sun Mar 15 15:45:38 2020 from 192.115.8.101
[email protected]:~$ virt-manager
4、配置虚拟网络:以下分别为配置静态ip和静态ip网桥
1)查看网关
[email protected]:/etc/network# ip route
default via 192.115.8.1 dev ens33
192.115.8.0/24 dev ens33 proto kernel scope link src 192.115.8.108
192.115.122.0/24 dev virbr0 proto kernel scope link src 192.115.122.1 linkdown
2)、配置静态ip
[email protected]:/etc/network# cp interfaces interfaces.bak0315
[email protected]:/etc/network# cat 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 primary network interface
150131-116060-999358
auto ens33
iface ens33 inet static
address 192.115.8.112
#配置没有被使用的ip地址
netmask 255.255.255.0
#写入网关地址ip route 查看
gateway 192.115.8.1
#写dns解析地址
dns-nameservers 1.1.1.1
[email protected]:/etc/network#
[email protected]:~# /etc/init.d/networking restart
[ ok ] Restarting networking (via systemctl): networking.service.
[email protected]:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
link/ether 00:0c:29:b3:83:2e brd ff:ff:ff:ff:ff:ff
inet 192.115.8.112/24 brd 192.115.8.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:feb3:832e/64 scope link
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:a6:65:81 brd ff:ff:ff:ff:ff:ff
inet 192.115.122.1/24 brd 192.168.115.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:a6:65:81 brd ff:ff:ff:ff:ff:ff
3)、配置网桥
按道理修改下网关等相关信息,重启网络即可,因虚拟机等问题如不能通,可能还要重启下主机
[email protected]:~# cd /etc/network/
[email protected]:/etc/network# cat interfaces
auto lo
iface lo inet loopback
auto ens33
iface ens33 inet static
address 192.115.8.112
#配置没有被使用的ip地址
netmask 255.255.255.0
broadcast 192.115.8.125
#写入网关地址ip route 查看
gateway 192.115.8.1
#写dns解析地址
dns-nameservers 1.1.1.1
#dns-nameservers 8.8.8.8
auto brdg33
iface brdg33 inet static
address 192.115.8.112
#network 192.115.800.0
netmask 255.255.255.0
broadcast 192.115.8.125
gateway 192.115.8.1
dns-nameservers 8.8.8.8
bridge_ports ens33
bridge_stp off
bridge_fd 0
bridge_maxwait 0
#bridge_maxage 12
[email protected]:/etc/network#
[email protected]:~$ ifconfig brdg33
brdg33 Link encap:以太网 硬件地址 00:0c:29:b3:83:2e
inet 地址:192.115.8.112 广播:192.115.8.125 掩码:255.255.255.0
inet6 地址: fe80::20c:29ff:feb3:832e/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 跃点数:1
接收数据包:12723 错误:0 丢弃:0 过载:0 帧数:0
发送数据包:6777 错误:0 丢弃:0 过载:0 载波:0
碰撞:0 发送队列长度:1000
接收字节:3665385 (3.6 MB) 发送字节:3661879 (3.6 MB)
[email protected]:/etc/network# ping www.baidu.com
PING www.a.shifen.com (163.177.151.109) 56(84) bytes of data.
64 bytes from 163.177.151.109: icmp_seq=1 ttl=56 time=6.30 ms
64 bytes from 163.177.151.109: icmp_seq=2 ttl=56 time=6.19 ms
64 bytes from 163.177.151.109: icmp_seq=3 ttl=56 time=6.26 ms
^C
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2017ms
rtt min/avg/max/mdev = 6.198/6.256/6.304/0.101 ms
[email protected]:/etc/network#
5、virsh命令创建image
特别说明:笔者主机性能较差,选用ubuntu-12.10不加载任何模块安装,性能较优主机可以自行选择其他系统,命令并无区别。
1)、定义虚拟机片配置文件模板
模板见文末
[email protected]:/image#cp template.xml ubuntu-12.10.xml
2)、上传安装包到安装路径
[email protected]:/image# ls ubuntu-12.10-server-i386.iso
ubuntu-12.10-server-i386.iso
3)、定义UUID
[email protected]:/image# UUID=`uuidgen`
[email protected]:/image# sed -i "s,%UUID%,$UUID,g" ubuntu-12.10.xml
[email protected]:/image# grep uuid ubuntu-12.10.xml
<uuid>973e2bbc-3f94-4bbc-8448-05cff5e8c947</uuid>
4)、使用qemu-img 创建一个raw格式的image,并用绝对路径替换模板中路径
[email protected]:/image# qemu-img create -f raw ubuntu-12.10.raw 10G
Formatting 'ubuntu-12.10.raw', fmt=raw size=10737418240
[email protected]:/image# sed -i "s,%IMAGE_PATH%,/image/ubuntu-12.10.raw,g" ubuntu-12.10.xml
[email protected]:/image# grep '/image/ubuntu-12.10.raw' ubuntu-12.10.xml
<source file='/image/ubuntu-12.10.raw'/>
4)、替换模板中安装系统的虚拟光盘的绝对路径
[email protected]:/image# sed -i "s,%ISO_PATH%,/image/ubuntu-12.10-server-i386.iso,g" ubuntu-12.10.xml
[email protected]:/image# grep 'ubuntu-12.10-server-i386.' ubuntu-12.10.xml
<source file='/image/ubuntu-12.10-server-i386.iso'/>
5)、配置虚拟网卡,需要保证网桥brdg33已启用,使用如下命令生成并替换模板文件
[email protected]:/image# MAC="fa:95:$(dd if=/dev/urandom count=1 2>/dev/null|md5sum|sed 's/^\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4/')"
[email protected]:/image# sed -i "s,%MAC%,$MAC,g" ubuntu-12.10.xml
[email protected]:/image# grep 'fa:95' ubuntu-12.10.xml
<mac address='fa:95:c2:52:70:d8'/>
[email protected]:/image# MAC2="52:54:$(dd if=/dev/urandom count=1 2>/dev/null|md5sum|sed 's/^\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4/')"
[email protected]:/image# sed -i "s,%MAC2%,$MAC2,g" ubuntu-12.10.xml
[email protected]:/image# grep '52:54' ubuntu-12.10.xml
<mac address='52:54:78:23:73:6d'/>
6)、启动虚拟机
---定义虚拟机域,define命令作用在于,将虚拟机提交给libvirt管理。
[email protected]:/image# virsh define ubuntu-12.10.xml
定义域 ubuntu-12.10(从 ubuntu-12.10.xml)
---定义完成后,使用virsh list 命令查看虚拟机状态
[email protected]:/image# virsh list --all
Id 名称 状态
----------------------------------------------------
- ubuntu-12.10 关闭
--开启虚拟机,并确认状态
[email protected]:/image# virsh start ubuntu-12.10
域 ubuntu-12.10 已开始
[email protected]:~$ virsh list --all
Id 名称 状态
----------------------------------------------------
2 ubuntu-12.10 running
7)、查看虚拟机vnc端口
[email protected]:/image# virsh vncdisplay ubuntu-12.10
:0
8)、安装vncviewer并连接虚拟机
语法 vncviewer ip:port
[email protected]:/image# apt-get install vncviewer
[email protected]:~$ virsh list --all
Id 名称 状态
----------------------------------------------------
2 ubuntu-12.10 running
---此外,如果是通过VMware等虚拟机安装的宿主机Ubuntu,原VMware宿主机Windows上还需要安装Xmanager ,否则vncviewer 无法打开
[email protected]:~$ vncviewer 192.115.8.112:0
Connected to RFB server, using protocol version 3.8
No authentication needed
Authentication successful
Desktop name "QEMU (ubuntu-12.10)"
VNC server default format:
32 bits per pixel.
Least significant byte first in each pixel.
True colour: max red 255 green 255 blue 255, shift red 16 green 8 blue 0
Using default colormap which is TrueColor. Pixel format:
32 bits per pixel.
Least significant byte first in each pixel.
True colour: max red 255 green 255 blue 255, shift red 16 green 8 blue 0
Same machine: preferring raw encoding
--此处Xmanager 会弹出如下Ubuntu-12.10镜像安装界面,操作同系统安装,不赘述。
安装完成后,重新使用vncviewer 192.115.8.112:0会进入如下登录界面。
一些建议:笔者使用 virt-manager CPU跑满,直接安装卡死在Xmanager安装首页,故CPU性能不足,不建议使用virt-manager。
9)、建议在Windows上安装TightVNC Viewer,确保宿主机与Windows在同一网络即可使用192.115.8.112:0直连后进入安装页面。
该方法安装速度较快,建议使用该方法先生成image。
进入后界面同上
10)、TightVNC Viewer安装完成后,即可使用VNC登录主机
附:template.xml
<domain type='kvm'>
<name>%VM_NAME%</name>
<uuid>%UUID%</uuid>
<memory>1048576</memory>
<currentMemory>1048576</currentMemory>
<vcpu>1</vcpu>
<os>
<type arch='i686' machine='pc-0.14'>hvm</type>
<boot dev='hd'/>
<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>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='%IMAGE_PATH%'/>
<target dev='vda' bus='virtio'/>
<alias name='virtio-disk0'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='%ISO_PATH%'/>
<target dev='hdb' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='1' unit='0'/>
</disk>
<controller type='ide' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<controller type='fdc' index='0'/>
<interface type='bridge'>
<mac address='%MAC%'/>
<source bridge='brdg33'/>
<target dev='vnet0'/>
<alias name='net0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<interface type='network'>
<mac address='%MAC2%'/>
<source network='default'/>
<target dev='vnet7'/>
<alias name='net1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</interface>
<serial type='pty'><target port='0'/></serial>
<console type='pty'><target type='serial' port='0'/></console>
<input type='tablet' bus='usb'/><input type='mouse' bus='ps2'/>
<graphics type='vnc' port='5900' autoport='yes' listen='0.0.0.0' keymap='en-us'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<sound model='ich6'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</sound>
<video>
<model type='vga' vram='9216' heads='1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</memballoon>
</devices>
</domain>