LVM的创建扩容和缩减

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVM的创建扩容和缩减相关的知识,希望对你有一定的参考价值。

一、创建LVM系统

?1. 硬盘分区

??首先我们需要为我们的虚拟机添加一个虚拟硬盘,过程略。

查看硬盘设备:

[[email protected] ~]# fdisk -l
...
Disk /dev/sdc: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa2c6e480

   Device Boot      Start         End      Blocks   Id  System
...

如上/dev/sdc是我们新添加的硬盘设备。

我们需要在/dev/sdc基础上创建分区并修改分区系统类型为8e:

[[email protected] ~]# fdisk /dev/sdc 

WARNING: DOS-compatible mode is deprecated. It is strongly recommended to
         switch off the mode (command ‘c‘) and change display units to
         sectors (command ‘u‘).

Command (m for help): n                                   # 创建新分区
Command action
   e   extended                                                    # 扩展分区
   p   primary partition (1-4)                                # 主分区
p
Partition number (1-4): 1                                   # 分区号
First cylinder (1-1305, default 1):                     # 默认从第一个柱面开始划分
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +5G   # 分区大小

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (655-1305, default 655): 
Using default value 655
Last cylinder, +cylinders or +size{K,M,G} (655-1305, default 1305): +3G

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (1048-1305, default 1048): 
Using default value 1048
Last cylinder, +cylinders or +size{K,M,G} (1048-1305, default 1305): 
Using default value 1305

Command (m for help): t                                    # 修改分区系统类型
Partition number (1-4): 1                                    # 选择要修改的分区号
Hex code (type L to list codes): 8e                    #  修改为LVM分区类型,可使用‘L‘查看所有可使用类型
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)

Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)

Command (m for help): p                                   # 查看分区情况

Disk /dev/sdc: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa2c6e480

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1         654     5253223+  8e  Linux LVM
/dev/sdc2             655        1047     3156772+  8e  Linux LVM
/dev/sdc3            1048        1305     2072385   8e  Linux LVM

Command (m for help): w                                   # 保存分区表并退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] ~]# 

?2. 创建物理卷

创建物理卷的命令为pvcreate,利用该命令将希望添加到卷组的所有分区或者硬盘创建为物理卷。

[[email protected] ~]# pvcreate /dev/sdc{1,2}             # 将/dev/sdc1和2创建为物理卷
  Physical volume "/dev/sdc1" successfully created
  Physical volume "/dev/sdc2" successfully created
[[email protected] ~]# pvs                                           # 查看物理卷信息
  PV         VG     Fmt  Attr PSize  PFree   
  /dev/sdc1         lvm2 ----  5.01g    5.01g
  /dev/sdc2         lvm2 ----  3.01g    3.01g
[[email protected] ~]# pvdisplay                                 # 查看物理卷详细信息
 ...
  "/dev/sdc1" is a new physical volume of "5.01 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc1
  VG Name               
  PV Size               5.01 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               jKHeSG-XYna-zFYi-9Kd9-bWRM-f1zY-Qs7FZZ

  "/dev/sdc2" is a new physical volume of "3.01 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc2
  VG Name               
  PV Size               3.01 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               WcKTKi-av0e-so5s-QVXB-yf7G-Tynz-9TQDJb

[[email protected] ~]# 

?3. 创建卷组

创建卷组的命令为vgcreate,将使用pvcreate建立的物理卷创建为一个完整的卷组。

[[email protected] ~]# vgcreate myvg /dev/sdc{1,2}     # 创建卷组
  Volume group "myvg" successfully created
[[email protected] ~]# vgs                                            # 查看卷组信息
  VG     #PV #LV #SN Attr   VSize  VFree   
  myvg     2   0   0 wz--n-  8.02g    8.02g
[[email protected] ~]# vgdisplay myvg                         # 查看卷组详细信息
  --- Volume group ---
  VG Name               myvg
  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               8.02 GiB                                     # 卷组的大小,由上面两个分区决定
  PE Size               4.00 MiB                                     # PE大小可以在vgcreate命令中使用‘-s‘指定,默认为4M
  Total PE              2052
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2052 / 8.02 GiB
  VG UUID               4aIItM-FyPx-GJ47-1A70-b8W4-EwfH-beFZrZ

[[email protected] ~]# pvdisplay                                   # 再次查看物理卷,跟上一次查看有所区别
  --- Physical volume ---
  PV Name               /dev/sdc1
  VG Name               myvg
  PV Size               5.01 GiB / not usable 2.10 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              1282
  Free PE               1282
  Allocated PE          0
  PV UUID               jKHeSG-XYna-zFYi-9Kd9-bWRM-f1zY-Qs7FZZ

  --- Physical volume ---
  PV Name               /dev/sdc2
  VG Name               myvg
  PV Size               3.01 GiB / not usable 2.79 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              770
  Free PE               770
  Allocated PE          0
  PV UUID               WcKTKi-av0e-so5s-QVXB-yf7G-Tynz-9TQDJb

[[email protected] ~]# 

?4. 创建逻辑卷

创建逻辑卷的命令为lvcreate

[[email protected] ~]# lvcreate -L 6G -n mylv myvg
  Logical volume "mylv" created.
[[email protected] ~]#

使用方法:
lvcreate -L #G -n LV_NAME VG_NAME
参数解释:
-L: 指定逻辑卷大小,不可超过卷组大小
-n: 指定逻辑卷名称

?5. 挂载文件系统

至此我们的逻辑卷就已创建完成,接下来我们就需要格式化分区并挂载使用啦。

[[email protected] ~]# mkfs.ext4 /dev/myvg/mylv      # 格式化分区
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
393216 inodes, 1572864 blocks
78643 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1610612736
48 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

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

This filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[[email protected] ~]# mount /dev/myvg/mylv /mnt/      # 挂载分区到/mnt目录下
[[email protected] ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
...
/dev/mapper/myvg-mylv
                      5.8G   12M  5.5G   1% /mnt
[[email protected] ~]# 

二、扩展逻辑卷

上一章我们介绍了如何创建LVM系统,并挂载使用。如果我们分配的空间不够用,那么就需要我们来扩展分区大小了。
接下来我们将/dev/sdc3分区也加入到卷组,并扩展逻辑卷空间。

[[email protected] ~]# pvcreate /dev/sdc3                 # 创建物理卷
  Physical volume "/dev/sdc3" successfully created
[[email protected] ~]# vgextend myvg /dev/sdc3      # 添加物理卷到卷组
  Volume group "myvg" successfully extended
[[email protected] ~]# pvs                                         # 查看物理卷信息
  PV         VG     Fmt  Attr PSize  PFree   
  /dev/sdb   datavg lvm2 a--u 20.00g 1020.00m
  /dev/sdc1  myvg   lvm2 a--u  5.01g       0 
  /dev/sdc2  myvg   lvm2 a--u  3.01g    2.02g
  /dev/sdc3  myvg   lvm2 a--u  1.97g    1.97g        # /dev/sdc3已加入myvg卷组
[[email protected] ~]# vgs                                        # 卷组信息
  VG     #PV #LV #SN Attr   VSize  VFree   
  datavg   1   1   0 wz--n- 20.00g 1020.00m
  myvg     3   1   0 wz--n-  9.99g    3.99g               # 卷组容量比之前扩大
[[email protected] ~]# lvextend -L 9G /dev/myvg/mylv    # 扩展逻辑卷容量到9G
  Size of logical volume myvg/mylv changed from 6.00 GiB (1536 extents) to 9.00 GiB (2304 extents).
  Logical volume mylv successfully resized.
[[email protected] ~]# lvs
  LV     VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  nsas01 datavg -wi-ao---- 19.00g                                                    
  mylv   myvg   -wi-a-----  9.00g                              # 逻辑卷容量成功扩展到9G
[[email protected] ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
...
/dev/mapper/myvg-mylv
                      5.8G   12M  5.5G   1% /mnt             # 但是分区空间还是初始大小
[[email protected] ~]# resize2fs -p /dev/myvg/mylv  # 改变分区大小为逻辑卷大小
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/myvg/mylv is mounted on /mnt; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/myvg/mylv to 2359296 (4k) blocks.
The filesystem on /dev/myvg/mylv is now 2359296 blocks long.

[[email protected] ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
...
/dev/mapper/myvg-mylv
                      8.8G   14M  8.3G   1% /mnt           # 分区大小成功扩展到9G
[[email protected] ~]# 

三、缩减逻辑卷

注意事项:

  1. 缩减前需要先卸载挂载;
  2. 要确保缩减后的空间依然能够存储原有的所有数据;
  3. 缩减之前需要先强行检查文件,确保文件系统处于一致性状态。
[[email protected] ~]# umount /dev/myvg/mylv             # 卸载挂载分区
[[email protected] ~]# e2fsck -f /dev/myvg/mylv           # 强行检查文件系统
e2fsck 1.41.12 (17-May-2010)
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/myvg/mylv: 11/589824 files (0.0% non-contiguous), 73247/2359296 blocks
[[email protected] ~]# resize2fs /dev/myvg/mylv 8G    # 改变分区大小
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/myvg/mylv to 2097152 (4k) blocks.
The filesystem on /dev/myvg/mylv is now 2097152 blocks long.

[[email protected] ~]# lvreduce -L 8G /dev/myvg/mylv  # 改变逻辑卷大小
  WARNING: Reducing active logical volume to 8.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce myvg/mylv? [y/n]: y
  Size of logical volume myvg/mylv changed from 9.00 GiB (2304 extents) to 8.00 GiB (2048 extents).
  Logical volume mylv successfully resized.
[[email protected] ~]# mount /dev/myvg/mylv /mnt/
[[email protected] ~]# df -h
...
/dev/mapper/myvg-mylv
                      7.8G   14M  7.4G   1% /mnt                 # 分区大小成功缩减为8G
[[email protected] ~]# 

四、从卷组移除物理卷

[[email protected] ~]# lvs
  LV     VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  nsas01 datavg -wi-ao---- 19.00g                                                    
  mylv   myvg   -wi-ao----  8.00g                                                    
[[email protected] ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree   
  datavg   1   1   0 wz--n- 20.00g 1020.00m
  myvg     3   1   0 wz--n-  9.99g    1.99g
[[email protected] ~]# pvs
  PV         VG     Fmt  Attr PSize  PFree   
  /dev/sdc1  myvg   lvm2 a--u  5.01g       0 
  /dev/sdc2  myvg   lvm2 a--u  3.01g   16.00m
  /dev/sdc3  myvg   lvm2 a--u  1.97g    1.97g
[[email protected] ~]# 

我们的目前状态:
挂载分区空间为8G;
逻辑卷(LV)空间大小为8G;
卷组(VG)空间大小为9.99G,剩余1.99G;
物理卷/dev/sdc3空间未使用。
那么我们是否可以将/dev/sdc3从卷组中移除出来呢?

[[email protected] ~]# pvmove /dev/sdc3              # 移动/dev/sdc3中数据到其他物理卷
  No data to move for myvg
[[email protected] ~]# vgreduce myvg /dev/sdc3  # 从myvg卷组中移除/dev/sdc3物理卷
  Removed "/dev/sdc3" from volume group "myvg"
[[email protected] ~]# pvremove /dev/sdc3           # 移除物理卷/dev/sdc3
  Labels on physical volume "/dev/sdc3" successfully wiped
[[email protected] ~]# pvs                                      # /dev/sdc3成功从卷组myvg中移除
  PV         VG     Fmt  Attr PSize  PFree   
  /dev/sdb   datavg lvm2 a--u 20.00g 1020.00m
  /dev/sdc1  myvg   lvm2 a--u  5.01g       0 
  /dev/sdc2  myvg   lvm2 a--u  3.01g   16.00m
[[email protected] ~]# vgs                                      # 卷组空间大小缩减了/dev/sdc3空间大小
  VG     #PV #LV #SN Attr   VSize  VFree   
  datavg   1   1   0 wz--n- 20.00g 1020.00m
  myvg     2   1   0 wz--n-  8.02g   16.00m
[[email protected] ~]# 

五、常用命令

  1. 物理卷管理(PV)
    pvcreate、pvdisplay、pvmove、pvremove、pvs
  2. 卷组管理(VG)
    vgcreate、vgdisplay、vgextend、vgreduce、vgremove、vgrename、vgs
  3. 逻辑卷管理(LV)
    lvcreate、lvdisplay、lvextend、lvreduce、lvremove、lvrename、lvs

以上是关于LVM的创建扩容和缩减的主要内容,如果未能解决你的问题,请参考以下文章

LVM逻辑卷-创建扩容缩减迁移快照

基于Centos6系统创建逻辑卷LVM,执行扩容,缩减,删除等操作

LVM逻辑卷管理--在线扩容逻辑卷与卷组容量缩减逻辑卷快照

(转载) Linux LVM分区之VG扩容LV扩容LV缩减LVM快照

python运维实战--LVM创建和在线扩容

Linux LVM 之LV