====== LVM (Logical Volume Management) 概念與基本語法 ====== +--------+ +--------+ | | | | | File | | File | | System | | System | | {s} | | {s} | +--------+ +--------+ | | +---+----+ +---+----+ | | | | | Logical| | Logical| | Volume | | Volume | | {s} | | {s} | +----+---+ +----+---+ | | +-+ +-+ | | +--+------------+--+ | | | Volume Group | | {s}| +---------+--------+ | +----------+----------+ | | | +----+---+ +----+---+ +----+---+ |Physical| |Physical| |Physical| | Volume | | Volume | | Volume | | {s}| | {s}| | {s}| +--------+ +--------+ +--------+ * Physical Volume (PV) : 就是實體區, 可以是一顆硬碟(Disk)或是一個分割區(Partition) 相關指令 : pvcreate, pvdisplay, pvs, pvchange, pvscan * Volume Group (VG) : 就是整合實體區(PV)與邏輯區(LV), 相關指令 : vgcreate, vgdisplay, vgs, vgextend, vgscan * Logical Volume(LV) : 就是邏輯區, 可以建立一個邏輯來格式化成檔案系統(File System), 相關指令 : lvcreate, lvdisplay, lvs, lvscan, lvresize, lvextend * 使用 LVM 的好處是讓檔案系統可以更有彈性的來調整, 不必然受限於實體的硬碟大小 * 例如原有硬碟(或分割)大小空間滿了, 想要加一顆硬碟(或分割)後, 讓原本資料目錄的空間變大, 以下是在 KVM 虛擬環境先將一顆 10G 的 QEMU 虛擬硬碟加上 10G (變成 20G)後, 透過一系列的程序讓 / 空間多出 10G 的作法 - 將加上的 10G 空間, 使用 parted 指令建立新的分割區 參考指令 print , mkpart primary , set <編號> lvm ++完成內容| (parted) print Model: QEMU QEMU HARDDISK (scsi) 磁碟 /dev/sda:21.5GB 磁區大小 (邏輯/物理):512B/512B 分割區:msdos 編號 起始點 結束點 大小 類型 檔案系統 旗標 1 1049kB 525MB 524MB primary ext4 啟動 2 525MB 10.7GB 10.2GB primary lvm 3 10.7GB 21.5GB 10.7GB primary lvm [root@pve-svn ~]# fdisk -l /dev/sda Disk /dev/sda: 21.5 GB, 21474836480 bytes 64 heads, 32 sectors/track, 20480 cylinders Units = cylinders of 2048 * 512 = 1048576 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000a3b61 Device Boot Start End Blocks Id System /dev/sda1 * 2 501 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 502 10240 9972736 8e Linux LVM Partition 2 does not end on cylinder boundary. /dev/sda3 10241 20480 10485760 8e Linux LVM Partition 3 does not end on cylinder boundary. ++ - 用 pvcreate 命令將新產生的分割區 /dev/sda3 建立為 PV ++執行語法| pvcreate /dev/sda3 [root@pve-svn ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name VolGroup PV Size 9.51 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 2434 Free PE 0 Allocated PE 2434 PV UUID 0gGDwt-cU4B-ohe4-zLG8-VB3V-SDzX-aUSQ6S "/dev/sda3" is a new physical volume of "10.00 GiB" --- NEW Physical volume --- PV Name /dev/sda3 VG Name PV Size 10.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID Zotmmo-VZng-6TfS-pJKA-k3TR-O87q-vAPurv [root@pve-svn ~]# pvscan PV /dev/sda2 VG VolGroup lvm2 [9.51 GiB / 0 free] PV /dev/sda3 lvm2 [10.00 GiB] Total: 2 [19.51 GiB] / in use: 1 [9.51 GiB] / in no VG: 1 [10.00 GiB] [root@pve-svn ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 VolGroup lvm2 a--u 9.51g 0 /dev/sda3 lvm2 ---- 10.00g 10.00g ++ - 將剛產生的 PV /dev/sda3 以 vgextend 命令來加入現有的 VG - VolGroup ++執行語法| [root@pve-svn ~]# vgdisplay --- Volume group --- VG Name VolGroup System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size 9.51 GiB PE Size 4.00 MiB Total PE 2434 Alloc PE / Size 2434 / 9.51 GiB Free PE / Size 0 / 0 VG UUID eK3ash-hbJh-yrjl-Gbci-8gyc-ueCE-4oVjiZ [root@pve-svn ~]# vgextend VolGroup /dev/sda3 Volume group "VolGroup" successfully extended [root@pve-svn ~]# vgdisplay --- Volume group --- VG Name VolGroup System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 5 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 2 Act PV 2 VG Size 19.50 GiB PE Size 4.00 MiB Total PE 4993 Alloc PE / Size 2434 / 9.51 GiB Free PE / Size 2559 / 10.00 GiB VG UUID eK3ash-hbJh-yrjl-Gbci-8gyc-ueCE-4oVjiZ [root@pve-svn ~]# vgs VG #PV #LV #SN Attr VSize VFree VolGroup 2 2 0 wz--n- 19.50g 10.00g ++ - 使用 lvextend 或 lvresize 來加大現有 lv_root 的 LV ++執行語法| [root@pve-svn ~]# lvdisplay --- Logical volume --- LV Path /dev/VolGroup/lv_root LV Name lv_root VG Name VolGroup LV UUID JbZGhn-aEuR-sohP-RSGc-FF09-hk9V-Qi5dXL LV Write Access read/write LV Creation host, time localhost.localdomain, 2013-05-23 11:46:40 +0800 LV Status available # open 1 LV Size 7.54 GiB Current LE 1930 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Logical volume --- LV Path /dev/VolGroup/lv_swap LV Name lv_swap VG Name VolGroup LV UUID 5SRhkK-6vuF-SAe8-g7P1-FhJp-E0YM-rrBcvj LV Write Access read/write LV Creation host, time localhost.localdomain, 2013-05-23 11:46:46 +0800 LV Status available # open 1 LV Size 1.97 GiB Current LE 504 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1 [root@pve-svn ~]# lvextend --size +10G /dev/VolGroup/lv_root Insufficient free space: 2560 extents needed, but only 2559 available [root@pve-svn ~]# lvextend -l +2560 /dev/VolGroup/lv_root Insufficient free space: 2560 extents needed, but only 2559 available [root@pve-svn ~]# lvextend -l +2559 /dev/VolGroup/lv_root Size of logical volume VolGroup/lv_root changed from 7.54 GiB (1930 extents) to 17.54 GiB (4489 extents). Logical volume lv_root successfully resized. [root@pve-svn ~]# lvresize -l +100%FREE /dev/VolGroup/lv_root [root@pve-svn ~]# vgs -o +lv_size,lv_name VG #PV #LV #SN Attr VSize VFree LSize LV VolGroup 2 2 0 wz--n- 19.50g 0 17.54g lv_root VolGroup 2 2 0 wz--n- 19.50g 0 1.97g lv_swap [root@pve-svn ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv_root VolGroup -wi-ao---- 17.54g lv_swap VolGroup -wi-ao---- 1.97g ++ - 透過 resize2fs 讓 LV 增加的空間實際生效 ++執行語法| [root@pve-svn ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 7.3G 1.8G 5.2G 26% / tmpfs 498M 0 498M 0% /dev/shm /dev/sda1 477M 126M 326M 28% /boot [root@pve-svn ~]# resize2fs /dev/VolGroup/lv_root resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required old desc_blocks = 1, new_desc_blocks = 2 Performing an on-line resize of /dev/VolGroup/lv_root to 4596736 (4k) blocks. The filesystem on /dev/VolGroup/lv_root is now 4596736 blocks long. [root@pve-svn ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 18G 1.8G 15G 12% / tmpfs 498M 0 498M 0% /dev/shm /dev/sda1 477M 126M 326M 28% /boot ++ ===== 參考網址 ===== * https://www.thegeekdiary.com/redhat-centos-a-beginners-guide-to-lvm-logical-volume-manager/ * https://www.digitalocean.com/community/tutorials/an-introduction-to-lvm-concepts-terminology-and-operations * https://www.thegeekdiary.com/centos-rhel-how-to-add-physical-volume-pv-to-a-volume-group-vg-in-lvm/ * https://pve.proxmox.com/wiki/Resize_disks {{tag>lvm}}