LVM
LVM全稱邏輯卷管理(LogicVolume Manager,LVM),是Linux 系統對磁盤分區的一種管理機制,LVM是建立在硬盤和分區至上的一個邏輯層,可以將多個物理分區通過軟件,整合在一起,讓這些分區看起來就像是一個獨立的大磁盤一樣,來提高磁盤分區管理的靈活性,可靈活的對磁盤大小進行動態調整。LVM 僅能彈性地使用(或調整)磁盤空間,無法提供磁盤陣列的容錯能力。如果 LVM 出現故障,所有存儲於 LVM 上的文件可能就會永久消失。
LVM 術語
物理卷(PhysicalVolume,PV) 是LVM存儲結構中處於最底層,物理卷可以是整個硬盤,也可是其中的某個分區,這裏可以簡單看作爲LVM物理卷的分區,其分區系統識別碼(System ID)必須是標識爲“8e Linux LVM”.
卷組(Volume Group,VG)是建立在物理卷之上,它由一個或多個物理卷組成卷組組成,可以動態添加捲組中,在卷組上可以創建一個或多個"LVM分區"(邏輯卷)一個LVM 系統中可以自由一個卷組,也可以包含多個卷組LVM的卷組類似於非LVM系統中的物理硬盤
邏輯卷(Logic Volume,LV)建立在卷組之上,它是從卷組中"切出"的一塊空間邏輯卷船艦之後,其大小可以伸縮。這裏可以分享下從某本書中看到一個事例,這樣方便記憶與理解。事例:物理卷 好比麪粉; 卷組好比麪糰; 邏輯卷好比饅頭,饅頭可大可小,可控制。
物理擴展單元(Physical Extent):物理卷可以進一步細分爲物理擴展單元,每個物理擴展單元具有唯一的編號, 也是LVM能夠尋址的最小單元。物理擴展單元的大小可以配置,默認值爲4MB.
邏輯擴展單元(Logical Extent):邏輯卷可以劃分尋址的邏輯擴展單元。在同一個卷組中,物理擴展單元與邏輯擴展單元的大小相同,且一一對應。
LVM 創建流程圖
LVM 管理工具
PV | VG | LV | 作用 |
pvcreate | vgcreate | lvcreate | 創建物理卷、卷組、邏輯卷 |
pvdisplay | vgdisplay | lvdisplay | 查看物理卷、卷組、邏輯卷大小 |
pvremove | vgremove | lvremove | 移除物理卷、卷組、邏輯卷 |
Null | vgextend | lvextend | 擴展卷組、邏輯卷大小 |
Null | vgredure | lvredure | 減少卷組、邏輯卷大小 |
pvsan | vgscan | lvscan | 物理卷、卷組、邏輯卷 |
創建和管理LVM
創建分區
使用分區工具fdisk創建LVM分區,方法和創建其他一般分區的方式是一樣的,並將此分區System ID 更改爲:"8eLinux"
[root@dirhost140 ~]#fdisk /dev/sdd Command (m for help):p Disk /dev/sdd: 500.1GB, 500107862016 bytes 255 heads, 63sectors/track, 60801 cylinders Units = cylinders of16065 * 512 = 8225280 bytes Sector size(logical/physical): 512 bytes / 512 bytes I/O size(minimum/optimal): 512 bytes / 512 bytes Disk identifier:0x2311983e Device BootStartEndBlocksIdSystem /dev/sdd11130610490413+83Linux Command (m for help):d Selected partition 1 Command (m for help):n Command action eextended pprimary partition (1-4) p Partition number(1-4): 1 First cylinder(1-60801, default 1): Using default value 1 Last cylinder,+cylinders or +size{K,M,G} (1-60801, default 60801): +20G Command (m for help):t Selected partition 1 Hex code (type L tolist codes): 8e Changed system typeof partition 1 to 8e (Linux LVM) Command (m for help):w The partition tablehas been altered! Calling ioctl() tore-read partition table. Syncing disks.
創建物理卷
創建物理卷的命令爲pvcreate,利用該命令將希望添加到卷組的所有分區或者磁盤創建爲物理卷。這裏這裏以磁盤分區/dev/sdd1爲例,將 /dev/sdd1 初始化成物理卷:
[root@dirhost140 ~]#pvcreate /dev/sdd1 Physical volume "/dev/sdd1"successfully created
創建卷組
[root@dirhost140 ~]#vgcreate vg04 /dev/sdd1 Volume group "vg04" successfullycreated
創建邏輯卷
[root@dirhost140 ~]#lvcreate-L +5G -n lvtest01 vg04 Logical volume "lvtest01" created
這裏通過lvdisplay 命令去查看LV 的詳細信息
[root@dirhost140 ~]#lvdisplay /dev/vg04/lvtest01 --- Logical volume --- LV Name/dev/vg04/lvtest01 VG Namevg04 LV UUID6RJCH7-Qbbo-JTMl-Jpec-J6KS-rBgw-rP2uDN LV Write Acce***ead/write LV Statusavailable # open0 LV Size5.00 GiB Current LE1280 Segments1 Allocationinherit Read ahead sectorsauto - currently set to256 Block device253:16
掛載並讀取LVM
[root@dirhost140 ~]#mount /dev/vg04/lvtest01 /aaa/ [root@dirhost140 ~]#df -Th /dev/vg04/lvtest01 FilesystemTypeSizeUsed Avail Use% Mounted on /dev/mapper/vg04-lvtest01 ext45.0G138M4.6G3% /aaa
調整邏輯卷大小
擴大LVM邏輯卷的大小
調整邏輯卷大小需要先擴大LV,再放大文件系統, 在擴大LVM邏輯卷大小之前,先檢查下其LV 的Block大小
[root@DirHost140 ~]#tune2fs -l /dev/vg04/lvtest01 |grep 'Block' Block count:1310720#5G = Block count*4/1024/1024 Block size:4096 Blocks pergroup:32768 Blocks per group:8192
擴大邏輯卷1GB 大小
[root@DirHost140 ~]#lvextend -L +1G /dev/vg04/lvtest01 Extending logical volume lvtest01 to 6.00 GiB Logical volume lvtest01 successfully resized
雖然已經將LV空間擴充了1GB大小,但文件系統仍然是5G,所以這裏需要放大下文件系統.
[root@DirHost140 ~]#df -Th/dev/mapper/vg04-lvtest01 FilesystemTypeSizeUsed Avail Use% Mounted on /dev/mapper/vg04-lvtest01 ext45.0G138M4.6G3% /aaa [root@DirHost140 ~]#resize2fs /dev/vg04/lvtest01 resize2fs 1.41.12(17-May-2010) Filesystem at/dev/vg04/lvtest01 is mounted on /aaa; on-line resizing required old desc_blocks = 1,new_desc_blocks = 1 Performing an on-lineresize of /dev/vg04/lvtest01 to 1572864 (4k) blocks. The filesystem on/dev/vg04/lvtest01 is now 1572864 blocks long. [root@DirHost140 ~]#df -Th /dev/mapper/vg04-lvtest01 FilesystemTypeSizeUsed Avail Use% Mounted on /dev/mapper/vg04-lvtest01 ext46.0G138M5.5G3% /aaa
減小LVM邏輯卷的大小
減小LVM 3GB的大小空間
由於LVM 不支持在線減小LVM,因此需要umountLV,離線進行減少LVM 3G大小空間先減小邏輯卷大小,再減小文件系統大小(前提備份,卸載操作),
[root@dirhost140 /]#umount /aaa [root@dirhost140 /]#resize2fs /dev/vg04/lvtest01 3G resize2fs 1.41.12(17-May-2010) Filesystem at/dev/vg04/lvtest01 is mounted on /aaa; on-line resizing required On-line shrinkingfrom 1572864 to 786432 not supported. [root@dirhost140 /]#umount /aaa/ [root@dirhost140 /]#resize2fs /dev/vg04/lvtest01 3G resize2fs 1.41.12(17-May-2010) Please run 'e2fsck -f/dev/vg04/lvtest01' first.
[root@dirhost140 /]#e2fsck -f /dev/vg04/lvtest01 e2fsck 1.41.12(17-May-2010) Pass 1: Checkinginodes, blocks, and sizes Pass 2: Checkingdirectory structure Pass 3: Checkingdirectory connectivity Pass 4: Checkingreference counts Pass 5: Checkinggroup summary information /dev/vg04/lvtest01:11/393216 files (0.0% non-contiguous), 60014/1572864 blocks [root@dirhost140 /]#resize2fs /dev/vg04/lvtest01 3G resize2fs 1.41.12(17-May-2010) Resizing thefilesystem on /dev/vg04/lvtest01 to 786432 (4k) blocks. The filesystem on/dev/vg04/lvtest01 is now 786432 blocks long.
[root@dirhost140 /]#lvreduce -L 3G /dev/vg04/lvtest01 WARNING: Reducing active logical volume to3.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want toreduce lvtest01? [y/n]: y Reducing logical volume lvtest01 to 3.00 GiB Logical volume lvtest01 successfully resized [root@dirhost140 /]#mount /dev/vg04/lvtest01 /aaa [root@dirhost140 /]#df -Th /dev/vg04/lvtest01 FilesystemTypeSizeUsed Avail Use% Mounted on /dev/mapper/vg04-lvtest01 ext43.0G136M2.7G5% /aaa
LVM高級應用
創建邏輯卷快照
LVM提供了快照功能可以用此技巧,靈活的使用磁盤空間以及備份文件系統,這裏用之前創建過的lvtest01 作快照
建立並讀取邏輯卷快照
[root@DirHost140 ~]#df -Th/dev/mapper/vg04-lvtest01 FilesystemTypeSizeUsed Avail Use% Mounted on /dev/mapper/vg04-lvtest01 ext43.0G136M2.7G5% /aaa [root@DirHost140 ~]# [root@DirHost140 ~]# lvcreate-L 300M -s -n backup1 /dev/vg04/lvtest01 Logical volume "backup1" created [root@DirHost140 ~]#umount /aaa/ [root@DirHost140 ~]#mount /dev/vg04/backup1 /aaa/ [root@DirHost140 ~]#df -Th /dev/mapper/vg04-backup1 FilesystemTypeSizeUsed Avail Use% Mounted on /dev/mapper/vg04-backup1 ext43.0G136M2.7G5% /aaa
雖然從上面看到新創建的快照backup1 已掛載到/aaa 目錄, 且顯示總共用的大小爲3G,可用空間爲2.7G。由於之前創建快照時只給了300M大小,因此次lv快照backup1最多隻能存儲300M 空間,當超過300M空間時,系統將會報錯;
[root@DirHost140 ~]#lvdisplay /dev/mapper/vg04-backup1 --- Logical volume --- LV Path/dev/vg04/backup1 LV Namebackup1 VG Namevg04 LV UUIDfJcyES-d1sG-IDio-dqmn-icTj-Ptrj-EiO31r LV Write Acce***ead/write LV Creation host, time DirHost140.cn.ibm.com,2013-10-11 17:33:13 +0800 LV snapshot statusactive destination for lvtest01 LV Statusavailable # open1 LV Size3.00 GiB Current LE768 COW-table size300.00 MiB COW-table LE75 Allocated to snapshot0.01% Snapshot chunk size4.00 KiB Segments1 Allocationinherit Read ahead sectorsauto - currently set to256 Block device253:29
當在此邏輯卷快照backup1存儲超過300M左右大小的文件,系統會提示I/O錯誤(這裏會出現短暫的延時),當我們再查看邏輯卷快照時,它的狀態將變爲inactive狀態,從而導致邏輯卷快照將被損毀,即不可用。同時自動umount /aaa 掛載,具體細節請見下列清單
[root@DirHost140 ~]#dd if=/dev/mapper/vg04-backup1 of=/aaa/300Mfile bs=30M count=10 10+0 records in 10+0 records out 314572800 bytes (315MB) copied, 3.07172 s, 102 MB/s Message fromsyslogd@DirHost140 at Oct 11 17:39:53 ... kernel:journal commit I/O error [root@DirHost140 /]#lvdisplay /dev/mapper/vg04-backup1 /dev/vg04/backup1: read failed after 0 of4096 at 3221159936: Input/output error /dev/vg04/backup1: read failed after 0 of4096 at 3221217280: Input/output error /dev/vg04/backup1: read failed after 0 of4096 at 0: Input/output error /dev/vg04/backup1: read failed after 0 of4096 at 4096: Input/output error --- Logical volume --- LV Path/dev/vg04/backup1 LV Namebackup1 VG Namevg04 LV UUIDfJcyES-d1sG-IDio-dqmn-icTj-Ptrj-EiO31r LV Write Acce***ead/write LV Creation host, time DirHost140.cn.ibm.com,2013-10-11 17:33:13 +0800 LV snapshot statusINACTIVE destination for lvtest01 LV Statusavailable # open0 LV Size3.00 GiB Current LE768 COW-table size300.00 MiB COW-table LE75 Snapshot chunk size4.00 KiB Segments1 Allocationinherit Read ahead sectorsauto - currently set to256 Block device253:29
即使邏輯卷快照能擴大其空間大小並激活它,但裏面的數據已破壞,已經失去備份文件的機制,相當於一個無效的快照文件,因此需要移除它。
[root@DirHost140 /]#lvchange -ay /dev/vg04/backup1 /dev/vg04/backup1: read failed after 0 of 4096at 3221159936: Input/output error /dev/vg04/backup1: read failed after 0 of4096 at 3221217280: Input/output error /dev/vg04/backup1: read failed after 0 of4096 at 0: Input/output error /dev/vg04/backup1: read failed after 0 of4096 at 4096: Input/output error Change of snapshotbackup1 will also change its origin lvtest01. Proceed? [y/n]: y [root@DirHost140 /]#lvdisplay /dev/vg04/backup1|grep 'snapshot status' /dev/vg04/backup1: read failed after 0 of4096 at 3221159936: Input/output error /dev/vg04/backup1: read failed after 0 of4096 at 3221217280: Input/output error /dev/vg04/backup1: read failed after 0 of4096 at 0: Input/output error /dev/vg04/backup1: read failed after 0 of4096 at 4096: Input/output error LV snapshot statusINACTIVE destination for lvtest01 [root@DirHost140 /]#mount /dev/vg04/backup1 /aaa mount: you mustspecify the filesystem type [root@DirHost140 /]#
卸載邏輯卷快照
快照也佔用了卷組的空間,因此適當的卸載未多大作用的快照 是非常有必要的.
[root@DirHost140 /]#lvremove /dev/vg04/backup1 /dev/vg04/backup1: read failed after 0 of4096 at 3221159936: Input/output error /dev/vg04/backup1: read failed after 0 of4096 at 3221217280: Input/output error /dev/vg04/backup1: read failed after 0 of4096 at 0: Input/output error /dev/vg04/backup1: read failed after 0 of4096 at 4096: Input/output error Do you really want toremove active logical volume backup1? [y/n]: y Logical volume "backup1"successfully removed [root@DirHost140 /]#
移動物理卷
用來實體磁盤的分區快要出故障了,或者你打算用較快SDD硬盤來換較慢的硬盤.這裏LVM採用pvremove 指令可以將某個物理卷的數據移動另一個物理捲上.
必須採取的步驟:
(1)加入新的物理捲到卷組中.
加入新的物理卷
[root@DirHost140 ~]# pvcreate /dev/sdd2 Physicl volume"/dev/sdd2"successfullycreated
擴展卷組大小
[root@DirHost140 ~]# vgextendvg04 /dev/sdd2 Volume group"vg04" successfully extended
查看卷組
通過pvscan可以發現新增的物理捲成功加入到卷組中了.
[root@DirHost140 ~]#pvscan PV /dev/sdd1VG vg04lvm2 [20.01 GiB / 17.01GiB free] PV /dev/sdd2VG vg04lvm2 [20.01 GiB / 20.01GiB free] #新增物理卷,未使用
移動物理卷數據到新加入的物理卷
新增的物理卷加入到卷組後就可以移動舊物理卷的數據了,這會用到指令pvmove 指令
進行數據的移動. 同時移動物理卷數據是一個高風險的行爲. 請在執行這個步驟前,務必備份好所有數據.
[root@DirHost140 ~]#pvmove /dev/sdd1 /dev/sdd2 /dev/sdd1: Moved: 0.5% /dev/sdd1: Moved: 26.2% /dev/sdd1: Moved: 50.4% /dev/sdd1: Moved: 75.1% /dev/sdd1: Moved: 99.0% /dev/sdd1: Moved: 100.0% [root@DirHost140 /]#pvscan PV /dev/sdd1VG vg04lvm2 [20.01 GiB / 20.01GiB free] PV /dev/sdd2VG vg04lvm2 [20.01 GiB / 17.01 GiB free] [root@DirHost140 ~]#umount /aaa [root@DirHost140 ~]#mount /dev/vg04/lvtest01 /aaa [root@DirHost140 ~]#cd /aaa/; ls
將舊的物理卷從物理卷中移除
通過以上事例的話就可以卸載原來(舊)物理卷,需要使用指令,pvremove 進行卸載工作.
在卸載物理卷前,我們必須先卸載卷組,纔可以進行. 按順序實施.
[root@DirHost140 /]#umount/aaa [root@DirHost140 /]#vgreduce vg04 /dev/sdd1 Removed "/dev/sdd1" from volumegroup "vg04" [root@DirHost140 /]#pvremove /dev/sdd1 Labelson physical volume "/dev/sdd1" successfully wiped [root@DirHost140 /]#pvscan PV /dev/sdd2VG vg04lvm2 [20.01 GiB / 17.01GiB free]
sdd1已經從卷組中卸載掉了,數據已經遷移到sdd2上了,並投入使用了。其數據不會丟失,在此查詢並掛載剛纔之前的lvtest01,可發現數據依然存在.
[root@DirHost140 /]#mount /dev/vg04/lvtest01 /aaa/ ; ls LAFix_getNativeUpd.shLAFix_SDD_param.txttest.sh [root@DirHost140 /]#df -Th /dev/mapper/vg04-lvtest01 FilesystemTypeSizeUsed Avail Use% Mounted on /dev/mapper/vg04-lvtest01 ext43.0G69M2.8G3% /aaa
遷移整個LVM磁盤系統至其他Linux系統
遷移LVM到另外一臺計算機中,通常採取三個步驟即可。第一步,在原先的計算機中導出卷組;第二步,將LVM的磁盤安裝到目的計算機上,第三步,在目的計算機中輸入卷組.
導出卷組
用到vgexport 指令,導出前卸載該卷組中的所有邏輯卷,並使用vgchange 將卷組修改爲非有效(Inactive)配置.
[root@DirHost140 ~]#umount /dev/vg04/lvtest01 [root@DirHost140 ~]#vgchange -a n vg04 0 logical volume(s) in volume group"vg04" now active [root@DirHost140 ~]#vgexport vg04^C [root@DirHost140 ~]#vgexport vg04 Volume group "vg04" successfullyexported
導出卷組後,就可以把該羣組的所有物理卷的磁盤安裝到目的計算機上了,即可實施導入卷組。
在另外一臺Linux導入卷組
在開始導入卷組前,確認先掃描下所有物理卷,讓RedHatLinux可以驅動這些物理卷.可以使用vgimport指令來導入卷組,導入後 修改有效的(Active)配置,纔可以掛載使用其文件系統.
[root@dirhost162 ~]#pvscan PV /dev/sdd2VG vg04lvm2 [20.01 GiB / 17.01GiB free] PV /dev/sdb1VG vg01lvm2 [1.17 TiB / 750.00GiB free] PV /dev/sdb2VG vg01lvm2 [500.01 GiB /500.01 GiB free] [root@dirhost162 ~]#vgimport vg04 Volume group "vg04" successfullyimported [root@dirhost162 ~]#vgchange -a y vg04#激活卷組 1 logical volume(s) in volume group"vg04" now active
驗證新導入卷組的數據
[root@dirhost162 ~]#mount /dev/vg04/lvtest01 /mnt [root@dirhost162 ~]#cd /mnt/;ls LAFix_getNativeUpd.shLAFix_SDD_param.txttest.sh
根據上面的實例可以看到,LVM具有很好的可伸縮性,使用起來非常方便,可以方便地對卷組、邏輯卷的大小進行調整,它也可結合羣集系統一起使用,也可以更好的發揮特性,同時通過本篇的文章可以掌握LVM使用技巧。