centos6.7环境半虚拟化软件xen及xm配置工具使用详解



1、xen软件的安装及配置

环境准备:


①操作系统:centos6.7(注意最好使用centos6.7,centos6.5无法使用xen的图形化界面创建操作系统)
②调整虚拟机配置,内存4G(推荐2G以上)
③增加xen安装yum源(系统自带的源没有包括xen软件)
# cd /etc/yum.repos.d/
# vim xen4.repo
[xen4]
name=Xen4 Project
baseurl=http://mirrors.aliyun.com/centos/6.7/xen4/x86_64
enabled=1
gpgcheck=0
cost=300

# yum repolist
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
xen4                                                                                                                                                                    | 3.4 kB     00:00     
xen4/primary_db                                                                                                                                                         | 166 kB     00:00     
base                                                                                                                                                                    | 2.9 kB     00:00     
updates                                                                                                                                                                 | 2.9 kB     00:00     
repo id                                                                                   repo name                                                                                      status
xen4                                                                                      Xen4 Project                                                                                     263
base                                                                                      localyum                                                                                       3,995
updates                                                                                   localyum                                                                                       3,995
repolist: 8,253


安装xen虚拟机服务
# yum install -y xen
# vim /boot/grub/grub.conf
加入kernel /xen.gz dom0_mem=1024M cpufreq=xen dom0_max_vcpus=2 dom0_vcpus_pin

default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (3.18.21-16.el6.x86_64)
        root (hd0,0)
        kernel /xen.gz dom0_mem=1024M cpufreq=xen dom0_max_vcpus=2 dom0_vcpus_pin
        module /vmlinuz-3.18.21-16.el6.x86_64 ro root=/dev/mapper/vg_node4-root rd_NO_LUKS LANG=en_US.UTF-8 rd_LVM_LV=vg_node4/root rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_node4/swap  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb 
        module /initramfs-3.18.21-16.el6.x86_64.img
title CentOS (2.6.32-431.el6.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/vg_node4-root rd_NO_LUKS LANG=en_US.UTF-8 rd_LVM_LV=vg_node4/root rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_node4/swap  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
        initrd /initramfs-2.6.32-431.el6.x86_64.img

centos6.7环境半虚拟化软件xen及xm配置工具使用详解_第1张图片

重新启动系统,启动xend服务,并验证服务是否正常启动

centos6.7环境半虚拟化软件xen及xm配置工具使用详解_第2张图片

# service xend start
Starting xend daemon:                                      [  OK  ]
[root@node4 ~]# xm list
WARNING: xend/xm is deprecated.
Name                                        ID   Mem VCPUs      State   Time(s)
Domain-0                                     0  1024     2     r-----     58.5
[root@node4 ~]# xl list
Name                                        ID   Mem VCPUs	State	Time(s)
Domain-0                                     0  1024     2     r-----      58.7
[root@node4 ~]# xl info
host                   : node4.chinasoft.com
release                : 3.18.21-16.el6.x86_64
version                : #1 SMP Sat Sep 26 01:24:19 UTC 2015
machine                : x86_64
nr_cpus                : 4
max_cpu_id             : 63
nr_nodes               : 1
cores_per_socket       : 2
threads_per_core       : 1
cpu_mhz                : 2099
hw_caps                : 1fabfbff:28100800:00000000:00007f00:f6ba2223:00000000:00000001:00000281
virt_caps              : hvm
total_memory           : 4095
free_memory            : 3022
sharing_freed_memory   : 0
sharing_used_memory    : 0
outstanding_claims     : 0
free_cpus              : 0
xen_major              : 4
xen_minor              : 4
xen_extra              : .3-3.el6
xen_version            : 4.4.3-3.el6
xen_caps               : xen-3.0-x86_64 xen-3.0-x86_32p hvm-3.0-x86_32 hvm-3.0-x86_32p hvm-3.0-x86_64 
xen_scheduler          : credit
xen_pagesize           : 4096
platform_params        : virt_start=0xffff800000000000
xen_changeset          : Wed Oct 21 16:18:30 2015 +0100 git:5418e02-dirty
xen_commandline        : dom0_mem=1024M cpufreq=xen dom0_max_vcpus=2 dom0_vcpus_pin
cc_compiler            : gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16)
cc_compile_by          : mockbuild
cc_compile_domain      : centos.org
cc_compile_date        : Thu Oct 29 12:18:52 UTC 2015
xend_config_format     : 4
[root@node4 ~]# xm info
WARNING: xend/xm is deprecated.
host                   : node4.chinasoft.com
release                : 3.18.21-16.el6.x86_64
version                : #1 SMP Sat Sep 26 01:24:19 UTC 2015
machine                : x86_64
nr_cpus                : 4
nr_nodes               : 1
cores_per_socket       : 2
threads_per_core       : 1
cpu_mhz                : 2099
hw_caps                : 1fabfbff:28100800:00000000:00007f00:f6ba2223:00000000:00000001:00000281
virt_caps              : hvm
total_memory           : 4095
free_memory            : 3022
free_cpus              : 0
xen_major              : 4
xen_minor              : 4
xen_extra              : .3-3.el6
xen_caps               : xen-3.0-x86_64 xen-3.0-x86_32p hvm-3.0-x86_32 hvm-3.0-x86_32p hvm-3.0-x86_64 
xen_scheduler          : credit
xen_pagesize           : 4096
platform_params        : virt_start=0xffff800000000000
xen_changeset          : Wed Oct 21 16:18:30 2015 +0100 git:5418e02-dirty
xen_commandline        : dom0_mem=1024M cpufreq=xen dom0_max_vcpus=2 dom0_vcpus_pin
cc_compiler            : gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16)
cc_compile_by          : mockbuild
cc_compile_domain      : centos.org
cc_compile_date        : Thu Oct 29 12:18:52 UTC 2015
xend_config_format     : 4



2、配置网络

安装桥接工具
# yum install -y bridge-utils


# cd /etc/sysconfig/network-scripts/
# cp ifcfg-eth0 ifcfg-br0
配置网桥设备
# cat ifcfg-br0
DEVICE=br0
TYPE=Bridge
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
IPADDR=192.168.8.44
NETMASK=255.255.255.0
GATEWAY=192.168.8.254
DNS1=192.168.8.251
IPV6INIT=no
USERCTL=no

# cat ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
BRIDGE=br0
IPV6INIT=no
USERCTL=no
重启生效
# service network restart

# ifconfig
br0       Link encap:Ethernet  HWaddr 00:0C:29:D7:3A:10  
          inet addr:192.168.8.44  Bcast:192.168.8.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fed7:3a10/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:520 errors:0 dropped:0 overruns:0 frame:0
          TX packets:47 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:32011 (31.2 KiB)  TX bytes:5460 (5.3 KiB)


eth0      Link encap:Ethernet  HWaddr 00:0C:29:D7:3A:10  
          inet6 addr: fe80::20c:29ff:fed7:3a10/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2198 errors:0 dropped:2 overruns:0 frame:0
          TX packets:60 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:249888 (244.0 KiB)  TX bytes:6922 (6.7 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:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)


# brctl show
bridge name	bridge id		STP enabled	interfaces
br0		8000.000c29d73a10	no		eth0
# brctl stp br0 on
# brctl show
bridge name	bridge id		STP enabled	interfaces
br0		8000.000c29d73a10	yes		eth0




可以生成自动化脚本
#!/bin/bash

brctl addbr br0
ifconfig eth0 0 up
brctl addif br0 eth0
ifconfig br0 192.168.8.45/24 up
brctl stp br0 on


3、使用xm命令行工具配置添加虚拟机

获取linux启动文件
# lftp http://192.168.8.42/cobbler/ks_mirror/centos6.5-x86_64/isolinux/
> mget vmlinuz initrd.img


准备启动文件
# cd /etc/xen/
将xm样例1作为模板
# cp xmexample1 centos6
# vim centos6 

kernel = "/root/vmlinuz"
ramdisk = "/root/initrd.img"
memory = 1024
name = "centos6"
vif = [ 'bridge=br0' ]
disk = [ 'file:/xen/vm1/centos6.img,xvda,w' ]
on_reboot = 'destroy'
on_crash = 'restart'

# mkdir /xen/vm1 -pv
# cd /xen/vm1


# dd if=/dev/zero of=centos6.img oflag=direct seek=102399 bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.00446458 s, 235 MB/s


# ls -lh
total 1.0M
-rw-r--r-- 1 root root 100G May  1 00:46 centos6.img


# cd /etc/xen
# xm create -c centos6

centos6.7环境半虚拟化软件xen及xm配置工具使用详解_第3张图片

centos6.7环境半虚拟化软件xen及xm配置工具使用详解_第4张图片

centos6.7环境半虚拟化软件xen及xm配置工具使用详解_第5张图片

centos6.7环境半虚拟化软件xen及xm配置工具使用详解_第6张图片

centos6.7环境半虚拟化软件xen及xm配置工具使用详解_第7张图片

等安装完成后,在系统重启之前,将虚拟机配置/etc/xen/centos6修改为如下:
#kernel = "/root/vmlinuz"
#ramdisk = "/root/initrd.img"
bootloader = "/usr/bin/pygrub" # 使用pygrub引导系统启动:
memory = 1024
name = "centos6"
vif = [ 'bridge=br0' ]
disk = [ 'file:/xen/vm1/centos6.img,xvda,w' ]
on_reboot = 'destroy'


通过命令连接到虚拟机centos6:
# xm console centos6


# ifconfig查看IP
修改IP,重启网络服务报错:Bringing up interface eth0:  Device eth0 has different MAC address than expected, ignoring.
# ifconfig eth0查看的硬件地址和ifcfg-eth0里面的硬件地址不一致,修改为一致即可

centos6.7环境半虚拟化软件xen及xm配置工具使用详解_第8张图片

centos6.7环境半虚拟化软件xen及xm配置工具使用详解_第9张图片

ctrl+]可以退出当前虚拟机,通过终端可以连接进入8.47




安装vnc软件
# yum install -y tigervnc-server
使用命令创建vnc链接,并设置密码
# vncserver :1
可以看到
# ss -tnlp
windows安装vnc客户端可以连接到Linux主机中

centos6.7环境半虚拟化软件xen及xm配置工具使用详解_第10张图片


# vim centos67


kernel = "/root/vmlinuz"
ramdisk = "/root/initrd.img"
vcpus=2
memory = 512
name = "centos6.7"
vif = [ 'bridge=br0' ]
disk = [ 'file:/xen/vm2/centos67.img,xvda,w' ]
on_reboot = 'destroy'


kernel="/root/vmlinuz"
ramdisk="/root/initrd.img"
vcpus=2
memory=512
name="centos6.7"
vif=['bridge=br0']
disk = ['file:/xen/vm2/centos67.qcow2,xvda,w']
vfb=['vnc=1,vnclisten=0.0.0.0']
on_reboot = 'destroy'
#on_crash = 'restart'


# mkdir -pv /xen/vm2
# qemu-img create -f qcow2 -o ? /xen/vm2/centos67.qcow2
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)


# qemu-img create -f qcow2 -o size=80G,preallocation=metadata /xen/vm2/centos6.qcow2
Formatting '/xen/vm2/centos67.qcow2', fmt=qcow2 size=128849018880 encryption=off cluster_size=65536 preallocation='metadata' 
[root@localhost xen]# ll -h /xen/vm2
total 19M
-rw-r--r-- 1 root root 121G May 17 10:23 centos67.qcow2


libvirt工具的使用
# yum install -y libvirt
# yum install -y virt-manager
# virt-manager &
报错:暂时无法找到解决方案

Unable to initialize GTK: could not open display

后面发现是xshell和xmanager环境的问题,重新安装即可


centos6.7环境半虚拟化软件xen及xm配置工具使用详解_第11张图片

centos6.7环境半虚拟化软件xen及xm配置工具使用详解_第12张图片

centos6.7环境半虚拟化软件xen及xm配置工具使用详解_第13张图片

继续安装报错,估计是红帽对xen的支持不好,关于xen的研究和学习就告一段落:

Unable to complete install: 'internal error: unable to execute QEMU command 'qom-list': The command qom-list has not been found'


Traceback (most recent call last):
  File "/usr/share/virt-manager/virtManager/asyncjob.py", line 44, in cb_wrapper
    callback(asyncjob, *args, **kwargs)
  File "/usr/share/virt-manager/virtManager/create.py", line 1928, in do_install
    guest.start_install(False, meter=meter)
  File "/usr/lib/python2.6/site-packages/virtinst/Guest.py", line 1229, in start_install
    noboot)
  File "/usr/lib/python2.6/site-packages/virtinst/Guest.py", line 1297, in _create_guest
    dom = self.conn.createLinux(start_xml or final_xml, 0)
  File "/usr/lib64/python2.6/site-packages/libvirt.py", line 3488, in createLinux
    if ret is None:raise libvirtError('virDomainCreateLinux() failed', conn=self)
libvirtError: internal error: unable to execute QEMU command 'qom-list': The command qom-list has not been found

centos6.7环境半虚拟化软件xen及xm配置工具使用详解_第14张图片


你可能感兴趣的:(centos6.7环境半虚拟化软件xen及xm配置工具使用详解)