生产环境lvm磁盘扩容!!!

一次就好,亲身体验生产环境lvm磁盘扩容

这一天体验了真正的生产环境“三急”!中午客户打电话说报表几个小时没更新了,是不是你们系统有问题啊… 于是开始排除发现磁盘空间满了,需要进行扩容,咱又没有扩容经验潜心研究一下午,终于得出结论 ,以下将描述我亲身扩容的详细步骤

提前说明:此次有两个实验,一:测试环境从磁盘分区到扩容完整步骤,二:正式环境从物理卷直接扩容到lvm上(按需索取,内容相差不大)

一.测试环境完整步骤

  1. 使用df -Th 命令查看磁盘信息 ,可以看到当前lvm逻辑卷容量是38G
	[root@Z ~]# df -TH
	文件系统                类型      容量  已用  可用 已用% 挂载点
	/dev/mapper/centos-root xfs        38G  2.4G   36G    7% /
	devtmpfs                devtmpfs  1.1G     0  1.1G    0% /dev
	tmpfs                   tmpfs     1.1G     0  1.1G    0% /dev/shm
	tmpfs                   tmpfs     1.1G   10M  1.1G    1% /run
	tmpfs                   tmpfs     1.1G     0  1.1G    0% /sys/fs/cgroup
	/dev/sda1               xfs       1.1G  139M  925M   14% /boot
	tmpfs                   tmpfs     208M     0  208M    0% /run/user/0
  1. 查看磁盘分区,可以看到sda 磁盘64G,下面sda2分区分了40G
[root@Z ~]# fdisk -l

磁盘 /dev/sda:64.4 GB, 64424509440 字节,125829120 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000b05f5

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    83886079    40893440   8e  Linux LVM


磁盘 /dev/mapper/centos-root:37.7 GB, 37706792960 字节,73646080 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-swap:4160 MB, 4160749568 字节,8126464 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
  1. 新建磁盘分区sda3
[root@Z ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。


命令(输入 m 获取帮助):p (p是查看当前分区)

磁盘 /dev/sda:64.4 GB, 64424509440 字节,125829120 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000b05f5

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    83886079    40893440   8e  Linux LVM




命令(输入 m 获取帮助):n(n是新建分区)
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p):   (回车默认选择p主分区)
Using default response p
分区号 (3,4,默认 3):   (回车默认3)
起始 扇区 (83886080-125829119,默认为 83886080):
将使用默认值 83886080
Last 扇区, +扇区 or +size{K,M,G} (83886080-125829119,默认为 125829119):+19G(这里给新建分区19G)
分区 3 已设置为 Linux 类型,大小设为 19 GiB

命令(输入 m 获取帮助):t(指定类型)
分区号 (1-3,默认 3):(要指定的分区号,刚刚默认3,这里也默认3)
Hex 代码(输入 L 列出所有代码):8e   (8e代表LVM)
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):w  (w保存)
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
正在同步磁盘。

注意:保存后最后几行提示设备资源忙,这时同步不了磁盘,需要重启服务器或者执行partprobe,我这里是执行的命令,如果这里不做操作的话,下面格式化磁盘的时候会报错!!!

  1. 查看分区,及格式化分区
[root@Z ~]# fdisk -l

磁盘 /dev/sda:64.4 GB, 64424509440 字节,125829120 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000b05f5

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    83886079    40893440   8e  Linux LVM
/dev/sda3        83886080   123731967    19922944   8e  Linux LVM

磁盘 /dev/mapper/centos-root:37.7 GB, 37706792960 字节,73646080 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

格式化分区
[root@Z ~]# mkfs.xfs  -f  /dev/sda3
meta-data=/dev/sda3              isize=512    agcount=4, agsize=1245184 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=4980736, 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

不执行"注意"命令或不重启设备,格式化时报找不到设备:
[root@Z ~]# mkfs.xfs  -f  /dev/sda3
/dev/sda3: 没有那个文件或目录

  1. 将分区转换为物理卷,使用pvdisplay命令查看物理卷信息:pvdisplay
转换物理卷
[root@Z ~]# pvcreate /dev/sda3
 WARNING: xfs signature detected on /dev/sda3 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/sda3.
  Physical volume "/dev/sda3" successfully created.

查看物理卷
 [root@Z ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <39.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              9983
  Free PE               1
  Allocated PE          9982
  PV UUID               v5DSQS-Cumb-xtyK-BWsx-ayfS-T0iT-FC3rbE
   
  "/dev/sda3" is a new physical volume of "19.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name               
  PV Size               19.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               Xkbr0u-Ce9h-jvqW-JSZr-nwxY-qYNo-OSqqWm
  1. 使用pvdisplay命令查看物理卷信息:pvdisplay,看到总容量Alloc PE 为38.9, Free 可分配容量没了
[root@Z ~]# 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               <39.00 GiB
  PE Size               4.00 MiB
  Total PE              9983
  Alloc PE / Size       9982 / 38.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               hTO0Fm-c4q8-yboo-cSig-XLcK-CXo7-JoTInz
  1. 将刚才初始化的物理卷添加到主分区的物理卷:vgextend centos(vg name ) /dev/sda3,添加后查看vgdisplay,可以看到Free 可分配容量有了19G
[root@Z ~]# vgextend centos /dev/sda3
  Volume group "centos" successfully extended
  
[root@Z ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               57.99 GiB
  PE Size               4.00 MiB
  Total PE              14846
  Alloc PE / Size       9982 / 38.99 GiB
  Free  PE / Size       4864 / 19.00 GiB
  VG UUID               hTO0Fm-c4q8-yboo-cSig-XLcK-CXo7-JoTInz
  1. 查看逻辑卷lvm信息: lvdisplay,主要看 LV Path ,LV Size
[root@Z ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                vfUMhU-5XVp-s4uB-lVs3-436b-29Vh-StDsYb
  LV Write Access        read/write
  LV Creation host, time localhost, 2022-03-30 16:58:54 +0800
  LV Status              available
  # open                 2
  LV Size                <3.88 GiB
  Current LE             992
  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                4KsFu6-jk8H-WsUa-1RVM-2DUq-gyPE-Nb6Rcx
  LV Write Access        read/write
  LV Creation host, time localhost, 2022-03-30 16:58:54 +0800
  LV Status              available
  # open                 1
  LV Size                <35.12 GiB
  Current LE             8990
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

  1. 使用lvextend命令扩容分区:lvextend -l +4864 /dev/centos/root,扩容完lvdisplay查看 LV size 大小
    4864是:Free PE的id 号 通过vgdisplay命令查看
    /dev/centos/root 是:LV path 通过lvdisplay 命令查看
[root@Z ~]# lvextend -l +4864 /dev/centos/root
  Size of logical volume centos/root changed from <35.12 GiB (8990 extents) to <54.12 GiB (13854 extents).
  Logical volume centos/root successfully resized.

[root@Z ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                vfUMhU-5XVp-s4uB-lVs3-436b-29Vh-StDsYb
  LV Write Access        read/write
  LV Creation host, time localhost, 2022-03-30 16:58:54 +0800
  LV Status              available
  # open                 2
  LV Size                <3.88 GiB
  Current LE             992
  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                4KsFu6-jk8H-WsUa-1RVM-2DUq-gyPE-Nb6Rcx
  LV Write Access        read/write
  LV Creation host, time localhost, 2022-03-30 16:58:54 +0800
  LV Status              available
  # open                 1
  LV Size                <54.12 GiB
  Current LE             13854
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
  1. 挂载:xfs_growfs /dev/centos/root ;
    磁盘类型为xfs 所以使用:xfs_frowfs 命令
    磁盘类型为ext4类型则使用:resize2fs 命令
[root@Z ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=2301440 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=9205760, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=4495, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 9205760 to 14186496

11.df -TH 查看磁盘信息

[root@Z ~]# df -TH
文件系统                类型      容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root xfs        59G  2.4G   56G    5% /
devtmpfs                devtmpfs  1.1G     0  1.1G    0% /dev
tmpfs                   tmpfs     1.1G     0  1.1G    0% /dev/shm
tmpfs                   tmpfs     1.1G   10M  1.1G    1% /run
tmpfs                   tmpfs     1.1G     0  1.1G    0% /sys/fs/cgroup
/dev/sda1               xfs       1.1G  139M  925M   14% /boot
tmpfs                   tmpfs     208M     0  208M    0% /run/user/0

二.正式环境特殊情况

呃呃呃,正式环境是鄙人当时创建的lvm,当时分区vdb1给了300G,但是创建的lvm确给了160G,没用几个月业务就导致磁盘爆满,参照完整步骤,我这里只需要把物理卷剩余的容量分给lvm即可,开始操作

  1. 查看df -Th, /home确实满了
[root@sm dev]# df -TH
Filesystem             Type      Size  Used Avail Use% Mounted on
devtmpfs               devtmpfs  8.4G     0  8.4G   0% /dev
tmpfs                  tmpfs     8.4G   25k  8.4G   1% /dev/shm
tmpfs                  tmpfs     8.4G  631k  8.4G   1% /run
tmpfs                  tmpfs     8.4G     0  8.4G   0% /sys/fs/cgroup
/dev/vda1              ext4       53G  5.8G   45G  12% /
/dev/mapper/datavg-lv0 ext4      159G  132G   19G  88% /home
tmpfs                  tmpfs     1.7G     0  1.7G   0% /run/user/0
  1. 查看vg卷组 :vgdisplay ,Free PE可用容量为150,这就是vdb300G分了160给lvm,卷组剩余的容量
[root@sm dev]# vgdisplay
  --- Volume group ---
  VG Name               datavg
  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               <300.00 GiB
  PE Size               4.00 MiB
  Total PE              76799
  Alloc PE / Size       38400 / 150.00 GiB
  Free  PE / Size       38399 / <150.00 GiB
  VG UUID               t1PBKO-ukOY-OYNd-mqAx-AK4H-Lpcx-42u4Fl
  1. 查看逻辑卷lvm信息: lvdisplay,主要看 LV Path ,LV Size
[root@sm dev]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/datavg/lv0
  LV Name                lv0
  VG Name                datavg
  LV UUID                3wObg6-kwlf-RB6g-bmPv-HFGi-5B2i-t52Z5j
  LV Write Access        read/write
  LV Creation host, time VM-30-13-centos, 2021-11-11 16:44:19 +0800
  LV Status              available
  # open                 1
  LV Size                150.00 GiB
  Current LE             38400
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           252:0

4.使用lvextend命令扩容分区:lvextend -l +38399 /dev/datavg/lv0,扩容完lvdisplay查看 LV size 大小
38399是:Free PE的id 号 通过vgdisplay命令查看
/dev/datavg/lv0 是:LV path 通过lvdisplay 命令查看

[root@sm dev]# lvextend -l +38399 /dev/datavg/lv0
  Size of logical volume datavg/lv0 changed from 150.00 GiB (38400 extents) to <300.00 GiB (76799 extents).
  Logical volume datavg/lv0 successfully resized.
  1. lvextend命令查看LV Size是否是扩容后的容量
    vgdisplay命苦查看 Free PE是否还有可用容量,因上一步将剩余容量全部添加到lvm内,此时FreePE应为0
[root@smp3 dev]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/datavg/lv0
  LV Name                lv0
  VG Name                datavg
  LV UUID                3wObg6-kwlf-RB6g-bmPv-HFGi-5B2i-t52Z5j
  LV Write Access        read/write
  LV Creation host, time VM-30-13-centos, 2021-11-11 16:44:19 +0800
  LV Status              available
  # open                 1
  LV Size                <300.00 GiB
  Current LE             76799
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           252:0
   
[root@sm dev]# vgdisplay
  --- Volume group ---
  VG Name               datavg
  System ID             
  Format                lvm2
  Metadata Areas        1
  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                1
  Act PV                1
  VG Size               <300.00 GiB
  PE Size               4.00 MiB
  Total PE              76799
  Alloc PE / Size       76799 / <300.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               t1PBKO-ukOY-OYNd-mqAx-AK4H-Lpcx-42u4Fl
  1. 挂载:resize2fs /dev/datavg/lv0 ,因正式环境磁盘类型为ext4,所以使用resize2fs命令进行挂载,
    挂载后df -Th 看下容量是否正确
[root@sm dev]# resize2fs /dev/datavg/lv0 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/datavg/lv0 is mounted on /home; on-line resizing required
old_desc_blocks = 19, new_desc_blocks = 38
The filesystem on /dev/datavg/lv0 is now 78642176 blocks long.

[root@sm dev]# df -TH
Filesystem             Type      Size  Used Avail Use% Mounted on
devtmpfs               devtmpfs  8.4G     0  8.4G   0% /dev
tmpfs                  tmpfs     8.4G   25k  8.4G   1% /dev/shm
tmpfs                  tmpfs     8.4G  631k  8.4G   1% /run
tmpfs                  tmpfs     8.4G     0  8.4G   0% /sys/fs/cgroup
/dev/vda1              ext4       53G  5.8G   45G  12% /
/dev/mapper/datavg-lv0 ext4      317G  132G  171G  44% /home
tmpfs                  tmpfs     1.7G     0  1.7G   0% /run/user/0

至此扩容完成,也因此事对磁盘的理解更上一层楼,知识浅薄,不成敬意,有什么疑问评论区提出

你可能感兴趣的:(笔记,linux,运维,centos,服务器)