vps磁盘扩容笔记(扩容磁盘空间)
ztj100 2024-11-09 15:19 39 浏览 0 评论
在VPS上,一般建议分成两块磁盘挂载,其中一块为系统盘,另一块为数据盘,在于,系统盘数据一般不会变更,也不会考虑到扩容的问题;而数据盘,随着时间的推移,您会发现您的磁盘会越来越不够了,必然会考虑到扩容的问题,而扩容,在vps上,有两种方案,一种是已分区的,这种扩容比较麻烦,需要停服务处理;另一种是裸分区的,这种比较简单,可以不用停服务,几秒钟就搞定了,下面的内容,就是针对这两种进行分别处理的笔记。因森哥目前使用的是阿里云的ECS,故此次以阿里云的ECS为例进行讲解,其他的VPS也基本上一样,起码,森哥在本地测试的时候,是这么一回事的。
声明:由于头条PC端显示代码内容时,格式会乱,建议使用手机端查看。森哥在最后面也提示了个链接,可以直接查看森哥发布的原文。
本次扩容的数据盘是已在生产环境中使用的硬盘。因磁盘空间即将用满,故扩容了空间。
要求,在保留旧数据的同时,又能将空间扩容成功。
参考文档:扩展分区和文件系统_Linux数据盘
扩容磁盘,涉及到两种格式,一种是已有分区的扩容,另一种是裸分区的扩容。这两种操作的方式不一样。
最简单的就是裸分区的扩容,无损而且不需要停服务;
最麻烦的就是有分区的扩容。
现在森哥带着您一起操作吧。
扩展已有分区
为了防止数据丢失,不建议扩容已挂载( mount)的分区和文件系统。请先取消挂载( umount)分区,完成扩容并正常使用后,重新挂载( mount)。如果确实需要扩容已挂载( mount)的分区,则操作如下:
- 实例内核版本 <3.6:先取消挂载该分区,再修改分区表,最后扩容文件系统。
- 实例内核版本 ≥3.6:先修改对应分区表,再通知内核更新分区表,最后扩容文件系统。
如果新磁盘空间用于扩容已有的分区,按照以下步骤在实例中完成扩容:
步骤一:修改分区表
1、运行fdisk -lu /dev/vdb,并记录旧分区的起始和结束的扇区位置。
本示例中, /dev/vdb1 的起始扇区位置是 2048,结束扇区位置是 41943039。
[root@localhost ~]# fdisk -lu /dev/vdb
Disk /dev/vdb: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x9277b47b
?
Device Boot Start End Blocks Id System
/dev/vdb1 2048 41943039 20970496 83 Linux
2、查看数据盘的挂载路径,根据返回的文件路径卸载分区,直至完全卸载已挂载的分区。
[root@ecshost ~]# mount | grep "/dev/vdb"
/dev/vdb1 on /mnt type ext4 (rw,relatime,data=ordered)
[root@ecshost ~]# umount /dev/vdb1
[root@ecshost ~]# mount | grep "/dev/vdb"
3、使用fdisk工具删除旧分区。
i、运行fdisk -u /dev/vdb:分区数据盘。
ii、输入p:打印分区表。
iii、输入d:删除分区。
iv、输入p:确认分区已删除。
v、输入w:保存修改并退出。
[root@ecshost ~]# fdisk -u /dev/vdb
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): p
Disk /dev/vdb: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x9277b47b
Device Boot Start End Blocks Id System
/dev/vdb1 2048 41943039 20970496 83 Linux
Command (m for help): d
Selected partition 1
Partition 1 is deleted
Command (m for help): p
Disk /dev/vdb: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x9277b47b
Device Boot Start End Blocks Id System
Command (m for help): 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.
4、使用fdisk命令新建分区。
i、运行fdisk -u /dev/vdb:分区数据盘。
ii、输入p:打印分区表。
iii、输入n:新建分区。
iv、输入p:选择分区类型为主分区。
v、输入<分区号>:选择分区号。本示例选取了1。
警告 新分区的起始位置必须和旧分区的起始位置相同,结束位置必须大于旧分区的结束位置,否则会导致扩容失败。
vi、输入w:保存修改并退出。
本示例中,将/dev/vdb1由20 GiB扩容到40 GiB。
[root@ecshost ~]# fdisk -u /dev/vdb
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): p
Disk /dev/vdb: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x9277b47b
Device Boot Start End Blocks Id System
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-83886079, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-83886079, default 83886079):
Partition 1 of type Linux and of size 30 GiB is set
Command (m for help): p
Disk /dev/vdb: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x9277b47b
Device Boot Start End Blocks Id System
/dev/vdb1 2048 62916607 31457280 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
5、运行lsblk /dev/vdb确保分区表已经增加。
6、运行e2fsck -n /dev/vdb1再次检查文件系统,确认扩容分区后的文件系统状态为clean。
步骤二:通知内核更新分区表。
运行partprobe或者partx -u,以通知内核数据盘的分区表已经修改,需要同步更新。
步骤三:扩容文件系统。
- ext*文件系统(例如ext3和ext4):运行resize2fs /dev/vdb1并重新挂载分区。
[root@ecshost ~]# resize2fs /dev/vdb1
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vdb1 to 7864320 (4k) blocks.
The filesystem on /dev/vdb1 is now 7864320 blocks long.
[root@ecshost ~]# mount /dev/vdb1 /mnt
- xfs文件系统:先运行mount /dev/vdb1 /mnt/命令,再运行xfs_growfs /dev/vdb1。
[root@ecshost ~]# mount /dev/vdb1 /mnt/
[root@ecshost ~]# xfs_growfs /dev/vdb1
meta-data=/dev/vdb1 isize=512 agcount=4, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=5242880, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 5242880 to 7864320
扩容裸设备文件系统
当数据盘没有创建分区,并且在裸设备上创建了文件系统时,您可以参见以下步骤直接扩容文件系统。
1.根据文件系统的类型,执行不同的扩容命令。
- ext*:使用root权限执行resize2fs命令扩容文件系统,例如:
resize2fs /dev/vdb
- xfs:使用root权限执行xfs_growfs命令扩容文件系统,例如:
xfs_growfs /dev/vdb
2.运行df -h查看数据盘扩容结果。显示容量完成扩充,表示扩容成功。
[root@ecshost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 1.6G 36G 5% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 460K 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/vdb 98G 37G 61G 37% /mnt
tmpfs 783M 0 783M 0% /run/user/0
相关推荐
- Java的SPI机制详解
-
作者:京东物流杨苇苇1.SPI简介SPI(ServiceProvicerInterface)是Java语言提供的一种接口发现机制,用来实现接口和接口实现的解耦。简单来说,就是系统只需要定义接口规...
- 一文读懂 Spring Boot 启动原理,开发效率飙升!
-
在当今的Java开发领域,SpringBoot无疑是最热门的框架之一。它以其“约定大于配置”的理念,让开发者能够快速搭建和启动应用,极大地提高了开发效率。但是,你是否真正了解Spring...
- ServiceLoader
-
ServiceLoader是Java提供的一种服务发现机制(ServiceProviderInterface,SPI)...
- 深入探索 Spring Boot3 中的自定义扩展操作
-
在当今互联网软件开发领域,SpringBoot无疑是最受欢迎的框架之一。随着其版本迭代至SpringBoot3,它为开发者们带来了更多强大的功能和特性,其中自定义扩展操作更是为我们在项目开发中...
- Spring Boot启动过程全面解析:从入门到精通
-
一、SpringBoot概述SpringBoot是一个基于Spring框架的快速开发脚手架,它通过"约定优于配置"的原则简化了Spring应用的初始搭建和开发过程。...
- Spring Boot 3.x 自定义 Starter 详解
-
今天星期六,继续卷springboot3.x。在SpringBoot3.x中,自定义Starter是封装和共享通用功能、实现“约定优于配置”理念的强大机制。通过创建自己的Starte...
- Spring Boot 的 3 种动态 Bean 注入技巧
-
在SpringBoot开发中,动态注入Bean是一种强大的技术,它允许我们根据特定条件或运行时环境灵活地创建和管理Bean。相比于传统的静态Bean定义,动态注入提供了更高的灵活性和可...
- 大佬用4000字带你彻底理解SpringBoot的运行原理!
-
SpringBoot的运行原理从前面创建的SpringBoot应用示例中可以看到,启动一个SpringBoot工程都是从SpringApplication.run()方法开始的。这个方法具体完成...
- Springboot是如何实现自动配置的
-
SpringBoot的自动配置功能极大地简化了基于Spring的应用程序的配置过程。它能够根据类路径中的依赖和配置文件中的属性,自动配置应用程序。下面是SpringBoot实现自动配置的...
- Spring Boot3.x 应用的生命周期深度解析
-
SpringBoot应用的生命周期可以清晰地划分为三个主要阶段:启动阶段(Startup)...
- Springboot 启动流程及各类事件生命周期那点事
-
前言本文通过Springboot启动方法分析SpringApplication逻辑。从静态run方法执行到各个阶段发布不同事件完成整个应用启动。...
- Spring框架基础知识-常用的接口1
-
BeanDefinition基本概念BeanDefinition是Spring框架中描述bean配置信息的核心接口,它包含了创建bean实例所需的所有元数据。...
- Java 技术岗面试全景备战!从基础到架构的系统性通关攻略分享
-
Java技术岗的面试往往是一项多维度的能力检验。本文将会从核心知识点、项目经验到面试策略,为你梳理一份系统性的备战攻略!...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- idea eval reset (50)
- vue dispatch (70)
- update canceled (42)
- order by asc (53)
- spring gateway (67)
- 简单代码编程 贪吃蛇 (40)
- transforms.resize (33)
- redisson trylock (35)
- 卸载node (35)
- np.reshape (33)
- torch.arange (34)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- vue foreach (34)
- idea设置编码为utf8 (35)
- vue 数组添加元素 (34)
- std find (34)
- tablefield注解用途 (35)
- python str转json (34)
- java websocket客户端 (34)
- tensor.view (34)
- java jackson (34)
- vmware17pro最新密钥 (34)
- mysql单表最大数据量 (35)