Linux系統LVM(卷)部署-擴容-縮容-快照-刪除
常用LVM命令總結:
注: 以下案例均採用的系統版本是Oracle linux 7.3
LVM案例:
部署案例:
第 1 步:讓新新增的兩塊硬碟裝置支援LVM 技術。
[[email protected] ~]# pvcreate /dev/sdb /dev/sdc
Physical volume "/dev/sdb" successfully created
Physical volume "/dev/sdc" successfully created
第 2 步:把兩塊硬碟裝置加入到vgoracle卷組中,然後檢視卷組的狀態。卷組名稱可以根據實際情況自定義
[[email protected]
~]# vgcreate vgoracle /dev/sdb /dev/sdc Volume group "vgoracle" successfully created [[email protected] ~]# vgdisplay vgoracle --- Volume group --- VG Name vgoracle 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 fFAto3-gIjc-ArFU-N69J-4LU5-G1iq-969tec
第3 步:切割出一個約為150MB 的邏輯卷裝置。
說明:
這裡需要注意切割單位的問題。在對邏輯捲進行切割時有兩種計量單位。第一種是以容
量為單位,所使用的引數為-L。例如,使用-L 150M 生成一個大小為150MB 的邏輯卷。另外
一種是以基本單元的個數為單位,所使用的引數為-l。每個基本單元的大小預設為4MB。例
如,使用-l 37 可以生成一個大小為37×4MB=148MB 的邏輯卷。
[[email protected] ~]# lvcreate -n vo -l 37 vgoracle
Logical volume "vo" created.
[[email protected] ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vgoracle/vo
LV Name vo
VG Name vgoracle
LV UUID T9wwib-tClT-TUMz-RJBV-QHTu-dc0E-wF33fS
LV Write Access read/write
LV Creation host, time oelora, 2019-01-10 10:47:15 +0800
LV Status available
\# open 0
LV Size 148.00 MiB
Current LE 37
Segments 1
Allocation inherit
Read ahead sectors auto
\- currently set to 8192
Block device 251:0
第 4 步:把生成好的邏輯捲進行格式化,然後掛載使用。
[[email protected] ~]# mkfs.ext4 /dev/vgoracle/vo
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
38000 inodes, 151552 blocks
7577 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33816576
19 block groups
8192 blocks per group, 8192 fragments per group
2000 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
[[email protected] ~]# mkdir /oracle
[[email protected] ~]# mount /dev/vgoracle/vo /oracle
第5 步:檢視掛載狀態,並寫入到配置檔案,使其永久生效。
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs 2.0G 84K 2.0G 1% /dev/shm
tmpfs 2.0G 8.9M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda3 42G 5.3G 37G 13% /
/dev/sda1 497M 183M 315M 37% /boot
tmpfs 396M 16K 396M 1% /run/user/42
tmpfs 396M 0 396M 0% /run/user/0
/dev/mapper/vgoracle-vo 140M 1.6M 128M 2% /oracle
[[email protected] ~]# echo "/dev/vgoracle/vo /oracle ext4 defaults 0 0" >> /etc/fstab
[[email protected] ~]# more /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jan 10 08:27:01 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=788f2c24-2f34-4616-a170-4074dee2b669 / xfs defaults 0 0
UUID=64a8f3f5-d8b2-42cf-9c8e-a5d57725f527 /boot xfs defaults 0 0
UUID=95c524ac-5b49-49ac-958b-bf4d2b756ca1 swap swap defaults 0 0
/dev/vgoracle/vo /oracle ext4 defaults 0 0
LVM部署完成
LVM擴容案例:
首先取消掛載上一個案例中的磁碟目錄[[email protected] ~]# umount /oracle
第 1 步:把上一個實驗中的邏輯卷vo 擴充套件至290MB。
[[email protected] ~]# lvextend -L 290M /dev/vgoracle/vo
Rounding size to boundary between physical extents: 292.00 MiB.
Size of logical volume vgoracle/vo changed from 148.00 MiB (37 extents) to 292.00 MiB (73 extents).
Logical volume vgoracle/vo successfully resized.
第 2 步:檢查硬碟完整性,並重置硬碟容量。(如果發現容量未增加則重啟系統在重置容量)
[[email protected] ~]# lvextend -L 290M /dev/vgoracle/vo
Rounding size to boundary between physical extents: 292.00 MiB.
Size of logical volume vgoracle/vo changed from 148.00 MiB (37 extents) to 292.00 MiB (73 extents).
Logical volume vgoracle/vo successfully resized.
[[email protected] ~]# e2fsck -f /dev/vgoracle/vo
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vgoracle/vo: 11/38000 files (0.0% non-contiguous), 10453/151552 blocks
第 3 步:重新掛載硬碟裝置並檢視掛載狀態。
[[email protected] ~]# mount -a
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs 2.0G 84K 2.0G 1% /dev/shm
tmpfs 2.0G 8.9M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda3 42G 5.3G 37G 13% /
/dev/sda1 497M 183M 315M 37% /boot
tmpfs 396M 12K 396M 1% /run/user/42
tmpfs 396M 0 396M 0% /run/user/0
/dev/mapper/vgoracle-vo 279M 2.1M 259M 1% /oracle
擴容LVM完成
縮小LVM:
取消上面案例中掛載的磁碟目錄[[email protected] ~]# umount /oracle
第 1 步:檢查檔案系統的完整性。
[[email protected] ~]# e2fsck -f /dev/vgoracle/vo
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vgoracle/vo: 11/74000 files (0.0% non-contiguous), 15507/299008 blocks
第 2 步:把邏輯卷vo 的容量減小到120MB。
[[email protected] ~]# resize2fs /dev/vgoracle/vo 120M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vgoracle/vo to 122880 (1k) blocks.
The filesystem on /dev/vgoracle/vo is now 122880 blocks long.
[[email protected] ~]# lvreduce -L 120M /dev/vgoracle/vo
WARNING: Reducing active logical volume to 120.00 MiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vgoracle/vo? [y/n]: y
Size of logical volume vgoracle/vo changed from 292.00 MiB (73 extents) to 120.00 MiB (30 extents).
Logical volume vgoracle/vo successfully resized.
第 3 步:重新掛載檔案系統並檢視系統狀態。
[[email protected] ~]# mount -a
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs 2.0G 84K 2.0G 1% /dev/shm
tmpfs 2.0G 8.9M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sda3 42G 5.3G 37G 13% /
/dev/sda1 497M 183M 315M 37% /boot
tmpfs 396M 16K 396M 1% /run/user/42
tmpfs 396M 0 396M 0% /run/user/0
/dev/mapper/vgoracle-vo 113M 1.6M 103M 2% /oracle
縮容LVM完成
邏輯卷快照:
注:
快照卷的容量必須等同於邏輯卷的容量;
快照卷僅一次有效,一旦執行還原操作後則會被立即自動刪除。
首先檢視卷組資訊
[[email protected] ~]# vgdisplay
--- Volume group ---
VG Name vgoracle
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
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 30 / 120.00 MiB
Free PE / Size 10208 / 39.88 GiB
VG UUID fFAto3-gIjc-ArFU-N69J-4LU5-G1iq-969tec
第 1 步:使用-s 引數生成一個快照卷,使用-L 引數指定切割的大小。另外,還需要在命令後面寫上是針對哪個邏輯卷執行的快照操作。
[[email protected] ~]# lvcreate -L 120M -s -n SNAP /dev/vgoracle/vo
Using default stripesize 64.00 KiB.
Logical volume "SNAP" created.
[[email protected] ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vgoracle/vo
LV Name vo
VG Name vgoracle
LV UUID T9wwib-tClT-TUMz-RJBV-QHTu-dc0E-wF33fS
LV Write Access read/write
LV Creation host, time oelora, 2019-01-10 10:47:15 +0800
LV snapshot status source of
SNAP [active]
LV Status available
# open 1
LV Size 120.00 MiB
Current LE 30
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 251:0
--- Logical volume ---
LV Path /dev/vgoracle/SNAP
LV Name SNAP
VG Name vgoracle
LV UUID JAu30I-CrqR-il0W-fYS8-REhm-w5gx-u0dFo7
LV Write Access read/write
LV Creation host, time oelora, 2019-01-10 11:13:21 +0800
LV snapshot status active destination for vo
LV Status available
# open 0
LV Size 120.00 MiB
Current LE 30
COW-table size 120.00 MiB
COW-table LE 30
Allocated to snapshot 0.01%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 251:3
第 2 步:在邏輯卷所掛載的目錄中建立一個100MB 的垃圾檔案,然後再檢視快照卷的狀態。可以發現儲存空間佔的用量上升了。
[[email protected] ~]# dd if=/dev/zero of=/oracle/files count=1 bs=100M
1+0 records in
1+0 records out
104857600 bytes (105 MB) copied, 3.36023 s, 31.2 MB/s
[[email protected] ~]# lvdisplay
--- Logical volume ---
LV Path /dev/vgoracle/vo
LV Name vo
VG Name vgoracle
LV UUID T9wwib-tClT-TUMz-RJBV-QHTu-dc0E-wF33fS
LV Write Access read/write
LV Creation host, time oelora, 2019-01-10 10:47:15 +0800
LV snapshot status source of
SNAP [active]
LV Status available
# open 1
LV Size 120.00 MiB
Current LE 30
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 251:0
--- Logical volume ---
LV Path /dev/vgoracle/SNAP
LV Name SNAP
VG Name vgoracle
LV UUID JAu30I-CrqR-il0W-fYS8-REhm-w5gx-u0dFo7
LV Write Access read/write
LV Creation host, time oelora, 2019-01-10 11:13:21 +0800
LV snapshot status active destination for vo
LV Status available
# open 0
LV Size 120.00 MiB
Current LE 30
COW-table size 120.00 MiB
COW-table LE 30
Allocated to snapshot 81.15%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 251:3
第 3 步:為了校驗SNAP 快照卷的效果,需要對邏輯捲進行快照還原操作。在此之前記得先解除安裝掉邏輯卷裝置與目錄的掛載。
[[email protected] ~]# umount /oracle
[[email protected] ~]# lvconvert --merge /dev/vgoracle/SNAP
Merging of volume vgoracle/SNAP started.
vo: Merged: 31.39%
vo: Merged: 100.00%
第 4 步:快照卷會被自動刪除掉,並且剛剛在邏輯卷裝置被執行快照操作後再創建出來的100MB 的垃圾檔案也被清除了。
[[email protected] ~]# mount -a
[[email protected] /]# ls /oracle/
lost+found
LVM快照完成
刪除邏輯卷:
說明:當生產環境中想要重新部署LVM 或者不再需要使用LVM 時,則需要執行LVM 的刪除操作。為此,需要提前備份好重要的資料資訊,然後依次刪除邏輯卷、卷組、物理卷裝置,這個順序不可顛倒。
第 1 步:取消邏輯卷與目錄的掛載關聯,刪除配置檔案中永久生效的裝置引數。
[[email protected] /]# umount /oracle
[[email protected] /]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jan 10 08:27:01 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=788f2c24-2f34-4616-a170-4074dee2b669 / xfs defaults 0 0
UUID=64a8f3f5-d8b2-42cf-9c8e-a5d57725f527 /boot xfs defaults 0 0
UUID=95c524ac-5b49-49ac-958b-bf4d2b756ca1 swap swap defaults 0 0
第 2 步:刪除邏輯卷裝置,需要輸入y 來確認操作。
[[email protected] /]# lvremove /dev/vgoracle/vo
Do you really want to remove active logical volume vgoracle/vo? [y/n]: y
Logical volume "vo" successfully removed
第 3 步:刪除卷組,此處只寫卷組名稱即可,不需要裝置的絕對路徑。
[[email protected] /]# vgremove vgoracle
Volume group "vgoracle" successfully removed
第 4 步:刪除物理卷裝置。
[[email protected] /]# pvremove /dev/sdb /dev/sdc
Labels on physical volume "/dev/sdb" successfully wiped.
Labels on physical volume "/dev/sdc" successfully wiped.