1 CentOS下磁盘LVM扩容

248 篇文章 11 订阅
145 篇文章 15 订阅

前几天遇到了这样一个情景:

一个服务器是以虚拟机的形式提供的,通过df -h看服务器磁盘空间只有30多个G,但通过fdisk -l看发现有一个160G的磁盘空间 没有挂载,因此需要在不重启的前提下将160G的磁盘挂载。

之前处理过好几次这种问题,但一段时间不弄,又有点忘了,趁此机会将此案例分享一下,加深记忆。

在说这个案例前,先简要谈一下处理这个问题所要用到的逻辑卷管理(LVM)相关知识。

逻辑卷管理LVM是一个多才多艺的硬盘系统工具。无论在Linux或者其他类似的系统,都是非常的好用。传统分区使用固定大小分区,重新调整大小十分麻烦。但是,LVM可以创建和管理“逻辑”卷,而不是直接使用物理硬盘。可以让管理员弹性的管理逻辑卷的扩大缩小,操作简单,而不损坏已存储的数据。可以随意将新的硬盘添加到LVM,以直接扩展已经存在的逻辑卷。LVM并不需要重启就可以让内核知道分区的存在。

LVM使用分层结构,如下图所示。
在这里插入图片描述
LVM逻辑卷基础知识
PV (physical volume)物理卷
VG (Volume Group)卷组
LV ( Logical Volume)逻辑卷
它们之间的关系

  1. 先创建PV(物理卷)
  2. 给PV(物理卷)中创建一个或多个VG(

物理卷命令说明
在这里插入图片描述
示例
说明:
1. 当前有一块磁盘为 /dev/sdb
2. 使用fdisk命令将它进行分区 fdisk /dev/sdb
3. 将分区后的磁盘改为 8e 【也就是LVM系统】
4. 使用 pvcreate 命令将它进行初始化物理卷,比如: pvcreate /dev/sdb1
操作:
1. fdisk /dev/sdb命令进行创建物理卷
2. pvcreate /dev/sdb1 创建物理卷
3. pvs 查看当前物理卷信息

案例:磁盘扩容

1.查看磁盘文件可用空间,发现可用磁盘空间只有30多个

[root@localhost ~]# df -h

在这里插入图片描述
2.查看磁盘空间对的详细信息,可见有一个160G的磁盘

[root@server tomcat-atm]# fdisk -l

在这里插入图片描述
3.创建磁盘分区,使用‘8e’类型来使其可用于LVM

[root@server tomcat-atm]# fdisk /dev/sdb 

在这里插入图片描述
在这里插入图片描述
4.创建物理卷

root@server tomcat-atm]# pvcreate /dev/sdb

Physical volume "/dev/sdb" successfully created
  1. 检查物理卷的创建情况
[root@server tomcat-atm]# pvdisplay

在这里插入图片描述
6. 扩展卷组

[root@server tomcat-atm]# vgextend centos /dev/sdb

Volume group "centos" successfully extended
  1. 查看卷组
[root@server tomcat-atm]# vgdisplay

在这里插入图片描述
8. 扩展lvm卷

[root@server tomcat-atm]# lvextend -l +100%FREE /dev/mapper/centos-root

Size of logical volume centos/root changed from 35.47 GiB (9080 extents) to 185.50 GiB (47489 extents).

Logical volume root successfully resized.
  1. 扩展容量
[root@server tomcat-atm]# xfs_growfs /dev/mapper/centos-root 

在这里插入图片描述
10.查看磁盘空间,可以看到扩容成功
在这里插入图片描述

扩展Linux系统根分区

技术:逻辑卷管理(LVM)
在这里插入图片描述

具体步骤:

1.查看磁盘空间

[root@localhost ~]# df -h
#查看磁盘空间,根分区的大小是18G,已经用了43%。
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root   18G  7.0G  9.5G  43% /
tmpfs                         2.0G   76K  2.0G   1% /dev/shm
/dev/sda1                     485M   40M  421M   9% /boot
/dev/sr0                      3.6G  3.6G     0 100% /media/cdrom

2.创建磁盘分区

[root@localhost ~]# 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: 0x00083529

   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
#/dev/sdb盘还有足够的空间
Disk /dev/sdb: 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: 0x00000000

Disk /dev/mapper/VolGroup-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 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/VolGroup-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xac7c929b.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/sdb: 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: 0xac7c929b

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1#分区号
First cylinder (1-2610, default 1):  #回车用默认的
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): #回车用默认的
Using default value 2610

Command (m for help): t #改变类型
Selected partition 1
Hex code (type L to list codes): 8e #LVM的分区代码
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdb: 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: 0xac7c929b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        2610    20964793+  8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe
#将磁盘分区表变化信息通知内核,请求操作系统重新加载分区表,有些小报错不要紧。
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy).  As a result, it may not reflect all of your changes until after reboot.
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sdb (Device or resource busy).  As a result, it may not reflect all of your changes until after reboot.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Error: Invalid partition table - recursive partition on /dev/sr0.

在这里插入图片描述

3.创建物理卷

[root@localhost ~]# 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: 0x00083529

   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: 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: 0xac7c929b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        2610    20964793+  8e  Linux LVM

Disk /dev/mapper/VolGroup-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 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/VolGroup-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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@localhost ~]# pvcreate /dev/sdb1
#由fdisk -l查到LVM卷是/dev/sdb1,所以用/dev/sdb1来创建物理卷。
  Physical volume "/dev/sdb1" successfully created

在这里插入图片描述
在这里插入图片描述
报错 -bash: pvcreate: command not found

安装yum -y install lvm2即可解决
在这里插入图片描述

4.扩展卷组

[root@localhost ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               VolGroup
  PV Size               19.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4994
  Free PE               0
  Allocated PE          4994
  PV UUID               qkBdcn-RQaR-ZliO-Ja2S-RhMC-stw8-CO213I

  "/dev/sdb1" is a new physical volume of "19.99 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               19.99 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               H7tDqG-6AZv-yBSA-U0ov-Ni0y-dlPA-kAA83i

[root@localhost ~]# vgextend VolGroup /dev/sdb1
  Volume group "VolGroup" successfully extended
You have new mail in /var/spool/mail/root
[root@localhost ~]# vgdisplay
  --- Volume group ---
  VG Name               VolGroup
  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               39.50 GiB
  PE Size               4.00 MiB
  Total PE              10112
  Alloc PE / Size       4994 / 19.51 GiB
  Free  PE / Size       5118 / 19.99 GiB
  VG UUID               dc0vI9-KzGu-4S59-A1xv-NM4Y-rYBF-3jDNRn

在这里插入图片描述

5.扩展逻辑卷

[root@localhost ~]# lvextend -l +100%free /dev/mapper/VolGroup-lv_root
#free也可以大写FREE
  Extending logical volume lv_root to 37.50 GiB
  Logical volume lv_root successfully resized
[root@localhost ~]# blkid /dev/mapper/VolGroup-lv_root
#查看分区文件系统类型
/dev/mapper/VolGroup-lv_root: UUID="972fdddd-ddb8-469f-a342-e075eaeabeb2" TYPE="ext4" 
[root@localhost ~]# e2fsck -f /dev/mapper/VolGroup-lv_root
#e2fsck -f检查分区
e2fsck 1.41.12 (17-May-2010)
/dev/mapper/VolGroup-lv_root is mounted.
e2fsck: Cannot continue, aborting.

[root@localhost ~]# resize2fs /dev/mapper/VolGroup-lv_root
#如果分区是xfs系统,则用xfs_growfs命令对文件系统进行扩展
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/VolGroup-lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/mapper/VolGroup-lv_root to 9830400 (4k) blocks.
The filesystem on /dev/mapper/VolGroup-lv_root is now 9830400 blocks long.

[root@localhost ~]# df -h
#查看磁盘空间,根分区已扩展到37G。
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root   37G  7.0G   29G  20% /
tmpfs                         2.0G  224K  2.0G   1% /dev/shm
/dev/sda1                     485M   40M  421M   9% /boot
/dev/sr0                      3.6G  3.6G     0 100% /media/cdrom

6.将新硬盘挂载到新目录

#fdisk -l

在这里插入图片描述
创建第一个区分40G
在这里插入图片描述
在这里插入图片描述
创建第二个区分10G
在这里插入图片描述
在这里插入图片描述
格式化硬盘
格式化硬盘1
在这里插入图片描述
在这里插入图片描述
格式化硬盘2
在这里插入图片描述
挂载硬盘到目录
在这里插入图片描述
在这里插入图片描述






参考链接 :

来源:运维学习分享
参考资料:
https://linux.cn/article-3218-1.html
https://www.cnblogs.com/kevingrace/p/5825963.html
3分钟看懂linux磁盘划分https://mp.weixin.qq.com/s/WHO3_tHhfjytD703emOJYg

[1]
CentOS 7.2 添加磁盘并创建新区: https://blog.csdn.net/rznice/article/details/51984831
[2]
CentOS7下LVM的简单创建: https://www.linuxidc.com/Linux/2017-06/144428.htm
[3]
LVM基础详细说明及动态扩容lvm逻辑卷的操作记录: https://www.cnblogs.com/kevingrace/p/5845634.html

参考链接 :
Centos7创建LVM磁盘管理 :https://mp.weixin.qq.com/s/IcGIT_ugaRC_zPJor0EbEA

Linux磁盘扩容 | LVM逻辑卷使用手册 :

https://mp.weixin.qq.com/s?__biz=MzAwMjg1NjY3Nw==&mid=2247487338&idx=2&sn=eb0251f0cfed9de88495028e94d62778&source=41#wechat_redirect

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值