一. LVM簡介
二. LVM基本術語
三. 安裝LVM
四. 建立和管理LVM**app
五. ECS上LVM擴容實戰(磁盤擴容)ide
LVM是邏輯卷管理(Logical Volume Manager)的簡稱,它是Linux環境下對磁盤分區進行管理的一種機制,LVM是創建在硬盤和分區之上的邏輯層,來提升磁盤分區管理的靈活性。工具
LVM的工做原理其實很簡單,它就是經過將底層的物理磁盤抽象的封裝起來,而後以邏輯卷的方式呈現給上層應用。在傳統的磁盤管理機制中,咱們的上層應用是直接訪問文件系統,從而對底層的物理硬盤進行讀取,而在LVM中,其經過對底層的硬盤進行封裝,當咱們對底層的物理硬盤進行操做時,其再也不是針對於分區進行操做,而是經過邏輯捲來對其進行底層的磁盤管理操做。好比說我增長一個物理硬盤,這個時候上層的服務是感受不到的,由於呈現給上層服務的是以邏輯卷的方式。ui
LVM最大的特色就是能夠對磁盤進行動態管理。由於邏輯卷的大小是能夠動態調整的,並且不會丟失現有的數據。若是咱們新增長了硬盤,其也不會改變現有上層的邏輯卷。做爲一個動態磁盤管理機制,邏輯卷技術大大提升了磁盤管理的靈活性。spa
物理磁盤code
物理存儲介質(The physical media):這裏指系統的存儲設備:硬盤,如:/dev/xvda、/dev/vdb等等,是存儲系統最低層的存儲單元。orm
PV(Physical Volume)- 物理卷
物理卷在邏輯卷管理中處於最底層,它能夠是實際物理硬盤上的分區,也能夠是整個物理硬盤ip
VG(Volumne Group)- 卷組
卷組創建在物理卷之上,一個卷組中至少要包括一個物理卷,在卷組創建以後可動態添加物理捲到卷組中。一個邏輯卷管理系統工程中能夠只有一個卷組,也能夠擁有多個卷組。ci
LV(Logical Volume)- 邏輯卷
邏輯卷創建在卷組之上,卷組中的未分配空間能夠用於創建新的邏輯卷,邏輯卷創建後能夠動態地擴展和縮小空間。系統中的多個邏輯卷能夠屬於同一個卷組,也能夠屬於不一樣的多個卷組。
LVM使用分層結構,以下圖所示:rem
3.1 檢查系統內是否安裝了LVM管理工具
# rpm -qa |grep lvm lvm2-2.02.143-12.el6_9.1.x86_64 lvm2-libs-2.02.143-12.el6_9.1.x86_64 lvm2-devel-2.02.143-12.el6_9.1.x86_64注:目前線上的鏡像基本都集成了該工具
3.2 若是沒有安裝,可使用yum的方式安裝
# yum -y install lvm2*
4.1 建立一個 LVM 分區
fdisk的參數(n/p/1/回車/回車/t/8e/w)
# fdisk /dev/xvdb 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): p Disk /dev/xvdb: 53.7 GB, 53687091200 bytes 255 heads, 63 sectors/track, 6527 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xa2228253 Device Boot Start End Blocks Id System Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-6527, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-6527, default 6527): Using default value 6527 Command (m for help): t Selected partition 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): p Disk /dev/xvdb: 53.7 GB, 53687091200 bytes 255 heads, 63 sectors/track, 6527 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xa2228253 Device Boot Start End Blocks Id System /dev/xvdb1 1 6527 52428096 8e Linux LVM Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
注:8e表明是lvm的分區
Command (m for help): t Selected partition 1 Hex code (type L to list codes): L 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT- 4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx 5 Extended 42 SFS 86 NTFS volume set da Non-FS data 6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility 8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt 9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT f W95 Ext'd (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary 16 Hidden FAT16 64 Novell Netware af HFS / HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT 1e Hidden W95 FAT1
4.2 建立 PV
# pvcreate /dev/xvdb1 Physical volume "/dev/xvdb1" successfully created
查看 PV 信息,能夠看到有 30G 的容量
# pvdisplay --- NEW Physical volume --- PV Name /dev/xvdb1 VG Name PV Size 30.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID fuWBVL-laFd-0Bgm-q1TG-EzuP-xnet-7qwHEo
注:有些信息是後補的,所以可能會有先後信息不一致的狀況,請見諒
使用下列命令能夠刪除物理卷:
# pvremove /dev/xvdb1
4.3 建立 VG
# vgcreate vg1 /dev/xvdb1 Volume group "vg1" successfully created
查看卷組信息
# vgdisplay --- Volume group --- VG Name vg1 System ID Format lvm2 Metadata Areas 1 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 1 Act PV 1 VG Size 30.00 GiB PE Size 4.00 MiB Total PE 7679 Alloc PE / Size 0 / 0
從輸出中,咱們能夠看見卷組的使用量/總量,物理卷給卷組提供空間,只要在這個卷組中還有可用空間,咱們就能夠建立/擴展邏輯卷。
使用下列命令刪除卷組:
# vgremove vg1
4.4 建立 LV
# lvcreate -L 30G -n lv1 vg1 Volume group "vg1" has insufficient free space (7679 extents): 7680 required. Free PE / Size 7679 / 30.00 GiB VG UUID 7lh871-eolr-jXhX-OD3F-kwIo-s9nU-RtSwmh
vgdisplay看有30g,可是實際建立的時候報錯了,
free PE 7679 * pe size 4M /1024=29.99 ,先建立一個29G的lv出來再擴展試下
4.5 擴容 LV
# vgdisplay (看下剩下多少)
能夠看到還剩下1020M,添加1020M到lv1裏面
# lvextend -L +1020M /dev/vg1/lv1
注:這個擴容是VG自己還有餘量,給LV擴容餘量而非物理磁盤的擴容
4.6 格式化+掛載 LVM 分區
# mkfs.ext4 /dev/vg1/lv1
掛載 LVM
# mount /dev/vg1/lv1 /lvm
查看容量信息
# df -h
5.1 控制檯擴容磁盤(這裏不作詳細圖示了)
ECS 控制檯--實例列表--管理--本實例磁盤--更多--磁盤擴容
設置目標容量,肯定擴容
擴容完成後須要控制檯重啓一下ecs實例才能夠
注:若是是待掛載狀態則掛載後系統內看到的就是擴容後的容量了
5.2 登錄擴容
#fdisk -l
注:查看一下磁盤信息,能夠看到,數據盤已經擴容到50g,可是lvm分區仍是30g
Disk /dev/xvdb: 53.7 GB, 53687091200 bytes 255 heads, 63 sectors/track, 6527 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xa2228253 Device Boot Start End Blocks Id System /dev/xvdb1 1 3916 31455238+ 8e Linux LVM Disk /dev/mapper/vg1-lv1: 32.2 GB, 32208060416 bytes 255 heads, 63 sectors/track, 3915 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000
由於是物理磁盤級別的擴容,所以須要卸載掛載的分區,並取消分區卷組的激活
# umount /lvm # vgchange -an vg1 0 logical volume(s) in volume group "vg1" now active
注:取消激活後,能夠嘗試mount /dev/vg(tab鍵補充)就會發現已經沒有vg的路徑了
將擴容後的空閒磁盤建立一個分區出來
fdisk /dev/xvdb 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): p Disk /dev/xvdb: 53.7 GB, 53687091200 bytes 255 heads, 63 sectors/track, 6527 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xa2228253 Device Boot Start End Blocks Id System /dev/xvdb1 1 3916 31455238+ 8e Linux LVM Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (3917-6527, default 3917): Using default value 3917 Last cylinder, +cylinders or +size{K,M,G} (3917-6527, default 6527): Using default value 6527 Command (m for help): p Disk /dev/xvdb: 53.7 GB, 53687091200 bytes 255 heads, 63 sectors/track, 6527 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0xa2228253 Device Boot Start End Blocks Id System /dev/xvdb1 1 3916 31455238+ 8e Linux LVM /dev/xvdb2 3917 6527 20972857+ 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
將新增的分區加到卷組裏面
# vgextend vg1 /dev/xvdb2 Physical volume "/dev/xvdb2" successfully created Volume group "vg1" successfully extended
查看卷組信息能夠看到FREE PE多了出來
# vgdisplay --- Volume group --- VG Name vg1 System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 7 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 50.00 GiB PE Size 4.00 MiB Total PE 12799 Alloc PE / Size 7679 / 30.00 GiB Free PE / Size 5120 / 20.00 GiB VG UUID 9dBOCJ-KiSM-r6vK-pICt-n2tQ-fqBT-SLluiM
擴容前從新激活一下邏輯卷
# vgchange -ay vg1 1 logical volume(s) in volume group "vg1" now active
將這20G空間擴容到邏輯捲上
# lvextend -L +20G /dev/vg1/lv1 Size of logical volume vg1/lv1 changed from 30.00 GiB (7679 extents) to 50.00 GiB (12799 extents). Logical volume lv1 successfully resized.
查看卷組信息,已經都分配完了
# vgdisplay --- Volume group --- VG Name vg1 System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 8 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 50.00 GiB PE Size 4.00 MiB Total PE 12799 Alloc PE / Size 12799 / 50.00 GiB Free PE / Size 0 / 0 VG UUID 9dBOCJ-KiSM-r6vK-pICt-n2tQ-fqBT-SLluiM
重置一下邏輯卷的大小並掛載
# mount /dev/vg1/lv1 /lvm # resize2fs /dev/vg1/lv1 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/vg1/lv1 is mounted on /lvm; on-line resizing required old desc_blocks = 2, new_desc_blocks = 4 Performing an on-line resize of /dev/vg1/lv1 to 13106176 (4k) blocks. The filesystem on /dev/vg1/lv1 is now 13106176 blocks long. # df -Th Filesystem Type Size Used Avail Use% Mounted on /dev/xvda1 ext4 20G 18G 1.4G 93% / tmpfs tmpfs 498M 0 498M 0% /dev/shm /dev/mapper/vg1-lv1 ext4 50G 180M 47G 1% /lvm # lvdisplay --- Logical volume --- LV Path /dev/vg1/lv1 LV Name lv1 VG Name vg1 LV UUID 627piC-rDhg-M3eE-13XC-Ed8p-HCfs-PDY0c4 LV Write Access read/write LV Creation host, time lvm, 2018-03-21 07:21:18 +0000 LV Status available # open 0 LV Size 50.00 GiB Current LE 12799 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0
注:實際操做中若是df -h看不到擴容後的大小,看下resize2fs是否是有提示執行e2fsck的提示
# resize2fs /dev/vg1/lv1 resize2fs 1.41.12 (17-May-2010) Please run 'e2fsck -f /dev/vg1/lv1' first.
區別應是我前面掛載了因此直接resize成功,不掛載則須要檢查一下磁盤信息
詳情請閱讀原文