LVM邏輯卷實(shí)現(xiàn)磁盤鏡像
大家都知道用lvm作分區(qū)擴(kuò)展很方便森枪,其實(shí)用lvs還可以用作磁盤鏡像病毡,類似于raid1雷滋,即多塊磁盤互相同步備份平绩,可以有效避免數(shù)據(jù)丟失的尷尬恭垦。
1.新建磁盤分區(qū)
新建4個(gè)分區(qū)/dev/sdb1,/dev/sdb2,/dev/sdb3,/dev/sdb4泪幌,如:
Disk /dev/sdb: 1999.8 GB, 1999844147200 bytes
255 heads, 63 sectors/track, 243133 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 374 3004123+ 83 Linux
/dev/sdb2 375 748 3004155 83 Linux
/dev/sdb3 749 1122 3004155 83 Linux
/dev/sdb4 1123 1496 3004155 83 Linux
其中/dev/sdb1,/dev/sdb2用作數(shù)據(jù)磁盤,/dev/sdb2用作/dev/sdb1的鏡像署照;
/dev/sdb3用作日志祸泪;/dev/sdb4作為備用,當(dāng)sdb1或sdb2損害時(shí)備用建芙,類似于raid5中的熱備盤没隘。
2.創(chuàng)建pv和vg
pvcreate /dev/sdb1
pvcreate /dev/sdb2
pvcreate /dev/sdb3
pvcreate /dev/sdb4
vgcreate vg_test /dev/sdb1 /dev/sdb2 /dev/sdb3
注:/dev/sdb4暫未使用,待備用
3.創(chuàng)建包含鏡像功能的邏輯卷(mirror_test)
[root@localhost ~]# lvcreate -L 2.8G -m1 -n mirror_test vg_test /dev/sdb1 /dev/sdb2 /dev/sdb3
Rounding up size to full physical extent 2.80 GB
Logical volume "mirror_test" created
[root@localhost ~]# lvs -a -o +devices
LV VG Attr LSize Origin Snap% Move Log Copy% Convert Devices
mirror_test vg_test mwi-a- 2.80G mirror_test_mlog 24.27 mirror_test_mimage_0(0),mirror_test_mimage_1(0)
[mirror_test_mimage_0] vg_test Iwi-ao 2.80G /dev/sdb1(0)
[mirror_test_mimage_1] vg_test Iwi-ao 2.80G /dev/sdb2(0)
[mirror_test_mlog] vg_test lwi-ao 4.00M /dev/sdb3(0)
其中:-m1參數(shù)為創(chuàng)建鏡像禁荸;-L參數(shù)為設(shè)定鏡像卷的大小為2.8G右蒲;-n參數(shù)為指定鏡像名稱阀湿。這個(gè)鏡像包括三部分:sdb1和sdb2位數(shù)據(jù)卷和鏡像卷,sdb3為日志卷瑰妄。
4.格式化掛載
[root@localhost ~]# mkfs.ext3 /dev/vg_test/mirror_test
[root@localhost ~]# mkdir -p /test
[root@localhost ~]# mount /dev/vg_test/mirror_test /test
5.測(cè)試
(1)創(chuàng)建測(cè)試文件
for x in {1..100};do echo "$x test test test " >> /test/test.txt ;done
(2)模擬對(duì)/dev/sdb2進(jìn)行破壞
[root@localhost ~]# dd if=/dev/zero of=/dev/sdb2 count=10 bs=1M
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.008574 seconds, 1.2 GB/s
[root@localhost ~]# lvs -a -o +devices
Couldn't find device with uuid z74R0l-ZLUV-X6TS-QrpF-nXDZ-gc74-UvwGC0.
LV VG Attr LSize Origin Snap% Move Log Copy% Convert Devices
mirror_test vg_test mwi-ao 2.80G mirror_test_mlog 100.00 mirror_test_mimage_0(0),mirror_test_mimage_1(0)
[mirror_test_mimage_0] vg_test iwi-ao 2.80G /dev/sdb1(0)
[mirror_test_mimage_1] vg_test iwi-ao 2.80G unknown device(0)
[mirror_test_mlog] vg_test lwi-ao 4.00M /dev/sdb3(0)
查看狀態(tài)發(fā)現(xiàn)/dev/sdb2處于“unknown device(0)”狀態(tài)陷嘴。
[root@localhost ~]# lvscan
Couldn't find device with uuid z74R0l-ZLUV-X6TS-QrpF-nXDZ-gc74-UvwGC0.
ACTIVE '/dev/vg_test/mirror_test' [2.80 GB] inherit
(3)重新掛載確認(rèn)數(shù)據(jù)可讀取
[root@localhost ~]# umount /test
[root@localhost test]# cd
[root@localhost ~]# mount /dev/vg_test/mirror_test /test
[root@localhost ~]# cat /test/test.txt
lvs mirror test
(4)將壞掉的設(shè)備刪除
[root@localhost ~]# vgreduce --removemissing --force vg_test
Couldn't find device with uuid z74R0l-ZLUV-X6TS-QrpF-nXDZ-gc74-UvwGC0.
WARNING: Bad device removed from mirror volume, vg_test/mirror_test
WARNING: Mirror volume, vg_test/mirror_test converted to linear due to device failure.
Wrote out consistent volume group vg_test
移除后vgdisplay /dev/vg_test發(fā)現(xiàn)mirrored volumes選項(xiàng)沒有了;并且由于sdb2的實(shí)效间坐,
鏡像mirror_test也已經(jīng)有mirror模式轉(zhuǎn)變成線性模式灾挨,如果恢復(fù)我們需要用到lvconvert命令來恢復(fù)。
(5)鏡像數(shù)據(jù)恢復(fù)
[root@localhost ~]# vgextend vg_test /dev/sdb4
Volume group "vg_test" successfully extended
[root@localhost ~]# lvconvert -m1 /dev/vg_test/mirror_test /dev/sdb1 /dev/sdb4 /dev/sdb3
vg_test/mirror_test: Converted: 0.0%
vg_test/mirror_test: Converted: 20.8%
vg_test/mirror_test: Converted: 40.4%
vg_test/mirror_test: Converted: 60.1%
vg_test/mirror_test: Converted: 79.4%
vg_test/mirror_test: Converted: 99.2%
vg_test/mirror_test: Converted: 100.0%
ok竹宋,恢復(fù)完成劳澄,我們來查看下狀態(tài):
[root@localhost ~]# lvdisplay /dev/vg_test
--- Logical volume ---
LV Name /dev/vg_test/mirror_test
VG Name vg_test
LV UUID RUamnD-nYCt-D9yp-cNdX-ix1y-r9P9-wenSjb
LV Write Access read/write
LV Status available
# open 1
LV Size 2.80 GB
Current LE 717
Mirrored volumes 2
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:8
[root@localhost ~]# cat /test/test.txt
lvs mirror test
[root@localhost ~]# lvs -a -o +devices
LV VG Attr LSize Origin Snap% Move Log Copy% Convert Devices
mirror_test vg_test mwi-ao 2.80G mirror_test_mlog 100.00 mirror_test_mimage_0(0),mirror_test_mimage_1(0)
[mirror_test_mimage_0] vg_test iwi-ao 2.80G /dev/sdb1(0)
[mirror_test_mimage_1] vg_test iwi-ao 2.80G /dev/sdb4(0)
[mirror_test_mlog] vg_test lwi-ao 4.00M /dev/sdb3(0)
鏡像卷由原來的sdb2轉(zhuǎn)變?yōu)閟db4
總結(jié):在某些不支持raid的服務(wù)器上,我們可以使用lvm的磁盤鏡像來解決防止數(shù)據(jù)丟失的問題了蜈七。