执子之手

与子偕老


  • 首页

  • 分类

  • 归档

  • 标签

  • 关于

  • 搜索
close

无损扩容Linux磁盘分区容量

时间: 2022-12-01   |   分类: 运维     |   阅读: 2110 字 ~5分钟   |   访问: 0

1. 背景

公司有台虚拟机,当初安装的时候给的磁盘容量偏小(80G),最近频繁报磁盘空间不足。因为上面跑了比较重要的服务,没有办法重装,只好考虑如何扩容。物理机上面的空闲空间还比较多,通过虚拟机管理软件修改了虚拟机的配置,很容易的把虚拟磁盘容量调大了,调整到了215G。

虚拟磁盘调整后,就需要在Linux系统中进行扩容操作。扩容主要有两步工作要做:

  1. 调整分区大小:将新增的空间添加到分区内容;需要用到工具parted;
  2. 调整文件系统大小:分区容量扩大后,使用resize2fs命令将文件系统扩容到整个磁盘容量。

备注:当时分区的时候没有选择LVM管理,如果使用LVM管理可以通过lvextend命令进行分区的扩容,具体方法参考附录中的链接。

2. 具体操作

调整分区大小需要用到parted工具,这是一个分区管理工具。可以无损调整分区大小,但是是一个命令行工具,使用起来没有Windows上面的一些工具方便。另外,也不支持移动分区,只适合将分区后面的空间添加到现有分区上。如果要扩容的分区后面有其他分区,那这个工具就无能为力,除非把后面的分区删掉。

2.1 查看分区信息

首先使用print命令查看一下当前的分区情况。可以看到:这个磁盘分区很简单,只有两个分区。一个是grub用的引导分区,容量1M左右;另一个就是主分区,ext4格式。

 1# 开启parted工具
 2root@testserver:~# parted
 3GNU Parted 3.2
 4Using /dev/xvda
 5Welcome to GNU Parted! Type 'help' to view a list of commands.
 6# 使用print命令显示当前分区信息
 7# 可以看到,当前硬盘大小215G,但是实际使用了86G左右
 8(parted) print                                                            
 9Model: Xen Virtual Block Device (xvd)
10Disk /dev/xvda: 215GB
11Sector size (logical/physical): 512B/512B
12Partition Table: gpt
13Disk Flags: 
14
15Number  Start   End     Size    File system  Name  Flags
16 1      1049kB  2097kB  1049kB                     bios_grub
17 2      2097kB  85.9GB  85.9GB  ext4

2.2 调整分区大小

使用parted的resizepart命令调整分区大小。此处要输入的是分区终点的位置,此处使用的是整个磁盘的容量大小:215GB。

 1(parted) resizepart                                                       
 2Partition number? 2                                                       
 3Warning: Partition /dev/xvda2 is being used. Are you sure you want to continue?
 4Yes/No? yes                                                               
 5End?  [85.9GB]? 215GB   
 6
 7# 打印一下分区信息看看,大小已经调整                            
 8(parted) print                                                            
 9Model: Xen Virtual Block Device (xvd)
10Disk /dev/xvda: 215GB
11Sector size (logical/physical): 512B/512B
12Partition Table: gpt
13Disk Flags: 
14
15Number  Start   End     Size    File system  Name  Flags
16 1      1049kB  2097kB  1049kB                     bios_grub
17 2      2097kB  215GB   215GB   ext4
18 

2.3 文件系统扩容

使用parted命令调整分区大小后,就可以使用resize2fs命令进行文件系统扩容了。

扩容前:

1root@testserver:~# df -h
2/dev/xvda2       79G   59G   16G  79% /

使用resize2fs调整ext4分区容量:

1root@testserver:~# resize2fs /dev/xvda2
2resize2fs 1.44.1 (24-Mar-2018)
3Filesystem at /dev/xvda2 is mounted on /; on-line resizing required
4old_desc_blocks = 10, new_desc_blocks = 25
5The filesystem on /dev/xvda2 is now 52428283 (4k) blocks long.
6
7root@testserver:~# df -h
8/dev/xvda2      197G   59G  129G  32% /

可以看到文件系统大小已经调整成功了。

3. 另一个例子

另一台虚拟机的情况比上面的复杂,它的分区多了一个交换分区。要无损在线扩容需要首先删除交换分区。下面是操作步骤的记录。

3.1 原始状态

查看一下原始状态。可见原始分区/dev/sda1为72G,后面还跟着一个8G的交换分区。

 1ubuntu@devserver:~$ df -h
 2/dev/sda1        71G   39G   29G  58% /
 3
 4# 另加8G的交换分区
 5root@devserver:~# free -h
 6              total        used        free      shared  buff/cache   available
 7Mem:           7.8G        5.5G        1.4G         12M        909M        2.0G
 8Swap:          8.0G          0B        8.0G
 9
10# 使用fdisk查看分区情况,一共两个区,一个72G分区,一个8G的交换分区
11root@devserver:~# fdisk -l
12Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
13Units: sectors of 1 * 512 = 512 bytes
14Sector size (logical/physical): 512 bytes / 512 bytes
15I/O size (minimum/optimal): 512 bytes / 512 bytes
16Disklabel type: dos
17Disk identifier: 0x316b4807
18
19Device     Boot     Start       End   Sectors Size Id Type
20/dev/sda1  *         2048 150996991 150994944  72G 83 Linux
21/dev/sda2       150999038 167770111  16771074   8G  5 Extended
22/dev/sda5       150999040 167770111  16771072   8G 82 Linux swap / Solaris
23
24# 使用parted查看分区信息
25root@devserver:~# parted
26GNU Parted 3.2
27Using /dev/sda
28Welcome to GNU Parted! Type 'help' to view a list of commands.
29(parted) print                                                            
30Model: VMware Virtual disk (scsi)
31Disk /dev/sda: 107GB
32Sector size (logical/physical): 512B/512B
33Partition Table: msdos
34Disk Flags: 
35
36Number  Start   End     Size    Type      File system     Flags
37 1      1049kB  77.3GB  77.3GB  primary   ext4            boot
38 2      77.3GB  85.9GB  8587MB  extended
39 5      77.3GB  85.9GB  8587MB  logical   linux-swap(v1)

3.2 分区扩容

1. 因为交换分区占据了磁盘末端,所以直接扩容分区1不成功:

1(parted) resizepart                                                       
2Partition number? 1                                                       
3Warning: Partition /dev/sda1 is being used. Are you sure you want to continue?
4Yes/No? y                                                                 
5End?  [77.3GB]? 98GB                                                      
6Error: Can't have overlapping partitions.

2. 所以思路上是先删除交换分区,然后扩容ext4分区,然后重新创建swap分区。请看操作:

 1# 首先禁用交换分区
 2root@devserver:~# swapoff -a
 3# df看一下,可以看到交换分区已经禁用了
 4root@devserver:~# free -h
 5              total        used        free      shared  buff/cache   available
 6Mem:           7.8G        5.9G        746M         12M        1.1G        1.6G
 7Swap:            0B          0B          0B
 8
 9# 使用parted删除分区5,2
10# 删除分区5
11(parted) rm 5                                                             
12(parted) print                                                            
13Model: VMware Virtual disk (scsi)
14Disk /dev/sda: 107GB
15Sector size (logical/physical): 512B/512B
16Partition Table: msdos
17Disk Flags: 
18
19Number  Start   End     Size    Type      File system  Flags
20 1      1049kB  77.3GB  77.3GB  primary   ext4         boot
21 2      77.3GB  85.9GB  8587MB  extended
22
23# 删除分区2
24(parted) rm 2                                                             
25(parted) print                                                            
26Model: VMware Virtual disk (scsi)
27Disk /dev/sda: 107GB
28Sector size (logical/physical): 512B/512B
29Partition Table: msdos
30Disk Flags: 
31
32Number  Start   End     Size    Type     File system  Flags
33 1      1049kB  77.3GB  77.3GB  primary  ext4         boot
34
35# 扩容分区1,注意保留部分空间用于重建交换分区
36(parted) resizepart                                                       
37Partition number? 1                                                       
38Warning: Partition /dev/sda1 is being used. Are you sure you want to continue?
39Yes/No? yes                                                               
40End?  [77.3GB]? 98GB
41(parted) print                                                            
42Model: VMware Virtual disk (scsi)
43Disk /dev/sda: 107GB
44Sector size (logical/physical): 512B/512B
45Partition Table: msdos
46Disk Flags: 
47
48Number  Start   End     Size    Type     File system  Flags
49 1      1049kB  98.0GB  98.0GB  primary  ext4         boot
50
51# 创建扩展分区2
52(parted) mkpart
53Partition type?  primary/extended? extended                               
54Start? 98GB                                                               
55End? 107GB                                                                
56(parted) print                                                            
57Model: VMware Virtual disk (scsi)
58Disk /dev/sda: 107GB
59Sector size (logical/physical): 512B/512B
60Partition Table: msdos
61Disk Flags: 
62
63Number  Start   End     Size    Type      File system  Flags
64 1      1049kB  98.0GB  98.0GB  primary   ext4         boot
65 2      98.0GB  107GB   9373MB  extended               lba
66
67# 创建逻辑分区5
68(parted) mkpart                                                           
69Partition type?  primary/logical? logical                                 
70File system type?  [ext2]? swap                                           
71parted: invalid token: swap
72File system type?  [ext2]? help                                           
73parted: invalid token: help
74File system type?  [ext2]? ext4                                           
75Start? 98GB                                                               
76End? 107GB                                                                
77(parted) print                                                            
78Model: VMware Virtual disk (scsi)
79Disk /dev/sda: 107GB
80Sector size (logical/physical): 512B/512B
81Partition Table: msdos
82Disk Flags: 
83
84Number  Start   End     Size    Type      File system  Flags
85 1      1049kB  98.0GB  98.0GB  primary   ext4         boot
86 2      98.0GB  107GB   9373MB  extended               lba
87 5      98.0GB  107GB   9372MB  logical   ext4         lba
88
89(parted) q                                                                
90Information: You may need to update /etc/fstab.

3. 分区扩容后使用resize2fs调整文件系统容量。操作记录略。

3.3 重新启用交换分区

最后重新格式化成交换分区,并重新启用:

1root@devserver:~# mkswap /dev/sda5
2Setting up swapspace version 1, size = 8.7 GiB (9372168192 bytes)
3no label, UUID=0d7f7cb5-ec6c-4843-8d8d-f53df4f6bf23

因为重建了交换分区,交换分区的UUID变了,所以需要编辑/etc/fstab替换之前的交换分区设置。如果不修改,会导致交换分区无法自动挂载。

1# swap was on /dev/sda5 during installation
2UUID=0d7f7cb5-ec6c-4843-8d8d-f53df4f6bf23 none            swap    sw              0       0

然后使用swapon命令启用交换分区即可。

1root@devserver:~# swapon --all --verbose
2swapon: /dev/sda5: found signature [pagesize=4096, signature=swap]
3swapon: /dev/sda5: pagesize=4096, swapsize=9372172288, devsize=9372172288
4swapon /dev/sda5
5root@devserver:~# free -h
6              total        used        free      shared  buff/cache   available
7Mem:           7.8G        6.5G        120M         12M        1.2G        1.1G
8Swap:          8.7G          0B        8.7G

附录、参考资料

  • 通过LVM扩容逻辑卷
  • Resizing an EXT4 or XFS partition on CentOS without unmounting
  • 8 Linux ‘Parted’ Commands to Create, Resize and Rescue Disk Partitions
#Linux# #Ext4#
在Jenkins Maven Plugin中指定不同的JDK版本
奇怪现象:两个Consumer消费同一个分区(Partition)
  • 文章目录
  • 站点概览
Orchidflower

Orchidflower

Do one thing at a time, and do well.

77 日志
6 分类
84 标签
GitHub 知乎 OSC 豆瓣
  • 1. 背景
  • 2. 具体操作
    • 2.1 查看分区信息
    • 2.2 调整分区大小
    • 2.3 文件系统扩容
  • 3. 另一个例子
    • 3.1 原始状态
    • 3.2 分区扩容
    • 3.3 重新启用交换分区
  • 附录、参考资料
© 2009 - 2024 执子之手
Powered by - Hugo v0.113.0
Theme by - NexT
ICP - 鲁ICP备17006463号-1
0%