Xen

 

         Xen是一種開源的虛擬機監控器(VMM),屬於Type-I;支持全虛擬化和半虛擬化;html

         Xen的Hypervisor直接運行在硬件之上,其所建立的虛擬機都要運行在Hypervisor上;Xen只對CPU和內存進行了虛擬化,也就是說Xen只負責管理驅動CPU和內存其餘的像IO等設備都是交給Dom0中的操做系統管理的,本身不負責管理,這是由於IO的種類不少,須要開發不少的驅動程序,是比較麻煩的,而且操做系統已經能夠支持大多數IO設備了,直接使用豈不爽哉!前端

         Xen管理虛擬機的方式:linux

                   Xen將每一個運行在其上的虛擬機都稱之爲Domain,每一個虛擬機具備一個Domain號,其中第一個虛擬機被稱爲Dom0,其餘的以此類推;這些Domain能夠分爲兩類,即Domain0和其餘DomainU#;vim

                   Xen安裝之後會自動運行一個名爲Dom0的虛擬機,接下來的Xen的全部管理虛擬機的功能都是經過這個Dom0來實現的;這個Dom0既爲用戶管理其餘虛擬機提供了接口,也負責接受其餘虛擬機的IO調用請求(即DomU#的CPU和內存是由Hypervisor管理分配的,IO是由Dom0管理的),因此Dom0也被稱爲Privileged Domain(特權域);windows

                   Dom0也具備用戶空間和內核空間;後端

                   在Xen中虛擬出來的CPU被當作一個進程或線程,而後Xen Hypervisor將這些線程或進程調度到物理CPU的某個核心上,從而實現CPU的虛擬化;而內存的虛擬能夠理解爲Hypervisor對真實的物理內存進行了二次映射,即Hypervisor對物理內存空間進行一遍映射,處於DomU中的虛擬機又對Hypervisor的虛擬內存進行了一遍映射(映射方式即爲一般的映射方式);centos

                   Note:Xen只有在CPU支持虛擬化的狀況下才能夠進行全虛擬化,不然只能進行半虛擬化;Xen還能夠將兩者結合,使用HVM(硬件輔助的虛擬化)進行CPU和內存的虛擬化,對於IO設備使用PV(半虛擬化)進行虛擬化;api

         Xen的IO模擬:網絡

                   當DomU中的虛擬機須要使用IO設備時,通常都會經過Dom0爲其模擬一個專用的相應的IO設備,供其使用;app

                            好比磁盤設備的使用就是首先在Dom0中首先掛載相應的硬件磁盤設備,而後生成其對應的磁盤映像文件,再經過對磁盤映像文件進行模擬從而生成磁盤硬件設備,再將其提供給DomU中的虛擬機使用。這也意味着DomU中的虛擬機要安裝相應的硬件驅動纔可使用這個模擬的磁盤設備;大致過程就是Domu中的虛擬機使用IO要具備相對於Dom0提供的模擬的IO設備的驅動,而Dom0還要再進行一次轉換,即真正的相對於硬件IO的驅動,顯而易見這種方式的性能不怎麼樣!

                   IO的半虛擬化:

                            Xen在DomU中的虛擬機上模擬一個前端驅動,而後在Dom0中在爲其模擬一個對應的後端驅動;當虛擬機使用IO驅動時,就能夠經過前端驅動(front)直接將請求交給後端驅動(backend),中間不作什麼複雜的轉換(好比上面那種方式中須要進行的模擬磁盤尋道之類的操做)。後端也不作什麼多餘的處理,直接將請求交給物理IO硬件;顯然,這種方式的性能比上一種高不少!

                   Note:當衆多的磁盤映像文件一塊兒對底層的IO硬件設備進行請求時,其會使用一個稱爲環狀緩衝的機制來解決這個問題。大致上就是將發送來的請求都放置到環上,當環放滿時就意味着緩衝器滿了,就只能阻塞新的IO請求,使其等待緩衝區釋放空間,在將其調度進來;

         Xen的PV技術:

 

                   不依賴於CPU的HVM特性,但要求Guest OS(DomU中的虛擬機)的內核要進行修改進而知曉本身運行在PV環境中,其對CPU和內的使用要經過對Hypervisor 進行Hyper Call調用來進行;

                  能夠運行在DomU中的OS:Linux(2.6.24+),NetBSD,FreeBSD,OpenSolaris等;

                            Note:windows不能夠,由於Windows的內核是閉源的,除非微軟本身進行修改,使其能夠運行在Xen的PV模式中;

         Xen的HVM技術:

 

                   依賴於Intel VT或AMD的AMD-V的硬件虛擬化功能,而且通常還要依賴於QEMU來模擬IO設備;

                   能夠運行在DomU中的OS:幾乎全部市面上的操做系統;

         Xen的PV on HVM技術:

                   即上面提到的CPU爲HVM模式,IO設備爲PV模式;

                   能夠運行在DomU中的OS:只要OS能夠驅動PV接口類型的IO設備便可;

         XenStore:

                   爲各Domain提供的共享信息的存儲空間;且有着層級結構的名稱空間;位於Dom0中;

         Xen的工具棧:

                   xm/xend:在Xen Hypervisor的Dom0中要啓動xend服務;

                            xm:命令行管理工具,有不少子命令;

                   xl:Xen的輕量化工具,相似於xm而且會取代xm;

                   xe/xapi:提供了對Xen進行管理的API;

 

                   virsh/libvirt:在虛擬機上提供libvirt庫,並啓動了libvirtd服務便可經過遠程對虛擬機進行管理;既能夠管理Xen又能夠管理KVM和Qemu;可是相對於上面對提到的工具比較重量級一點;

 

         Xen的使用;

                   環境:

                            在Vmware中建立一個虛擬機用於安裝Xen;(Centos7)

                   1.在Vmware中的虛擬機的設置中將硬件虛擬化勾選上,這樣就可讓Vmware將物理的硬件虛擬化功能映射到其管理的虛擬機上,使之支持硬件虛擬化;

 

                   安裝Xen

                            1. 安裝搭建Xen的虛擬化環境

          

yum -y install centos-release-xen

sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/CentOS-Xen.repo

yum --enablerepo=centos-virt-xen-48 -y update kernel

yum --enablerepo=centos-virt-xen-48 -y install xen

 

                            2. 修改虛擬機的內存數

                                     

cat /etc/default/grub

    GRUB_CMDLINE_XEN_DEFAULT="dom0_mem=1024M,max:1024M cpuinfo com1=115200,8n1 console=com1,tty loglvl=all guest_loglvl=all"

                                              
    GRUB_CMDLINE_LINUX_XEN_REPLACE_DEFAULT="console=hvc0 earlyprintk=xen nomodeset"

    Note:文件中會多出上面的兩行;其中dom0_mem用於指定Dom0的內存大小;

 

                                              更多信息請查看:http://xenbits.xen.org/docs/unstable/misc/xen-command-line.html#dom0_vcpus_pin

                            3. 將支持 Xen 的 Linux 內核添加到 grub.conf中,以便系統能默認啓動支持 Xen 的 Linux 內核;這個操做能夠經過grub-bootxen.sh 自動完成; 

                            4.重啓系統,Xen環境配置完成;

                            5.重啓系統以後能夠經過xl list查看是否有Dom0;

                   xl命令行工具:man help | man xl.conf

                            xl list:列出關於域的信息;

 

                                     ID:domain的ID標識,逐個遞增;

                                     Mem:內存大小;

                                     VCPUs:指定Domain的虛擬CPU個數;

                                     State:Domain的狀態;

                     r:運行

                     b:阻塞

                     p:暫停於內存中,若是忽然宕機則數據會丟失

                     s:中止

                     c:崩潰

                     d:dying,處於正在關閉的過程當中

 

                                     Time:運行時間;

                            經常使用指令:

                                     create:建立虛擬機

                                     console:鏈接虛擬機

                                     destory:銷燬虛擬機

                                     shutdown:關機

                                     reboot:重啓

                                     pause:暫停,將數據暫停在內存中

                                     unpause:解鎖暫停狀態

                                     save:將DomU的內存中的數據轉存到指定的磁盤文件中,相似於Vmware中的掛起;

                                               ~]# xl save  busybox-001 /tmp/busybox-001.img /etc/xen/busybox

                                     restore:從指定的磁盤文件中恢復DomU數據到內存中;

                                               ~]# xl restore /etc/xen/busybox /tmp/busybox-001.img

                                     vcpu-list:顯示domain中使用的虛擬CPU;

                                     vcpu-pin:將虛擬CPU固定只能在某些物理CPU上運行;

                                               ~]# xl vcpu-pin busybox-001 0 3

                                     info:查看當前Hypervisor的信息;

                                     dmesg:顯示虛擬機啓動時所輸出的信息;

                                     top:顯示虛擬機對資源的使用狀況;

                                     pci-list:顯示pass-through PCI設備;

                                     pci-attach:插入一個pass-through PCI設備;

                                     pci-detach:拆除一個pass-through PCI設備;

                                     network-list:查看虛擬機上的網卡設備;

                                     network-attach:添加一塊網卡設備;

                                     network-detach:移除一塊網卡設備;

                                     block-list:查看虛擬機上的磁盤設備;

                                     block-attach:添加一塊磁盤設備;

                                     block-detach:移除一塊磁盤設備;

                   建立Xen的PV模式的虛擬機(DomU使用Dom0中的kernel):

                            步驟:

                                     1.指定虛擬機要使用的kernel文件;

                                               能夠位於Dom0中,也能夠位於DomU中;

                                     2.指定虛擬機要使用的initramfs或initrd文件;

                                     3.安裝DomU的內核模塊;位於DomU中;

                                     4.爲DomU提供根文件系統;

                                     5.設置Swap設備;

                                     6.將上述的內容定義在DomU的配置文件中;

                            Note:使用xl工具配置虛擬機能夠經過man xl.cfg來查看幫助信息;

                                     xl.cfg中經常使用的指令:

                                               name=:指定惟一的domain名稱;

                                               type=:指定建立的虛擬機類型;

                                                        pv:半虛擬化;

                                                        pvh:PV on HVM;

                                                        hvm:硬件輔助的虛擬化;

                                               vcpus=:指定VCPU的個數;

                                               maxvcpus=:指定可使用的最大VCPU個數;

                                               cpus=:指定可使用的物理CPU個數;

                                                        能夠指定僅使用固定CPU;好比cpus="0-3,5,^1"表示僅使用0,2,3,5號四個物理CPU;

                                               memory=:指定可使用的內存大小;

                                               maxmem=:指定可使用的最大內存大小;

                                               on_poweroff=:指定在Guest虛擬機中執行關機時,在Dom0中要執行的動做;

                                                        destroy:銷燬(關機),默認

                                                        restart:重啓

                                                        rename-restart:改名後重啓

                                                        preserve:保存這個域

                                                        coredump-destroy:將虛擬機中的內核空間中的數據到出到指定文件中進行保存,而後銷燬這個domain;默認導出到/var/lib/xen/dump/NAME中;

                                               uuid=:DomU的惟一標識;

                                               disk=:經過列表指定磁盤設備;多個磁盤之間使用逗號隔開;這裏的磁盤能夠是磁盤映像文件,也能夠是Dom0中的某個物理分區;其中使用物理設備性能比較好,可是不便於虛擬機的遷移;

                                                        格式:disk=[<target>, [<format>, [<vdev>, [<access>]]]]

                                                                 target:表示磁盤映像文件或設備文件路徑;好比:/dev/vg/guest-volume

                                                                 format:表示磁盤格式;好比raw(默認), qcow, qcow2, vhd, qed;

                                                                           能夠經過qemu-img工具建立以上格式的磁盤文件;

                                                                                    qemu-img create [-q] [-f fmt] [-o options] filename [size]

                                                                                    例子:~]# qemu-img-xen create -f raw -o size=2G /images/xen/busybox.img

                                                                                    Note:qemu-img  create -f qcow2 -o ?:能夠查看某種格式所支持的選項;

                                                                 vdev:此設備在DomU中被識別爲的硬件設備格式;好比hd[a-z]、sd[a-z]、xvd[a-z]等;

                                                                 access:表示此設備的訪問權限;

                                                                           ro,r:表示只讀;

                                                                           rw,w:表示讀寫(默認);

                                                        例子:disk=["/images/xen/linux.img,raw,xvda,rw","/images/xen/file.img,qcow2,xvdb,rw"]

                                                        官方文檔:http://xenbits.xen.org/docs/unstable/man/xl-disk-configuration.5.html

                                               vif=:經過列表指定虛擬網卡設備;

                                                        格式:vif = [ '<vifspec>', '<vifspec>', ... ]

                                                                 mac=:指定MAC地址,通常以00:16:3E開頭;

                                                                 bridge=:指定此網絡接口在Dom0中被關聯至哪一個橋設備上;

                                                                 mode=:指定虛擬成的網卡類型:

                                                                           rtl8139 (default)

                                                                           e1000

                                                                 vifname=:指定在Dom0中接口的名稱;

                                                                 ip=:指定固定IP地址;

                                                                 rate=:指定設備的傳輸速率;

                                                        例子:vif = [ 'mac=00:16:3E:74:34:32', 'mac=00:16:3e:5f:48:e4,bridge=xenbr1' ]

                                                        官方文檔:http://xenbits.xen.org/docs/unstable/man/xl-network-configuration.5.html

                                               vfb=:經過列表指定虛擬的顯示接口;

                                                        能夠經過sdl(vfb = [ 'sdl=1' ])或者vnc(vfb = [ 'vnc=1' ])模擬實現;

                                                        若是使用vnc啓動圖形界面的話,須要在本地安裝tigervnc這個軟件包;這個VNC時工做在Dom0中的;

                                               PV模式的專有選項:

                                                        kernel=:指定虛擬機的內核文件路徑;位於Dom0中;

                                                        ramdisk=:指明爲kernel中指定的內核提供的ramdisk文件路徑;位於Dom0中;

                                                        bootloader=:若是DomU使用本身的kernel及ramdisk,而非上面再Dom0中所提供的,則須要使用此處的bootloader選項來加載內核;這個bootloader功能是經過Dom0中的應用程序模擬出來的;與kernel和ramdisk選項相排斥,選擇一種方法便可;

                                                        root=:指定根文件系統的位置;通常是與kernel和ramdisk選項一塊兒使用;

                                                        extra=:指定內核引導時使用的額外參數;通常是與kernel和ramdisk選項一塊兒使用;好比init=/bin/sh

                            具體配置過程:

                                     1.建立磁盤映像文件:

                       mkdir -pv /images/xen

                       qemu-img-xen create -f raw -o size=2G /images/xen/busybox.img

                       mkfs.ext4 /images/xen/busybox.img

                       mount -o loop /images/xen/busybox.img /mnt

 

                                     2.使用busybox構建根目錄:

                       yum group install "開發工具"

                       yum install ncurses-devel.x86_64

                       tar xf busybox-1.22.1.tar.bz2

                       cd busybox-1.22.1/

                       make menuconfig

                             Busybox Settings  --->

                                 Build Options  --->

                                       [*] Build BusyBox as a static binary (no shared libs)

                       make

                       make install

                       cp -a _install/* /mnt/

                       cd /mnt

                       mkdir proc sys dev etc var home boot media

                       chroot /mnt/ /bin/sh

                       exit

 

                                     3.爲DomU中的虛擬機提供kernel和initramfs文件:

                         cd /boot

                         ln -sv vmlinuz-2.6.32-754.el6.x86_64 vmlinuz

                         ln -sv initramfs-2.6.32-754.el6.x86_64.img initramfs.img

                           Note:使用3.10內核時虛擬機建立之後會自動銷燬,使用4.9內核時啓動之後根文件系統不是事先指定的,而是其本身的(這點多是由於我使用的是相對於Xen的4.9的內核所引發的);

                         cd /etc/xen/

                         cp xlexample.pvlinux busybox

                         cat busybox

                             name = "busybox-001"

                             kernel = "/boot/vmlinuz"

                             ramdisk = "/boot/initramfs.img"

                             extra = "selinux=0 init=/bin/sh"

                             memory = 256

                             vcpus = 2

                             disk = [ '/images/xen/busybox.img,raw,xvda,rw' ]

                             root =  "/dev/xvda ro"

 

                                               cd /boot

                                               ln -sv vmlinuz-2.6.32-754.el6.x86_64 vmlinuz

                                               ln -sv initramfs-2.6.32-754.el6.x86_64.img initramfs.img

                                               Note:使用3.10內核時虛擬機建立之後會自動銷燬,使用4.9內核時啓動之後根文件系統不是事先指定的,而是其本身的(這點多是由於我使用的是相對於Xen的4.9的內核所引發的);

                                              cd /etc/xen/

                                               cp xlexample.pvlinux busybox

                                               cat busybox

                                                        name = "busybox-001"

                                                        kernel = "/boot/vmlinuz"

                                                        ramdisk = "/boot/initramfs.img"

                                                        extra = "selinux=0 init=/bin/sh"

                                                        memory = 256

                                                        vcpus = 2

                                                        disk = [ '/images/xen/busybox.img,raw,xvda,rw' ]

                                                        root =  "/dev/xvda ro"

                                     4.建立虛擬機:

                                               xl -v create busybox

                                              xl console busybox-001   進入虛擬機,使用Ctrl + ] 退出;

                                     5.配置網絡接口:

                                               修改配置文件的方式:

                                                        cd /etc/sysconfig/network-scripts/

                                                        cp ifcfg-ens33 ifcfg-xenbr0

                                                        vim ifcfg-ens33

                                                                 將於IP地址有關的所有刪除,好比網關、掩碼等,添加一項:BRIDGE=xenbr0;

                                                                 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33

                                                                           TYPE=Ethernet

                                                                           PROXY_METHOD=none

                                                                           BROWSER_ONLY=no

                                                                           BOOTPROTO=static

                                                                           DEFROUTE=yes

                                                                           IPV4_FAILURE_FATAL=no

                                                                           IPV6INIT=yes

                                                                           IPV6_AUTOCONF=yes

                                                                           IPV6_DEFROUTE=yes

                                                                           IPV6_FAILURE_FATAL=no

                                                                           IPV6_ADDR_GEN_MODE=stable-privacy

                                                                           NAME=ens33

                                                                           UUID=9549a5c4-9cd6-4f9f-9d1b-08bdae8783f7

                                                                           DEVICE=ens33

                                                                           ONBOOT=yes

                                                                           BRIDGE=xenbr0

                                                                 Note:對於Centos6不要使用NetworkManager管理網絡;

                                                        vim ifcfg-xenbr0

                                                                  修改設備名爲xenbr0、刪除物理地址以及UUID、將TYPE改成Brider;

                                                                 ~]# cat /etc/sysconfig/network-scripts/ifcfg-xenbr0

                                                                           TYPE=Bridge

                                                                           PROXY_METHOD=none

                                                                           BROWSER_ONLY=no

                                                                           BOOTPROTO=dhcp

                                                                           DEFROUTE=yes

                                                                          IPV4_FAILURE_FATAL=no

                                                                           IPV6INIT=yes

                                                                           IPV6_AUTOCONF=yes

                                                                           IPV6_DEFROUTE=yes

                                                                           IPV6_FAILURE_FATAL=no

                                                                           IPV6_ADDR_GEN_MODE=stable-privacy

                                                                           NAME=xenbr0

                                                                           DEVICE=xenbr0

                                                                           ONBOOT=yes

                                                        systemctl restart network.service

                                               使用命令行工具的方式:

                                                        ifconfig ens33 0 up

                                                        brctl addbr xenbr0

                                                        brctl addif xenbr0 ens33

                                                        ifconfig xenbr0 192.168.80.132/24 up

                                               Note:使用3.7或3.10內核設置橋設備時會出現死機的狀況;

                                     6.添加虛擬網卡設備

                                               vim /etc/xen/busybox

                                                        vif = [ 'bridge=xenbr0' ]   將網卡橋接到xenbr0上;

                                               mount -o loop /images/xen/busybox.img /mnt

                                               mkdir -pv /mnt/lib/modules

                                               cp /lib/modules/2.6.32-754.el6.x86_64/kernel/drivers/net/xen-netfront.ko /mnt/lib/modules

                                               xl console busybox-001

                                               insmod /lib/modules/xen-netfront.ko

                                               ifconfig -a    eth0已經出現,此網卡設備即爲front-net;

                                               Ctrl + ]

                                               ifconfig -a    出現一個名爲vif2.0的網卡,此網卡設備即爲backed-net;其中2.0的2表示Domain ID爲2的虛擬機的網卡,0表示第一塊網卡設備;

                                               brctl show

                                                        發現vif2.0被橋接到了xenbr0上;

                                               xl console busybox-001  再次進入虛擬機

                                               ifconfig eth0 192.168.80.134 up

                                               ping -c 4 192.168.80.134

                                               Note:當咱們只想要某些虛擬機主機之間通訊,而不讓它們與外網通訊時,能夠建立一個橋設備,而後將它們全都橋接到這個橋設備上便可(這個不要將物理鏈接外網的網卡橋接到這個橋設備上),這就是Vmware中的VMnet2模式;僅主機模式就是在這個的基礎上爲物理主機模擬出一塊網卡,再將這塊虛擬網卡橋接到哪一個橋設備上而且關閉核心間轉發功能便可;

                   使用DomU本身的kernel建立虛擬機:

                            1.建立磁盤鏡像文件:

                                     ~]# qemu-img create -f raw -o size=3G /images/xen/busybox3.img

                            2.對磁盤鏡像文件進行分區:

                                     使用losetup命令進行分區

                                               losetup -f:顯示第一個可用的loop設備;

                                               losetup -a:顯示全部已用的loop設備;

                                               格式:losetup loop_device file

                                     ~]# losetup /dev/loop0  /images/xen/busybox3.img  將映像文件關聯至loop設備上,以便於進行分區;

                                     ~]# fdisk  /dev/loop0

                                               添加兩個主分區,一個爲100M,一個爲1G;

                                     ~]# kpartx -av /dev/loop0  從分區表建立設備映射關係

                                     ~]# mkfs.ext4 /dev/mapper/loop0p1

                                     ~]# mkfs.ext4 /dev/mapper/loop0p2

                                     ~]# mkdir /mnt/{boot,sysroot}

                                     ~]# cd /mnt/

                                     mnt]# mount /dev/mapper/loop0p1 boot/

                                     mnt]# mount /dev/mapper/loop0p2 sysroot/

                                     ~]# cp /boot/vmlinuz-2.6.32-754.el6.x86_64 boot/vmlinuz

                                     ~]# cp /boot/initramfs-2.6.32-754.el6.x86_64.img boot/iniramfs.img

                                     ~]# grub-install --roor-directory=/mnt /dev/loop0

                                     ~]# cat /mnt/boot/grub/grub.conf       -->Centos6

                                               default=0

                                               timeout=5

                                               title BusyBox(kernel-2.6)

                                                        root(hd0,0)

                                                        kernel /vmlinuz root=/dev/xvda1 ro selinux=0 init=/bin/sh

                                                        initrd /initramfs.img

 

                                     ~]# cp -a /root/busybox-1.22.1/_install/*  sysroot/

                                     ~]# kpartx -d /dev/loop0

                                     ~]# losetup -a

                                     ~]# cp /etc/xen/busybox1 /etc/xen/busybox3

                                     ~]# cat /etc/xen/busybox3

                                               name = "busybox-003"

                                               memory = 256

                                               vcpus = 2

                                               vif = [ 'bridge=xenbr0' ]

                                               disk = [ 'file:/images/xen/busybox3.img,xvda,rw' ]

                                               bootloader = 'pygrub'

                                     ~]# xl -v create /etc/xen/busybox3 -c

 

                                     Note:當虛擬機建立成功之後,可使用cloud-init將虛擬機的磁盤映像文件中的惟一標識信息都去掉,而後將其做爲模板,之後就能夠直接建立虛擬機了,每次使其建立新的虛擬機時,都會自動生成相應的新的標識信息,好比MAC地址、UUID等;

                                     Xen虛擬機除了使用xl命令管理之外,還可使用virt-manager這個圖形化工具管理;

 

 

 

     注:根據馬哥視頻作的學習筆記,若有錯誤,歡迎指正;侵刪

相關文章
相關標籤/搜索