#topicpath
----
仮想マシン構築のために [[Xen:http://www.xensource.com/]] を入れてみます。
Xen は 3.0 が最新版ですが、Linux kernel 2.4 には対応していないので 2.0 にしました。

#contents

* 仮想マシンの設計 [#la830a7d]

OS は全部 CentOS 3.8 にしています。
domU の仮想マシンは2つ作ります。

- 全体
-- メモリ 2048MB
-- ディスク 80GB

- dom0
-- メモリ 256MB
-- swap 2GB
-- ディスク (domU を除いた残り)
- domU (domain-1)
-- メモリ 1024MB
-- swap 2GB
-- ディスク 20GB
- domU (domain-2)
-- メモリ 768MB
-- swap 2GB
-- ディスク 20GB

* 必要なものを確認 [#l767df58]

- iproute2

OS のインストール時に入っていました。

 [root@centos3 root]# rpm -q iproute
 iproute-2.4.7-11.30E.1

- bridge-utils

入っていなかったので、yum でインストールします。

 [root@centos3 root]# rpm --import http://ftp.riken.jp/Linux/centos/RPM-GPG-KEY-CentOS-3
 [root@centos3 root]# yum install bridge-utils
 Server: CentOS-3 - Addons
 Server: CentOS-3 - Base
 Server: CentOS-3 - Extras
 Server: CentOS-3 - Updates
 Finding updated packages
 Downloading needed headers
 Resolving dependencies
 Dependencies resolved
 I will do the following:
 [install: bridge-utils 0.9.3-8.i386]
 Is this ok [y/N]: y
 Downloading Packages
 Getting bridge-utils-0.9.3-8.i386.rpm
 bridge-utils-0.9.3-8.i386 100% |=========================|  24 kB    00:00
 Running test transaction:
 Test transaction complete, Success!
 bridge-utils 100 % done 1/1
 Installed:  bridge-utils 0.9.3-8.i386
 Transaction(s) Complete

- python-twisted

これは yum でも取れなかったので、手で入れます。

 [root@centos3 root]# wget http://tmrc.mit.edu/mirror/twisted/Twisted-1.3.0.tar.gz
 [root@centos3 root]# tar xvpfz Twisted-1.3.0.tar.gz
 [root@centos3 root]# cd Twisted-1.3.0
 [root@centos3 Twisted-1.3.0]# python setup.py install

* インストール [#a32359c2]

** ソースコードの取得からmakeまで [#d200f952]

 [root@centos3 root]# wget http://www.cl.cam.ac.uk/Research/SRG/netos/xen/downloads/xen-2.0.7-src.tgz
 [root@centos3 root]# tar xvpfz xen-2.0.7-src.tgz
 [root@centos3 root]# cd xen-2.0

Makefile を Linux 2.4 用に変えます。

 #KERNELS ?= linux-2.6-xen0 linux-2.6-xenU
 KERNELS ?= linux-2.4-xen0 linux-2.4-xenU

ここで make すれば Xen 用の kernel が出来上がるのですが、
デバイスドライバがなくうまく動かないことがあります。

例えばわたしの環境では、NIC がうまく動きませんでした。
デバイスドライバがロードされていないため、
起動時にこのように言われてしまいます。

 eth0 has different mac address than expected.

linux-2.4.30-xen-sparse/arch/xen/ ディレクトリに、
カーネルの設定ファイルがあります。

- dom0 : defconfig-xen0
- domU : defconfig-xenU

ここでは dom0 に次の行を追加しました。

 CONFIG_8139T00=y

必要なデバイスの追加が済んだら、いよいよ make します。

 [root@centos3 xen-2.0]# make linux24

Linux 2.4.30 のカーネルソースを取ってきて、
Xen 用に make してくれます。
dom0 と domU の二つを作るので、少々時間が掛かります。

終わったらインストール。

 [root@centos3 xen-2.0]# make install

** GRUBの設定 [#v38adb33]

/boot/grub/grub.conf に dom0 のエントリを追加します。
dom0 用に 256MB のメモリを割り当てています。

 title CentOS-3 (2.4.30 Xen-dom0)
         root (hd0,0)
         kernel /xen.gz dom0_mem=262144
         module /vmlinuz-2.4-xen0 ro root=/dev/hda2 console=tty0

** 追加の設定 [#z38a5269]

パフォーマンスのために、/lib/tls を見えないようにする必要があるそうです。

 [root@centos3 root]# mv /lib/tls /lib/tls.disabled

* dom0 で起動 [#x2e59704]

再起動して、GRUB のメニューから dom0 のものを選びます。
見た目はほとんど変わりませんが、
メモリが 256MB に制限されていることがわかります。

 [root@centos3 root]# uname -r
 2.4.30-xen0

 [root@centos3 root]# cat /proc/meminfo
         total:    used:    free:  shared: buffers:  cached:
 Mem:  268435456 246272000 22163456        0 10432512 121716736
 Swap: 2089209856        0 2089209856
 MemTotal:       262144 kB
 MemFree:         21644 kB
 MemShared:           0 kB
 Buffers:         10188 kB
 Cached:         118864 kB
 SwapCached:          0 kB
 Active:          36300 kB
 Inactive:        92892 kB
 HighTotal:           0 kB
 HighFree:            0 kB
 LowTotal:       262144 kB
 LowFree:         21644 kB
 SwapTotal:     2040244 kB
 SwapFree:      2040244 kB

* xend の起動 [#z930b22d]

仮想マシンを管理する xend を起動する設定をします。
すでにスクリプトが /etc/init.d/xend にあるので、
chkconfig で設定できます。

 [root@centos3 root]# chkconfig --add xend
 [root@centos3 root]# chkconfig --list xend
 xend            0:オフ  1:オフ  2:オン  3:オン  4:オン  5:オン  6:オフ
 [root@centos3 root]# service xend start

xm list で動作確認します。
まだ Domain-0 (dom0) しかありません。

 [root@centos3 root]# xm list
 Name              Id  Mem(MB)  CPU  State  Time(s)  Console
 Domain-0           0      251    0  r----     54.8

* domU のインストール [#r68c9260]

domain-1 と domain-2 用にディスクイメージを作ります。

 [root@centos3 root]# cd /opt
 [root@centos3 opt]# mkdir domain-1 domain-2
 [root@centos3 opt]# dd if=/dev/zero of=domain-1/root.img bs=1M count=20480
 [root@centos3 opt]# dd if=/dev/zero of=domain-1/swap.img bs=1M count=2048
 [root@centos3 opt]# dd if=/dev/zero of=domain-2/root.img bs=1M count=20480
 [root@centos3 opt]# dd if=/dev/zero of=domain-2/swap.img bs=1M count=2048

まず domain-1 の中身を作っていきます。
イメージファイルをフォーマットします。

 [root@centos3 opt]# mke2fs -j domain-1/root.img
 [root@centos3 opt]# mkswap domain-1/swap.img

ファイルシステムをマウントして、yum と rpm の環境を作ります。

 [root@centos3 opt]# mount -t ext3 -o loop domain-1/root.img /mnt
 [root@centos3 opt]# mkdir /mnt/bin /mnt/boot /mnt/dev /mnt/etc /mnt/home /mnt/lib \
 > /mnt/root /mnt/sbin /mnt/tmp /mnt/usr /mnt/var
 [root@centos3 opt]# rpm --root /mnt --initdb
 [root@centos3 opt]# rpm --root /mnt \
 > --import http://ftp.riken.jp/Linux/centos/RPM-GPG-KEY-CentOS-3
 [root@centos3 opt]# mkdir -p /mnt/var/cache/yum

/etc/fstab を書きます。

 [root@centos3 opt]# cat /mnt/etc/fstab
 /dev/sda1               /                       ext3    defaults        1 1
 /dev/sda2               swap                    swap    defaults        0 0
 none                    /dev/pts                devpts  gid=5,mode=620  0 0
 none                    /dev/shm                tmpfs   defaults        0 0
 none                    /proc                   proc    defaults        0 0

yum で CentOS 3.8 をインストールしていきます。
dom0 と同じなので、/etc/yum.conf の設定もそのままで構いません。
ネットワークインストールということで、かなりの時間が掛かります。

 [root@centos3 opt]# yum --installroot=/mnt groupinstall Base

domU 用のカーネルモジュールをインストールします。

 [root@centos3 opt]# cp -Rp /lib/modules/2.4.30-xenU /mnt/lib/modules

また、domU でも /lib/tls は無効にしておきます。

 [root@centos3 opt]# mv /mnt/lib/tls /mnt/lib/tls.disabled

これで準備は終了です。

 [root@centos3 opt]# umount /mnt

* domU を起動 [#z4087dc5]

** 設定ファイルを書く [#zc423857]

/etc/xen/xmexample[12] のファイルを参考にして、
設定ファイルを書きます。
ここではこのように書きました。

 [root@centos3 root]# cat /opt/domain-1/domU.conf
 kernel = "/boot/vmlinuz-2.4-xenU"
 memory = 384
 name = "domain-1"
 nics = 1
 disk = [ 'file:/opt/domain-1/root.img,sda1,w', 'file:/opt/domain-1/swap.img,sda2,w' ]
 root = "/dev/sda1 ro"

** xm create で起動 [#q6c67c72]

いよいよ仮想マシンの domU を起動してみます。

 [root@centos3 root]# xm create -c /opt/domain-1/domU.conf
 ....
 CentOS release 3.8 (Final)
 Kernel 2.4.30-xenU on an i686
  
 localhost login:

無事起動して、プロンプトが出てきました。

dom0 側で動作状況を確認してみます。

 [root@centos3 root]# xm list
 Name              Id  Mem(MB)  CPU  State  Time(s)  Console
 Domain-0           0      251    0  r----     38.5
 domain-1           2      384    0  -b---      3.7    9602

** domU の終了 [#aec1c70c]

shutdown した後は、C-] で dom0 に戻って来ます。

 ...
 Turning off swap:
 Turning off quotas:
 Halting system...
 Power down.
             
 ************ REMOTE CONSOLE EXITED *****************
 [root@centos3 root]#

dom0 側から終わらせるには、xm shutdown を使います。

 [root@centos3 root]# xm shutdown domain-1

* domU の設定 [#hd0b0e86]

** ネットワークの設定 [#vf749609]

dom0 側で ifconfig すると、xen-br0 が増えていることが
確認できます。これを bridge にするんですね。

 [root@centos3 root]# ifconfig -a
 eth0      Link encap:Ethernet  HWaddr 00:07:40:ZZ:YY:CA
           inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:233 errors:0 dropped:0 overruns:0 frame:0
           TX packets:256 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000
           RX bytes:188737 (184.3 Kb)  TX bytes:38249 (37.3 Kb)
           Interrupt:10 Base address:0xdc00
  
 lo        Link encap:Local Loopback
           inet addr:127.0.0.1  Mask:255.0.0.0
           UP LOOPBACK RUNNING  MTU:16436  Metric:1
           RX packets:338 errors:0 dropped:0 overruns:0 frame:0
           TX packets:338 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:0
           RX bytes:31884 (31.1 Kb)  TX bytes:31884 (31.1 Kb)
 
 xen-br0   Link encap:Ethernet  HWaddr 00:07:40:ZZ:YY:CA
           inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.255
           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
           RX packets:233 errors:0 dropped:0 overruns:0 frame:0
           TX packets:252 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:0
           RX bytes:185475 (181.1 Kb)  TX bytes:37715 (36.8 Kb)

domU 起動用の設定ファイルにネットワーク関連の情報を追記します。
mac= のところは他とかぶらないように付ける必要があります。
gateway は dom0 の IP アドレスを指定しましょう。

 vif = [ 'mac=00:07:40:ZZ:YY:CB, bridge=xen-br0' ]
 netmask = "255.255.255.0"
 gateway = "192.168.1.2"
 hostname = "domain-1"

domU を起動すると、ifconfig で見えることが確認できます。

 [root@localhost root]# uname -r
 2.4.30-xenU
 [root@localhost root]# ifconfig -a
 eth0      Link encap:Ethernet  HWaddr 00:07:40:ZZ:YY:CB
           BROADCAST MULTICAST  MTU:1500  Metric:1
           RX packets:0 errors:0 dropped:0 overruns:0 frame:0
           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:1000
           RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
 
 lo        Link encap:Local Loopback
           LOOPBACK  MTU:16436  Metric:1
           RX packets:0 errors:0 dropped:0 overruns:0 frame:0
           TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
           collisions:0 txqueuelen:0
           RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

あとは IP アドレスを振れば通信できます。

 [root@localhost root]# ifconfig eth0 192.168.1.11 netmask 255.255.255.0

起動時に有効になるように、/etc/ 以下の設定も書いておきましょう。

 (/etc/sysconfig/network)
 NETWORKING=yes
 HOSTNAME=domain-1
 GATEWAY=192.168.1.1
 
 (/etc/sysconfig/network-scripts/ifcfg-eth0)
 DEVICE=eth0
 BOOTPROTO=static
 BROADCAST=192.168.1.255
 HWADDR=00:07:40:ZZ:YY:CB
 IPADDR=192.168.1.11
 NETMASK=255.255.255.0
 NETWORK=192.168.1.0
 ONBOOT=yes
 TYPE=Ethernet
 
 (/etc/resolv.conf)
 nameserver xxx.xxx.xxx.xxx
 
 (/etc/hosts)
 127.0.0.1 localhost.localdomain localhost
 192.168.1.2 centos3
 192.168.1.11 domain-1
 192.168.1.12 domain-2

ファイルの編集時に vi を使うと terminal の設定が合わなくて
見た目が大変なので、TERM 環境変数を vt100 にしておくといいです。

 [root@localhost root]# export TERM=vt100

これで domU を再起動すると、domU から外に出れているはずです。

 [root@domain-1 root]# ping www.xensource.com
 PING www.xensource.com (65.19.129.98) 56(84) bytes of data.
 64 bytes from 65.19.129.98: icmp_seq=0 ttl=53 time=125 ms
 64 bytes from 65.19.129.98: icmp_seq=1 ttl=53 time=128 ms
 64 bytes from 65.19.129.98: icmp_seq=2 ttl=53 time=124 ms
 64 bytes from 65.19.129.98: icmp_seq=3 ttl=53 time=125 ms

** 他の設定 [#d9c702fa]

あとはユーザを作ったりソフトウェアをインストールしたりなど、
普通の OS の設定と同じです。

ssh で入れるようにしておくと便利ですね。

* domU のコピー [#i3c4dd74]

これまで作って来た domain-1 は /opt/domain-1/* に
イメージファイルや設定ファイルがありますが、
これらをコピーして domain-2/ に置き、
設定を変えれば domU がもう一つ作れます。

2つの domU の設定ファイルはこのようになります。
ディスクとネットワークの部分が違います。
メモリは両方 384MB にすると足りなくなったので、
ちょっと小さくしています。(全体が 1024MB のとき)

** /opt/domain-1/domU.conf [#gb8886bc]

 kernel = "/boot/vmlinuz-2.4-xenU"
 memory = 370
 name = "domain-1"
 nics = 1
 disk = [ 'file:/opt/domain-1/root.img,sda1,w', 'file:/opt/domain-1/swap.img,sda2,w' ]
 root = "/dev/sda1 ro"
 vif = [ 'mac=00:07:40:ZZ:YY:CB, bridge=xen-br0' ]
 netmask = "255.255.255.0"
 gateway = "192.168.1.2"
 hostname = "domain-1"

** /opt/domain-2/domU.conf [#l72fe386]

 kernel = "/boot/vmlinuz-2.4-xenU"
 memory = 370
 name = "domain-2"
 nics = 1
 disk = [ 'file:/opt/domain-2/root.img,sda1,w', 'file:/opt/domain-2/swap.img,sda2,w' ]
 root = "/dev/sda1 ro"
 vif = [ 'mac=00:07:40:ZZ:YY:CC, bridge=xen-br0' ]
 netmask = "255.255.255.0"
 gateway = "192.168.1.2"
 hostname = "domain-2"

全部起動してみると、こんな感じになります。

 [root@centos3 root]# xm list
 Name              Id  Mem(MB)  CPU  State  Time(s)  Console
 Domain-0           0      251    0  r----    407.6
 domain-1           7      370    0  -b---      0.5    9607
 domain-2           8      370    0  -b---      0.2    9608

起動していれば、C-] で console を抜けても大丈夫です。
xm console でいつでも戻れます。
シリアルコンソールみたいな感覚ですね。

 [root@centos3 root]# uname -r
 2.4.30-xen0
 [root@centos3 root]# xm console domain-1
 ************ REMOTE CONSOLE: CTRL-] TO QUIT ********
                                                                                 
 [root@domain-1 root]# uname -r
 2.4.30-xenU

Xen で使うメモリは dom0 側の xm info コマンドで確認できます。
version が化けていますが、これは uname -v から取ってきているだけなので気にしないことにします。

 [root@centos3 root]# xm info
 system                 : Linux
 host                   : centos3
 release                : 2.4.30-xen0
 version                : #2 2006' 10D 7&R %0#!&R 22:29:54 JST
 machine                : i686
 cores                  : 1
 hyperthreads_per_core  : 1
 cpu_mhz                : 2592
 memory                 : 1023
 free_memory            : 10

* dom0/domU で 1GB 以上のメモリを使うとき [#nf8fc9d6]

これまでに作った kernel では、メモリを 1024MB 割り当てる設定にしても 900MB 弱しか使ってくれません。

ちゃんと認識させるためには、
Linux の kernel のオプションを変える必要があります。

dom0 は 256MB でいいので、domU 用の kernel を作り直しましょう。

 [root@centos3 root]# cd xen-2.0/linux-2.4.30-xenU
 [root@centos3 root]# make ARCH=xen clean
 [root@centos3 root]# make ARCH=xen menuconfig

menuconfig で "Processor type and features" から
"High Memory Support" の設定で、"4GB" を選びます。

設定できたら make します。

 [root@centos3 root]# make ARCH=xen dep
 [root@centos3 root]# make ARCH=xen
 [root@centos3 root]# make ARCH=xen install

改めてメモリの割り当てを設定して、全部起動してみます。
ちゃんと 1GB ほどのメモリが認識されていることがわかります。

- domain-0 (dom0): 256MB
- domain-1 (domU): 1152MB
- domain-2 (domU): 616MB (残り) 

 [root@centos3 root]# xm list
 Name              Id  Mem(MB)  CPU  State  Time(s)  Console
 Domain-0           0      251    0  r----    468.7
 domain-1           6     1151    0  -b---      2.2    9606
 domain-2           7      615    0  -b---      2.2    9607

* domU の自動起動 [#xe8b25ee]

/etc/init.d/xendomains を使います。

 [root@centos3 root]# chkconfig --add xendomains
 [root@centos3 root]# chkconfig --list xendomains
 xendomains      0:オフ  1:オフ  2:オフ  3:オン  4:オン  5:オン  6:オフ

CentOS 3 ではメッセージの表示に不具合があるので、
次のように直します。

 --- xendomains.orig     2007-01-09 22:24:41.000000000 +0900
 +++ xendomains  2007-01-09 22:25:10.000000000 +0900
 @@ -26,21 +26,7 @@
  AUTODIR=/etc/xen/auto
  LOCKFILE=/var/lock/subsys/xendomains
  
 -if [ -e /lib/lsb ]; then
 -    # assume an LSB-compliant distro (Debian with LSB package,
 -    # recent-enough SuSE, others...)
 -
 -    #. /lib/lsb/init-functions # source LSB standard functions
 -
 -    on_fn_exit()
 -    {
 -       if [ $RETVAL -eq 0 ]; then
 -           log_success_msg
 -       else
 -           log_failure_msg
 -       fi
 -    }
 -elif [ -r $INITD/functions ]; then
 +if [ -r $INITD/functions ]; then
      # assume a Redhat-like distro
      . $INITD/functions # source Redhat functions
 

あとは /etc/xen/auto/ ディレクトリに設定ファイルを入れておくことで、
dom0 の起動時に domU も上げてくれます。

 [root@centos3 root]# ln -s /opt/domain-1/domU.conf /etc/xen/auto/domain-1
 [root@centos3 root]# ln -s /opt/domain-2/domU.conf /etc/xen/auto/domain-2

* dom0 の自動起動 [#m1df6dc4]

ここまでうまくいったら、dom0 も自動起動するようにしましょう。
/boot/grub/grub.conf の default= を dom0 のエントリ番号に変えます。
先頭は 0 なので、dom0 のエントリは 1 になります。

 default=1
 timeout=10
 splashimage=(hd0,0)/grub/splash.xpm.gz
 title CentOS-3 (2.4.21-47.EL)
         root (hd0,0)
         kernel /vmlinuz-2.4.21-47.EL ro root=LABEL=/
         initrd /initrd-2.4.21-47.EL.img
 title CentOS-3 (2.4.30 Xen-dom0)
         root (hd0,0)
         kernel /xen.gz dom0_mem=262144
         module /vmlinuz-2.4-xen0 ro root=/dev/hdb2 console=tty0

----
(memo)

- xen 2.0.7 を展開
- vi Makefile
- make linux24
- cd linux-2.4.30-xen0 (-xenUも)
-- make ARCH=xen clean
-- make ARCH=xen menuconfig
-- make ARCH=xen dep
-- make ARCH=xen
-- make ARCH=xen install
-- make ARCH=xen modules_install

(2回作らなくて済む方法はないか)

- make install-xen

----
#comment

トップ   差分 履歴 リロード   一覧 検索 最終更新   ヘルプ   最終更新のRSS