Xenの仮想サーバにディスクを増設する(LVM編)

仮想サーバのディスク容量をオンデマンドで増やすということを書いていながら、先日の記事ではディスクイメージ上に作ったext3ファイルシステムを仮想サーバでそのままマウントしていて、柔軟な容量調整ができていない。後から考えれば当然LVMを使うべきだろうということで、やってみた。

これから行うオペレーションでは、storage1-1上にあるデータはすべて消えるので注意。また、仮想サーバ(storage1)はシャットダウン状態からはじめる。

①せっかくなので2つのディスクイメージ(物理ディスクとして認識)をさせるため、コピーを作成。

[root@ml115 ~]# cp /var/xen/images/storage1-1 /var/xen/images/storage1-2

②仮想サーバの定義ファイルを編集し、disk=の後に2つのファイルを追加

[root@ml115 xen]# vi /etc/xen/storage1
# Automatically generated xen config file
name = "storage1"
memory = "256"
disk = [  'tap:aio:/var/lib/xen/images/storage1,xvda,w','file:/var/lib/xen/images/storage1-1,hda,w','file:/var/lib/xen/image
s/storage1-2,hdb,w' ]
vif = [ 'mac=00:16:3e:2b:7f:18, bridge=xenbr0', ]
vfb = ["type=vnc,vncunused=1"]
uuid = "f83c4a0b-4831-f424-d341-ccf824e91418"
bootloader="/usr/bin/pygrub"
vcpus=1
on_reboot   = 'restart'
on_crash    = 'restart'

③仮想サーバを起動

[root@ml115 ~]# xm create /etc/xen/storage1
Using config file "/etc/xen/storage1".
Going to boot CentOS (2.6.18-8.el5xen)
  kernel: /vmlinuz-2.6.18-8.el5xen
  initrd: /initrd-2.6.18-8.el5xen.img
Started domain storage1

④起動したら仮想サーバに入り、/dev/hdaと/dev/hdbが出来ていることを確認する

[root@ml115 ~]# xm console storage1

storage1 login: root
Password:
Last login: Wed Oct 31 00:25:03 on xvc0
[root@storage1 ~]# ls /dev/
console  loop1     parport2  ram4     tty0   tty24  tty4   tty55  urandom
core     loop2     parport3  ram5     tty1   tty25  tty40  tty56  vcs
cpu      loop3     port      ram6     tty10  tty26  tty41  tty57  vcsa
disk     loop4     ppp       ram7     tty11  tty27  tty42  tty58  VolGroup00
evtchn   loop5     ptmx      ram8     tty12  tty28  tty43  tty59  X0R
fb       loop6     pts       ram9     tty13  tty29  tty44  tty6   xvc0
fb0      loop7     ram       ramdisk  tty14  tty3   tty45  tty60  xvda
fd       MAKEDEV   ram0      random   tty15  tty30  tty46  tty61  xvda1
full     mapper    ram1      rawctl   tty16  tty31  tty47  tty62  xvda2
gpmctl   md0       ram10     root     tty17  tty32  tty48  tty63  zero
hda      mem       ram11     rtc      tty18  tty33  tty49  tty7
hdb      net       ram12     shm      tty19  tty34  tty5   tty8
initctl  null      ram13     stderr   tty2   tty35  tty50  tty9
input    nvram     ram14     stdin    tty20  tty36  tty51  ttyS0
kmsg     oldmem    ram15     stdout   tty21  tty37  tty52  ttyS1
log      parport0  ram2      systty   tty22  tty38  tty53  ttyS2
loop0    parport1  ram3      tty      tty23  tty39  tty54  ttyS3

⑤/dev/hdaと/dev/hdbにパーティションを作成(タイプは8e = LVM)

[root@storage1 ~]# fdisk /dev/hda
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): p

Disk /dev/hda: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-522, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-522, default 522):
Using default value 522

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@storage1 ~]# fdisk /dev/hda

Command (m for help): p

Disk /dev/hda: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1               1         522     4192933+  8e  Linux LVM

Command (m for help): q

/dev/hdbも同様なので省略

⑥Physical Volume(物理ボリューム)を作成

[root@storage1 ~]# pvcreate /dev/hda1
  Physical volume "/dev/hda1" successfully created
[root@storage1 ~]# pvcreate /dev/hdb1
  Physical volume "/dev/hdb1" successfully created

⑦Volume Group (ボリュームグループ)を作成

[root@storage1 ~]# vgcreate VolGroup01 /dev/hda1 /dev/hdb1
  Volume group "VolGroup01" successfully created
[root@storage1 ~]# vgdisplay -v VolGroup01
    Using volume group(s) on command line
    Finding volume group "VolGroup01"
  --- Volume group ---
  VG Name               VolGroup01
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               7.99 GB
  PE Size               4.00 MB
  Total PE              2046
  Alloc PE / Size       0 / 0
  Free  PE / Size       2046 / 7.99 GB
  VG UUID               CcYdak-GPlc-mkAd-mStt-3rNE-AtW4-6SaA6h

  --- Physical volumes ---
  PV Name               /dev/hda1
  PV UUID               lqkP61-1Uei-5HlP-Yqd5-eNGi-r0x0-EhWOoN
  PV Status             allocatable
  Total PE / Free PE    1023 / 1023

  PV Name               /dev/hdb1
  PV UUID               ysaCdA-VncS-AH0A-243e-jR2A-D6in-LRtS2C
  PV Status             allocatable
  Total PE / Free PE    1023 / 1023

⑧Logical Volume (論理ボリューム)を作成

[root@storage1 ~]# lvcreate -l 100%VG -n LogVol02 VolGroup01
  Logical volume "LogVol02" created

⑨ファイルシステムを作成

[root@storage1 ~]# mke2fs -j /dev/VolGroup01/LogVol02
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
1048576 inodes, 2095104 blocks
104755 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2147483648
64 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

⑩マウントしてみる

[root@storage1 ~]# mount /dev/VolGroup01/LogVol02 /home
[root@storage1 ~]# cd /home
[root@storage1 home]# ls
lost+found
[root@storage1 home]# mount
/dev/mapper/VolGroup00-LogVol00 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/xvda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/mapper/VolGroup01-LogVol02 on /home type ext3 (rw)
[root@storage1 home]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       1491808   1121568    293236  80% /
/dev/xvda1              101086     12139     83728  13% /boot
tmpfs                   131072         0    131072   0% /dev/shm
/dev/mapper/VolGroup01-LogVol02
                       8248760    149628   7680112   2% /home
[root@storage1 home]#

⑪/etc/fstabに書いて自動的にマウントするようにする

[root@storage1 ~]# vi /etc/fstab

/dev/VolGroup00/LogVol00 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
sysfs                   /sys                    sysfs   defaults        0 0
/dev/VolGroup00/LogVol01 swap                   swap    defaults        0 0
/dev/VolGroup01/LogVol02 /home                  ext3    defaults        0 0