Linux系統LVM(卷)部署-擴容-縮容-快照-刪除

常用LVM命令總結:
Linux系統LVM(卷)部署-擴容-縮容-快照-刪除

注: 以下案例均採用的系統版本是Oracle linux 7.3

LVM案例:

部署案例:

第 1 步:讓新添加的兩塊硬盤設備支持LVM 技術。

[root@linuxprobe ~]# pvcreate /dev/sdb /dev/sdc
Physical volume "/dev/sdb" successfully created
Physical volume "/dev/sdc" successfully created

第 2 步:把兩塊硬盤設備加入到vgoracle卷組中,然後查看卷組的狀態。卷組名稱可以根據實際情況自定義

[root@oelora ~]# vgcreate vgoracle /dev/sdb /dev/sdc
  Volume group "vgoracle" successfully created
[root@oelora ~]# 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 的邏輯卷。

[root@oelora ~]# lvcreate -n vo -l 37 vgoracle
  Logical volume "vo" created.
[root@oelora ~]# 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 步:把生成好的邏輯捲進行格式化,然後掛載使用。

[root@oelora ~]# 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 

[root@oelora ~]# mkdir /oracle
[root@oelora ~]# mount /dev/vgoracle/vo /oracle

第5 步:查看掛載狀態,並寫入到配置文件,使其永久生效。

[root@oelora ~]# 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
[root@oelora ~]# echo "/dev/vgoracle/vo /oracle ext4 defaults 0 0" >> /etc/fstab
[root@oelora ~]# 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擴容案例:

首先取消掛載上一個案例中的磁盤目錄
[root@oelora ~]# umount /oracle

第 1 步:把上一個實驗中的邏輯卷vo 擴展至290MB。

[root@oelora ~]# 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 步:檢查硬盤完整性,並重置硬盤容量。(如果發現容量未增加則重啓系統在重置容量)

[root@oelora ~]# 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.
[root@oelora ~]# 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 步:重新掛載硬盤設備並查看掛載狀態。

[root@oelora ~]# mount -a
[root@oelora ~]# 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:

取消上面案例中掛載的磁盤目錄
[root@oelora ~]# umount /oracle

第 1 步:檢查文件系統的完整性。

[root@oelora ~]# 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。

[root@oelora ~]# 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.

[root@oelora ~]# 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 步:重新掛載文件系統並查看系統狀態。

[root@oelora ~]# mount -a
[root@oelora ~]# 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完成


邏輯卷快照:

注:
快照卷的容量必須等同於邏輯卷的容量;
快照卷僅一次有效,一旦執行還原操作後則會被立即自動刪除。

首先查看卷組信息

[root@oelora ~]# 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 參數指定切割的大小。另外,還需要在命令後面寫上是針對哪個邏輯卷執行的快照操作。

[root@oelora ~]# lvcreate -L 120M -s -n SNAP /dev/vgoracle/vo
  Using default stripesize 64.00 KiB.
  Logical volume "SNAP" created.

[root@oelora ~]# 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 的垃圾文件,然後再查看快照卷的狀態。可以發現存儲空間佔的用量上升了。

[root@oelora ~]# 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

[root@oelora ~]# 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 快照卷的效果,需要對邏輯捲進行快照還原操作。在此之前記得先卸載掉邏輯卷設備與目錄的掛載。

[root@oelora ~]# umount /oracle
[root@oelora ~]# lvconvert --merge /dev/vgoracle/SNAP
  Merging of volume vgoracle/SNAP started.
  vo: Merged: 31.39%
  vo: Merged: 100.00%

第 4 步:快照卷會被自動刪除掉,並且剛剛在邏輯卷設備被執行快照操作後再創建出來的100MB 的垃圾文件也被清除了。

[root@oelora ~]# mount -a 
[root@oelora /]# ls /oracle/
lost+found

LVM快照完成


刪除邏輯卷:

說明:當生產環境中想要重新部署LVM 或者不再需要使用LVM 時,則需要執行LVM 的刪除操作。爲此,需要提前備份好重要的數據信息,然後依次刪除邏輯卷、卷組、物理卷設備,這個順序不可顛倒。
第 1 步:取消邏輯卷與目錄的掛載關聯,刪除配置文件中永久生效的設備參數。

[root@oelora /]# umount /oracle
[root@oelora /]# 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 來確認操作。

[root@oelora /]# lvremove /dev/vgoracle/vo
Do you really want to remove active logical volume vgoracle/vo? [y/n]: y
  Logical volume "vo" successfully removed

第 3 步:刪除卷組,此處只寫卷組名稱即可,不需要設備的絕對路徑。

[root@oelora /]# vgremove vgoracle
  Volume group "vgoracle" successfully removed

第 4 步:刪除物理卷設備。

[root@oelora /]# pvremove /dev/sdb /dev/sdc
  Labels on physical volume "/dev/sdb" successfully wiped.
  Labels on physical volume "/dev/sdc" successfully wiped.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章