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逻辑卷),通过图解更容易读懂物理磁盘、磁盘分区、物理卷、卷组、逻辑卷之间的关系(个人理解,仅供参考)

测试环境
内核
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

缩减逻辑卷LV
缩减逻辑卷的注意事项:
1、查看逻辑卷使用空间状况
2、不能在线缩减,得先卸载 切记
3、确保缩减后的空间大小依然能存储原有的所有数据
4、在缩减之前应该先强行检查文件,以确保文件系统处于一至性状态

[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

缩减磁盘空间
发现物理磁盘空间使用不足,将其中一块硬盘或分区拿掉,避免磁盘浪费
1、pvmove /dev/sdb1 #将/dev/sdb1上存储的数据移到其它物理卷中
2、vgreduce VGtest /dev/sdb1 #将/dev/sdb1从VGtest卷组中移除
3、pvremove /dev/sdb1 #将/dev/sdb1从物理卷上移除

[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

知识扩展
resize2fs命令
resize2fs命令被用来增大或者收缩未加载的“ext2/ext3”文件系统的大小。如果文件系统是处于mount状态下,那么它只能做到扩容,前提条件是内核支持在线resize。,linux kernel 2.6支持在mount状态下扩容但仅限于ext3文件系统。
-d:打开调试特性;
-p:打印已完成的百分比进度条;
-f:强制执行调整大小操作,覆盖掉安全检查操作;
-F:开始执行调整大小前,刷新文件系统设备的缓冲区
如果是xfs文件系统,则需要使用xfs_growfs命令
e2fsck命令
e2fsck是检查ext2、ext3、ext4等文件系统的正确性。
补充说明:
e2fsck执行后的传回值及代表意义如下:
0 没有任何错误发生。
1 文件系统发生错误,并且已经修正。
2 文件系统发生错误,并且已经修正。
4 文件系统发生错误,但没有修正。
8 运作时发生错误。
16 使用的语法发生错误。
128 共享的函数库发生错误。
参 数:
-a 不询问使用者意见,便自动修复文件系统。
-b 指定superblock,而不使用预设的superblock。
-B <区块大小> 指定区块的大小,单位为字节。
-c 一并执行badblocks,以标示损坏的区块。
-C 将检查过程的信息完整记录在file descriptor中,使得整个检查过程都能完整监控。
-d 显示排错信息。
-f 即使文件系统没有错误迹象,仍强制地检查正确性。
-F 执行前先清除设备的缓冲区。
-l <文件> 将文件中指定的区块加到损坏区块列表。
-L <文件> 先清除损坏区块列表,再将文件中指定的区块加到损坏区块列表。因此损坏区块列表的区块跟文件中指定的区块是一样的。
-n 以只读模式开启文件系统,并采取非互动方式执行,所有的问题对话均设置以"no"回答。
-p 不询问使用者意见,便自动修复文件系统。
-r 此参数只为了兼容性而存在,并无实际作用。
-s 如果文件系统的字节顺序不适当,就交换字节顺序,否则不做任何动作。
-S 不管文件系统的字节顺序,一律交换字节顺序。
-t 显示时间信息。
-v 执行时显示详细的信息。
-V 显示版本信息。
-y 采取非互动方式执行,所有的问题均设置以"yes"回答。

转载于:https://blog.51cto.com/13691477/2299707

你可能感兴趣的:(LVM逻辑卷管理--在线扩容、逻辑卷与卷组容量缩减、逻辑卷快照)