51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

LVM逻辑卷管理及快照

许多Linux使用者安装操作系统时都会遇到这样的困境:如何精确评估和分配各个硬盘分区的容量,如果当初评估不准确,一旦系统分区不够用时可能不得不备份、删除相关数据,甚至被迫重新规划分区并重装操作系统,以满足应用系统的需要。

LVM是Linux环境中对磁盘分区进行管理的一种机制,是建立在硬盘和分区之上、文件系统之下的一个逻辑层,可提高磁盘分区管理的灵活性。RHEL5默认安装的分区格式就是LVM逻辑卷的格式,需要注意的是/boot分区不能基于LVM创建,必须独立出来。

一.LVM原理

要想理解好LVM的原理,我们必须首先要掌握4个基本的逻辑卷概念。

PE  (Physical Extend)  物理拓展

PV  (Physical Volume)  物理卷

VG  (Volume Group)  卷组

LV  (Logical Volume)  逻辑卷

我们知道在使用LVM对磁盘进行动态管理以后,我们是以逻辑卷的方式呈现给上层的服务的。所以我们所有的操作目的,其实就是去创建一个LV (Logical Volume),逻辑卷就是用来取代我们之前的分区,我们通过对逻辑卷进行格式化,然后进行挂载操作就可以使用了。那么LVM的工作原理是什么呢?所谓无图无真相,咱们下面通过图来对逻辑卷的原理进行解释!!

1.将我们的物理硬盘格式化成PV(Physical Volume)

我们看到,这里有两块硬盘,一块是sda,另一块是sdb,在LVM磁盘管理里,我首先要将这两块硬盘格式化为我们的PV (Physical Volume),也就是我们的物理卷,其实格式化物理卷的过程中LVM是将底层的硬盘划分为了一个一个的PE (Physical Extend),我们的LVM磁盘管理中PE的默认大小是4M大小 ,其实PE就是我们逻辑卷管理的最基本单位。比如说我有一个400M的硬盘,那么在将其格式化成PV的时候,其实际就是将这块物理硬盘划分成了100个的PE,因为PE默认的大小就是4M。这个就是我们的第一步操作。

2.创建一个VG(Volume Group)

在将硬盘格式化成PV以后,我们第二步操作就是创建一个卷组,也就是VG (Volume Group),卷组在这里我们可以将其抽象化成一个空间池,VG的作用就是用来装PE的,我们可以把一个或者多个PV加到VG当中,因为在第一步操作时就已经将该硬盘划分成了多个PE,所以将多个PV加到VG里面后,VG里面就存放了许许多多来自不同PV中的PE,我们通过上面的图片就可以看到,我们格式化了两块硬盘,每个硬盘分别格式化成了3个PE,然后将两块硬盘的PE都加到了我们的VG当中,那么我们的VG当中就包含了6个PE,这6个PE就是两个硬盘的PE之和。通常创建一个卷组的时候我们会为其取一个名字,也就是该VG的名字。

3.基于VG创建我们最后要使用的LV(Logical Volume)

注意 】PV以及VG创建好以后我们是不能够直接使用的,因为PV、VG是我们逻辑卷底层的东西,我们其实最后使用的是在VG基础上创建的LV (Logical Volume),所以第三步操作就是基于VG来创建我们最终要使用的LV。

当我们创建好我们的VG以后,这个时候我们创建LV其实就是从VG中拿出我们指定数量的PE,还是拿上图来说,我们看到我们此时的VG里面已经拥有了6个PE,这时候我们创建了我们的第一个逻辑卷,它的大小是4个PE的大小,也就是16M(因为一个PE的默认大小是4M),而这4个PE有三个是来自于第一块硬盘,而另外一个PE则是来自第二块硬盘。当我们创建第二个逻辑卷时,它的大小就最多只有两个PE的大小了,因为其中的4个PE已经分配给了我们的第一个逻辑卷。

所以创建逻辑卷其实就是我们从VG中拿出我们指定数量的PE,VG中的PE可以来自不同的PV,我们可以创建的逻辑卷的大小取决于VG当中PE存在的数量,并且我们创建的逻辑卷其大小一定是PE的整数倍(即逻辑卷的大小一定要是4M的整数倍)。

4.将我们创建好的LV进行文件系统的格式化,然后挂载使用

在创建好LV以后,这个时候我们就能够对其进行文件系统的格式化了,我们最终使用的就是我们刚创建好的LV,其就相当于传统的文件管理的分区,我们首先要对其进行文件系统的格式化操作,然后通过mount命令对其进行挂载,这个时候我们就能够像使用平常的分区一样来使用我们的逻辑卷了。

我们在创建好LV以后,我们会在 /dev 目录下看到我们的LV信息,例如 /dev/vgname/lvname, 我们每创建一个VG,其会在/dev目录下创建一个以该VG名字命名的文件夹,在该VG的基础上创建好LV以后,我们会在这个VG目录下多出一个以LV名字命名的逻辑卷。

下面我们来对整个LVM的工作原理进行一个总结:

(1)物理磁盘被格式化为PV,空间被划分为一个个的PE

(2)不同的PV加入到同一个VG中,不同PV的PE全部进入到了VG的PE池内

(3)LV基于PE创建,大小为PE的整数倍,组成LV的PE可能来自不同的物理磁盘

(4)LV直接可以格式化后挂载使用

(5)LV的扩充缩减实际上就是增加或减少组成该LV的PE数量,其过程不会丢失原始数据

我们看到,我们这里如果要对LV进行扩充,直接加进来一块sdc硬盘,然后将其格式化成PE,然后将该PV加入到了VG当中,这个时候我们就可以通过增加LV中PE的数量来动态的对LV进行扩充了,只要我们的LV的大小不要超过我们VG空余空间的大小就行!

二、创建LVM逻辑卷

熟悉了LVM的工作原理,首先是要将我们的物理硬盘格式化成PV,然后将多个PV加入到创建好的VG中,最后通过VG创建我们的LV。

三、拉伸一个逻辑卷

我们知道相比于传统磁盘管理方式的各种问题,使用LVM逻辑卷来管理我们的磁盘,我们可以对其进行动态的管理。在传统的磁盘管理方式中,我们如果出现分区大小不足的情况下,我们此时只能通过加入一块物理硬盘,然后对其进行分区,因为加入的硬盘作为独立的文件系统存在,所以对原有分区并没有影响,如果此时我们需要扩大分区,就只能先将之前的分区先卸载掉,然后将所有的信息转移到新的分区下,最后再将新的分区挂载上去,如果是在生产环境下,这样是不可想象的,正因为如此,我们才出现了LVM的磁盘管理方式,可以动态的对我们的磁盘进行管理。

我们首先来看下动态拉伸一个逻辑卷的示意图:

我们从上图可以看到,我们在对逻辑卷进行拉伸时,其实际就是向逻辑卷中增加PE的数量,而PE的数量是由VG中剩余PE的数量所决定的。

注意:逻辑卷的拉伸操作可以在线进行,不需要卸载掉我们的逻辑卷

这样的好处就是当我们的逻辑卷的大小不够用时,我们不需要对其进行卸载,就可以动态的增加我们的逻辑卷的大小,并不会对我们的系统产生任何影响。例如如果我们的服务器上运行着一个重要的服务或者数据库,并要求我们7*24小时不间断保持在线,那么这样的动态增加逻辑卷的大小就非常的有必要了。

创建逻辑卷

创建磁盘分区

准备磁盘/dev/sdb进行分区

[root@manage ~]# fdisk /dev/sdb

The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.
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.


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): 1
First sector (2048-209715199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): +20480M
Partition 1 of type Linux and of size 20 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 (41945088-209715199, default 41945088):
Using default value 41945088
Last sector, +sectors or +size{K,M,G} (41945088-209715199, default 209715199): +20480M
Partition 2 of type Linux and of size 20 GiB is set


Command (m for help): n
Partition type:
p   primary (2 primary, 0 extended, 2 free)
e   extended
Select (default p): p
Partition number (3,4, default 3):
First sector (83888128-209715199, default 83888128):
Using default value 83888128
Last sector, +sectors or +size{K,M,G} (83888128-209715199, default 209715199): +20480M
Partition 3 of type Linux and of size 20 GiB is set


Command (m for help): n
Partition type:
p   primary (3 primary, 0 extended, 1 free)
e   extended
Select (default e): p
Selected partition 4
First sector (125831168-209715199, default 125831168):
Using default value 125831168
Last sector, +sectors or +size{K,M,G} (125831168-209715199, default 209715199):
Using default value 209715199
Partition 4 of type Linux and of size 40 GiB is set


Command (m for help): p


Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 \* 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x63d509ac


Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41945087    20971520   83  Linux
/dev/sdb2        41945088    83888127    20971520   83  Linux
/dev/sdb3        83888128   125831167    20971520   83  Linux
/dev/sdb4       125831168   209715199    41942016   83  Linux


Command (m for help): w
The partition table has been altered!

`Calling ioctl() to re-read partition table.
Syncing disks.`

刷新磁盘信息
[root@manage ~]# partprobe /dev/sdb
创建物理卷
[root@manage ~]# pvcreate /dev/sdb[1-2]
WARNING: ext4 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/sdb1.
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdb2" successfully created.
[root@manage ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               centos
  PV Size               98.80 GiB / not usable 2.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              25293
  Free PE               1
  Allocated PE          25292
  PV UUID               rxE9e7-z7d1-RCZW-OUCR-37lX-2stt-ss3nTG

"/dev/sdb1" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name               /dev/sdb1
VG Name

PV Size               20.00 GiB
Allocatable           NO
PE Size               0

Total PE              0
Free PE               0
Allocated PE          0
PV UUID               dghIHv-uPHZ-4Pn2-HMVy-WOQl-EyPz-fjgrbi

`"/dev/sdb2" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name               /dev/sdb2
VG Name`
`
PV Size               20.00 GiB
Allocatable           NO
PE Size               0`
`
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               yWV7Nz-Mipv-ccDd-73Kn-IO1E-DCO1-J1kgB6`

创建Data_Db1卷组
[root@manage ~]# vgcreate Data_Db1 /dev/sdb[1-2]
  Volume group "Data_Db1" successfully created
[root@manage ~]# vgdisplay 
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               98.80 GiB
  PE Size               4.00 MiB
  Total PE              25293
  Alloc PE / Size       25292 / <98.80 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               TjkjQH-5P7M-fhba-kczk-RMsu-M3Rn-NWmrEz
`--- Volume group ---
VG Name               Data_Db1
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               ZJMjvj-2Cqe-IuqM-MJv0-8GDZ-rY4I-STzGsN`

创建总PE大小的逻辑卷
[root@manage ~]# lvcreate -n Data_DBs1 -l 10238 Data_Db1
  Logical volume "Data_DBs1" created.
[root@manage ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                TEche2-ARme-vfhG-sfNR-2r71-GNCh-47be6r
  LV Write Access        read/write
  LV Creation host, time manage.lolicp.com, 2020-03-04 12:18:10 +0800
  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/home
LV Name                home
VG Name                centos
LV UUID                tti5Hf-xWXK-QSw9-7gNB-LEXQ-W5Yo-SQPTOl
LV Write Access        read/write
LV Creation host, time manage.lolicp.com, 2020-03-04 12:18:10 +0800
LV Status              available


open 1
======



LV Size                \<46.80 GiB
Current LE             11980
Segments               1
Allocation             inherit
Read ahead sectors     auto




* currently set to     8192
  Block device           253:2




--- Logical volume ---
LV Path                /dev/centos/root
LV Name                root
VG Name                centos
LV UUID                MhLNDC-EQAu-cLtR-gpNR-sIDx-sqWy-Bj9Vow
LV Write Access        read/write
LV Creation host, time manage.lolicp.com, 2020-03-04 12:18:10 +0800
LV Status              available


open 1
======



LV Size                50.00 GiB
Current LE             12800
Segments               1
Allocation             inherit
Read ahead sectors     auto




* currently set to     8192
  Block device           253:0




--- Logical volume ---
LV Path                /dev/Data_Db1/Data_DBs1
LV Name                Data_DBs1
VG Name                Data_Db1
LV UUID                a2oBiw-1O85-r2qe-bnZM-ABaw-DMQK-nLl0NF
LV Write Access        read/write
LV Creation host, time manage.lolicp.com, 2020-03-23 12:15:24 +0800
LV Status              available


open 0
======



LV Size                39.99 GiB
Current LE             10238
Segments               2
Allocation             inherit
Read ahead sectors     auto

`
`
* `currently set to     8192
  Block device           253:3`
格式化逻辑卷并挂载
[root@manage ~]# mkfs.ext4 /dev/Data_Db1/Data_DBs1 
mke2fs 1.42.9 (28-Dec-2013)
Discarding device blocks: done                            
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
2621440 inodes, 10483712 blocks
524185 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2157969408
320 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
   32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
   4096000, 7962624

Allocating group tables: done

Writing inode tables: done

Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

`[root@manage ~]# mkdir /data
[root@manage ~]# mount /dev/Data_Db1/Data_DBs1 /data/
[root@manage ~]# df -Th |grep '/data$'
/dev/mapper/Data_Db1-Data_DBs1 ext4       40G   49M   38G   1% /data`

扩容逻辑卷

添加物理卷及添加至卷组
[root@manage ~]# pvcreate /dev/sdb3
  Physical volume "/dev/sdb3" successfully created.
[root@manage ~]# vgextend Data_Db1 /dev/sdb3
  Volume group "Data_Db1" successfully extended

\[root@manage \~\]# vgdisplay
--- Volume group ---
VG Name               centos
System ID

Format                lvm2
Metadata Areas        1
Metadata Sequence No  4
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                3
Open LV               3
Max PV                0
Cur PV                1
Act PV                1
VG Size               98.80 GiB
PE Size               4.00 MiB
Total PE              25293
Alloc PE / Size       25292 / \<98.80 GiB
Free  PE / Size       1 / 4.00 MiB
VG UUID               TjkjQH-5P7M-fhba-kczk-RMsu-M3Rn-NWmrEz


--- Volume group ---
VG Name               Data_Db1
System ID

Format                lvm2
Metadata Areas        3
Metadata Sequence No  3
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                1
Open LV               1
Max PV                0
Cur PV                3
Act PV                3
VG Size               \<59.99 GiB
PE Size               4.00 MiB
Total PE              15357
Alloc PE / Size       10238 / 39.99 GiB
Free  PE / Size       5119 / \<20.00 GiB
VG UUID               ZJMjvj-2Cqe-IuqM-MJv0-8GDZ-rY4I-STzGsN


\[root@manage \~\]# lvdisplay
--- Logical volume ---
LV Path                /dev/centos/swap
LV Name                swap
VG Name                centos
LV UUID                TEche2-ARme-vfhG-sfNR-2r71-GNCh-47be6r
LV Write Access        read/write
LV Creation host, time manage.lolicp.com, 2020-03-04 12:18:10 +0800
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/home
LV Name                home
VG Name                centos
LV UUID                tti5Hf-xWXK-QSw9-7gNB-LEXQ-W5Yo-SQPTOl
LV Write Access        read/write
LV Creation host, time manage.lolicp.com, 2020-03-04 12:18:10 +0800
LV Status              available


open 1
======



LV Size                \<46.80 GiB
Current LE             11980
Segments               1
Allocation             inherit
Read ahead sectors     auto




* currently set to     8192
  Block device           253:2




--- Logical volume ---
LV Path                /dev/centos/root
LV Name                root
VG Name                centos
LV UUID                MhLNDC-EQAu-cLtR-gpNR-sIDx-sqWy-Bj9Vow
LV Write Access        read/write
LV Creation host, time manage.lolicp.com, 2020-03-04 12:18:10 +0800
LV Status              available


open 1
======



LV Size                50.00 GiB
Current LE             12800
Segments               1
Allocation             inherit
Read ahead sectors     auto




* currently set to     8192
  Block device           253:0




--- Logical volume ---
LV Path                /dev/Data_Db1/Data_DBs1
LV Name                Data_DBs1
VG Name                Data_Db1
LV UUID                a2oBiw-1O85-r2qe-bnZM-ABaw-DMQK-nLl0NF
LV Write Access        read/write
LV Creation host, time manage.lolicp.com, 2020-03-23 12:15:24 +0800
LV Status              available


open 1
======



LV Size                39.99 GiB
Current LE             10238
Segments               2
Allocation             inherit
Read ahead sectors     auto

`
`
* `currently set to     8192
  Block device           253:3`
扩容1G至逻辑卷
[root@manage ~]# lvextend -L +1G /dev/Data_Db1/Data_DBs1 
  Size of logical volume Data_Db1/Data_DBs1 changed from 39.99 GiB (10238 extents) to 40.99 GiB (10494 extents).
  Logical volume Data_Db1/Data_DBs1 successfully resized.
[root@manage ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                TEche2-ARme-vfhG-sfNR-2r71-GNCh-47be6r
  LV Write Access        read/write
  LV Creation host, time manage.lolicp.com, 2020-03-04 12:18:10 +0800
  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/home
LV Name                home
VG Name                centos
LV UUID                tti5Hf-xWXK-QSw9-7gNB-LEXQ-W5Yo-SQPTOl
LV Write Access        read/write
LV Creation host, time manage.lolicp.com, 2020-03-04 12:18:10 +0800
LV Status              available


open 1
======



LV Size                \<46.80 GiB
Current LE             11980
Segments               1
Allocation             inherit
Read ahead sectors     auto




* currently set to     8192
  Block device           253:2




--- Logical volume ---
LV Path                /dev/centos/root
LV Name                root
VG Name                centos
LV UUID                MhLNDC-EQAu-cLtR-gpNR-sIDx-sqWy-Bj9Vow
LV Write Access        read/write
LV Creation host, time manage.lolicp.com, 2020-03-04 12:18:10 +0800
LV Status              available


open 1
======



LV Size                50.00 GiB
Current LE             12800
Segments               1
Allocation             inherit
Read ahead sectors     auto




* currently set to     8192
  Block device           253:0




--- Logical volume ---
LV Path                /dev/Data_Db1/Data_DBs1
LV Name                Data_DBs1
VG Name                Data_Db1
LV UUID                a2oBiw-1O85-r2qe-bnZM-ABaw-DMQK-nLl0NF
LV Write Access        read/write
LV Creation host, time manage.lolicp.com, 2020-03-23 12:15:24 +0800
LV Status              available


open 1
======



LV Size                40.99 GiB
Current LE             10494
Segments               3
Allocation             inherit
Read ahead sectors     auto

`
`
* `currently set to     8192
  Block device           253:3`
取消挂载并检查分区完整性

如果是系统分区,则可以跳过。

[root@manage ~]# umount /data/
[root@manage ~]# e2fsck -f /dev/Data_Db1/Data_DBs1 
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/Data_Db1/Data_DBs1: 20037/2621440 files (0.0% non-contiguous), 259016/10483712 blocks
刷新磁盘容量信息

Ext4文件系统

[root@manage ~]# resize2fs /dev/Data_Db1/Data_DBs1 
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/Data_Db1/Data_DBs1 to 10745856 (4k) blocks.
The filesystem on /dev/Data_Db1/Data_DBs1 is now 10745856 blocks long.

XFS文件系统只可扩容,不可减配

[root@manage ~]# xfs_growfs /dev/Data_Db1/Data_DBs1
挂载逻辑卷查看容量
[root@manage ~]# mount /dev/Data_Db1/Data_DBs1 /data/
[root@manage ~]# df -Th |grep '/data$'
/dev/mapper/Data_Db1-Data_DBs1 ext4       41G  241M   38G   1% /data

缩小逻辑卷

[root@manage ~]# umount /data/
[root@manage ~]# e2fsck -f /dev/Data_Db1/Data_DBs1 
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/Data_Db1/Data_DBs1: 20037/2686976 files (0.0% non-contiguous), 263128/10745856 blocks
[root@manage ~]# resize2fs /dev/Data_Db1/Data_DBs1 40G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/Data_Db1/Data_DBs1 to 10485760 (4k) blocks.
The filesystem on /dev/Data_Db1/Data_DBs1 is now 10485760 blocks long.
`[root@manage ~]# lvreduce -L 40G /dev/Data_Db1/Data_DBs1
WARNING: Reducing active logical volume to 40.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce Data_Db1/Data_DBs1? [y/n]: y
Size of logical volume Data_Db1/Data_DBs1 changed from 41.99 GiB (10750 extents) to 40.00 GiB (10240 extents).
Logical volume Data_Db1/Data_DBs1 successfully resized.
[root@manage ~]# mount /dev/Data_Db1/Data_DBs1 /data/
[root@manage ~]# df -Th |grep '/data$'
/dev/mapper/Data_Db1-Data_DBs1 ext4       40G  241M   37G   1% /data`

删除逻辑卷

[root@manage ~]# lvremove /dev/Data_Db1/Data_DBs1 
Do you really want to remove active logical volume Data_Db1/Data_DBs1? [y/n]: y
  Logical volume "Data_DBs1" successfully removed
[root@manage ~]# vgremove Data_Db1
  Volume group "Data_Db1" successfully removed
[root@manage ~]# pvremove /dev/sdb[1-4]
  PV /dev/sdb4 is used by VG Data_Db2 so please use vgreduce first.
  (If you are certain you need pvremove, then confirm by using --force twice.)
  /dev/sdb4: physical volume label not removed.
  Labels on physical volume "/dev/sdb1" successfully wiped.
  Labels on physical volume "/dev/sdb2" successfully wiped.
  Labels on physical volume "/dev/sdb3" successfully wiped.
[root@manage ~]# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home centos -wi-ao---- <46.80g                                                    
  root centos -wi-ao----  50.00g                                                    
  swap centos -wi-ao----   2.00g                                                    
[root@manage ~]# vgs
  VG       #PV #LV #SN Attr   VSize   VFree  
  Data_Db2   1   0   0 wz--n- <40.00g <40.00g
  centos     1   3   0 wz--n-  98.80g   4.00m
[root@manage ~]# pvs
  PV         VG       Fmt  Attr PSize   PFree  
  /dev/sda3  centos   lvm2 a--   98.80g   4.00m
  /dev/sdb4  Data_Db2 lvm2 a--  <40.00g <40.00g

逻辑卷快照

创建物理卷及卷组
[root@manage ~]# pvcreate /dev/sdb4
[root@manage ~]# vgcreate Data_Db2 /dev/sdb4

-L 1G 表示此次创建快照大小
-s 表示创建快照
-n Data_Dbs2 表示新的快照名为Data_Dbs2
/dev/Data_Db1/Data_DBs1 为要创建快照的逻辑卷

[root@manage ~]# lvcreate -L 1G -s -n Data_Dbs2 /dev/Data_Db1/Data_DBs1
  Logical volume "Data_Dbs2" created.
[root@manage ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                TEche2-ARme-vfhG-sfNR-2r71-GNCh-47be6r
  LV Write Access        read/write
  LV Creation host, time manage.lolicp.com, 2020-03-04 12:18:10 +0800
  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/home
LV Name                home
VG Name                centos
LV UUID                tti5Hf-xWXK-QSw9-7gNB-LEXQ-W5Yo-SQPTOl
LV Write Access        read/write
LV Creation host, time manage.lolicp.com, 2020-03-04 12:18:10 +0800
LV Status              available


open 1
======



LV Size                \<46.80 GiB
Current LE             11980
Segments               1
Allocation             inherit
Read ahead sectors     auto




* currently set to     8192
  Block device           253:2




--- Logical volume ---
LV Path                /dev/centos/root
LV Name                root
VG Name                centos
LV UUID                MhLNDC-EQAu-cLtR-gpNR-sIDx-sqWy-Bj9Vow
LV Write Access        read/write
LV Creation host, time manage.lolicp.com, 2020-03-04 12:18:10 +0800
LV Status              available


open 1
======



LV Size                50.00 GiB
Current LE             12800
Segments               1
Allocation             inherit
Read ahead sectors     auto




* currently set to     8192
  Block device           253:0




--- Logical volume ---
LV Path                /dev/Data_Db1/Data_DBs1
LV Name                Data_DBs1
VG Name                Data_Db1
LV UUID                a2oBiw-1O85-r2qe-bnZM-ABaw-DMQK-nLl0NF
LV Write Access        read/write
LV Creation host, time manage.lolicp.com, 2020-03-23 12:15:24 +0800
LV snapshot status     source of
Data_Dbs2 \[active\]
LV Status              available


open 1
======



LV Size                1.00 GiB
Current LE             256
Segments               1
Allocation             inherit
Read ahead sectors     auto




* currently set to     8192
  Block device           253:3




--- Logical volume ---
LV Path                /dev/Data_Db1/Data_Dbs2
LV Name                Data_Dbs2
VG Name                Data_Db1
LV UUID                Fsoklv-9MYF-2jQv-eDKe-U1GK-M4Zr-Q4QYr9
LV Write Access        read/write
LV Creation host, time manage.lolicp.com, 2020-03-23 12:55:36 +0800
LV snapshot status     active destination for Data_DBs1
LV Status              available


open 0
======



LV Size                1.00 GiB
Current LE             256
COW-table size         1.00 GiB
COW-table LE           256
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           253:6`

还原逻辑卷快照

删除文件
[root@manage ~]# echo "test123" > /data/cs.log
[root@manage ~]# cd /data/
[root@manage data]# ls
cs.log  docker  lost+found  nginx-1.17.8  nginx-1.17.8.tar.gz  php-7.4.3  php-7.4.3.tar.gz  vsftpd-3.0.3  vsftpd-3.0.3.tar.gz
[root@manage data]# rm -rf *
还原文件
[root@manage ~]# umount /data/
[root@manage ~]# lvconvert --merge /dev/Data_Db1/Data_Dbs2 
  Merging of volume Data_Db1/Data_Dbs2 started.
  Data_Db1/Data_DBs1: Merged: 99.41%
  Data_Db1/Data_DBs1: Merged: 100.00%
[root@manage ~]# mount /dev/Data_Db1/Data_DBs1 /data/
[root@manage ~]# ls /data/
cs.log  docker  lost+found  nginx-1.17.8  nginx-1.17.8.tar.gz  php-7.4.3  php-7.4.3.tar.gz  vsftpd-3.0.3  vsftpd-3.0.3.tar.gz
[root@manage ~]# cat /data/cs.log 
test123
赞(1)
未经允许不得转载:工具盒子 » LVM逻辑卷管理及快照