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
[[email protected] ~]# uname -r
4.9.13-22.el6.x86_64
[[email protected] ~]# 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
[[email protected] ~]# xl list
Name ID Mem VCPUs State Time(s)
Domain-0 0 1024 4 r----- 310.2
2、設定橋接
安裝橋工具
# yum -y install bridge-utils
[[email protected] ~]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# cp ifcfg-eth0 ifcfg-br0
[[email protected] 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
[[email protected] 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
[[email protected] network-scripts]# service network restart
[[email protected] ~]# 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)
[[email protected] ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.ac220bda5c5a no eth0
virbr0 8000.52540050b5d5 yes virbr0-nic
[[email protected] ~]# brctl stp br0 on
[[email protected] ~]# 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)、建立虛拟磁盤映像檔案
建立空的虛拟磁盤映像(稀疏磁盤鏡像),以之作為建立非特權域的虛拟磁盤檔案,此映像檔案并不真正占用為其指定的空間,而是随着存儲的内容而變化。
[[email protected] ~]# mkdir /xen
[[email protected] ~]# ll -d /xen/
drwxr-xr-x 2 root root 4096 Mar 20 16:54 /xen/
[[email protected] xen]# dd if=/dev/zero of=centos6.img oflag=direct seek=102399 bs=1M count=1
[[email protected] xen]# du -sh centos6.img
1.0M centos6.img
[[email protected] 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 目錄下
[[email protected] xen]# mkdir /source
[[email protected] xen]# rz
[[email protected] source]# ls
CentOS-6.6-x86_64-minimal.iso
3)、安裝Apache并且挂載(CentOS-6.5-x86_64-minimal.iso)ISO鏡像檔案
[[email protected] xen]# yum install httpd -y
[[email protected] xen]# service httpd start
[[email protected] xen]# cd /var/www/html/
[[email protected] html]# ls
[[email protected] html]# mkdir iso
[[email protected] html]# mount
mount mount.nfs mount.nfs4 mountpoint mountstats mount.tmpfs
[[email protected] 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
[[email protected] 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)、為虛拟機提供核心和核心子產品檔案
[[email protected] html]# cp /var/www/html/iso/isolinux/{vmlinuz,initrd.img} /source
[[email protected] html]# ls /source/
CentOS-6.6-x86_64-minimal.iso initrd.img vmlinuz
5)、修改xlexample.pvlinux檔案
為新的非特權域建立配置檔案(推薦複制這個檔案到指定的位置):
[[email protected] html]# vim /etc/xen/xlexample.pvlinux
[[email protected] 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)、建立虛拟機
[[email protected] ~]# 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
[[email protected] ~]# 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"
[[email protected] ~]# vim /etc/xen/xlexample.pvlinux
[[email protected] ~]# 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'
[[email protected] ~]# xl list
Name ID Mem VCPUs State Time(s)
Domain-0 0 1024 4 r----- 926.8
centos6 2 512 2 -b---- 5.6
[[email protected] ~]# xl reboot 2 (centos 的ID)
[[email protected] ~]# xl list
Name ID Mem VCPUs State Time(s)
Domain-0 0 1024 4 r----- 929.7
centos6 3 512 2 -b---- 5.5
[[email protected] ~]# xl shutdown centos6
[[email protected] ~]# 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
[[email protected] ]$ stat test.raw
檔案:"test.raw"
大小:10737418240 塊:0 IO 塊:4096 普通檔案
[[email protected] ]$ 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 格式不具有的功能。
[[email protected] ~]# mkdir -pv /xen/vm1
[[email protected] ~]# cp /etc/xen/xlexample.pvlinux /xen/vm1/centos6.vm1
[[email protected] ~]# qemu-img create -f qcow2 /xen/vm1/centos6.6.qcow2 100G
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# 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
[[email protected] ~]# du -sh /xen/vm2/centos6.6.qcow2
196K /xen/vm2/centos6.6.qcow2
轉換:
$> qemu-img convert test.qcow2 -O raw test.img
[[email protected] vm2]# qemu-img convert /xen/centos6.img -O qcow2 /xen/centos6.qcow2
安裝centos6.vm2:
[[email protected] ~]# 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鏡像:
[[email protected] ~]# 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'
[[email protected] ~]# 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
配置檔案:
[[email protected] ~]# 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' ]
[[email protected] ~]# 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"
[[email protected] ~]# vim /xen/vm2/centos6.vm2
[[email protected] ~]# 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'
[[email protected] ~]# 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
[[email protected] ~]# xl reboot centos6.vm2
[[email protected] ~]# xl shutdown centos6.vm2
[[email protected] ~]# xl create /xen/vm2/centos6.vm2
[[email protected] ~]# 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:
轉載于:https://blog.51cto.com/lijianmin2008/1942968