3.mdadm用法
基本语法 : mdadm [mode] <raid-device> [options] <component-devices>
mode  有7种:
  Assemble:将以前定义的某个阵列加入当前在用阵列。
  Build:Build a legacy array ,每个device 没有 superblocks
  Create:创建一个新的阵列,每个device 具有 superblocks
  Manage: 管理阵列,比如 add 或 remove
  Misc:允许单独对阵列中的某个 device 做操作,比如抹去superblocks 或 终止在用的阵列。
  Follow or Monitor:监控 raid 1,4,5,6 和 multipath 的状态
  Grow:改变raid 容量或 阵列中的 device 数目
可用的 [options]:
-A, --assemble:加入一个以前定义的阵列
-B, --build:Build a legacy array without superblocks.
-C, --create:创建一个新的阵列
-Q, --query:查看一个device,判断它为一个 md device 或是 一个 md 阵列的一部分
-D, --detail:打印一个或多个 md device 的详细信息
-E, --examine:打印 device 上的 md superblock 的内容
-F, --follow, --monitor:选择 Monitor 模式
-G, --grow:改变在用阵列的大小或形态
-h, --help:帮助信息,用在以上选项后,则显示该选项信息
--help-options
-V, --version
-v, --verbose:显示细节
-b, --brief:较少的细节。用于 --detail 和 --examine 选项
-f, --force
-c, --config= :指定配置文件,缺省为 /etc/mdadm.conf
-s, --scan:扫描配置文件或 /proc/mdstat以搜寻丢失的信息。配置文件/etc/mdadm.conf
mdadm.conf 格式:
DEVICE  所用到的设备分区列表(在mdadm扫描时,将扫描这些分区)
ARRAY   定义实际的磁盘阵列
简单实例:
DEVICE /dev/sda10 /dev/sda11 /dev/sdb10 /dev/sdb11
ARRAY /dev/md0 devices=/dev/sda11, /dev/sda10
­
4.实战RAID
下面将通过实际操作,创建一个RAID1:
4.1 格式化待做软RAID的分区:
准备两个待做RAID1的分区/dev/sda10, /dev/sdb10:
要求: 大小近乎相等,格式为Linux raid autodetect (即:fd);
# fdisk /dev/sda /dev/sda10      1012   1025     112423   fd  Linux raid autodetect
The number of cylinders for this disk is set to 9964.
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): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (1012-9964, default 1012):
Using default value 1012
Last cylinder or size or sizeM or sizeK (1012-1039, default 1012): 1025
Command (m for help): p
Disk /dev/sda: 81.9 GB, 81964302336 bytes
255 heads, 63 sectors/track, 9964 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot    Start       End    Blocks   Id  System
/dev/sda1         1        9964    80035798    5  Extended
/dev/sda5       917         927       88326    7  HPFS/NTFS
/dev/sda6       928         996      554211    b  Win95 FAT32
/dev/sda7       997        1011      120456    7  HPFS/NTFS
/dev/sda8      1040        9964    71690031   83  Linux
/dev/sda9         1         916     7357707   83  Linux
/dev/sda10     1012        1025      112423   83  Linux
Partition table entries are not in disk order
Command (m for help): t    (更改分区类型) Partition number (1-10): 10  
 
Hex code (type L to list codes): fd  (分区类型为 Linux raid autodetect) Command (m for help): w    (保存退出)
 
# fdisk -l /dev/sda
Disk /dev/sda: 81.9 GB, 81964302336 bytes
255 heads, 63 sectors/track, 9964 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot    Start       End    Blocks   Id  System
/dev/sda1         1        9964    80035798    5  Extended
/dev/sda5         917         927       88326    7  HPFS/NTFS
/dev/sda6         928         996      554211    b  Win95 FAT32
/dev/sda7         997        1011      120456    7  HPFS/NTFS
/dev/sda8        1040        9964    71690031   83  Linux
/dev/sda9          1         916     7357707   83  Linux
 
Partition table entries are not in disk order
 
# fdisk -l /dev/sdb
Disk /dev/sdb: 81.9 GB, 81964302336 bytes
255 heads, 63 sectors/track, 9964 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot    Start       End    Blocks   Id  System
/dev/sdb2      4501        9964    43889580    5  Extended
/dev/sdb3       667        4500    30796605   83  Linux
/dev/sdb4       658         666       72292   83  Linux
/dev/sdb5      5114        5362     2000061   83  Linux
/dev/sdb6      5751        5878     1028128   83  Linux
/dev/sdb7      5721        5750      240943   83  Linux
/dev/sdb8      5620        5720      811251   83  Linux
/dev/sdb9      6261        9964    29752348   83  Linux
/dev/sdb10     4501        4514  112423   fd  Linux raid autodetect
Partition table entries are not in disk order
 
#modprobe raid1.ko mdadm: chunk size defaults to 64K
#mdadm -C /dev/md0 --level=1 --raid-devices=2 /dev/sda10 /dev/sdb10
 
mdadm: /dev/sda10 appears to contain an ext2fs file system
    size=112320K  mtime=Sat Jan  1 06:39:59 2000
mdadm: /dev/sdb10 appears to be part of a raid array:
    size=112320K ctime=Sat Jan  1 02:45:14 2000
Continue creating array? y
mdadm: array /dev/md0 started.
# mkfs.ext3 /dev/md0  (格式化raid)    mke2fs 1.37 (21-Mar-2005)
 
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
28112 inodes, 112320 blocks
5616 blocks (5.00%) reserved for the super user
First data block=1
14 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored . blocks:
        8193, 24577, 40961, 57345, 73729
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
# mdadm -D /dev/md0 (查看raid信息)
/dev/md0:
        Version : 00.90.01
  Creation Time : Sat Jan  1 02:45:14 2000
     Raid Level : raid1
     Array Size : 112320 (109.71 MiB 115.02 MB)
    Device Size : 112320 (109.71 MiB 115.02 MB)
   Raid Devices : 2
  Total Devices : 2
Preferred Minor : 0
    Persistence : Superblock is persistent
    Update Time : Sat Jan  1 05:08:27 2000
          State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 0
  Spare Devices : 0
           UUID : 797b80d8:4b70e2d1:7e427c7e:c9546d1b
         Events : 0.10
    Number   Major   Minor   RaidDevice State
       0       8       10        0      active sync   /dev/sda10
       1       8       26        1      active sync   /dev/sdb10
# cat /proc/mdstat Personalities :
 
md0 : active raid1 sda10[0] sdb10[1]
      112320 blocks [2/2] [UU] raid autodetect
unused devices: <none>
# mount /dev/md0  /share/raid  (挂载raid)
# cp /share/partitionA2 /share/raid
# ls /share/raid
lost found                    asdf.asf
netatalk.tar
# umount /share/raid
# mdadm  /dev/md0 -f /dev/sda10  
# mdadm  /dev/md0 -r /dev/sda10  (将sda10从raid中移除)
# mdadm -D /dev/md0
/dev/md0:
        Version : 00.90.01
  Creation Time : Sat Jan  1 02:45:14 2000
     Raid Level : raid1
     Array Size : 112320 (109.71 MiB 115.02 MB)
    Device Size : 112320 (109.71 MiB 115.02 MB)
   Raid Devices : 2
  Total Devices : 1
Preferred Minor : 0
    Persistence : Superblock is persistent
    Update Time : Sat Jan  1 05:30:28 2000
          State : clean, degraded
Active Devices : 1
Working Devices : 1
Failed Devices : 0
  Spare Devices : 0
           UUID : 797b80d8:4b70e2d1:7e427c7e:c9546d1b
         Events : 0.52
    Number   Major   Minor   RaidDevice State
       0       0        0        0      removed
       1       8       26        1      active sync   /dev/sdb10
# cat /proc/mdstat Personalities : 将sda10的分区类型改为linux (即83),然后重启,重新挂载,查看分区里的文件:
 
md0 : active raid1 sdb10[1]
      112320 blocks [2/1] [_U]
­
unused devices: <none>
 
# fdisk /dev/sda # reboot 和原来作为raid时的内容完全一样,说明raid1 的确是在每个硬盘都写入数据,有较高的可靠性。
t-->11 --> 83 --> w
 
# mount /dev/sda10 /share/partition
# ls /share/partition
lost found                    asdf.asf
netatalk.tar
 
4.3 数据恢复:
下面在sda上新建另一分区,sda11,并把它作为raid1的一个分区,即替代原来的sda10;
# mdadm /dev/md0 -a /dev/sda11
mdadm: hot added /dev/sda11
# mdadm -D /dev/md0
/dev/md0:
        Version : 00.90.01
  Creation Time : Sat Jan  1 02:45:14 2000
     Raid Level : raid1
     Array Size : 112320 (109.71 MiB 115.02 MB)
    Device Size : 112320 (109.71 MiB 115.02 MB)
   Raid Devices : 2
  Total Devices : 3
Preferred Minor : 0
    Persistence : Superblock is persistent
    Update Time : Sat Jan  1 05:50:00 2000
          State : clean
Active Devices : 2
Working Devices : 3
Failed Devices : 0
  Spare Devices : 1
           UUID : 797b80d8:4b70e2d1:7e427c7e:c9546d1b
         Events : 0.111
    Number   Major   Minor   RaidDevice State
       0       8       11        0      active sync   /dev/sda11
       1       8       26        1      active sync   /dev/sdb10
# mount /dev/md0 /share/raid
# ls /share/raid
lost found                    asdf.asf
netatalk.tar
# mdadm -S /dev/md0 (停止raid) (根据mdadm.conf文件启动raid)
# mdadm –As /dev/md0
# mdadm –Ds /etc/mdadm.conf