使用Linux
很久了,必定會意識到一個問題,某個分區容量不夠用了,想要擴容怎麼辦?這裏就涉及到LVM
邏輯卷的管理了,能夠動態調整Linux
分區容量。html
全稱Logical Volume Manager
,能夠動態調整磁盤容量,提升磁盤管理靈活性。node
在安裝CentOS 7
的過程當中選擇自動分區時,默認就是以LVM
的方案安裝的系統。linux
可是/boot
分區必須獨立出來,不能基於LVM
建立。vim
物理卷,Physical Volume
,是LVM
機制的基本存儲設備,一般對應一個普通分區或是整個硬盤。centos
建立物理卷時,會在分區或磁盤頭部建立一個用於記錄LVM
屬性的保留區塊,並把存儲空間分割成默認大小爲4MB
的基本單元(Physical Extend,PE),從而構成物理卷。安全
普通分區先轉換分區類型爲8e
;整塊硬盤,能夠將全部的空間劃分爲一個主分區再作調整。app
卷組,Volume Group
,是由一個或多個物理卷組成的一個總體。能夠動態添加、移除物理卷,建立時能夠指定PE
大小。dom
邏輯卷,Logical Volume
,創建在卷組之上,與物理卷沒有直接關係。格式化後,便可掛載使用。ide
經過以上對三者的解釋能夠看出,創建LVM
的過程。首先,將普通分區或整個硬盤建立爲物理卷;而後,將一個或多個物理卷建立爲卷組;最後,在卷組上分割不一樣的數據存儲空間造成邏輯卷。有了邏輯卷,就能夠格式化、掛載使用了。ui
功能 | PV 管理命令 | VG 管理命令 | LV 管理命令 |
---|---|---|---|
Scan(掃描) | pvscan | vgscan | lvscan |
Create(創建) | pvcreate | vgcreate | lvcreate |
Display(顯示) | pvdisplay | vgdisplay | lvdisplay |
Remove(移除) | pvremove | vgremove | lvremove |
Extend(擴展) | / | vgextend | lvextend |
Reduce(減小) | / | vgreduce | lvreduce |
-e
:僅顯示屬於輸出卷組的物理卷 -n
:僅顯示不屬於任何卷組的物理卷
[root@localhost ~]# pvscan PV /dev/sda2 VG centos lvm2 [<19.00 GiB / 0 free] PV /dev/sdb1 lvm2 [<20.00 GiB] PV /dev/sdc1 lvm2 [<20.00 GiB] Total: 3 [58.99 GiB] / in use: 1 [<19.00 GiB] / in no VG: 2 [<40.00 GiB]
[root@localhost ~]# pvcreate /dev/sd{b,c}1 Physical volume "/dev/sdb1" successfully created. Physical volume "/dev/sdc1" successfully created.
[root@localhost ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name centos PV Size <19.00 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 4863 Free PE 0 Allocated PE 4863 PV UUID E4eovQ-CgPo-OSDK-75MO-BfD8-pGsP-BUlqip "/dev/sdb1" is a new physical volume of "<20.00 GiB" --- NEW Physical volume --- PV Name /dev/sdb1 VG Name PV Size <20.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID gR5mm3-f50o-XLTq-34GY-n6Iz-WXkQ-uMZAkJ "/dev/sdc1" is a new physical volume of "<20.00 GiB" --- NEW Physical volume --- PV Name /dev/sdc1 VG Name PV Size <20.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID O2gKxa-ONts-kyXK-zZrO-3dDg-FhtI-V6GPy9
TIPS:只有不屬於卷組的物理卷才能夠刪除還原。
[root@localhost ~]# pvremove /dev/sdd1 Labels on physical volume "/dev/sdd1" successfully wiped.
LVM
卷組[root@localhost ~]# vgscan Reading volume groups from cache. Found volume group "class" using metadata type lvm2 Found volume group "centos" using metadata type lvm2
-l
:卷組上容許建立的最大邏輯卷數 -p
:卷組中容許添加的最大物理卷數 -s
:卷組上的物理卷的PE
大小
[root@localhost ~]# vgcreate class /dev/sd{b,c}1 Volume group "class" successfully created
[root@localhost ~]# vgdisplay --- Volume group --- VG Name class System ID Format lvm2 Metadata Areas 2 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 2 Act PV 2 VG Size 39.99 GiB PE Size 4.00 MiB Total PE 10238 Alloc PE / Size 0 / 0 Free PE / Size 10238 / 39.99 GiB VG UUID ZtQvGX-YOdy-U8wj-mGQs-TT1e-1PBR-dBNKAG --- Volume group --- VG Name centos 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 <19.00 GiB PE Size 4.00 MiB Total PE 4863 Alloc PE / Size 4863 / <19.00 GiB Free PE / Size 0 / 0 VG UUID bLPJGb-SQQ3-KcUb-OSdo-qaMR-9hJE-MTkc7a
TIPS:卷組下面的邏輯卷沒有被掛載使用,卷組刪除時會一併刪除下面的邏輯卷。
[root@localhost ~]# vgremove class Volume group "class" successfully removed
[root@localhost ~]# vgextend class /dev/sdd1 Volume group "class" successfully extended
-a
:若是命令行中沒有指定要刪除的物理卷,則刪除全部的空物理卷。(all)
TIPS:刪除啊前確保對應的物理卷沒有在使用,就是下面沒有邏輯卷,不然刪除不了。
[root@localhost ~]# vgreduce class /dev/sdd1 Removed "/dev/sdd1" from volume group "class"
[root@localhost ~]# vgreduce -a class Physical volume "/dev/sdb1" still in use Physical volume "/dev/sdc1" still in use Removed "/dev/sdd1" from volume group "class"
LVM
邏輯卷[root@localhost ~]# lvscan ACTIVE '/dev/class/stu01' [25.00 GiB] inherit ACTIVE '/dev/class/stu02' [14.99 GiB] inherit ACTIVE '/dev/centos/swap' [2.00 GiB] inherit ACTIVE '/dev/centos/root' [<17.00 GiB] inherit
-L
:指定邏輯卷的大小,單位爲KMGT
-l
:指定百分比建立,如50%vg
卷組的一半空間、100%free
全部剩下的。 -n
:指定邏輯卷的名稱
[root@localhost ~]# lvcreate -L 15G -n stu01 class Logical volume "stu01" created.
[root@localhost ~]# lvcreate -l 100%free -n stu02 class Logical volume "stu02" created.
[root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/class/stu01 LV Name stu01 VG Name class LV UUID XUP7dq-IM9f-PdKk-XpDH-J1hx-kkuB-cOu3Qx LV Write Access read/write LV Creation host, time localhost.localdomain, 2019-08-24 09:42:35 +0800 LV Status available # open 0 LV Size 25.00 GiB Current LE 6400 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 --- Logical volume --- LV Path /dev/class/stu02 LV Name stu02 VG Name class LV UUID P7iMC1-X0oT-6TCH-vLYj-HW2j-6QMu-rS7Jyp LV Write Access read/write LV Creation host, time localhost.localdomain, 2019-08-24 09:59:30 +0800 LV Status available # open 0 LV Size 14.99 GiB Current LE 3838 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:3 --- Logical volume --- LV Path /dev/centos/swap LV Name swap VG Name centos LV UUID NXiaXT-T6yv-ktM2-plyS-j6Hg-hkvu-1JQ2eL LV Write Access read/write LV Creation host, time localhost, 2019-08-16 17:12:19 +0800 LV Status available # open 2 LV Size 2.00 GiB Current LE 512 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:1 --- Logical volume --- LV Path /dev/centos/root LV Name root VG Name centos LV UUID 7FnsfT-B8xi-3vgu-8N0k-nzNn-8Rra-eoE4iC LV Write Access read/write LV Creation host, time localhost, 2019-08-16 17:12:19 +0800 LV Status available # open 1 LV Size <17.00 GiB Current LE 4351 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
TIPS:備份數據,解掛載,刪除。
[root@localhost ~]# umount /mnt/stu02/ [root@localhost ~]# lvremove /dev/class/stu02
LVM
邏輯卷的空間大小-L
:指定擴展邏輯卷的大小,單位爲KMGT
-l
:指定百分比擴建,如100%free
全部剩下的。
[root@localhost ~]# lvextend -L +8G /dev/class/stu01 [root@localhost ~]# lvextend -L +8G /dev/class/stu02
TIPS:已掛載,使用在線調整大小,xfs
使用xfs_growfs
,ext4
使用resize2fs
。
[root@localhost ~]# xfs_growfs /dev/class/stu01 [root@localhost ~]# resize2fs /dev/class/stu02
LVM
邏輯卷的空間大小,-L
:指定邏輯卷的大小,單位爲KMGT
TIPS:對於xfs
文件系統,備份數據,解掛載,減小空間,強制格式化,從新掛載。須要注意xfs
文件系統只能擴大,不能縮小,非要縮小的話,只有縮小後並強制格式化,但這麼作數據就丟失了。
[root@localhost ~]# umount /mnt/stu01/ [root@localhost ~]# lvreduce -L -10G /dev/class/stu01 [root@localhost ~]# mkfs.xfs -f /dev/class/stu01 [root@localhost ~]# mount -a [root@localhost ~]# df -hT
TIPS:對於ext4
這類文件系統,備份數據,解掛載,e2fsck -f
檢查文件系統完整性,resize2fs
設置文件系統大小,lvreduce
設置邏輯卷大小,從新掛載。
[root@localhost ~]# umount /mnt/stu02/ [root@localhost ~]# e2fsck -f /dev/class/stu02 [root@localhost ~]# resize2fs /dev/class/stu02 10G [root@localhost ~]# lvreduce -L 10G /dev/class/stu02 [root@localhost ~]# mount -a [root@localhost ~]# df -hT
1.添加兩塊20G
的磁盤,重啓機器,並對其進行分區,所有空間分爲一個主分區,類型改成Linux LVM
,ID爲8e
。分區過程省略,不清楚可參考上一篇博客。
[root@localhost ~]# fdisk -l /dev/sd{b,c} | grep -i "linux lvm" /dev/sdb1 2048 41943039 20970496 8e Linux LVM /dev/sdc1 2048 41943039 20970496 8e Linux LVM
2.將/dev/sdb1
、/dev/sdc1
轉換爲物理卷。
[root@localhost ~]# pvcreate /dev/sd{b,c}1 Physical volume "/dev/sdb1" successfully created. Physical volume "/dev/sdc1" successfully created.
3.建立名稱爲class
的卷組,並將sdb1
和sdc1
添加進卷組。
[root@localhost ~]# vgcreate class /dev/sd{b,c}1 Volume group "class" successfully created
4.劃分大小爲15G
,名稱爲stu01
的邏輯卷,而後再擴容10G
;剩下的空間建立名稱爲stu02
的邏輯卷。
[root@localhost ~]# lvcreate -L 15G -n stu01 class Logical volume "stu01" created. [root@localhost ~]# lvextend -L +10G /dev/class/stu01 Size of logical volume class/stu01 changed from 15.00 GiB (3840 extents) to 25.00 GiB (6400 extents). Logical volume class/stu01 successfully resized.
[root@localhost ~]# lvcreate -l 100%free -n stu02 class Logical volume "stu02" created.
5.格式化邏輯卷,stu01
爲xfs
,stu02
爲ext4
,設置自動掛載,掛載後就可使用了。
[root@localhost ~]# mkfs.xfs /dev/class/stu01 meta-data=/dev/class/stu01 isize=512 agcount=4, agsize=1638400 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=6553600, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=3200, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# mkfs.ext4 /dev/class/stu02 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 983040 inodes, 3930112 blocks 196505 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2151677952 120 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 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
[root@localhost ~]# mkdir /mnt/stu0{1,2} [root@localhost ~]# vim /etc/fstab //最後面添加下面兩行內容 /dev/class/stu01 /mnt/stu01 xfs defaults 0 0 /dev/class/stu02 /mnt/stu02 ext4 defaults 0 0
[root@localhost ~]# mount -a [root@localhost ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/centos-root xfs 17G 927M 17G 6% / devtmpfs devtmpfs 901M 0 901M 0% /dev tmpfs tmpfs 912M 0 912M 0% /dev/shm tmpfs tmpfs 912M 8.7M 904M 1% /run tmpfs tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 xfs 1014M 143M 872M 15% /boot tmpfs tmpfs 183M 0 183M 0% /run/user/0 /dev/mapper/class-stu01 xfs 25G 33M 25G 1% /mnt/stu01 /dev/mapper/class-stu02 ext4 15G 41M 14G 1% /mnt/stu02
1.再添加一塊20G的磁盤,重啓機器,並對其進行分區,所有空間分爲一個主分區,類型改成Linux LVM
,ID爲8e
。
[root@localhost ~]# fdisk -l /dev/sd{b..d} | grep -i "linux lvm" /dev/sdb1 2048 41943039 20970496 8e Linux LVM /dev/sdc1 2048 41943039 20970496 8e Linux LVM /dev/sdd1 2048 41943039 20970496 8e Linux LVM
2.將/dev/sdd1
轉換爲物理卷。
[root@localhost ~]# pvcreate /dev/sdd1 Physical volume "/dev/sdd1" successfully created.
3.如果卷組剩餘空間不夠,就要先對class
卷組進行擴容。
[root@localhost ~]# vgdisplay class --- Volume group --- VG Name class System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 6 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 39.99 GiB PE Size 4.00 MiB Total PE 10238 Alloc PE / Size 10238 / 39.99 GiB Free PE / Size 0 / 0 VG UUID iB1h9h-2uQa-rD7o-GBVK-pOCA-ek2c-TXieLr
[root@localhost ~]# vgextend class /dev/sdd1 Volume group "class" successfully extended
[root@localhost ~]# vgdisplay class --- Volume group --- VG Name class System ID Format lvm2 Metadata Areas 3 Metadata Sequence No 7 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 3 Act PV 3 VG Size <59.99 GiB PE Size 4.00 MiB Total PE 15357 Alloc PE / Size 10238 / 39.99 GiB Free PE / Size 5119 / <20.00 GiB VG UUID iB1h9h-2uQa-rD7o-GBVK-pOCA-ek2c-TXieLr
4.對stu01
和stu02
都擴容8G
,分別使用xfs_growfs
和resize2fs
在線調整大小。
[root@localhost ~]# lvextend -L +8G /dev/class/stu01 Size of logical volume class/stu01 changed from 25.00 GiB (6400 extents) to 33.00 GiB (8448 extents). Logical volume class/stu01 successfully resized.
[root@localhost ~]# lvextend -L +8G /dev/class/stu02 Size of logical volume class/stu02 changed from 14.99 GiB (3838 extents) to 22.99 GiB (5886 extents). Logical volume class/stu02 successfully resized.
[root@localhost ~]# xfs_growfs /dev/class/stu01 meta-data=/dev/mapper/class-stu01 isize=512 agcount=4, agsize=1638400 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=6553600, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=3200, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 6553600 to 8650752
[root@localhost ~]# resize2fs /dev/class/stu02 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/class/stu02 is mounted on /mnt/stu02; on-line resizing required old_desc_blocks = 2, new_desc_blocks = 3 The filesystem on /dev/class/stu02 is now 6027264 blocks long.
[root@localhost ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/centos-root xfs 17G 927M 17G 6% / devtmpfs devtmpfs 901M 0 901M 0% /dev tmpfs tmpfs 912M 0 912M 0% /dev/shm tmpfs tmpfs 912M 8.7M 903M 1% /run tmpfs tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 xfs 1014M 143M 872M 15% /boot /dev/mapper/class-stu01 xfs 33G 33M 33G 1% /mnt/stu01 /dev/mapper/class-stu02 ext4 23G 44M 22G 1% /mnt/stu02 tmpfs tmpfs 183M 0 183M 0% /run/user/0
關於收縮 LVM 邏輯卷,能夠參考上面的命令lvreduce
。這會涉及到數據安全性問題,謹慎使用。
內容很少,可是還要實際操做才能瞭解。就是三步走,建立物理卷,建立卷組,建立邏輯卷,而後格式化並掛載使用。擴容的話,沒有掛載使用,就正常擴,而後格式化掛載,已經掛載使用了,擴容後在線調整生效。還有下一篇關於磁盤配額。