http://luochen2015.blog.51cto.com/9772274/1741540


    自从 RedHat 在 RHEL 6 放弃 Xen 后,下载 Xen 源码编译安装似乎是在 RHEL/CentOS 6.x 上安装 Xen 的唯一办法。不过,开源世界永远不缺少热情的志愿者,Xen Made Easy 这个项目为我们提供了可用的 Xen 第三方软件源,免去了自己编译源码的痛苦。现在,我们有了更好的官方选择,就是 Xen4CentOS6. Xen4CentOS6 是来自 CentOS, Xen, Citrix, Godaddy, Rackspace 社区和相关团队合作开发的一个开源项目,旨在为 CentOS 6.x 维护一个稳定的 Xen 工具链(Xen hypervisor 和相关 Xen 工具),让 Xen 运行在 CentOS 6 上变得更容易,更可靠。


    1、安装Xen


   升级整个系统后重启,然后加入 CentOS 官方 Xen 源(Xen4CentOS6)并安装 Xen 内核及相关工具。需要注意的是,采用 CentOS-6.5-x86_64-minimal.iso 最小化安装的系统没有包括 Perl,Xen 工具需要 Perl 的支持,所以需要安装 perl 先:


   # yum install centos-release-xen

   # yum update

    

   # yum install perl

   # yum install xen

   

   yum install -y xen kernel-xen xen-libs python-virtinst bridge-utils libvirt virt-manager


   安装完支持 Xen 的 Linux 内核后需要加新条目到 grub.conf,以便系统能默认启动带 Xen 的 Linux 内核(而不是常规 Linux 内核),这个操作可以通过 grub-bootxen.sh 自动完成,弄完后打开 grub.conf 文件再次确认一下:


   # /usr/bin/grub-bootxen.sh


   kernel /xen.gz dom0_mem=1024M,max:1024M cpuinfo com1=115200,8n1 console=com1,tty loglvl=all guest_loglvl=all

   

   # /usr/bin/grub-bootxen.sh


   # vi /etc/grub.conf

   ...

   title CentOS (3.10.25-11.el6.centos.alt.x86_64)

       root (hd0,0)

       kernel /xen.gz dom0_mem=1024M,max:1024M loglvl=all guest_loglvl=all

       module /vmlinuz-3.10.25-11.el6.centos.alt.x86_64 ro root=/dev/mapper/vg_node11-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_node11/lv_swap  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rd_LVM_LV=vg_node11/lv_root rhgb quiet

   



default=0

timeout=5

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

hiddenmenu

title CentOS (4.9.13-22.el6.x86_64)

       root (hd0,0)

       kernel /xen.gz dom0_mem=1024M,max:1024M cpuinfo com1=115200,8n1 console=com1,tty loglvl=all guest_loglvl=all

       module /vmlinuz-4.9.13-22.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet

       module /initramfs-4.9.13-22.el6.x86_64.img

title CentOS 6 (2.6.32-573.el6.x86_64)

       root (hd0,0)

       kernel /vmlinuz-2.6.32-573.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_NO_MD rd_LVM_LV=VolGroup/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=VolGroup/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet

       initrd /initramfs-2.6.32-573.el6.x86_64.img



   # reboot

        

        [root@localhost ~]# uname -r

4.9.13-22.el6.x86_64


   [root@localhost ~]# xl info

host                   : localhost

release                : 4.9.13-22.el6.x86_64

version                : #1 SMP Sun Feb 26 22:18:35 UTC 2017

machine                : x86_64

nr_cpus                : 4

max_cpu_id             : 3

nr_nodes               : 1

cores_per_socket       : 4

threads_per_core       : 1

cpu_mhz                : 3192

hw_caps                : bfebfbff:28100800:00000000:00007f00:77bae3ff:00000000:00000001:00000281

virt_caps              : hvm_directio

total_memory           : 8142

free_memory            : 7005

sharing_freed_memory   : 0

sharing_used_memory    : 0

outstanding_claims     : 0

free_cpus              : 0

xen_major              : 4

xen_minor              : 6

xen_extra              : .3-8.el6

xen_version            : 4.6.3-8.el6

xen_caps               : xen-3.0-x86_64 xen-3.0-x86_32p 

xen_scheduler          : credit

xen_pagesize           : 4096

platform_params        : virt_start=0xffff800000000000

xen_changeset          : Wed Feb 15 02:12:22 2017 -0600 git:35503e1-dirty

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

cc_compiler            : gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-17)

cc_compile_by          : mockbuild

cc_compile_domain      : centos.org

cc_compile_date        : Tue Feb 28 14:18:26 UTC 2017

xend_config_format     : 4

        

        [root@localhost ~]# xl list

Name                                        ID   Mem VCPUs State Time(s)

Domain-0                                     0  1024     4     r-----     310.2

        


2、设置桥接


  安装桥工具

  # yum -y install bridge-utils


       [root@localhost ~]# cd /etc/sysconfig/network-scripts/

       [root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-br0

  [root@localhost network-scripts]# vim ifcfg-eth0 

   DEVICE=eth0

HWADDR=AC:22:0B:DA:5C:5A

TYPE=Ethernet

UUID=418bd5da-a9a1-4071-9728-4ba9d404402e

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=none

BRIDGE=br0


  [root@localhost network-scripts]# vim ifcfg-br0

  DEVICE=br0

#HWADDR=AC:22:0B:DA:5C:5A

#TYPE=Ethernet

TYPE=Bridge

#UUID=418bd5da-a9a1-4071-9728-4ba9d404402e

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

IPADDR=192.168.1.53

NETMASK=255.255.255.0

GATEWAY=192.168.1.1

DNS1=202.106.0.20


  [root@localhost network-scripts]# service network restart


  [root@localhost ~]# ifconfig 

br0       Link encap:Ethernet  HWaddr AC:22:0B:DA:5C:5A  

          inet addr:192.168.1.53  Bcast:192.168.1.255  Mask:255.255.255.0

          inet6 addr: fe80::6008:b1ff:fe77:73b4/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:225847 errors:0 dropped:493 overruns:0 frame:0

          TX packets:763 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:18229754 (17.3 MiB)  TX bytes:80159 (78.2 KiB)


eth0      Link encap:Ethernet  HWaddr AC:22:0B:DA:5C:5A  

          inet6 addr: fe80::ae22:bff:feda:5c5a/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:697779 errors:0 dropped:563 overruns:0 frame:0

          TX packets:4752 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:70139777 (66.8 MiB)  TX bytes:528522 (516.1 KiB)


lo        Link encap:Local Loopback  

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

          UP LOOPBACK RUNNING  MTU:65536  Metric:1

          RX packets:0 errors:0 dropped:0 overruns:0 frame:0

          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1 

          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)


virbr0    Link encap:Ethernet  HWaddr 52:54:00:50:B5:D5  

          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0

          UP BROADCAST MULTICAST  MTU:1500  Metric:1

          RX packets:0 errors:0 dropped:0 overruns:0 frame:0

          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

       

       [root@localhost ~]# brctl show

bridge name bridge id STP enabled interfaces

br0 8000.ac220bda5c5a no eth0

virbr0 8000.52540050b5d5 yes virbr0-nic

       [root@localhost ~]# brctl stp br0 on

[root@localhost ~]# brctl show

bridge name bridge id STP enabled interfaces

br0 8000.ac220bda5c5a yes eth0

virbr0 8000.52540050b5d5 yes virbr0-nic


      3、创建虚拟机

          

          http://luochen2015.blog.51cto.com/9772274/1741540

          

          1)、创建虚拟磁盘映像文件


              创建空的虚拟磁盘映像(稀疏磁盘镜像),以之作为新建非特权域的虚拟磁盘文件,此映像文件并不真正占用为其指定的空间,而是随着存储的内容而变化。


             [root@localhost ~]# mkdir /xen

             [root@localhost ~]# ll -d /xen/

drwxr-xr-x 2 root root 4096 Mar 20 16:54 /xen/

[root@localhost xen]# dd if=/dev/zero of=centos6.img oflag=direct seek=102399 bs=1M count=1

[root@localhost xen]# du -sh centos6.img 

1.0M centos6.img

             [root@localhost xen]# ll -h 

total 1.0M

-rw-r--r-- 1 root root 100G Mar 20 16:56 centos6.img


     2)、用Xftp插件将CentOS-6.5-x86_64-minimal.iso 文件上传到xen 的/source 目录下             


       [root@localhost xen]# mkdir /source

       [root@localhost xen]# rz

       [root@localhost source]# ls

CentOS-6.6-x86_64-minimal.iso


          3)、安装Apache并且挂载(CentOS-6.5-x86_64-minimal.iso)ISO镜像文件

             [root@localhost xen]# yum install httpd -y

             [root@localhost xen]# service httpd start

             

             [root@localhost xen]# cd /var/www/html/

[root@localhost html]# ls

[root@localhost html]# mkdir iso

[root@localhost html]# mount

mount        mount.nfs    mount.nfs4   mountpoint   mountstats   mount.tmpfs  

[root@localhost html]# mount -o loop /source/CentOS-6.6-x86_64-minimal.iso /var/www/html/iso/

mount: /source/CentOS-6.6-x86_64-minimal.iso is write-protected, mounting read-only

[root@localhost html]# ls iso/

CentOS_BuildTag  GPL       Packages                  RPM-GPG-KEY-CentOS-6           RPM-GPG-KEY-CentOS-Testing-6

EFI              p_w_picpaths    RELEASE-NOTES-en-US.html  RPM-GPG-KEY-CentOS-Debug-6     TRANS.TBL

EULA             isolinux  repodata                  RPM-GPG-KEY-CentOS-Security-6


          4)、为虚拟机提供内核和内核模块文件


            [root@localhost html]# cp /var/www/html/iso/isolinux/{vmlinuz,initrd.img} /source

[root@localhost html]# ls /source/

CentOS-6.6-x86_64-minimal.iso  initrd.img  vmlinuz

          

          5)、修改xlexample.pvlinux文件

              

              为新的非特权域创建配置文件(推荐复制这个文件到指定的位置):


            [root@localhost html]# vim /etc/xen/xlexample.pvlinux 

            [root@localhost html]# cat /etc/xen/xlexample.pvlinux 

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

# Example PV Linux guest configuration

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

#

# This is a fairly minimal example of what is required for a

# Paravirtualised Linux guest. For a more complete guide see xl.cfg(5)


# Guest name

name = "centos6"


# 128-bit UUID for the domain as a hexadecimal number.

# Use "uuidgen" to generate one if required.

# The default behavior is to generate a new UUID each time the guest is started.

#uuid = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"


# Kernel p_w_picpath to boot

kernel = "/source/vmlinuz"


# Ramdisk (optional)

#ramdisk = "/boot/initrd.gz"

ramdisk = "/source/initrd.img"


# Kernel command line options

#extra = "root=/dev/xvda1"


# Initial memory allocation (MB)

memory = 512


# Maximum memory (MB)

# If this is greater than `memory' then the slack will start ballooned

# (this assumes guest kernel support for ballooning)

#maxmem = 512


# Number of VCPUS

vcpus = 2


# Network devices

# A list of 'vifspec' entries as described in

# docs/misc/xl-network-configuration.markdown

vif = [ 'bridge=br0' ]


# Disk Devices

# A list of `diskspec' entries as described in

# docs/misc/xl-disk-configuration.txt

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


  6)、创建虚拟机


    [root@localhost ~]# xl create -c /etc/xen/xlexample.pvlinux


Welcome to CentOS for x86_64


                    

       ┌────────────────┤ Manual TCP/IP Configuration ├─────────────────┐

       │                                                                │

       │ Enter the IPv4 and/or the IPv6 address and prefix (address /   │

       │ prefix).  For IPv4, the dotted-quad netmask or the CIDR-style  │

       │ prefix are acceptable. The gateway and name server fields must │

       │ be valid IPv4 or IPv6 addresses.                               │

       │                                                                │

       │ IPv4 address: 192.168.1.11____ / 24______________              │

       │ Gateway:      192.168.1.1______________________________        │

       │ Name Server:  _________________________________________        │

       │                                                                │

       │             ┌────┐                        ┌──────┐             │

       │             │ OK │                        │ Back │             │

       │             └────┘                        └──────┘             │

       │                                                                │

       │                                                                │

       └────────────────────────────────────────────────────────────────┘

                    

                    


  <Tab>/<Alt-Tab> between elements  | <Space> selects | <F12> next screen


Welcome to CentOS for x86_64


        ┌────────────────────────┤ URL Setup ├─────────────────────────┐

        │                                                              │

        │          Please enter the URL containing the CentOS          │

        │          installation p_w_picpath on your server.                  │

        │                                                              │

        │ http://192.168.1.53/iso_____________________________________ │

        │                                                              │

        │ [ ] Enable HTTP proxy                                        │

        │                                                              │

        │ Proxy URL        ___________________________________         │

        │ Username         _______________                             │

        │                                                              │

        │ Password         _______________                             │

        │                                                              │

        │            ┌────┐                       ┌──────┐             │

        │            │ OK │                       │ Back │             │

        │            └────┘                       └──────┘             │

        │                                                              │

        │                                                              │

        └──────────────────────────────────────────────────────────────┘


  <Tab>/<Alt-Tab> between elements  | <Space> selects | <F12> next screen


  [root@localhost ~]# iptables -F                                    #注意防火墙

       

Welcome to CentOS for x86_64

 ┌────────────────────────────────┤ Warning ├─────────────────────────────────┐

 │                                                                            │

 │         102400MB                                                ↑          │

 │         Xen Virtual Block Device                                          │

 │                                                                          │

 │         This device may need to be reinitialized.                        │

 │                                                                          │

 │         REINITIALIZING WILL CAUSE ALL DATA TO BE LOST!                    │

 │                                                                          │

 │         This action may also be applied to all other disks                │

 │         needing reinitialization.                                        │

 │                                                                          │

 │         Device details:                                                  │

 │         xen-vbd-51712                                           ↓          │

 │                                                                            │

 │  ┌────────┐   ┌────────────┐   ┌───────────────┐   ┌───────────────────┐   │

 │  │ Ignore │   │ Ignore all │   │ Re-initialize │   │ Re-initialize all │   │

 │  └────────┘   └────────────┘   └───────────────┘   └───────────────────┘   │

 │                                                                            │

 │                                                                            │

 └────────────────────────────────────────────────────────────────────────────┘


  <Tab>/<Alt-Tab> between elements   |  <Space> selects   |  <F12> next screen


Welcome to CentOS for x86_64

 

 

                    ┌───────┤ Time Zone Selection ├───────┐

                    │                                     │

                    │ In which time zone are you located? │

                    │                                     │

                    │ [*] System clock uses UTC           │

                    │                                     │

                    │  Asia/Samarkand                  ↑  │

                    │  Asia/Seoul                        │

                    │  Asia/Shanghai                    │

                    │  Asia/Singapore                    │

                    │  Asia/Srednekolymsk              ↓  │

                    │                                     │

                    │      ┌────┐          ┌──────┐       │

                    │      │ OK │          │ Back │       │

                    │      └────┘          └──────┘       │

                    │                                     │

                    │                                     │

                    └─────────────────────────────────────┘

 


  <Tab>/<Alt-Tab> between elements   |  <Space> selects   |  <F12> next screen


Welcome to CentOS for x86_64

 

 

                    

                ┌──────────────┤ Root Password ├───────────────┐

                │                                              │

                │    Pick a root password. You must type it    │

    ┌──────────────────────────┤ Weak Password ├──────────────────────────┐

    │                                                                     │

    │ You have provided a weak password: it is based on a dictionary word │

    │                                                                     │

    │           ┌────────┐                      ┌────────────┐            │

    │           │ Cancel │                      │ Use Anyway │            │

    │           └────────┘                      └────────────┘            │

    │                                                                     │

    │                                                                     │

    └─────────────────────────────────────────────────────────────────────┘

                

                └──────────────────────────────────────────────┘

                    

                    

 


  <Tab>/<Alt-Tab> between elements   |  <Space> selects   |  <F12> next screen


Welcome to CentOS for x86_64

 

       ┌─────────────────────┤ Partitioning Type ├─────────────────────┐

       │                                                               │

       │ Installation requires partitioning of your hard drive.  The   │

       │ default layout is suitable for most users.  Select what space │

       │ to use and which drives to use as the install target.         │

       │                                                               │

       │                 Use entire drive                              │

       │                 Replace existing Linux system                 │

       │                 Use free space                                │

       │                                                               │

       │   Which drive(s) do you want to use for this installation?    │

       │      [*]   xvda   102400 MB (Xen Virtual Block Devic) ↑       │

       │                                                             │

       │                                                               │

       │                      ┌────┐   ┌──────┐                        │

       │                      │ OK │   │ Back │                        │

       │                      └────┘   └──────┘                        │

       │                                                               │

       │                                                               │

       └───────────────────────────────────────────────────────────────┘


<Space>,<+>,<-> selection   |   <F2> Add drive   |   <F12> next screen


Welcome to CentOS for x86_64

 

       

       

       

       

     ┌─────────────┤ Writing storage configuration to disk ├──────────────┐

     │                                                                    │

     │ The partitioning options you have selected will now be written to  │

     │ disk.  Any data on deleted or reformatted partitions will be lost. │

     │                                                                    │

     │        ┌─────────┐               ┌───────────────────────┐         │

     │        │ Go back │               │ Write changes to disk │         │

     │        └─────────┘               └───────────────────────┘         │

     │                                                                    │

     │                                                                    │

     └────────────────────────────────────────────────────────────────────┘

       

       

       

       

       


  <Tab>/<Alt-Tab> between elements   |  <Space> selects   |  <F12> next screen


Welcome to CentOS for x86_64

 

       

       

       

       

     

     

     

          ┌──────────────────────┤ Formatting ├──────────────────────┐

          │                                                          │

          │ Creating ext4 filesystem on /dev/mapper/VolGroup-lv_root │

          │                                                          │

          │                                                          │

          └──────────────────────────────────────────────────────────┘

     

     

       

       

       

       

       


  <Tab>/<Alt-Tab> between elements   |  <Space> selects   |  <F12> next screen


Welcome to CentOS for x86_64

 

       

       

       

     ┌─────────────────────┤ Package Installation ├──────────────────────┐

     │                                                                   │

     │                                                                   │

     │                                 4%                                │

     │                                                                   │

     │                   Packages completed: 8 of 205                    │

     │                                                                   │

     │ Installing glibc-common-2.12-1.149.el6.x86_64 (107 MB)            │

     │ Common binaries and locale data for glibc                         │

     │                                                                   │

     │                                                                   │

     │                                                                   │

     └───────────────────────────────────────────────────────────────────┘

       

       

       

       


  <Tab>/<Alt-Tab> between elements   |  <Space> selects   |  <F12> next screen


  Welcome to CentOS for x86_64

 

       

       

       

     ┌───────────────────────────┤ Complete ├────────────────────────────┐

     │                                                                   │

     │ Congratulations, your CentOS installation is complete.            │

     │                                                                   │

     │ Please reboot to use the installed system.  Note that updates may │

     │ be available to ensure the proper functioning of your system and  │

     │ installation of these updates is recommended after the reboot.    │

     │                                                                   │

     │                            ┌────────┐                             │

     │                            │ Reboot │                             │

     │                            └────────┘                             │

     │                                                                   │

     │                                                                   │

     └───────────────────────────────────────────────────────────────────┘

       

       

       


                                <Enter> to exit

terminating anaconda...done

sending termination signals...done

sending kill signals...done

disabling swap...

/dev/dm-2

unmounting filesystems...

/mnt/runtime done

disabling /dev/loop0

/dev/pts done

/selinux done

/mnt/sysp_w_picpath/boot done

/mnt/sysp_w_picpath/dev/pts done

/mnt/sysp_w_picpath/dev/shm done

/mnt/sysp_w_picpath/dev done

/mnt/sysp_w_picpath/home done

/mnt/sysp_w_picpath/proc/bus/usb done

/mnt/sysp_w_picpath/proc done

/mnt/sysp_w_picpath/sys done

/mnt/sysp_w_picpath/selinux done

/mnt/sysp_w_picpath done

waiting for mdraid sets to become clean...

rebooting system

Restarting system.


在重启之前需要先修改配置文件xlexample.pvlinux


注销:#kernel = "/source/vmlinuz"

      #ramdisk = "/source/initrd.img"


添加:bootloader = "/usr/bin/pygrub"



[root@localhost ~]# vim /etc/xen/xlexample.pvlinux 


[root@localhost ~]# cat /etc/xen/xlexample.pvlinux 

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

# Example PV Linux guest configuration

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

#

# This is a fairly minimal example of what is required for a

# Paravirtualised Linux guest. For a more complete guide see xl.cfg(5)


# Guest name

name = "centos6"


# 128-bit UUID for the domain as a hexadecimal number.

# Use "uuidgen" to generate one if required.

# The default behavior is to generate a new UUID each time the guest is started.

#uuid = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"


# Kernel p_w_picpath to boot

#kernel = "/source/vmlinuz"


# Ramdisk (optional)

#ramdisk = "/boot/initrd.gz"

#ramdisk = "/source/initrd.img"


bootloader = "/usr/bin/pygrub"


# Kernel command line options

#extra = "root=/dev/xvda1"


# Initial memory allocation (MB)

memory = 512


# Maximum memory (MB)

# If this is greater than `memory' then the slack will start ballooned

# (this assumes guest kernel support for ballooning)

#maxmem = 512


# Number of VCPUS

vcpus = 2


# Network devices

# A list of 'vifspec' entries as described in

# docs/misc/xl-network-configuration.markdown

vif = [ 'bridge=br0' ]


# Disk Devices

# A list of `diskspec' entries as described in

# docs/misc/xl-disk-configuration.txt

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

on_reboot = 'restart'

on_crash = 'destroy'


[root@localhost ~]# xl list

Name                                        ID   Mem VCPUs State Time(s)

Domain-0                                     0  1024     4     r-----     926.8

centos6                                      2   512     2     -b----       5.6


[root@localhost ~]# xl reboot 2   (centos 的ID)


[root@localhost ~]# xl list

Name                                        ID   Mem VCPUs State Time(s)

Domain-0                                     0  1024     4     r-----     929.7

centos6                                      3   512     2     -b----       5.5


[root@localhost ~]# xl shutdown centos6

[root@localhost ~]# xl console centos 6



设置vif:


在启动DomU时,可以为其定义可用的虚拟网络接口个数、每个虚拟网络接口的属性等,这仅需要在其对应的配置文件中使用vif选项即可。vif选项的值是一个列表,列表中的每个条目使用单引号引用,用于定义对应虚拟网络接口属性,条目之间使用逗号分隔。比如下面的示例就为当前域定义了三个虚拟网络接口,每个接口的属性均采用了默认配置。

vif = [ ‘ ‘, ‘ ‘, ‘ ‘ ]


每个网络接口可定义的属性语法格式为‘type= TYPE, mac=MAC, bridge=BRIDGE, ip=IPADDR, script= SCRIPT," + \ "backend=DOM, vifname=NAME, rate=RATE, model=MODEL, accel=ACCEL’。

type:网络接口的类型,即其前端设备类型,默认为netfront;其可用的值还有有ioemu,表示使用QEMU的网络驱动;

mac:指定此接口的MAC地址,默认为00:16:3E:(IEEE分配给XenSource的地址段)开头的随机地址;

bridge:指定此接口使用的桥接设备,默认为Dom0内的第一个桥接设备;

ip:为当前域定义固定IP地址,如果网络中存在DHCP服务器,请确保此地址一定没有包含于DHCP的可分配地址范围中;事实上,在DHCP环境中,可以直接在DHCP服务器上为此接口分配固定IP地址,因此,没有必要再使用此参数手动指定;

script:指定用于配置当前接口网络属性的脚本,默认为xend的配置文件中使用vif-script指定的脚本;

vifname:定义当前网络接口的后端设备在Dom0显示的名字;默认为vifDomID.DevID,其在当前域启动时自动生成,并随当前域ID的变化而改变;为其使用易于识别的固定名称有助于后期的管理工作;

rate:为当前接口指定可用带宽,例如rate=10MB/s;

model:由QEMU仿真的网络设备的类型,因此,只有在type的值为ioemu此参数才能意义;其可能的取值有lance、ne2k_isa、ne2k_pci、rt1839和smc91c111。默认为ne2k_pci;


无论DomU中安装的是什么操作系统,为其定义网络接口时指定固定的MAC地址和接口名称通常是很有必要,因为其有助于追踪特定域的报文及当域多次启动后仍能使用相同的网络接口名称从而保证日志信息的连贯性。此外,如果Dom0中定义了多个桥接设备,还应该为桥接的网络接口使用bridge参数指定固定桥接到的桥接设备。下面的示例展示了指定此三个参数的接口定义。

vif = [ ‘vifname=web0.0, mac=00:16:3E:00:00:01, bridge=xenbr0’ ]


我的示例:

vif = [ 'vifname=web.0,bridge=br0,mac=00:16:3E:6E:AD:FC,ip=192.168.1.15']




关于qcow2格式的磁盘:


qcow2 镜像格式是 QEMU 模拟器支持的一种磁盘镜像。它也是可以用一个文件的形式来表示一块固定大小的块设备磁盘。与普通的 raw 格式的镜像相比,有以下特性:


更小的空间占用,即使文件系统不支持空洞(holes);

支持写时拷贝(COW, copy-on-write),镜像文件只反映底层磁盘的变化;

支持快照(snapshot),镜像文件能够包含多个快照的历史;

可选择基于 zlib 的压缩方式

可以选择 AES 加密


创建 qcow2 和 raw 文件以及两种镜像的对比

使用 QEMU 软件包自带的 qemu-img 软件创建 qcow2 文件。

清单 4. 创建 qcow2 和 raw 文件


$ qemu-img create -f qcow2 test.qcow2 10G

Formatting 'test.qcow2', fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 lazy_refcounts=off


$ qemu-img create -f raw test.raw 10G

Formatting 'test.raw', fmt=raw size=10737418240

对比两种格式的文件的实际大小以及占用空间大小如下:

清单 5. qcow2 和 raw 文件占用空间情况对比

$ ll -sh test.*

200K -rw-r--r-- 1 qiaoliyong qiaoliyong 193K 5 月   6 10:29 test.qcow2

   0 -rw-r--r-- 1 qiaoliyong qiaoliyong  10G 5 月   6 10:28 test.raw


[qiaoliyong@localhost ]$ stat test.raw

  文件:"test.raw"

  大小:10737418240 块:0          IO 块:4096   普通文件


[qiaoliyong@localhost ]$ stat test.qcow2

  文件:"test.qcow2"

  大小:197120     块:400        IO 块:4096   普通文件


从对比中可以看出 qcow 格式的镜像文件大小位 197120 字节,占用空间为 200K,占用了 200 块磁盘空间。而 raw 格式的文件则没有占用磁盘空间,它是一个空洞文件。


Raw 格式与 qcow2 转化:


qemu-img convert -f raw -O qcow2 test.raw test.qcow2


小结

本文着重介绍了 QEMU 虚拟机使用的镜像文件 qcow2 的格式以及特性,并与 raw 格式镜像做了对比。qcow2 格式的文件虽然在性能上比Raw 格式的有一些损失(主要体现在对于文件增量上,qcow2 格式的文件为了分配 cluster 多花费了一些时间),但是 qcow2 格式的镜像比 Raw 格式文件更小,只有在虚拟机实际占用了磁盘空间时,其文件才会增长,能方便的减少迁移花费的流量,更适用于云计算系统,同时,它还具有加密,压缩,以及快照等 raw 格式不具有的功能。


[root@localhost ~]# mkdir -pv /xen/vm1


[root@localhost ~]# cp /etc/xen/xlexample.pvlinux /xen/vm1/centos6.vm1


[root@localhost ~]# qemu-img create -f qcow2 /xen/vm1/centos6.6.qcow2 100G

[root@localhost ~]# qemu-img info /xen/vm1/centos6.6.qcow2 

p_w_picpath: /xen/vm1/centos6.6.qcow2

file format: qcow2

virtual size: 100G (107374182400 bytes)

disk size: 196K

cluster_size: 65536



[root@localhost ~]# qemu-img create -f qcow2 /xen/vm2/centos6.6.qcow2 100G

Formatting '/xen/vm2/centos6.6.qcow2', fmt=qcow2 size=107374182400 encryption=off cluster_size=65536 

[root@localhost ~]# qemu-img info /xen/vm2/centos6.6.qcow2 

p_w_picpath: /xen/vm2/centos6.6.qcow2

file format: qcow2

virtual size: 100G (107374182400 bytes)

disk size: 196K

cluster_size: 65536

[root@localhost ~]# du -sh /xen/vm2/centos6.6.qcow2 

196K /xen/vm2/centos6.6.qcow2


转换:

$> qemu-img convert test.qcow2 -O raw test.img

[root@localhost vm2]# qemu-img convert /xen/centos6.img -O qcow2 /xen/centos6.qcow2


安装centos6.vm2:


[root@localhost ~]# qemu-img create -f qcow2 -o ? /xen/vm2/

Supported options:

size             Virtual disk size

backing_file     File name of a base p_w_picpath

backing_fmt      Image format of the base p_w_picpath

encryption       Encrypt the p_w_picpath

cluster_size     qcow2 cluster size

preallocation    Preallocation mode (allowed values: off, metadata, falloc, full)


创建稀疏格式的qcow2镜像:


[root@localhost ~]# qemu-img create -f qcow2 -o size=100G,preallocation=metadata /xen/vm2/centos6.qcow2

Formatting '/xen/vm2/centos6.qcow2', fmt=qcow2 size=107374182400 encryption=off cluster_size=65536 preallocation='metadata' 

[root@localhost ~]# qemu-img info /xen/vm2/centos6.qcow2 

p_w_picpath: /xen/vm2/centos6.qcow2

file format: qcow2

virtual size: 100G (107374182400 bytes)

disk size: 16M

cluster_size: 65536


配置文件:


[root@localhost ~]# cat /xen/vm2/centos6.vm2 

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

# Example PV Linux guest configuration

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

#

# This is a fairly minimal example of what is required for a

# Paravirtualised Linux guest. For a more complete guide see xl.cfg(5)


# Guest name

name = "centos6.vm2"


# 128-bit UUID for the domain as a hexadecimal number.

# Use "uuidgen" to generate one if required.

# The default behavior is to generate a new UUID each time the guest is started.

#uuid = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"


# Kernel p_w_picpath to boot

kernel = "/source/vmlinuz"


# Ramdisk (optional)

#ramdisk = "/boot/initrd.gz"

ramdisk = "/source/initrd.img"


# Kernel command line options

#extra = "root=/dev/xvda1"


#bootloader = "/usr/bin/pygrub"


# Initial memory allocation (MB)

memory = 512


# Maximum memory (MB)

# If this is greater than `memory' then the slack will start ballooned

# (this assumes guest kernel support for ballooning)

#maxmem = 512


# Number of VCPUS

vcpus = 2


# Network devices

# A list of 'vifspec' entries as described in

# docs/misc/xl-network-configuration.markdown

vif = [ 'bridge=br0' ]


# Disk Devices

# A list of `diskspec' entries as described in

# docs/misc/xl-disk-configuration.txt

disk = [ 'file:/xen/vm2/centos6.qcow2,xvda,rw' ]



[root@localhost ~]# xl create -c /xen/vm2/centos6.vm2 


... ...


Welcome to CentOS for x86_64


                    ┌────────┤ Choose a Language ├────────┐

                    │                                     │

                    │ What language would you like to use │

                    │ during the installation process?    │

                    │                                     │

                    │      Catalan                ↑       │

                    │      Chinese(Simplified)           │

                    │      Chinese(Traditional)         │

                    │      Croatian                     │

                    │      Czech                         │

                    │      Danish                       │

                    │      Dutch                         │

                    │      English                ↓       │

                    │                                     │

                    │               ┌────┐                │

                    │               │ OK │                │

                    │               └────┘                │

                    │                                     │

                    │                                     │

                    └─────────────────────────────────────┘


  <Tab>/<Alt-Tab> between elements  | <Space> selects | <F12> next screen


  Welcome to CentOS for x86_64


                    

                    

                        ┌───┤ Installation Method ├───┐

                        │                             │

                        │ What type of media contains │

                        │ the installation p_w_picpath?     │

                        │                             │

                        │        Local CD/DVD         │

                        │        Hard drive           │

                        │        NFS directory        │

                        │        URL                  │

                        │                             │

                        │   ┌────┐       ┌──────┐     │

                        │   │ OK │       │ Back │     │

                        │   └────┘       └──────┘     │

                        │                             │

                        │                             │

                        └─────────────────────────────┘

                    

                    


  <Tab>/<Alt-Tab> between elements  | <Space> selects | <F12> next screen

上面这项:选择URL


Welcome to CentOS for x86_64


                    

                 ┌────────────┤ Configure TCP/IP ├────────────┐

                 │                                            │

                 │ [*] Enable IPv4 support                    │

                 │        ( ) Dynamic IP configuration (DHCP) │

                 │        (*) Manual configuration            │

                 │                                            │

                 │ [ ] Enable IPv6 support                    │

                 │        (*) Automatic                       │

                 │        ( ) Automatic, DHCP only            │

                 │        ( ) Manual configuration            │

                 │                                            │

                 │        ┌────┐              ┌──────┐        │

                 │        │ OK │              │ Back │        │

                 │        └────┘              └──────┘        │

                 │                                            │

                 │                                            │

                 └────────────────────────────────────────────┘

                    

                    


  <Tab>/<Alt-Tab> between elements  | <Space> selects | <F12> next screen


  Welcome to CentOS for x86_64


                    

       ┌────────────────┤ Manual TCP/IP Configuration ├─────────────────┐

       │                                                                │

       │ Enter the IPv4 and/or the IPv6 address and prefix (address /   │

       │ prefix).  For IPv4, the dotted-quad netmask or the CIDR-style  │

       │ prefix are acceptable. The gateway and name server fields must │

       │ be valid IPv4 or IPv6 addresses.                               │

       │                                                                │

       │ IPv4 address: 192.168.1.13____ / 24______________              │

       │ Gateway:      192.168.1.1______________________________        │

       │ Name Server:  _________________________________________        │

       │                                                                │

       │             ┌────┐                        ┌──────┐             │

       │             │ OK │                        │ Back │             │

       │             └────┘                        └──────┘             │

       │                                                                │

       │                                                                │

       └────────────────────────────────────────────────────────────────┘

                    

                    


  <Tab>/<Alt-Tab> between elements  | <Space> selects | <F12> next screen


Welcome to CentOS for x86_64


        ┌────────────────────────┤ URL Setup ├─────────────────────────┐

        │                                                              │

        │          Please enter the URL containing the CentOS          │

        │          installation p_w_picpath on your server.                  │

        │                                                              │

        │ http://192.168.1.53/iso/____________________________________ │

        │                                                              │

        │ [ ] Enable HTTP proxy                                        │

        │                                                              │

        │ Proxy URL        ___________________________________         │

        │ Username         _______________                             │

        │                                                              │

        │ Password         _______________                             │

        │                                                              │

        │            ┌────┐                       ┌──────┐             │

        │            │ OK │                       │ Back │             │

        │            └────┘                       └──────┘             │

        │                                                              │

        │                                                              │

        └──────────────────────────────────────────────────────────────┘


  <Tab>/<Alt-Tab> between elements  | <Space> selects | <F12> next screen


Welcome to CentOS for x86_64

 ┌────────────────────────────────┤ Warning ├─────────────────────────────────┐

 │                                                                            │

 │         Error processing drive:                                 ↑          │

 │                                                                          │

 │         xen-vbd-51712                                                    │

 │         102416MB                                                          │

 │         Xen Virtual Block Device                                          │

 │                                                                          │

 │         This device may need to be reinitialized.                        │

 │                                                                          │

 │         REINITIALIZING WILL CAUSE ALL DATA TO BE LOST!                    │

 │                                                                          │

 │         This action may also be applied to all other disks                │

 │         needing reinitialization.                               ↓          │

 │                                                                            │

 │  ┌────────┐   ┌────────────┐   ┌───────────────┐   ┌───────────────────┐   │

 │  │ Ignore │   │ Ignore all │   │ Re-initialize │   │ Re-initialize all │   │

 │  └────────┘   └────────────┘   └───────────────┘   └───────────────────┘   │

 │                                                                            │

 │                                                                            │

 └────────────────────────────────────────────────────────────────────────────┘


  <Tab>/<Alt-Tab> between elements   |  <Space> selects   |  <F12> next screen


选择第三项


Welcome to CentOS for x86_64

 

 

                    ┌───────┤ Time Zone Selection ├───────┐

                    │                                     │

                    │ In which time zone are you located? │

                    │                                     │

                    │ [*] System clock uses UTC           │

                    │                                     │

                    │  Asia/Samarkand                  ↑  │

                    │  Asia/Seoul                        │

                    │  Asia/Shanghai                    │

                    │  Asia/Singapore                    │

                    │  Asia/Srednekolymsk              ↓  │

                    │                                     │

                    │      ┌────┐          ┌──────┐       │

                    │      │ OK │          │ Back │       │

                    │      └────┘          └──────┘       │

                    │                                     │

                    │                                     │

                    └─────────────────────────────────────┘

 


  <Tab>/<Alt-Tab> between elements   |  <Space> selects   |  <F12> next screen


  当然是上海啦!


  Welcome to CentOS for x86_64

 

 

                    

                ┌──────────────┤ Root Password ├───────────────┐

                │                                              │

                │    Pick a root password. You must type it    │

    ┌──────────────────────────┤ Weak Password ├──────────────────────────┐

    │                                                                     │

    │ You have provided a weak password: it is based on a dictionary word │

    │                                                                     │

    │           ┌────────┐                      ┌────────────┐            │

    │           │ Cancel │                      │ Use Anyway │            │

    │           └────────┘                      └────────────┘            │

    │                                                                     │

    │                                                                     │

    └─────────────────────────────────────────────────────────────────────┘

                

                └──────────────────────────────────────────────┘

                    

                    

 


  <Tab>/<Alt-Tab> between elements   |  <Space> selects   |  <F12> next screen


设置root密码


Welcome to CentOS for x86_64

 

       ┌─────────────────────┤ Partitioning Type ├─────────────────────┐

       │                                                               │

       │ Installation requires partitioning of your hard drive.  The   │

       │ default layout is suitable for most users.  Select what space │

       │ to use and which drives to use as the install target.         │

       │                                                               │

       │                 Use entire drive                              │

       │                 Replace existing Linux system                 │

       │                 Use free space                                │

       │                                                               │

       │   Which drive(s) do you want to use for this installation?    │

       │      [*]   xvda   102416 MB (Xen Virtual Block Devic) ↑       │

       │                                                             │

       │                                                               │

       │                      ┌────┐   ┌──────┐                        │

       │                      │ OK │   │ Back │                        │

       │                      └────┘   └──────┘                        │

       │                                                               │

       │                                                               │

       └───────────────────────────────────────────────────────────────┘


<Space>,<+>,<-> selection   |   <F2> Add drive   |   <F12> next screen


Welcome to CentOS for x86_64

 

       

       

       

       

     ┌─────────────┤ Writing storage configuration to disk ├──────────────┐

     │                                                                    │

     │ The partitioning options you have selected will now be written to  │

     │ disk.  Any data on deleted or reformatted partitions will be lost. │

     │                                                                    │

     │        ┌─────────┐               ┌───────────────────────┐         │

     │        │ Go back │               │ Write changes to disk │         │

     │        └─────────┘               └───────────────────────┘         │

     │                                                                    │

     │                                                                    │

     └────────────────────────────────────────────────────────────────────┘

       

       

       

       

       


  <Tab>/<Alt-Tab> between elements   |  <Space> selects   |  <F12> next screen


  开始安装


  Welcome to CentOS for x86_64

 

       

       

       

     ┌─────────────────────┤ Package Installation ├──────────────────────┐

     │                                                                   │

     │                                                                   │

     │                                94%                                │

     │                                                                   │

     │                  Packages completed: 182 of 205                   │

     │                                                                   │

     │ Installing selinux-policy-targeted-3.7.19-260.el6.noarch (3 MB)   │

     │ SELinux targeted base policy                                      │

     │                                                                   │

     │                                                                   │

     │                                                                   │

     └───────────────────────────────────────────────────────────────────┘

       

       

       

       


  <Tab>/<Alt-Tab> between elements   |  <Space> selects   |  <F12> next screen


Welcome to CentOS for x86_64

 

       

       

       

     ┌───────────────────────────┤ Complete ├────────────────────────────┐

     │                                                                   │

     │ Congratulations, your CentOS installation is complete.            │

     │                                                                   │

     │ Please reboot to use the installed system.  Note that updates may │

     │ be available to ensure the proper functioning of your system and  │

     │ installation of these updates is recommended after the reboot.    │

     │                                                                   │

     │                            ┌────────┐                             │

     │                            │ Reboot │                             │

     │                            └────────┘                             │

     │                                                                   │

     │                                                                   │

     └───────────────────────────────────────────────────────────────────┘

       

       

       


                                <Enter> to exit

在重启前别忘了改配置文件!


注释掉:

#kernel = "/source/vmlinuz"

#ramdisk = "/source/initrd.img"


启用:

bootloader = "/usr/bin/pygrub"


[root@localhost ~]# vim /xen/vm2/centos6.vm2

[root@localhost ~]# cat /xen/vm2/centos6.vm2 

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

# Example PV Linux guest configuration

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

#

# This is a fairly minimal example of what is required for a

# Paravirtualised Linux guest. For a more complete guide see xl.cfg(5)


# Guest name

name = "centos6.vm2"


# 128-bit UUID for the domain as a hexadecimal number.

# Use "uuidgen" to generate one if required.

# The default behavior is to generate a new UUID each time the guest is started.

#uuid = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"


# Kernel p_w_picpath to boot

#kernel = "/source/vmlinuz"


# Ramdisk (optional)

#ramdisk = "/boot/initrd.gz"

#ramdisk = "/source/initrd.img"


# Kernel command line options

#extra = "root=/dev/xvda1"


bootloader = "/usr/bin/pygrub"


# Initial memory allocation (MB)

memory = 512


# Maximum memory (MB)

# If this is greater than `memory' then the slack will start ballooned

# (this assumes guest kernel support for ballooning)

#maxmem = 512


# Number of VCPUS

vcpus = 2


# Network devices

# A list of 'vifspec' entries as described in

# docs/misc/xl-network-configuration.markdown

vif = [ 'bridge=br0' ]


# Disk Devices

# A list of `diskspec' entries as described in

# docs/misc/xl-disk-configuration.txt

disk = [ 'file:/xen/vm2/centos6.qcow2,xvda,rw' ]

on_reboot = 'restart'

on_crash = 'destroy'



[root@localhost ~]# xl list

Name                                        ID   Mem VCPUs State Time(s)

Domain-0                                     0  1024     4     r-----    1180.5

centos6.vm1                                 14   512     2     -b----      92.0

centos6                                     26   512     2     -b----      74.9

centos6.vm2                                 31   512     2     -b----       5.4


[root@localhost ~]# xl reboot centos6.vm2

[root@localhost ~]# xl shutdown centos6.vm2

[root@localhost ~]# xl create /xen/vm2/centos6.vm2

[root@localhost ~]# xl console centos.vm2



CentOS release 6.6 (Final)

Kernel 2.6.32-504.el6.x86_64 on an x86_64


localhost.localdomain login: root

Password: