欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

详解基于Linux的LVM无缝磁盘水平扩容

程序员文章站 2022-03-20 13:52:58
环境 名称 属性 cpu x5650...

环境

名称 属性
cpu x5650
内存 4g
磁盘 20g+4tb

详解基于Linux的LVM无缝磁盘水平扩容 

详解基于Linux的LVM无缝磁盘水平扩容

这时候,我们假设服务器已经有如下东西:

  • 安装好了parted(yum install parted)
  • 分区容量已经告捷,公司为服务器增加了一块4t硬盘用来增加容量!
  • 除了/boot分区外,其余都为lvm 模式的扩展逻辑分区

软件安装与信息查看

首先查看软件是否安装:

[root@candy ~]# rpm -qa | grep lvm
lvm2-libs-2.02.118-2.el6.x86_64
lvm2-2.02.118-2.el6.x86_64
[root@candy ~]# lv
lvchange   lvextend   lvmdiskscan lvmsar    lvresize
lvconvert  lvm     lvmdump   lvreduce   lvs
lvcreate   lvmchange  lvmetad   lvremove   lvscan
lvdisplay  lvmconf   lvmsadc   lvrename
[root@candy ~]# rpm -qa | grep parted
parted-2.1-29.el6.x86_64

软件准备好后顺便查看下 vm 硬件:

[root@candy ~]# df -h
filesystem      size used avail use% mounted on
/dev/mapper/vg_candy-root
           7.3g 231m 6.7g  4% /
tmpfs         1.9g   0 1.9g  0% /dev/shm
/dev/sda1       477m  30m 422m  7% /boot
/dev/mapper/vg_candy-home
           3.9g 8.0m 3.7g  1% /home
/dev/mapper/vg_candy-usr
           3.9g 382m 3.3g 11% /usr
/dev/mapper/vg_candy-var
           3.9g  63m 3.6g  2% /var
[root@candy ~]# fdisk -l
disk /dev/sda: 21.5 gb, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x0006e71c
  device boot   start     end   blocks  id system
/dev/sda1  *      1     64   512000  83 linux
partition 1 does not end on cylinder boundary.
/dev/sda2       64    2611  20458496  8e linux lvm
disk /dev/sdb: 4398.0 gb, 4398046511104 bytes
255 heads, 63 sectors/track, 534698 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: 0x00000000
disk /dev/mapper/vg_candy-root: 8061 mb, 8061452288 bytes
255 heads, 63 sectors/track, 980 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: 0x00000000
disk /dev/mapper/vg_candy-usr: 4294 mb, 4294967296 bytes
255 heads, 63 sectors/track, 522 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: 0x00000000
disk /dev/mapper/vg_candy-var: 4294 mb, 4294967296 bytes
255 heads, 63 sectors/track, 522 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: 0x00000000
disk /dev/mapper/vg_candy-home: 4294 mb, 4294967296 bytes
255 heads, 63 sectors/track, 522 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: 0x00000000
[root@candy ~]# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sda2 /dev/sdb

这时候我们看到,centos 已经知道有个sdb的盘了,但是没挂载与分区。

使用parted进行扩容前的分区

现在假设除了/boot 其余分区都满了! 我们需要先进行分区,将4t的硬盘用上(/dev/sdb).

[root@candy ~]# parted /dev/sdb
gnu parted 2.1
使用 /dev/sdb
welcome to gnu parted! type 'help' to view a list of commands.
(parted) mklabel
新的磁盘标签类型? gpt
(parted) mkpart
分区名称? []?
文件系统类型? [ext2]? ext4
起始点? 0t
结束点? 4t
(parted) p
model: vmware, vmware virtual s (scsi)
disk /dev/sdb: 4398gb
sector size (logical/physical): 512b/512b
partition table: gpt
number start  end   size  file system name 标志
 1   1049kb 4398gb 4398gb
(parted)set 1 lvm on
(parted)p
model: vmware, vmware virtual s (scsi)
disk /dev/sdb: 4398gb
sector size (logical/physical): 512b/512b
partition table: gpt
number start  end   size  file system name 标志
1   1049kb 4398gb 4398gb           lvm
(parted)q
[root@candy ~]# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdb1

现在让我们来解释一下操作:

  1. mklabel将磁盘转换为gpt分区,因为mbr分区无法支持tb级硬盘(具体请自行查阅,parted当然也支持tb级以下的操作)
  2. mkpart将转换好的gpt磁盘进行分区,需要提供文件系统类型、开始点、结束点。
  3. 输入p 打印出刚才才做后生成的分区。
  4. set 1 lvm on 是将num为1的分区,tager设置为lvm ,这样才能添加到vm卷组里

下一步,我们查看我们的lvm组名叫什么:

[root@candy ~]# vgs
 vg    #pv #lv #sn attr  vsize vfree
 vg_candy  1  4  0 wz--n- 19.51g  0
 
[root@candy ~]# vgdisplay
 --- volume group ---
 vg name        vg_candy
 system id
 format        lvm2
 metadata areas    1
 metadata sequence no 5
 vg access       read/write
 vg status       resizable
 max lv        0
 cur lv        4
 open lv        4
 max pv        0
 cur pv        1
 act pv        1
 vg size        19.51 gib
 pe size        4.00 mib
 total pe       4994
 alloc pe / size    4994 / 19.51 gib
 free pe / size    0 / 0
 vg uuid        gcm5ml-fc88-xk5q-xupc-kj7y-alfk-i770gc
[root@candy ~]# lvscan
 active      '/dev/vg_candy/usr' [4.00 gib] inherit
 active      '/dev/vg_candy/var' [4.00 gib] inherit
 active      '/dev/vg_candy/home' [4.00 gib] inherit
 active      '/dev/vg_candy/root' [7.51 gib] inherit
[root@candy ~]#

知道lvm卷组叫 'vg_candy'了, 我们就使用vgextend来向lvm 'vg_candy' 卷组添加成员了.

[root@candy ~]# vgextend vag_candy /dev/sdb1
physical volume "/dev/sdb1" successfully created
volume group "vg_candy" successfully extended

现在我们再用vgs命令看一下pv个数:

[root@candy ~]# vgs
vg    #pv #lv #sn attr  vsize vfree
vg_candy  2  4  0 wz--n- 4.02t 4.00t

大家对比未添加时的vgs是否有看到pv个数从1变为2了!

现在让我们用vgdisplay看看现在分区的容量.

[root@candy ~]# vgdisplay
 --- volume group ---
 vg name        vg_candy
 system id
 format        lvm2
 metadata areas    2
 metadata sequence no 6
 vg access       read/write
 vg status       resizable
 max lv        0
 cur lv        4
 open lv        4
 max pv        0
 cur pv        2
 act pv        2
 vg size        4.02 tib
 pe size        4.00 mib
 total pe       1053569
 alloc pe / size    4994 / 19.51 gib
 free pe / size    1048575 / 4.00 tib
 vg uuid        gcm5ml-fc88-xk5q-xupc-kj7y-alfk-i770gc

再次对比未添加的vgdiplay命令发现,freepe 的value 从 0 / 0 变为 1048575 / 4.00 tib . 这样说明candy已经成功将4tb的lvm 分区添加进入到vg_candy 逻辑卷组里了。

现在我们对所有逻辑分区进行扩容前,df -h 看看有哪些分区。

[root@candy ~]# df -h
filesystem         size used avail use% mounted on
/dev/mapper/vg_candy-root 7.3g 231m 6.7g  4% /
 tmpfs           1.9g   0 1.9g  0% /dev/shm
/dev/sda1         477m  30m 422m  7% /boot
/dev/mapper/vg_candy-home 3.9g 8.0m 3.7g  1% /home         
/dev/mapper/vg_candy-usr  3.9g 382m 3.3g 11% /usr
/dev/mapper/vg_candy-var  3.9g  63m 3.6g  2% /var

现在麻烦的事已经做完了,接下来就剩下为分区进行扩容了.

为磁盘进行扩容

假设:我们需要向 这些分区都添加500g容量: //home/usr/var

[root@candy ~]# lvextend -l +500g /dev/mapper/vg_candy-root
 size of logical volume vg_candy/root changed from 7.51 gib (1922 extents) to 507.51 gib (129922 extents).
 logical volume root successfully resized
 
[root@candy ~]# lvextend -l +500g /dev/mapper/vg_candy-home
 size of logical volume vg_candy/home changed from 4.00 gib (1024 extents) to 504.00 gib (129024 extents).
 logical volume home successfully resized
 
[root@candy ~]# lvextend -l +500g /dev/mapper/vg_candy-usr
 size of logical volume vg_candy/usr changed from 4.00 gib (1024 extents) to 504.00 gib (129024 extents).
 logical volume usr successfully resized
 
[root@candy ~]# lvextend -l +500g /dev/mapper/vg_candy-var
 size of logical volume vg_candy/var changed from 4.00 gib (1024 extents) to 504.00 gib (129024 extents).
 logical volume var successfully resized

大家注意,这一步操作时不可逆的! 如果输入了错误的分区名字还好! 如果添加到其它分区了,就得重复n遍来缩容,比较麻烦哦! 建议大家注仔细核对上面的filesystem. 切记切记....

提示扩容成功! 让我们再次用上述命令查看lvm信息:

[root@candy ~]# vgs
 vg    #pv #lv #sn attr  vsize vfree
 vg_candy  2  4  0 wz--n- 4.02t 2.05t 
 
[root@candy ~]# lvscan
 active      '/dev/vg_candy/usr' [504.00 gib] inherit
 active      '/dev/vg_candy/var' [504.00 gib] inherit
 active      '/dev/vg_candy/home' [504.00 gib] inherit
 active      '/dev/vg_candy/root' [507.51 gib] inherit
 
[root@candy ~]# vgdisplay
 --- volume group ---
 vg name        vg_candy
 system id
 format        lvm2
 metadata areas    2
 metadata sequence no 10
 vg access       read/write
 vg status       resizable
 max lv        0
 cur lv        4
 open lv        4
 max pv        0
 cur pv        2
 act pv        2
 vg size        4.02 tib
 pe size        4.00 mib
 total pe       1053569
 alloc pe / size    516994 / 1.97 tib
 free pe / size    536575 / 2.05 tib
 vg uuid        gcm5ml-fc88-xk5q-xupc-kj7y-alfk-i770gc
 
[root@candy ~]# df -h
filesystem         size used avail use% mounted on
/dev/mapper/vg_candy-root 7.3g 231m 6.7g  4% /
 tmpfs           1.9g   0 1.9g  0% /dev/shm
/dev/sda1         477m  30m 422m  7% /boot
/dev/mapper/vg_candy-home 3.9g 8.0m 3.7g  1% /home         
/dev/mapper/vg_candy-usr  3.9g 382m 3.3g 11% /usr
/dev/mapper/vg_candy-var  3.9g  63m 3.6g  2% /var

看到这里大家可能奇怪! 为什么df -h 还没变呢?

刷新磁盘信息与写入

我们需要用resize2fs 重设分区大小, 这样才能使df -h 也能生效.

而resize2fs 需要的时间与你给予的容量线性叠加. 也就是说你给的容量越多,resize2fs 的需要的时间就越多。

[root@candy ~]# resize2fs /dev//mapper/vg_candy-root
resize2fs 1.41.12 (17-may-2010)
filesystem at /dev//mapper/vg_candy-root is mounted on /; on-line resizing required old desc_blocks = 1, new_desc_blocks = 32
performing an on-line resize of /dev//mapper/vg_candy-root to 133040128 (4k) blocks.
the filesystem on /dev//mapper/vg_candy-root is now 133040128 blocks long.

[root@candy ~]# resize2fs /dev//mapper/vg_candy-usr
resize2fs 1.41.12 (17-may-2010)
filesystem at /dev//mapper/vg_candy-usr is mounted on /usr; on-line resizing required old desc_blocks = 1, new_desc_blocks = 32
performing an on-line resize of /dev//mapper/vg_candy-usr to 132120576 (4k) blocks.
the filesystem on /dev//mapper/vg_candy-usr is now 132120576 blocks long.

[root@candy ~]# resize2fs /dev//mapper/vg_candy-var
resize2fs 1.41.12 (17-may-2010)
filesystem at /dev//mapper/vg_candy-var is mounted on /var; on-line resizing required old desc_blocks = 1, new_desc_blocks = 32
performing an on-line resize of /dev//mapper/vg_candy-var to 132120576 (4k) blocks.
the filesystem on /dev//mapper/vg_candy-var is now 132120576 blocks long.

[root@candy ~]# resize2fs /dev//mapper/vg_candy-home
resize2fs 1.41.12 (17-may-2010)
filesystem at /dev//mapper/vg_candy-home is mounted on /home; on-line resizing required old desc_blocks = 1, new_desc_blocks = 32
performing an on-line resize of /dev//mapper/vg_candy-home to 132120576 (4k) blocks.
the filesystem on /dev//mapper/vg_candy-home is now 132120576 blocks long.

[root@candy ~]# df -h
filesystem          size used avail use% mounted on
/dev/mapper/vg_candy-root   500g 245m 474g  1% /
tmpfs             1.9g   0 1.9g  0% /dev/shm
/dev/sda1           477m  30m 422m  7% /boot
/dev/mapper/vg_candy-home   496g  16m 471g  1% /home
/dev/mapper/vg_candy-usr   496g 390m 471g  1% /usr
/dev/mapper/vg_candy-var   496g  71m 471g  1% /var

结束语

顺便给大家晒一张重启后系统自动挂载的图!

详解基于Linux的LVM无缝磁盘水平扩容

详解基于Linux的LVM无缝磁盘水平扩容

linux系统还是比较聪明的. 其实重启与不重启都能使用, 只是为大家演示重启后的效果.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。