debian xen 建立

2009年11月27日 由 月影鹏鹏 留言 »

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

现在我们编辑/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:~#

广告位

留言