Linux mdadm命令

Linux mdadm命令:管理Linux软RAID。

Linux mdadm命令 功能描述

使用mdadm命令可以管理Linux软RAID,比如创建、调整、监控RAID。

  • mdadm主要有以下8种使用模式。
  • create:创建一个新的RAID,每个设备都具有元数据(超级块)。
  • build:创建或组合一个没有元数据(超级块)的RAID。
  • assemble:组装以前创建的RAID的组件集成到一个活动RAID。
  • manage:更改一个现有的RAID,比如添加新的备用成员和删除故障设备。
  • misc:报告或修改各种RAID相关设备,比如查询RAID或者设备的状态信息、删除旧的超级块。
  • grow:调整/重新塑造一个活动RAID,比如改变RAID容量或阵列中的设备数目。
  • monitor:监控一个或多个RAID的更改。
  • incremental:添加设备到RAID中,或从RAID中删除设备。

Linux mdadm命令 语法

mdadm [模式] [ RAID设备] [选项] [RAID成员]

命令中各选项的含义如表所示。



mdadm命令选项含义

备注:创建RAID准备工作–创建RAID分区。
在创建RAID设备之前,先要通过fdisk命令来创建RAID分区,创建方法和创建普通的磁盘分区类似,只不过在分区的时候将分区类型ID指定为fd。
使用以下命令创建磁盘分区/dev/sda5、/dev/sda6、/dev/sda7和/dev/sda8,每个磁盘分区大小为5GB,分区类型ID为fd。

[root@rhel ~]# 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
//在此输入n,开始创建分区
Command action
  e  extended
  p  primary partition(1-4)
e
//在此输入e,开始创建扩展分区
Selected partition 4
First cylinder (19856-65270, default 19856):
//直接按回车键,从磁盘剩余起始柱面开始创建扩展分区
Using default value 19856
Last cylinder, +cylinders or +size{K, M, G} (19856-65270, default 65270):
//直接按回车键,则从第19856个柱面到最后一个柱面均为扩展分区
Using default value 65270
Command(m for help): n
First cylinder (19856-65270, default 19856):
//直接按回车键,从磁盘剩余起始柱面开始创建逻辑驱动器
Using default value 19856
Last cylinder, +cylinders or+size{K, M, G}(19856-65270, default 65270): +5G
//在此输入“+5G”,代表创建容量为5GB的逻辑驱动器
Command(m for help): n
First cylinder (20510-65270, default 20510):
Using default value 20510
Last cylinder, +cylinders or+size{K, M, G}(20510-65270, default 65270): +5G
Command(m for help): n
First cylinder (21164-65270, default 21164):
Using default value 21164
Last cylinder, +cylinders or+size{K, M, G}(21164-65270, default 65270): +5G
Command(m for help): n
First cylinder (21818-65270, default 21818):
Using default value 21818
Last cylinder, +cylinders or+size{K, M, G}(21818-65270, default 65270): +5G
Command(m for help): t
//在此输入t,开始转换分区类型
Partition number(1-8):5
//输入分区号码,在此输入5,说明要转换/dev/sda5分区的类型
Hex code(type L to list codes): fd
Changed system type of partition 5 to fd (Linux raid autodetect)
//转换5号分区类型为fd(Linux raid auto)
Command(m for help): t
Partition number(1-8):6
Hex code(type L to list codes): fd
Changed system type of partition 6 to fd (Linux raid autodetect)
Command(m for help): t
Partition number(1-8):7
Hex code(type L to list codes): fd
Changed system type of partition 7 to fd (Linux raid autodetect)
Command(m for help): t
Partition number(1-8):8
Hex code(type L to list codes): fd
Changed system type of partition 8 to fd (Linux raid autodetect)
Command(m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
  分区之后使用以下命令查看磁盘分区情况。
[root@rhel ~]# fdisk -l /dev/sda
Disk /dev/sda: 536.9 GB, 536870912000 bytes
255 heads, 63 sectors/track, 65270 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: 0x00034c36
  Device Boot     Start       End     Blocks  Id  System
/dev/sda1  *         1         13     102400  83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2            13      19595  157286400  83  Linux
/dev/sda3         19595      19856    2097152  82  Linux swap/Solaris
/dev/sda4         19856      65270  364794299   5  Extended
/dev/sda5         19856      20509    5251535  fd  Linux raid autodetect
/dev/sda6         20510      21163    5253223+  fd  Linux raid autodetect
/dev/sda7         21164      21817    5253223+  fd  Linux raid autodetect
/dev/sda8         21818      22471    5253223+  fd  Linux raid autodetect
//在此准备了4个磁盘分区做RAID 5实验
使用以下命令重新启动Linux系统。
[root@rhel ~]# init 6

Linux mdadm命令 示例

创建RAID 5设备,名称为/dev/md0,其成员为/dev/sda5、/dev/sda6和/dev/sda7

[root@rhel ~]# mdadm -C -v /dev/md0-l5-n3 /dev/sda5 /dev/sda6 /dev/sda7
mdadm: layout defaults to left-symmetric
mdadm: layout defaults to left-symmetric
mdadm: chunk size defaults to 512K
mdadm: size set to 5246976K
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
//RAID设备创建完成之后会立即启用

查看RAID设备/dev/md0详细信息

[root@rhel ~]# mdadm -D /dev/md0
/dev/md0:
      Version : 1.2
  Creation Time : Mon Aug 19 01:02:312013
    Raid Level : raid5
    Array Size : 10493952 (10.01 GiB 10.75 GB)
  Used Dev Size : 5246976 (5.00 GiB 5.37 GB)
  Raid Devices : 3
  Total Devices : 3
    Persistence : Superblock is persistent
    Update Time : Mon Aug 19 01:10:332013
        State : clean
Active Devices : 3
Working Devices : 3
Failed Devices : 0
  Spare Devices : 0
        Layout : left-symmetric
    Chunk Size : 512K
          Name: rhel:0  (local to host rhel)
          UUID : e7ed9356:a7d4e1ea:da5465ac:53f3dfe6
        Events : 18
    Number  Major  Minor  RaidDevice State
      0      8       5       0     active sync  /dev/sda5
      1      8       6       1     active sync  /dev/sda6
      3      8       7       2     active sync  /dev/sda7

查看RAID成员/dev/sda5、/dev/sda6和/dev/sda7详细信息

[root@rhel ~]# mdadm -E /dev/sda5 /dev/sda6 /dev/sda7
/dev/sda5:
  Magic: a92b4efc
 Version:1.2
Feature Map:0x0
Array UUID: e7ed9356:a7d4e1ea:da5465ac:53f3dfe6
Name: rhel:0 (local to host rhel)
Creation Time: Mon Aug 19 01:02:312013
Raid Level: raid5
Raid Devices:3
Avail Dev Size:10494878(5.00 GiB 5.37 GB)
Array Size:10493952(10.01 GiB 10.75 GB)
Used Dev Size:10493952(5.00 GiB 5.37 GB)
Data Offset:8192 sectors
Super Offset:8 sectors
  State: clean
Device UUID:57b31675:6484d43c:19a142a9:c40a5e1e
Update Time: Mon Aug 19 01:11:292013
Checksum:9003c6c-correct
  Events:18
  Layout: left-symmetric
Chunk Size:512K
Device Role: Active device 0
Array State: AAA('A'==active, '.'==missing)
/dev/sda6:
  Magic: a92b4efc
 Version:1.2
Feature Map:0x0
Array UUID: e7ed9356:a7d4e1ea:da5465ac:53f3dfe6
  Name: rhel:0 (local to host rhel)
Creation Time: Mon Aug 19 01:02:312013
Raid Level: raid5
Raid Devices:3
Avail Dev Size:10498255(5.01 GiB 5.38 GB)
Array Size:10493952(10.01 GiB 10.75 GB)
Used Dev Size:10493952(5.00 GiB 5.37 GB)
Data Offset:8192 sectors
Super Offset:8 sectors
  State: clean
Device UUID: cf95211c:93e66cd4:be5ab5bb:0021f0a0
Update Time: Mon Aug 19 01:11:292013
Checksum: dca85e26-correct
  Events:18
  Layout: left-symmetric
Chunk Size:512K
Device Role: Active device 1
Array State : AAA ('A' == active, '.' == missing)
/dev/sda7:
    Magic : a92b4efc
  Version : 1.2
 Feature Map : 0x0
 Array UUID : e7ed9356:a7d4e1ea:da5465ac:53f3dfe6
    Name: rhel:0 (local to host rhel)
Creation Time : Mon Aug 19 01:02:312013
 Raid Level : raid5
Raid Devices : 3
 Avail Dev Size:10498255(5.01 GiB 5.38 GB)
 Array Size : 10493952 (10.01 GiB 10.75 GB)
Used Dev Size : 10493952 (5.00 GiB 5.37 GB)
Data Offset : 8192 sectors
Super Offset : 8 sectors
    State : clean
Device UUID : ad9a8e7d:ee42d240:7cbd7057:254ad3a8
Update Time : Mon Aug 19 01:11:292013
  Checksum : 4e194b44- correct
  Events : 18
  Layout : left-symmetric
 Chunk Size : 512K
Device Role : Active device 2
Array State : AAA ('A' == active, '.' == missing)

停用RAID设备/dev/md0

[root@rhel ~]# umount /dev/md0
//先卸载RAID设备文件系统
[root@rhel ~]# mdadm -S /dev/md0
mdadm: stopped /dev/md0

启用RAID设备/dev/md0

[root@rhel ~]# mdadm -A /dev/md0 /dev/sda5 /dev/sda6 /dev/sda7
mdadm: /dev/md0 has been started with 3 drives.

向RAID设备/dev/md0中添加备用成员/dev/sda8

[root@rhel ~]# mdadm /dev/md0-a /dev/sda8
mdadm: added /dev/sda8

将RAID设备/dev/md0中的备用成员转换为正式成员

[root@rhel ~]# mdadm -G /dev/md0-n4
mdadm: Need to backup 3072K of critical section..

将RAID成员/dev/sda7模拟为故障磁盘

[root@rhel ~]# mdadm -f /dev/md0 /dev/sda7
mdadm: set /dev/sda7 faulty in /dev/md0

热移除有故障的RAID成员/dev/sda7

[root@rhel ~]# mdadm -r /dev/md0 /dev/sda7
mdadm: hot removed /dev/sda7 from /dev/md0

删除RAID设备/dev/md0

[root@rhel ~]# umount /dev/md0
//卸载RAID设备文件系统[root@rhel ~]# mdadm -Ss /dev/md0
mdadm: stopped /dev/md0
//停用RAID设备
[root@rhel ~]# mdadm --zero-superblock /dev/sda5 /dev/sda6 /dev/sda7 /dev/sda8
//删除RAID设备

扩展RAID设备容量

[root@rhel ~]# mdadm -C /dev/md0-l5-n3 /dev/sda5 /dev/sda6 /dev/sda7-x1 /dev/sda8-z 4G
mdadm: /dev/sda5 appears to be part of a raid array:
    level=raid5 devices=3 ctime=Sat Dec  7 16:14:272013
mdadm: /dev/sda6 appears to be part of a raid array:
    level=raid5 devices=3 ctime=Sat Dec  7 16:14:272013
mdadm: /dev/sda7 appears to be part of a raid array:
    level=raid5 devices=3 ctime=Sat Dec  7 16:14:272013
mdadm: largest drive (/dev/sda6) exceeds size (4194304K) by more than 1%
Continue creating array? y                    //输入y确认创建RAID设备
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
//创建RAID 5设备,正式成员为/dev/sda5、/dev/sda6和/dev/sda7,备用成员为/dev/sda8,每个RAID成员使用4GB容量(每个RAID成员总容量为5GB)
[root@rhel ~]# mdadm -G /dev/md0-z 5G
mdadm: component size of /dev/md0 has been set to 5242880K
//扩展RAID设备/dev/md0容量,每个RAID成员使用5GB容量
赞(1)
未经允许不得转载:极客笔记 » Linux mdadm命令
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址