在我們使用Linux的過程中難免會對現有的RAID磁盤陣列組的硬盤管理空間進行增加、減小操作等,如果當初安裝系統時劃分的硬盤空間沒有考慮到今后的硬盤有可能會進行調整的話,我們就要重新規劃并重裝操作系統,以滿足應用需求,這時Linux給我提供了一項硬盤設備管理技術LVM邏輯卷管理器(Logical Volume Manager)通過它,我們就可以實現對硬盤空間的動態劃分和調整。
以下是LVM一些名詞的解釋,解釋來自百度LVM:
PhysicalStorageMedia 物理存儲設備
指系統的物理存儲設備:磁盤,如:/dev/hda、/dev/sda等,是存儲系統最底層的存儲單元。
PV(Physical Volume)物理卷
指磁盤分區或從邏輯上與磁盤分區具有同樣功能的設備,是LVM的基本存儲邏輯塊,但和基本的物理存儲介質相比,卻包含有與LVM相關的管理參數。
VG(Volume Group)卷組
類似于非LVM系統中的物理磁盤,其由一個或多個物理卷PV組成,可以在卷組上創建一個或多個LV。
LV(Logical Volume)邏輯卷
類似于非LVM系統中的磁盤分區,邏輯卷建立在卷組VG之上,在邏輯卷LV之上可以建立文件系統。
PE(Physical Extent)物理塊
每一個物理卷PV被劃分為稱為PE的基本單元,具有唯一編號的PE是可以被LVM尋址的最小單元,PE的大小是可以配置的,默認為4MB。所以物理卷(PV)大小等同的基本單元PE組成。
LE(Logical Extent)邏輯塊
邏輯卷LV也被劃分為可被尋址的基本單位,稱為LE。在同一個卷組中,LE的大小和PE是相同的,并且一一對應。
部署實踐
1、為實驗準備2塊20G大小的硬盤
如果不想在虛擬環境下重啟識別新硬盤的話,可以執行下面的命令---
echo "- - -" > /sys/class/scsi_host/host0/scan
注意:三個- - -號之間有空隔。
2、將新添加的硬盤添加支持LVM邏輯卷管理中:
[root@CentOS7 ~]#pvcreate /dev/sdb /dev/sdc
Physical volume "/dev/sdb" successfully created.
Physical volume "/dev/sdc" successfully created.
3、創建storage卷組,將新添加的兩塊硬盤設備加入到卷組中,查看卷組狀態:
[root@CentOS7 ~]#vgcreate storage /dev/sdb /dev/sdc ##添加入卷組
Volume group "storage" successfully created
[root@CentOS7 ~]#vgdisplay
--- Volume group ---
VG Name storage ##創建“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的邏輯設備:
在對邏輯卷劃分中,如果我們使用-l參數來指定使用PE基本單元的個數,或者也可以使用常見以-L參數來以容量為單位劃分。
[root@CentOS7 ~]#lvcreate -n lv -L 200 storage ##200M大小的邏輯設備
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 ##創建掛載目錄
[root@CentOS7 ~]#mount /dev/storage/lv /lvmstorage ##掛載
6、查看掛載狀態,并將掛載狀態寫入配置文件使其重啟掛載信息不丟失
[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 ##卷狀態
[root@CentOS7 ~]#echo "/dev/storage/lv /lvmstorage xfs defaults 0 0" >> /etc/fstab ##echo 寫入配置文件
7、縮小邏輯卷
注意:對邏輯卷縮小操作不當將會有丟失數據的危險,所以在生產環境中不得已需要縮小邏輯卷的話,也記得將重要的數據提前備份,以防數據丟失。另外對LVM邏輯卷縮小操作前需要檢查文件系統的完整性,確保數據安全。
此外由于為mkfs.xfs格式系統,xfs默認只能擴大不能縮小,所以我們需要利用xfsdump / xfsrestore 來實現縮小,縮小后需要重新格式化才能掛載。(適用系統剛安裝好,邏輯分區內沒有什么數據或數據不多且不重要的情況下)
[root@CentOS7 ~]#rpm -qa xfsdump ##查看系統有無xfsdump
xfsdump-3.1.4-1.el7.x86_64
[root@CentOS7 ~]#yum -y install xfsdump ##安裝xfsdump套件
[root@CentOS7 ~]#umount /lvmstorage/ ##卸載設備和掛載點的關聯
檢查文件系統的完整性:
[root@CentOS7 ~]#e2fsck -f /dev/storage/lv ##e2fsck -f 強制檢查
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 檔案系統的 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.
減小后將邏輯分區重新通過mkfs.xfs命令重新格式化才能掛載上
[root@CentOS7 ~]#mkfs.xfs -f /dev/storage/lv ##格式化
將文件系統重新掛載并查看硬盤狀態:
[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、擴容邏輯卷,只要卷組中的空間足夠大就可以一直為邏輯卷擴容。
將邏輯卷lv擴展至300M:
[root@CentOS7 ~]#lvextend -L 300M /dev/storage/lv ##擴展至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 ##目標XFS文件系統來擴展
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查看分區空間
[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 ##擴展至300M
9、邏輯卷快照建立
在對邏輯卷管理中我們還可以建立快照卷功能,這項功能類似于VMware中的快照功能,提供數據糾錯還原功能。
往邏輯卷組設備所掛載的目錄用dd寫入一個文件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參數生成一個快照卷,-L參數指定劃分的大小:
[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設備卷所掛載的目錄中創建一個100M的二進制文件,再來查看邏輯卷狀態:
[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
檢驗SNAP快照效果,對邏輯卷進行快照合并還原,(先卸載邏輯設備目錄的掛載)
[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/ ##邏輯卷設備被快照后創建的100M文件也被還原了
readme
10、刪除邏輯卷
當不再需要使用LVM邏輯卷管理器時,我們可以依次安裝順序刪除邏輯卷
取消邏輯卷與目錄的掛載關聯,并刪除 /etc/fstab配置文件中的設備刪除:
[root@CentOS7 ~]#umount /lvmstorage/
[root@CentOS7 ~]#sed -i '$d' /etc/fstab ##因為設備文件在最后一行,我們直接用sed -i執行刪除
將LV邏輯卷設備刪除,需要屬y確認操作:
[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物理卷設備移除:
[root@CentOS7 ~]#pvremove /dev/sdb /dev/sdc
Labels on physical volume "/dev/sdb" successfully wiped.
Labels on physical volume "/dev/sdc" successfully wiped.
確認上述操作執行成功,無提示信息則為操作正確完成邏輯卷設備移除:
[root@CentOS7 ~]#lvdisplay ;vgdisplay ;pvdisplay
總結:
- 在對邏輯卷大小調整時,針對xfs和ext4不同的文件系統中,所使用的命令都不相同
resize2fs命令 針對的是ext2、ext3、ext4文件系統
xfs_growfs命令 針對的是xfs文件系統 - xfs文件系統只支持增大分區空間的情況,不支持縮小,要減小的話,只能在減小后將邏輯分區重新通過mkfs.xfs命令重新格式化才能掛載,這樣就會將原來存在的數據清除,所以在對xfs文件系統格式操作時需切記。
- 在對邏輯卷快照操作能僅有一次有效,一旦被還原后則會被自動刪除
- 移除邏輯卷要依次刪除LV邏輯卷、VG卷組后在移除PV物理卷設備,順序不可顛倒。
- 當然對硬盤的一切修改操作都需要建立在擁有備份的前提下才可進行。總之謹記住硬盤有價,數據無價,請謹慎操作!