1台のHDDにCentOSをインストールしたシステムを,あとからソフトウェアRAIDにする時の作業メモです. LVM+ext3の少し古い環境を想定していますが,LVM+XFSでもだいたい同じはずです.
ポイントは
- OSインストールしてから,あとでソフトウェアRAID
- RAIDのレベルはRAID1(ミラーリング)
- ダウンタイムは1回のリブートのみ
- SELinuxはずっとEnforcing
あたりです.
物理的にHDDは2台つながっていて,以下のような状態とします.
/dev/sda
- GRUB:
(hd0,0)
- OSがインストールされ,現在稼働中
/dev/sdb
- GRUB:
(hd1,0)
- 全く空の状態
/dev/sda
の論理的な状態は以下のとおりです.
/dev/sda1
/boot
/dev/sda2
- LVMのボリュームグループ
VolGroup00
の論理ボリュームLogVol00
/
swap
- LVMのボリュームグループ
これを/dev/sda
と/dev/sdb
でミラーリングして作った/dev/md0
と/dev/md1
上で以下のようにする想定です.
/dev/md0
(/dev/sda1
と/dev/sdb1
でミラーリング)/boot
/dev/md1
(/dev/sda2
と/dev/sdb2
でミラーリング)- LVMのボリュームグループ
VolGroup00
の論理ボリュームLogVol00
/
swap
- LVMのボリュームグループ
では,始めましょう.
現在の状況を確認します.
# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
ext3 3.9G 2.3G 1.5G 61% /
/dev/sda1 ext3 99M 20M 75M 21% /boot
tmpfs tmpfs 1006M 0 1006M 0% /dev/shm
# fdisk -l
Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 1044 8281507+ 8e Linux LVM
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
まず,LVMのメッセージを消すおまじないをします. LVMを触るたびに出るメッセージがこれで抑制できます.
# vgscan
Reading all physical volumes. This may take a while...
Found volume group "VolGroup00" using metadata type lvm2
つぎに/dev/sdb
のパーティションレイアウトを変更します.
ポイントは,現在の/dev/sda
と全く同じ状態にすることです.
/dev/sda
の状態は,先ほどのfdisk -l
の出力を確認してください.
# fdisk /dev/sdb
The number of cylinders for this disk is set to 1044.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): p
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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-1044, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-1044, default 1044): 13
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (14-1044, default 14): 14
Last cylinder or +size or +sizeM or +sizeK (14-1044, default 1044): 1044
Command (m for help): p
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 13 104391 83 Linux
/dev/sdb2 14 1044 8281507+ 83 Linux
Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): L
0 Empty 1e Hidden W95 FAT1 80 Old Minix bf Solaris
1 FAT12 24 NEC DOS 81 Minix / old Lin c1 DRDOS/sec (FAT-
( .. snip .. )
16 Hidden FAT16 63 GNU HURD or Sys ab Darwin boot fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux raid autodetect)
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): fd
Changed system type of partition 2 to fd (Linux raid autodetect)
Command (m for help): p
Disk /dev/sdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 13 104391 fd Linux raid autodetect
/dev/sdb2 14 1044 8281507+ fd Linux raid autodetect
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
/dev/md0
と/dev/md1
を使えるようにします.
# mdadm --create /dev/md0 --auto=yes --level=raid1 --raid-devices=2 missing /dev/sdb1
mdadm: array /dev/md0 started.
# mdadm --create /dev/md1 --auto=yes --level=raid1 --raid-devices=2 missing /dev/sdb2
mdadm: array /dev/md1 started.
mdadm.conf
を生成して,mdadm
がメタデバイスを正しく認識するようにします.
# echo 'DEVICE partitions' > /etc/mdadm.conf
# mdadm --detail --scan >> /etc/mdadm.conf
# echo 'MAILADDR root' >> /etc/mdadm.conf
DEVICE partitions
は/proc/partitions
を参照させる指示です.
手で列挙することも可能ですが,/proc/partitions
が正常であればこのように設定した方が先のトラブルを防げます.
MAILADDR root
はRAIDにエラーが発生した場合などに通知を送信するメールアドレスです.
ホスト上のroot
に送ってほしいので@
以下を書いていません.
特定のメールアドレスに送りたい場合はMAILADDR foo@example.jp
のように指定します.
この場合,メールを配送するために,ホスト上でMTAが稼働している必要があります.
ここまででメタデバイスが(縮退状態ですが)利用可能になっています.
# cat /proc/mdstat
Personalities : [raid1]
md1 : active raid1 sdb2[1]
8281408 blocks [2/1] [_U]
md0 : active raid1 sdb1[1]
104320 blocks [2/1] [_U]
unused devices: <none>
[_U]
という表記は,片方のドライブが欠けていることを示しています.
/dev/sdb
上のパーティションしかメタデバイスに参加させていないのでこうなります.
正常な状態であれば[UU]
と表示されます.
今回は/dev/sda
のファイルシステムがext3なので,ext3で作成します.
環境に応じて適切なファイルシステムを選択してください.
# mke2fs -j /dev/md0
# mke2fs -j /dev/md1
/dev/md0
をマウントして,/dev/sda1
にある/boot
を/dev/md0
にコピーします.
# mkdir /mnt/boot
# mount -t ext3 /dev/md0 /mnt/boot/
# cp -a /boot/* /mnt/boot/
# umount /mnt/boot
# umount /boot
# mount -t ext3 /dev/md0 /boot/
ちゃんと/dev/md0
がマウントされて稼働しているか確認します.
# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
ext3 3.9G 2.3G 1.5G 61% /
tmpfs tmpfs 1006M 0 1006M 0% /dev/shm
/dev/md0 ext3 99M 20M 75M 21% /boot
/boot
を/dev/sda1
ではなく/dev/md0
を見るように設定します.
# vi /etc/fstab
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
/dev/md0 /boot ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
LVMの機能を使ってパーティションを/dev/sda2
から/dev/md1
へコピーします.
LVMのおかげでゼロダウンタイムで移行できます.
# pvcreate /dev/md1
Physical volume "/dev/md1" successfully created
# vgextend VolGroup00 /dev/md1
Volume group "VolGroup00" successfully extended
# pvmove /dev/sda2 /dev/md1
/dev/sda2: Moved: 2.0%
/dev/sda2: Moved: 4.8%
/dev/sda2: Moved: 6.7%
( .. snip .. )
/dev/sda2: Moved: 90.9%
/dev/sda2: Moved: 95.6%
/dev/sda2: Moved: 100.0%
# vgreduce VolGroup00 /dev/sda2
Removed "/dev/sda2" from volume group "VolGroup00"
最後にinitrd
を再生成します.
これを忘れると再起動でこけるので,絶対に忘れないようにしましょう.
# mkinitrd -f /boot/initrd-`uname -r`.img `uname -r`
再起動して,メタデバイスから起動します.
ブート時にGRUBの画面が出たら,適当なキーを押してGRUBの画面に入ります.
ブートオプションのroot (hd0,0)
となっているところをroot (hd1,0)
に変更してブートします.
なお,事前に/etc/grub.conf
をいじるとドツボにはまる可能性があるので,素直にブート画面から変更した方がいいと思います.
現時点でのメタデバイスは/dev/sdb
だけの縮退状態なので,/dev/sda
を初期化してメタデバイスに参加させます.
まず/dev/sda2
上の物理ボリュームを消去します.
/
パーティションを移行したときに,ボリュームグループから物理ボリュームを削除したので,ここでは物理ボリュームじたいを削除するだけです.
# pvremove /dev/sda2
Labels on physical volume "/dev/sda2" successfully wiped
/dev/sda
を初期化してパーティションを再設定します.
パーティションテーブルは,この時点の/dev/sdb
と同じにします.
# fdisk /dev/sda
最後にデバイスをホットアドします.
# mdadm /dev/md0 --add /dev/sda1
mdadm: added /dev/sda1
# mdadm /dev/md1 --add /dev/sda2
mdadm: added /dev/sda2
メタデバイスの状態を確認すると
# cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sda1[0] sdb1[1]
104320 blocks [2/2] [UU]
md1 : active raid1 sda2[2] sdb2[1]
8281408 blocks [2/1] [_U]
[>....................] recovery = 3.3% (278528/8281408) finish=13.4min speed=9947K/sec
unused devices: <none>
となっており,/dev/md0
は正常な状態で/dev/md1
は再構築中になっています.
最後に,2台のHDDそれぞれのMBRにGRUBをインストールします. 再構築中にやっても問題ないです.
# grub
Probing devices to guess BIOS drives. This may take a long time.
GNU GRUB version 0.97 (640K lower / 3072K upper memory)
[ Minimal BASH-like line editing is supported. For the first word, TAB
lists possible command completions. Anywhere else TAB lists the possible
completions of a device/filename.]
grub> root (hd1,0)
root (hd1,0)
Filesystem type is ext2fs, partition type 0xfd
grub> setup (hd1)
setup (hd1)
Checking if "/boot/grub/stage1" exists... no
Checking if "/grub/stage1" exists... yes
Checking if "/grub/stage2" exists... yes
Checking if "/grub/e2fs_stage1_5" exists... yes
Running "embed /grub/e2fs_stage1_5 (hd1)"... 15 sectors are embedded.
succeeded
Running "install /grub/stage1 (hd1) (hd1)1+15 p (hd1,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.
grub> root (hd0,0)
root (hd0,0)
Filesystem type is ext2fs, partition type 0xfd
grub> setup (hd0)
setup (hd0)
Checking if "/boot/grub/stage1" exists... no
Checking if "/grub/stage1" exists... yes
Checking if "/grub/stage2" exists... yes
Checking if "/grub/e2fs_stage1_5" exists... yes
Running "embed /grub/e2fs_stage1_5 (hd0)"... 15 sectors are embedded.
succeeded
Running "install /grub/stage1 (hd0) (hd0)1+15 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.
grub> quit
quit
両方のHDDにブートローダをインストールしておかないと,片方のHDDが死んだときに別のHDDからブートできなくなります. 忘れずにインストールしておく必要があります.
しばらく待ってRAIDの再構築が完了したらおしまいです. どうなってるか確認しておくと
# cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sda1[0] sdb1[1]
104320 blocks [2/2] [UU]
md1 : active raid1 sda2[0] sdb2[1]
8281408 blocks [2/2] [UU]
unused devices: <none>
もしmdadm
が立ち上がっていない場合は,立ち上げておきましょう.
# service mdmonitor start
# chkconfig mdmonitor on