构建软RAID5磁盘阵列&RAID设备恢复
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了构建软RAID5磁盘阵列&RAID设备恢复相关的知识,希望对你有一定的参考价值。
构建软RAID5磁盘阵列&RAID设备恢复
需求描述
为Linux服务器添加4块SCSI硬盘
使用mdadm软件包,构建RAID5磁盘阵列,提高磁盘存储的性能和可靠性
安装mdadm
准备用于RAID阵列的分区 -- 为Linux服务器添加4块SCSI硬盘,并使用fdisk工具个划分出一块2GB的分区依次为:/dev/sde1、/dev/sdg1、/dev/sdh1、/dev/sdf1 -- 将其类型ID更改为“fd”,对应为“Linux raid autodetect”,表示支持用于RAID磁盘阵列
创建RAID设备
创建热备磁盘
查看mdstat
设定faulty
再次查看mdstat
在RAID设备中建立文件系统
挂载并使用文件系统
设备恢复
查看mdadm.conf examples,根据这个创建mdadm.conf
一、准备用于RAID阵列的分区
现有磁盘信息:
1.sde2分出2G的主分区,并转换成"fd”(Linux raid autodetect ):
(1).创建
[[email protected] jason]# fdisk /dev/sde 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): p Disk /dev/sde: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0xe4b1d138 Device Boot Start End Blocks Id System /dev/sde1 1 262 2104483+ fd Linux raid autodetect Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (263-2610, default 263): Using default value 263 Last cylinder, +cylinders or +size{K,M,G} (263-2610, default 2610): +2G
(2).转换
Command (m for help): t Partition number (1-4): fd Partition number (1-4): 2 Hex code (type L to list codes): fd Changed system type of partition 2 to fd (Linux raid autodetect)
(3).查看
Command (m for help): p Disk /dev/sde: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0xe4b1d138 Device Boot Start End Blocks Id System /dev/sde1 1 262 2104483+ fd Linux raid autodetect /dev/sde2 263 524 2104515 fd Linux raid autodetect
(4).保存并退出
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: Device or resource busy. 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.
2.sdg2分出2G的主分区,并转换成"fd”(Linux raid autodetect ):
(1).创建
[[email protected] jason]# fdisk /dev/sdg 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): 2 First cylinder (263-2610, default 263): Using default value 263 Last cylinder, +cylinders or +size{K,M,G} (263-2610, default 2610): +2G
(2).转换
Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): fd Changed system type of partition 2 to fd (Linux raid autodetect)
(3).查看
Command (m for help): p Disk /dev/sdg: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x37970afa Device Boot Start End Blocks Id System /dev/sdg1 1 262 2104483+ fd Linux raid autodetect /dev/sdg2 263 524 2104515 fd Linux raid autodetect
(4).保存并退出
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: Device or resource busy. 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. [[email protected] jason]#
3.sdh2分出2G的主分区,并转换成"fd”(Linux raid autodetect ):
(1).创建
[[email protected] jason]# fdisk /dev/sdh 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): 2 First cylinder (263-2610, default 263): Using default value 263 Last cylinder, +cylinders or +size{K,M,G} (263-2610, default 2610): +2G
(2).转换
Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): fd Changed system type of partition 2 to fd (Linux raid autodetect)
(3).查看
Command (m for help): p Disk /dev/sdh: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0xdeb9d0d0 Device Boot Start End Blocks Id System /dev/sdh1 1 262 2104483+ fd Linux raid autodetect /dev/sdh2 263 524 2104515 fd Linux raid autodetect
(4).保存并退出
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: Device or resource busy. 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. [[email protected] jason]#
4.sdf2分出2G的主分区,并转换成"fd”(Linux raid autodetect ):
(1).创建
[[email protected] jason]# fdisk /dev/sdf 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): 2 First cylinder (263-2610, default 263): Using default value 263 Last cylinder, +cylinders or +size{K,M,G} (263-2610, default 2610): +2G
(2).转换
Command (m for help): t Partition number (1-4): f Partition number (1-4): 2 Hex code (type L to list codes): fd Changed system type of partition 2 to fd (Linux raid autodetect)
(3).查看
Command (m for help): p Disk /dev/sdf: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x65c9c4f1 Device Boot Start End Blocks Id System /dev/sdf1 1 262 2104483+ fd Linux raid autodetect /dev/sdf2 263 524 2104515 fd Linux raid autodetect
(4).保存并退出
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: Device or resource busy. 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. [[email protected] jason]#
5.sdc3 分出2G的主分区,并转换成"fd”(Linux raid autodetect ):
这块盘是做热备用的
(1).创建
[[email protected] jason]# fdisk /dev/sdc 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) 3 Invalid partition number for type `3‘ Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (5225-10443, default 5225): Using default value 5225 Last cylinder, +cylinders or +size{K,M,G} (5225-10443, default 10443): +2G
(2).转换
Command (m for help): t Partition number (1-4): 3 Hex code (type L to list codes): fd Changed system type of partition 3 to fd (Linux raid autodetect)
(3).查看
Command (m for help): p‘ Disk /dev/sdc: 85.9 GB, 85899345920 bytes 255 heads, 63 sectors/track, 10443 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: 0x8967e34e Device Boot Start End Blocks Id System /dev/sdc1 1261220980858+ 83 Linux /dev/sdc22613522420980890 8e Linux LVM /dev/sdc352255486 2104515 fd Linux raid autodetect
(4).保存并退出
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: Device or resource busy. 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)
6.探测刚刚的分区是否被电脑识别:
[[email protected] jason]# partprobe /dev/sde sde sde1 [[email protected] jason]# partprobe /dev/sdg sdg sdg1 [[email protected] jason]# partprobe /dev/sdh sdh sdh1 [[email protected] jason]# partprobe /dev/sdf sdf sdf1 [[email protected] jason]# partprobe /dev/sdc sdc sdc1 sdc2 [[email protected] jason]# partx -a /dev/sde sde sde1 [[email protected] jason]# partx -a /dev/sdg sdg sdg1 [[email protected] jason]# partx -a /dev/sdh sdh sdh1 [[email protected] jason]# partx -a /dev/sdf sdf sdf1 [[email protected] jason]# partx -a /dev/sdf
第一次探测失败。
[[email protected] jason]# partx -a /dev/sdf //按“enter”键 BLKPG: Device or resource busy error adding partition 1 [[email protected] jason]# [[email protected] jason]# [[email protected] jason]# partx -a /dev/sdf sdf sdf1 sdf2 [[email protected] jason]# partx -a /dev/sdf sdf sdf1 sdf2 [[email protected] jason]# partx -a /dev/sde //按“enter”键 BLKPG: Device or resource busy error adding partition 1 [[email protected] jason]# partx -a /dev/sde sde sde1 sde2 [[email protected] jason]# partx -a /dev/sdg //按“enter”键 BLKPG: Device or resource busy error adding partition 1 [[email protected] jason]# partx -a /dev/sdg sdg sdg1 sdg2 [[email protected] jason]# partx -a /dev/sdh sdh sdh1 [[email protected] jason]# partx -a /dev/sdh //按“enter”键 BLKPG: Device or resource busy error adding partition 1 [[email protected] jason]# partx -a /dev/sdh //按“enter”键 sdh sdh1 sdh2 [[email protected] jason]# partx -a /dev/sdc BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 [[email protected] jason]# partx -a /dev/sdc sdc sdc1 sdc2 sdc3
这里在partx -a /dev/sdx后面按enter键之后,再Tab,就能识别出来.第一次的时候每个命令后面接的都是Tab键,都没有识别出来
7.再次查看分区信息:
[[email protected] jason]# fdisk -l | grep "/dev/sd[eghf]2" /dev/sde2 263 524 2104515 fd Linux raid autodetect /dev/sdg2 263 524 2104515 fd Linux raid autodetect /dev/sdf2 263 524 2104515 fd Linux raid autodetect /dev/sdh2 263 524 2104515 fd Linux raid autodetect [[email protected] jason]# fdisk -l | grep "/dev/sdc3" /dev/sdc352255486 2104515 fd Linux raid autodetect [[email protected] jason]#
二、创建RAID设备
1.创建阵列
注意:此处要增加一个热备"-x 1"[eghf]后面加上热备的硬盘分区
[[email protected] jason]# mdadm -Cv /dev/md1 -a yes -n4 -l5 -x1 /dev/sd[eghf]2 /dev/sdc3 mdadm: layout defaults to left-symmetric mdadm: layout defaults to left-symmetric mdadm: chunk size defaults to 512K mdadm: size set to 2102272K mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md1 started. [[email protected] jason]#
解释:
-a:就是如果有需要创建的,就直接创建,比如说md0这个之前是不存在的目录,那么在创建阵列的同时直接创建md0
md1:一般把RAID命名成md0
-n:几块硬盘,就写几
-l:级别,或者说类型,5,就是RAID5
后面的路径信息时构成RAID的成员
-C:是创建阵列
-x是增加热备盘,即sdc3
2.创建后的查看:
[[email protected] jason]# ls -l /dev/md1 brw-rw---- 1 root disk 9, 1 Jun 20 13:07 /dev/md1
另一种查看方式:
[[email protected] jason]# cat /proc/mdstat Personalities : [raid6] [raid5] [raid4] md1 : active raid5 sdh2[5] sdc3[4](S) sdg2[2] sdf2[1] sde2[0] 6306816 blocks super 1.2 level 5, 512k chunk, algorithm 2 [4/4] [UUUU] md0 : active raid5 sde1[5] sdf1[4] sdh1[2] sdg1[1] 6306816 blocks super 1.2 level 5, 512k chunk, algorithm 2 [4/4] [UUUU] unused devices: <none> [[email protected] jason]#
解释:
[4/4]:第一个4是代表4个分区,第二个4表示正常工作的有几个
[UUUU]:表示up,正常启动的有几个,如果有一个有问题,那么会用“_”(下划线)来表示。
上面sdf1[4]代表的是最后一个U
sde2[0]代表是第一个U
sdf2[1]则是第二个
sdg2[2]是第三个
sdh2[5]是第四个
三、在RAID设备中建立文件系统
格式化:
[[email protected] jason]# mkfs -t ext4 /dev/md1 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=128 blocks, Stripe width=384 blocks 394352 inodes, 1576704 blocks 78835 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1614807040 49 block groups 32768 blocks per group, 32768 fragments per group 8048 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 38 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [[email protected] jason]#
四、挂载并使用文件系统
1.创建mdata目录在根下面,作为挂载点:
[[email protected] jason]# mkdir /mdata2
2.挂载/dev/md1:
[[email protected] jason]# cd / [[email protected] /]# ls -l | grep mdata2 drwxr-xr-x2 root root 4096 Jun 20 13:12 mdata2 [[email protected] /]# mount /dev/md1 /mdata2
3.查看挂载信息的方式:
a.mount
[[email protected] /]# mount | grep "/dev/md1" /dev/md1 on /mdata2 type ext4 (rw)
b.另一种查看挂载信息的方式:
[[email protected] /]# df -hT | grep "/dev/md1" /dev/md1 ext4 6.0G 141M 5.5G 3% /mdata2 [[email protected] /]#
因为是RAID 5,有一块盘是用来做校验的,所以实际的容量是6G。
五、修改/etc/fstab文件,实现开机自动启动
1.先备份一下fstab文件
[[email protected] etc]# cp fstab fstab.bak2
2.更改配置文件
[[email protected] etc]# vim fstab [[email protected] etc]#
3.测试是否奏效:
a.先卸载并查看挂载信息:
[[email protected] etc]# umount /dev/md1
[[email protected] etc]# mount | grep "md1"
b.挂载,并查看信息:
[[email protected] etc]# mount /mdata2
[[email protected] etc]# mount | grep "md1" /dev/md1 on /mdata2 type ext4 (rw)
六、设备恢复
1.扫描或查看阵列信息
[[email protected] etc]# mdadm -vDs ARRAY /dev/md0 level=raid5 num-devices=4 metadata=1.2 name=test2:0 UUID=10a00bcf:d67ab596:18b5fa25:8ef335e1 devices=/dev/sde1,/dev/sdg1,/dev/sdh1,/dev/sdf1 ARRAY /dev/md1 level=raid5 num-devices=4 metadata=1.2 spares=1 name=test2:1 UUID=8f4b5df4:f380ce6c:2ff605af:1b33ebcd devices=/dev/sde2,/dev/sdf2,/dev/sdg2,/dev/sdh2,/dev/sdc3 [[email protected] etc]#
2.参照examples创建mdadm.conf
a.查找到mdadm.conf
[[email protected] /]# locate mdadm.conf /etc/mdadm.conf /usr/share/doc/mdadm-3.2.6/mdadm.conf-example /usr/share/man/man5/mdadm.conf.5.gz
b.修改
先将exmple复制到/etc/mdadm.conf
[[email protected] /]# cd /usr/share/doc/mdadm-3.2.6/ [[email protected] mdadm-3.2.6]# vim mdadm.conf-example [[email protected] mdadm-3.2.6]# cd /etc [[email protected] etc]# ls -l | grep mdadm.conf -rw-r--r--1 root root181 Jun 16 17:05 mdadm.conf [[email protected] etc]# cp mdadm.conf mdadm.conf.bak [[email protected] etc]# cd /usr/share/doc/mdadm-3.2.6/ cp: overwrite `/etc/mdadm.conf‘? y [[email protected] mdadm-3.2.6]# cd /etc [[email protected] etc]# vim mdadm.conf [[email protected] etc]#
查看example:
[[email protected] etc]# vim mdadm.conf [[email protected] ~]$ vim /usr/share/doc/mdadm-3.2.6/mdadm.conf-example
# mdadm configuration file # # mdadm will function properly without the use of a configuration file, # but this file is useful for keeping track of arrays and member disks. # In general, a mdadm.conf file is created, and updated, after arrays # are created. This is the opposite behavior of /etc/raidtab which is # created prior to array construction. # # # the config file takes two types of lines: # # DEVICE lines specify a list of devices of where to look for # potential member disks # # ARRAY lines specify information about how to identify arrays so # so that they can be activated # # You can have more than one device line and use wild cards. The first # example includes SCSI the first partition of SCSI disks /dev/sdb, # /dev/sdc, /dev/sdd, /dev/sdj, /dev/sdk, and /dev/sdl. The second # line looks for array slices on IDE disks. # #DEVICE /dev/sd[bcdjkl]1 #DEVICE /dev/hda1 /dev/hdb1 # # If you mount devfs on /dev, then a suitable way to list all devices is: #DEVICE /dev/discs/*/* # # # The AUTO line can control which arrays get assembled by auto-assembly, # meaing either "mdadm -As" when there are no ‘ARRAY‘ lines in this file, # or "mdadm --incremental" when the array found is not listed in this file. # By default, all arrays that are found are assembled. # If you want to ignore all DDF arrays (maybe they are managed by dmraid), # and only assemble 1.x arrays if which are marked for ‘this‘ homehost, # but assemble all others, then use #AUTO -ddf homehost -1.x +all # # ARRAY lines specify an array to assemble and a method of identification. # Arrays can currently be identified by using a UUID, superblock minor number, # or a listing of devices. # # super-minor is usually the minor number of the metadevice # UUID is the Universally Unique Identifier for the array # Each can be obtained using # # mdadm -D <md> # #ARRAY /dev/md0 UUID=3aaa0122:29827cfa:5331ad66:ca767371 #ARRAY /dev/md1 super-minor=1 #ARRAY /dev/md2 devices=/dev/hda1,/dev/hdb1 # # ARRAY lines can also specify a "spare-group" for each array. mdadm --monitor # will then move a spare between arrays in a spare-group if one array has a failed # drive but no spare #ARRAY /dev/md4 uuid=b23f3c6d:aec43a9f:fd65db85:369432df spare-group=group1 #ARRAY /dev/md5 uuid=19464854:03f71b1b:e0df2edd:246cc977 spare-group=group1 # # When used in --follow (aka --monitor) mode, mdadm needs a # mail address and/or a program. This can be given with "mailaddr" # and "program" lines to that monitoring can be started using #mdadm --follow --scan & echo $! > /run/mdadm/mon.pid # If the lines are not found, mdadm will exit quietly #MAILADDR [email protected] #PROGRAM /usr/sbin/handle-mdadm-events 65,1 Bot
修改:
[[email protected] jason]# mdadm -S /dev/md1 mdadm: unrecognised word&n以上是关于构建软RAID5磁盘阵列&RAID设备恢复的主要内容,如果未能解决你的问题,请参考以下文章
Raid5磁盘阵列数据恢复,服务器raid数据恢复步骤和方法