没有实际的测试硬件,以下是在VMware Workstation中安装centos 5.5进行的。
前提:
xen提供半虚拟化和全虚拟化两种模式,全虚拟化要求CPU支持vmx(Intel的VT)或svm(AMD的SVM)技术,半虚拟化只要求支持pae。在xen中安装windows系统需要全虚拟化技术,否则只能装相同版本的linux.
 
查看CPU参数:
[root@study ~]# cat /proc/cpuinfo | grep flags
flags : fpu tsc msr pae cx8 apic mtrr cmov pat clflush acpi mmx fxsr sse sse2 ss syscall nx lm constant_tsc ida arat pni ssse3 cx16 sse4_1 sse4_2 popcnt lahf_lm
以上信息看到只有pae,所以只能运行在半虚拟化下,另半虚拟化安装方式不支持光盘安装,只能用http、nfs、ftp等。
 
1.安装xen套件与核心:
yum -y install kernel-xen xen xen-lib virt-manager python-virtinst
 
软件:
kernel-xen: Linux核心,使用xen可以运行;
xen: 主要套件,包括配置文件、启动脚本、运行库;
xen-lib:运行库;
virt-manager:对xen图形管理工具
python-virtinst: 提供Python的virt-install命令行工具
 
2.修改默认的kernel启动顺序
[root@study ~]# vim /boot/grub/menu.lst
default=0        #将“1”修改成“0”,表示启动第一个内核,即xen内核
timeout=5
splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.18-194.el5xen)
root (hd0,0)
kernel /xen.gz-2.6.18-194.el5
module /vmlinuz-2.6.18-194.el5xen ro root=LABEL=/
module /initrd-2.6.18-194.el5xen.img
title CentOS (2.6.18-194.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-194.el5 ro root=LABEL=/
initrd /initrd-2.6.18-194.el5.img
修改完成后重启系统,检查当前的内核
 
[root@study ~]# uname -r
2.6.18-194.el5xen
查看服务
[root@study ~]# chkconfig --list | grep xend
xend 0:off 1:off 2:on 3:on 4:on 5:on 6:off
xendomains 0:off 1:off 2:off 3:on 4:on 5:on 6:off
 
3.检查xen是否启动
[root@study ~]# xm list
Name ID Mem(MiB) VCPUs State Time(s)
Domain-0 0 864 1 r----- 24.6
看到第一个虚拟机Domain-0(xen中的虚拟机就是domain)
 
4.创建安装源
由于在半虚拟化环境下不支持使用光盘安装,只能使用http、ftp、nfs方式安装
配置ftp安装源:
[root@study ~]# yum -y install vsftpd
[root@study ~]# chkconfig vsftpd on
[root@study ~]# mount /dev/cdrom /var/ftp/pub/ #将光盘挂载到pub目录下,默认匿名访问
[root@study ~]# /etc/init.d/vsftpd start
 
5.创建虚拟机
可以使用命令virt-install --prompt 根据应答方法创建虚拟机,也可以使用直接使用命令创建
 
  
  1. virt-install -n centos -r 256 --vcpu=1 -f /data/centos -s 5 -p --nographics -l ftp://192.168.209.253/pub 
参数:
-n centos #主机名
-r 256 #内存大小
--vcpu=1 #cpu数量
-f /data/centos #磁盘文件
-s 5 #磁盘空间,单位G
-p #半虚拟化
-l ftp//:192.168.209.253/pub #安装源
执行命令后,使用文本方式安装也可以选择vnc,安装过程和一般安装系统方法类似。
clip_p_w_picpath002
如果你希望在整个安装过程中实现自动安装,可通过使用kickstart的图形工具建立ks.cfg然后执行命令:
 
  
  1. virt-install -n centos -r 256 --vcpu=1 -f /data/centos -s 5 -p --nographics -l \ 
  2. ftp://192.168.209.253/pub -x ks=ftp://192.168.209.128/pub/ks.cfg \
  3. -x ks=ftp://192.168.209.128/pub/ks.cfg  #ks.cfg路径 
clip_p_w_picpath004
附:ks.cfg(安装完后的密码为“redhat”)
 
  
  1. #platform=x86, AMD64, or Intel EM64T 
  2. # System authorization information 
  3. auth --useshadow --enablemd5 
  4. # System bootloader configuration 
  5. bootloader --location=mbr 
  6. # Clear the Master Boot Record 
  7. zerombr 
  8. # Partition clearing information 
  9. clearpart --all --initlabel 
  10. # Use text mode install 
  11. text 
  12. # Firewall configuration 
  13. firewall --disabled 
  14. # Run the Setup Agent on first boot 
  15. firstboot --disable 
  16. # System keyboard 
  17. keyboard us 
  18. # System language 
  19. lang en_US 
  20. # Installation logging level 
  21. logging --level=info 
  22. # Use network installation 
  23. url --url=ftp://192.168.209.253/pub 
  24. # Network information 
  25. network --bootproto=dhcp --device=eth0 --onboot=on 
  26. # Reboot after installation 
  27. reboot 
  28. #Root password 
  29. rootpw --iscrypted $1$36bsCbH1$UDJ6eCqPi2Nm1KsgajvHs1 
  30. # SELinux configuration 
  31. selinux --disabled 
  32. # Do not configure the X Window System 
  33. skipx 
  34. # System timezone 
  35. timezone --isUtc Asia/Shanghai 
  36. # Install OS instead of upgrade 
  37. install 
  38. # Disk partitioning information 
  39. part /boot --fstype ext3 --size=100 --ondisk=xvda 
  40. part / --fstype ext3 --size=2048 --ondisk=xvda --asprimary 
  41. part swap --size=512 --ondisk=xvda 
  42. part /data --fstype="ext3" --grow --size=1 --ondisk=xvda 
  43. %packages 
  44. @base 
  45. @development-libs 
  46. @development-tools 
  47. @core 
  48. @text-internet 
  49. @editors 
  50. keyutils 
  51. trousers 
  52. fipscheck 
  53. device-mapper-multipath 
  54. imake 
  55. ntp 
 
7.如果你想再创建多一个domain,那可能通过复制方式建立:
[root@study ~]# cp -a /data/centos /data/centos1 #复制磁盘
[root@study ~]# cp /etc/xen/centos /etc/xen/centos1 #复制配置文件
修改配置文件中的name、uuid、disk、mac
[root@study ~]# vim /etc/xen/centos1
 
  
  1. name = "centos1" 
  2. uuid = "ffeb8bd5-736c-83b2-c40d-1d3b15570a3d" 
  3. maxmem = 256 
  4. memory = 256 
  5. vcpus = 1 
  6. bootloader = "/usr/bin/pygrub" 
  7. on_poweroff = "destroy" 
  8. on_reboot = "restart" 
  9. on_crash = "restart" 
  10. disk = [ "tap:aio:/data/centos1,xvda,w" ] 
  11. vif = [ "mac=00:16:36:67:a2:22,bridge=xenbr0,script=vif-bridge" ] 
启动复制的虚拟机
[root@study ~]# xm create -c centos1
系统启动完成后,修改主机名、ip等信息
 
8.操作管理虚拟机
运行
[root@study ~]# xm create -c centos
关闭
[root@study ~]# xm shutdown centos
重启
[root@study ~]# xm reboot centos
进入控制台
[root@study ~]# xm console centos
退出控制台
按键盘上的“ctrl+]”
删除
[root@study ~]# xm destroy centos #关闭电源
[root@study ~]# rm /etc/xen/centos #删除配置
[root@study ~]# rm /data/centos #删除磁盘
配置xen随宿主机启动
[root@study ~]# ln -s /etc/xen/centos /etc/xen/auto/centos
 
xm相关命令:
xm create vm1 #其中vm1 为虚拟机的配置文件,位于/etc/xen/vm/vm1
xm create -c vm1 #如果需要启动控制台,则可以运行命令
xm list #可以列出所有的虚拟机
xm console <domid> #可以显示某虚拟机的控制台
xm reboot <domid> #可以重起id 为domid 的虚拟机
xm shutdown <domid> #可以关闭id 为domid 的虚拟机
xm destroy <domid> #可以销毁id 为domid 的虚拟机
xm console fc5 #从终端或控制台登录正在运行的虚拟操作系统
xm save <DomId> <File> # 存储正在运行的虚拟操作系统的状态
xm restore <File> # 唤醒虚拟操作系统
xm pause <DomId> #停止正在运行的虚拟操作系统
xm unpause <DomId> #激活停止的虚拟操作系统
xm mem-set <DomId> <Mem> #调整虚拟平台/虚拟操作系统的占用内存
xm shutdown 虚拟操作系统的Name或DomID #关闭被虚拟的系统
xm destroy <DomId> #立即停止虚拟的系统 (重要);
xm vcpu-set <DomId> <VCPUs> #调整虚拟平台及虚拟操作系统的虚拟CPU个数
xm top 或 xentop # 查看虚拟系统运行的状态
 
virsh常用命令如下:
命令 说明
help 显示该命令的说明
quit 结束 virsh,回到 Shell
connect 连接到指定的虚拟机服务器
create 启动一个新的虚拟机
destroy 删除一个虚拟机
start 开启(已定义的)非启动的虚拟机
define 从 XML 定义一个虚拟机
undefine 取消定义的虚拟机
dumpxml 转储虚拟机的设置值
list 列出虚拟机
reboot 重新启动虚拟机
save 存储虚拟机的状态
restore 回复虚拟机的状态
suspend 暂停虚拟机的执行
resume 继续执行该虚拟机
dump 将虚拟机的内核转储到指定的文件,以便进行分析与排错
shutdown 关闭虚拟机
setmem 修改内存的大小
setmaxmem 设置内存的最大值
setvcpus 修改虚拟处理器的数量