mdadm repo
- - https://mirrors.edge.kernel.org/pub/linux/utils/raid/mdadm/ANNOUNCE
raid基础知识:https://zhuanlan.zhihu.com/p/667925807
official user guid: https://raid.wiki.kernel.org/index.php/A_guide_to_mdadm#Manage
创建raid
mdadm --verbose --create /dev/md0 --raid-devices 2 --level 1 /dev/sdb1 /dev/sdc1 --bitmap internal --name raid1
可以使用metadata参数指定元数据版本 --metadata 0.9
。
raid创建好了之后会触发数据同步。
root@raspberrypi:/home/curtis# cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sdc1[1] sdb1[0]
1048512 blocks [2/2] [UU]
[===>.................] resync = 16.6% (174848/1048512) finish=0.7min speed=19427K/sec
bitmap: 1/1 pages [4KB], 65536KB chunk
unused devices: <none>
创建软raid时不进行数据同步
mdadm --verbose --create /dev/md0 --raid-devices 2 --level 1 /dev/sdb1 /dev/sdc1 --bitmap internal --name raid1 --assume-clean
简要打印raid信息
仅打印ARRAY的基本信息。
[root@192 home]# mdadm --examine --brief --scan
ARRAY /dev/md127 UUID=bc970440:d9e8f2f7:fe2970c7:3724fbef
组装
根据提供磁盘设备进行组装:
mdadm --assemble /dev/md0 /dev/sda1 /dev/sdb1
-
如果将单成员盘设定fail状态,是否可以重新扫回raid组?
mdadm --manage /dev/md* --fail /dev/sd* mdadm --stop /dev/md* mdadm --assemble --scan
-
无法将成员盘添加进入raid组,因为该成员盘的event计数小于recent磁盘计数。
-
如果将单成员盘设定为fail并从RAID组移除,是否可以重新扫回raid组?- - 不行
自动扫描并组装
mdadm --assemble --scan
用于在系统启动阶段组装并运行所有阵列(如果出现问题,通常情况下会得到一个部分组装的阵列,也就是raid处于降级状态)。
增量组装raid
Incremental Assembly
Add a single device to an appropriate array. If the addition of the device makes the array runnable, the array will be started. This provides a convenient interface to a hot-plug system. As each device is detected, mdadm has a chance to include it in some array as appropriate. Optionally(可选), when the --fail flag is passed in we will remove the device from any active array instead of adding it.
If a CONTAINER is passed to mdadm in this mode, then any arrays within that container will be assembled and started.
root@raspberrypi:/home/curtis# mdadm --incremental --help
Usage: mdadm --incremental [-Rqrsf] device
This usage allows for incremental(增量) assembly of md arrays. Devices can be
added one at a time as they are discovered. Once an array has all expected
devices, it will be started.
Optionally, the process can be reversed(逆转) by using the fail option.
When fail mode is invoked(援引), mdadm will see if the device belongs to an array
and then both fail (if needed) and remove the device from that array.
Options that are valid with incremental assembly (-I --incremental) are:
--run -R : Run arrays as soon as a minimal number of devices are
: present rather than waiting for all expected.
--quiet -q : Don't print any information messages, just errors.
--rebuild-map -r : Rebuild the 'map' file that mdadm uses for tracking
: partial(部分) arrays.
--scan -s : Use with -R to start any arrays that have the minimal
: required number of devices, but are not yet started.
--fail -f : First fail (if needed) and then remove device from
: any array that it is a member of.
从现象来看下mdadm命令之后,会在后台起一个后台监控进程?
root@raspberrypi:/home/curtis# ps aux | grep mdadm
root 52986 0.0 0.0 2756 2028 ? Ss 01:33 0:00 /sbin/mdadm --monitor --scan
root 54179 0.0 0.0 5912 740 pts/0 S+ 01:37 0:00 grep mdadm
当磁盘被添加进入系统时自动组装raid设备。
[ 9589.362473] block device autoloading is deprecated and will be removed.
[ 9589.410402] md/raid1:md127: active with 1 out of 2 mirrors
[ 9589.436613] md127: detected capacity change from 0 to 2093056
被自动添加的raid设备信息:
root@raspberrypi:/home/curtis# cat /proc/mdstat
Personalities : [raid1]
md127 : active (auto-read-only) raid1 sdc1[1]
1046528 blocks super 1.2 [2/1] [_U]
bitmap: 0/1 pages [0KB], 65536KB chunk
unused devices: <none>
在raid降级的场景下组装的raid设备是只读的? - - 在非降级场景热插入磁盘也是auto-read-only。
降级状态设定为auto-read-only可以理解为:
为了确保数据的一致性,假设raid设备在自动探测添加之前已经发生了数据不一致的问题,如果将降级的raid设备设定为可写状态,当写IO刚好访问了数据不一致的扇区,将会导致数据损坏。
双盘状态下也被设定为auto-read-only?需要怎么理解?
有没有办法将raid设备强制设定为可写?
root@raspberrypi:/home/curtis# mdadm --misc --help
Usage: mdadm misc_option devices...
This usage is for performing some task on one or more devices, which
may be arrays or components, depending on the task.
The --misc option is not needed (though it is allowed) and is assumed
if the first argument in a misc option.
Options that are valid with the miscellaneous mode are:
--query -Q : Display general information about how a
device relates to the md driver
--detail -D : Display details of an array
--detail-platform : Display hardware/firmware details
--examine -E : Examine superblock on an array component
--examine-bitmap -X: Display contents of a bitmap file
--examine-badblocks: Display list of known bad blocks on device
--zero-superblock : erase the MD superblock from a device.
--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
--test -t : exit status 0 if ok, 1 if degrade, 2 if dead, 4 if missing
--wait -W : wait for resync/rebuild/recovery to finish
--action= : initiate or abort ('idle' or 'frozen') a 'check' or 'repair'.
设定为可读可写的命令:
root@raspberrypi:/home/curtis# mdadm --readwrite /dev/md127
root@raspberrypi:/home/curtis# cat /proc/mdstat
Personalities : [raid1]
md127 : active raid1 sdc1[1]
1046528 blocks super 1.2 [2/1] [_U]
bitmap: 0/1 pages [0KB], 65536KB chunk
unused devices: <none>
触发数据同步
触发数据同步:
mdadm --action repair /dev/md0
支持的action类型mdadm: action must be one of idle, frozen, check, repair
root@raspberrypi:/home/curtis#
root@raspberrypi:/home/curtis# cat /proc/mdstat
Personalities : [raid1]
md0 : active raid1 sdc1[1] sdb1[0]
1048512 blocks [2/2] [UU]
[=>...................] resync = 6.9% (73152/1048512) finish=0.8min speed=18288K/sec
bitmap: 0/1 pages [0KB], 65536KB chunk
unused devices: <none>
停止raid阵列
mdadm --stop /dev/md0
阵列stop之后,对应的/dev设备节点被删除。
root@raspberrypi:/home/curtis# cat /proc/mdstat
Personalities : [raid1]
unused devices: <none>
root@raspberrypi:/home/curtis# ll /dev/md0
ls: cannot access '/dev/md0': No such file or directory
清除raid设备信息
mdadm stop掉阵列之后,因为raid信息已经实现了持久化,需要将磁盘中的元数据清除。
mdadm --zero-superblock /dev/sdb1 /dev/sdc1
root@raspberrypi:/home/curtis# mdadm --examine /dev/sdb1
mdadm: No md superblock detected on /dev/sdb1.
root@raspberrypi:/home/curtis# mdadm --examine /dev/sdc1
mdadm: No md superblock detected on /dev/sdc1.
查看成员盘raid信息
root@raspberrypi:/home/curtis# mdadm --examine /dev/sdc1
/dev/sdc1:
Magic : a92b4efc
Version : 1.2
Feature Map : 0x1
Array UUID : 6b2764f0:f77467c3:9a95210b:36ee43a5
Name : raspberrypi:raid1 (local to host raspberrypi)
Creation Time : Sun Mar 10 00:54:19 2024
Raid Level : raid1
Raid Devices : 2
Avail Dev Size : 2093056 (1022.00 MiB 1071.64 MB)
Array Size : 1046528 (1022.00 MiB 1071.64 MB)
Data Offset : 4096 sectors
Super Offset : 8 sectors
Unused Space : before=4016 sectors, after=0 sectors
State : active
Device UUID : 8d7ae3a6:5d4753a0:9277ebb6:6612097b
Internal Bitmap : 8 sectors from superblock
Update Time : Sun Mar 10 00:54:34 2024
Bad Block Log : 512 entries available at offset 16 sectors
Checksum : d46b0cb9 - correct
Events : 7
Device Role : Active device 1
Array State : AA ('A' == active, '.' == missing, 'R' == replacing)
Name字段其实就是homeset:name,这两个字段都是可以在创建raid时指定的。
查看成员盘bitmap信息
root@raspberrypi:/home/curtis# mdadm --examine-bitmap /dev/sdc1
Filename : /dev/sdc1
Magic : 6d746962
Version : 4
UUID : 6b2764f0:f77467c3:9a95210b:36ee43a5
Events : 25
Events Cleared : 0
State : OK
Chunksize : 64 MB
Daemon : 5s flush period
Write Mode : Normal
Sync Size : 1046528 (1022.00 MiB 1071.64 MB)
Bitmap : 16 bits (chunks), 0 dirty (0.0%)
bitmap的大小可以由管理的磁盘大小除Chunksize得到。
查看raid设备信息
root@raspberrypi:/home/curtis# mdadm --detail /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Sun Mar 10 00:54:19 2024
Raid Level : raid1
Array Size : 1046528 (1022.00 MiB 1071.64 MB)
Used Dev Size : 1046528 (1022.00 MiB 1071.64 MB)
Raid Devices : 2
Total Devices : 2
Persistence : Superblock is persistent
Intent Bitmap : Internal
Update Time : Sun Mar 10 00:55:18 2024
State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0
Consistency Policy : bitmap
Name : raspberrypi:raid1 (local to host raspberrypi)
UUID : 6b2764f0:f77467c3:9a95210b:36ee43a5
Events : 25
Number Major Minor RaidDevice State
0 8 17 0 active sync /dev/sdb1
1 8 33 1 active sync /dev/sdc1
软raid管理命令
If a device is given before any options, or if the first option is one of --add, --re-add, --add-spare, --fail, --remove, or --replace, then the MANAGE mode is assumed. Anything other than these will cause the Misc mode to be assumed.
replace命令实际上就是修改了磁盘状态标志位
root@raspberrypi:/home/curtis# mdadm --manage /dev/md0 --replace /dev/sda1
mdadm: Marked /dev/sda1 (device 1 in /dev/md0) for replacement
root@raspberrypi:/home/curtis# cat /sys/block/md0/md/dev-sdb1/state
in_sync,want_replacement