LVM(Logic Volume Management,邏輯卷管理
由多個塊設備(pv,卷)組成一個邏輯卷組(vg),接著在邏輯組上創建邏輯卷(lv),實現在線縮減邏輯卷與邏輯卷組。
實驗:
1、VM虛擬機添加硬盤:
2G(/dev/sdb)
3G(/dev/sdc)
5G(/dev/sdd)
2、查看硬盤信息:
[root@neolinux ~]# fdisk -l | grep -e "^Disk /dev/sd[b-d]" Disk /dev/sdb: 2147 MB, 2147483648 bytes Disk /dev/sdc: 3221 MB, 3221225472 bytes Disk /dev/sdd: 5368 MB, 5368709120 bytes
3、將磁盤設定為PV,并且查看PV的信息:
[root@neolinux ~]# pvcreate /dev/sd[b-d] #將磁盤設置為PV Physical volume "/dev/sdb" successfully created Physical volume "/dev/sdc" successfully created Physical volume "/dev/sdd" successfully created [root@neolinux ~]# pvs #顯示當前PV信息 PV VG Fmt Attr PSize PFree /dev/sda3 VG lvm2 a-- 17.80g 0 /dev/sdb lvm2 --- 2.00g 2.00g /dev/sdc lvm2 --- 3.00g 3.00g /dev/sdd lvm2 --- 5.00g 5.00g [root@neolinux ~]# pvdisplay /dev/sdb #顯示PV詳細信息 "/dev/sdb" is a new physical volume of "2.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb VG Name PV Size 2.00 GiB Allocatable NO PE Size 0 #PE Size是在創建VG時指定 Total PE 0 #該磁盤一共有多少個PE,取決于創建VG時,指定PE的大小 Free PE 0 #該磁盤剩余可用PE數量 Allocated PE 0 PV UUID cu50Yp-PcWC-o76e-CTyK-CBgt-BjaJ-2z98lt #每一個PV的UUID都不一樣 [root@neolinux ~]#
4、創建VG,并且查看VG的信息:
[root@neolinux ~]# vgcreate -s 16M myvg /dev/sd[b-d] #創建名字為myvg的邏輯卷組,添加/dev/sdb,/dev/sdc,/dev/sdd到此卷組中,指定PE大小為16M,默認為4M Volume group "myvg" successfully created [root@neolinux ~]# vgs VG #PV #LV #SN Attr VSize VFree VG 1 2 0 wz--n- 17.80g 0 myvg 3 0 0 wz--n- 9.95g 9.95g #此VG 一共有多少容量可使用 [root@neolinux ~]# vgdisplay myvg --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 3 Act PV 3 VG Size 9.95 GiB PE Size 16.00 MiB #VG中PE大小 Total PE 637 Alloc PE / Size 0 / 0 Free PE / Size 637 / 9.95 GiB VG UUID DLTJNf-he5S-I8rw-jUNZ-8CK1-Cb3w-TqCMCD [root@neolinux ~]# pvdisplay /dev/sdb --- Physical volume --- PV Name /dev/sdb VG Name myvg #顯示磁盤加入到myvg卷組中 PV Size 2.00 GiB / not usable 16.00 MiB Allocatable yes PE Size 16.00 MiB Total PE 127 Free PE 127 Allocated PE 0 PV UUID cu50Yp-PcWC-o76e-CTyK-CBgt-BjaJ-2z98lt
5、創建邏輯卷lv,并查看lv信息
[root@neolinux ~]# lvcreate -n mylv -L 6G -p rw myvg #創建lv Logical volume "mylv" created. #-n:指定lv名稱 #-L:指定lv大小 #-p:指定讀寫權限 [root@neolinux ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert LV01 VG -wi-ao---- 9.77g LV02 VG -wi-ao---- 8.04g mylv myvg -wi-a----- 6.00g [root@neolinux ~]# lvdisplay /dev/myvg/mylv --- Logical volume --- LV Path /dev/myvg/mylv LV Name mylv VG Name myvg LV UUID Ob0WO9-CMn0-pVLu-xd5a-fy4g-ABDM-JGB8et LV Write Access read/write LV Creation host, time neolinux, 2016-06-04 02:29:09 +0800 LV Status available # open 0 LV Size 6.00 GiB Current LE 384 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2 [root@neolinux ~]#
6、格式化文件系統,掛載使用
[root@neolinux ~]# mke2fs -t ext4 /dev/myvg/mylv #格式化分區 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 393216 inodes, 1572864 blocks 78643 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1610612736 48 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 34 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@neolinux ~]# mount /dev/myvg/mylv /mnt #掛載使用 [root@neolinux ~]# mount | grep /mnt /dev/mapper/myvg-mylv on /mnt type ext4 (rw) [root@neolinux ~]# df -Th /mnt/ Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/myvg-mylv ext4 5.8G 12M 5.5G 1% /mnt [root@neolinux ~]# cp /etc/rc.d/init.d/functions /mnt [root@neolinux ~]# ll /mnt/ total 44 -rw-r--r-- 1 root root 25419 Jun 4 02:55 functions drwx------ 2 root root 16384 Jun 4 02:51 lost+found [root@neolinux ~]#
7、在線擴展lv容量
[root@neolinux ~]# lvextend -L +1G /dev/myvg/mylv #+號表示在原有容量的基礎上新增多少,不寫+號表示容量擴容到多少,在擴展容量之前,應該提前檢查該LV所在的VG上可用容量是否滿足需求,在去擴展LV的邏輯邊界 Size of logical volume myvg/mylv changed from 6.00 GiB (384 extents) to 7.00 GiB (448 extents). Logical volume mylv successfully resized [root@neolinux ~]# df -Th /mntFilesystem #查看掛載/mnt目錄容量大小,沒有發生變化,是因為沒有擴展LV的邏輯邊界 Type Size Used Avail Use% Mounted on/dev/mapper/myvg-mylv ext4 5.8G 13M 5.5G 1% /mnt [root@neolinux ~]# resize2fs /dev/myvg/mylv #擴展LV的邏輯邊界,這里沒有指定大小,表示與LV的物理邊界一樣大 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/myvg/mylv is mounted on /mnt; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/myvg/mylv to 1835008 (4k) blocks. The filesystem on /dev/myvg/mylv is now 1835008 blocks long. [root@neolinux ~]# df -Th /mnt #擴展之后的容量顯示 Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/myvg-mylv ext4 6.8G 14M 6.5G 1% /mnt [root@neolinux ~]# ll /mnt/ #/mnt目錄下文件一樣存在 total 44 -rw-r--r-- 1 root root 25419 Jun 4 02:55 functions drwx------ 2 root root 16384 Jun 4 02:51 lost+found 在線擴容步驟總結: 1、檢查VG的容量是否滿足擴容需求,不滿足,請先擴容VG 2、執行擴容LV物理邊界命令 3、確認LV物理邊界是否擴容成功 4、執行擴容LV邏輯邊界 5、檢查掛載目錄容量,確認擴容完成
8、縮減LV容量
[root@neolinux ~]# umount /mnt #取消掛載 [root@neolinux ~]# e2fsck -f /dev/myvg/mylv #強制對磁盤進行檢查 e2fsck 1.41.12 (17-May-2010) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/myvg/mylv: 12/458752 files (0.0% non-contiguous), 65030/1835008 blocks [root@neolinux ~]# resize2fs /dev/myvg/mylv 3G #縮減LV的邏輯邊界到3G容量大小 resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/mylv to 786432 (4k) blocks. The filesystem on /dev/myvg/mylv is now 786432 blocks long. [root@neolinux ~]# lvreduce /dev/myvg/mylv -L 3G #縮減LV物理邊界到3G容量大小 WARNING: Reducing active logical volume to 3.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce mylv? [y/n]: y #告警,確認 Size of logical volume myvg/mylv changed from 7.00 GiB (448 extents) to 3.00 GiB (192 extents). Logical volume mylv successfully resized [root@neolinux ~]# lvs #檢查LV容量縮減是否完成 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert LV01 VG -wi-ao---- 9.77g LV02 VG -wi-ao---- 8.04g mylv myvg -wi-a----- 3.00g [root@neolinux ~]# mount /dev/myvg/mylv /mnt/ #重新掛載該LV [root@neolinux ~]# ll /mnt/ #檢查數據依然存在 total 44 -rw-r--r-- 1 root root 25419 Jun 4 02:55 functions drwx------ 2 root root 16384 Jun 4 02:51 lost+found
9、移除PV
[root@neolinux ~]# vgreduce myvg /dev/sdb #在當LV掛載時,選擇移除PV,在選擇移除PV的時候,要考慮到該移除PV的總容量要小于當前該VG的剩余可用容量 Removed "/dev/sdb" from volume group "myvg" [root@neolinux ~]# pvs #確認PV已經移除VG PV VG Fmt Attr PSize PFree /dev/sda3 VG lvm2 a-- 17.80g 0 /dev/sdb lvm2 --- 2.00g 2.00g /dev/sdc myvg lvm2 a-- 2.98g 2.98g /dev/sdd myvg lvm2 a-- 4.98g 1.98g [root@neolinux ~]# ll /mnt/ #LV掛載數據依然存在 total 44 -rw-r--r-- 1 root root 25419 Jun 4 02:55 functions drwx------ 2 root root 16384 Jun 4 02:51 lost+found [root@neolinux ~]# vgs VG #PV #LV #SN Attr VSize VFree VG 1 2 0 wz--n- 17.80g 0 myvg 2 1 0 wz--n- 7.97g 4.97g #我們在此進行LV縮減至2G [root@neolinux ~]# df -Th /mnt Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/myvg-mylv ext4 2.0G 3.1M 1.9G 1% /mnt [root@neolinux ~]# pvs #可以清楚的看到LV上的數據保存在/dev/sdc這個PV中, PV VG Fmt Attr PSize PFree /dev/sda3 VG lvm2 a-- 17.80g 0 /dev/sdb lvm2 --- 2.00g 2.00g /dev/sdc myvg lvm2 a-- 2.98g 1008.00m /dev/sdd myvg lvm2 a-- 4.98g 4.98g [root@neolinux ~]# #數據在/dev/sdc這個PV中,我們需要把這個PV移除VG [root@neolinux ~]# pvmove /dev/sdc /dev/sdd #移動/dev/sdc上的PE到/dev/sdd /dev/sdc: Moved: 0.0% /dev/sdc: Moved: 100.0% [root@neolinux ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda3 VG lvm2 a-- 17.80g 0 /dev/sdb lvm2 --- 2.00g 2.00g /dev/sdc myvg lvm2 a-- 2.98g 2.98g #可以看到/sdc上沒有存在數據 /dev/sdd myvg lvm2 a-- 4.98g 2.98g #實際存儲的數據轉移到/dev/sdd上 [root@neolinux ~]# #接下來我們可以安心把/dev/sdc從VG上卸載 [root@neolinux ~]# vgreduce myvg /dev/sdc Removed "/dev/sdc" from volume group "myvg" [root@neolinux ~]# ll /mnt/ #此時LV不受任何影響 total 44 -rw-r--r-- 1 root root 25419 Jun 4 03:35 functions drwx------ 2 root root 16384 Jun 4 03:34 lost+found [root@neolinux ~]#
10、PV移除
[root@neolinux ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda3 VG lvm2 a-- 17.80g 0 /dev/sdb lvm2 --- 2.00g 2.00g /dev/sdc lvm2 --- 3.00g 3.00g /dev/sdd myvg lvm2 a-- 4.98g 2.98g [root@neolinux ~]# pvremove /dev/sdb Labels on physical volume "/dev/sdb" successfully wiped
11、快照snopshot功能
[root@neolinux ~]# df -Th /mnt #檢查mylv,大小為2G,使用3.1M Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/myvg-mylv ext4 2.0G 3.1M 1.9G 1% /mnt [root@neolinux ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert LV01 VG -wi-ao---- 9.77g LV02 VG -wi-ao---- 8.04g mylv myvg -wi-ao---- 2.00g [root@neolinux ~]# vgs #查看mylv所在的vg還有6G的容量,足夠我們創建快照卷 VG #PV #LV #SN Attr VSize VFree VG 1 2 0 wz--n- 17.80g 0 myvg 2 1 0 wz--n- 7.97g 5.97g [root@neolinux ~]# lvcreate -L 3G -s -n mylv-snapshot /dev/myvg/mylv #創建快照卷,大小為3G,快照卷選擇的大小,應該是需要快照的卷容量加上需要備份時間內此卷的變化容量 Reducing COW size 3.00 GiB down to maximum usable size 2.02 GiB. #比如說快照備份時間需要30分鐘,在30分鐘原卷數據變化量1G,那么我們應該選擇快照卷的大小為大于2G+1G Logical volume "mylv-snapshot" created.
12、對快照卷備份管理
[root@neolinux ~]# mount /dev/myvg/mylv-snapshot /opt/ #快照卷是需要格式化就可以掛載,查看 [root@neolinux ~]# ls /opt/ functions lost+found [root@neolinux ~]# touch /mnt/test #在原卷創建test文件 [root@neolinux ~]# ll /mnt/ total 44 -rw-r--r-- 1 root root 25419 Jun 4 03:35 functions drwx------ 2 root root 16384 Jun 4 03:34 lost+found -rw-r--r-- 1 root root 0 Jun 5 03:32 test [root@neolinux ~]# [root@neolinux ~]# tar JcPf /backup/mylv-`date +%F`.tar.xz /opt/* #對快照卷文件系統級別備份 [root@neolinux ~]# dd if=/dev/myvg/mylv-snapshot of=/backup/mylv-`date +%F`.dd #對快照卷做塊級別的備份 4194304+0 records in 4194304+0 records out 2147483648 bytes (2.1 GB) copied, 45.3875 s, 47.3 MB/s [root@neolinux ~]# umount /opt/ #對快照卷取消掛載 [root@neolinux ~]# lvremove /dev/myvg/mylv-snapshot #刪除快照卷 Do you really want to remove active logical volume mylv-snapshot? [y/n]: y Logical volume "mylv-snapshot" successfully removed [root@neolinux ~]#
13、模擬對原卷破壞
[root@neolinux ~]# dd if=/dev/zero of=/dev/myvg/mylv bs=512 count=1000 #模擬對原卷的破壞 1000+0 records in 1000+0 records out 512000 bytes (512 kB) copied, 0.0212266 s, 24.1 MB/s [root@neolinux ~]# ll /mnt/ #檢查數據,其實這個時候原卷已經損壞 total 0 [root@neolinux ~]# mount /dev/mapper/VG-LV01 on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw) /dev/sda1 on /boot type ext4 (rw) /dev/mapper/VG-LV02 on /usr type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) /dev/mapper/myvg-mylv on /mnt type ext4 (rw) [root@neolinux ~]# umount /mnt/ #取消掛載原卷 [root@neolinux ~]# mount /dev/myvg/mylv /mnt/ #重新掛載原卷,已經掛載不上去了 mount: you must specify the filesystem type
14、使用對數據塊的備份恢復原卷數據
[root@neolinux ~]# dd if=/backup/mylv-2016-06-05.dd of=/dev/myvg/mylv #對快照卷進行恢復 4194304+0 records in 4194304+0 records out 2147483648 bytes (2.1 GB) copied, 53.3457 s, 40.3 MB/s [root@neolinux ~]# mount /dev/myvg/mylv /mnt #重新掛載 [root@neolinux ~]# ll /mnt/ #檢查數據為快照卷備份那時刻的數據 total 44 -rw-r--r-- 1 root root 25419 Jun 4 03:35 functions drwx------ 2 root root 16384 Jun 4 03:34 lost+found [root@neolinux ~]#
15、編寫一個小腳本對lvm進行定時備份
#!/bin/bash #/root/scripts/backuplvm.sh # /sbin/lvcreate -L 3G -s -n mylv-snapshot /dev/myvg/mylv /bin/mount /dev/myvg/mylv-snapshot /opt/ /bin/tar JcPf /backup/mylv-`date +%F` /opt/* /bin/umount /opt/ /sbin/lvremove /dev/myvg/mylv-snapshot # [root@neolinux scripts]# chmod +x /root/scripts/backuplvm.sh [root@neolinux scripts]# crontab -l #每天晚上23點59分59秒開始備份 0 0 * * * /bin/bash /root/scripts/backuplvm.sh [root@neolinux scripts]#
原創文章,作者:nice_neo_linux,如若轉載,請注明出處:http://www.www58058.com/17666