1,查看是否支持硬件虚拟化
egrep --color=auto ‘(vmx|xvm)‘ /proc/cpuinfo
2,安装xen
yum -y install xen kernel-xen
3,title CentOS (3.7.4-1.el6xen.x86_64)
root (hd0,0)
kernel /xen.gz dom0_mem=512M cpufreq=xen dom0_max_vcpus=1 dom0_vcpus_pin
module /vmlinuz-3.7.4-1.el6xen.x86_64 ro root=UUID=44a2a303-b91c-46cf-9fde-fd8119b9cb1c rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD LANG=zh_CN.UTF-8 rd_NO_LVM crashkernel=auto rhgb quiet rd_NO_DM rhgb quiet
module /initramfs-3.7.4-1.el6xen.x86_64.img
4,创建映像文件
dd if=/dev/zero of=/data/xen/test1.img bs=1M oflag=direct seek=102399 count=1
5,创建桥设备
cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-br0
[root@node202 network-scripts]# cat ifcfg-eth0
DEVICE="eth0"
BOOTPROTO="none"
#HWADDR="00:0C:29:48:E3:01"
IPV6INIT="yes"
MTU="1500"
NM_CONTROLLED="no"
ONBOOT="yes"
TYPE="Ethernet"
#UUID="8331e082-ba65-43bc-9e98-12dedf151d87"
BRIDGE=br0
[root@node202 network-scripts]# cat ifcfg-br0
DEVICE="br0"
BOOTPROTO="none"
#HWADDR="00:0C:29:48:E3:01"
IPV6INIT="yes"
MTU="1500"
NM_CONTROLLED="yes"
ONBOOT="yes"
IPADDR=192.168.100.202
NETMASK=255.255.255.0
TYPE="Bridge"
#UUID="8331e082-ba65-43bc-9e98-12dedf151d87"
6,创建一个配置文件
#vim /etc/xen/test1
kernel = "/tmp/vmlinuz"
ramdisk = "/tmp/initrd.img"
memory = 512
name = "test1"
vcpus = 1
vif = [ ‘bridge=br0‘ ]
disk = [ ‘file:/data/xen/test1.img,xvda,w‘ ]
7,启动虚拟机
xm create test1
安装系统
8,改配置文件重起系统
[root@node202 xen]# vim test1
memory = 512
bootloader="/usr/bin/pygrub"
vcpus=1
on_reboot = ‘restart‘
on_crash = ‘destroy‘
name = "test1"
vif = [ ‘bridge=br0‘ ]
disk = [ ‘file:/data/xen/test1.img,xvda,w‘ ]
------------
disk = [ "phy:sdc,xvda,w",‘file:/data/xen/test1.img,xvda,w‘ ]
phy表示物理磁盘
vif = [ "mac=xxxxx,bridge=br0,script=vif-bridge" ]
-----------------
xm create test1
当启动虚拟机后会有一个虚拟网卡桥到本机
vif6.0 Link encap:Ethernet HWaddr FE:FF:FF:FF:FF:FF
inet6 addr: fe80::fcff:ffff:feff:ffff/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4 errors:0 dropped:0 overruns:0 frame:0
TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:32
RX bytes:272 (272.0 b) TX bytes:1404 (1.3 KiB)
本机会有一个仅主机的虚拟网卡
virbr0 Link encap:Ethernet HWaddr 52:54:00:95:6C:DE
inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:272 (272.0 b) TX bytes:0 (0.0 b)
可以拷贝以前的img文件直接启动另一个虚拟机
完成
~
io半虚拟化设备分为前端和后端,前端在个domU上
后端在dom0上,domU有一个特殊驱动能识别前端的这个特殊的设备
如:( /lib/modules/2.6.32-431.el6.x86_64/kernel/drivers/net/xen-netfront.ko ),
并且在处理数据的时候只是简单的处理,然后交给后端,这样大大提高了性能
如果io是完全虚拟化,domU看到的是一个真正的设备(虚拟的)
在发生数据时前端后端都要对数据进行处理,影响性能
区分虚拟和模拟的一个重要标准是,在模拟中虚拟机的任何cpu指令都是运行在vcpu
上的,而在虚拟中非特权指令是直接运行在物理cpu上的,只有特权指令才交给vmm
翻译执行
io的半虚拟化和cpu的半虚拟没有关系,cpu半虚拟化,io可以完全虚拟化
但是明显半虚拟化性能好
虚拟需要cpu支持硬件虚拟化,就是cpu在生产的时候有五个ring
宿主机的kernel运行在ring -1 ,
完全虚拟化的两种加速方式:BT HVM
xen的半虚拟化:
xen运行在硬件上直接管理cpu memory 另外运行一个特权域
管理io和其他的设备 各虚拟机知道自己运行在虚拟的环境中(因为kernel经过修改)
对特权指令的调用会发起:hyper call,而不是直接运行 非特权还是直接在Cpu上运行
[root@localhost tmp]# yum -y install xen-4.1.2-20.el6.x86_64.rpm xen-runtime-4.1.2-20.el6.x86_64.rpm xen-libs-4.1.2-20.el6.x86_64.rpm xen-licenses-4.1.2-20.el6.x86_64.rpm xen-hypervisor-4.1.2-20.el6.x86_64.rpm kernel-xen-3.7.4-1.el6xen.x86_64.rpm kernel-xen-release-6-4.noarch.rpm bridge-utils-1.5-2.el6.x86_64.rpm kernel-xen-firmware-3.7.4-1.el6xen.x86_64.rpm
title CentOS (3.7.4-1.el6xen.x86_64)
root (hd0,0)
kernel /xen.gz dom0_mem=512M cpufreq=xen dom0_max_vcpus=1 dom0_vcpus_pin
module /vmlinuz-3.7.4-1.el6xen.x86_64 ro root=UUID=44a2a303-b91c-46cf-9fde-fd8119b9cb1c rd_NO_LUKS KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD LANG=zh_CN.UTF-8 rd_NO_LVM crashkernel=auto rhgb quiet rd_NO_DM rhgb quiet
module /initramfs-3.7.4-1.el6xen.x86_64.img
service xend start
xm list
xl info
xl help
xen虚拟状态:
r:
b: 阻塞
p: 暂停
s: 停止
c: 崩溃
d: dying, 正在关闭的过程中
dd if=/dev/zero of=centos.img bs=1M oflag=direct seek=1023 count=1
# mount -o loop centos.img /mnt
xm的配置文件:
kernel:内核
ramdisk: initramfs或initrd
name: 域名称
memory: 内存大小
disk: 磁盘设备文件列表,格式disk=["disk1", "disk2",], 每个disk都由三个参数进行定义:“backend-dev”,“frontend-dev”,“mode”
backend-dev: 有两种类型,物理设备,虚拟磁盘映像文件,格式为分别为phy:device和file:/path/to/image_file;
front-dev: 定义其在DomU中设备类型;虚拟磁盘映像文件对应的设备文件名称通常为xvd[a-z]
mode: 访问权限模型,r, w
vcpus: 虚拟CPU的个数;
root: 根文件系统所在的设备;
extra: 传递给内核的额外参数;selinux=0
on_reboot: 执行xm reboot命令时的操作,有destroy和restart;
on_crash: 有destroy, restart, preserve(保存崩溃时的信息以用于调试)
vif :vif = [‘ip="172.16.100.11", bridge=br0‘]
type: 设备类型,默认为netfront
mac: 指定mac地址;
bridge: 指定桥接到的物理设备
ip: ip地址;
script: 配置此接口的脚本文件
vifname: 后端设备名称
bootloader: 引导器文件的路径,一般指的PyGrub的路径;
xm console centos6
[root@localhost network-scripts]# cat ifcfg-eth0
DEVICE="eth0"
BOOTPROTO="none"
IPV6INIT="no"
NM_CONTROLLED="no"
ONBOOT="yes"
TYPE="Ethernet"
BRIDGE="br0"
[root@localhost network-scripts]# cat ifcfg-br0
DEVICE="br0"
BOOTPROTO="none"
IPADDR="192.168.100.26"
IPV6INIT="no"
MTU="1500"
NETMASK="255.255.255.0"
NM_CONTROLLED="no"
ONBOOT="yes"
TYPE="Bridge"
# brctl stp br0 on
在配置文件/etc/xen配置kernel initrd 为安装引导的 定义好vcpu memory disk
vif = [ ‘dridge=br0‘]
disk = [ ‘file://xen/vm2/centos6.img,xvda,w‘]
然后
xm create -c centos6
xm list
xm destory centos6
安装好后 修改/etc/xen kernel........
bootloader = "/usr/bin/pugrub"
只要写了bootloader后就不需要在写kernel initrd root extra
因为bootloader会读取虚拟磁盘上的启动引导扇区,加载kernel
样例配置文件有两种xm and xl
显示采用c/s架构vfb帧缓冲,在虚拟机启动一个图形显示请求,然后发送给dom0
在dom0安装tigervnc-server
启动vcn: vncserver :1
qemu-img -f qcow2 -o size=120G,preallocation=metadata /xen/xm1/centos6.qcow2
qemu-img info /xen/xm1/centos6.qcow2
使用了bootloader, pygrup示例:
#ramdisk="/boot/initramfs-2.6.32-358.el6.x86_64.img"
#kernel="/boot/vmlinuz-2.6.32-358.el6.x86_64"
name="linux"
vcpus=1
memory=128
disk=[‘file:/xen/vm2/dom2.img,xvda,w‘,]
bootloader="/usr/bin/pygrub"
#root="/dev/xvda2 ro"
#extra="selinux=0 init=/sbin/init"
vif=[ ‘bridge=br0‘ ]
on_crash="destroy"
on_reboot="restart"
使用Dom0中的kernel和ramdisk引导的示例:
ramdisk="/boot/initramfs-2.6.32-358.el6.x86_64.img"
kernel="/boot/vmlinuz-2.6.32-358.el6.x86_64"
name="test"
vcpus=1
memory=128
disk=[‘file:/xen/vm1/test.img,xvda,w‘,]
root="/dev/xvda ro"
extra="selinux=0 init=/sbin/init"
自定义安装,并启用了vnc功能:
#ramdisk="/xen/isolinux/initrd.img"
#kernel="/xen/isolinux/vmlinuz"
name="rhel6"
vcpus=2
memory=512
disk=[‘file:/xen/vm3/rhel6.img,xvda,w‘,]
bootloader="/usr/bin/pygrub"
#root="/dev/xvda2 ro"
#extra="selinux=0 init=/sbin/init"
#extra="ks=http://172.16.0.1/rhel6.x86_64.cfg"
vif=[ ‘bridge=br0‘ ]
on_crash="destroy"
on_reboot="destroy"
vfb=[ ‘vnc=1,vnclisten=0.0.0.0‘ ]
yum -y install libvirt
service libvirtd start
yum install virt-manager
yum -y install virt-viewer
virt-install -n "centos6.4" -r 512 --vcpu=1 -l http://172.16.0.1/centos6.4/ --disk path=/xen/vm1/centos6.4.qcow --network bridge=br0 --force
----------------------------------------
kvm
# grep -Ei --color=auto "vmx|svm" /proc/cpuinfo
1,先配置好桥
2,安装包,模块
# modprobe kvm_intel
# modprobe kvm
# lsmod | grep kvm
kvm_intel 54285 0
kvm 333172 1 kvm_intel
yum -y install libvirt
service libvirtd start
yum install virt-manager
yum -y install virt-viewer qemu-kvm qemu-kvm-tools virt-install
3,create disk img
# qemu-img create -f qcow2 -o preallocation=metadata /images/vm2/test.qcow2 10G
# qemu-img resize /images/vm2/test.qcow2 +10G 增到10G
Image resized.
# qemu-img info /images/vm2/test.qcow2
image: /images/vm2/test.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 1.7M
cluster_size: 65536
转换格式:
# qemu-img create -f vmdk -o ? test.vmdk #模拟查看相关的帮助信息
Supported options:
size Virtual disk size
adapter_type Virtual adapter type, can be one of ide (default), lsilogic, buslogic or legacyESX
backing_file File name of a base image
compat6 VMDK version 6 image
# qemu-img convert -O vmdk -o adapter_type=lsilogic /images/vm2/test.qcow2 /images/vm2/test.vmdk
转换成VMware的vmdk格式
# qemu-img info /images/vm2/test.vmdk
image: /images/vm2/test.vmdk
file format: vmdk
virtual size: 20G (21474836480 bytes)
disk size: 16K
创建快照
# qemu-img snapshot -l /images/vm1/rhel6.qcow2 创建之前查看是否有快照;建议不要同名
# qemu-img snapshot -c rhel6-1.snap /images/vm1/rhel6.qcow2
# qemu-img snapshot -l /images/vm1/rhel6.qcow2
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
1 rhel6-1.snap 0 2014-05-25 10:51:34 00:00:00.000
Parameters to snapshot subcommand: #详细帮助
‘snapshot‘ is the name of the snapshot to create, apply or delete
‘-a‘ applies a snapshot (revert disk to saved state) 应用快照
‘-c‘ creates a snapshot 创建快照
‘-d‘ deletes a snapshot ***快照
‘-l‘ lists all snapshots in the given image 查看快照列表
4,安装
为了显示方便;需要安装vnc软件
# yum -y install tigervnc tigervnc-server
#ln -sv /usr/libexec/qemu-kvm /usr/bin
#qemu-img create -f qcow2 -o size=100G,preallocation=metadata /data/test3.img
qemu-kvm -name "redhat" -m 512 -smp 1 -drive file=/data/test3.img,\
> if=virtio,index=0,media=disk,format=qcow2 \
> -drive file=/root/rhel-server-6.4-i386-dvd.iso,media=cdrom,index=1 \
> -boot order=d
使用# vncviewer :5900连接查看,如果打不开安装下面的包
#yum install xorg-x11-server-Xorg
-------------
以pxe引导安装
#需要网卡支持;所以需要写一个配置网卡接口的脚本;默认在/etc/qemu-ifup
# vim /etc/qemu-ifup
#!/bin/bash
#
switch=br0
if [ -n $1 ];then
ifconfig $1 up
sleep 1
brctl addif $switch $1
exit 0
else
echo "Error: No Specifed interface."
exit 1
fi
# chmod +x /etc/qemu-ifup
#脚本能自动调用命令中ifname=""网卡的名称
# qemu-kvm -name "test1" -m 512 -smp 1 -drive file=/data/test1.qcow2,media=disk,if=virtio,index=0,format=qcow2 -net nic,model=virtio -net tap,ifname=vnet0,downscript=no -boot order=c
VNC server running on `::1:5900‘
# vncviewer :5900
-----pxe安装virt-install
# virt-install --name "centos6" -r 512 --vcpus 2 --disk path=/images/vm2/centos.img,size=120 --network bridge=br0,model=virtio --pxe --force
用virt-manager查看,如果有乱码安装一下包
#yum install dejavu-lgc-sans-fonts
如果键盘不一致
open-->deftails-->dispiay VNC-->keymap=en-us
原文地址:http://evildexy.blog.51cto.com/8607019/1423607