在 Linux 上创建 Software RAID 10

昨天重装一台老服务器的时候发现 Intel hardware RAID 控制卡有问题,不能识别所有硬盘,但是安装操作系统过程中可以识别所有硬盘,还有一个问题就是操作系统安装正常,但是安装完后无法启动,某种原因导致 BIOS 不能从硬盘启动系统。所以打算把操作系统安装到一个 USB 盘上,然后从 USB 盘启动系统,并给上面的6块硬盘做成 Software RAID 10 后挂载到系统里用。

做 Software RAID 不要求硬盘都一模一样,但是强烈推荐用同一厂商、型号和大小的硬盘。为啥 RAID 10,不选 RAID0, RAID1, RAID5 呢?答:RAID0 太危险,RAID1 性能稍逊一些,RAID5 频繁写情况下性能差,RAID10 似乎是当今磁盘阵列的最佳选择,特别适合做 KVM/Xen/VMware 虚拟机母机(host)的本地存储系统(如果不考虑 SAN 和分布式存储的话)。

这台服务器上有6块完全相同的硬盘,给每块硬盘分成一个区,分区格式为 Linux software raid:

# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-91201, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-91201, default 91201):
Using default value 91201

Command (m for help): p

Disk /dev/sda: 750.2 GB, 750156374016 bytes
255 heads, 63 sectors/track, 91201 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0005c259

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1       91201   732572001   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): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

按照上面的 /dev/sda 的分区例子依次给剩下的5块硬盘 sdc, sdd, sde, sdf, sdg 分区、更改分区格式:

# fdisk /dev/sdc
...
# fdisk /dev/sdd
...
# fdisk /dev/sde
...
# fdisk /dev/sdf
...
# fdisk /dev/sdg
...

分区完成后就可以开始创建 RAID 了,在上面的6个相同大小的分区上创建 raid10:

# mdadm --create /dev/md0 -v --raid-devices=6 --level=raid10 /dev/sda1 /dev/sdc1 /dev/sdd1 /dev/sde1 /dev/sdf1 /dev/sdg1
mdadm: layout defaults to n2
mdadm: layout defaults to n2
mdadm: chunk size defaults to 512K
mdadm: size set to 732440576K
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

查看磁盘阵列的初始化过程(build),根据磁盘大小和速度,整个过程大概需要几个小时:

# watch cat /proc/mdstat
Every 2.0s: cat /proc/mdstat                                       Tue Feb 11 12:51:25 2014

Personalities : [raid10]
md0 : active raid10 sdg1[5] sdf1[4] sde1[3] sdd1[2] sdc1[1] sda1[0]
      2197321728 blocks super 1.2 512K chunks 2 near-copies [6/6] [UUUUUU]
      [>....................]  resync =  0.2% (5826816/2197321728) finish=278.9min speed=13
0948K/sec

unused devices: 

等阵列完成初始化后,就可以给 md0 设备创建分区和文件系统了,有了文件系统就可以挂载到系统里:

# fdisk /dev/md0
# mkfs.ext4 /dev/md0p1

# mkdir /raid10
# mount /dev/md0p1 /raid10

修改 /etc/fstab 文件让每次系统启动时自动挂载:

# vi /etc/fstab
...
/dev/md0p1 /raid10 ext4 noatime,rw 0 0

在上面的 /etc/fstab 文件里使用 /dev/md0p1 设备名不是一个好办法,因为 udev 的缘故,这个设备名常在重启系统后变化,所以最好用 UUID,使用 blkid 命令找到相应分区的 UUID:

# blkid
...
/dev/md0p1: UUID="093e0605-1fa2-4279-99b2-746c70b78f1b" TYPE="ext4"

然后修改相应的 fstab,使用 UUID 挂载:

# vi /etc/fstab
...
#/dev/md0p1 /raid10 ext4 noatime,rw 0 0
UUID=093e0605-1fa2-4279-99b2-746c70b78f1b /raid10 ext4 noatime,rw 0 0

查看 RAID 的情况:

# mdadm --query --detail /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Tue Feb 11 12:50:38 2014
     Raid Level : raid10
     Array Size : 2197321728 (2095.53 GiB 2250.06 GB)
  Used Dev Size : 732440576 (698.51 GiB 750.02 GB)
   Raid Devices : 6
  Total Devices : 6
    Persistence : Superblock is persistent

    Update Time : Tue Feb 11 18:48:10 2014
          State : clean
 Active Devices : 6
Working Devices : 6
 Failed Devices : 0
  Spare Devices : 0

         Layout : near=2
     Chunk Size : 512K

           Name : local:0  (local to host local)
           UUID : e3044b6c:5ab972ea:8e742b70:3f766a11
         Events : 70

    Number   Major   Minor   RaidDevice State
       0       8        1        0      active sync   /dev/sda1
       1       8       33        1      active sync   /dev/sdc1
       2       8       49        2      active sync   /dev/sdd1
       3       8       65        3      active sync   /dev/sde1
       4       8       81        4      active sync   /dev/sdf1
       5       8       97        5      active sync   /dev/sdg1