ในบทความนี้จะอธิบายการสร้างคอนฟิก Software RAID บนลินุกซ์ เพื่อเป็นการป้องกันข้อมูลเสียหายอันเนื่องมาจากดิสก์ที่ใช้เก็บ (Physical Disk) มีปัญหา
การสร้าง disk partition สำหรับทำ Software RAID
การทำ Software RAID บนลินุกซ์นั้น จะทำบน disk Parition ดังนั้นเราต้องแบ่ง parition ของดิสก์ให้เรียบร้อยก่อน โดยขนาดของแต่ละ partition ที่จะนำมารวมกันนั้น ควรจะมีขนาดเท่ากัน ซึ่งจริงๆ แล้วควรจะเป็นดิสก์ยี่ห้อเดียวกัน รุ่นเดียวกันด้วย เพื่อประสิทธิภาพดีที่สุด
ใช้คำสั่ง fdisk เพื่อแบ่ง parition ตามปกติ แต่ต้องใช้คำสั่ง t เพื่อเปลี่ยน partition id จาก 83 (Linux) ให้เป็น fd (Linux raid autodetect)
ตัวอย่างการสร้าง parition สำหรับทำ Software RAID
[root@test-raid ~]# fdisk /dev/sdb The number of cylinders for this disk is set t 48641. 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: 400.0 GB, 400088457216 bytes 255 heads, 63 sectors/track, 48641 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x0004a7cf 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-48641, default 1): Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-48641, default 48641): +10GB Command (m for help): p Disk /dev/sdb: 400.0 GB, 400088457216 bytes 255 heads, 63 sectors/track, 48641 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x0004a7cf Device Boot Start End Blocks Id System /dev/sdb1 1 1217 9775521 83 Linux Command (m for help): t Selected partition 1 Hex code (type L to list codes): fd Changed system type of partition 1 to fd (Linux raid autodetect) Command (m for help): p Disk /dev/sdb: 400.0 GB, 400088457216 bytes 255 heads, 63 sectors/track, 48641 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x0004a7cf Device Boot Start End Blocks Id System /dev/sdb1 1 1217 9775521 fd Linux raid autodetect Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
จากตัวอย่างเราจะเป็นการสร้าง partition ขนาด 10GB บน /dev/sdb แล้วเปลี่ยน id ให้เป็น fd (Linux raid autodetect) ซึ่งเราต้องทำทุก partition ที่จะมาทำเป็น RAID
การสร้าง RAID-1 (mirrored disk)
RAID-1 จะประกอบด้วยดิสก์สองก้อน (partition) ที่เก็บข้อมูลที่เหมือนกัน เพื่อป้องกันตัวใดตัวหนี่งมีปัญหา ข้อมูลก็ยังอยู่ โดยขนาดของ RAID-1 ที่สร้างได้ จะมีขนาดเท่ากับ partition เดียวเท่านั้น เช่นเรานำ paritition ที่มีขนาด 10GB สองอันมารวมกัน สร้างเป็น RAID-1 ขนาดที่ได้จากการสร้างก็จะเท่ากับ 10GB เท่านั้น
ในตัวอย่างเราจะสร้าง RAID-1 จาก /dev/sdb1 และ /dev/sdc1 โดยเริ่มต้นจากใช้คำสั่ง fdisk เพื่อตรวจสอบขนาดและ id ของแต่ละ parition ก่อนที่จะใช้คำสั่ง mdadm เพื่อสร้าง RAID-1 ขึ้นมา
[root@test-raid ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 400.0 GB, 400088457216 bytes
255 heads, 63 sectors/track, 48641 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x0004a7cf
Device Boot Start End Blocks Id System
/dev/sdb1 1 1217 9775521 fd Linux raid autodetect
[root@test-raid ~]# fdisk -l /dev/sdc
Disk /dev/sdc: 400.0 GB, 400088457216 bytes
255 heads, 63 sectors/track, 48641 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x0008e246
Device Boot Start End Blocks Id System
/dev/sdc1 1 1217 9775521 fd Linux raid autodetect
[root@test-raid ~]# mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1
mdadm: /dev/sdb1 appears to contain an ext2fs file system
size=9775520K mtime=Thu Jan 1 07:00:00 1970
mdadm: /dev/sdc1 appears to contain an ext2fs file system
size=9775520K mtime=Thu Jan 1 07:00:00 1970
mdadm: size set to 9775424K
Continue creating array? y
mdadm: array /dev/md0 started.
หลังจากที่ใช้คำสั่ง mdadm เพื่อสร้าง RAID ที่มีชื่อว่า /dev/md0 แล้ว จะมีการ resync หรือ build ตัว RAID ขึ้นมา (เหมือนกับในกรณี Hardware RAID) ต้องรอจนกว่าจะ build ครบ 100% ก่อนที่จะนำไปใช้งานต่อ ซึ่งสามารถตรวจสอบได้โดยดูใช้คำสั่ง mdadm หรือดูโดยตรงจากไฟล์ /proc/mdstat
ตัวอย่างการตรวจสอบสถานะการ resync ของ RAID
[root@test-raid ~]# mdadm --detail /dev/md0
/dev/md0:
Version : 00.90.03
Creation Time : Sun Aug 3 01:41:25 2008
Raid Level : raid1
Array Size : 9775424 (9.32 GiB 10.01 GB)
Used Dev Size : 9775424 (9.32 GiB 10.01 GB)
Raid Devices : 2
Total Devices : 2
Preferred Minor : 0
Persistence : Superblock is persistent
Update Time : Sun Aug 3 01:41:30 2008
State : active, resyncing
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0
Rebuild Status : 20% complete
UUID : 69a86de1:0240b077:f7ce8152:401caf96
Events : 0.3
Number Major Minor RaidDevice State
0 8 17 0 active sync /dev/sdb1
1 8 33 1 active sync /dev/sdc1
[root@test-raid ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4] [raid1]
md0 : active raid1 sdc1[1] sdb1[0]
9775424 blocks [2/2] [UU]
[====>................] resync = 20.0% (1962304/9775424) finish=2.0min speed=63300K/sec
unused devices: <none>
เวลาที่ใช้ในการ resync หรือ build นั้น ขึ้นอยู่กับขนาดและความเร็วของฮาร์ดดิสก์ เพื่อเป็นการทดสอบและเรียนรู้ แนะนำให้สร้างเป็น partition เล็กๆ ก่อน เพื่อทำความคุ้นเคย ก่อนที่จะนำไปใช้งานจริง
ตัวอย่างการแสดงสถานะเมื่อ resync เรียบร้อยแล้ว
[root@test-raid ~]# mdadm --detail /dev/md0
/dev/md0:
Version : 00.90.03
Creation Time : Sun Aug 3 01:41:25 2008
Raid Level : raid1
Array Size : 9775424 (9.32 GiB 10.01 GB)
Used Dev Size : 9775424 (9.32 GiB 10.01 GB)
Raid Devices : 2
Total Devices : 2
Preferred Minor : 0
Persistence : Superblock is persistent
Update Time : Sun Aug 3 01:43:59 2008
State : active
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0
UUID : 69a86de1:0240b077:f7ce8152:401caf96
Events : 0.7
Number Major Minor RaidDevice State
0 8 17 0 active sync /dev/sdb1
1 8 33 1 active sync /dev/sdc1
[root@test-raid ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4] [raid1]
md0 : active raid1 sdc1[1] sdb1[0]
9775424 blocks [2/2] [UU]
unused devices: <none>
เมื่อ RAID ถูก resync เรียบร้อยแล้ว เราสามารถนำไปใช้งานได้เหมือนกับ disk partition ปกติเลย เช่นนำไป format เพื่อสร้าง filesystem หรือจะนำไปใช้สร้าง Physical Volume เพื่อทำ LVM อีกทีก็ได้
ในที่นี้จะ format เพื่อสร้าง filesystem ขึ้นมาเลย RAID
หมายเหตุ ชื่อ /dev/md0 เป็นชื่อที่ได้จากการระบุตอนใช้คำสั่ง mdadm –create
[root@test-raid ~]# mkfs.ext3 /dev/md0
mke2fs 1.40.8 (13-Mar-2008)
Warning: 256-byte inodes not usable on older systems
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
612000 inodes, 2443856 blocks
122192 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2503999488
75 block groups
32768 blocks per group, 32768 fragments per group
8160 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 34 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@test-raid ~]# mkdir /export
[root@test-raid ~]# mount /dev/md0 /export
[root@test-raid ~]# df -h /export/
Filesystem Size Used Avail Use% Mounted on
/dev/md0 9.2G 150M 8.6G 2% /export
ขนาดที่ได้จากการสร้าง RAID-1 จะเท่ากับ parition เดียวเท่านั้น
การยกเลิกการใช้งาน Software RAID ที่สร้างขึ้นมา
ถ้าไม่ต้องการใช้งาน RAID ที่สร้างขึ้นมา สามารถใช้คำสั่ง mdadm –stop แล้วใช้ fdisk เพื่อลบ partition ที่สร้างขึ้นออกไป
ตัวอย่างการยกเลิก Software RAID ที่สร้างขึ้นมาชื่อ /dev/md0
[root@test-raid ~]# umount /export
[root@test-raid ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4] [raid1]
md0 : active raid1 sdc1[1] sdb1[0]
9775424 blocks [2/2] [UU]
unused devices: <none>
[root@test-raid ~]# mdadm --stop /dev/md0
mdadm: stopped /dev/md0
[root@test-raid ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4] [raid1]
unused devices: <none>
การสร้าง RAID-5 (striped disks with parity)
RAID-5 จะประกอบด้วยดิสก์จำนวน 3 ก้อนขึ้นไป (partition) โดยเวลาเขียนข้อมูลจะเขียนลงไปยังฮาร์ดดิสก์หลายๆ ตัว และจะใช้ดิสก์หนึ่งตัวในการทำ parity เพื่อตรวจสอบ ถ้าฮาร์ดดิสก์ตัวใดตัวหนึ่งมีปัญหาสามารถที่จะถอดออกแล้วเปลี่ยนตัวใหม่ ที่มีขนาดเท่ากันลงไปแทนได้ โดยที่ข้อมูลไม่เสียหายแต่อย่างไร
ขนาดของ RAID-5 ที่ได้จากการสร้าง จะเป็นผลรวมของจำนวนก้อนลบออกหนึ่ง (เพื่อทำ parity) เช่นถ้าเราสร้างจาก paritition ที่มีขนาด 10GB จำนวน 3 ก้อนเข้าด้วยกัน ขนาดของ partition ที่ได้จะมีขนาดเท่ากับ 10*(3-1) = 20 GB
ในตัวอย่างเราจะสร้าง RAID-5 จาก /dev/sdb1, /dev/sdc1 และ /dev/sdd1 ซึ่งตัวอย่างนี้จะถือว่าได้สร้างและเปลี่ยน paritition ให้เป็น fd เรียบร้อยแล้ว เช่นเดียวกับตอนสร้าง RAID-1
ตัวอย่างการใช้คำสั่ง mdadm –create เพื่อสร้าง RAID-5
[root@test-raid ~]# mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sdb1 /dev/sdc1 /dev/sdd1
mdadm: layout defaults to left-symmetric
mdadm: chunk size defaults to 64K
mdadm: /dev/sdb1 appears to contain an ext2fs file system
size=9775520K mtime=Thu Jan 1 07:00:00 1970
mdadm: /dev/sdc1 appears to contain an ext2fs file system
size=9775520K mtime=Thu Jan 1 07:00:00 1970
mdadm: /dev/sdd1 appears to contain an ext2fs file system
size=9775520K mtime=Thu Jan 1 07:00:00 1970
mdadm: size set to 9775424K
Continue creating array? y
mdadm: array /dev/md0 started.
เช่นเดียวกับตอนสร้าง RAID-1 ต้องรอให้ resync ครบ 100% ก่อนที่จะนำไปใช้งานได้
[root@test-raid ~]# mdadm --detail /dev/md0
/dev/md0:
Version : 00.90.03
Creation Time : Sun Aug 3 02:19:19 2008
Raid Level : raid5
Array Size : 19550848 (18.65 GiB 20.02 GB)
Used Dev Size : 9775424 (9.32 GiB 10.01 GB)
Raid Devices : 3
Total Devices : 3
Preferred Minor : 0
Persistence : Superblock is persistent
Update Time : Sun Aug 3 02:23:19 2008
State : clean
Active Devices : 3
Working Devices : 3
Failed Devices : 0
Spare Devices : 0
Layout : left-symmetric
Chunk Size : 64K
UUID : e1e67b3f:6070e1ea:6f0aded5:c30e4ee6
Events : 0.8
Number Major Minor RaidDevice State
0 8 17 0 active sync /dev/sdb1
1 8 33 1 active sync /dev/sdc1
2 8 49 2 active sync /dev/sdd1
[root@test-raid ~]# cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4] [raid1]
md0 : active raid5 sdd1[2] sdc1[1] sdb1[0]
19550848 blocks level 5, 64k chunk, algorithm 2 [3/3] [UUU]
unused devices: <none>
ตัวอย่างการ format เพื่อสร้าง filesystem ของ RAID-5
[root@test-raid ~]# mkfs.ext3 /dev/md0
mke2fs 1.40.8 (13-Mar-2008)
Warning: 256-byte inodes not usable on older systems
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
1224000 inodes, 4887712 blocks
244385 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
150 block groups
32768 blocks per group, 32768 fragments per group
8160 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@test-raid ~]# mount /dev/md0 /export
[root@test-raid ~]# df -h /export/
Filesystem Size Used Avail Use% Mounted on
/dev/md0 19G 173M 18G 1% /export
ข้อมูลอ้างอิง