Centos 7.5利用LVM实现动态扩容大小

62 篇文章 1 订阅

 

Centos 7.5 利用LVM实现动态扩容大小

操作系统:Centos 7.5 64位

说明:当有在操作系统时发现磁盘空间不足时,可以通过增加磁盘空间大小来满足,但是以哪种方式来增加可能有不同方法,现在针对操作系统在安装采用LVM方式来动态调整磁盘空间大小,

知识点解释:

 LVM是逻辑盘卷管理(Logical VolumeManager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。通过LVM系统管理员可以轻松管理磁盘分区,如:将若干个磁盘分区连接为一个整块的卷组(volumegroup),形成一个存储池。管理员可以在卷组上随意创建逻辑卷组(logicalvolumes),并进一步在逻辑卷组上创建文件系统。管理员通过LVM可以方便的调整存储卷组的大小,并且可以对磁盘存储按照组的方式进行命名、管理和分配

Linux的LVM非常强大,可以在生产运行系统上面直接在线扩展硬盘分区,可以把分区umount以后收缩分区大小,还可以在系统运行过程中把一个分区从一块硬盘搬到另一块硬盘上面去等等,简直就像变魔术,而且这一切都可以在一个繁忙运行的系统上面直接操作,不会对你的系统运行产生任何影响,很安全。

LVM使用有局限性。虽然能很方便的扩容和缩容磁盘的空间(扩容磁盘大小,文件不丢失),但是一旦出现问题,数据丢失,想要恢复数据就有点困难!

三步:

①创建一个物理分区-搞成物理卷②制作成一个卷组③划分成逻辑卷

 

1.如果在安装系统时选择是以LVM方式安装的话,可以通过已经安装好后系统磁盘查询

[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   70G  9.4G   61G   14% /
devtmpfs                 4.8G     0  4.8G    0% /dev
tmpfs                    4.9G     0  4.9G    0% /dev/shm
tmpfs                    4.9G  9.5M  4.8G    1% /run
tmpfs                    4.9G     0  4.9G    0% /sys/fs/cgroup
/dev/sda1               1014M  171M  844M   17% /boot
/dev/mapper/centos-home   27G   37M   27G    1% /home
tmpfs                    984M   12K  984M    1% /run/user/42
tmpfs                    984M     0  984M    0% /run/user/0

说明:centos-root 和centos-home 是通过LVM方式来配置的

2、使用df  -T -h命令查看操作系统的文件类型

[root@localhost ~]#  df  -T -h
文件系统                类型      容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root xfs        70G  9.4G   61G   14% /
devtmpfs                devtmpfs  4.8G     0  4.8G    0% /dev
tmpfs                   tmpfs     4.9G     0  4.9G    0% /dev/shm
tmpfs                   tmpfs     4.9G  9.5M  4.8G    1% /run
tmpfs                   tmpfs     4.9G     0  4.9G    0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  171M  844M   17% /boot
/dev/mapper/centos-home xfs        27G   37M   27G    1% /home
tmpfs                   tmpfs     984M   12K  984M    1% /run/user/42
tmpfs                   tmpfs     984M     0  984M    0% /run/user/0

说明:通过查询发现操作系统的文件格式是:xfs

3.查看卷组名称及卷组使用情况  vgdisplay

[root@localhost ~]#  vgdisplay
  --- Volume group ---
  VG Name               centos --VG名称
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               98.99 GiB --总共的空间大小
  PE Size               4.00 MiB
  Total PE              25342
  Alloc PE / Size       25341 / <98.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               oCJsUm-wF9P-a0Pc-rLvl-aCa1-0Inr-psWPzn

 

4.查看当前逻辑卷的空间状态,如下内容:/ 70G、/home 26G、swap 2G

[root@localhost ~]# lvdisplay
  --- Logical volume ---
  LV Path                 /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                0F9zrD-AfsY-teOk-OLqB-ZKa7-6fcZ-aVQ0VM
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-09-14 17:08:41 +0800
  LV Status              available
  # open                 1
  LV Size                70.00 GiB
  Current LE             17920
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/centos/home
  LV Name                home
  VG Name                centos
  LV UUID                v64Mg2-gDRG-UGoe-InX6-qqsG-GuiD-eRrcs8
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-09-14 17:08:42 +0800
  LV Status              available
  # open                 1
  LV Size                <26.99 GiB
  Current LE             6909
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                0le8V4-P7XQ-jtC7-7xtY-wKUv-dcNe-mJrB4T
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-09-14 17:08:43 +0800
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

5.现在感觉/ 70G不够用,想扩容增加300G,新插入一块300G硬盘;用‘fdisk -l’查看

[root@localhost ~]# fdisk -l

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

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

磁盘 /dev/sdb:322.1 GB, 322122547200 字节,629145600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


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


磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


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

[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   70G  9.4G   61G   14% /
devtmpfs                 4.8G     0  4.8G    0% /dev
tmpfs                    4.9G     0  4.9G    0% /dev/shm
tmpfs                    4.9G  9.5M  4.8G    1% /run
tmpfs                    4.9G     0  4.9G    0% /sys/fs/cgroup
/dev/sda1               1014M  171M  844M   17% /boot
/dev/mapper/centos-home   27G   37M   27G    1% /home
tmpfs                    984M   12K  984M    1% /run/user/42
tmpfs                    984M     0  984M    0% /run/user/0

6、格式化/dev/sdb磁盘


[root@localhost ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

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

Device does not contain a recognized partition table
使用磁盘标识符 0xbe0b32e5 创建新的 DOS 磁盘标签。

命令(输入 m 获取帮助):p

磁盘 /dev/sdb:322.1 GB, 322122547200 字节,629145600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xbe0b32e5

   设备 Boot      Start         End      Blocks   Id  System

 

命令(输入 m 获取帮助):m
命令操作
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

命令(输入 m 获取帮助):n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-629145599,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-629145599,默认为 629145599):
将使用默认值 629145599
分区 1 已设置为 Linux 类型,大小设为 300 GiB

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

Calling ioctl() to re-read partition table.
正在同步磁盘。

7、将 /dev/sdb配置为LVM格式

[root@localhost ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

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


命令(输入 m 获取帮助):t
已选择分区 1
Hex 代码(输入 L 列出所有代码):l

 
Hex 代码(输入 L 列出所有代码):8e
已将分区“Linux”的类型更改为“Linux LVM”

命令(输入 m 获取帮助):p

磁盘 /dev/sdb:322.1 GB, 322122547200 字节,629145600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xbe0b32e5

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   629145599   314571776   8e  Linux LVM

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

Calling ioctl() to re-read partition table.
正在同步磁盘。

8、查询配置为LVM格式后结果如下:


[root@localhost ~]# fdisk -l

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

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

磁盘 /dev/sdb:322.1 GB, 322122547200 字节,629145600 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0xbe0b32e5

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   629145599   314571776   8e  Linux LVM

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


磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


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

9.将新加的分区 /dev/sdb1 创建为物理卷

[root@localhost ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.

10.给卷组‘centos ’扩容,将物理卷 /dev/sdb1 扩展至‘centos ’卷组

[root@localhost ~]# vgextend centos /dev/sdb1
  Volume group "centos" successfully extended

 

10.此时卷组‘centos ’有300G空余空间,及 /dev/sdb1,将其全部扩展至 /home

root@localhost ~]# lvextend -l +100%FREE /dev/mapper/centos-home
  Size of logical volume centos/home changed from <26.99 GiB (6909 extents) to <326.99 GiB (83709 extents).
  Logical volume centos/home successfully resized.

11.发现扩展错分区了,把第10步的增加空间给缩小回去到原来值,26G


[root@localhost ~]# lvreduce -L -300G /dev/mapper/centos-home
  WARNING: Reducing active and open logical volume to <26.99 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos/home? [y/n]: yes
  Size of logical volume centos/home changed from <326.99 GiB (83709 extents) to <26.99 GiB (6909 extents).
  Logical volume centos/home successfully resized.

12.重新把PV中的300G多余空间分配给LV为root

[root@localhost ~]# lvextend -l +100%FREE /dev/mapper/centos-root
  Size of logical volume centos/root changed from 70.00 GiB (17920 extents) to 370.00 GiB (94720 extents).
  Logical volume centos/root successfully resized.

 

13.此时300G磁盘虽已扩展至 /root,但并没写入文件系统;进入 /etc/fstab 确认 /root 文件系统--->xfs文件系统

[root@localhost ~]# more /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Sep 14 17:08:44 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=28137713-1148-49bc-b74d-7ac9bdf0bc1b /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0

14、用xfs_growfs 命令把文件写入系统中,使扩容生效

[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=4587520 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=18350080, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=8960, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 18350080 to 96993280

说明:若使用ext4文件格式的,是使用resize2fs命令来生效

 

15.查看扩容后的硬盘空间小,如下内容则说明,已实现扩容。大功告成!

[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root  370G  9.4G  361G    3% /
devtmpfs                4.8G     0  4.8G    0% /dev
tmpfs                    4.9G     0  4.9G    0% /dev/shm
tmpfs                    4.9G  9.5M  4.8G    1% /run
tmpfs                    4.9G     0  4.9G    0% /sys/fs/cgroup
/dev/sda1               1014M  171M  844M   17% /boot
/dev/mapper/centos-home   27G   37M   27G    1% /home
tmpfs                    984M   12K  984M    1% /run/user/42
tmpfs                    984M     0  984M    0% /run/user/0

 

本次扩容指令汇总:

1.创建分区                                                #fdisk /dev/sdb

2.创建物理卷                                             #pvcreat /dev/sdb1

3.查看卷组名称及使用情况                         #vgdisplay

4.将物理卷扩展到卷组                               #vgextend cl /dev/sdb1   (此处‘cl’是卷组名称)

5.将卷组中空闲空间扩展到 /home           #lvextend -l +100%FREE /dev/mapper/cl-home

6.刷新文件系统是扩容生效 xfs_growfs /dev/mapper/centos-root

7.若是ext4文件格式使用扩容生效                     #resize2fs /dev/mapper/centos-home

相关LVM知识点如下:

这里总结下扩展与缩小LV的实施顺序:

扩展LV

1、卸载LV

命令:umount  “挂载目录”

2、扩展LV

命令:lvextend -L +500m  /dev/lv00/lv01 或者 lvresize -L 5G  /dev/lv00/lv01

lvextend 表示增加500M,lvresize表示增加到5G,是调整后的容量。

3、检查文件系统

命令:fsck -f  /dev/vg00/lv01

4、重建文件系统

  命令:resize2fs  /dev/lv00/lv01

这一步很关键,重建文件系统不用担心LV的数据会丢失,当然,以防万一,也可以先备份下LV里的数据。

5、挂载LV

命令:mount  /dev/lv001/lv01  /opt

缩小LV

  缩小LV需要事先知道LV中的数据容量大小,缩小容量值不应超过剩余空间容量,而且要知道减少LV容量后的新大小。可以通过lvscan来查看LV容量,通过vgdisplay来查看剩余容量。

1、卸载LV

命令:umount  “挂载目录”

2、检查文件系统

命令:fsck -f  /dev/lv00/lv01

3、调整LV大小并重建文件系统

命令:resize2fs  /dev/vg00/lv01  2G

4、减小LV容量

命令:lvreduce -L -3G  /dev/vg00/lv01

5、挂载LV

  命令:mount  /dev/lv001/lv01  /opt

  • 0
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值