简述理论
虚拟化的实现
模拟技术:模拟计算机的主要硬件硬件并协调工作,但是效率很低下,所以不常用
完全虚拟化:针对cpu的虚拟来分为,基于BT技术(Guset的特权指令在ring1,Host的特权指令在ring0,Guest的特权指令由Host的ring0来参与边翻译边执行)的完全虚拟化,基于HVM(硬件辅助,Guest的特权指令在ring0,Host的特权指令在ring-1,Guest意识不到自己处于虚拟化环境Intel-VT,AMD-V)的完全虚拟化
半虚拟化:Guest的内核知道自己处于虚拟化环境中,自己的特权指令会变成system call来调用Hpervisor执行
Xen
Dom0:能够管理控制VMguest,I/O,驱动,结合qemu来虚拟化磁盘,网卡等I/O设备
DomU:运行在Hpervisor上的各个虚机(VMguest)
Hpervisor:运行在物理机上,管理负责CPU和内存的虚拟化
Hardware:物理机
管理工具:xm/xend,xl(轻量级),xe/xapi(提供了管理Xen的api用于cloud环境),virsh(libvirt库)
实验过程
实验目的是利用xen来安装centos虚机
https://wiki.xen.org/wiki/Xen4CentOS
http://xenbits.xen.org/docs/unstable-staging/
在使用xen4centos时,yum源安装的Dom0的linux内核版本在配置设置桥ip时,会内核恐慌崩溃,因而转向参考Xen made easy
[root@centos ~]# yum install http://au1.mirror.crc.id.au/repo/kernel-xen-release-latest.noarch.rpm 自动产生Xen yum源配置文件
[root@centos ~]# yum install xen45 -y 安装Xen Hpervisor和依赖的包
[root@centos ~]# yum install kernel-xen -y 安装Dom0 使用的内核
[root@centos ~]# cat /boot/grub/grub.conf
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (4.4.21-1.el6xen.x86_64) 自动添加Dom0内核引导title
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.4.21-1.el6xen.x86_64 ro root=/dev/mapper/vg_centos-lv_root
rd_NO_LUKS.UTF-8 rd_LVM_LV=vg_centos/lv_swap rd_NO_MD
SYSFONT=latarcyrheb-sun16 crashkernel=auto rd_LVM_LV=vg_centos/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
module /initramfs-4.4.21-1.el6xen.x86_64.img
title CentOS 6 (2.6.32-504.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-504.el6.x86_64 ro root=/dev/mapper/vg_centos-lv_root
rd_NO_LUKS.UTF-8 rd_LVM_LV=vg_centos/lv_swap rd_NO_MD SYSFONT=latarcyrheb-sun16
crashkernel=auto rd_LVM_LV=vg_centos/lv_root KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb
quiet
initrd /initramfs-2.6.32-504.el6.x86_64.img
[root@centos ~]# reboot 选择刚添加grub.conf Dom0的title,此时本机已为Hpervisor+Dom0
[root@centos ~]# xl list
Name ID Mem VCPUs State Time(s)
Domain-0 0 1024 1 r----- 19.8
[root@centos ~]# xl info
host : centos.localdomain
release : 4.4.21-1.el6xen.x86_64
[root@centos ~]# brctl addbr br0 创建桥设备
[root@centos ~]# ifconfig eth0 0
[root@centos ~]# brctl addif br0 eth0
[root@centos ~]# cat /etc/sysconfig/network-scripts/{ifcfg-eth0,ifcfg-br0} 永久生效
DEVICE=eth0
TYPE=Ethernet 在做实验时桥设备的名字改为了xenbr0,用br0时 VMguest 无法启动
BRIDGE=br0
ONBOOT=yes
NM_CONTROLLED=no
DEVICE=br0
IPADDR=192.168.40.128
TYPE=Bridge
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=static
在192.168.30.140准备安装操作系统文件相当于PXE+DHCP+KICKSTART无人职守安装
利用system-config-kickstart 工具制作ks文件
[root@centos ~]# cat /ks/ks.cfg
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Firewall configuration
firewall --disabled
# Install OS instead of upgrade
install
# Use network installation
url --url= http://192.168.40.130/ 安装源 挂载到网站根目录
# Root password
rootpw --iscrypted $1$.bmjihWZ$ZpJnzWFzzEPsR5.cZgq2..
# System authorization information
auth --useshadow --passalgo=sha512
# Use graphical install
graphical
firstboot --disable
# System keyboard
keyboard us
# System language
lang en_US
# SELinux configuration
selinux --enforcing
# Installation logging level
logging --level=info
# System timezone
timezone Asia/Shanghai
# System bootloader configuration
bootloader --location=mbr
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all --initlabel 最小化安装
把ks文件放在 /ks目录下,在httpd主配置添加目录别名访问,请测试能否访问,如下
Alias /kss/ "/ks/"
<Directory "/ks">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
将welcome.conf重命名避免其生效,必须关闭selinux
[root@centos ~]# cd /etc/httpd/conf.d/
[root@centos conf.d]# ls
mod_dnssd.conf README welcome.conf.bak
挂在光盘到/var/www/html/下,请测试访问网站根目录是否为光盘内容
配置DHCP服务
subnet 192.168.40.0 netmask 255.255.255.0 {
range 192.168.40.10 192.168.40.20;
option subnet-mask 255.255.255.0;
next-server 192.168.40.130; tftp服务器ip
filename "pxelinux.0"; pxe引导程序文件
}
[root@centos ~]# vim /etc/dhcp/dhcpd.conf
[root@centos ~]# service dhcpd restart
Starting dhcpd: [ OK ]
[root@centos ~]# yum install tftp-server
[root@centos xinetd.d]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
[root@centos xinetd.d]# mount /dev/cdrom /mnt
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@centos xinetd.d]# cd /mnt/
[root@centos mnt]# cp images/pxeboot/vmlinuz /var/lib/tftpboot/
[root@centos mnt]# cp images/pxeboot/initrd.img /var/lib/tftpboot/
[root@centos mnt]# cp isolinux/* /var/lib/tftpboot/
cp: overwrite `/var/lib/tftpboot/initrd.img'? n
cp: overwrite `/var/lib/tftpboot/vmlinuz'? n
[root@centos mnt]# mkdir /var/lib/tftpboot/pxelinux.cfg
[root@centos ~]# cp isolinux/isolinux.cfg /var/lib/tftpboot/pxelinux.cfg/default
[root@centos ~]# chmod +wx /var/lib/tftpboot/pxelinux.cfg/default
label linux
menu label ^Install or upgrade an existing system
menu default
kernel vmlinuz ks= "http://192.168.40.130/kss/ks.cfg" 指定ks文件位置
append initrd=initrd.img
[root@centos ~]# grep "disable" /etc/xinetd.d/tftp 更改disable项为no
disable = no
[root@centos ~]# service xinetd start 启动xinetd服务
回到192.168.40.128 [root@centos ~]# man xl.cfg xl.cfg(5) Xen xl.cfg(5) NAME xl.cfg - XL Domain Configuration File Syntax 关于VMguest配置文件中各个指令的帮助 [root@centos ~]# 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 = "example.pvlinux" # 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 = "/boot/vmlinuz" # Ramdisk (optional) #ramdisk = "/boot/initrd.gz" # Kernel command line options extra = "root=/dev/xvda1" # Initial memory allocation (MB) memory = 128 # 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 = [ '' ] # Disk Devices # A list of `diskspec' entries as described in # docs/misc/xl-disk-configuration.txt disk = [ '/dev/vg/guest-volume,raw,xvda,rw' ] [root@centos xen]# cp xlexample.pvlinux centos [root@centos xen]# mkdir -p /images/xen [root@centos xen]# qemu-img create -o size=100G /images/xen/centos.img 创建VMguest磁盘文件 Formatting '/images/xen/centos.img', fmt=raw size=107374182400 [root@centos xen]# mkdir /images/kernel [root@centos xen]# cd /mnt/ [root@centos mnt]# cd isolinux/ [root@centos isolinux]# cp vmlinuz initrd.img /images/kernel/ [root@centos xen]# grep -v "^#" centos | grep -v "^$" name = "centos6.6" kernel = "/images/kernel/vmlinuz" 临时内核引导 ramdisk = "/images/kernel/initrd.img" extra = "ks=http://192.168.40.130/kss/ks.cfg" 指定ks文件位置 memory = 512 vcpus = 2 vif = [ 'xenbr0' ] disk = [ '/images/xen/centos.img,raw,xvda,rw' ] on_boot="shutdown" [root@centos xen]# xl create centos -c 根据VMguest的配置文件启动VM并连接登录 Parsing config from centos [root@centos xen]# xl list Name ID Mem VCPUs State Time(s) Domain-0 0 1022 1 r----- 54.4 centos6.6 5 512 2 -b---- 10.7


安装完成后更改centos配置文件注释掉kernel,initrd, 指明disk文件,extra为空, 加bootloader=pygrub即可,笔记本做实验很吃力,无法验证结果了,,,,
总结
Xen支持PV(front-backend I/O分段虚拟化)和HVM的虚拟化,VMguest磁盘也可以是Dom0的物理分区,内核也可以使用Dom0的内核,实验关于VMguest的管理管理命令xl,网卡vif,磁盘disk的配置参数涉及的比较少,需要多xl help ,参考相关文档来进一步熟悉记忆,在Xen虚拟化平台上安装操作系统也可以使用virt-manager 图形化来管理配置VMguest.
原创文章,作者:Snoo,如若转载,请注明出处:http://www.178linux.com/48079

