Linux LVM邏輯卷
需求:伺服器新新增一個掛載盤,為防止以後儲存空間不夠需要擴容時格式化磁碟,故使用LVM邏輯卷管理。
LVM簡介:
LVM是 Logical Volume Manager(邏輯卷管理)的簡寫,它是Linux環境下對磁碟分割槽進行管理的一種機制,它由Heinz Mauelshagen在Linux 2.4核心上實現,目前最新版本為:穩定版1.0.5,開發版 1.1.0-rc2,以及LVM2開發版。Linux使用者安裝Linux作業系統時遇到的一個常見的難以決定的問題就是如何正確地評估各分割槽大小,以分配合適的硬碟空間。普通的磁碟分割槽管理方式在邏輯分區劃分好之後就無法改變其大小,當一個邏輯分割槽存放不下某個檔案時,這個檔案因為受上層檔案系統的限制,也不能跨越多個分割槽來存放,所以也不能同時放到別的磁碟上。而遇到出現某個分割槽空間耗盡時,解決的方法通常是使用
LVM基本術語:
物理儲存介質(The physical media):這裡指系統的儲存裝置:硬碟,如:/dev/hda1、/dev/sda等等,是儲存系統最低層的儲存單元。
物理卷(physical volume):物理卷就是指硬碟分割槽或從邏輯上與磁碟分割槽具有同樣功能的裝置(如RAID),是LVM的基本儲存邏輯塊,但和基本的物理儲存介質(如分割槽、磁碟等)比較,卻包含有與LVM相關的管理引數。
卷組(Volume Group):LVM卷組類似於非LVM系統中的物理硬碟,其由物理卷組成。可以在卷組上建立一個或多個“LVM分割槽”(邏輯卷),LVM卷組由一個或多個物理卷組成。
邏輯卷(logical volume):LVM的邏輯卷類似於非LVM系統中的硬碟分割槽,在邏輯卷之上可以建立檔案系統(比如/home或者/usr等)。
PE(physical extent):每一個物理卷被劃分為稱為PE(Physical Extents)的基本單元,具有唯一編號的PE是可以被LVM定址的最小單元。PE的大小是可配置的,預設為4MB。
LE(logical extent):邏輯卷也被劃分為被稱為LE(Logical Extents) 的可被定址的基本單位。在同一個卷組中,LE的大小和PE是相同的,並且一一對應。
簡單來說就是:
PV:是物理的磁碟分割槽
VG:LVM中的物理的磁碟分割槽,也就是PV,必須加入VG,可以將VG理解為一個倉庫或者是幾個大的硬碟。
LV:也就是從VG中劃分的邏輯分割槽
1. 建立分割槽【8e為LVM分割槽格式】
[root@cqsjxw-nydsjpt01 ~]# fdisk /dev/vdc Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x72334656. Command (m for help): p Disk /dev/vdc: 1073.7 GB, 1073741824000 bytes, 2097152000 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x72334656 Device Boot Start End Blocks Id System Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-2097151999, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-2097151999, default 2097151999): Using default value 2097151999 Partition 1 of type Linux and of size 1000 GiB is set Command (m for help): t Selected partition 1 Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM' Command (m for help): p Disk /dev/vdc: 1073.7 GB, 1073741824000 bytes, 2097152000 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x72334656 Device Boot Start End Blocks Id System /dev/vdc1 2048 2097151999 1048574976 8e Linux LVM Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
2. 建立PV
[root@cqsjxw-nydsjpt01 ~]# pvcreate /dev/vdc1 Physical volume "/dev/vdc1" successfully created. [root@cqsjxw-nydsjpt01 ~]# pvs PV VG Fmt Attr PSize PFree /dev/vda3 centos7 lvm2 a-- 39.80g 0 /dev/vdc1 lvm2 --- <1000.00g <1000.00g [root@cqsjxw-nydsjpt01 ~]# pvdisplay --- Physical volume --- PV Name /dev/vda3 VG Name centos7 PV Size 39.80 GiB / not usable 0 Allocatable yes (but full) PE Size 4.00 MiB Total PE 10189 Free PE 0 Allocated PE 10189 PV UUID VVXMV7-zVhi-NSVz-frrY-YKFg-BoJH-ir33p3 "/dev/vdc1" is a new physical volume of "<1000.00 GiB" --- NEW Physical volume --- PV Name /dev/vdc1 VG Name PV Size <1000.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID N6wQJb-K6Au-wto7-SR2O-jGrD-oZtQ-IcRlLI
3. 建立VG
[root@cqsjxw-nydsjpt01 ~]# vgcreate vgdata /dev/vdc1 Volume group "vgdata" successfully created [root@cqsjxw-nydsjpt01 ~]# vgs VG #PV #LV #SN Attr VSize VFree centos7 1 1 0 wz--n- 39.80g 0 vgdata 1 0 0 wz--n- <1000.00g <1000.00g [root@cqsjxw-nydsjpt01 ~]# vgdisplay --- Volume group --- VG Name vgdata 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 <1000.00 GiB PE Size 4.00 MiB Total PE 255999 Alloc PE / Size 0 / 0 Free PE / Size 255999 / <1000.00 GiB VG UUID MZjnmV-Gukd-XaH2-76jQ-wjik-LaWp-yqoHTQ --- Volume group --- VG Name centos7 System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 2 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 1 Max PV 0 Cur PV 1 Act PV 1 VG Size 39.80 GiB PE Size 4.00 MiB Total PE 10189 Alloc PE / Size 10189 / 39.80 GiB Free PE / Size 0 / 0 VG UUID QTfxI7-AXVG-dGoO-zCQ8-wsFb-QlOe-HPSWPI
4. 物理卷儲存劃分給新的邏輯卷
[root@cqsjxw-nydsjpt01 ~]# lvcreate -L 999.90G -n lvdata01 vgdata Rounding up size to full physical extent 999.90 GiB Logical volume "lvdata01" created. [root@cqsjxw-nydsjpt01 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root centos7 -wi-ao---- 39.80g lvdata01 vgdata -wi-a----- 999.90g [root@cqsjxw-nydsjpt01 ~]# lvdisplay --- Logical volume --- LV Path /dev/vgdata/lvdata01 LV Name lvdata01 VG Name vgdata LV UUID r83fGv-U3ju-Yn9k-yuXN-pEK6-ltTL-eSB2AO LV Write Access read/write LV Creation host, time cqsjxw-nydsjpt01, 2021-07-29 11:11:09 +0800 LV Status available # open 0 LV Size 999.90 GiB Current LE 255975 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:1 --- Logical volume --- LV Path /dev/centos7/root LV Name root VG Name centos7 LV UUID npjBgn-XPHe-dNS2-bJGd-kYp2-RayO-dWttAc LV Write Access read/write LV Creation host, time localhost, 2018-06-30 14:35:21 +0800 LV Status available # open 1 LV Size 39.80 GiB Current LE 10189 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
5. 格式化邏輯卷檔案系統
[root@cqsjxw-nydsjpt01 ~]# mkfs.ext4 /dev/vgdata/lvdata01 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 65536000 inodes, 262118400 blocks 13105920 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2409627648 8000 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, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 102400000, 214990848 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
6. 掛載邏輯卷
[root@cqsjxw-nydsjpt01 ~]# mount /dev/vgdata/lvdata01 /home [root@cqsjxw-nydsjpt01 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos7-root 40G 2.4G 38G 6% / devtmpfs 32G 0 32G 0% /dev tmpfs 32G 56K 32G 1% /dev/shm tmpfs 32G 282M 32G 1% /run tmpfs 32G 0 32G 0% /sys/fs/cgroup /dev/vda2 197M 120M 77M 61% /boot tmpfs 6.3G 0 6.3G 0% /run/user/0 /dev/mapper/vgdata-lvdata01 985G 77M 934G 1% /home [root@cqsjxw-nydsjpt01 ~]# cat /etc/fstab UUID=97f655c8-0ae8-489c-90be-594feaceaa27 /home ext4 defaults 0 0
7. 擴充套件邏輯卷【轉載】
給邏輯卷增加空間並不會影響以前空間的使用,所以無需解除安裝檔案系統,直接通過命令lvextend –L +500M /dev/vgdata/lvdata01或者lvextend –l 2.5G /dev/vgdata/lvdata01 給lvdata01增加500M空間(lvdata01目前是15G空間)設定完成之後,記得使用resize2fs命令來同步檔案系統。
[root@DB02 ~]# lvextend -L +500M /dev/vgdata/lvdata01 Extending logical volume lvdata01 to 15.49 GiB Logical volume lvdata01 successfully resized [root@DB02 ~]# lvs LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert lvdata01 vgdata -wi-ao---- 15.49g [root@DB02 ~]# resize2fs /dev/vgdata/lvdata01 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/vgdata/lvdata01 is mounted on /u01; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/vgdata/lvdata01 to 4060160 (4k) blocks. The filesystem on /dev/vgdata/lvdata01 is now 4060160 blocks long.
參考文章: