vps磁盘扩容笔记(扩容磁盘空间)
ztj100 2024-11-09 15:19 10 浏览 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
相关推荐
- Whoosh,纯python编写轻量级搜索工具
-
引言在许多应用程序中,搜索功能是至关重要的。Whoosh是一个纯Python编写的轻量级搜索引擎库,可以帮助我们快速构建搜索功能。无论是在网站、博客还是本地应用程序中,Whoosh都能提供高效的全文搜...
- 如何用Python实现二分搜索算法(python二分法查找代码)
-
如何用Python实现二分搜索算法二分搜索(BinarySearch)是一种高效的查找算法,适用于在有序数组中快速定位目标值。其核心思想是通过不断缩小搜索范围,每次将问题规模减半,时间复杂度为(O...
- 路径扫描 -- dirsearch(路径查找器怎么使用)
-
外表干净是尊重别人,内心干净是尊重自己,干净,在今天这个时代,应该是一种极高的赞美和珍贵。。。----网易云热评一、软件介绍Dirsearch是一种命令行工具,可以强制获取web服务器中的目录和文件...
- 78行Python代码帮你复现微信撤回消息!
-
来源:悟空智能科技本文约700字,建议阅读5分钟。本文基于python的微信开源库itchat,教你如何收集私聊撤回的信息。...
- 从零开始学习 Python!2《进阶知识》 Python进阶之路
-
欢迎来到Python学习的进阶篇章!如果你说已经掌握了基础语法,那么这篇就是你开启高手之路的大门。我们将一起探讨面向对象编程...
- 白帽黑客如何通过dirsearch脚本工具扫描和收集网站敏感文件
-
一、背景介绍...
- Python之txt数据预定替换word预定义定位标记生成word报告(四)
-
续接Python之txt数据预定替换word预定义定位标记生成word报告(一)https://mp.toutiao.com/profile_v4/graphic/preview?pgc_id=748...
- Python——字符串和正则表达式中的反斜杠('\')问题详解
-
在本篇文章里小编给大家整理的是关于Python字符串和正则表达式中的反斜杠('\')问题以及相关知识点,有需要的朋友们可以学习下。在Python普通字符串中在Python中,我们用'\'来转义某些普通...
- Python re模块:正则表达式综合指南
-
Python...
- python之re模块(python re模块sub)
-
re模块一.re模块的介绍1.什么是正则表达式"定义:正则表达式是一种对字符和特殊字符操作的一种逻辑公式,从特定的字符中,用正则表达字符来过滤的逻辑。(也是一种文本模式;)2、正则表达式可以帮助我们...
- MySQL、PostgreSQL、SQL Server 数据库导入导出实操全解
-
在数字化时代,数据是关键资产,数据库的导入导出操作则是连接数据与应用场景的桥梁。以下是常见数据库导入导出的实用方法及代码,包含更多细节和特殊情况处理,助你应对各种实际场景。一、MySQL数据库...
- Zabbix监控系统系列之六:监控 mysql
-
zabbix监控mysql1、监控规划在创建监控项之前要尽量考虑清楚要监控什么,怎么监控,监控数据如何存储,监控数据如何展现,如何处理报警等。要进行监控的系统规划需要对Zabbix很了解,这里只是...
- mysql系列之一文详解Navicat工具的使用(二)
-
本章内容是系列内容的第二部分,主要介绍Navicat工具的使用。若查看第一部分请见:...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- Whoosh,纯python编写轻量级搜索工具
- 如何用Python实现二分搜索算法(python二分法查找代码)
- 路径扫描 -- dirsearch(路径查找器怎么使用)
- 78行Python代码帮你复现微信撤回消息!
- 从零开始学习 Python!2《进阶知识》 Python进阶之路
- 白帽黑客如何通过dirsearch脚本工具扫描和收集网站敏感文件
- Python之txt数据预定替换word预定义定位标记生成word报告(四)
- 假期苦短,我用Python!这有个自动回复拜年信息的小程序
- Python——字符串和正则表达式中的反斜杠('\')问题详解
- Python re模块:正则表达式综合指南
- 标签列表
-
- 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)
- node卸载 (33)
- npm 源 (35)
- vue3 deep (35)
- win10 ssh (35)
- exceptionininitializererror (33)
- 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)