文章目錄
raid1原理:
RAID-1 :mirroring(鏡像卷)需要磁盤兩塊以上
原理:是把一個磁盤的數據鏡像到另一個磁盤上,也就是說數據在寫入一塊磁盤的同時,會在另一塊閒置的磁盤上生成鏡像文件,(同步)
特性:當一塊硬盤失效時,系統會忽略該硬盤,轉而使用剩餘的鏡像盤讀寫數據,具備很好的磁盤冗餘能力。
mdadm介紹
mdadm是linux下用於創建和管理軟件RAID的命令,是一個模式化命令
mdadm 命令的參數
管理軟raid工具:mdadm
-C 建立一個新陣列
-A 激活磁盤陣列
-a 添加設備到陣列
-D 打印陣列設備詳細信息
-s 掃描配置文件或者/proc/mdstat 得到陣列缺失信息
-v 顯示詳細信息
-r 移除設備
-f 將設備狀態定爲故障
-l 設定磁盤陣列的級別
-n 指定磁盤陣列的成員(分區/磁盤的數量)
-x 指定陣列中備用磁盤的數量
實戰raid 1
新增兩塊硬盤
[root@192 ~]# ll /dev/sd*
brw-rw----. 1 root disk 8, 0 2月 28 02:57 /dev/sda
brw-rw----. 1 root disk 8, 1 2月 28 02:57 /dev/sda1
brw-rw----. 1 root disk 8, 2 2月 28 02:57 /dev/sda2
brw-rw----. 1 root disk 8, 16 2月 28 03:02 /dev/sdb
brw-rw----. 1 root disk 8, 17 2月 28 03:02 /dev/sdb1
brw-rw----. 1 root disk 8, 32 2月 28 02:57 /dev/sdc
1)創建分區
[root@192 ~]# 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):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +3G
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@192 ~]# fdisk /dev/sdb
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 (394-2610, default 394):
Using default value 394
Last cylinder, +cylinders or +size{K,M,G} (394-2610, default 2610): +5G
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@192 ~]# ll /dev/sd*
brw-rw----. 1 root disk 8, 0 2月 28 02:57 /dev/sda
brw-rw----. 1 root disk 8, 1 2月 28 02:57 /dev/sda1
brw-rw----. 1 root disk 8, 2 2月 28 02:57 /dev/sda2
brw-rw----. 1 root disk 8, 16 2月 28 03:09 /dev/sdb
brw-rw----. 1 root disk 8, 17 2月 28 03:09 /dev/sdb1
brw-rw----. 1 root disk 8, 18 2月 28 03:09 /dev/sdb2
brw-rw----. 1 root disk 8, 32 2月 28 02:57 /dev/sdc
[root@192 ~]#
2)創建raid1
[root@192 ~]# mdadm -C -v /dev/md1 -l 1 -n 2 -x 1 /dev/sdb1 /dev/sdb[2,3]
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: size set to 3154624K
mdadm: largest drive (/dev/sdb2) exceeds size (3154624K) by more than 1%
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.
[root@192 ~]#
3) 將RAID1信息保存到配置文件中
[root@192 ~]# mdadm -Dsv > /etc/mdadm.conf
[root@192 ~]#
4)檢查我們的磁盤陣列
[root@192 ~]# mdadm -D /dev/md1
/dev/md1:
Version : 1.2
Creation Time : Fri Feb 28 03:15:59 2020 //創建時間
Raid Level : raid1
Array Size : 3154624 (3.01 GiB 3.23 GB)
Used Dev Size : 3154624 (3.01 GiB 3.23 GB)
Raid Devices : 2
Total Devices : 3
Persistence : Superblock is persistent
Update Time : Fri Feb 28 03:17:48 2020
State : clean
Active Devices : 2 //當前活動設備
Working Devices : 3 //工作設備
Failed Devices : 0 // 失效設備
Spare Devices : 1 //熱備盤的數量
Name : 192.168.148.130:1 (local to host 192.168.148.130)
UUID : 2fd4866c:ad1637ee:12c3fb06:dbe2405f
Events : 17
Number Major Minor RaidDevice State
0 8 17 0 active sync /dev/sdb1
1 8 18 1 active sync /dev/sdb2
2 8 19 - spare /dev/sdb3
[root@192 ~]#
5) 在raid設備上創建文件系統並掛載
[root@192 ~]# mkfs.ext4 /dev/md1
mke2fs 1.41.12 (17-May-2010)
文件系統標籤=
操作系統:Linux
塊大小=4096 (log=2)
分塊大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
197200 inodes, 788656 blocks
39432 blocks (5.00%) reserved for the super user
第一個數據塊=0
Maximum filesystem blocks=809500672
25 block groups
32768 blocks per group, 32768 fragments per group
7888 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
正在寫入inode表: 完成
Creating journal (16384 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@192 ~]# mkdir /raid1
[root@192 ~]# mou
mount mount.fuse mount.nfs4 mountstats mousetweaks
mount.cifs mount.nfs mountpoint mount.tmpfs
[root@192 ~]# mount /dev/md1 /raid1/
[root@192 ~]# df -h | tail -l
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 3.6G 13G 22% /
tmpfs 491M 80K 491M 1% /dev/shm
/dev/sda1 485M 35M 426M 8% /boot
/dev/sr0 4.2G 4.2G 0 100% /media/CentOS_6.5_Final
/dev/md1 3.0G 69M 2.8G 3% /raid1
[root@192 ~]#
6) 創建測試文件,看如果一塊磁盤壞掉,數據是否丟失
[root@192 ~]# touch aaa.txt
[root@192 ~]# echo "磁盤壞了,我也在" >aaa.txt
[root@192 ~]#
7) 模擬損壞(sdb1盤壞掉了)
[root@192 ~]# mdadm /dev/md1 -f /dev/sdb1
mdadm: set /dev/sdb1 faulty in /dev/md1
[root@192 ~]#
[root@192 ~]# mdadm -D /dev/md1
/dev/md1:
Version : 1.2
Creation Time : Fri Feb 28 03:15:59 2020
Raid Level : raid1
Array Size : 3154624 (3.01 GiB 3.23 GB)
Used Dev Size : 3154624 (3.01 GiB 3.23 GB)
Raid Devices : 2
Total Devices : 3
Persistence : Superblock is persistent
Update Time : Fri Feb 28 03:29:29 2020
State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 1
Spare Devices : 0
Name : 192.168.148.130:1 (local to host 192.168.148.130)
UUID : 2fd4866c:ad1637ee:12c3fb06:dbe2405f
Events : 36
Number Major Minor RaidDevice State
2 8 19 0 active sync /dev/sdb3
1 8 18 1 active sync /dev/sdb2
0 8 17 - faulty /dev/sdb1
[root@192 ~]#
測試移除之後文件是否還在
[root@192 ~]# ls
aaa.txt install.log 公共的 視頻 文檔 音樂
anaconda-ks.cfg install.log.syslog 模板 圖片 下載 桌面
[root@192 ~]# cat aaa.txt
磁盤壞了,我也在
[root@192 ~]#
8) 移除壞掉的設備,同時另外加一個備份盤
[root@192 ~]# mdadm -r /dev/md1 /dev/sdb1
mdadm: hot removed /dev/sdb1 from /dev/md1
[root@192 ~]#
查看是否移除
[root@192 ~]# mdadm -D /dev/md1
/dev/md1:
Version : 1.2
Creation Time : Fri Feb 28 03:15:59 2020
Raid Level : raid1
Array Size : 3154624 (3.01 GiB 3.23 GB)
Used Dev Size : 3154624 (3.01 GiB 3.23 GB)
Raid Devices : 2
Total Devices : 2
Persistence : Superblock is persistent
Update Time : Fri Feb 28 03:37:08 2020
State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 0
Spare Devices : 0
Name : 192.168.148.130:1 (local to host 192.168.148.130)
UUID : 2fd4866c:ad1637ee:12c3fb06:dbe2405f
Events : 37
Number Major Minor RaidDevice State
2 8 19 0 active sync /dev/sdb3
1 8 18 1 active sync /dev/sdb2
[root@192 ~]#
9) 增加一塊熱備盤
[root@192 ~]# mdadm -a /dev/md1 /dev/sdc1
總結:
raid1中一塊硬盤壞了不影響raid正常運行
使用率是50%