百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>服务器> centos6.4快速安装和配置KVM虚拟机
分享文章到:

centos6.4快速安装和配置KVM虚拟机

发布时间:01/15 来源: 浏览: 关键词:
KVM目前已成为学术界的主流虚拟机之一,本文我们来看看在Centos6.4中,如何快速安装和配置KVM虚拟机。

centos6.4快速安装kvm

1. 开启处理器的虚拟化功能

进入BIOS,使能虚拟化功能;

进入linux,

grep -E "vmx|<a href="http://so.21ops.com/cse/search?s=9181936462520079739&entry=1&q=svm" class="bdcs-inlinelink" target="_blank">svm</a>" /proc/cpuinfo

查看是否有输出


2. 安装rpm包

yum install kvm python-virtinst libvirt libvirt-python virt-manager
virt-viewer libguestfs-tools

3. 检查内核模块kvm和kvm_intel是否已加载,否则手动加载

lsmod | grep kvm


Centos 6.4 KVM安装和配置 

测试使用环境:

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

[root@localhost ~]# cat /proc/cpuinfo | grep name | cut -d: -f2 | uniq -c

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

[root@localhost ~]# 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

 

[root@localhost ~]# 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

 

[root@localhost ~]# dmidecode|grep  'Maximum Capacity'

        Maximum Capacity: 288 GB

 

操作系统Centos 6.4 64位系统

[root@localhost ~]# cat /etc/issue

CentOS release 6.4 (Final)

Kernel \r on an \m

 

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

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

[root@localhost ~]# 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

 

[root@localhost ~]# grep -E -o 'vmx|svm' /proc/cpuinfo

vmx

二、安装kvm

[root@localhost ~]#yum -y install qemu-kvm libvirt python-virtinst bridge-utils

或者

##yum -y groupinstall 'Virtualization' 'Virtualization Client' 'Virtualization Platform' 'Virtualization Tools'

##查看模块

[root@localhost ~]#  lsmod | grep kvm

kvm_intel              53484  6

kvm                   316506  1 kvm_intel

a.网卡桥接配置

[root@localhost ~]# 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

 

[root@localhost ~]# 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

 

[root@localhost ~]# 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

 

[root@localhost ~]# 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

##创建存储镜像目录

[root@localhost ~]# mkdir -p /home/kvm/images

安装guest操作系统

[root@localhost ~]# 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 start win2003

 

##关闭(Guest) win2003

virsh -c qemu:///system destroy win2003

 

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

virsh -c qemu:///system edit win2003

 

##删除(Guest) win2003

virsh -c qemu:///system undefine win2003

 

##man virsh 可以了解命令

 

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

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

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

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

 

[root@localhost ~]# 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

 

[[root@localhost ~]# 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

 

 
Centos 6.4 KVM安装和配置 - Robby - Robby工作室

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

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

[root@localhost ~]# 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

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

[root@localhost ~]# 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位系统

 

[root@localhost ~]#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='http://mirrors.163.com/centos/6.4/os/x86_64/' \

--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 http://alt.fedoraproject.org/pub/alt/virtio-win/latest/images/bin/virtio-win-0.1-52.iso

wget http://www.linuxwind.org/download/virtio-win-0.1-52.tar.gz

##解压

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

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

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

 

[root@localhost ~]# 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

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

Centos 6.4 KVM安装和配置 - Robby - Robby工作室

 
##win3test.xml如下

[root@localhost ~]# 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>

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

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

##先关机分离软驱

[root@localhost ~]# virsh -c qemu:///system destroy win3test

[root@localhost ~]# virsh detach-disk win3test fda --persistent

成功分离磁盘

 

4)克隆备份虚拟机

a)克隆windows server 2003模板

[root@localhost ~]#virt-clone --original win3test --name 03template --file /home/kvm/images/03template.qcow2

或者

[root@localhost ~]#virt-clone \

--original win3test \

--name 2003template \

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

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

[root@localhost ~]# vi /etc/libvirt/qemu/03template.xml

 

 

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

##启动(Guest)win3test

[root@localhost ~]# virsh -c qemu:///system start win3test

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

[root@localhost ~]#qemu-img create -f raw /home/kvm/images/vm-add.img 1G

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

[root@localhost ~]#virsh attach-disk win3test /home/kvm/images/vm-add.img vdb \

--cache none ?persistent

##分离磁盘vdb

[root@localhost iso]# virsh detach-disk win3test vdb --persistent

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

 

 

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

##关闭(guest)win3test

[root@localhost ~]# virsh -c qemu:///system destroy win3test

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

[root@localhost ~]# qemu-img create -f qcow2 /home/kvm/images/vm-add.qcow2 1G

[root@localhost ~]# virsh attach-disk win3test /home/kvm/images/vm-add.qcow2 hde \

--cache none --persistent --subdriver qcow2

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

[root@localhost iso]# virsh detach-disk win3test hde --persistent

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

 

 

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

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

[root@localhost ~]#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磁盘

[root@localhost iso]# virsh detach-disk win3test vdb --persistent

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

 

 

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

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

[root@localhost ~]#virsh attach-disk win3test /home/kvm/images/vm-add.qcow2 vdb \

--persistent

##分离磁盘

[root@localhost ~]# virsh detach-disk win3test vdb --persistent

成功分离磁盘

 

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

 

 

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

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

[root@localhost ~]#virsh attach-disk win3test /home/iso/virtio-WIN2003-x86.vfd fda \

--type floppy ?persistent

##分离fda软驱

[root@localhost ~]#virsh detach-disk win3test fda --persistent

成功分离磁盘

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

 

 

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

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

[root@localhost~]# virsh detach-disk win3test hdc --persistent

成功分离磁盘

##加载光驱

[root@localhost ~]#virsh attach-disk win3test /home/iso/WIN_2003.iso hdc \

--type cdrom ?persistent

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

 

 

##换iso镜像文件

[root@localhost ~]#virsh change-media win3test hdc /home/iso/virtio-win-0.1-52.iso

succeeded to complete action update on media

##弹出镜像文件

[root@localhost ~]#virsh change-media win3test hdc --eject

 

b)克隆Centos 6.2系统模板

virt-clone \

--original linuxtest \

--name Centostemplate \

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

 

##列表guest系统

[root@localhost ~]#virsh -c qemu:///system list --all

 Id    名称                         状态

----------------------------------------------------

 -     linuxtest                      关闭

 -     win3test                       关闭

 

[root@localhost ~]#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

[root@localhost ~]#virsh -c qemu:///system edit Centostemplate

 

[root@localhost ~]#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

[root@localhost ~]# virsh -c qemu:///system start Centostemplate

 

[root@localhost ~]# virsh -c qemu:///system list --all

 Id    名称                         状态

----------------------------------------------------

 22    Centostemplate                 running

 -     linuxtest                      关闭

 -     win3test                       关闭

 

##用VNC连接

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

[root@localhost ~]# rm /etc/udev/rules.d/70-persistent-net.rules

 
Centos 6.4 KVM安装和配置 - Robby - Robby工作室

 

##关闭(Guest) Centostemplate

[root@localhost ~]#virsh -c qemu:///system destroy Centostemplate

 

##删除(Guest) Centostemplate

[root@localhost ~]#virsh -c qemu:///system undefined Centostemplate

 

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

[root@localhost ~]#brctl showmacs br0

 

##网桥设置mac生存期

[root@localhost ~]#brctl setageing br0 30

 

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

/etc/libvirt/qemu


打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有4人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板