Linux創建RAID實戰
一、Linux創建RAID0
- RAID0俗稱條帶,它將兩個或多個硬盤組成一個邏輯硬盤,容量是所有硬盤之和
- 因為是多個硬盤組合成一個,故可並行寫操作,寫入速度提高,但此方式硬盤數據沒有冗余,沒有容錯,一旦一個物理硬盤損壞,則所有數據均丟失
- RAID0適合於對數據量大,但安全性要求不高的場景,比如音像、視頻文件的存儲等
二、創建RAID0,並格式化、掛載使用
- 添加兩塊10G的虛擬硬盤
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 10G 0 disk //添加的第一塊10G的虛擬硬盤
sdb 8:16 0 10G 0 disk //添加的第二塊10G的虛擬硬盤
sr0 11:0 1 7.3G 0 rom
nvme0n1 259:0 0 80G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 79G 0 part
├─rhel-root 253:0 0 50G 0 lvm /
├─rhel-swap 253:1 0 2G 0 lvm [SWAP]
└─rhel-home 253:2 0 27G 0 lvm /home
- 將兩塊虛擬硬盤分區,分區ID為fd(Linux raid autodetect)
[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.32.1).
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 (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): //直接回車,將所有空間分成一個主分區
Created a new partition 1 of type 'Linux' and of size 10 GiB.
Command (m for help): t //更改分區ID類型
Selected partition 1
Hex code (type L to list all codes): fd //設置分區類型ID為fd
Changed type of partition 'Linux' to 'Linux raid autodetect'.
Command (m for help): p //查看創建的分區
Disk /dev/sda: 10 GiB, 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
Disklabel type: dos
Disk identifier: 0x3c4b6576
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 20971519 20969472 10G fd Linux raid autodetect //剛剛創建的分區,id=fd就是對的
Command (m for help): w //保存退出
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
同理,第二塊虛擬硬盤分區ID也是fd。
- 查看創建的分區
[root@localhost ~]# fdisk -l | grep raid
/dev/sdb1 2048 20971519 20969472 10G fd Linux raid autodetect
/dev/sda1 2048 20971519 20969472 10G fd Linux raid autodetect
- 創建 RAID0
[root@localhost ~]# yum install -y mdadm
[root@localhost ~]# mdadm -C -v /dev/md0 -l0 -n2 /dev/sd{a,b}1
mdadm: chunk size defaults to 512K
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
- 查看 raidstat 狀態
[root@localhost ~]# cat /proc/mdstat
Personalities : [raid0]
md0 : active raid0 sdb1[1] sda1[0]
20951040 blocks super 1.2 512k chunks
unused devices: <none>
- 查看 RAID0 的詳細信息
[root@localhost ~]# mdadm -D /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Tue Dec 15 13:07:37 2020
Raid Level : raid0
Array Size : 20951040 (19.98 GiB 21.45 GB)
Raid Devices : 2
Total Devices : 2
Persistence : Superblock is persistent
Update Time : Tue Dec 15 13:07:37 2020
State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0
Layout : original
Chunk Size : 512K
Consistency Policy : none
Name : localhost:0 (local to host localhost)
UUID : a3aad0f2:ed4fc982:e7dec94f:4d481ef0
Events : 0
Number Major Minor RaidDevice State
0 8 1 0 active sync /dev/sda1
1 8 17 1 active sync /dev/sdb1
- 格式化 /dev/md0,類型為xfs
[root@localhost ~]# mkfs.xfs /dev/md0
meta-data=/dev/md0 isize=512 agcount=16, agsize=327296 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=5236736, imaxpct=25
= sunit=128 swidth=256 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
- 掛載 /dev/md0
[root@localhost ~]# mkdir /raid0
[root@localhost ~]# mount /dev/md0 /raid0/
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 887M 0 887M 0% /dev
tmpfs 904M 0 904M 0% /dev/shm
tmpfs 904M 8.7M 895M 1% /run
tmpfs 904M 0 904M 0% /sys/fs/cgroup
/dev/mapper/rhel-root 50G 4.5G 46G 9% /
/dev/nvme0n1p1 1014M 173M 842M 18% /boot
/dev/mapper/rhel-home 27G 225M 27G 1% /home
tmpfs 181M 0 181M 0% /run/user/0
/dev/md0 20G 176M 20G 1% /raid0