centos7 基于lvm的根目录扩容
centos7 在安装后,通常我们通过df -h 来查看磁盘空间的时候率。如下:
1 2 3 4 5 6 7 8 9 | [root@oracle01 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 46G 32G 14G 71% / devtmpfs 1.9G 0 1.9G 0% /dev tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 1.9G 17M 1.9G 1% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/sda1 497M 125M 372M 26% /boot tmpfs 379M 0 379M 0% /run/user/0 |
如上,在根目录 / 下,已经使用了71%,快没有空间了,这个时候,如果需要增加空间,如果你是实体机,则只能 增加磁盘,如果你是虚拟机或者云服务器,那么可以自由扩展,比如vsphere(vmware的私有云平台),可以直接增加磁盘空间,我这里就是,于是我将磁盘空间从50G增加至100G。
直接增加磁盘空间,但是,不管哪种方式,增加后,会发现,并没有生效,df -h 能用的空间并没有增加,而新增的空间使用 fdisk -l 则可以查看到,如下:
可以看到,虽然我的磁盘空间大小为107G ,但是下面使用了的只有50G 左右。 增加的空间并没有被使用。
下面,就需要将没使用到的空间,用来扩容在根目录下,或别的挂载目录下。 在开始之前,我们必须要对linux的磁盘管理有一定的认识:
入上图,在centos中,对于储存和磁盘空间的管理是lvm 。上图就是lvm 的结构 。 我们常用的 fdisk -l 命令,显示的就是物理分区,相当于直接作用于磁盘上的。
在物理分区中,我们可以在不同的磁盘中,划分去分区,而这个分区必须要设置为lvm 分区格式,只有设置为lvm分区格式,才可以将这些分区,提取出来创建成物理卷,
也就是图中第二层,这些在不通物理磁盘中的物理卷,就可以组成一个大的卷组,这个卷组就相当于逻辑上的磁盘,然后可以在卷组上进行分区,分为逻辑卷。而这些逻辑卷就是我们常用的 df -h 来查看的磁盘使用锁显示的,例如 根目录 /home目录等就是常用的逻辑卷。
你可能会问为什么要这样做,因为一般的物理磁盘进行的物理分区,一旦确定,是不能修改大小的,也就无法很好的就行扩容。而且当容量大于一般磁盘的需求时,就没有像lvm一样可以通过组合多个物理磁盘来形成一个大的卷组使用。当然,也为了更好的管理。
好的。下面就开始对根目录进行扩容。
1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | [root@oracle01 ~]# fdisk /dev/sda ## /dev/sda为通过fdisk -l 查看到的物理磁盘(第一行) Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n ## n为创建一个新的分区 Partition type: ## 这里需要选择时创建主分区还是扩展分区,都可以,这里直接选了主分区 p primary (2 primary, 0 extended, 2 free) e extended Select ( default p): p ## 选择创建一个主分区,主分区只能有4个,编号为1-4,下面的全部直接回车就好了,会自动将剩余所用空间都创建 Partition number (3,4, default 3): First sector (104857600-209715199, default 104857600): Using default value 104857600 Last sector, +sectors or +size{K,M,G} (104857600-209715199, default 209715199): Using default value 209715199 Partition 3 of type Linux and of size 50 GiB is set Command (m for help): t ## t为修改分区类型 Partition number (1-3, default 3): 3 ## 刚才创建的分区编号为3 Hex code (type L to list all codes): 8e ## 8e就是 lvm格式的分区 Changed type of partition 'Linux' to 'Linux LVM' Command (m for help): 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: Device or resource busy. 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) Syncing disks. |
这里,在保存后,会发现,可能会出现报错,显示繁忙,无法重新读取分区信息。下面有解决办法。可以用过重启或者执行 partprobe or kpartx。所以,这里直接执行partprobe
1 | [root@oracle01 ~]# partprobe |
或者重启
2. 创建了物理分区后,就需要将物理分区创建为物理卷:
将物理分区3 创建物理卷,这里这个/dev/sda3是第一步创建的物理分区,通过fdisk -l 就可以看到
是在第一个物理磁盘/dev/sda下面的分区(一个磁盘可以有多个分区),原本已有/dev/sda1,/dev/sda2
通过第一步创建生成/dev/sda3这个分区,然后将这个分区 创建成物理卷
pvcreate /dev/sda3
/dev/sda3 successfully created.
## 查看物理卷
pvdisplay
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | --- Physical volume --- PV Name /dev/sda2 VG Name centos PV Size 49.51 GiB / not usable 3.00 MiB Allocatable yes PE Size 4.00 MiB Total PE 12674 Free PE 11 Allocated PE 12663 PV UUID 8T9dpf-SrDj-maVi-IlAb-tpPb-Lzyt-WLeRHG /dev/sda3 is a new physical volume of 50.00 GiB --- NEW Physical volume --- PV Name /dev/sda3 VG Name PV Size 50.00 GiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID uRRJtJ-Ds7J-dvqs-y5Lb-W95H-FXeh-C2QNtl |
3. 物理卷创建成功后,则需要将物理卷添加到卷组中,这样才能在卷组中进行分配。
[root@oracle01 ~]# 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 <49.51 GiB PE Size 4.00 MiB Total PE 12674 Alloc PE / Size 12663 / 49.46 GiB Free PE / Size 11 / 44.00 MiB ## 可以看到这个卷组只剩下44M的剩余空间了,其他的都划分为逻辑卷了,类似于已分区 VG UUID 3TMbc1-A7aP-DAMv-QtUU-VTjC-4tOf-iAxDkc [root@oracle01 ~]# vgextend centos /dev/sda3 ## 将物理卷加入到卷组 Volume group "centos" successfully extended [root@oracle01 ~]# vgdisplay ## 加入后,再次查看卷组 --- Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 5 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 99.50 GiB PE Size 4.00 MiB Total PE 25473 Alloc PE / Size 12663 / 49.46 GiB Free PE / Size 12810 / <50.04 GiB ## 可以看到,加入成功后,卷组的剩余空间变大了,这样就有更多的空间可以进行分配,或新创建新的逻辑卷 VG UUID 3TMbc1-A7aP-DAMv-QtUU-VTjC-4tOf-iAxDkc
4. 添加进卷组后,就是将剩余空间对逻辑卷进行扩容。
[root@oracle01 ~]# lvdisplay ## 查看逻辑卷 --- Logical volume --- LV Path /dev/centos/root ## 逻辑卷名称 LV Name root VG Name centos LV UUID RaEygR-53tg-UaJQ-5dJs-e8V4-by51-fcp1DQ LV Write Access read/write LV Creation host, time localhost.localdomain, 2017-09-30 19:12:05 +0800 LV Status available # open 1 LV Size 45.46 GiB ## 增加前,逻辑卷大小 Current LE 11639 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0 [root@oracle01 ~]# lvextend -l +100%FREE /dev/centos/root ## 将剩余百分百空间都添加到逻辑卷中 Size of logical volume centos/root changed from 45.46 GiB (11639 extents) to 95.50 GiB (24449 extents). Logical volume centos/root successfully resized. [root@oracle01 ~]# lvdisplay --- Logical volume --- LV Path /dev/centos/root LV Name root VG Name centos LV UUID RaEygR-53tg-UaJQ-5dJs-e8V4-by51-fcp1DQ LV Write Access read/write LV Creation host, time localhost.localdomain, 2017-09-30 19:12:05 +0800 LV Status available # open 1 LV Size 95.50 GiB ## 增加后,逻辑卷大小 Current LE 24449 Segments 3 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
5. 然后重新识别一下分区大小,就可以通过df -h看到新增的容量了
[root@oracle01 ~]# xfs_growfs /dev/centos/root ## 命令,后面跟的是逻辑卷的path meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=2979584 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 finobt=0 data = bsize=4096 blocks=11918336, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal bsize=4096 blocks=5819, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 11918336 to 25035776 [root@oracle01 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 96G 32G 64G 34% / ## 这里可以看到,已成功扩容 devtmpfs 1.9G 0 1.9G 0% /dev tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs 1.9G 17M 1.9G 1% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/sda1 497M 125M 372M 26% /boot tmpfs 379M 0 379M 0% /run/user/0
OK,到此,扩容完成。并且不需要重启,方便好用。
当你的才华还撑不起你的野心的时候,你就应该静下心来学习;当你的能力还驾驭不了你的目标时,就应该沉下心来,历练;梦想,不是浮躁,而是沉淀和积累,只有拼出来的美丽,没有等出来的辉煌,机会永远是留给最渴望的那个人,学会与内心深处的你对话,问问自己,想 要怎样的人生,静心学习,耐心沉淀,送给自己,共勉。
**************************************************************************************
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具