天天看点

虚拟化

(用户空间)软件级别虚拟化(os级别虚拟化)

   1、vmware workstation

   2、openvz

特点:

   1、只能实现完全虚拟化

   2、虚拟机的所设备都由应用层软件来模拟,所以效率低下,一般不于企业环境

   3、实现起来简便,可以造假,一般叫超载或者叫超卖

(内核空间)内核级别虚拟化(os级别虚拟化)

   kvm

   1、只能实现完全虚拟化,功能完善

   2、虚拟机的所有设备都由应用层软件来模拟,效率比软件级别虚拟化较强

(底层)hpyervisor虚拟化

   1、xen

   2、vmware esx

   3、微软hpyervior

   1、hpyervisor直接运行在硬件之上,对硬件(cpu、memory,中断)绝对的控制权,效率强劲

   2、需要一个domain0来管理除cpu、memeory、中断以外的硬件处理

   3、实现半虚拟化时需要修客户机内核,实现比较麻烦,装好软件后需要重启机器

   4、可以实现半虚拟化或者全虚拟化,需要cpu配合。

虚拟机分类:

   完全虚拟化(不需要更改操作系统的内核,所有的硬件都靠软件来模拟)

   半虚拟化(需要更改母机和虚拟机的内核,一般不需要软件来模拟,直接使用物理)

xen的逻辑结构:

below is a diagram of the xen architecture. the xen hypervisor runs directly on the hardware and is responsible for handling cpu, memory, and interrupts. it is the first program running after exiting the bootloader. on top of xen run a number of virtual machines. a running instance of a virtual machine in xen is called a domain or guest. a special domain, called domain 0 contains the drivers for all the devices in the system. domain 0 also contains a control stack to manage virtual machine creation, destruction, and configuration.

components in detail:

the xen hypervisor is an exceptionally lean (<150,000 lines of code) software layer that runs directly on the hardware and is responsible for managing cpu, memory, and interrupts. it is the first program running after the bootloader exits. the hypervisor itself has no knowledge of i/o functions such as networking and storage.

guest domains/virtual machines are virtualized environments, each running their own operating system and applications. xen supports two different virtualization modes: paravirtualization (pv) and hardware-assisted or full virtualization (hvm). both guest types can be used at the same time on a single xen system. it is also possible to use techniques used for paravirtualization in an hvm guest: essentially creating a continuum between pv and hvm. this approach is called pv on hvm. xen guests are totally isolated from the hardware: in other words, they have no privilege to access hardware or i/o functionality. thus, they are also called unprivileged domain (or domu).

the control domain (or domain 0) is a specialized virtual machine that 0 has special privileges like the capability to access the hardware directly, handles all access to the system’s i/o functions and interacts with the the other virtual machines. it also exposes a control interface to the outside world, through which the system is controlled. the xen hypervisor is not usable without domain 0, which is the first vm started by the system.

toolstack and console: domain 0 contains a control stack (also called toolstack) that allows a user to manage virtual machine creation, destruction, and configuration. the toolstack exposes an interface that is either driven by a command line console, by a graphical interface or by a cloud orchestration stack such as openstack or cloudstack.

xen-enabled operating systems: a xen domain 0 requires a xen-enabled kernel. paravirtualized guests require a pv-enabled kernel. linux distributions that are based on recent linux kernel are xen-enabled and usually contain packages that contain the xen hypervisor and tools xen (the default toolstack and console). all but legacy linux kernels are pv-enabled: in other words, they will run xen pv guests.

kvm的逻辑结构图:

xen的安装:

1、首先检查机器的cpu是否支持虚拟化,及安装xen的一些必要条件

2、配置好yum源

3、安装xen,需要重启机器,使用新的kernel来启动系统

4、安装虚拟机

   使用图形界面安装虚拟机

   使用字符或者脚本的方式安装虚拟机

5、管理和使用虚拟机

安装xen的必要条件:

● cpu support (intel vmx, amd svm)

● 使用nfs, ftp or http protocols发布红帽光盘的目录树

● 存储支持:文件、物理分区、san存储、lvm分区、iscsi

● 支持在完全虚拟化的系统列表:

   red hat enterprise linux 3 x86 optimized

   red hat enterprise linux 4 x86 optimized

   red hat enterprise linux 5 x86 optimized

   windows server 2000 32-bit     supported

   windows server 2003 32-bit     supported

   windows xp 32-bit                    supported

   windows vista 32-bit               supported

● 支持在半虚拟化的系统列表(前提是cpu支持虚拟化):

   amd32 和intel 32 架构:

       red hat enterprise linux 4 x86 optimized

       update 5 and higher

       red hat enterprise linux 5 x86 optimized(physicaladdress extension (pae) instruction set)

   amd64 和intel 64 架构:

        red hat enterprise linux 5.0 supports up to 32 cpu processor cores.

        red hat enterprise linux 5.1 supports up to 32 cpu processor cores.

        red hat enterprise linux 5.2 supports up to 64 cpu processor cores.

        red hat enterprise linux 5.3 supports up to 126 cpu processor cores.

        red hat enterprise linux 5.4 supports up to 256 cpu processor cores.

# cat /proc/cpuinfo | grep -e 'vmx|svm'        --我当前这个机器cpu是支持的

flags           : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc pni monitor ds_cpl vmx est tm2 cx16 xtpr lahf_lm

# wget ftp://192.168.0.254/pub/gls/server1.repo -p /etc/yum.repos.d/

# cat /etc/yum.repos.d/server1.repo         --vt和base是必须配置的,其它可选

[base]

name=instructor server repository

baseurl=ftp://instructor.example.com/pub/server

gpgcheck=0

[vt]

name=instructor vt repository

baseurl=ftp://instructor.example.com/pub/vt

3、安装xen

1)使用图形界面安装:system-config-packages

     或者在安装系统的时候ks.cfg文件中加入:%packages

                      @virtualization

2)使用命令行的方式来安装:

# yum install xen kernel-xen                                        --安装xen虚拟化所必须的软件

# yum install virt-manager virt-viewer libvirt libvirt-python python-virtinst    --虚拟机管理工具

3)调整系统启用新的xen kernel,并重新启动系统

# vim /boot/grub/grub.conf

default=0        --我这里是从1 title改成0

timeout=5

splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz

hiddenmenu

title red hat enterprise linux server (2.6.18-164.1.gls.el5xen)

       root (hd0,0)

       kernel /xen.gz-2.6.18-164.1.gls.el5

       module /vmlinuz-2.6.18-164.1.gls.el5xen ro root=/dev/vol0/root rhgb quiet

       module /initrd-2.6.18-164.1.gls.el5xen.img

title red hat enterprise linux server (2.6.18-164.el5)

       kernel /vmlinuz-2.6.18-164.el5 ro root=/dev/vol0/root rhgb quiet

       initrd /initrd-2.6.18-164.el5.img

# reboot

# uname -a

linux station11.example.com 2.6.18-164.1.gls.el5xen #1 smp fri sep 25 15:53:33 pdt 2009 i686 i686 i386 gnu/linux

图形界面安装虚拟机    --请参照截图

# virt-manager &

使用命令或者脚本的方式安装虚拟机

1)先生成ks.cfg文件把它放在你的安装目录树中,这里我是放在192.168.0.254的ftp服务器中

# cp ks.cfg /var/ftp/pub/

2)开始安装虚拟机

# virt-install -p -n vm02 -f /var/lib/xen/p_w_picpaths/vm02.img -s 8 -r 512 --vcpus=2 -b xenbr0 -l ftp://192.168.0.254/pub -x "ks=ftp://192.168.0.254/pub/ks.cfg"

-n           代表虚拟机的名字

-r           分配给虚拟机的内存

--vcpus      分配给虚拟机的cpu

--nographic  不适用图形界面

-f           虚拟机磁盘映像的所在地

-s           虚拟机磁盘映像的大小,单位gb

-p           半虚拟化的方式

-l           安装映像的路径

其它参数请参照virt-install --help

3)使用完全虚拟化安装windows的操作系统

虚拟机对应的文件:

   配置文件:

       # ls /etc/xen/vm*

       /etc/xen/vm02  /etc/xen/vm03

   磁盘文件:

       # ls /var/lib/xen/p_w_picpaths/        --默认路径

       vm02.img  vm03.img

   启动脚本:

       # ls /etc/init.d/xend*

       /etc/init.d/xend  /etc/init.d/xendomains

           --xend        后台服务

           --xendomains    启动guest的脚本,如果这个服务不启动那虚拟机将无法启动

1)xen自带常用管理命令:

基本管理命令:

xm list                    --显示虚拟机的状态

xm create [-c] domainname        --启动虚拟机

xm console domainname/id        --使用伪console连接虚拟机,在此模式小键盘不能使用

xm destroy domainname/id        --直接虚拟机的电源

xm reboot domainname/id        --软重启虚拟机

xm shutdown domainname/id    --软关机

xm pause            --把虚拟机的状态保存内存中

xm unpause

xm save            --把虚拟机的状态保存磁盘之上

xm restore

xm rename

监控命令:

xm top

xm dmesg

xm info

xm log

xm uptime

2)虚拟机启动管理

# service xendomains stop        --当这个服务停止时,domain0会把其它的domainn运行状态保存下来(pause),然后再关闭.

                              状态文件保存在:/var/lib/xen/save目录中,大小刚好是虚拟机的内存的大小

# service xendomains start        --启动时会从上面保存的目录中把虚拟机的状态恢复至保存那一刻的状态.

自动启动虚拟机(增加软链接)

# virsh autostart domainname

domain testserver marked as autostarted

# ls /etc/xen/auto

关闭自动启动虚拟机

# virsh autostart --disable domainname

domain testserver unmarked as autostarted

3)克隆虚拟机

   a.通过命令克隆

   b.手工克隆

a.通过命令克隆

# virsh save vm03    --在克隆之前必须关闭虚拟机或者把虚拟机设置在暂停状态.

# virt-clone -o vm03 -n newvm03 -f /var/lib/xen/p_w_picpaths/newvm03.img    --克隆

cloning /var/lib/xen/p_w_picpaths/vm03.img   4% [=                          ]  87 mb/s | 376 mb     01:27 eta

# virsh start newvm03    --启动新的虚拟机

b.手工克隆

i).先克隆磁盘镜像文件

# cd /var/lib/xen/p_w_picpaths/

# dd if=vm03.img of=newvm

ii).创建对应的配置文件

# cd /etc/xen

# cp -a vm03 newvm

# vim newvm        --修改配置文件,红色字体表示要修改的地方

name = "newvm"

uuid = "2aa1fae5-8550-affd-866f-27a9d6977d16"

maxmem = 512

memory = 512

vcpus = 2

bootloader = "/usr/bin/pygrub"

on_poweroff = "destroy"

on_reboot = "restart"

on_crash = "restart"

#vfb = [ "type=vnc,vncunused=1,keymap=en-us" ]

disk = [ "tap:aio:/var/lib/xen/p_w_picpaths/newvm.img,xvda,w" ]

vif = [ "mac=00:16:36:88:76:d4,bridge=xenbr0,script=vif-bridge" ]    --注意,修改mac地址时不要修改前三位

iii).启动克隆出来的虚拟机

# xm create newvm

虚拟机的vnc配置(明文通信):

1)修改监听端口为5901

vfb = [ "type=vnc,vncunused=1,vncdisplay=01,keymap=en-us" ]

2)修改监听地址:

vfb = [ "type=vnc,vncunused=1,vnclisten=0.0.0.0,keymap=en-us" ]

3)增加vnc访问密码:

vfb = [ "type=vnc,vncunused=1,vncpasswd=123,keymap=en-us" ]

4)给newvm虚拟机添加硬盘:

i)生成硬盘文件

# xm shutdown newvm    --先关掉虚拟机

# dd if=/dev/zero of=newvm-1.img bs=512k count=1 seek=4000000    --增加一个2tb的硬盘,

# du -sh newvm-1.img     --其实只有512k

528k    newvm-1.img

ii)在配置文件中添加硬盘

# vim /etc/xen/newvm

disk = [ "tap:aio:/var/lib/xen/p_w_picpaths/newvm.img,xvda,w","tap:aio:/var/lib/xen/p_w_picpaths/newvm-1.img,sda,w" ]

vif = [ "mac=00:16:36:88:76:d4,bridge=xenbr0,script=vif-bridge" ]

# xm create newvm -c

iii)大容量硬盘管理(2tb+)

[root@vm01 ~]# fdisk -l /dev/sda

disk /dev/sda: 2097.1 gb, 2097152524288 bytes

255 heads, 63 sectors/track, 254964 cylinders

units = cylinders of 16065 * 512 = 8225280 bytes

disk /dev/sda doesn't contain a valid partition table

gpt方式硬盘和分区的管理(mbr方式只支持小于2tb的硬盘,而gpt可以支持最大9zb硬盘及分区):

1、fdisk和parted相不兼容

2、parted命令分完区后立即生效,不需要partprobe

3、parted可以支持超过2tb分区的分区

4、不能交叉使用fdisk、parted命令去操作某个磁盘的分区

# parted /dev/sda

gnu parted 1.8.1

using /dev/sdb

welcome to gnu parted! type ‘help’ to view a list of commands.

(parted) mklabel gpt            --将mbr磁盘格式化为gpt

(parted) mkpart primary 0 100    --划分一个起始位置为0大小为100m的主分区

(parted) mkpart primary 100 200    --划分一个起始位置为100m大小为100m的主分区

(parted) mkpart primary 0 -1        --划分所有空间到一个分区

(parted) print                 --打印当前分区

(parted) quit    

#mkfs.ext3 -f /dev/sdb1

fdisk是不支持gpt磁盘,所以使用fdisk -l来查看磁盘刚才的分区时,查看会看到fdisk不支持gpt分区

5.(p2v v2p v2v)把虚拟的镜像迁移至lv中,或者逆向把lv中的虚拟机镜像迁移至硬盘上的单个文件.(可以用这个方法把物理机变成虚拟机,也可以把虚拟机变成物理机)

i)新建lv

# vgs

 vg   #pv #lv #sn attr   vsize   vfree

 vol0   1   2   0 wz--n- 292.97g 48.84g

# lvcreate -n lv_for_newvm -l 10g vol0

ii)把虚拟机关闭

# xm shutdown newvm

iii)把镜像dd至新建的lv中

# dd if=/var/lib/xen/p_w_picpaths/newvm.img of=/dev/mapper/vol0-lv_for_newvm

iv)更改配置文件把硬盘换刚才新建的那个lv

disk = [ "tap:aio:/dev/mapper/vol0-lv_for_newvm,xvda,w","tap:aio:/var/lib/xen/p_w_picpaths/newvm-1.img,xvdb,w" ]

iv)启到虚拟机

# xm create newvm -c    --如果能成功代表实验ok

6.xen的网络桥接方式:

i)   给虚拟添加一块网卡

ii)  桥接(虚拟机的网卡都桥接至真机的网卡,可以跟真机的物理网卡通信)

iii) 桥接至虚拟网卡上,只能跟所在网络的虚拟机通信

iv) 非桥接虚拟机的网络通信方式

v)  具体网卡的对应关系

vi)创建另一个虚拟网桥(vmnet1/vmnet2):

vif = [ "mac=00:16:36:88:76:d4,bridge=xenbr0,script=vif-bridge","mac=00:16:36:77:66:d5,bridge=xenbr0,script=vif-bridge" ]

ii)桥接(虚拟机的网卡都桥接至真机的网卡,可以跟真机的物理网卡通信)

iii)桥接至虚拟网卡上,只能跟所在网络的虚拟机通信

iv)非桥接虚拟机的网络通信方式

v)具体对应的网卡逻辑图:

桥接:

domain0

      真机的eth0-->vif0.0-->xenbr0--peth0-->真机的物理网卡

         eth1-->vif0.1-->xenbr0--peth0-->真机的物理网卡

domain1

   虚拟机的eth0-->vif1.0-->xenbr0-->peth0-->真机的物理网卡

         eth1-->vif1.1-->xenbr0-->peth0-->真机的物理网卡

domain2:

         eth0-->vif2.0-->xenbr0-->peth0-->真机的物理网卡

         eth1-->vif2.1-->xenbr0-->peth0-->真机的物理网卡

虚拟机的专有网络:

   虚拟机的eth0-->vif1.0-->virbr0

         eth1-->vif1.1-->virbr0

domain2

   虚拟机的eth0-->vif2.0-->virbr0

         eth1-->vif2.1-->virbr0

vi)创建另一个虚拟网桥:

# cp /etc/libvirt/qemu/networks/default.xml /etc/libvirt/qemu/networks/virbr1.xml

# vim /etc/libvirt/qemu/networks/virbr1.xml    --红色代表要改的内容

<network>

 <name>virbr1</name>

 <uuid>703619d1-1b15-4621-acc6-6f2d88f51eb9</uuid>

 <bridge name="virbr1" />

 <forward/>

 <ip address="192.168.123.1" netmask="255.255.255.0">

   <dhcp>

     <range start="192.168.123.2" end="192.168.123.254" />

   </dhcp>

 </ip>

</network>

# ln -s /etc/libvirt/qemu/networks/virbr1.xml /etc/libvirt/qemu/networks/autostart/virbr1.xml

# /etc/init.d/libvirtd restart

# ifconfig virbr1        --新的虚拟网桥已经产生了

virbr1    link encap:ethernet  hwaddr 00:00:00:00:00:00  

         inet addr:192.168.123.1  bcast:192.168.123.255  mask:255.255.255.0

         inet6 addr: fe80::200:ff:fe00:0/64 scope:link

         up broadcast running multicast  mtu:1500  metric:1

         rx packets:0 errors:0 dropped:0 overruns:0 frame:0

         tx packets:34 errors:0 dropped:0 overruns:0 carrier:0

         collisions:0 txqueuelen:0

         rx bytes:0 (0.0 b)  tx bytes:9929 (9.6 kib)

最后更改虚拟机的桥接网络,测试一下这个新网桥是否工作正常

思考:如何创建另一个网桥桥到eth1(xenbr1-->eth1)

继续阅读