在我們使用Linux的過(guò)程中難免會(huì)對(duì)現(xiàn)有的RAID磁盤陣列組的硬盤管理空間進(jìn)行增加、減小操作等,如果當(dāng)初安裝系統(tǒng)時(shí)劃分的硬盤空間沒(méi)有考慮到今后的硬盤有可能會(huì)進(jìn)行調(diào)整的話,我們就要重新規(guī)劃并重裝操作系統(tǒng),以滿足應(yīng)用需求,這時(shí)Linux給我提供了一項(xiàng)硬盤設(shè)備管理技術(shù)LVM邏輯卷管理器(Logical Volume Manager)通過(guò)它,我們就可以實(shí)現(xiàn)對(duì)硬盤空間的動(dòng)態(tài)劃分和調(diào)整。

以下是LVM一些名詞的解釋,解釋來(lái)自百度LVM:
PhysicalStorageMedia 物理存儲(chǔ)設(shè)備
指系統(tǒng)的物理存儲(chǔ)設(shè)備:磁盤,如:/dev/hda、/dev/sda等,是存儲(chǔ)系統(tǒng)最底層的存儲(chǔ)單元。
PV(Physical Volume)物理卷
指磁盤分區(qū)或從邏輯上與磁盤分區(qū)具有同樣功能的設(shè)備,是LVM的基本存儲(chǔ)邏輯塊,但和基本的物理存儲(chǔ)介質(zhì)相比,卻包含有與LVM相關(guān)的管理參數(shù)。
VG(Volume Group)卷組
類似于非LVM系統(tǒng)中的物理磁盤,其由一個(gè)或多個(gè)物理卷PV組成,可以在卷組上創(chuàng)建一個(gè)或多個(gè)LV。
LV(Logical Volume)邏輯卷
類似于非LVM系統(tǒng)中的磁盤分區(qū),邏輯卷建立在卷組VG之上,在邏輯卷LV之上可以建立文件系統(tǒng)。
PE(Physical Extent)物理塊
每一個(gè)物理卷PV被劃分為稱為PE的基本單元,具有唯一編號(hào)的PE是可以被LVM尋址的最小單元,PE的大小是可以配置的,默認(rèn)為4MB。所以物理卷(PV)大小等同的基本單元PE組成。
LE(Logical Extent)邏輯塊
邏輯卷LV也被劃分為可被尋址的基本單位,稱為L(zhǎng)E。在同一個(gè)卷組中,LE的大小和PE是相同的,并且一一對(duì)應(yīng)。
部署實(shí)踐
1、為實(shí)驗(yàn)準(zhǔn)備2塊20G大小的硬盤

如果不想在虛擬環(huán)境下重啟識(shí)別新硬盤的話,可以執(zhí)行下面的命令---
echo "- - -" > /sys/class/scsi_host/host0/scan
注意:三個(gè)- - -號(hào)之間有空隔。
2、將新添加的硬盤添加支持LVM邏輯卷管理中:
[root@CentOS7 ~]#pvcreate /dev/sdb /dev/sdc
Physical volume "/dev/sdb" successfully created.
Physical volume "/dev/sdc" successfully created.
3、創(chuàng)建storage卷組,將新添加的兩塊硬盤設(shè)備加入到卷組中,查看卷組狀態(tài):
[root@CentOS7 ~]#vgcreate storage /dev/sdb /dev/sdc ##添加入卷組
Volume group "storage" successfully created
[root@CentOS7 ~]#vgdisplay
--- Volume group ---
VG Name storage ##創(chuàng)建“storage”名稱
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 v8N8fM-s6x2-mTGN-AiCC-KGhK-uz9D-4w8LVc
4、劃分出200M的邏輯設(shè)備:
在對(duì)邏輯卷劃分中,如果我們使用-l參數(shù)來(lái)指定使用PE基本單元的個(gè)數(shù),或者也可以使用常見(jiàn)以-L參數(shù)來(lái)以容量為單位劃分。
[root@CentOS7 ~]#lvcreate -n lv -L 200 storage ##200M大小的邏輯設(shè)備
Logical volume "lv" created.
[root@CentOS7 ~]#lvdisplay ##查看邏輯卷信息
--- Logical volume ---
LV Path /dev/storage/lv
LV Name lv
VG Name storage
LV UUID ccH2lD-QUvS-IiUX-gc7K-kkE7-9MDD-UuMhmV
LV Write Access read/write
LV Creation host, time CentOS7.laishaohua, 2017-08-20 20:27:40 +0800
LV Status available
# open 0
LV Size 200.00 MiB ##大小為200M大小
Current LE 50
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
5、將邏輯卷格式為mkfs.xfs后掛載使用:
[root@CentOS7 ~]#mkfs.xfs /dev/storage/lv ##格式化
meta-data=/dev/storage/lv isize=512 agcount=4, agsize=12800 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=51200, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@CentOS7 ~]#mkdir /lvmstorage ##創(chuàng)建掛載目錄
[root@CentOS7 ~]#mount /dev/storage/lv /lvmstorage ##掛載
6、查看掛載狀態(tài),并將掛載狀態(tài)寫(xiě)入配置文件使其重啟掛載信息不丟失
[root@CentOS7 ~]#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 50G 1.2G 49G 3% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.7M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda3 40G 33M 40G 1% /app
/dev/sr0 7.8G 7.8G 0 100% /mnt/cdrom
/dev/sda1 1014M 116M 899M 12% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/storage-lv 197M 11M 187M 6% /lvmstorage ##卷狀態(tài)
[root@CentOS7 ~]#echo "/dev/storage/lv /lvmstorage xfs defaults 0 0" >> /etc/fstab ##echo 寫(xiě)入配置文件
7、縮小邏輯卷
注意:對(duì)邏輯卷縮小操作不當(dāng)將會(huì)有丟失數(shù)據(jù)的危險(xiǎn),所以在生產(chǎn)環(huán)境中不得已需要縮小邏輯卷的話,也記得將重要的數(shù)據(jù)提前備份,以防數(shù)據(jù)丟失。另外對(duì)LVM邏輯卷縮小操作前需要檢查文件系統(tǒng)的完整性,確保數(shù)據(jù)安全。
此外由于為mkfs.xfs格式系統(tǒng),xfs默認(rèn)只能擴(kuò)大不能縮小,所以我們需要利用xfsdump / xfsrestore 來(lái)實(shí)現(xiàn)縮小,縮小后需要重新格式化才能掛載。(適用系統(tǒng)剛安裝好,邏輯分區(qū)內(nèi)沒(méi)有什么數(shù)據(jù)或數(shù)據(jù)不多且不重要的情況下)
[root@CentOS7 ~]#rpm -qa xfsdump ##查看系統(tǒng)有無(wú)xfsdump
xfsdump-3.1.4-1.el7.x86_64
[root@CentOS7 ~]#yum -y install xfsdump ##安裝xfsdump套件
[root@CentOS7 ~]#umount /lvmstorage/ ##卸載設(shè)備和掛載點(diǎn)的關(guān)聯(lián)
檢查文件系統(tǒng)的完整性:
[root@CentOS7 ~]#e2fsck -f /dev/storage/lv ##e2fsck -f 強(qiáng)制檢查
e2fsck 1.42.9 (28-Dec-2013)
ext2fs_open2: Bad magic number in super-block
e2fsck: Superblock invalid, trying backup blocks...
e2fsck: Bad magic number in super-block while trying to open /dev/storage/lv
The superblock could not be read or does not describe a correct ext2
filesystem. If the device is valid and it really contains an ext2
filesystem (and not swap or ufs or something else), then the superblock
is corrupt, and you might try running e2fsck with an alternate superblock:
e2fsck -b 8193 <device>
Linux e2fsck命令用于檢查使用 Linux ext2 檔案系統(tǒng)的 partition 是否正常工作。
將LV邏輯卷的容量減小到150M:
[root@CentOS7 ~]#lvreduce -L 150M /dev/storage/lv
Rounding size to boundary between physical extents: 152.00 MiB.
WARNING: Reducing active logical volume to 152.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce storage/lv? [y/n]: y
Size of logical volume storage/lv changed from 200.00 MiB (50 extents) to 152.00 MiB (38 extents).
Logical volume storage/lv successfully resized.
減小后將邏輯分區(qū)重新通過(guò)mkfs.xfs命令重新格式化才能掛載上
[root@CentOS7 ~]#mkfs.xfs -f /dev/storage/lv ##格式化
將文件系統(tǒng)重新掛載并查看硬盤狀態(tài):
[root@CentOS7 ~]#mount -a
[root@CentOS7 ~]#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 50G 1.2G 49G 3% /
devtmpfs 479M 0 479M 0% /dev
tmpfs 489M 0 489M 0% /dev/shm
tmpfs 489M 6.7M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda3 40G 33M 40G 1% /app
/dev/sr0 7.8G 7.8G 0 100% /mnt/cdrom
/dev/sda1 1014M 116M 899M 12% /boot
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/storage-lv 149M 7.8M 141M 6% /lvmstorage ##縮小至150M
8、擴(kuò)容邏輯卷,只要卷組中的空間足夠大就可以一直為邏輯卷擴(kuò)容。
將邏輯卷lv擴(kuò)展至300M:
[root@CentOS7 ~]#lvextend -L 300M /dev/storage/lv ##擴(kuò)展至300M
Size of logical volume storage/lv changed from 152.00 MiB (38 extents) to 300.00 MiB (75 extents).
Logical volume storage/lv successfully resized.
[root@CentOS7 ~]#xfs_growfs /dev/storage/lv
xfs_growfs: /dev/storage/lv is not a mounted XFS filesystem
[root@CentOS7 ~]#xfs_growfs /dev/storage/lv ##目標(biāo)XFS文件系統(tǒng)來(lái)擴(kuò)展
meta-data=/dev/mapper/storage-lv isize=512 agcount=4, agsize=9728 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=38912, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=855, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 38912 to 76800
df -Th查看分區(qū)空間
[root@CentOS7 ~]#df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 xfs 50G 1.2G 49G 3% /
devtmpfs devtmpfs 479M 0 479M 0% /dev
tmpfs tmpfs 489M 0 489M 0% /dev/shm
tmpfs tmpfs 489M 6.7M 482M 2% /run
tmpfs tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda3 xfs 40G 33M 40G 1% /app
/dev/sr0 iso9660 7.8G 7.8G 0 100% /mnt/cdrom
/dev/sda1 xfs 1014M 116M 899M 12% /boot
tmpfs tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/storage-lv xfs 297M 7.9M 289M 3% /lvmstorage ##擴(kuò)展至300M
9、邏輯卷快照建立
在對(duì)邏輯卷管理中我們還可以建立快照卷功能,這項(xiàng)功能類似于VMware中的快照功能,提供數(shù)據(jù)糾錯(cuò)還原功能。
往邏輯卷組設(shè)備所掛載的目錄用dd寫(xiě)入一個(gè)文件20M的文件:
[root@CentOS7 ~]#dd if=/dev/zero of=/lvmstorage/readme bs=1M count=20
20+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.0348104 s, 602 MB/s
[root@CentOS7 /lvmstorage]#ll
total 20480
-rw-r--r--. 1 root root 20971520 Aug 20 15:18 readme
使用-s參數(shù)生成一個(gè)快照卷,-L參數(shù)指定劃分的大小:
[root@CentOS7 ~]#lvcreate -L 150M -s -n SNAP /dev/storage/lv
Using default stripesize 64.00 KiB.
Rounding up size to full physical extent 152.00 MiB
Logical volume "SNAP" created.
[root@CentOS7 ~]#lvdisplay
--- Logical volume ---
LV Path /dev/storage/SNAP
LV Name SNAP
VG Name storage
LV UUID 3Ftw8X-dqir-EEfA-ZAD9-KQjD-lQAS-np4Yn9
LV Write Access read/write
LV Creation host, time CentOS7.laishaohua, 2017-08-20 15:23:10 +0800
LV snapshot status active destination for lv
LV Status available
# open 0
LV Size 300.00 MiB
Current LE 75
COW-table size 152.00 MiB
COW-table LE 38
Allocated to snapshot 0.00%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:3
在LV設(shè)備卷所掛載的目錄中創(chuàng)建一個(gè)100M的二進(jìn)制文件,再來(lái)查看邏輯卷狀態(tài):
[root@CentOS7 ~]#dd if=/dev/zero of=/lvmstorage/files count=1 bs=100M
1+0 records in
1+0 records out
104857600 bytes (105 MB) copied, 3.13618 s, 33.4 MB/s
[root@CentOS7 ~]#lvdisplay
--- Logical volume ---
LV Path /dev/storage/SNAP
LV Name SNAP
VG Name storage
LV UUID 3Ftw8X-dqir-EEfA-ZAD9-KQjD-lQAS-np4Yn9
LV Write Access read/write
LV Creation host, time CentOS7.laishaohua, 2017-08-20 15:23:10 +0800
LV snapshot status active destination for lv
LV Status available
# open 0
LV Size 300.00 MiB
Current LE 75
COW-table size 152.00 MiB
COW-table LE 38
Allocated to snapshot 48.64% ##邏輯卷上升
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:3
檢驗(yàn)SNAP快照效果,對(duì)邏輯卷進(jìn)行快照合并還原,(先卸載邏輯設(shè)備目錄的掛載)
[root@CentOS7 ~]#lvconvert --merge /dev/storage/SNAP
Merging of volume storage/SNAP started.
lv: Merged: 39.23%
lv: Merged: 100.00%
[root@CentOS7 ~]#mount -a
[root@CentOS7 ~]#ls /lvmstorage/ ##邏輯卷設(shè)備被快照后創(chuàng)建的100M文件也被還原了
readme
10、刪除邏輯卷
當(dāng)不再需要使用LVM邏輯卷管理器時(shí),我們可以依次安裝順序刪除邏輯卷
取消邏輯卷與目錄的掛載關(guān)聯(lián),并刪除 /etc/fstab配置文件中的設(shè)備刪除:
[root@CentOS7 ~]#umount /lvmstorage/
[root@CentOS7 ~]#sed -i '$d' /etc/fstab ##因?yàn)樵O(shè)備文件在最后一行,我們直接用sed -i執(zhí)行刪除
將LV邏輯卷設(shè)備刪除,需要屬y確認(rèn)操作:
[root@CentOS7 ~]#lvremove /dev/storage/lv
Do you really want to remove active logical volume storage/lv? [y/n]: y
Logical volume "lv" successfully removed
將VG卷組刪除:
[root@CentOS7 ~]#vgremove storage
Volume group "storage" successfully removed
最后將PV物理卷設(shè)備移除:
[root@CentOS7 ~]#pvremove /dev/sdb /dev/sdc
Labels on physical volume "/dev/sdb" successfully wiped.
Labels on physical volume "/dev/sdc" successfully wiped.
確認(rèn)上述操作執(zhí)行成功,無(wú)提示信息則為操作正確完成邏輯卷設(shè)備移除:
[root@CentOS7 ~]#lvdisplay ;vgdisplay ;pvdisplay
總結(jié):
- 在對(duì)邏輯卷大小調(diào)整時(shí),針對(duì)xfs和ext4不同的文件系統(tǒng)中,所使用的命令都不相同
resize2fs命令 針對(duì)的是ext2、ext3、ext4文件系統(tǒng)
xfs_growfs命令 針對(duì)的是xfs文件系統(tǒng) - xfs文件系統(tǒng)只支持增大分區(qū)空間的情況,不支持縮小,要減小的話,只能在減小后將邏輯分區(qū)重新通過(guò)mkfs.xfs命令重新格式化才能掛載,這樣就會(huì)將原來(lái)存在的數(shù)據(jù)清除,所以在對(duì)xfs文件系統(tǒng)格式操作時(shí)需切記。
- 在對(duì)邏輯卷快照操作能僅有一次有效,一旦被還原后則會(huì)被自動(dòng)刪除
- 移除邏輯卷要依次刪除LV邏輯卷、VG卷組后在移除PV物理卷設(shè)備,順序不可顛倒。
- 當(dāng)然對(duì)硬盤的一切修改操作都需要建立在擁有備份的前提下才可進(jìn)行??傊?jǐn)記住硬盤有價(jià),數(shù)據(jù)無(wú)價(jià),請(qǐng)謹(jǐn)慎操作!