Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save curipha/03aa9e70e911527d17599f8dd49f78e4 to your computer and use it in GitHub Desktop.
Save curipha/03aa9e70e911527d17599f8dd49f78e4 to your computer and use it in GitHub Desktop.

OSのインストール後にソフトウェアRAIDをセットアップする

1台のHDDにCentOSをインストールしたシステムを,あとからソフトウェアRAIDにする時の作業メモです. LVM+ext3の少し古い環境を想定していますが,LVM+XFSでもだいたい同じはずです.

ポイントは

  • OSインストールしてから,あとでソフトウェアRAID
  • RAIDのレベルはRAID1(ミラーリング)
  • ダウンタイムは1回のリブートのみ
  • SELinuxはずっとEnforcing

あたりです.


物理的にHDDは2台つながっていて,以下のような状態とします.

  1. /dev/sda
  • GRUB: (hd0,0)
  • OSがインストールされ,現在稼働中
  1. /dev/sdb
  • GRUB: (hd1,0)
  • 全く空の状態

/dev/sdaの論理的な状態は以下のとおりです.

  • /dev/sda1
    • /boot
  • /dev/sda2
    • LVMのボリュームグループVolGroup00の論理ボリュームLogVol00
      • /
      • swap

これを/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

では,始めましょう.

前準備

現在の状況を確認します.

# 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/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

/bootパーティションの移行

/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の再生成

最後にinitrdを再生成します. これを忘れると再起動でこけるので,絶対に忘れないようにしましょう.

# mkinitrd -f /boot/initrd-`uname -r`.img `uname -r`

再起動

再起動して,メタデバイスから起動します. ブート時にGRUBの画面が出たら,適当なキーを押してGRUBの画面に入ります. ブートオプションのroot (hd0,0)となっているところをroot (hd1,0)に変更してブートします.

なお,事前に/etc/grub.confをいじるとドツボにはまる可能性があるので,素直にブート画面から変更した方がいいと思います.

/dev/sdaをメタデバイスに追加

現時点でのメタデバイスは/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

RAIDの設定完了

メタデバイスの状態を確認すると

# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment