磁盘阵列全名是『 Redundant Arrays of Inexpensive Disks, RAID 』,容错式廉价磁盘阵列。 RAID 可以通过一些技术(软件或硬件),将多个较小的磁盘整合成为一个较大的磁盘设备; 而这个较大的磁盘功能可不止是储存而已,他还具有数据保护的功能呢。整个 RAID 由亍选择的等级 (level) 不同,而使得整合后的磁盘具有不同的功能, 基本常见的等级有这几种:
RAID=0(等量模式,stripe):性能最佳
RAID-1 (映像模式, mirror):完整备份
RAID 0+1,RAID 1+0
RAID 5:性能与数据备份的均衡考虑
以下构建一个RAID1的软件磁盘阵列
首先删除掉之前的分区
[root@localhost ~]# fdisk /dev/vdb 建立三个分区,类型为fd(Linux raid auto)
[root@localhost ~]# partprobe
root@localhost ~]# mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/vdb{1..3} ##建立磁盘阵列,命名为/dev/md0 ,由两块分区组成,一块候补
-C --create :为创建 RAID 的选项;
-a --auto=yes :决定创建后面接的软件磁盘阵列装置,亦即 /dev/md0, /dev/md1...
--raid-devices=N :使用几个磁碟 (partition) 作为磁盘阵列的装置
--spare-devices=N :使用几个磁碟作为备用 (spare) 装置
--level=[015] :配置这组磁盘阵列的等级。支持很多,不过建议只要用 0, 1, 5 即可
--detail :后面所接的那个磁盘阵列装置的详细资讯
[root@localhost ~]# mkfs.xfs /dev/md0 ##格式化
[root@localhost ~]# mount /dev/md0 /mnt/
[root@localhost ~]# df -h
具体代码块如下:
监控方
[root@localhost ~]# watch -n 1 cat /proc/mdstat
操作方
第一:先清空之前的分区设定
第二:建立三个1G大小的分区,类型为fd(Linux raid auto)
[root@localhost ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +1G
Partition 1 of type Linux and of size 1 GiB is set
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2):
First sector (2099200-20971519, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-20971519, default 20971519): +1G
Partition 2 of type Linux and of size 1 GiB is set
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3):
First sector (4196352-20971519, default 4196352):
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-20971519, default 20971519): +1G
Partition 3 of type Linux and of size 1 GiB is set
Command (m for help): t
Partition number (1-3, default 3): 1
1e Hidden W95 FAT1 80 Old Minix
Hex code (type L to list all codes): fd
Changed type of partition 'Linux' to 'Linux raid autodetect'
Command (m for help): t
Partition number (1-3, default 3): 2
Hex code (type L to list all codes): fd
Changed type of partition 'Linux' to 'Linux raid autodetect'
Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): fd
Changed type of partition 'Linux' to 'Linux raid autodetect'
Command (m for help): wq
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.
[root@localhost ~]# partprobe
Error: Partition(s) 1 on /dev/vdb have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use. As a result, the old partition(s) will remain in use. You should reboot now before making further changes.
[root@localhost ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00013f3e
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20970332 10484142+ 83 Linux
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x9d727787
Device Boot Start End Blocks Id System
/dev/vdb1 2048 2099199 1048576 fd Linux raid autodetect
/dev/vdb2 2099200 4196351 1048576 fd Linux raid autodetect
/dev/vdb3 4196352 6293503 1048576 fd Linux raid autodetect
第三:建立磁盘阵列
[root@localhost ~]# mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/vdb{1..3}
mdadm: cannot open /dev/vdb1: Device or resource busy ##因为没有把挂载去掉
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3151228 7322672 31% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 80 942580 1% /dev/shm
tmpfs 942660 17048 925612 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
/dev/vdb1 98988 25776 73212 27% /pub
[root@localhost ~]# umount /pub
[root@localhost ~]# mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/vdb{1..3} ##建立磁盘阵列,命名为/dev/md0 ,由两块分区组成,一块候补
mdadm: Note: this array has metadata at the start and
may not be suitable as a boot device. If you plan to
store '/boot' on this device please ensure that
your boot-loader understands md/v1.x metadata, or use
--metadata=0.90
mdadm: largest drive (/dev/vdb2) exceeds size (102272K) by more than 1%
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
[root@localhost ~]# mkfs.xfs /dev/md0 ##格式化文件系统为xfs
meta-data=/dev/md0 isize=256 agcount=4, agsize=6392 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=25568, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# mount /dev/md0 /mnt/
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3124168 7349732 30% /
devtmpfs 927072 0 927072 0% /dev
tmpfs 942660 84 942576 1% /dev/shm
tmpfs 942660 17032 925628 2% /run
tmpfs 942660 0 942660 0% /sys/fs/cgroup
/dev/md0 1044588 32928 1011660 4% /mnt
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 10G 3.0G 7.1G 30% /
devtmpfs 906M 0 906M 0% /dev
tmpfs 921M 84K 921M 1% /dev/shm
tmpfs 921M 17M 904M 2% /run
tmpfs 921M 0 921M 0% /sys/fs/cgroup
/dev/md0 1021M 33M 988M 4% /mnt
以最小的显示
[root@localhost ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00013f3e
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20970332 10484142+ 83 Linux
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x9d727787
Device Boot Start End Blocks Id System
/dev/vdb1 2048 2099199 1048576 fd Linux raid autodetect
/dev/vdb2 2099200 4196351 1048576 fd Linux raid autodetect
/dev/vdb3 4196352 6293503 1048576 fd Linux raid autodetect
Disk /dev/md0: 104 MB, 104726528 bytes, 204544 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
``