LVM (逻辑卷)的增加扩容缩小删除操作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LVM (逻辑卷)的增加扩容缩小删除操作相关的知识,希望对你有一定的参考价值。

一、逻辑卷的创建

查看磁盘


[[email protected] ~ ]#lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  200G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  199G  0 part 
  ├─centos-root 253:0    0   50G  0 lvm  /
  ├─centos-swap 253:1    0    2G  0 lvm  [SWAP]
  └─centos-home 253:2    0  147G  0 lvm  /home
sdb               8:16   0   10G  0 disk                      //新加入的磁盘sdb
sdc               8:32   0    5G  0 disk                       //新加入的磁盘sdc
sr0              11:0    1  8.8G  0 rom  

为磁盘分区

[[email protected] ~ ]#fdisk /dev/sdb
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): 
First sector (2048-20971519, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): 
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help): t                                //选择标签
Selected partition 1
Hex code (type L to list all codes): 8e                 // 8e  Linux LVM 
Changed type of partition ‘Linux‘ to ‘Linux LVM‘
Command (m for help): w                                   //保存
The partition table has been altered!

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

同理分出(sdc)分区


[[email protected] ~ ]#lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  200G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  199G  0 part 
  ├─centos-root 253:0    0   50G  0 lvm  /
  ├─centos-swap 253:1    0    2G  0 lvm  [SWAP]
  └─centos-home 253:2    0  147G  0 lvm  /home
sdb               8:16   0   10G  0 disk 
└─sdb1            8:17   0   10G  0 part 
sdc               8:32   0    5G  0 disk 
└─sdc1            8:33   0    5G  0 part 
sr0              11:0    1  8.8G  0 rom 

创建物理卷(PV)pvcreate /dev/sdb1

[[email protected] ~ ]#pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.

查看物理卷信息(pvdisplay;pvs)

[[email protected] ~ ]#pvdisplay
--- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               vg0
  PV Size               <10.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              2559
  Free PE               1279
  Allocated PE          1280
  PV UUID               arm45N-isin-be4N-ob5a-Xwj1-i7Wc-Nwf73e

创建卷组(VG) vgcreate vg0 /dev/sdb1

[[email protected] ~ ]#vgcreate vg0 /dev/sdb1
  Volume group "vg0" successfully created

查看卷组信息(vgdisplay;vgs)

[[email protected] ~ ]#vgs
  VG     #PV #LV #SN Attr   VSize    VFree  
  centos   1   3   0 wz--n- <199.00g   4.00m
  vg0      1   0   0 wz--n-  <10.00g <10.00g

创建逻辑卷(LV)这里创建了名称为lv0,大小5GB的逻辑卷分区;lvcreate -L 5G -n /dev/vg0/lv0 vg0

[[email protected] ~ ]#lvcreate -L 5G -n /dev/vg0/lv0 vg0
  Logical volume "lv0" created.

查看逻辑卷信息(lvdisplay;lvs)

[[email protected] ~ ]#lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home centos -wi-ao---- 146.99g                                                    
  root centos -wi-ao----  50.00g                                                    
  swap centos -wi-ao----   2.00g                                                    
  lv0  vg0    -wi-ao----   5.00g   

使用 mkfs.xfs 格式化为 lv0 的xfs文件系统:

[[email protected] ~ ]#mkfs.xfs /dev/vg0/lv0 
meta-data=/dev/vg0/lv0           isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

挂载到/data目录下(你可以挂载到自己需要的目录下)

[[email protected] ~ ]#mount /dev/vg0/lv0 /data
[[email protected] ~ ]#lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  200G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  199G  0 part 
  ├─centos-root 253:0    0   50G  0 lvm  /
  ├─centos-swap 253:1    0    2G  0 lvm  [SWAP]
  └─centos-home 253:2    0  147G  0 lvm  /home
sdb               8:16   0   10G  0 disk 
└─sdb1            8:17   0   10G  0 part 
  └─vg0-lv0     253:3    0    5G  0 lvm  /data
sdc               8:32   0    5G  0 disk 
└─sdc1            8:33   0    5G  0 part 
sr0              11:0    1  8.8G  0 rom  

设置开机自动挂载,编辑 /etc/fstab 文件,加入最后一行 vim /etc/fstab

#
# /etc/fstab
# Created by anaconda on Tue Aug 14 16:09:13 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=936ee62c-93ff-4f19-ae07-1112add772b6 /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
UUID=8e7e026a-4025-4cee-ac6d-b3766fef9993  /data     xfs     defaults      0  0 





二、逻辑卷扩容


先使用vgs查看卷组剩余大小,有剩余空间在使用lvextend进行扩容,没有则要增加磁盘。

[[email protected] ~ ]#vgdisplay
  --- Volume group ---
  VG Name               vg0
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <10.00 GiB
  PE Size               4.00 MiB
  Total PE              2559
  Alloc PE / Size       1280 / 5.00 GiB
  Free  PE / Size       1279 / <5.00 GiB
  VG UUID               501kCS-IukG-st2J-5wUs-R7vQ-JdC9-wPjq0S

方法1:lvextend -L +1G /dev/app/app_lv

[[email protected] ~ ]#lvextend -l +100%FREE /dev/vg0/lv0
  Size of logical volume vg0/lv0 changed from 5.00 GiB (1280 extents) to <10.00 GiB (2559 extents).
  Logical volume vg0/lv0 successfully resized.

* 使用resize2f调整ext4格式文件系统大小 resize2fs /dev/app/app_lv

* 如果是xfs格式,使用xfs_growfs调整xfs格式文件系统大小 xfs_growfs /dev/app/app_lv


方法2:合并一条命令 lvextend -r -l +100%FREE /dev/vg0/lv0

[[email protected] ~ ]#lvextend -r -l +100%FREE /dev/vg0/lv0
  Size of logical volume vg0/lv0 unchanged from <10.00 GiB (2559 extents).
  Logical volume vg0/lv0 successfully resized.
meta-data=/dev/mapper/vg0-lv0    isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 1310720 to 2620416 

查看分区

[[email protected] ~ ]#lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  200G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  199G  0 part 
  ├─centos-root 253:0    0   50G  0 lvm  /
  ├─centos-swap 253:1    0    2G  0 lvm  [SWAP]
  └─centos-home 253:3    0  147G  0 lvm  /home
sdb               8:16   0   10G  0 disk 
└─sdb1            8:17   0   10G  0 part 
  └─vg0-lv0     253:2    0   10G  0 lvm  /data
sdc               8:32   0    5G  0 disk 
└─sdc1            8:33   0    5G  0 part 
sr0              11:0    1  8.8G  0 rom 





三、缩减逻辑卷(ext4格式)


1. 逻辑卷回缩不能在线进行,所以先卸载已经挂载的逻辑卷并检测文件系统

使用umount卸载 umount /dev/app/app_lv

[[email protected] ~ ]#umount /dev/vg0/lv0

使用e2fsck检测文件系统 e2fsck -f /dev/app/app_lv

[[email protected] ~ ]#e2fsck -f /dev/vg0/lv0 
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/vg0/lv0: 11/655360 files (0.0% non-contiguous), 83137/2620416 blocks

2. 使用resize2fs缩小文件系统为5G resize2f /dev/app/app_lv 5G

[[email protected] ~ ]#resize2fs /dev/vg0/lv0 1G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lv0 to 262144 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 262144 blocks long.

3. 使用lvreduce 缩小逻辑卷,上面要缩小到1G,原先是10G,所以这里减少9G lvreduce -L 1G /dev/vg0/lv0

[[email protected] ~ ]#lvreduce -L 1G /dev/vg0/lv0   
  WARNING: Reducing active logical volume to 1.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/lv0? [y/n]: y
  Size of logical volume vg0/lv0 changed from <10.00 GiB (2559 extents) to 1.00 GiB (256 extents).
  Logical volume vg0/lv0 successfully resized.

[[email protected] ~ ]#lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  200G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  199G  0 part 
  ├─centos-root 253:0    0   50G  0 lvm  /
  ├─centos-swap 253:1    0    2G  0 lvm  [SWAP]
  └─centos-home 253:3    0  147G  0 lvm  /home
sdb               8:16   0   10G  0 disk 
└─sdb1            8:17   0   10G  0 part 
  └─vg0-lv0     253:2    0    1G  0 lvm  
sdc               8:32   0    5G  0 disk 
└─sdc1            8:33   0    5G  0 part 
sr0              11:0    1  8.8G  0 rom

4. 挂载逻辑卷 mount /dev/vg0/lv0 /data

[[email protected] ~ ]#mount /dev/vg0/lv0 /data





四、逻辑卷删除

1. 先卸载你要删除的逻辑卷 umount /dev/app/app_lv

[[email protected] ~ ]#umount /dev/vg0/lv0 

2. 使用lvremove删除逻辑卷LV lvremove /dev/vg0/lv0

[[email protected] ~ ]#lvremove /dev/vg0/lv0 
Do you really want to remove active logical volume vg0/lv0? [y/n]: y
  Logical volume "lv0" successfully removed

3. 验证逻辑卷是否删除 lvdisplay | grep "/dev/vg0/lv0"

[[email protected] ~ ]#lvdisplay | grep "/dev/vg0/lv0"


4. 先使用vgchange关闭VG vgchange -a n vg0

[[email protected] ~ ]#vgchange -a n vg0
  0 logical volume(s) in volume group "vg0" now active

5.使用vgremove删除卷组VG vgremove /dev/vg0

[[email protected] ~ ]#vgremove /dev/vg0
  Volume group "vg0" successfully removed

6.使用pvremove删除物理卷PV pvremove /dev/sdb1

[[email protected] ~ ]#pvremove /dev/sdb1
___  Labels on physical volume "/dev/sdb1" successfully wiped.






以上是关于LVM (逻辑卷)的增加扩容缩小删除操作的主要内容,如果未能解决你的问题,请参考以下文章

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

LVM基本操作

LVM详细操作

CentOS 6.9之LVM创建,扩容等常用操作

创建逻辑卷(以及逻辑卷相关操作)

CentOS 6.9之LVM创建,扩容