RAID的创建
第一步:先查看我们系统的磁盘情况
[root@station40 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 17.8G 0 part /
sdb 8:16 0 200G 0 disk
├─sdb1 8:17 0 1G 0 part
├─sdb2 8:18 0 1G 0 part
├─sdb3 8:19 0 1G 0 part
├─sdb4 8:20 0 1K 0 part
├─sdb5 8:21 0 1G 0 part
└─sdb6 8:22 0 1G 0 part
sdc 8:32 0 100G 0 disk
sr0 11:0 1 3.7G 0 rom
第二步:删掉一些不用的磁盘
[root@station40 ~]# dd if=/dev/zero of=/dev/sdb bs=1 count=512
512+0 records in
512+0 records out
512 bytes (512 B) copied, 0.376112 s, 1.4 kB/s
第三步:同步一下内存信息
[root@station40 ~]# partx -d --nr 1-6 /dev/sdb
[root@station40 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 17.8G 0 part /
sdb 8:16 0 200G 0 disk
sdc 8:32 0 100G 0 disk
sr0 11:0 1 3.7G 0 rom
第四步:现在我们创建一个RAID-0
A:创建分区(最少三个)
[root@station40 ~]# fdisk /dev/sdb
WARNING: GPT (GUID Partition Table) detected on '/dev/sdb'! The util fdisk doesn't support GPT. Use GNU Parted.
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x4a846ad4.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
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): 1
First cylinder (1-26108, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-26108, default 26108): +2G
Command (m for help): P
Disk /dev/sdb: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 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: 0x4a846ad4
Device Boot Start End Blocks Id System
/dev/sdb1 1 262 2104483+ 83 Linux
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx
5 Extended 42 SFS 86 NTFS volume set da Non-FS data
6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility
8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt
9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access
a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary
16 Hidden FAT16 64 Novell Netware af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto (raid标签,也可以不选则,但是不方便后期查找)
1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT
1e Hidden W95 FAT1
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux raid autodetect)
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
创建的过程中,需要特别注意的是选择磁盘的RAID的型号这个动作一定要记得
b:创建一块RAID磁盘后可以检查;
[root@station40 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 17.8G 0 part /
sdb 8:16 0 200G 0 disk
└─sdb1 8:17 0 2G 0 part
sdc 8:32 0 100G 0 disk
sr0 11:0 1 3.7G 0 rom
c:相同的步骤可以继续分出3个相同的分区
[root@station40 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 17.8G 0 part /
sdb 8:16 0 200G 0 disk
├─sdb1 8:17 0 2G 0 part
├─sdb2 8:18 0 2G 0 part
├─sdb3 8:19 0 2G 0 part
├─sdb4 8:20 0 1K 0 part
└─sdb5 8:21 0 2G 0 part
sdc 8:32 0 100G 0 disk
sr0 11:0 1 3.7G 0 rom
d:分区OK后我们现在需要确认一下系统中是否有RAID磁盘
例:[root@station40 ~]# ll /dev/md*
ls: cannot access /dev/md*: No such file or directory
e:我们先分一个RIAD-0阵列
[root@station40 ~]# mdadm -C /dev/md0 -a yes -l 0 -n 2 /dev/sdb1 /dev/sdb2
mdadm: /dev/sdb1 appears to contain an ext2fs file system 提醒的错误显示之前sdb这个磁盘有以前实验ext2格式,不用在意直接y选项
size=1060256K mtime=Thu Jan 1 08:00:00 1970
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
现在可以查看一下创建的过程
[root@station40 ~]# mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Mon Apr 24 10:09:02 2017
Raid Level : raid0 (RAID级别)
Array Size : 4204544 (4.01 GiB 4.31 GB)
Raid Devices : 2
Total Devices : 2
Persistence : Superblock is persistent
Update Time : Mon Apr 24 10:09:02 2017
State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0
Chunk Size : 512K (默认大小)
Name : station40.magelinux.com:0 (local to host station40.magelinux.com)
UUID : b08b2229:03516277:61d40ba7:0c5be81b (此处的UUID和之前的分区的UUID不是一样)
Events : 0
Number Major Minor RaidDevice State
0 8 17 0 active sync /dev/sdb1 (两个设备组成)
1 8 18 1 active sync /dev/sdb2
f:下面我们将它格式化(需要注意的是我们格式化是放在创建RAID之后进行统一格式化处理)
[root@station40 ~]# mkfs.ext4 /dev/md0
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
262944 inodes, 1051136 blocks
52556 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1077936128
33 block groups
32768 blocks per group, 32768 fragments per group
7968 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
接着查看磁盘情况:
root@station40 ~]# blkid
/dev/sda3: UUID="953e4427-c667-41a4-9bee-b8c4ab55ce42" TYPE="ext4"
/dev/sda1: UUID="18e40da7-01ac-483c-a427-566276b7fbcd" TYPE="ext4"
/dev/sda2: UUID="af50c30e-5861-4125-ad83-bc74b88b2c6a" TYPE="swap"
/dev/sdb1: UUID="88cadd96-353c-4b02-bf7b-3c4eea28adc1" TYPE="ext4"
/dev/sdb2: UUID="b08b2229-0351-6277-61d4-0ba70c5be81b" UUID_SUB="a6244ed9-665b-2474-a3ea-60bf2458bd8b" LABEL="station40.magelinux.com:0" TYPE="linux_raid_member"
/dev/md0: UUID="dabc8242-0315-4987-9ec6-7e5b652ec77d" TYPE="ext4"
g:格式化之后我们就可以进行挂载了;
[root@station40 ~]# mkdir -p /mut/raid0
[root@station40 ~]# mount /dev/md0 /mut/raid0/
h:现在可以测试一下写性能:
root@station40 /mut/raid0]# dd if=/dev/zero of=file bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 18.0445 s, 59.5 MB/s
现在测试写的性能:
[root@station40 /mut/raid0]# dd if=file of=/dev/null
4194304+0 records in
4194304+0 records out
2147483648 bytes (2.1 GB) copied, 3.67849 s, 584 MB/s
第五步:现在我们创建一个RAID-5
a:因为考虑到目前没有备用磁盘sparedisk,所以需要加一块;
b;添加OK后就可以开始raid-5的创建
[root@station40 /mut/raid0]# mdadm -C /dev/md5 -a yes -l 5 -n 3 -x 1 /dev/sdb3 /dev/sdb5 /dev/sdb6 /dev/sdb7 (红色为增加的备用磁盘为1块磁盘为/dev/sdb7)
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md5 started.
c.创建OK后可以检查
[root@station40 /mut/raid0]# mdadm -D /dev/md5
/dev/md5:
Version : 1.2
Creation Time : Mon Apr 24 11:21:44 2017
Raid Level : raid5
Array Size : 4204544 (4.01 GiB 4.31 GB)
Used Dev Size : 2102272 (2.00 GiB 2.15 GB)
Raid Devices : 3
Total Devices : 4
Persistence : Superblock is persistent
Update Time : Mon Apr 24 11:22:07 2017
State : clean
Active Devices : 3
Working Devices : 4
Failed Devices : 0
Spare Devices : 1
Layout : left-symmetric
Chunk Size : 512K
Name : station40.magelinux.com:5 (local to host station40.magelinux.com)
UUID : 45fad8bb:492498b4:7a0adc06:19f0a544
Events : 18
Number Major Minor RaidDevice State
0 8 19 0 active sync /dev/sdb3
1 8 21 1 active sync /dev/sdb5
4 8 22 2 active sync /dev/sdb6
3 8 23 - spare /dev/sdb7
d:接着进行格式化处理
[root@station40 /mut/raid0]# mkfs.ext4 /dev/md5
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
262944 inodes, 1051136 blocks
52556 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1077936128
33 block groups
32768 blocks per group, 32768 fragments per group
7968 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 34 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
e:现在可以查看系统中的raid的情况
[root@station40 ~]# mdadm -Ds
ARRAY /dev/md0 metadata=1.2 name=station40.magelinux.com:0 UUID=b08b2229:03516277:61d40ba7:0c5be81b
ARRAY /dev/md5 metadata=1.2 spares=1 name=station40.magelinux.com:5 UUID=45fad8bb:492498b4:7a0adc06:19f0a544
f:现在我们可以检查raid-5的容错性先
[root@station40 ~]# mdadm -Ds > /etc/mdadm.conf (我们需要将系统中的raid设备加载至其配置文件中,这样就不会影响开关机)
同时我们取消raid的挂载
[root@station40 ~]# umount /mut/raid0
然后我们就可以禁用raid设备了
[root@station40 ~]# mdadm -S /dev/md0 stop
mdadm: stopped /dev/md0
mdadm: Cannot open stop
禁用后我们查看该设备是看不到的
root@station40 ~]# ll /dev/md0
ls: cannot access /dev/md0: No such file or directory
现在可以启用该设备了(这里如果开始没有写配置文件的那一步则这次就无法启动的)
[root@station40 ~]# mdadm -A /dev/md0
mdadm: /dev/md0 has been started with 2 drives
g:现在可以模拟将RAID-5中的sdb3损坏
[root@station40 ~]# mdadm /dev/md5 -f /dev/sdb3
mdadm: set /dev/sdb3 faulty in /dev/md5
现在可以看下系统中raid-5的情况
[root@station40 ~]# mdadm -D /dev/md5
/dev/md5:
Version : 1.2
Creation Time : Mon Apr 24 11:21:44 2017
Raid Level : raid5
Array Size : 4204544 (4.01 GiB 4.31 GB)
Used Dev Size : 2102272 (2.00 GiB 2.15 GB)
Raid Devices : 3
Total Devices : 4
Persistence : Superblock is persistent
Update Time : Mon Apr 24 17:30:21 2017
State : clean
Active Devices : 3
Working Devices : 3
Failed Devices : 1 (一块损坏状态)
Spare Devices : 0
Layout : left-symmetric
Chunk Size : 512K
Name : station40.magelinux.com:5 (local to host station40.magelinux.com)
UUID : 45fad8bb:492498b4:7a0adc06:19f0a544
Events : 37
Number Major Minor RaidDevice State
3 8 23 0 active sync /dev/sdb7 (备用的磁盘挂上去了)
1 8 21 1 active sync /dev/sdb5
4 8 22 2 active sync /dev/sdb6
0 8 19 - faulty /dev/sdb3
h:现在我们把损坏的硬盘移除掉
[root@station40 ~]# mdadm /dev/md5 -r /dev/sdb3
mdadm: hot removed /dev/sdb3 from /dev/md5
[root@station40 ~]# mdadm -D /dev/md5
/dev/md5:
Version : 1.2
Creation Time : Mon Apr 24 11:21:44 2017
Raid Level : raid5
Array Size : 4204544 (4.01 GiB 4.31 GB)
Used Dev Size : 2102272 (2.00 GiB 2.15 GB)
Raid Devices : 3
Total Devices : 3
Persistence : Superblock is persistent
Update Time : Mon Apr 24 17:45:48 2017
State : clean
Active Devices : 3
Working Devices : 3
Failed Devices : 0
Spare Devices : 0
Layout : left-symmetric
Chunk Size : 512K
Name : station40.magelinux.com:5 (local to host station40.magelinux.com)
UUID : 45fad8bb:492498b4:7a0adc06:19f0a544
Events : 38
Number Major Minor RaidDevice State
3 8 23 0 active sync /dev/sdb7
1 8 21 1 active sync /dev/sdb5
4 8 22 2 active sync /dev/sdb6
可以看到/dev/sdb3已经被移除。
现在我们再追加一个磁盘
[root@station40 ~]# mdadm /dev/md5 -a /dev/sdb3
mdadm: added /dev/sdb3
[root@station40 ~]# mdadm -D /dev/md5
/dev/md5:
Version : 1.2
Creation Time : Mon Apr 24 11:21:44 2017
Raid Level : raid5
Array Size : 4204544 (4.01 GiB 4.31 GB)
Used Dev Size : 2102272 (2.00 GiB 2.15 GB)
Raid Devices : 3
Total Devices : 4
Persistence : Superblock is persistent
Update Time : Mon Apr 24 17:49:13 2017
State : clean
Active Devices : 3
Working Devices : 4
Failed Devices : 0
Spare Devices : 1
Layout : left-symmetric
Chunk Size : 512K
Name : station40.magelinux.com:5 (local to host station40.magelinux.com)
UUID : 45fad8bb:492498b4:7a0adc06:19f0a544
Events : 39
Number Major Minor RaidDevice State
3 8 23 0 active sync /dev/sdb7
1 8 21 1 active sync /dev/sdb5
4 8 22 2 active sync /dev/sdb6
5 8 19 - spare /dev/sdb3
同时我们需要知道当我们增加raid磁盘阵列需要增加成员的的时候可以使用
将磁盘增加至4块
[root@station40 ~]# mdadm -G /dev/md5 -n 4 -a /dev/sdb3
增加的磁盘因为没有文件系统所以需要在添加OK后同步一下RAID的文件格式
[root@station40 ~]# resize3fs /dev/md5