4下KVM安装和配置,CentOS下KVM虚拟机的创建

Centos 6.4下KVM安装和配置

测试使用环境:

服务器:Dell R710 CPU型号物理CPU颗数内存大小和数量

[[email protected]
~]# cat /proc/cpuinfo | grep name | cut -d: -f2 | uniq -c

16 Intel(R) Xeon(R) CPU L5520 @ 2.27GHz

[[email protected]
~]# cat /proc/cpuinfo | grep physical | sort -n | uniq -c

16 address sizes : 40 bits physical, 48 bits virtual

8 physical id : 0

8 physical id : 1

##说明两颗物理cpu

[[email protected]
~]# dmidecode|grep -A5 “Memory Device”|grep Size | cut -d: -f2 | sort
-n | uniq -c

10 No Module Installed

8 4096 MB

##接了8根内存,每根内存大小4G

##还有相关命令

##dmidecode | grep -A16 “Memory Device” | grep Speed | sort -n | uniq
-c

[[email protected]
~]# dmidecode|grep ‘Maximum Capacity’

Maximum Capacity: 288 GB

操作系统Centos 6.4 64位系统

[[email protected]
~]# cat /etc/issue

CentOS release 6.4 (Final)

Kernel \r on an \m

本文介绍如何安装KVM,在宿主机上如何安装操作系统,添加分离磁盘,软驱,光驱和更改ISO镜像文件,如何克隆Guest系统主机。

一、检查CPU是否支持虚拟化。(在主板BIOS中开启CPU的VirtualizationTechnology)

[[email protected]
~]# uname -a

Linux localhost.localdomain 2.6.32-358.2.1.el6.x86_64 #1 SMP Wed Mar
13 00:26:49 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

[[email protected]
~]# grep -E -o ‘vmx|svm’ /proc/cpuinfo

vmx

二、安装kvm

[[email protected]
~]#yum -y install qemu-kvm libvirt python-virtinst bridge-utils

或者

##yum -y groupinstall ‘Virtualization’ ‘Virtualization Client’
‘Virtualization Platform’ ‘Virtualization Tools’

##查看模块

[[email protected]
~]# lsmod | grep kvm

kvm_intel 53484 6

kvm 316506 1 kvm_intel

a.网卡桥接配置

[[email protected]
~]# cat /etc/sysconfig/network-scripts/ifcfg-em1

DEVICE=”em1″

BOOTPROTO=”none”

HWADDR=”00:24:E8:60:49:66″

ONBOOT=”yes”

TYPE=”Ethernet”

IPADDR=192.168.18.138

NETMASK=255.255.255.192

GATEWAY=192.168.18.129

DNS1=202.102.134.68

DNS2=202.102.128.68

IPV6INIT=no

USERCTL=no

BRIDGE=br0

[[email protected]
~]# cat /etc/sysconfig/network-scripts/ifcfg-br0

DEVICE=”br0″

BOOTPROTO=”none”

HWADDR=”00:24:E8:60:49:66″

ONBOOT=”yes”

TYPE=”Bridge”

IPADDR=192.168.18.138

NETMASK=255.255.255.192

GATEWAY=192.168.18.129

DNS1=202.102.134.68

DNS2=202.102.128.68

IPV6=no

USERCTL=no

##重启网卡

/etc/init.d/network restart

[[email protected]
~]# ip route list

192.168.18.128/26 dev br0 proto kernel scope link src 192.168.18.138

192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1

169.254.0.0/16 dev br0 scope link metric 1006

default via 192.168.18.129 dev br0

[[email protected]
~]# brctl show

bridge name bridge id STP enabled interfaces

br0 8000.0024e8604966 no em1

virbr0 8000.52540098e2ed yes virbr0-nic

b.创建虚拟机(创建宿主机的Guest系统)Guest系统可以是linux或者windows等系统

我们使用管理工具virt-install

##创建存储镜像目录

[[email protected]
~]# mkdir -p /home/kvm/images

安装guest操作系统

[[email protected]
~]# virt-install –connect qemu:///system \

-n win2003 \

-r 1024 \

–vcpus=2 \

–disk path=/home/kvm/images/win2003.img,cache=writeback,size=10 \

-c /home/iso/WIN_2003.iso \

–vnc –vnclisten=0.0.0.0 –vncport=5901 –noautoconsole \

–os-type windows \

–accelerate –network bridge=br0 \

–hvm –os-variant=win2k3

##下面对virt-install命令做简单介绍,详细可以查看virt-install –help

–connect=URI

##连接到虚拟机管理程序与libvirt的URI,如qemu:///system

-r MEMORY, –ram=MEMORY

##分配虚拟机实例(Guest)内存,单位为M

-n NAME, –name=NAME

##虚拟机实例(Guest)的名称

–vcpus=VCPUS

##配置实例(Guest)CPU数目

–disk path=DISKFILE

##指定实例(Guest)存储路径及其镜像

-c CDROM, –cdrom=CDROM

##指定安装镜像所在位置和名称,它可以是一个ISO映像的路径,它也可以是一##个URL,从中提取/访问的最小引导ISO映像。

–vnc

##启用VNC远程管理,该选项不被赞同,可以用”–graphics vnc,…”代替

–vnclisten=VNCLISTEN

##可以用”–graphics vnc,listen=LISTEN,…”代替

–vncport=VNCPORT

##可以用”–graphics vnc,port=PORT,…”代替

–noautoconsole

##不要自动尝试连接到guest虚拟机控制台。默认行为是启动一个VNC客户端显##示的图形控制台

–os-type=OS_TYPE

##优化实例(Guest)配置操作系统类型如linux或windows

–os-variant=OS_VARIANT

##优化实例(Guest)配置一个特定的操作系统(如##win7,winxp,win2k3,win2k8,rhel6)

–hvm

##要求使用完全虚拟化

–accelerate

##加速该选项已经过时可以参考–virt-type qemu

-w NETWORK, –network=NETWORK,opt1=val1,opt2=val2

##实例(Guest)连接到主机的网络,网桥–network
bridge=br0,用nat路由出站##–network network=default

##==========================================

##列表(Guest)win2003

virsh -c qemu:///system list –all

##启动Guest)win2003

virsh -c qemu:///system startwin2003

##关闭(Guest)win2003

virsh -c qemu:///system destroywin2003

##编辑(Guest)win2003配置文件xml

virsh -c qemu:///system editwin2003

##删除(Guest)win2003

virsh -c qemu:///system undefinewin2003

##man virsh可以了解命令

三、我们来创建实例(Guest)win3test

1)首先创建磁盘镜像文件,

文件格式qcow2格式是kvm支持的标准格式,raw格式为虚拟磁盘文件通用格式。

创建文件名win3test.qcow2的磁盘镜像文件名,文件格式为qcow2,磁盘大小为10G

[[email protected]
~]# qemu-img create -f qcow2 /home/kvm/images/win3test.qcow2 15G

Formatting ‘/home/kvm/images/win3test.qcow2’, fmt=qcow2 size=16106127360
encryption=off cluster_size=65536

[[[email protected]
~]# qemu-img info /home/kvm/images/win3test.qcow2

image: /home/kvm/images/win3test.qcow2

file format: qcow2

virtual size: 15G (16106127360 bytes)

disk size: 264K

cluster_size: 65536

2)内存1G,2核CPU ,15G硬盘安装window 2003系统

virt-install –connect qemu:///system \

–name win3test \

–ram 1024 \

–vcpus=2 \

–disk
path=/home/kvm/images/win3test.qcow2,device=disk,format=qcow2,bus=ide,cache=writeback,size=15
\

–cdrom /home/iso/WIN_2003.iso –os-type=windows \

–network bridge=br0,model=virtio,model=e1000\

–hvm –os-variant=win2k3 \

–graphics vnc,listen=0.0.0.0,port=5902 \

–virt-type=kvm \

–noautoconsole

图片 1

四、我们来创建实例(Guest)linuxtest。

1)创建文件名linuxtest.qcow2的磁盘镜像文件,文件格式为qcow2,磁盘大小为73G

[[email protected]
~]# qemu-img create -f qcow2 /home/kvm/images/linuxtest.qcow2 73G

Formatting ‘/home/kvm/images/linuxtest.qcow2’, fmt=qcow2
size=78383153152 encryption=off cluster_size=65536

##=======================================================

[[email protected]
~]# qemu-img info /home/kvm/images/linuxtest.qcow2

image: /home/kvm/images/linuxtest.qcow2

file format: qcow2

virtual size: 73G (78383153152 bytes)

disk size: 136K

cluster_size: 65536

##=========================================================

2)内存4G,4核CPU ,73G硬盘安装centos 6.4 64位系统

[[email protected]
~]#virt-install –connect qemu:///system \

–name linuxtest \

–ram 4096 \

–vcpus=4 \

–disk
path=/home/kvm/images/linuxtest.qcow2,device=disk,format=qcow2,bus=virtio,cache=none,size=73
\

–location=” \

–os-type=linux \

–network bridge=br0,model=virtio,model=e1000\

–hvm –os-variant=rhel6 \

–graphics vnc,listen=0.0.0.0,port=5903 \

–virt-type=kvm \

–noautoconsole

4)virtio半虚拟化驱动来提高性能(微软操作系统的IO性能)

下面安装Windows server 2003 Enterprise

wget

wget

##解压

tar zxvf virtio-win-0.1-52.tar.gz

安装系统的时候记得按F6从软驱加载驱动

内存4G,4核CPU ,15G硬盘安装windows server 2003系统

[[email protected]
~]# virt-install –connect qemu:///system \

–name win3test \

–ram 4096 \

–vcpus=4 \

–disk
path=/home/kvm/images/win3test.qcow2,device=disk,format=qcow2,bus=virtio,cache=none,size=15
\

–cdrom /home/iso/WIN_2003.iso –os-type=windows \

–disk path=/home/iso/virtio-WIN2003-x86.vfd,device=floppy,perms=rw \

–network bridge=br0,model=virtio,model=e1000 \

–hvm –os-variant=win2k3 \

–graphics vnc,listen=0.0.0.0,port=5902 \

–virt-type=kvm \

–noautoconsole

##===================================

图片 2

##win3test.xml如下

[[email protected]
~]# cat /etc/libvirt/qemu/win3test.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 win3test

or other application using the libvirt API.

–>

<domain type=’kvm’>

<name>win3test</name>

<uuid>9fd1785e-430a-2aa7-050a-ed76247c7e64</uuid>

<memory unit=’KiB’>4194304</memory>

<currentMemory unit=’KiB’>4194304</currentMemory>

<vcpu placement=’static’>4</vcpu>

<os>

<type arch=’x86_64′ machine=’rhel6.4.0′>hvm</type>

<boot dev=’hd’/>

</os>

<features>

<acpi/>

<apic/>

<pae/>

</features>

<clock offset=’localtime’>

<timer name=’rtc’ tickpolicy=’catchup’/>

</clock>

<on_poweroff>destroy</on_poweroff>

<on_reboot>restart</on_reboot>

<on_crash>restart</on_crash>

<devices>

<emulator>/usr/libexec/qemu-kvm</emulator>

<disk type=’file’ device=’disk’>

<driver name=’qemu’ type=’qcow2′ cache=’none’/>

<source file=’/home/kvm/images/win3test.qcow2’/>

<target dev=’vda’ bus=’virtio’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x04′
function=’0x0’/>

</disk>

<disk type=’file’ device=’floppy’>

<driver name=’qemu’ type=’raw’ cache=’none’/>

<source file=’/home/iso/virtio-WIN2003-x86.vfd’/>

<target dev=’fda’ bus=’fdc’/>

<address type=’drive’ controller=’0′ bus=’0′ target=’0′ unit=’0’/>

</disk>

<disk type=’file’ device=’cdrom’>

<driver name=’qemu’ type=’raw’/>

<source file=’/home/iso/WIN_2003.iso’/>

<target dev=’hdc’ bus=’ide’/>

<readonly/>

<address type=’drive’ controller=’0′ bus=’1′ target=’0′ unit=’0’/>

</disk>

<controller type=’usb’ index=’0′>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x01′
function=’0x2’/>

</controller>

<controller type=’fdc’ index=’0’/>

<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:cc:a1:bd’/>

<source bridge=’br0’/>

<model type=’e1000’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x03′
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=’5902′ autoport=’no’ listen=’0.0.0.0′>

<listen type=’address’ address=’0.0.0.0’/>

</graphics>

<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>

##=============================

##红色部分为软驱,稍后要卸载(做模版的时候)

##先关机分离软驱

[[email protected]
~]# virsh -c qemu:///system destroy win3test

[[email protected]
~]#virsh detach-disk win3test fda –persistent

成功分离磁盘

4)克隆备份虚拟机

a)克隆windows server 2003模板

[[email protected]
~]#virt-clone –original win3test –name 03template –file
/home/kvm/images/03template.qcow2

或者

[[email protected]
~]#virt-clone \

–original win3test \

–name 2003template \

–file /home/kvm/images/2003template.qcow2

##编辑配置03template.xml修改vnc端口修改为5905

[[email protected]
~]# vi /etc/libvirt/qemu/03template.xml

##1)添加分离磁盘===========================

##启动(Guest)win3test

[[email protected]
~]# virsh -c qemu:///system start win3test

##创建磁盘格式raw,添加磁盘vdb

[[email protected]
~]#qemu-img create -f raw /home/kvm/images/vm-add.img 1G

##成功热加载vdb硬盘,硬盘在系统上能正常使用

[[email protected]
~]#virsh attach-disk win3test /home/kvm/images/vm-add.img vdb \

–cache none –persistent

##分离磁盘vdb

[[email protected]
iso]# virsh detach-disk win3test vdb –persistent

##======================================

##======================================

##关闭(guest)win3test

[[email protected]
~]# virsh -c qemu:///system destroy win3test

##添加IDE硬盘,文件格式qcow2(系统开启来后无法使用新增磁盘,应该是BUG)

[[email protected]
~]# qemu-img create -f qcow2 /home/kvm/images/vm-add.qcow2 1G

[[email protected]
~]# virsh attach-disk win3test /home/kvm/images/vm-add.qcow2 hde \

–cache none –persistent –subdriver qcow2

##分离IDE磁盘关,闭系统后操作

[[email protected]
iso]# virsh detach-disk win3test hde –persistent

##======================================

##======================================

##(guest)win3test关机状态的时候能成功添加,但是启动报错,应该是BUG

[[email protected]
~]#virsh attach-disk win3test /home/kvm/images/vm-add.qcow2 vdb \

–address pci:0000.00.06.0 –persistent \

–driver qemu –subdriver qcow2 –sourcetype file

virsh # start win3test

错误:开始域win3test失败

错误:internal error Process exited while reading console log output:
char device redirected to /dev/pts/0

qemu-kvm: -drive
file=/home/kvm/images/vm-add.qcow2,if=none,id=drive-virtio-disk1,format=qcow2:
could not open disk image /home/kvm/images/vm-add.qcow2: Invalid
argument

##分离vdb磁盘

[[email protected]
iso]# virsh detach-disk win3test vdb –persistent

##======================================

##======================================

##(guest)win3test开机状态添加磁盘成功,但是磁盘无法读写。因为磁盘文件为qcow2

[[email protected]
~]#virsh attach-disk win3test /home/kvm/images/vm-add.qcow2 vdb \

–persistent

##分离磁盘

[[email protected]
~]# virsh detach-disk win3test vdb –persistent

成功分离磁盘

##======================================

##2)添加分离软驱=============================

##添加fda软盘(guest)win3test关机后操作

[[email protected]
~]#virsh attach-diskwin3test/home/iso/virtio-WIN2003-x86.vfdfda \

–type floppy–persistent

##分离fda软驱

[[email protected]
~]#virsh detach-disk win3test fda –persistent

成功分离磁盘

##======================================

##3)添加分离光驱=============================

##添加分离光驱(guest)win3test关机后操作

[[email protected]~]#
virsh detach-disk win3test hdc –persistent

成功分离磁盘

##加载光驱

[[email protected]
~]#virsh attach-diskwin3test/home/iso/WIN_2003.isohdc \

–type cdrom–persistent

##======================================

##换iso镜像文件

[[email protected]
~]#virsh change-media win3test hdc /home/iso/virtio-win-0.1-52.iso

succeeded to complete action update on media

##弹出镜像文件

[[email protected]
~]#virsh change-media win3test hdc –eject

b)克隆Centos 6.2系统模板

virt-clone \

–original linuxtest \

–name Centostemplate \

–file /home/kvm/images/Centostemplate.qcow2

##列表guest系统

[[email protected]
~]#virsh -c qemu:///system list –all

Id名称状态


– linuxtest关闭

– win3test关闭

[[email protected]
~]#virt-clone \

> –original linuxtest \

> –name Centostemplate \

> –file /home/kvm/images/Centostemplate.qcow2

正在克隆linuxtest.qcow2 | 2.0 GB 00:05

Clone ‘Centostemplate’ created successfully.

##编辑xml修改vnc端口修改为5904

[[email protected]
~]#virsh -c qemu:///system edit Centostemplate

[[email protected]
~]#cat /etc/libvirt/qemu/Centostemplate.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 Centostemplate

or other application using the libvirt API.

–>

<domain type=’kvm’>

<name>Centostemplate</name>

<uuid>8b5f02f2-ee18-376a-4388-3e8ffff64c06</uuid>

<memory unit=’KiB’>4194304</memory>

<currentMemory unit=’KiB’>4194304</currentMemory>

<vcpu placement=’static’>4</vcpu>

<os>

<type arch=’x86_64′ machine=’rhel6.4.0′>hvm</type>

<boot dev=’hd’/>

</os>

<features>

<acpi/>

<apic/>

<pae/>

</features>

<clock offset=’utc’/>

<on_poweroff>destroy</on_poweroff>

<on_reboot>restart</on_reboot>

<on_crash>restart</on_crash>

<devices>

<emulator>/usr/libexec/qemu-kvm</emulator>

<disk type=’file’ device=’disk’>

<driver name=’qemu’ type=’qcow2′ cache=’none’/>

<source file=’/home/kvm/images/Centostemplate.qcow2’/>

<target dev=’vda’ bus=’virtio’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x04′
function=’0x0’/>

</disk>

<controller type=’usb’ index=’0′>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x01′
function=’0x2’/>

</controller>

<interface type=’bridge’>

<mac address=’52:54:00:47:13:79’/>

<source bridge=’br0’/>

<model type=’e1000’/>

<address type=’pci’ domain=’0x0000′ bus=’0x00′ slot=’0x03′
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=’5904’autoport=’no’ listen=’0.0.0.0′>

<listen type=’address’ address=’0.0.0.0’/>

</graphics>

<video>

<model type=’cirrus’ 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>

##===================================

##启动Centostemplate

[[email protected]
~]# virsh -c qemu:///system start Centostemplate

[[email protected]
~]# virsh -c qemu:///system list –all

Id名称状态


22 Centostemplate running

– linuxtest关闭

– win3test关闭

##用VNC连接

##登陆进系统删除网卡的MAC和70-persistent-net.rules然后重启机器(这样网卡不会冲突能启动)

[[email protected]
~]# rm /etc/udev/rules.d/70-persistent-net.rules

##关闭(Guest) Centostemplate

[[email protected]
~]#virsh -c qemu:///system destroy Centostemplate

##删除(Guest) Centostemplate

[[email protected]
~]#virsh -c qemu:///system undefined Centostemplate

##关于网桥,查看mac地址

[[email protected]
~]#brctl showmacs br0

##网桥设置mac生存期

[[email protected]
~]#brctl setageing br0 30

##xml虚拟机配置文件目录

/etc/libvirt/qemu

6.4下KVM安装和配置 测试使用环境:
服务器:Dell R710 CPU型号物理CPU颗数内存大小和数量
[[email protected]
~]# cat /proc/cpuinfo | grep name | cut -d:…

CentOS 6.5下安装KVM

CentOS下KVM虚拟机的创建、管理与迁移

kvm虚拟机管理
一、环境
role hostname ip OS
kvm_server target 192.168.32.40/24 rhel6.0_x64
vir_guest1 node4 192.168.32.34/24 rhel5.5_i386
vir_guest2 node5 192.168.32.35/24 rhel5.5_i386
manager manager 192.168.32.33/24 rhel5.5_i386
ESXi ESXi 192.168.2.251/24 ESXi 3.5
二、安裝kvm
[[email protected]
~]# yum install -y qemu-kvm.x86_64 qemu-kvm-tools.x86_64
#安装kvm内核
[[email protected]
~]# yum install libvirt.x86_64 libvirt-cim.x86_64
libvirt-client.x86_64 libvirt-java.noarch libvirt-python.x86_64
#安装virt管理工具
[[email protected]
~]# modprobe kvm #加载kvm内核
[[email protected]
~]# modprobe kvm-intel #intel
cpu加载kvm-intel内核,要支持全虚拟化,必须开启;要求cpu支持,通过bios可设置。
[[email protected]
~]# modprobe kvm-amd #amd cpu加载kvm-intel

[[email protected]
~]# modprobe -ls | grep kvm #查看内核是否开启
kernel/arch/x86/kvm/kvm.ko
kernel/arch/x86/kvm/kvm-intel.ko
kernel/arch/x86/kvm/kvm-amd.ko
[[email protected]
~]# modprobe -ls | grep kvm-intel
kernel/arch/x86/kvm/kvm-intel.ko

三、安装guest虚拟机
1、直接通过virt-manager安装、管理虚拟机(略)

2、通过命令行安装guest虚拟机
[[email protected]
~]# yum install virt-viewer #开启图形控制台安装虚拟客户机需要,
[[email protected]
~]# virt-install \ #安装选项可用virt-install –help查看
–name node4 \ #虚拟机名
–ram=1024 \ #分配内存大小,MB
–arch=x86_64 \ #模拟的CPU 构架
–vcpus=1 \ #配置虚拟机的vcpu 数目
–check-cpu \ #检查确定vcpu是否超过物理
CPU数目,如果超过则发出警告。
–os-type=linux \
#要安装的操作系统类型,例如:’linux’、’unix’、’windows’
–os-variant=rhel5 \ #操作系统版本,如:’fedora6′, ‘rhel5’,
‘solaris10’, ‘win2k’
–disk
path=/virhost/node7.img,device=disk,bus=virtio,size=20,sparse=true \
#虚拟机所用磁盘或镜像文件,size大小G
–bridge=br0 \ #指定网络,采用透明网桥
–noautoconsole \ #不自动开启控制台
–pxe #网络安装

四、利用virsh对虚拟机管理
1、开关机
[[email protected]
~]# virsh start node4 #开机
[[email protected]
~]# virsh create /etc/libvirt/qemu/node4.xml
#直接通过主机配置文档启动主机
[[email protected]
~]# virsh shutdown node4 #关机
[[email protected]
~]# virsh destroy node4 #强制关闭电源
[[email protected]
~]# virsh list –all #查看虚拟机状态

一、检查CPU是否支持虚拟化。(在主板BIOS中开启CPU的VirtualizationTechnology)

Id 名称 状态

18 node4 running

  • node5 关闭
  • win8 关闭

2、添加删除虚拟机
[[email protected]
~]# virsh define /etc/libvirt/qemu/node5.xml
#根据主机配置文档添加虚拟机
[[email protected]
~]# virsh list –all #node5已经添加

# grep -E -o ‘vmx|svm’ /proc/cpuinfo

Id 名称 状态

18 node4 running

  • node5 关闭
  • win8 关闭

[[email protected]
~]# virsh undefine node5 #移除虚拟机
[[email protected]
~]# ls /etc/libvirt/qemu
networks node4.xml win8.xml
[[email protected]
~]# virsh list –all #node5已经被移除

vmx

Id 名称 状态

18 node4 running

  • win8 关闭

3、远程管理虚拟机(qemu+ssh连接)
[[email protected]
~]# yum install virt-viewer
[[email protected]
~]# export DISPLAY=192.168.40.18:0.0
[[email protected]
~]# virt-viewer -c qemu///system node4
#本地管理虚拟机,system:获取system权限,注意qemu后是三个/
[[email protected]
~]# virt-viewer -c
qemu+ssh://[email protected]/system
node4 #远程linux通过virt-viewer+ssh管理虚拟机
Xlib: extension “RANDR” missing on display “192.168.40.18:0.0”.
[email protected]’s
password:
[email protected]’s
password:
#会弹出virt-viwer的gtk管理界面

4、使用已存在的虚拟机配置文档安裝新的虚拟机
[[email protected]
~]# qemu-img create -f qcow2 /virhost/kvm_node/node6.img 20G
#为新建虚拟机生产磁盘镜像文件

[[email protected]
~]# virsh list

二、基础环境配置

Id 名称 状态

18 node4 running

[[email protected]
~]# virsh dumpxml node4 >/etc/libvirt/qemu/node6.xml
#导出虚拟机node6的硬件配置信息为/etc/libvirt/qemu/node6.xml

[root[email protected]
~]# vim /etc/libvirt/qemu/node6.xml
<domain type=’kvm’ id=’20’> #修改node6的id号
<name>node6</name> #虚拟机node6的name
<uuid>4b7e91eb-6521-c2c6-cc64-c1ba72707fc7</uuid>
#uuid必须修改,否则会和node4的冲突
<memory>524288</memory>
<currentMemory>524288</currentMemory>
<vcpu>1</vcpu>
<os>
<type arch=’x86_64′ machine=’rhel5.4.0′>hvm</type>
<boot dev=’network’/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset=’utc’/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type=’file’ device=’disk’>
<driver name=’qemu’ type=’qcow2′ cache=’none’/>
<source file=’/virhost/node4.img’/> #指定新虚拟机的硬盘文件
<target dev=’vda’ bus=’virtio’/>
</disk>
<interface type=’bridge’>
<mac address=’54:52:00:69:d5:c7’/>
<source bridge=’br0’/>
<target dev=’vnet0’/>
<model type=’virtio’/>
</interface>
<interface type=’bridge’>
<mac address=’54:52:00:69:d5:d7’/>
<source bridge=’br0’/>
<target dev=’vnet1’/>
<model type=’virtio’/>
</interface>
<serial type=’pty’>
<source path=’/dev/pts/4’/>
<target port=’0’/>
</serial>
<console type=’pty’ tty=’/dev/pts/4′>
<source path=’/dev/pts/4’/>
<target port=’0’/>
</console>
<input type=’mouse’ bus=’ps2’/>
<graphics type=’vnc’ port=’5900′ autoport=’yes’ keymap=’en-us’/>
</devices>
</domain>

[[email protected]
~]# virsh define /etc/libvirt/qemu/node6.xml
#使用虚拟描述文档建立虚拟机,可用virsh edit node6修改node6的配置文件

[[email protected]
~]# virsh start node6
#启动虚拟机

5 为虚拟机开启vnc
[[email protected]
~]# virsh edit node4 #编辑node4的配置文件;不建议直接通过vim
node4.xml修改。
<graphics type=’vnc’ port=’-1′ autoport=’yes’ listen=’127.0.0.1′
keymap=’en-us’/>
#port=’-1′
autoport=’yes’:port自动分配,监听回环网络(virt-manager管理需要listen=’127.0.0.1’),无密码
改为
<graphics type=’vnc’ port=’5904′ autoport=’no’ listen=’0.0.0.0′
keymap=’en-us’ passwd=’xiaobai’/>
#固定vnc管理端口5904,不自动分配,vnc密码xiaobai,监听所有网络

  1. 远程vnc访问地址:192.168.32.40:5904

五、存储池和存储卷的管理
1.创建 KVM主机存储池
1).创建基于文件夹(目录)的存储池
[[email protected]
virhost]# virsh pool-define-as vmware_pool –type dir –target
/virhost/vmware
#定义存储池vmware_pool

[[email protected]
virhost]# virsh pool-create-as –name vmware_pool –type dir –target
/virhost/vmware
#创建存储池vmware_pool,类型为文件目录,/virhost/vmware,与pool-define-as结果一样

2).创建基于文件系统的存储池
[[email protected]
virhost]#virsh pool-define-as –name vmware_pool –type fs
–source-dev /dev/vg_target/LogVol02 –source-format ext4 –target
/virhost/vmware

[[email protected]
virhost]#virsh pool-create-as –name vmware_pool –type fs
–source-dev /dev/vg_target/LogVol02 –source-format ext4 –target
/virhost/vmware

3).查看存储池信息
[[email protected]
virhost]# virsh pool-info vmware_pool #查看存储域(池)信息
名称: vmware_pool
UUID: 2e9ff708-241f-fd7b-3b57-25df273a55db
状态: running
Persistent: no
自动启动: no
容量: 98.40 GB
分配: 18.39 GB
可用: 80.01 GB
4).启动存储池
[[email protected]
virhost]# virsh pool-start vmware_pool #启动存储池
[[email protected]
virhost]# virsh pool-list

1.安装KVM软件包

名称 状态 自动开始

default 活动 yes
virhost 活动 yes
vmware_pool 活动 no
5)销毁存储域,取消存储池
[[email protected]
virhost]# virsh pool-destroy vmware_pool #销毁存储池
[[email protected]
virhost]# virsh pool-list –all

# yum -y groupinstall ‘Virtualization’ ‘Virtualization Client’
‘Virtualization Platform’ ‘Virtualization Tools’

名称 状态 自动开始

default 活动 yes
virhost 活动 yes
vmware_pool 不活跃 no
[[email protected]
virhost]# virsh pool-undefine vmware_pool #取消存储池的定义
[[email protected]
virhost]# virsh pool-list –all

查看模块

名称 状态 自动开始

default 活动 yes
virhost 活动 yes

2.创建了存储池后,就可以创建一个卷,这个卷是用来做虚拟机的硬盘
[[email protected]
virhost]# virsh vol-create-as –pool vmware_pool –name node6.img
–capacity 10G –allocation 1G –format qcow2
#创建卷
node6.img,所在存储池为vmware_pool,容量10G,初始分配1G,文件格式类型qcow2

[[email protected]
virhost]# virsh vol-info /virhost/vmware/node6.img #查看卷信息
名称: node6.img
类型: 文件
容量: 10.00 GB
分配: 136.00 KB

3.在存储卷上安装虚拟主机
[[email protected]
virhost]#virt-install –connect qemu:///system \
-n node7 \
-r 512 \
-f /virhost/vmware/node7.img \
–vnc \
–os-type=linux \
–os-variant=rhel6 \
–vcpus=1 \
–network bridge=br0 \
-c /mnt/rhel-server-6.0-x86_64-dvd.iso

六、虚拟机的迁移(vmware 2 kvm)
1.安装软件
[[email protected]
~]# yum install -y virt-v2v.x86_64
[[email protected]
~]# rpm -ivh libguestfs-winsupport-1.0-7.el6.x86_64.rpm
virtio-win-1.2.0-1.el6.noarch.rpm
#虚拟机为windows时需要libguestfs-winsupport 包来支持 NTFS 文件系统和
virtio-win 包来支持 Windows para-virtualized 存储和网络设备驱动

2.创建 KVM 主机存储域(略)
virt-v2v 在迁移虚拟机的过程中,需要拷贝被迁移虚拟机到 KVM
主机预先定义的存储池中。

3.创建 KVM 主机网络接口(略)
虚拟机在迁移之后将连接 KVM 主机网络,
因此主机必须有一个与之相匹配的网络接口,比如说网桥。

4.在 KVM 主机上创建或修改 $HOME/.netrc 文件,添加 VMware ESXi
服务器的用户名和密码。
[[email protected]
~]# cat ~/.netrc
machine 192.168.2.251 login root password xxxxxx
[[email protected]
~]# chmod 0600 ~/.netrc

5.Vmware ESXi到KVM的迁移
[[email protected]
~]# virt-v2v -ic esx://192.168.2.251/?no_verify=1 -op virhost -b br0
ipserver
** HEAD

==> 401 Unauthorized
** HEAD

==> 200 OK
** GET

==> 200 OK (2084s)
unknown filesystem /dev/hda
unknown filesystem /dev/fd0
virt-v2v: Installation failed because the following files referenced in
the configuration file are required, but missing:
rhel/4/kernel-smp-2.6.9-89.EL.i686.rpm
virt-v2v: 非 virtio 驱动程序配置的 tserver21

#所有选项可通过配置文件/etc/virt-v2v.conf来指定
#-op:指定用于转换后的存储域(pool),virhost
#-b:指定网络为网桥br0
#-ic:指定要转换的源地址

[[email protected]
kvm_node]# virsh list –all

# lsmod | grep kvm

Id 名称 状态

1 node4 running

  • node5 关闭
  • tserver21 关闭
  • win8 关闭
    [[email protected]
    kvm_node]# virsh start tserver21

6.KVM到KVM迁移
[[email protected]
kvm_node]# virt-v2v -ic qemu+ssh://192.168.32.179/system -op virhost
-b br0 node6
[email protected]’s
password:
[email protected]’s
password:
unknown filesystem label SWAP-vda3
virt-v2v: The connected hypervisor does not support a machine type of
rhel5.4.0. It will be set to the current default.
virt-v2v: virtio 驱动程序配置的 node6

[[email protected]
kvm_node]# virsh list –all

kvm_intel              53484  6

Id 名称 状态

1 node4 running

  • node5 关闭
  • node6 关闭
  • tserver21 关闭
  • win8 关闭

[[email protected]
kvm_node]# virsh start node6

kvm虚拟机管理
一、环境 role hostname ip OS kvm_server target 192.168.32.40/24
rhel6.0_x64 vir_guest1 node4 192.168.32.34/2…

kvm                   316506  1 kvm_intel

2.网卡桥接配置

2.1
如果启用了NetworkManager,最好关闭该服务,因为该服务与network有冲突。

chkconfig NetworkManager off

service NetworkManager stop

2.2 关闭 selinux 并重启系统

# vi /etc/sysconfig/selinux

SELINUX=disabled

2.3 编辑网卡

# vi /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0

TYPE=Ethernet

ONBOOT=yes

BOOTPROTO=none

BRIDGE=br0

# vi /etc/sysconfig/network-scripts/ifcfg-br0

DEVICE=br0

TYPE=Bridge

ONBOOT=yes

BOOTPROTO=none

IPADDR=192.168.200.15

NETMASK=255.255.255.0

GATEWAY=192.168.200.1

DNS1=202.106.0.20

2.4 重启服务器

reboot

2.5 查看网络

# brctl show

bridge name bridge idSTP enabled

interfaces

br0 8000.ac4e914d0aa2no

eth0

virbr0 8000.5254004d5befyes

virbr0-nic

2.6 检查ip转发是否开启

# cat /etc/sysctl.conf |grep ip_forward

net.ipv4.ip_forward = 0 

3.创建虚拟机

3.1
创建磁盘镜像文件,文件格式qcow2格式是kvm支持的标准格式,raw格式为虚拟磁盘文件通用格式。

创建文件名win2008r2.qcow2的磁盘镜像文件名,文件格式为qcow2,磁盘大小为100G

# qemu-img create -f qcow2 /kvm/pjgl/win2008r2.qcow2 100G

查看磁盘镜像文件信息 

# qemu-img info /kvm/pjgl/win2008r2.qcow2

发表评论

电子邮件地址不会被公开。 必填项已用*标注