eric
作者eric2015-04-08 16:02
系统运维工程师, 某金融单位

xen 虚拟机安装

字数 14425阅读 1495评论 0赞 1

前言

现在越来越多的公司为了降低成本,都在使用虚拟化技术,尽量将单台服务器的性能发挥到极致,现在成熟的虚拟化技术有xen,kvm VMware等。这里主要介绍一下xen虚拟化,xen既可以使用半虚拟化技术也可以使用完全虚拟化技术。在半虚拟化技术下获得高效能的表现,较少的性能 损失(典型的情况下大约损失2%,在最糟的情况下会有8%的效能耗损)但是在半虚拟化下需要修改被虚拟系统的内核,以实现系统能被完美的虚拟在Xen上 面。完全虚拟化则是不需要修改系统内核则可以直接运行在Xen上面,所以说在完全虚拟化下Windows也可以运行在xen虚拟机上,估计没有人这么做 吧。呵呵!好了,开始xen的学习之路吧。

环境部署

在192.168.10.38 上安装XEN虚拟机

一、XEN环境安装

1.1、检测cpu是否支持虚拟化

1
2
egrep  "flag" /proc/cpuinfo
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm syscall nx pdpe1gb rdtscp lm constant_tsc ida nonstop_tsc arat pni monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr sse4_1 sse4_2 popcnt lahf_lm

如果看到输出中有 pae,那么cpu就支持半虚拟化,如果输出中同时有pae和vmx/svm(注:vmx为intel,svm为amd),那么cpu支持全虚拟化

二、安装xen核心软件和内核支持

1
yum install  kernel-xen xen xen-libs python-virtinst virt-manager -y

yum install  kernel-xen xen xen-libs python-virtinst virt-manager -y
kernel-xen     :修改的 Linux 核心,使可以跑 Xen 的环境 
xen            :主要的 xen 套件,包括配置文件、启动脚本和一些函数库 
xen-libs       :Xen 所需要的函数库; 
python-virtinst:提供终端安装的软件; 

virt-manager   :用于图形界面管理xen

三、修改启动菜单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@localhost ~]# vim /etc/grub.conf 
 
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/sda2
#          initrd /initrd-version.img
#boot=/dev/sda
default=0         #将1修改为0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.18-400.1.1.el5xen)
        root (hd0,0)
        kernel /xen.gz-2.6.18-400.1.1.el5
        module /vmlinuz-2.6.18-400.1.1.el5xen ro root=LABEL=/
        module /initrd-2.6.18-400.1.1.el5xen.img
title CentOS (2.6.18-348.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-348.el5 ro root=LABEL=/
        initrd /initrd-2.6.18-348.el5.img

注意:
default=0  #数字为启动内核title的顺序。按0. 1 . 2 .3 ..排列顺序。
timeout=5  5改成0  #数字为开机启动停留时间
然后重启系统

四、查看xen是否安装成功

1
2
3
4
[root@localhost ~]# xm list
Name                                      ID Mem(MiB) VCPUs State   Time(s)
Domain-0                                   0    23691    16 r-----     26.4
[root@localhost ~]#

能正常使用xm命令,就说明xen安装成功了,下面开始创建虚拟机。

五、创建虚拟机(domu)

1.1、环境准备

1
2
3
4
5
6
7
8
9
10
root@localhost ~]# mkdir -p /data/xen
[root@localhost ~]# cd /data/xen
使用dd命令创建一个20G的文件
[root@localhost xen]# dd if=/dev/zero of=/data/xen/xen1 bs=1M count=20480
20480+0 records in
20480+0 records out
21474836480 bytes (21 GB) copied, 83.0621 seconds, 259 MB/s
[root@localhost xen]# ls
xen1
[root@localhost xen]#

安装虚拟机操作系统(安装的方式有nfs,http,ftp,这里我们使用最简单方便的http)

1.2、安装apache服务器并挂载镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
root@localhost ~]# yum install httpd -y
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# mkdir ios
[root@localhost html]# ls
ios
#我是直接copy的镜像,这里偷懒了,你们需要自己下载镜像
[root@localhost html]# scp 192.168.10.36:/root/CentOS-5.9-x86_64-bin-DVD-1of2.iso .
The authenticity of host '192.168.10.36 (192.168.10.36)' can't be established.
RSA key fingerprint is 5c:46:47:eb:bb:56:ee:0d:75:aa:4b:78:53:b1:c1:14.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.10.36' (RSA) to the list of known hosts.
root@192.168.10.36's password: 
CentOS-5.9-x86_64-bin-DVD-1of2.iso                           100% 4467MB  41.8MB/s   01:47    
[root@localhost html]# ls
CentOS-5.9-x86_64-bin-DVD-1of2.iso  ios
#挂载镜像到ios目录下
[root@localhost html]# mount -o loop CentOS-5.9-x86_64-bin-DVD-1of2.iso  ios/
[root@localhost html]# /etc/init.d/httpd  start
Starting httpd:                                            [  OK  ]
[root@localhost html]#

1.3、游览器访问测试http服务器

1.png

提示:如果没有出现此页面请检查服务器80端口是否打开,并关闭防火墙,在次测试

到此我们已经可以使用virt-manager(图形界面)或者virt-install来安装系统了,但是本人比较懒,不喜欢一步一步的安装。所 以这里我使用pxe安装系统。由于virt-manager安装比较简单,都是图形的也就是点击鼠标的事,这里就不多说了。我们这里使用virt- install来安装。

在/var/www/html目录下创建pxe文件centos5.9.cfg 内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
[root@localhost html]# pwd
/var/www/html
[root@localhost html]# cat centos5.9.cfg 
# Kickstart file automatically generated by anaconda.
install
#注意 url修改成自己环境的ip,就是你搭建httpd服务的ip
url --url="http://192.168.10.38/ios"
lang en_US.UTF-8
keyboard us
network --device eth0 --bootproto dhcp
#network --bootproto dhcp --device eth1 --onboot=on
rootpw --iscrypted $1$TxCbDPoc$h1aA92rWYpNhuAILlqZeK0
reboot
firewall --disabled
authconfig --enableshadow --enablemd5
selinux --disabled
timezone Asia/Shanghai
bootloader --location=mbr --driveorder=xvda
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
clearpart --all --drives=xvda --initlabel
part /boot --fstype ext3 --size=100 --ondisk=xvda
part swap --size=4096
part / --fstype ext3 --size=100 --grow --asprimary
# The following is the partition information you requested
# Note that any partitions you deleted are not expressed
# here so unless you clear all partitions first, this is
# not guaranteed to work
 
%packages
@admin-tools
@base
@core
@dialup
@editors
@system-tools
system-config-netboot
system-config-netboot-cmd
kexec-tools
iscsi-initiator-utils
fipscheck
device-mapper-multipath
sgpio
perl-Convert-ASN1
imake
emacs
audit
gcc
%post
chkconfig sendmail off
[root@localhost html]#

如果在使用我这个pxe文件中有问题的的时候,可以自行使用如下命令生成pxe文件

创建kickstart 文件(yum install   system-config-kickstart  -y )

1
[root@localhost ~]# system-config-kickstart  (使用此命令生存kickstart文件))

至于pxe安装系统后续会写一篇cobbler自动化安装系统的文章,这里就不多做介绍了如果大家遇到任何问题大家可以留言。

安装配置dhcp服务器(为后续的自动化安装做准备工作)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost ~]# yum install dhcp -y
#修改dhcp的配置文件为
[root@localhost ~]# vim /etc/dhcpd.conf 
ddns-update-style interim;
ignore client-updates;
option domain-name "1006.tv";
option domain-name-servers 192.168.10.38;
default-lease-time 43200;
max-lease-time 86400;
log-facility local7;
subnet 192.168.10.0 netmask 255.255.255.0 {
        range 192.168.10.235 192.168.10.250;
        option routers 192.168.10.38;
}
next-server 192.168.10.38;
filename="pxelinux.0";
#启动服务
[root@localhost ~]# /etc/init.d/dhcpd start

注意:需要将10.38替换成自己的ip,并设置合理的地址段

自动化安装系统(可以大大的提高工作效率)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost html]# virt-install -n xen1 --vcpus 10 -r 20480 -f  /data/xen/xen1  --nographics -p -l http://192.168.10.38/ios -x "ks=http://192.168.10.38/centos5.9.cfg"
 
 
Starting install...
Retrieving file .treeinfo...                                            |  417 B     00:00     
Retrieving file vmlinuz...                                              | 2.1 MB     00:00     
Retrieving file initrd.img...                                           |  12 MB     00:00     
Creating domain...                                                      |    0 B     00:09     
Connected to domain xen1
Escape character is ^]
Bootdata ok (command line is  method=http://192.168.10.38/ios ks=http://192.168.10.38/centos5.9.cfg)
Linux version 2.6.18-348.el5xen (mockbuild@builder10.centos.org) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-54)) #1 SMP Tue Jan 8 18:35:04 EST 2013
BIOS-provided physical RAM map:
 Xen: 0000000000000000 - 0000000500800000 (usable)
No mptable found.
Built 1 zonelists.  Total pages: 5171443
Kernel command line:  method=http://192.168.10.38/ios ks=http://192.168.10.38/centos5.9.cfg
Initializing CPU#0

出现这些信息以后,你就可以去喝茶了。待会系统就安装好了。呵呵。

注意:virt-install 后面的参数,我这里给xen1分配了10颗cpu,20G内存,(如果内存没有这么大,cpu没有这么多需要自己调小一些)使用文本模式安装,如果参数需要调整请使用查看帮助信息

1
[root@localhost ~]# virt-install  --help

六、连接创建好的虚拟机xen1

使用xm list 可以看到我们创建的xen1虚拟机是启动的状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost ~]# xm list
Name                                      ID Mem(MiB) VCPUs State   Time(s)
Domain-0                                   0     3594    16 r-----    342.2
xen1                                       5    20480    10 -b----     10.4
连接到xen1上
[root@localhost ~]# xm console xen1
 
CentOS release 5.9 (Final)
Kernel 2.6.18-348.el5xen on an x86_64
 
localhost.localdomain login: root
Password: 
登录成功
[root@localhost ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog
[root@localhost ~]#

至此虚拟机安装完成,如有任何疑问请留言!

注意一下,这里最容易出错的地方就是自动化安装的过程,有可能会提示不能下载pxe文件,此时需要确定http服务是否正常,dhcp服务是否正常(虚拟机需要获得ip地址才能访问http)

总结

     关于xen的安装部署还是比较容易的,这里我们就介绍这么多了,如果在安装配置中遇到什么问题请留言!

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

1

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关文章

相关问题

相关资料

X社区推广