原作者:FalkoTimme CU网友:tinybiz译
这个教程提供手把手的指导,来教会你怎样在DebianEtch(4.0)的系统中安装Xen。
Xen可以帮助你在使用*nix类系统(比如Linux和Freebsd)的主操作系统中创建客户操作系统,也可以叫做“虚拟机”或者domUs,你可以使用Xen在相同的硬件条件下,把你的应用安装在不同的虚拟机当中,这些虚拟机是相互独立的。例如一台虚拟机是作为邮件服务器,一台是作为大流量的web站点,另外的一台作为你的服务客户的web站点,或者创建一个作为Dns服务器的虚拟机等等,这样做的话,不单可以节约你的资金,更重要的是可以大大提升系统的安全性。如果你的作为Dns服务器的虚拟机被入侵了,并不会对你其他的虚拟机产生任何影响.更值得一提的是,你可以移动虚拟机从一台Xen服务器到另外一台。
我不保证这篇论文会适合你!
1前言
我使用主机名为server1.example.com和IP地址为192.168.0.100的DebianEtch系统来作为我的主系统(dom0).我同时也会在主系统中安装DebianEtch系统来作为我的虚拟机(domU)。
这个教程会为你讲解怎样安装Image-based虚拟机和LVM-based虚拟机。
注意:Xen3.3.1配合使用的相当老的2.6.18内核。如果你使用新的硬件,这个系统可能由于找不到驱动而不能启动。
2.安装Xen
在我们安装Xen之前,我们必须要安装一些系统依赖的软件包。
apt-getinstall make gcc libc6-dev zlib1g-dev python python-devpython-twisted bridge-utils iproute libcurl3 libcurl3-dev bzip2module-init-tools transfig tgif libncurses5-dev patchlibvncserver-dev
libsdl-dev libjpeg62-dev bcc bin86 gawkpciutils-dev mercurial build-essential
安装完成后,我们下载Xen3.3.1
cd /usr/src
wgethttp://bits.xensource.com/oss-xen/release/3.3.1/xen-3.3.1.tar.gz
并且解压它:
tar-xvzf xen-3.3.1.tar.gz
cd xen-3.3.1
现在我们就可以用下面的命令来编译它了:
makeworld && makedist
这将会花掉一些时间,请耐心等待,编译完成后,我们就可以安装Xen了:
./install.sh
安装的过程中会显示类似下面的输出:
server1:/usr/src/xen-3.3.1#./install.sh
Installing Xen from './dist/install' to '/'...
-installing for udev-based system
- modifying permissions
Alldone.
Checking to see whether prerequisite tools areinstalled...
Xen CHECK-INSTALL Sun Feb 8 21:22:39 CET2009
Checking check_brctl: OK
Checking check_crypto_lib:OK
Checking check_curl: unused, OK
Checking check_iproute:OK
Checking check_python: OK
Checking check_python_xml:OK
Checking check_udev: OK
Checking check_xml2: unused,OK
Checking check_zlib_lib: OK
Alldone.
server1:/usr/src/xen-3.3.1#
运行
mv/lib/tls /lib/tls.disabled
然后为Xen创建系统启动项:
update-rc.dxend defaults 20 21
update-rc.d xendomains defaults 21 20
然后我们打开/etc/modules文件并且确保这个文件中有loopmax_loop=64这一行(特别注意:这一步骤仅仅是针对你想常见image-based的虚拟机,如果你想创建lvm-based的虚拟机你可以取消这一步骤):
vi/etc/modules
loop max_loop=64
现在我们来看一下/boot文件夹里的东西:
ls-l /boot/
server1:/usr/src/xen-3.3.1# ls -l /boot/
total15546
-rw-r--r-- 1 root root 71818 2007-03-26 18:32config-2.6.18-4-486
-rw-r--r-- 1 root root 662642009-02-08 21:21 config-2.6.18.8-xen
drwxr-xr-x 2 root root 1024 2008-12-14 00:07 grub
-rw-r--r-- 1 root root 48457772008-12-14 00:04 initrd.img-2.6.18-4-486
drwx------ 2 root root 12288 2008-12-13 23:59 lost+found
-rw-r--r-- 1 rootroot 716728 2007-03-27 00:39System.map-2.6.18-4-486
-rw-r--r-- 1 root root 7525412009-02-08 21:21 System.map-2.6.18.8-xen
-rw-r--r-- 1 root root1220128 2007-03-27 00:39 vmlinuz-2.6.18-4-486
-rw-r--r-- 1 rootroot 1541626 2009-02-08 21:21 vmlinuz-2.6.18.8-xen
-rw-r--r-- 1root root 433689 2009-02-08 21:14 xen-3.3.1.gz
lrwxrwxrwx1 root root 12 2009-02-08 21:22 xen-3.3.gz-> xen-3.3.1.gz
lrwxrwxrwx 1 root root 122009-02-08 21:22 xen-3.gz -> xen-3.3.1.gz
lrwxrwxrwx 1 rootroot 12 2009-02-08 21:22 xen.gz ->xen-3.3.1.gz
-rw-r--r-- 1 root root 6180789 2009-02-08 21:14xen-syms-3.3.1
server1:/usr/src/xen-3.3.1#
正如你所看到的,这里显示的是新内核2.6.18.8-xen,但是这里没有ramdisk,因此我们来创建一个。
depmod2.6.18.8-xen
update-initramfs -c -k2.6.18.8-xen
升级我们的启动引导
update-grub
然后我们重启下系统
reboot
启动完后我们运行命令
uname–r
这时系统的新的Xen内核就会被显示出来
server1:~#uname -r
2.6.18.8-xen
server1:~#
3.安装xen-tools
我们将会使用Xen-tools来创建虚拟机。Xen-tools可以很容易的创建虚拟机,请阅读这个教程来获取更多信息http://www.howtoforge.com/xen_tools_xen_shell_argo.
我们不从DebianEtch的软件库里安装xen-tools(因为版本相当老),而是直接从开发者的软件库安装,这样的话我们就可以获取最新的版本,打开/etc/apt/sources.list文件。
vi/etc/apt/sources.list
添加下面一行
deb http://apt.steve.org.uk/etchetch main non-free contrib
然后运行
wget-q http://apt.steve.org.uk/apt-key.gpg-O- | apt-key add - && apt-get update
安装xen-tools:
apt-getinstall xen-tools
4.创建Image-Based的虚拟机
现在我们编辑/etc/xen-tools/xen-tools.conf.这个文件包含了xen-create-image脚本使用的默认的参数,无需你在命令行中定义其他的参数。我改变下面的参数并且其他的都设置为默认。
vi/etc/xen-tools/xen-tools.conf
[...]
dir =/home/xen
[...]
dist = etch #Default distribution to install.
[...]
gateway =192.168.0.1
netmask = 255.255.255.0
broadcast =192.168.0.255
[...]
passwd = 1
[...]
kernel = /boot/vmlinuz-`uname -r`
initrd =/boot/initrd.img-`uname -r`
[...]
mirror =http://ftp.de.debian.org/debian/
[...]
serial_device= hvc0 #default
[...]
disk_device = xvda#default
[...]
Dir这一行定义虚拟机的镜像存储的位置。
Dist确定将要在虚拟机里安装的版本(DebianEtch)(文件里有一个注释说明所有支持的版本)。
Passwd=1可以使你定义一个创建新的客户域时的root密码。
Mirror定义离你最近的Debian镜像
确保你定义了网关,子网掩码和广播地址。如果你不这样做的话,当你使用xen-create-image命令时,即使你确定了一个IP地址,你的客户域也不能正常工作。
最重要的是在文件中需添加serial_device= hvc0 (or serial_device =xvc0)这一行,否则你的虚拟机可能不能正常启动。
在我们继续下面的操作之前,我们需要创建虚拟机镜像存储文件的目录。
mkdir/home/xen
现在我们来创建我们的第一个客户域,xen1.example.com,所使用的Ip地址是192.168.0.101:
xen-create-image--hostname=xen1.example.com --size=4Gb --swap=256Mb--ip=192.168.0.101 --memory=128Mb --arch=i386--role=udev
操作已经在/etc/xen-tools/xen-tools.conf文件中定义的命令,在操作你没有在/etc/xen-tools/xen-tools.conf定义的命令时,请确保你在命令中加入了--role=udev,否则你的虚拟机可能不能正常启动!
要学习更多的可用参数说明,请查看xen-create-image的man说明。
manxen-create-image
xen-create-image这时就会为我们创建xen1.example.com虚拟机,这将花费一些时间,输出的结果类似于下面所示。
server1:~#xen-create-image --hostname=xen1.example.com --size=4Gb --swap=256Mb--ip=192.168.0.101 --memory=128Mb --arch=i386 --role=udev
GeneralInformation
--------------------
Hostname : xen1.example.com
Distribution : etch
Partitions : swap 256Mb (swap)
/ 4Gb (ext3)
Image type : sparse
Memorysize : 128Mb
Kernel path : /boot/vmlinuz-2.6.18.8-xen
Initrd path : /boot/initrd.img-2.6.18.8-xen
NetworkingInformation
----------------------
IP Address 1 :192.168.0.101 [MAC: 00:16:3E:85:BC:FC]
Netmask : 255.255.255.0
Broadcast :192.168.0.255
Gateway :192.168.0.1
WARNING
-------
Loopback module not loadedand you're using loopback images
Run the following to load themodule:
modprobe loop loop_max=255
Creatingpartition image:/home/xen/domains/xen1.example.com/swap.img
Done
Creatingswap on /home/xen/domains/xen1.example.com/swap.img
Done
Creatingpartition image:/home/xen/domains/xen1.example.com/disk.img
Done
Creatingext3 filesystem on/home/xen/domains/xen1.example.com/disk.img
Done
Installationmethod: debootstrap
Done
Running hooks
Done
Role:udev
File:/etc/xen-tools/role.d/udev
Role script completed.
CreatingXen configuration file
Done
Setting up root password
Enternew UNIX password:
Retype new UNIX password:
passwd: passwordupdated successfully
All done
Logfile produced at:
/var/log/xen-tools/xen1.example.com.log
server1:~#
在这里/etc/xen/xen1.example.com.cfg就会出现xen1.example.com的配置文件,打开它并在文件底部添加下面的参数extra= 'xencons=hvc0 console=hvc0'(或者extra= 'xencons=xvc0console=xvc0'如果你在etc/xen-tools/xen-tools.conf文件中配置的是serial_device= xvc0)
vi/etc/xen/xen1.example.com.cfg
#
# Configuration file for theXen instance xen1.example.com, created
# by xen-tools 4.1 on SunFeb 8 21:42:15 2009.
#
#
# Kernel+ memory size
#
kernel ='/boot/vmlinuz-2.6.18.8-xen'
ramdisk ='/boot/initrd.img-2.6.18.8-xen'
memory ='128'
#
# Disk device(s).
#
root = '/dev/xvda2 ro'
disk =[
'file:/home/xen/domains/xen1.example.com/disk.img,xvda2,w',
'file:/home/xen/domains/xen1.example.com/swap.img,xvda1,w',
]
#
# Physicalvolumes
#
#
# Hostname
#
name = 'xen1.example.com'
#
# Networking
#
vif = ['ip=192.168.0.101,mac=00:16:3E:85:BC:FC']
#
# Behaviour
#
on_poweroff ='destroy'
on_reboot = 'restart'
on_crash = 'restart'
extra = 'xencons=hvc0console=hvc0'
如果你不加入这一行的话,你再使用xmconsole命令进入vm的终端时,启动进程就会被永远挂起了(但是事实上,vm正在正常运行,你可以使用ssh连接它)。
(请注意:如果你有双核或者四核的cpu并且想在虚拟机中使用所有的核,请在配置文件中添加vcpus= '2' or vcpus = '4'这一行)。
如果想启动虚拟机,运行命令
xmcreate /etc/xen/xen1.example.com.cfg
输入
xmconsolexen1.example.com
就会进入到了虚拟机(如果想返回dom0,在console时敲入CTRL+],在putty时敲入CTRL+5),或者你使用SSh的客户端进入(192.168.0.101).
(如果在vm系统启动进程中看到下列信息
EXT3-fs:mounted filesystem with ordered datamode.
***************************************************************
***************************************************************
**WARNING: Currently emulating unsupported memory accesses **
** in /lib/tls glibc libraries. Theemulation is **
** slow. To ensure full performance you should **
** install a 'xen-friendly' (nosegneg) version of **
** the library, or disable tls supportby executing **
** the following as root: **
** mv/lib/tls /lib/tls.disabled **
**Offending process: modprobe (pid=1757) **
***************************************************************
***************************************************************
Continuing...
在vm中运行下列命令
mv/lib/tls /lib/tls.disabled)。
如果想查看主机中所有虚拟机,输入
xmlist
输出的结果类似于
server1:~#xm list
Name ID Mem VCPUs State Time(s)
Domain-0 0 874 1 r----- 714.2
xen1.example.com 1 128 1 -b---- 63.8
server1:~#
如果你想关闭虚拟机,请输入下列命令
xmshutdownxen1.example.com
如果你想虚拟机xen1.example.com在主机启动的时候自动启动,输入下列命令
ln-s /etc/xen/xen1.example.com.cfg /etc/xen/auto
下面将为大家介绍几个重要的Xen命令
xmcreate -c /path/to/config –启动虚拟机
xmshutdown <name> -关闭虚拟机
xmdestroy <name> -立刻关闭虚拟机,类似于直接关闭电源按钮
xmlist列出所有运行的系统
xmconsole <name> -登入虚拟机
xmhelp –命令说明列表
使用xen-create-image命令可以显示所有创建过的虚拟机的列表。
xen-list-images
server1:~#xen-list-images
Name: xen1.example.com
Memory: 128
IP:192.168.0.101
server1:~#
5.创建LVM-Based虚拟机
在这一章将会说明怎样设置一个LVM-Based虚拟机而非使用磁盘镜像的虚拟机,使用磁盘镜像的虚拟机运行时十分慢,并增大了磁盘IO的负担。
在这个例子中,我使用一个带有LVM卷组大约50GB空间的/dev/vg0Debian Etch主机,/dev/vg0包含两个逻辑卷,dev/vg0/root和/dev/vg0/swap_1另外剩余12Gb空间没有分配-可以使我们自爱创建我们的虚拟机时创建逻辑卷。
Vgdisplay
server1:~#vgdisplay
--- Volume group ---
VG Name vg0
SystemID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VGAccess read/write
VG Status resizable
MAX LV 0
CurLV 2
Open LV 2
Max PV 0
Cur PV 1
ActPV 1
VG Size 49.52 GB
PE Size 4.00 MB
TotalPE 12678
AllocPE / Size 3072 / 12.00 GB
Free PE/ Size 9606 / 37.52 GB
VGUUID iXaW0I-RkGD-WQlQ-dMaD-qfM5-xkvq-2w5uP5
server1:~#
lvdisplay
server1:~#lvdisplay
--- Logical volume ---
LVName /dev/vg0/root
VG Name vg0
LV UUID 17thCh-CgS8-ZvrJ-FOai-B1AX-89sv-xxq12a
LV WriteAccess read/write
LVStatus available
#open 1
LV Size 10.00 GB
Current LE 2560
Segments 1
Allocation inherit
Readahead sectors 0
Block device 253:0
---Logical volume ---
LV Name /dev/vg0/swap_1
VGName vg0
LV UUID u2EZyP-3Vp5-Lr32-nquG-ub5a-f0U5-VI3WRN
LVWrite Access read/write
LVStatus available
#open 1
LV Size 2.00 GB
Current LE 512
Segments 1
Allocation inherit
Readahead sectors 0
Block device 253:1
server1:~#
现在我们编辑/etc/xen-tools/xen-tools.conf.这个文件包含了xen-create-image脚本使用的默认的参数,无需你在命令行中定义其他的参数。我改变下面的参数并且其他的都设置为默认。
vi/etc/xen-tools/xen-tools.conf
[...]
lvm = vg0
[...]
dist = etch # Default distribution toinstall.
[...]
gateway = 192.168.0.1
netmask = 255.255.255.0
broadcast = 192.168.0.255
[...]
passwd= 1
[...]
kernel =/boot/vmlinuz-`uname -r`
initrd =/boot/initrd.img-`uname -r`
[...]
mirror =http://ftp.de.debian.org/debian/
[...]
serial_device= hvc0 #default
[...]
disk_device = xvda#default
[...]
确保lvm这一行没有注销,并在这一行填入你的卷组(在我的试验中是vg0).同时确保dir这一行已被注销。Dist确定将要在虚拟机里安装的版本(DebianEtch)(文件里有一个注释说明所有支持的版本)。
Passwd=1可以使你定义一个创建新的客户域时的root密码。
Mirror定义离你最近的Debian镜像
确保你定义了网关,子网掩码和广播地址。如果你不这样做的话,当你使用xen-create-image命令时,即使你确定了一个IP地址,你的客户域也不能正常工作。
最重要的是在文件中需添加serial_device= hvc0 (or serial_device = xvc0)这一行,否则你的虚拟机可能不能正常启动。
在我们继续下面的操作之前,我们需要创建虚拟机镜像存储文件的目录。
mkdir/home/xen
现在我们来创建我们的第一个客户域,xen1.example.com,所使用的Ip地址是192.168.0.101:
xen-create-image--hostname=xen1.example.com --size=4Gb --swap=256Mb--ip=192.168.0.101 --memory=128Mb --arch=i386--role=udev
操作已经在/etc/xen-tools/xen-tools.conf文件中定义的命令,在操作你没有在/etc/xen-tools/xen-tools.conf定义的命令时,请确保你在命令中加入了—role=udev,否则你的虚拟机可能不能正常启动!
要学习更多的可用参数说明,请查看xen-create-image的man说明。
manxen-create-image
xen-create-image这时就会为我们创建xen1.example.com虚拟机,这将花费一些时间,输出的结果类似于下面所示。
server1:~#xen-create-image --hostname=xen1.example.com --size=4Gb --swap=256Mb--ip=192.168.0.101 --memory=128Mb --arch=i386 --role=udev
GeneralInformation
--------------------
Hostname : xen1.example.com
Distribution : etch
Partitions : swap 256Mb (swap)
/ 4Gb (ext3)
Image type : full
Memorysize : 128Mb
Kernel path : /boot/vmlinuz-2.6.18.8-xen
Initrd path : /boot/initrd.img-2.6.18.8-xen
NetworkingInformation
----------------------
IP Address 1 :192.168.0.101 [MAC: 00:16:3E:1C:B9:F3]
Netmask : 255.255.255.0
Broadcast :192.168.0.255
Gateway :192.168.0.1
Creating swap on/dev/vg0/xen1.example.com-swap
Done
Creating ext3filesystem on /dev/vg0/xen1.example.com-disk
Done
Installationmethod: debootstrap
Done
Running hooks
Done
Role:udev
File:/etc/xen-tools/role.d/udev
Role script completed.
CreatingXen configuration file
Done
Setting up root password
Enternew UNIX password:
Retype new UNIX password:
passwd: passwordupdated successfully
All done
Logfile produced at:
/var/log/xen-tools/xen1.example.com.log
server1:~#
正如你从输出结果中看到的那样,xen-create-image已经在vg0卷组中为我们的虚拟机创建了一个新的逻辑卷,/dev/vg0/xen1.example.com-disk为vm的启动文件,用下面命令可以查看,Lvdisplay
同时你也会看到另外一个逻辑卷/dev/vg0/xen1.example.com-swap作为vm的swap
server1:~#lvdisplay
--- Logical volume ---
LVName /dev/vg0/root
VG Name vg0
LV UUID 17thCh-CgS8-ZvrJ-FOai-B1AX-89sv-xxq12a
LV WriteAccess read/write
LVStatus available
#open 1
LV Size 10.00 GB
Current LE 2560
Segments 1
Allocation inherit
Readahead sectors 0
Block device 253:0
---Logical volume ---
LV Name /dev/vg0/swap_1
VGName vg0
LV UUID u2EZyP-3Vp5-Lr32-nquG-ub5a-f0U5-VI3WRN
LVWrite Access read/write
LVStatus available
#open 1
LV Size 2.00 GB
Current LE 512
Segments 1
Allocation inherit
Readahead sectors 0
Block device 253:1
---Logical volume ---
LV Name /dev/vg0/xen1.example.com-swap
VGName vg0
LV UUID No03G8-PzA3-67JH-m936-rUQY-rZFi-djIu21
LVWrite Access read/write
LVStatus available
#open 0
LV Size 256.00 MB
Current LE 64
Segments 1
Allocation inherit
Readahead sectors 0
Block device 253:2
---Logical volume ---
LV Name /dev/vg0/xen1.example.com-disk
VGName vg0
LV UUID xDwpFO-XALQ-7I1N-TA3k-paYc-biG7-TjrNIM
LVWrite Access read/write
LVStatus available
#open 0
LV Size 4.00 GB
Current LE 1024
Segments 1
Allocation inherit
Readahead sectors 0
Block device 253:3
server1:~#
在这里/etc/xen/xen1.example.com.cfg就会出现xen1.example.com的配置文件,打开它并在文件底部添加下面的参数extra= 'xencons=hvc0 console=hvc0'(或者extra= 'xencons=xvc0console=xvc0'如果你在etc/xen-tools/xen-tools.conf文件中配置的是serial_device= xvc0)
vi/etc/xen/xen1.example.com.cfg
#
# Configuration file for theXen instance xen1.example.com, created
# by xen-tools 4.1 on SunFeb 8 22:01:30 2009.
#
#
# Kernel+ memory size
#
kernel ='/boot/vmlinuz-2.6.18.8-xen'
ramdisk ='/boot/initrd.img-2.6.18.8-xen'
memory ='128'
#
# Disk device(s).
#
root = '/dev/xvda2 ro'
disk =[
'phy:/dev/vg0/xen1.example.com-disk,xvda2,w',
'phy:/dev/vg0/xen1.example.com-swap,xvda1,w',
]
#
# Physicalvolumes
#
#
# Hostname
#
name = 'xen1.example.com'
#
# Networking
#
vif = ['ip=192.168.0.101,mac=00:16:3E:1C:B9:F3']
#
# Behaviour
#
on_poweroff ='destroy'
on_reboot = 'restart'
on_crash = 'restart'
extra = 'xencons=hvc0console=hvc0'
如果你不加入这一行的话,你再使用xmconsole命令进入vm的终端时,启动进程就会被永远挂起了(但是事实上,vm正在正常运行,你可以使用ssh连接它)。
(如果你使用的是磁盘镜像而非逻辑卷,disk这一行是下面设置
disk= [ 'file:/path/to/xen1.example.com/disk.img,xvda1,w','file:/path/to/xen1.example.com/swap.img,xvda2,w'])
(请注意:如果你有双核或者四核的cpu并且想在虚拟机中使用所有的核,请在配置文件中添加vcpus= '2' or vcpus = '4'这一行)。
如果要启动虚拟机,运行
xmcreate /etc/xen/xen1.example.com.cfg
输入
xmconsolexen1.example.com
就会进入到了虚拟机(如果想返回dom0,在console时敲入CTRL+],在putty时敲入CTRL+5),或者你使用SSh的客户端进入(192.168.0.101).
(如果在vm系统启动进程中看到下列信息
EXT3-fs:mounted filesystem with ordered datamode.
***************************************************************
***************************************************************
**WARNING: Currently emulating unsupported memory accesses **
** in /lib/tls glibc libraries. Theemulation is **
** slow. To ensure full performance you should **
** install a 'xen-friendly' (nosegneg) version of **
** the library, or disable tls supportby executing **
** the following as root: **
** mv/lib/tls /lib/tls.disabled **
**Offending process: modprobe (pid=1757) **
***************************************************************
***************************************************************
Continuing...
在vm中运行下列命令
mv/lib/tls /lib/tls.disabled)。
如果你想关闭虚拟机,请输入下列命令
xmshutdownxen1.example.com
如果你想虚拟机xen1.example.com在主机启动的时候自动启动,输入下列命令
ln-s /etc/xen/xen1.example.com.cfg /etc/xen/auto
下面将为大家介绍几个重要的Xen命令
xmcreate -c /path/to/config –启动虚拟机
xmshutdown <name> -关闭虚拟机
xmdestroy <name> -立刻关闭虚拟机,类似于直接关闭电源按钮
xmlist列出所有运行的系统
xmconsole <name> -登入虚拟机
xmhelp –命令说明列表
使用xen-create-image命令可以显示所有创建过的虚拟机的列表。
xen-list-images
server1:~#xen-list-images
Name: xen1.example.com
Memory: 128
IP:192.168.0.101
server1:~#
[ 本帖最后由 八重樱 于 2009-3-6 11:40 编辑 ] |