天天看點

LVM邏輯卷管理--線上擴容、邏輯卷與卷組容量縮減、邏輯卷快照

LVM邏輯卷管理

公司生産環境中使用了LVM邏輯卷管理,是以今天花時間整理一下。

通過LVM技術整合所有的磁盤資源進行分區,然後建立PV實體卷形成一個資源池,再劃分卷組,最後在卷組上建立不同的邏輯卷,繼而初始化邏輯卷,挂載到系統中使用。

LVM對比傳統硬碟存儲的優點

1、 統一管理:整合多個磁盤或分區形成一個資源池

2、 靈活性:可以使用不同磁盤、不同分區來組成一個邏輯卷

3、 可伸縮性:邏輯卷和卷組的容量都可以使用指令來擴充或者縮減,且不會影響破壞原有資料,支援磁盤熱插拔。

4、 支援熱插拔

5、 支援線上資料移動

6、 裝置命名友善

7、 鏡像卷:可以很友善的做資料鏡像

8、 卷快照:把邏輯卷中的資料快照儲存到新的邏輯卷進行備份

邏輯分區與邏輯卷的差別

邏輯卷與邏輯分區不是同一概念,邏輯分區容量大小不能改變;邏輯卷容量大小可以改變(基于實體卷和卷組之上的邏輯卷);邏輯卷管理器(LVM)可以更輕松的管理磁盤空間(整合的磁盤池)

LVM原理圖

Physical volume (PV實體卷)、Volume group (VG卷組)、Logical volume(LV邏輯卷),通過圖解更容易讀懂實體磁盤、磁盤分區、實體卷、卷組、邏輯卷之間的關系(個人了解,僅供參考)

LVM邏輯卷管理--線上擴容、邏輯卷與卷組容量縮減、邏輯卷快照

測試環境

核心

LVM1版本需要2.4(包括)以後的核心支援;LVM2需要2.6(包括)以後的核心版本支援。

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 
[root@localhost ~]# uname -r
3.10.0-693.el7.x86_64           

新添加兩塊磁盤

[root@localhost ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0000457f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes           

磁盤分區

第一塊磁盤

[root@localhost ~]# fdisk /dev/sdb
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p #主分區
Partition number (2-4, default 2): 
First sector (20973568-41943039, default 20973568): 
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): 
Using default value 41943039
Partition 2 of type Linux and of size 10 GiB is set

Command (m for help): p #第一塊磁盤劃分兩個主分區

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xfba75d9e

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux
/dev/sdb2        20973568    41943039    10484736   83  Linux
Command (m for help): t #做LVM管理,需要修改分區類型為Linux LVM
Partition number (1,2, default 2): 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xfba75d9e

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   8e  Linux LVM
/dev/sdb2        20973568    41943039    10484736   8e  Linux LVM
Command (m for help): w #儲存分區資訊
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
告訴核心
[root@localhost ~]# partprobe /dev/sdb #更新核心分區表

[root@localhost ~]# ls -l /dev/sdb*
brw-rw----. 1 root disk 8, 16 Oct 13 11:30 /dev/sdb
brw-rw----. 1 root disk 8, 17 Oct 13 11:30 /dev/sdb1
brw-rw----. 1 root disk 8, 18 Oct 13 11:30 /dev/sdb2
[root@localhost ~]#           

第二塊磁盤

建立一個主分區和一個邏輯分區,用來測試擴充分區和邏輯分區是否能夠建立PV實體卷并加入VG卷組,實驗證明,擴充分區是無法建立PV和加入VG,主分區和邏輯分區可以。

[root@localhost ~]# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x5d61bb7c.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p #主分區
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e #擴充分區,要建立擴充分區之後,才能建立邏輯分區;擴充分區隻能建立一個,分區表支援建立最多四分主分區,如果想要建立4個以上的分區,必須建立擴充分區,然後建立邏輯分區
Partition number (2-4, default 2): 
First sector (20973568-41943039, default 20973568): 
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): 
Using default value 41943039
Partition 2 of type Extended and of size 10 GiB is set

Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l #建立邏輯分區
Adding logical partition 5
First sector (20975616-41943039, default 20975616): 
Using default value 20975616
Last sector, +sectors or +size{K,M,G} (20975616-41943039, default 41943039): 
Using default value 41943039
Partition 5 of type Linux and of size 10 GiB is set

Command (m for help): p

Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x5d61bb7c

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048    20973567    10485760   83  Linux
/dev/sdc2        20973568    41943039    10484736    5  Extended
/dev/sdc5        20975616    41943039    10483712   83  Linux

Command (m for help): t #修改分區類型
Partition number (1,2,5, default 5): 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): t
Partition number (1,2,5, default 5): 2 
Hex code (type L to list all codes): 8e

You cannot change a partition into an extended one or vice versa.
Delete it first.

Type of partition 2 is unchanged: Extended

Command (m for help): t
Partition number (1,2,5, default 5): 5
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x5d61bb7c

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048    20973567    10485760   8e  Linux LVM
/dev/sdc2        20973568    41943039    10484736    5  Extended
/dev/sdc5        20975616    41943039    10483712   8e  Linux LVM

Command (m for help): w #儲存分區表資訊
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe /dev/sdc
[root@localhost ~]# ls -l /dev/sdc*
brw-rw----. 1 root disk 8, 32 Oct 13 11:35 /dev/sdc
brw-rw----. 1 root disk 8, 33 Oct 13 11:35 /dev/sdc1
brw-rw----. 1 root disk 8, 34 Oct 13 11:35 /dev/sdc2
brw-rw----. 1 root disk 8, 37 Oct 13 11:35 /dev/sdc5           

建立PV、VG、LV

建立PV實體卷

[root@localhost ~]# pvcreate /dev/sdc2 #sdc2是擴充分區,無法做成實體卷PV
  Device /dev/sdc2 not found (or ignored by filtering).
[root@localhost ~]# pvcreate /dev/sdb1 #把sdb1做成實體卷PV,也可以用下面的寫法,一次性把所有主分區或邏輯分區做成實體卷PV
  Physical volume "/dev/sdb1" successfully created.
[root@localhost ~]# pvcreate /dev/sdb2 /dev/sdc1 /dev/sdc5
  Physical volume "/dev/sdb2" successfully created.
  Physical volume "/dev/sdc1" successfully created.
  Physical volume "/dev/sdc5" 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               610jgI-z9pr-N5H1-R1Qv-jVMh-cMOD-2VKNm5

  "/dev/sdb1" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               l65LfK-FxkO-I8ux-0Lj6-jQB1-ev6d-jl4D8v

  "/dev/sdc1" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc1
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               olFzUh-w2jf-sK32-i4lr-oYHP-zWTf-dKmgYw

  "/dev/sdc5" is a new physical volume of "<10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc5
  VG Name               
  PV Size               <10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               54wYXO-sN3e-Lyc6-2ZQg-YMkH-6khg-TqbKOA

  "/dev/sdb2" is a new physical volume of "<10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb2
  VG Name               
  PV Size               <10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               aqkWHa-RIxf-15Y9-mgLw-x82r-thpL-kxzYhc

[root@localhost ~]# pvs #顯示所有的實體卷,大小都是10G(分區的時候配置設定的)
  PV         VG     Fmt  Attr PSize   PFree  
  /dev/sda2  centos lvm2 a--  <19.00g      0 
  /dev/sdb1         lvm2 ---   10.00g  10.00g
  /dev/sdb2         lvm2 ---  <10.00g <10.00g
  /dev/sdc1         lvm2 ---   10.00g  10.00g
  /dev/sdc5         lvm2 ---  <10.00g <10.00g           

建立卷組

[root@localhost ~]# vgcreate VGtest1 /dev/sdb1 /dev/sdc1 #建立卷組1,卷組的PV實體卷,可以是不同磁盤,即整合了所有磁盤分區做成資源池
  Volume group "VGtest1" successfully created
[root@localhost ~]# vgcreate VGtest2 /dev/sdb2 /dev/sdc5 #建立卷組2
  Volume group "VGtest2" successfully created
[root@localhost ~]# vgs #顯示所有卷組資訊
  VG      #PV #LV #SN Attr   VSize   VFree 
  VGtest1   2   0   0 wz--n-  19.99g 19.99g
  VGtest2   2   0   0 wz--n-  19.99g 19.99g
  centos    1   2   0 wz--n- <19.00g     0 
[root@localhost ~]# vgdisplay #檢視所有卷組的詳細資訊
  --- 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               FjdwU1-IYgt-Q6r2-uGm7-q1de-g0yh-EsGm64

  --- Volume group ---
  VG Name               VGtest1
  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               19.99 GiB
  PE Size               4.00 MiB
  Total PE              5118
  Alloc PE / Size       0 / 0   
  Free  PE / Size       5118 / 19.99 GiB
  VG UUID               pk62Bc-lRQW-iXim-VUng-PrIk-s9WR-10uctH

  --- Volume group ---
  VG Name               VGtest2
  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               19.99 GiB
  PE Size               4.00 MiB
  Total PE              5118
  Alloc PE / Size       0 / 0   
  Free  PE / Size       5118 / 19.99 GiB
  VG UUID               XsUyyE-1OSa-Xbno-L0W3-cL3E-XmeH-qb3QWW           

建立邏輯卷

[root@localhost ~]# lvcreate -n LVtest1 -L 1G VGtest1 #-n表示建立邏輯卷名,-L表示配置設定邏輯卷的空間大小,VGtest1表示在卷組VGtest1上建立邏輯卷LVtest1
  Logical volume "LVtest1" created.
[root@localhost ~]# lvcreate -n LVtest2 -L 1G VGtest2
  Logical volume "LVtest2" created.
[root@localhost ~]# lvs #顯示邏輯卷的資訊(大小為1G,上面配置設定的)
  LV      VG      Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  LVtest1 VGtest1 -wi-a-----   1.00g                                                    
  LVtest2 VGtest2 -wi-a-----   1.00g                                                    
  root    centos  -wi-ao---- <17.00g                                                    
  swap    centos  -wi-ao----   2.00g                                                    
[root@localhost ~]# lvdisplay #顯示邏輯卷的詳細資訊
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                gS0ted-R1jU-NKWR-rpFb-quUd-Yu7w-c0VfGz
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-09-14 09:38:10 -0400
  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                LTcnK5-ufCZ-5Cwx-Lpfi-Frgo-NMBm-OEE73C
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-09-14 09:38:10 -0400
  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

  --- Logical volume ---
  LV Path                /dev/VGtest1/LVtest1
  LV Name                LVtest1
  VG Name                VGtest1
  LV UUID                PMv1Zv-WtJy-13v4-1GBc-5WaB-mcpk-f7dN22
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-10-13 11:48:28 -0400
  LV Status              available
  # open                 0
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

  --- Logical volume ---
  LV Path                /dev/VGtest2/LVtest2
  LV Name                LVtest2
  VG Name                VGtest2
  LV UUID                HCk2OB-3iVy-Ryma-rKeH-kIie-NYKr-ZVkiIf
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-10-13 11:48:47 -0400
  LV Status              available
  # open                 0
  LV Size                1.00 GiB
  Current LE             256
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:3
會建立對應的目錄和檔案
[root@localhost ~]# ls -l /dev/VGtest1/LVtest1 
lrwxrwxrwx. 1 root root 7 Oct 13 11:48 /dev/VGtest1/LVtest1 -> ../dm-2
[root@localhost ~]# ls -l /dev/d
disk/   dm-0    dm-1    dm-2    dm-3    dmmidi  dri/    
[root@localhost ~]# ls -l /dev/dm-2
brw-rw----. 1 root disk 253, 2 Oct 13 11:48 /dev/dm-2           

格式化LV邏輯卷

[root@localhost ~]# mke2fs -t ext4 /dev/VGtest1/LVtest1 #實體卷需要格式化之後才能使用,格式化為ext4格式
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
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

[root@localhost ~]# mkfs.ext4 /dev/VGtest2/LVtest2 #也可以使用這種方式格式化
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
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done           

挂載LV邏輯卷

[root@localhost ~]# mkdir /appdata
[root@localhost ~]# mkdir /applog
[root@localhost ~]# mount /dev/VGtest1/LVtest1 /appdata #把LV邏輯卷挂載到實際的目錄
[root@localhost ~]# mount /dev/VGtest2/LVtest2 /applog
[root@localhost ~]# df -h
Filesystem                   Size  Used Avail Use% Mounted on
/dev/mapper/centos-root       17G  1.2G   16G   7% /
devtmpfs                     482M     0  482M   0% /dev
tmpfs                        493M     0  493M   0% /dev/shm
tmpfs                        493M  6.8M  486M   2% /run
tmpfs                        493M     0  493M   0% /sys/fs/cgroup
/dev/sda1                   1014M  125M  890M  13% /boot
tmpfs                         99M     0   99M   0% /run/user/0
/dev/mapper/VGtest1-LVtest1  976M  2.6M  907M   1% /appdata
/dev/mapper/VGtest2-LVtest2  976M  2.6M  907M   1% /applog
[root@localhost ~]# mount |grep VGtest
/dev/mapper/VGtest1-LVtest1 on /appdata type ext4 (rw,relatime,seclabel,data=ordered)
/dev/mapper/VGtest2-LVtest2 on /applog type ext4 (rw,relatime,seclabel,data=ordered)
永久挂載LV邏輯卷
[root@localhost ~]# vim /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Fri Sep 14 09:38:12 2018
#
# 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
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=944557a0-3f7c-434d-8202-c960db70b860 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/VGtest1/LVtest1 /appdata                   ext4    defaults        0 0
/dev/VGtest2/LVtest2 /applog                    ext4    defaults        0 0
~
~
"/etc/fstab" 13L, 617C written                                                                   
[root@localhost ~]# mount -a  #重新加載/etc/fstab檔案           

VG卷組擴容

建立一個PV實體卷,然後加入VG即可(fdisk建立分區->修改分區類型Linux LVM->向核心注冊新分區->建立實體卷->把實體卷加入需要擴容的卷組)。

發現卷組pv空間不夠,我們需要擴大卷組空間,現在系統上新增了一塊20G的硬碟/dev/sdc。

[root@localhost ~]# fdisk /dev/sdd #把新添加的磁盤進行分區
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xc29c0ac3.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p #主分區
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G
Partition 1 of type Linux and of size 5 GiB is set

Command (m for help): t #修改分區類型
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdd: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xc29c0ac3

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1            2048    10487807     5242880   8e  Linux LVM
Command (m for help): w #儲存分區資訊
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe /dev/sdd #更新核心分區表
[root@localhost ~]# pvcreate /dev/sdd1 #建立實體卷PV
  Physical volume "/dev/sdd1" successfully created.
[root@localhost ~]# pvs
  PV         VG      Fmt  Attr PSize   PFree  
  /dev/sda2  centos  lvm2 a--  <19.00g      0 
  /dev/sdb1  VGtest1 lvm2 a--  <10.00g  <9.00g
  /dev/sdb2  VGtest2 lvm2 a--  <10.00g  <9.00g
  /dev/sdc1  VGtest1 lvm2 a--  <10.00g <10.00g
  /dev/sdc5  VGtest2 lvm2 a--  <10.00g <10.00g
  /dev/sdd1          lvm2 ---    5.00g   5.00g
[root@localhost ~]# vgextend VGtest1 /dev/sdd1 #擴充VG卷組容量,把實體卷加入卷組
  Volume group "VGtest1" successfully extended
[root@localhost ~]# vgs
  VG      #PV #LV #SN Attr   VSize   VFree  
  VGtest1   3   1   0 wz--n- <24.99g <23.99g
  VGtest2   2   1   0 wz--n-  19.99g  18.99g
  centos    1   2   0 wz--n- <19.00g      0 
[root@localhost ~]# pvs
  PV         VG      Fmt  Attr PSize   PFree  
  /dev/sda2  centos  lvm2 a--  <19.00g      0 
  /dev/sdb1  VGtest1 lvm2 a--  <10.00g  <9.00g
  /dev/sdb2  VGtest2 lvm2 a--  <10.00g  <9.00g
  /dev/sdc1  VGtest1 lvm2 a--  <10.00g <10.00g
  /dev/sdc5  VGtest2 lvm2 a--  <10.00g <10.00g
  /dev/sdd1  VGtest1 lvm2 a--   <5.00g  <5.00g           

LV邏輯卷擴容 (支援線上擴充)

線上将/dev/VGtest/LVtest1 擴充到4G,并且要求資料可以正常通路

[root@localhost ~]# echo "this is a test for LVM" >/appdata/test
[root@localhost ~]# cat /appdata/test #在挂載的邏輯卷裡添加資料,用來測試在邏輯卷擴容是否會破壞原有資料
this is a test for LVM
[root@localhost ~]# lvs
  LV      VG      Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  LVtest1 VGtest1 -wi-ao----   1.00g                                                    
  LVtest2 VGtest2 -wi-ao----   1.00g                                                    
  root    centos  -wi-ao---- <17.00g                                                    
  swap    centos  -wi-ao----   2.00g                                                    
[root@localhost ~]# lvextend -L +2G /dev/VGtest1/LVtest1  #擴容邏輯卷LVtest1,增加2G空間容量(從對應的卷組中劃分空間容量)
  Size of logical volume VGtest1/LVtest1 changed from 1.00 GiB (256 extents) to 3.00 GiB (768 extents).
  Logical volume VGtest1/LVtest1 successfully resized.
[root@localhost ~]# lvs
  LV      VG      Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  LVtest1 VGtest1 -wi-ao----   3.00g                                                    
  LVtest2 VGtest2 -wi-ao----   1.00g                                                    
  root    centos  -wi-ao---- <17.00g                                                    
  swap    centos  -wi-ao----   2.00g                                                    

[root@localhost ~]# resize2fs /dev/VGtest1/LVtest1 #使用resize2fs指令來進行确認增加容量,前面的步驟隻是初步配置設定,還不能實際使用,需要此步驟來确定實際配置設定使用
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/VGtest1/LVtest1 is mounted on /appdata; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/VGtest1/LVtest1 is now 786432 blocks long.
xfs系統确認實際使用的指令
xfs_growfs /dev/VGtest1/LVtest1
檢視挂載目錄的空間容量大小
[root@localhost ~]# df -h|grep VGtest
/dev/mapper/VGtest2-LVtest2  976M  2.6M  907M   1% /applog
/dev/mapper/VGtest1-LVtest1  3.0G  3.1M  2.8G   1% /appdata

資料并沒有受損
[root@localhost ~]# cat /appdata/test 
this is a test for LVM           
[root@localhost ~]# e2fsck -f /dev/VGtest1/LVtest1 #處于挂載狀态的LV邏輯卷無法強制檢查
e2fsck 1.42.9 (28-Dec-2013)
/dev/VGtest1/LVtest1 is mounted.
e2fsck: Cannot continue, aborting.
[root@localhost ~]# umount /dev/VGtest1/LVtest1 #取消邏輯卷挂載
[root@localhost ~]# e2fsck -f /dev/VGtest1/LVtest1 #檢查邏輯卷
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/VGtest1/LVtest1: 12/196608 files (0.0% non-contiguous), 21309/786432 blocks
[root@localhost ~]# resize2fs /dev/VGtest1/LVtest1 1G #首先需要确定縮減邏輯卷到多大空間容量,-1G表示縮減1G大小,1G表示縮減至1G(原來空間是3G)
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/VGtest1/LVtest1 to 262144 (4k) blocks.
The filesystem on /dev/VGtest1/LVtest1 is now 262144 blocks long.

[root@localhost ~]# lvreduce -L 1G /dev/VGtest1/LVtest1 #再進行邏輯卷LV容量縮減
  WARNING: Reducing active logical volume to 1.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce VGtest1/LVtest1? [y/n]: y
  Size of logical volume VGtest1/LVtest1 changed from 3.00 GiB (768 extents) to 1.00 GiB (256 extents).
  Logical volume VGtest1/LVtest1 successfully resized.
[root@localhost ~]# mount /dev/VGtest1/LVtest1 /appdata #重新挂載邏輯卷
[root@localhost ~]# lvs
  LV      VG      Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  LVtest1 VGtest1 -wi-ao----   1.00g                                                    
  LVtest2 VGtest2 -wi-ao---- 500.00m                                                    
  root    centos  -wi-ao---- <17.00g                                                    
  swap    centos  -wi-ao----   2.00g                                                    
[root@localhost ~]# df -h|grep VGtest
/dev/mapper/VGtest2-LVtest2  460M  1.6M  424M   1% /applog
/dev/mapper/VGtest1-LVtest1  976M  2.6M  914M   1% /appdata
[root@localhost ~]# cat /appdata/test 
this is a test for LVM           
[root@localhost appdata]# touch stu{0..100}
[root@localhost appdata]# pvs
  PV         VG      Fmt  Attr PSize   PFree  
  /dev/sda2  centos  lvm2 a--  <19.00g      0 
  /dev/sdb1  VGtest1 lvm2 a--  <10.00g  <9.00g
  /dev/sdb2  VGtest2 lvm2 a--  <10.00g  <9.51g
  /dev/sdc1  VGtest1 lvm2 a--  <10.00g <10.00g
  /dev/sdc5  VGtest2 lvm2 a--  <10.00g <10.00g
  /dev/sdd1  VGtest1 lvm2 a--   <5.00g  <5.00g
[root@localhost appdata]# pvmove /dev/sdb1 #把sdb1磁盤分區的資料轉移到其它磁盤上
  /dev/sdb1: Moved: 6.64%
  /dev/sdb1: Moved: 100.00%
[root@localhost appdata]# vgreduce VGtest1 /dev/sdb1 #把分區sdb1從VG卷組中移除
  Removed "/dev/sdb1" from volume group "VGtest1"
[root@localhost appdata]# pvremove /dev/sdb1 #把sdb1分區從PV實體卷中釋放出來
  Labels on physical volume "/dev/sdb1" successfully wiped.
#如果磁盤劃分了多個分區,做成多個實體卷加入了卷組,那麼可以能重複以上步驟,把所有的空間都從卷組和實體卷中釋放,然後可以拿走這塊磁盤做它用

實作快照,進行備份還原
在/mnt/lvm目錄上,我們将原始的目錄檔案進行快照,然後将/LVtets1目錄中的内容清空,并進行還原
[root@localhost ~]# lvs
  LV      VG      Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  LVtest1 VGtest1 -wi-ao----   1.00g                                                    
  LVtest2 VGtest2 -wi-ao---- 500.00m                                                    
  root    centos  -wi-ao---- <17.00g                                                    
  swap    centos  -wi-ao----   2.00g      
[root@localhost ~]# cat /appdata/test 
this is a test for LVM
[root@localhost ~]# df -h|grep VGtest
/dev/mapper/VGtest2-LVtest2  460M  1.6M  424M   1% /applog
/dev/mapper/VGtest1-LVtest1  976M  2.6M  914M   1% /appdata
[root@localhost ~]# lvcreate -L 20M -n appdatabackup -s -p r /dev/VGtest1/LVtest1 #利用LVtest1邏輯卷,快照形成一個備份的實體卷appdatabackup(邏輯卷快照功能)
  Using default stripesize 64.00 KiB.
  Logical volume "appdatabackup" created.
[root@localhost ~]# lvs
  LV            VG      Attr       LSize   Pool Origin  Data%  Meta%  Move Log Cpy%Sync Convert
  LVtest1       VGtest1 owi-aos---   1.00g                                                     
  appdatabackup VGtest1 sri-a-s---  20.00m      LVtest1 0.06                                   
  LVtest2       VGtest2 -wi-ao---- 500.00m                                                     
  root          centos  -wi-ao---- <17.00g                                                     
  swap          centos  -wi-ao----   2.00g                                                     
[root@localhost ~]# mkdir /tmp/backup
[root@localhost ~]# mount /dev/VGtest1/appdatabackup /tmp/backup #把快照的邏輯卷挂載到目錄上
mount: /dev/mapper/VGtest1-appdatabackup is write-protected, mounting read-only
[root@localhost ~]# cat /tmp/backup/test 
this is a test for LVM
[root@localhost ~]# ls -l /appdata/test 
-rw-r--r--. 1 root root 23 Oct 13 12:33 /appdata/test
[root@localhost ~]# ls -l /tmp/backup/test 
-rw-r--r--. 1 root root 23 Oct 13 12:33 /tmp/backup/test
[root@localhost ~]# rm -rf /appdata/test #删除LVtest1邏輯卷的資料
[root@localhost ~]# ls -l /appdata/test 
ls: cannot access /appdata/test: No such file or directory
[root@localhost ~]# cp /tmp/backup/test /appdata/ #從快照中恢複資料到邏輯卷LVtest1上
[root@localhost ~]# ls -l /appdata/test 
-rw-r--r--. 1 root root 23 Oct 13 13:06 /appdata/test
[root@localhost ~]# df -h|grep VGtest
/dev/mapper/VGtest2-LVtest2        460M  1.6M  424M   1% /applog
/dev/mapper/VGtest1-LVtest1        976M  2.6M  914M   1% /appdata
/dev/mapper/VGtest1-appdatabackup  976M  2.6M  914M   1% /tmp/backup