lvm磁盘在线扩缩容

centos7.7根目录安装系统时使用lvm分配的磁盘空间太小,使用lvextend在线扩容根目录

post thumb
Ops
作者 Louis 发表于 2020年7月17日

[TOC]

背景: 公司安装centos7.7系统时, 分配的/目录才50G, 分配/home家目录800G. 部署应用后, 经常收到磁盘报警. 因此有了这篇文章. 使用lvreduce减少磁盘容量的时候. 这个是个危险操作, 注意备份文件!!!

查看目前的磁盘分配情况, 可以清楚的看到根目录太小. 且使用的是lvm磁盘.

$ lsblk 
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0 931.5G  0 disk 
├─sda1            8:1    0     1G  0 part /boot
└─sda2            8:2    0 930.5G  0 part 
  ├─centos-root 253:0    0    50G  0 lvm  /
  ├─centos-swap 253:1    0   7.9G  0 lvm  
  └─centos-home 253:2    0 872.6G  0 lvm  /home
sr0              11:0    1  1024M  0 rom 

扩容方案一

减少/home

首先umount卸载我们的lvm盘. 最好是增加磁盘, 不到必要时刻, 不要缩减磁盘.

$ umount /home
umount: /home: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
## 这个是有程序正在使用.  查看文件是由哪些程序在用.
$ lsof /home 

## 处理好程序使用问题, 再次卸载
$ umount /home
## 确定已经卸载.
$ lsblk 
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0 931.5G  0 disk 
├─sda1            8:1    0     1G  0 part /boot
└─sda2            8:2    0 930.5G  0 part 
  ├─centos-root 253:0    0    50G  0 lvm  /
  ├─centos-swap 253:1    0   7.9G  0 lvm  
  └─centos-home 253:2    0 872.6G  0 lvm  
sr0              11:0    1  1024M  0 rom 

我们可以使用lvreduce减少/home家目录的大小. 使用lvdisplay查看具体的细节.

使用lvreduce减少磁盘容量的操作. 这个是个危险操作, 操作之前注意备份文件!!!

$ lvdisplay 
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                LCp1Rl-0Gsh-9EXw-dStO-hYyS-FbYU-X42SzS
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-12-02 21:44:39 +0800
  LV Status              available
  # open                 0
  LV Size                <7.88 GiB
  Current LE             2016
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/home
  LV Name                home
  VG Name                centos
  LV UUID                0gSQDa-O00q-ktow-eKCT-GYTD-DOEi-1zF8on
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-12-02 21:44:40 +0800
  LV Status              available
  # open                 0
  LV Size                <872.63 GiB
  Current LE             223393
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                EYr2b1-7k7F-rLGl-B9sf-N4Rf-3Q33-rTuhCO
  LV Write Access        read/write
  LV Creation host, time localhost, 2019-12-02 21:44:59 +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     256
  Block device           253:0

## 使用lvreduce减少磁盘容量. 这个是个危险操作, 注意备份文件!!! 
## 使用lvreduce减少磁盘容量. 这个是个危险操作, 注意备份文件!!! 
## 使用lvreduce减少磁盘容量. 这个是个危险操作, 注意备份文件!!! 
## 这个是减少至80G. 
$  lvreduce  -L 80G /dev/centos/home 
  WARNING: Reducing active logical volume to 80.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos/home? [y/n]: y
  Size of logical volume centos/home changed from 800.00 GiB (204800 extents) to 80.00 GiB (20480 extents).
  Logical volume centos/home successfully resized.

使用vgdisplay查看我们的lvm还有多少FREE空间, 由于上面从800G减少至80G. 这边可以看到我们的vg的空间释放了. 有792.63 GiB.

$ vgdisplay 
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <930.51 GiB
  PE Size               4.00 MiB
  Total PE              238210
  Alloc PE / Size       35296 / <137.88 GiB
  Free  PE / Size       202914 / 792.63 GiB
  VG UUID               I96Nn6-dD0r-uDci-YbxF-owCn-vxJ6-ieviLy

增加/目录容量

使用lvextend增加我们的/目录. 这个时候, 我们的根目录已经增加到了最大了.

$ lvextend -l +100%FREE /dev/mapper/centos-root
  Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 842.63 GiB (215714 extents).
  Logical volume centos/root successfully resized.

使用命令查看, 发现获取的目录依旧是50G, 没有变化, 这里需要用xfs_growfs命令进行, redhat使用resize2fs

$ df -h
Filesystem         			Size  Used Avail Use% Mounted on
devtmpfs      				16G     0   16G   0% /dev
tmpfs         				16G     0   16G   0% /dev/shm
tmpfs         				16G  2.0M   16G   1% /run
tmpfs         				16G     0   16G   0% /sys/fs/cgroup
/dev/mapper/centos-root     50G   21G   30G  41% /
/dev/sda1                   1014M  223M  792M  22% /boot

# for centos
$ xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=68, agsize=3276800 blks
         =                       sectsz=4096  attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=220891136, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=6400, version=2
         =                       sectsz=4096  sunit=1 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

# for redhat
$ resize2fs /dev/mapper/centos-root
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/mapper/centos-root
Couldn't find valid filesystem superblock.

意外情况

我们lvreduce的磁盘挂载出现问题. 这里我只能重新格式化了, 提醒大家使用lvm扩缩容先备份文件. 特别是要减少的磁盘文件.

$ mount /dev/mapper/centos-home  /home/
mount: /dev/mapper/centos-home: can't read superblock
$ xfs_repair /dev/mapper/centos-home
.....
couldn't verify primary superblock - attempted to perform I/O beyond EOF
.....

只能重新格式化了

## 格式化文件系统. 请注意操作前备份. 
$ mkfs.xfs -f /dev/mapper/centos-home
$ mount /dev/mapper/centos-home  /home/

扩容方案二

直接增加一块盘, 最快捷了, 这理添加了一块/dev/sdb

$ fdisk /dev/sdb
lvm是需要更改类型的. t -> 8e
WARNING: If you have created or modified any DOS 6.xpartitions, please see the fdisk manual page for additionalinformation.
Changed type of partition 'FAT12' to 'Linux LVM'

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    83886079    41942016   8e  Linux LVM


[root@dev-k8s-master ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  
sdb               8:16   0   40G  0 disk 
└─sdb1            8:17   0   40G  0 part 
sr0              11:0    1  942M  0 rom 

创建pv

$ pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
$ pvs
  PV         VG     Fmt  Attr PSize   PFree  
  /dev/sda2  centos lvm2 a--  <19.00g      0 
  /dev/sdb1         lvm2 ---  <40.00g <40.00g

扩容vg

$ vgextend  centos /dev/sdb1 
  Volume group "centos" successfully extended
$ vgs
  VG     #PV #LV #SN Attr   VSize  VFree  
  centos   2   2   0 wz--n- 58.99g <40.00g

扩容lvm并且resize

$ lvextend -l +100%FREE /dev/mapper/centos-root
  Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to 56.99 GiB (14590 extents).
  Logical volume centos/root successfully resized.
$ xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=1113856 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=4455424, 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 4455424 to 14940160

查看

[root@dev-k8s-master ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   57G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  
sdb               8:16   0   40G  0 disk 
└─sdb1            8:17   0   40G  0 part 
  └─centos-root 253:0    0   57G  0 lvm  /
sr0              11:0    1  942M  0 rom 

记录一下~

Tags: