首先,先创建3个lvm类型的分区:
Device Boot Start End Blocks Id System /dev/sdb1 1 52 417658+ 8e Linux LVM /dev/sdb2 53 104 417690 8e Linux LVM /dev/sdb3 105 111 56227+ 8e Linux LVM
创建物理卷:
pvcreate /dev/sdb1 /dev/sdb2 /dev/sdb3 Physical volume "/dev/sdb1" successfully created Physical volume "/dev/sdb2" successfully created Physical volume "/dev/sdb3" successfully created
查看物理卷:
pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg_mail lvm2 a-- 19.51g 0 /dev/sdb1 lvm2 a-- 407.87m 407.87m /dev/sdb2 lvm2 a-- 407.90m 407.90m /dev/sdb3 lvm2 a-- 54.91m 54.91m
创建卷组:
vgcreate myvg /dev/sdb1 /dev/sdb2 /dev/sdb3 Volume group "myvg" successfully created
查看卷组:
vgs VG #PV #LV #SN Attr VSize VFree myvg 3 0 0 wz--n- 860.00m 860.00m vg_mail 1 2 0 wz--n- 19.51g 0
创建LVM镜像逻辑卷:
lvcreate -L 300M -n mylv -m1 myvg Logical volume "mylv" created
再来查看卷组大小:
vgs VG #PV #LV #SN Attr VSize VFree myvg 3 1 0 wz--n- 860.00m 256.00m vg_mail 1 2 0 wz--n- 19.51g 0
通过和上面的比较,不难发现,这里我创建的是300M的逻辑卷,而卷组却减少了600M左右,因为我们-m1这个参数使得我们的逻辑卷有了镜像的功能。
再来查看lv中各设备的状态:
lvs -a -o +devices LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert Devices mylv myvg mwi-a-m-- 300.00m mylv_mlog 100.00 mylv_mp_w_picpath_0(0),mylv_mp_w_picpath_1(0) [mylv_mp_w_picpath_0] myvg iwi-aom-- 300.00m /dev/sdb1(0) [mylv_mp_w_picpath_1] myvg iwi-aom-- 300.00m /dev/sdb2(0) [mylv_mlog] myvg lwi-aom-- 4.00m /dev/sdb3(0) lv_root vg_mail -wi-ao--- 17.57g /dev/sda2(0) lv_swap vg_mail -wi-ao--- 1.94g /dev/sda2(4498)
这里我们可以看出,sdb1和sdb2为数据卷和镜像卷,sdb3为日志卷
格式化和挂载:
mkfs.ext4 /dev/myvg/mylv mount /dev/myvg/mylv /mnt/
现在我们先向lv中写入数据然后损坏sdb1:
dd if=/dev/zero of=/mnt/jj bs=1M count=100 100+0 records in 100+0 records out 104857600 bytes (105 MB) copied, 1.13108 s, 92.7 MB/s
dd if=/dev/zero of=/dev/sdb1 count=10
再次查看lv中个设备的状态:
lvs -a -o +devices Couldn't find device with uuid zcHbJz-hec4-bLBX-cuKd-4Sv9-kgOS-3ZyR8O. LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert Devices mylv myvg mwi-aom-p 300.00m mylv_mlog 100.00 mylv_mp_w_picpath_0(0),mylv_mp_w_picpath_1(0) [mylv_mp_w_picpath_0] myvg iwi-aom-p 300.00m unknown device(0) [mylv_mp_w_picpath_1] myvg iwi-aom-- 300.00m /dev/sdb2(0) [mylv_mlog] myvg lwi-aom-- 4.00m /dev/sdb3(0) lv_root vg_mail -wi-ao--- 17.57g /dev/sda2(0) lv_swap vg_mail -wi-ao--- 1.94g /dev/sda2(4498)
这里我们可以看到sdb1变成了unknown device。但是这时在挂载点下,我们还是可以进行读写操作的。
把坏掉的设备从卷组中移除:
vgreduce --removemissing --force myvg Couldn't find device with uuid zcHbJz-hec4-bLBX-cuKd-4Sv9-kgOS-3ZyR8O. Wrote out consistent volume group myvg [root@mail mnt]# lvs -a -o +devices LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert Devices mylv myvg -wi-ao--- 300.00m /dev/sdb2(0) lv_root vg_mail -wi-ao--- 17.57g /dev/sda2(0) lv_swap vg_mail -wi-ao--- 1.94g /dev/sda2(4498)
已经移除了。然后我们重新挂载,可看到之前写的jj那个文件还在。
mount -o remount /mnt/ ls jj lost+found
我们再创建sdb4来取代sdb1的位置
Device Boot Start End Blocks Id System /dev/sdb1 1 52 417658+ 8e Linux LVM /dev/sdb2 53 104 417690 8e Linux LVM /dev/sdb3 105 111 56227+ 8e Linux LVM /dev/sdb4 112 163 417690 8e Linux LVM Command (m for help): w 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@mail mnt]# partx -a call: partx -opts [device] wholedisk [root@mail mnt]# partx -a /dev/sdb BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 BLKPG: Device or resource busy error adding partition 3 [root@mail mnt]# pvcreate /dev/sdb4 Physical volume "/dev/sdb4" successfully created [root@mail mnt]# vgextend myvg /dev/sdb4 Volume group "myvg" successfully extended
扩展并查看lv中的设备:
lvconvert -m1 /dev/myvg/mylv /dev/sdb4 myvg/mylv: Converted: 0.0% myvg/mylv: Converted: 100.0% [root@mail mnt]# lvs -a -o +devices LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert Devices mylv myvg mwi-aom-- 300.00m mylv_mlog 100.00 mylv_mp_w_picpath_0(0),mylv_mp_w_picpath_1(0) [mylv_mp_w_picpath_0] myvg iwi-aom-- 300.00m /dev/sdb2(0) [mylv_mp_w_picpath_1] myvg iwi-aom-- 300.00m /dev/sdb4(0) [mylv_mlog] myvg lwi-aom-- 4.00m /dev/sdb4(75) lv_root vg_mail -wi-ao--- 17.57g /dev/sda2(0) lv_swap vg_mail -wi-ao--- 1.94g /dev/sda2(4498) [root@mail mnt]#
可以看到sdb2和sdb4为数据卷和镜像卷了。这就是lvm的镜像。lvm的功能很强大,可以在man文档最后的Examples中看到。