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
splashimage=(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 images 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 image 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 image 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/sysimage/boot done
/mnt/sysimage/dev/pts done
/mnt/sysimage/dev/shm done
/mnt/sysimage/dev done
/mnt/sysimage/home done
/mnt/sysimage/proc/bus/usb done
/mnt/sysimage/proc done
/mnt/sysimage/sys done
/mnt/sysimage/selinux done
/mnt/sysimage 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 image 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
image: /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
image: /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 image
backing_fmt Image format of the base image
encryption Encrypt the image
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
image: /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 image 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 image? │
│ │
│ 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 image 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 image 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: