linux 软RAID0/5的实现

2023年 7月 16日 12.6k 0

对多磁盘来讲要想能够组织多块磁盘当做一个设备来进行工作,他们是在内核中配置完成的。而用户的所有功能是不可能直达内核的,so,我们需要一个专门的管理工具,帮忙来实现组织。这个工具叫做mdadn.能偶组织软RAID管理。

软RAID0
示例:创建一个大小为10G的raid0:
注意:不应该使用同一个磁盘上的多个分区创建raid设备:
创建两个个1G逻辑卷,t修改为fd,修改成raid autodetect

partx -a 重新识别分区表
1,准备第一块硬盘!
fdisk /dev/sdb
Command (m for help): n(创建)
Command action
e extended
p primary partition (1-4)
p(创建主分区)
Partition number (1-4): 1(分区号)
First cylinder (1-2610, default 1): 1(开始柱面)
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +5G(创建5G)
*将分区修改为fd Linux raid auto
Command (m for help): t(修改)
Selected partition 1
Hex code (type L to list codes):fd(Linux raid auto)
Changed system type of partition 1 to fd (Linux raid autodetect)
w保存,
2,装备第二块硬盘!
(第二块我把swap给干掉了,swapoff /dev/sda3)
Command (m for help): t(修改)
Partition number (1-4): 3(编号)
Hex code (type L to list codes): fd(Linux raid auto)
Changed system type of partition 3 to fd (Linux raid autodetect)

Command (m for help): p

Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 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: 0x00083353

Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 15413 123489280 83 Linux
/dev/sda3 15413 15666 2031616 fd Linux raid autodetect
wq保存
3,重新识别分区表
[root@localhost ~]#partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
[root@localhost ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
[root@localhost ~]# partx -a /dev/sda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
[root@localhost ~]#

mdadm -C指明创建位置/dev/md/nd0(nd0自己命名)
-a yes 自动创建设备文件,
-n 2,表示一共有两块硬盘来创建,
-L 0,表示创建raid0,后面跟上两块硬盘
[root@localhost ~]# mdadm -C /dev/md/nd0 -a yes -n 2 -l 0 /dev/sda3 /dev/sdb1
mdadm: /dev/nd0 is an invalid name for an md device. Try /dev/md/nd0
[root@localhost ~]# mdadm -C /dev/md/nd0 -a yes -n 2 -l 0 /dev/sda3 /dev/sdb1
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md/nd0 started.
[root@localhost ~]#
创建完成!查看raid状态
[root@localhost ~]# cat /proc/mdstat
Personalities : [raid0]
md127 : active raid0 sdb1[1] sda3[0]
7282688 blocks super 1.2 512k chunks

unused devices: <none>
[root@localhost ~]#
[root@localhost ~]#ls -l /dev/md/nd0
lrwxrwxrwx. 1 root root 8 Apr 6 05:55 /dev/md/nd0 -> ../md127
[root@localhost ~]#
格式化成ext4文件系统类型
[root@localhost ~]#mke2fs -t ext4 /dev/md/nd0
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=256 blocks
455168 inodes, 1820672 blocks
91033 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1866465280
56 block groups
32768 blocks per group, 32768 fragments per group
8128 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]#
我们挂载把!
[root@localhost ~]# mkdir /tmp/mark.raid0.1
[root@localhost ~]#mount /dev/md/nd0 /tmp/mark.raid0.1/
[root@localhost ~]#mount
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
/dev/md127 on /tmp/mark.raid0.1 type ext4 (rw)
[root@localhost ~]#ls /tmp/mark.raid0.1/
lost+found
[root@localhost ~]#
示例二:创建一个10G的raid5
/dev/sdb2 655 1308 5253255 fd Linux raid autodetect
/dev/sdb3 1309 1962 5253255 fd Linux raid autodetect
/dev/sdb4 1963 2616 5253255 fd Linux raid autodetect
创建三个5G的分区,使用T改成fd,
partx -a
[root@localhost ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
[root@localhost ~]#
[root@localhost ~]#mdadm -C /dev/md/nd5 -a yes -n 3 -l 5 /dev/sdb[2,3,4]
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md/nd5 started.
[root@localhost ~]# cat /proc/mdstat
Personalities : [raid0] [raid6] [raid5] [raid4]
md126 : active raid5 sdb4[3] sdb3[1] sdb2[0]
10498048 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]

md127 : inactive sda3[0]
2030592 blocks super 1.2

unused devices: <none>
查看运行状态
[root@localhost ~]#watch -n1 'cat /proc/mdstat'
格式化成ext4文件系统类型
[root@localhost ~]# mkfs.ext4 /dev/md/nd5
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=256 blocks
657072 inodes, 2624512 blocks
131225 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2688548864
81 block groups
32768 blocks per group, 32768 fragments per group
8112 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 27 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]#
挂载
[root@localhost ~]# mount /dev/md/nd5 /tmp/mark.raid0.1/
[root@localhost ~]#mount
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
/dev/md126 on /tmp/mark.raid0.1 type ext4 (rw)
[root@localhost ~]#
验证raid5
[root@localhost mark.raid0.1]# cp /etc/fstab ./
[root@localhost mark.raid0.1]# ls
fstab lost+found
[root@localhost mark.raid0.1]#
我们把其中一块硬盘标记损坏状态
[root@localhost mark.raid0.1]#mdadm -f /dev/md/nd5 /dev/sdb2
mdadm: set /dev/sdb2 faulty in /dev/md/nd5
[root@localhost mark.raid0.1]#
使用 mdadm -D查看,sdb2是否已经挂了
[root@localhost mark.raid0.1]# mdadm -D /dev/md/nd5
Number Major Minor RaidDevice State
0 0 0 0 removed
1 8 19 1 active sync /dev/sdb3
3 8 20 2 active sync /dev/sdb4

0 8 18 - faulty /dev/sdb2
我们在回头看fstab,文件仍然存在。
[root@localhost mark.raid0.1]#cat /tmp/mark.raid0.1/fstab

#
# /etc/fstab
# Created by anaconda on Sat Apr 4 08:45:23 2015
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=4fb479dc-0364-41ac-a12d-841210d879a4 / ext4 defaults 1 1
UUID=30d2724b-a9be-4e32-8b9a-cf731a92173a /boot ext4 defaults 1 2
UUID=7568de25-a229-43cb-952a-ca5fd83bbdcc swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
[root@localhost mark.raid0.1]#
使用sdb5替换sdb3,
[root@localhost mark.raid0.1]#mdadm -a /dev/md/nd5 /dev/sdb5
拆掉sdb3(建议不要在修复过程中拆掉)
[root@localhost mark.raid0.1]#mdadm -r /dev/md/nd5 /dev/sdb3
停止掉nd5
[root@localhost ~]#umount /dev/md/nd5
[root@localhost ~]# mdadm -S /dev/md/nd5
mdadm: stopped /dev/md/nd5
[root@localhost ~]#cat /proc/mdstat
Personalities : [raid0] [raid6] [raid5] [raid4]
md127 : inactive sda3[0]
2030592 blocks super 1.2

unused devices: <none>
[root@localhost ~]#
在装配:
[root@localhost ~]#mdadm -A /dev/md/nd5 /dev/sdb[3,4,5]

相关文章

服务器端口转发,带你了解服务器端口转发
服务器开放端口,服务器开放端口的步骤
产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
如何使用 WinGet 下载 Microsoft Store 应用
百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

发布评论