2014年9月19日星期五

What is systemd

systemd System and Service Manager

What is this?

systemd is a system and service manager for Linux, compatible with SysV and LSB init scripts. systemd provides aggressive parallelization capabilities, uses socket and D-Bus activation for starting services, offers on-demand starting of daemons, keeps track of processes using Linux control groups, supports snapshotting and restoring of the system state, maintains mount and automount points and implements an elaborate transactional dependency-based service control logic. It can work as a drop-in replacement for sysvinit. See Lennart's blog story for a longer introduction, and the three status updates since then. Also see the Wikipedia article. If you are wondering whether systemd is for you, please have a look at this comparison of init systems by one of the creators of systemd.

License

This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

Spelling

Yes, it is written systemd, not system D or System D, or even SystemD. And it isn't system d either. Why? Because it's a system daemon, and under Unix/Linux those are in lower case, and get suffixed with a lower case d. And since systemd manages the system, it's called systemd. It's that simple. But then again, if all that appears too simple to you, call it (but never spell it!) System Five Hundred since D is the roman numeral for 500 (this also clarifies the relation to System V, right?). The only situation where we find it OK to use an uppercase letter in the name (but don't like it either) is if you start a sentence withsystemd. On high holidays you may also spell it sÿstëmd. But then again, Système D is not an acceptable spelling and something completely different (though kinda fitting).

2014年9月18日星期四

linux下测试磁盘的读写IO速度

有时候我们在做维护的时候,总会遇到类似于IO特别高,但不能判定是IO瓶颈还是软件参数设置不当导致热盘的问题.这时候通常希望能知道磁盘的读写速度,来进行下一步的决策.
下面是两种测试方法:
(1)使用hdparm命令这是一个是用来获取ATA/IDE硬盘的参数的命令,是由早期Linux IDE驱动的开发和维护人员 Mark Lord开发编写的( hdparm has been written by Mark Lord <mlord@pobox.com>, the primary developer and maintainer of the (E)IDE driver for Linux, with suggestions from many netfolk).该命令应该也是仅用于Linux系统,对于UNIX系统,ATA/IDE硬盘用的可能比较少,一般大型的系统都是使用磁盘阵列的.

使用方法很简单
# hdparm -Tt /dev/sda
/dev/sda:
Timing cached reads: 6676 MB in 2.00 seconds = 3340.18 MB/sec
Timing buffered disk reads: 218 MB in 3.11 seconds = 70.11 MB/sec

可以看到,2秒钟读取了6676MB的缓存,约合3340.18 MB/sec;
在3.11秒中读取了218MB磁盘(物理读),读取速度约合70.11 MB/sec
(2)使用dd命令这不是一个专业的测试工具,不过如果对于测试结果的要求不是很苛刻的话,平时可以使用来对磁盘的读写速度作一个简单的评估.
另外由于这是一个免费软件,基本上×NIX系统上都有安装,对于Oracle裸设备的复制迁移,dd工具一般都是首选.

在使用前首先了解两个特殊设备
/dev/null 伪设备,回收站.写该文件不会产生IO
/dev/zero 伪设备,会产生空字符流,对它不会产生IO

测试方法:
a.测试磁盘的IO写速度# time dd if=/dev/zero of=/test.dbf bs=8k count=300000
300000+0 records in
300000+0 records out
10.59s real 0.43s user 9.40s system
# du -sm /test.dbf
2347 /test.dbf
可以看到,在10.59秒的时间里,生成2347M的一个文件,IO写的速度约为221.6MB/sec;
当然这个速度可以多测试几遍取一个平均值,符合概率统计.
b.测试磁盘的IO读速度# df -m
Filesystem 1M-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
19214 9545 8693 53% /
/dev/sda1 99 13 82 14% /boot
none 506 0 506 0% /dev/shm
# time dd if=/dev/mapper/VolGroup00-LogVol00 of=/dev/null bs=8k
2498560+0 records in
2498560+0 records out
247.99s real 1.92s user 48.64s system

上面的试验在247.99秒的时间里读取了19214MB的文件,计算下来平均速度为77.48MB/sec
c.测试IO同时读和写的速度
# time dd if=/dev/sda1 of=test.dbf bs=8k
13048+1 records in
13048+1 records out
3.73s real 0.04s user 2.39s system
# du -sm test.dbf
103 test.dbf
上面测试的数据量比较小,仅作为参考.

相比两种方法:
前者是linux上专业的测试IDE/ATA磁盘的工具,但是使用范围有局限性;(此试验仅仅使用了测试磁盘IO的参数,对于其他参数及解释参考man手册)
后者可以通用,但不够专业,也没有考虑到缓存和物理读的区分,测试的数据也是仅作参考,不能算是权威.

mdadm使用详解

★mdadm简介
我们可以使用man mdadm命令来查看mdadm的帮助信息:
[root@localhost mdadm-2.6.2]# man mdadm
☆mdadm用法 
基本语法
mdadm [mode] <raid-device> [options] <component-devices>
目前支持
LINEAR, RAID0(striping), RAID1(mirroring), RAID4, RAID5, RAID6, RAID10, MULTIPATH和FAULTY

模式(7种):
  • Assemble:加入一个以前定义的阵列
  • Build:创建一个没有超级块的阵列
  • Create:创建一个新的阵列,每个设备具有超级块
  • Manage: 管理阵列(如添加和删除)
  • Misc:允许单独对阵列中的某个设备进行操作(如停止阵列)
  • Follow or Monitor:监控RAID的状态
  • Grow:改变RAID的容量或阵列中的设备数目

选项: 
-A, --assemble:加入一个以前定义的阵列 
-B, --build:创建一个没有超级块的阵列(Build a legacy array without superblocks.) 
-C, --create:创建一个新的阵列 
-F, --follow, --monitor:选择监控(Monitor)模式 
-G, --grow:改变激活阵列的大小或形态 
-I, --incremental:添加一个单独的设备到合适的阵列,并可能启动阵列 
--auto-detect:请求内核启动任何自动检测到的阵列 
-h, --help:帮助信息,用在以上选项后,则显示该选项信息 
--help-options:显示更详细的帮助 
-V, --version:打印mdadm的版本信息 
-v, --verbose:显示细节 
-b, --brief:较少的细节。用于 --detail 和 --examine 选项 
-Q, --query:查看一个device,判断它为一个 md device 或是 一个 md 阵列的一部分 
-D, --detail:打印一个或多个 md device 的详细信息 
-E, --examine:打印 device 上的 md superblock 的内容 
-c, --config= :指定配置文件,缺省为 /etc/mdadm.conf 
-s, --scan:扫描配置文件或 /proc/mdstat以搜寻丢失的信息。配置文件/etc/mdadm.conf 
★使用mdadm创建RAID5
Create (mdadm --create)模式用来创建一个新的阵列。 在这里我们首先使用mdadm --create --help查看一下帮助:
[root@localhost mdadm-2.6.2]# mdadm --create --help 
Usage:  mdadm --create device -chunk=X --level=Y --raid-devices=Z devices 
This usage will initialise a new md array, associate some 
devices with it, and activate the array.   In order to create an 
array with some devices missing, use the special word 'missing' in 
place of the relevant device name. 
Before devices are added, they are checked to see if they already contain 
raid superblocks or filesystems.  They are also checked to see if 
the variance in device size exceeds 1%. 
If any discrepancy is found, the user will be prompted for confirmation 
before the array is created.  The presence of a '--run' can override this 
caution. 
If the --size option is given then only that many kilobytes of each 
device is used, no matter how big each device is. 
If no --size is given, the apparent size of the smallest drive given 
is used for raid level 1 and greater, and the full device is used for 
other levels. 
Options that are valid with --create (-C) are: 
--bitmap=          : Create a bitmap for the array with the given filename 
--chunk=      -c   : chunk size of kibibytes 
--rounding=        : rounding factor for linear array (==chunk size) 
--level=      -l   : raid level: 0,1,4,5,6,linear,multipath and synonyms 
--parity=     -p   : raid5/6 parity algorithm: {left,right}-{,a}symmetric 
--layout=          : same as --parity 
--raid-devices= -n : number of active devices in array 
--spare-devices= -x: number of spares (eXtras) devices in initial array 
--size=       -z   : Size (in K) of each drive in RAID1/4/5/6/10 - optional 
--force       -f   : Honour devices as listed on command line.  Don't 
                    : insert a missing drive for RAID5. 
--run         -R   : insist of running the array even if not all 
                    : devices are present or some look odd. 
--readonly    -o   : start the array readonly - not supported yet. 
--name=       -N   : Textual name for array - max 32 characters 
--bitmap-chunk=    : bitmap chunksize in Kilobytes. 
--delay=      -d   : bitmap update delay in seconds.
接下来我们使用mdadm创建在/dev/md0上创建一个由sdb、sdc、sdd3块盘组成(另外1块盘sde为热备)的RAID5:
[root@localhost mdadm-2.6.2]# mdadm --create --verbose /dev/md0 --level=raid5 --raid-devices=3 /dev/sdb /dev/sdc /dev/sdd --spare-devices=1 /dev/sde 
mdadm: layout defaults to left-symmetric 
mdadm: chunk size defaults to 64K 
mdadm: size set to 8388544K 
mdadm: array /dev/md0 started.
每个mdadm的选项都有一个缩写的形式,例如,上面我们创建RAID 5的命令可以使用下列的缩写形式:
[root@localhost mdadm-2.6.2]# mdadm -Cv /dev/md0 -l5 -n3 /dev/sdb /dev/sdc /dev/sdd -x1 /dev/sde
二者的效果是相同的。
★查看RAID状态
接下来我们使用cat /proc/mdstat命令来查看一下RAID的状态,我们也可以利用watch命令来每隔一段时间刷新/proc/mdstat的输出。使用CTRL+C可以取消。
[root@localhost mdadm-2.6.2]# watch -n 10 'cat /proc/mdstat' 
Every 10s: cat /proc/mdstat                             Thu May 24 11:53:46 2007 
Personalities : [raid5] 
read_ahead 1024 sectors 
md0 : active raid5 sdd[4] sde[3] sdc[1] sdb[0] 
     16777088 blocks level 5, 64k chunk, algorithm 2 [3/2] [UU_] 
     [====&gt;................]  recovery = 24.0% (2016364/8388544) finish=10.2min 
speed=10324K/sec 
unused devices: <none> 
[root@localhost mdadm-2.6.2]#
接下来我们为阵列创建文件系统:
[root@localhost mdadm-2.6.2]# mkfs.ext3 /dev/md0 
mke2fs 1.34 (25-Jul-2003) 
Filesystem label= 
OS type: Linux 
Block size=4096 (log=2) 
Fragment size=4096 (log=2) 
2097152 inodes, 4194272 blocks 
209713 blocks (5.00%) reserved for the super user 
First data block=0 
128 block groups 
32768 blocks per group, 32768 fragments per group 
16384 inodes per group 
Superblock backups stored on blocks: 
       32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
       4096000 
Writing inode tables: done 
Creating journal (8192 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. 
You have new mail in /var/spool/mail/root
我们尝试向RAID中写入一个test2文件:
[root@localhost eric4ever]# vi test2 
copy succeed! 
eric@tlf 
[url]http://eric4ever.googlepages.com/[/url] 
done! 
[root@localhost eric4ever]# ls 
LATEST.tgz  mdadm-2.6.2  test2 
[root@localhost eric4ever]# mount /dev/md0 /mnt/md0 
[root@localhost eric4ever]# df -lh 
Filesystem            Size  Used Avail Use% Mounted on 
/dev/sda1             2.9G  1.8G  1.1G  63% / 
/dev/sda3             4.6G   33M  4.3G   1% /opt 
none                  125M     0  125M   0% /dev/shm 
/dev/md0               16G   33M   15G   1% /mnt/md0 
[root@localhost eric4ever]# ls /mnt/md0 
lost+found 
[root@localhost eric4ever]# cp ./test2 /mnt/md0 
[root@localhost eric4ever]# ls /mnt/md0 
lost+found  test2 
[root@localhost eric4ever]# ls -lh /mnt/md0 
total 20K 
drwx------    2 root     root          16K May 24 11:55 lost+found 
-rw-r--r--    1 root     root           63 May 24 11:56 test2
使用mdadm --detail /dev/md0(或mdadm -D /dev/md0)命令以及cat /proc/mdstat命令可以查看RAID设备的状态:
[root@localhost eric4ever]# mdadm -D /dev/md0  (或mdadm --detail /dev/md0) 
/dev/md0: 
       Version : 00.90.00 
Creation Time : Thu May 24 13:45:35 2007 
    Raid Level : raid5 
    Array Size : 16777088 (16.00 GiB 17.18 GB) 
Used Dev Size : 8388544 (8.00 GiB 8.59 GB) 
  Raid Devices : 3 
Total Devices : 5 
Preferred Minor : 0 
   Persistence : Superblock is persistent 
   Update Time : Thu May 24 13:45:36 2007 
         State : active, degraded, recovering 
Active Devices : 2 
Working Devices : 4 
Failed Devices : 1 
Spare Devices : 2 
        Layout : left-symmetric 
    Chunk Size : 64K 
Rebuild Status : 16% complete 
          UUID : 4b15050e:7d0c477d:98ed7d00:0f3c29e4 
        Events : 0.2 
   Number   Major   Minor   RaidDevice State 
      0       8       16        0      active sync   /dev/sdb 
      1       8       32        1      active sync   /dev/sdc 
      2       0        0        2      removed 
      3       8       64        3      spare   /dev/sde 
      4       8       48        4      spare   /dev/sdd
通过mdadm -D命令,我们可以查看RAID的版本、创建的时间、RAID级别、阵列容量、可用空间、设备数量、超级块、更新时间、各个设备的状态、RAID算法以及块大小等信息,通过上面的信息我们可以看到目前RAID正处于重建过程之中,进度为16%,其中/dev/sdb和/dev/sdc两块盘已经同步。使用watch命令每个30秒刷新一下查看的进度:
[root@localhost eric4ever]# watch -n 30 'cat /proc/mdstat' 
Every 30s: cat /proc/mdstat                             Thu May 24 13:55:56 2007 
Personalities : [raid5] 
read_ahead 1024 sectors 
md0 : active raid5 sdd[4] sde[3] sdc[1] sdb[0] 
     16777088 blocks level 5, 64k chunk, algorithm 2 [3/2] [UU_] 
     [==============&gt;......]  recovery = 72.3% (6067444/8388544) finish=3.7min 
speed=10324K/sec 
unused devices: <none>
当进度进行到100%时,显示如下:
Every 30s: cat /proc/mdstat                             Thu May 24 14:00:57 2007 
Personalities : [raid5] 
read_ahead 1024 sectors 
md0 : active raid5 sdd[2] sde[3] sdc[1] sdb[0] 
     16777088 blocks level 5, 64k chunk, algorithm 2 [3/3] [UUU] 
unused devices: <none>
这是我们再使用mdadm -D命令查看一下:
[root@localhost eric4ever]# mdadm -D /dev/md0  (或mdadm --detail /dev/md0) 
/dev/md0: 
       Version : 00.90.00 
Creation Time : Thu May 24 13:45:35 2007 
    Raid Level : raid5 
    Array Size : 16777088 (16.00 GiB 17.18 GB) 
Used Dev Size : 8388544 (8.00 GiB 8.59 GB) 
  Raid Devices : 3 
Total Devices : 5 
Preferred Minor : 0 
   Persistence : Superblock is persistent 
   Update Time : Thu May 24 13:59:48 2007 
         State : active 
Active Devices : 3 
Working Devices : 4 
Failed Devices : 1 
Spare Devices : 1 
        Layout : left-symmetric 
    Chunk Size : 64K 
          UUID : 4b15050e:7d0c477d:98ed7d00:0f3c29e4 
        Events : 0.3 
   Number   Major   Minor   RaidDevice State 
      0       8       16        0      active sync   /dev/sdb 
      1       8       32        1      active sync   /dev/sdc 
      2       8       48        2      active sync   /dev/sdd 
      3       8       64        3      spare   /dev/sde

这时我们可以发现sdb、sdc、sdd三块盘均已经同步。 
一般来说,一个新阵列被创建后我们最好创建一个/etc/mdadm.conf文件。没有该文件在激活阵列时我们就得指定更详细的信息,为方便,我们使用下列命令:
[root@localhost eric4ever]# mdadm --detail --scan 
ARRAY /dev/md0 level=raid5 num-devices=3 spares=1 UUID=4b15050e:7d0c477d:98ed7d00:0f3c29e4 
[root@localhost eric4ever]# mdadm --detail --scan &gt;&gt; /etc/mdadm.conf 
[root@localhost eric4ever]# cat /etc/mdadm.conf 
ARRAY /dev/md0 level=raid5 num-devices=3 spares=1 UUID=4b15050e:7d0c477d:98ed7d00:0f3c29e4
★启动停止RAID
使用--stop或-S命令选项可以停止运行的阵列(注意: 停止前必须先umount):
[root@localhost eric4ever]# umount /mnt/md0 
[root@localhost eric4ever]# mdadm -S /dev/md0  (或mdadm --stop /dev/md0) 
mdadm: stopped /dev/md0

重新启动可以使用:
[root@localhost eric4ever]# mdadm -As /dev/md0 
mdadm: /dev/md0 has been started with 3 drives and 1 spare.
★模拟故障
同raidtools一样,mdadm也可以软件模拟故障,命令选项为--fail或--set-faulty:
[root@localhost eric4ever]# mdadm --set-faulty --help 
Usage: mdadm arraydevice options component devices... 
This usage is for managing the component devices within an array. 
The --manage option is not needed and is assumed if the first argument 
is a device name or a management option. 
The first device listed will be taken to be an md array device, and 
subsequent devices are (potential) components of that array. 
Options that are valid with management mode are: 
--add         -a   : hotadd subsequent devices to the array 
--remove      -r   : remove subsequent devices, which must not be active 
--fail        -f   : mark subsequent devices a faulty 
--set-faulty       : same as --fail 
--run         -R   : start a partially built array 
--stop        -S   : deactivate array, releasing all resources 
--readonly    -o   : mark array as readonly 
--readwrite   -w   : mark array as readwrite 
[root@localhost eric4ever]# mdadm --fail --help 
Usage: mdadm arraydevice options component devices... 
This usage is for managing the component devices within an array. 
The --manage option is not needed and is assumed if the first argument 
is a device name or a management option. 
The first device listed will be taken to be an md array device, and 
subsequent devices are (potential) components of that array. 
Options that are valid with management mode are: 
--add         -a   : hotadd subsequent devices to the array 
--remove      -r   : remove subsequent devices, which must not be active 
--fail        -f   : mark subsequent devices a faulty 
--set-faulty       : same as --fail 
--run         -R   : start a partially built array 
--stop        -S   : deactivate array, releasing all resources 
--readonly    -o   : mark array as readonly 
--readwrite   -w   : mark array as readwrite
接下来我们模拟/dev/sdb故障:
[root@localhost eric4ever]# mdadm --manage --set-faulty /dev/md0 /dev/sdb 
mdadm: set /dev/sdb faulty in /dev/md0
查看一下系统日志,如果你配置了冗余磁盘,可能会显示如下信息:
   kernel: raid5: Disk failure on sdb, disabling device. 
   kernel: md0: resyncing spare disk sde to replace failed disk
检查/proc/mdstat,如果配置的冗余磁盘可用,阵列可能已经开始重建。 
首先我们使用mdadm --detail /dev/md0命令来查看一下RAID的状态:
[root@localhost eric4ever]# mdadm --detail /dev/md0 
/dev/md0: 
       Version : 00.90.00 
Creation Time : Thu May 24 13:45:35 2007 
    Raid Level : raid5 
    Array Size : 16777088 (16.00 GiB 17.18 GB) 
Used Dev Size : 8388544 (8.00 GiB 8.59 GB) 
  Raid Devices : 3 
Total Devices : 5 
Preferred Minor : 0 
   Persistence : Superblock is persistent 
   Update Time : Thu May 24 14:07:55 2007 
         State : active, degraded, recovering 
Active Devices : 2 
Working Devices : 3 
Failed Devices : 2 
Spare Devices : 1 
        Layout : left-symmetric 
    Chunk Size : 64K 
Rebuild Status : 3% complete 
          UUID : 4b15050e:7d0c477d:98ed7d00:0f3c29e4 
        Events : 0.6 
   Number   Major   Minor   RaidDevice State 
      0       8       16        0      faulty spare   /dev/sdb 
      1       8       32        1      active sync   /dev/sdc 
      2       8       48        2      active sync   /dev/sdd 
      3       8       64        3      spare rebuilding   /dev/sde
查看/proc/mdstat:
[root@localhost eric4ever]# cat /proc/mdstat 
Personalities : [raid5] 
read_ahead 1024 sectors 
md0 : active raid5 sdb[4] sde[3] sdd[2] sdc[1] 
     16777088 blocks level 5, 64k chunk, algorithm 2 [3/2] [_UU] 
     [==&gt;..................]  recovery = 10.2% (858824/8388544) finish=12.4min speed=10076K/sec 
unused devices: <none>
再查看一下RAID状态:
[root@localhost eric4ever]# mdadm --detail /dev/md0 
/dev/md0: 
       Version : 00.90.00 
Creation Time : Thu May 24 13:45:35 2007 
    Raid Level : raid5 
    Array Size : 16777088 (16.00 GiB 17.18 GB) 
Used Dev Size : 8388544 (8.00 GiB 8.59 GB) 
  Raid Devices : 3 
Total Devices : 5 
Preferred Minor : 0 
   Persistence : Superblock is persistent 
   Update Time : Thu May 24 14:08:27 2007 
         State : active, degraded, recovering 
Active Devices : 2 
Working Devices : 4 
Failed Devices : 1 
Spare Devices : 2 
        Layout : left-symmetric 
    Chunk Size : 64K 
Rebuild Status : 11% complete 
          UUID : 4b15050e:7d0c477d:98ed7d00:0f3c29e4 
        Events : 0.8 
   Number   Major   Minor   RaidDevice State 
      0       0        0        0      removed 
      1       8       32        1      active sync   /dev/sdc 
      2       8       48        2      active sync   /dev/sdd 
      3       8       64        3      spare   /dev/sde 
      4       8       16        4      spare   /dev/sdb
已经完成到11%了。查看一下日志消息:
[root@localhost eric4ever]# tail /var/log/messages 
May 24 14:08:27 localhost kernel:  --- rd:3 wd:2 fd:1 
May 24 14:08:27 localhost kernel:  disk 0, s:0, o:0, n:0 rd:0 us:0 dev:[dev 00:00] 
May 24 14:08:27 localhost kernel:  disk 1, s:0, o:1, n:1 rd:1 us:1 dev:sdc 
May 24 14:08:27 localhost kernel:  disk 2, s:0, o:1, n:2 rd:2 us:1 dev:sdd 
May 24 14:08:27 localhost kernel: RAID5 conf printout: 
May 24 14:08:27 localhost kernel:  --- rd:3 wd:2 fd:1 
May 24 14:08:27 localhost kernel:  disk 0, s:0, o:0, n:0 rd:0 us:0 dev:[dev 00:00] 
May 24 14:08:27 localhost kernel:  disk 1, s:0, o:1, n:1 rd:1 us:1 dev:sdc 
May 24 14:08:27 localhost kernel:  disk 2, s:0, o:1, n:2 rd:2 us:1 dev:sdd 
May 24 14:08:27 localhost kernel: md: cannot remove active disk sde from md0 ...
使用mdadm -E命令查看一下/dev/sdb的情况:
[root@localhost eric4ever]# mdadm -E /dev/sdb 
/dev/sdb: 
         Magic : a92b4efc 
       Version : 00.90.00 
          UUID : 4b15050e:7d0c477d:98ed7d00:0f3c29e4 
Creation Time : Thu May 24 13:45:35 2007 
    Raid Level : raid5 
Used Dev Size : 8388544 (8.00 GiB 8.59 GB) 
    Array Size : 16777088 (16.00 GiB 17.18 GB) 
  Raid Devices : 3 
Total Devices : 5 
Preferred Minor : 0 
   Update Time : Thu May 24 14:08:27 2007 
         State : active 
Active Devices : 2 
Working Devices : 4 
Failed Devices : 1 
Spare Devices : 2 
      Checksum : a6a19662 - correct 
        Events : 0.8 
        Layout : left-symmetric 
    Chunk Size : 64K 
     Number   Major   Minor   RaidDevice State 
this     4       8       16        4      spare   /dev/sdb 
  0     0       0        0        0      faulty removed 
  1     1       8       32        1      active sync   /dev/sdc 
  2     2       8       48        2      active sync   /dev/sdd 
  3     3       8       64        3      spare   /dev/sde 
  4     4       8       16        4      spare   /dev/sdb
自动修复完成后,我们再查看一下RAID的状态:
[root@localhost eric4ever]# mdadm --detail /dev/md0 
/dev/md0: 
       Version : 00.90.00 
Creation Time : Thu May 24 13:45:35 2007 
    Raid Level : raid5 
    Array Size : 16777088 (16.00 GiB 17.18 GB) 
Used Dev Size : 8388544 (8.00 GiB 8.59 GB) 
  Raid Devices : 3 
Total Devices : 5 
Preferred Minor : 0 
   Persistence : Superblock is persistent 
   Update Time : Thu May 24 14:21:54 2007 
         State : active 
Active Devices : 3 
Working Devices : 4 
Failed Devices : 1 
Spare Devices : 1 
        Layout : left-symmetric 
    Chunk Size : 64K 
          UUID : 4b15050e:7d0c477d:98ed7d00:0f3c29e4 
        Events : 0.9 
   Number   Major   Minor   RaidDevice State 
      0       8       64        0      active sync   /dev/sde 
      1       8       32        1      active sync   /dev/sdc 
      2       8       48        2      active sync   /dev/sdd 
      4       8       16        4      spare   /dev/sdb 
[root@localhost eric4ever]# cat /proc/mdstat 
Personalities : [raid5] 
read_ahead 1024 sectors 
md0 : active raid5 sdb[4] sde[0] sdd[2] sdc[1] 
     16777088 blocks level 5, 64k chunk, algorithm 2 [3/3] [UUU] 
unused devices: <none>
我们可以看到/dev/sde已经替换了/dev/sdb。看看系统的日志消息:
[root@localhost eric4ever]# tail /var/log/messages 
May 24 14:21:54 localhost kernel:  --- rd:3 wd:3 fd:0 
May 24 14:21:54 localhost kernel:  disk 0, s:0, o:1, n:0 rd:0 us:1 dev:sde 
May 24 14:21:54 localhost kernel:  disk 1, s:0, o:1, n:1 rd:1 us:1 dev:sdc 
May 24 14:21:54 localhost kernel:  disk 2, s:0, o:1, n:2 rd:2 us:1 dev:sdd 
May 24 14:21:54 localhost kernel: md: updating md0 RAID superblock on device 
May 24 14:21:54 localhost kernel: md: sdb [events: 00000009]<6>(write) sdb's sb offset: 8388544 
May 24 14:21:54 localhost kernel: md: sde [events: 00000009]<6>(write) sde's sb offset: 8388544 
May 24 14:21:54 localhost kernel: md: sdd [events: 00000009]<6>(write) sdd's sb offset: 8388544 
May 24 14:21:54 localhost kernel: md: sdc [events: 00000009]<6>(write) sdc's sb offset: 8388544 
May 24 14:21:54 localhost kernel: md: recovery thread got woken up ...
recovery thread got woken up ... 
这时我们可以从/dev/md0中移除/dev/sdb设备:
[root@localhost eric4ever]# mdadm /dev/md0 -r /dev/sdb 
mdadm: hot removed /dev/sdb
类似地,我们可以使用下列命令向/dev/md0中添加一个设备:
[root@localhost eric4ever]# mdadm /dev/md0 --add /dev/sdf
★监控RAID
mdadm的监控模式提供一些实用的功能,你可以使用下列命令来监控/dev/md0,delay参数意味着检测的时间间隔,这样紧急事件和严重的错误会及时发送给系统管理员:
[root@localhost eric4ever]# mdadm --monitor --mail=eric4ever@localhost --delay=300 /dev/md0
当使用监控模式时,mdadm不会退出,你可以使用下列命令:
[root@localhost eric4ever]# nohup mdadm --monitor --mail=eric4ever@localhost --delay=300 /dev/md0 & 
[1] 3113 
[root@localhost eric4ever]# nohup: appending output to `nohup.out'

用mdadm管理软件RAID

昨天提到安装程序实现软件RAID代替硬件RAID的方法,今天再进一步谈谈手动创建软RAID和日常维护的方法。
 mdadm使用的也是md驱动,由于其拥有多种模式,而且单一工具,不依赖任何配置文件,是替代raidtools的好工具。目前几乎所有发行版本使用的都是该工具。

一、安装和编译
源码下载:http://www.cse.unsw.edu.au/~neilb/source/mdadm/
编译:
tar xzvf ./mdadm-1.6.0.tgz
cd mdadm-1.6.0
make install

rpm安装:
rpm -ivh mdadm-1.6.0-3.rpm

※源码最新版本是2.5,由于我使用的1.6.0的rpm包,所以还是以该版本为例。

二、模式
 mdadm有6种模式,前两种模式:Create、Assemble用于配置和激活阵列;Manage模式用于操作在活动阵列中的设备;Follow或Monitor模式允许管理员对活动阵列配置事件提醒和动作;Build模式用于对旧阵列使用旧版本的md驱动;还有Grow模式可以扩展阵列;剩下的是Misc模式,它包括对多种内部的任务和没有指定特殊模式的一些操作。

系统平台: 红旗 DC Server for x86
硬件平台: Intel Xeon 3.0G DP、1个系统硬盘,3个组成阵列磁盘


三、部署
1、准备磁盘
 只能使用Sofware RAID格式的磁盘才能组成阵列,所以,首先我们要把做好磁盘格式。正如上面提到的,除了系统盘sda外,我们需要对sdb、sdc、sdd进行操作
a)对sdb进行分区
fdisk /dev/sdb

点击在新窗口中浏览此图片
分区前状态:

点击在新窗口中浏览此图片
n,划分区:

点击在新窗口中浏览此图片
t,修改分区格式为fd:

点击在新窗口中浏览此图片
w,保存:

点击在新窗口中浏览此图片
b)同样的方法,对sdc、sdd进行分区和保存
最后状态如下:
点击在新窗口中浏览此图片

2、创建阵列
 mdadm可以支持LINEAR、RAID0  (striping)、 RAID1(mirroring)、 RAID4、RAID5、RAID6和MULTIPATH的阵列模式。
创建命令格式如下:
mdadm [mode] <raiddevice> [options] <component disks>

其中:
引用
[mode]表示引用的模式
指定软RAID设备号,如/dev/md0
[options]可以有长短两种表示方式
表示组成阵列的磁盘分区,可以用shell的方法简短表示

例如:创建一个RAID 0设备:
mdadm --create --verbose /dev/md0 --level=0 --raid-devices=3 /dev/sdb1 /dev/sdc1 /dev/sdd1

--level表示创建的阵列模式,--raid-devices表示参与阵列的磁盘数量。
点击在新窗口中浏览此图片
也可以这样表达,意思是一样的:
mdadm -Cv /dev/md0 -l0 -n3 /dev/sd{b,c,d}1

还可以增加-c128参数,指定chunk size为128K(默认64K)

3、配置文件
 mdadm不采用/etc/mdadm.conf作为主要配置文件,它可以完全不依赖该文件而不会影响阵列的正常工作。
 该配置文件的主要作用是方便跟踪软RAID的配置。对该配置文件进行配置是有好处的,但不是必须的。推荐对该文件进行配置。
引用
格式:
DEVICE 参与阵列的设备
ARRAY 阵列的描述

 通常可以这样来建立:
echo DEVICE /dev/sd{b,c,d}1 >> /etc/mdadm.conf
mdadm --detail --scan >> /etc/mdadm.conf

 结果如下:
引用
# cat /etc/mdadm.conf
DEVICE /dev/sdb1 /dev/sdc1 /dev/sdd1
ARRAY /dev/md0 level=raid0 num-devices=3 UUID=8ba81579:e20fb0e8:e040da0e:f0b3fec8
  devices=/dev/sdb1,/dev/sdc1,/dev/sdd1


4、格式化阵列
 后续,只要你把/dev/md0作为一个单独的设备来进行操作即可:
mkfs.ext3 /dev/md0
mkdir /mnt/test
mount /dev/md0 /mnt/test

 结果如下:
点击在新窗口中浏览此图片

5、若要开机自动挂载,请加入/etc/fstab中:
引用
/dev/md0                /mnt/tes                auto    defaults        0 0


四、监控和管理
 mdadm可以非常方便的对阵列进行监控和管理的操作,也包括了停止和启动阵列等常用维护。
1、查看
cat /proc/mdstat

可以查看所有使用md驱动的阵列的状态:
点击在新窗口中浏览此图片
mdadm --detail /dev/md0
查看指定阵列的详细信息(-D):
点击在新窗口中浏览此图片
2、停止
mdadm -S /dev/md0

停止指定阵列,并释放磁盘(--stop):
点击在新窗口中浏览此图片
※注意:停止后,原组成阵列的磁盘将处于空闲状态,一旦吾操作这些磁盘,将不能再重启激活原阵列。
3、启动
mdadm -A /dev/md0 /dev/sd{b,c,d}1

启动指定的阵列,也可理解为讲一个新阵列装配到系统中(--assemble):
点击在新窗口中浏览此图片
若你已经在上面配置了/etc/mdadm.conf文件,也可用-s查找:
mdadm -As /dev/md0

点击在新窗口中浏览此图片
4、测试
 如果你没有配置/etc/mdadm.conf文件,而且又忘了某磁盘属于那个阵列,则你可以使用检测:(--examine)
mdadm -E /dev/sdb1

点击在新窗口中浏览此图片
获得UUID后,也可以这样激活阵列:
mdadm -Av /dev/md0 --uuid=8ba81579:e20fb0e8:e040da0e:f0b3fec8 /dev/sd*

可以看到,只要磁盘没有损坏,这样装配是非常方便的:
点击在新窗口中浏览此图片
5、添加及删除磁盘
 mdadm可以在Manage模式下,对运行中的阵列进行添加及删除磁盘。常用于标识failed磁盘,增加spare(冗余)磁盘,以及替换磁盘等。
例如:原来状态是:
点击在新窗口中浏览此图片
则可以使用--fail指定坏磁盘,并--remove走:
mdadm /dev/md0 --fail /dev/sdc1 --remove /dev/sdc1

点击在新窗口中浏览此图片
点击在新窗口中浏览此图片
等待同步完成后,结果:
点击在新窗口中浏览此图片
※需要注意的是,对于某些阵列模式,如RAID0等,是不能用--fail和--remove的。
点击在新窗口中浏览此图片
增加一个新的阵列用磁盘
mdadm /dev/md0 --add /dev/sdc1

点击在新窗口中浏览此图片
※需要注意的是,对于某些阵列模式,如RAID0等,是不能用--add的。
6、监控
 在Follow或Monitor状态下,可以使用mdadm对阵列进行监控,例如当阵列出现问题的时候,发送邮件给管理员;或者当磁盘出现问题的时候进行自动的磁盘替换。
nohup mdadm --monitor --mail=sysadmin --delay=300 /dev/md0 &

上述定义:没300秒监控一次,当阵列出现错误,会发送邮件给sysadmin用户。由于monitor启动后是不会自动退出的,所以需要加上nohup和&,使之持续在后台运行。
 在Follow模式下,是允许共享冗余磁盘的。
 例如,我们有两个阵列:/dev/md0、/dev/md1,而/dev/md0里面有一个spare磁盘。当我们在/etc/mdadm.conf中定义类似:
引用
DEVICE  /dev/sd*
ARRAY   /dev/md0 level=raid1 num-devices=3 spare-group=database
  UUID=410a299e:4cdd535e:169d3df4:48b7144a
ARRAY  /dev/md1 level=raid1 num-device=2 spare-group=database
  UUID=59b6e564:739d4d28:ae0aa308:71147fe7

 也就是定义一个spare-group组。并运行上面的monitor模式命令。这样,当组成/dev/md1的其中一个磁盘出现问题的时候,mdadm会自动从/dev/md0上移走spare磁盘,并加入/dev/md1中,而不需要人工干预。(请注意,能实现这样工作的,只有在该阵列支持冗余的情况下才能实现,如raid1、raid5等。而对于raid0等阵列模式,是无效的)

五、其他
1、增加spare磁盘
 可以通过在创建的时候指定冗余磁盘:
mdadm -Cv /dev/md0 -l1 -n2 -x1 /dev/sd{b,c,d}1

-x(--spare-devices)参数用于指定冗余磁盘的数量,结果:
点击在新窗口中浏览此图片
 另外,对于full的阵列(例如已经有2个磁盘的RAID1),则直接使用-add参数,mdadm会自动把冗余的磁盘作为spare disk。
2、删除阵列
mdadm -S /dev/md0


rm /dev/md0

修改/etc/mdadm.conf、/etc/fstab等配置文件,把相关的地方去掉;
最后,用fdisk对磁盘进行重新分区即可。
3、重建阵列
 我们也可以在没有fdisk的情况下把使用过,但目前没有属于任何阵列的磁盘划分到新阵列中:
点击在新窗口中浏览此图片
确认后即可。

六、附录
mdadm中文man文档
参考资料:
mdadm: A New Tool For Linux Software RAID Management