在 Linux 下管理磁盘分区

新建了一台机器,发现它的 swap 是单独创建的一块物理磁盘分区,我使用 fdisk 删除 swap 的 vda2 分区然后将容量合并到主分区中,记录一下基本的使用过程。

查看到的 swap 大致信息如下:

1
2
3
4
5
6
7
# free -h
total used free shared buff/cache available
Mem: 1.9Gi 226Mi 1.7Gi 2.0Mi 129Mi 1.7Gi
Swap: 1.0Gi 0B 1.0Gi
# swapon
NAME TYPE SIZE USED PRIO
/dev/vda2 partition 1023M 0B -2

发现 swap 在 vda2 上,直接删,然后以 swap 文件形式创建更大的。

先禁用 swap 分区。

1
2
3
4
# 禁用 /dev/vda2
sudo swapoff /dev/vda2
# 检查是否存在 fstab 条目,一般不存在。
sudo vim /etc/fstab

以下操作需要在 root 账户下操作,所有操作不可逆,操作分区表非常危险,务必备份重要文件后操作!!!

查看分区

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 进入 fdisk 操作。
sudo fdisk /dev/vda

# 进入 fdisk 交互。
Welcome to fdisk (util-linux 2.38.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.


Command (m for help):

输入 p 查看当前分区信息

1
2
3
4
5
6
7
8
9
10
11
12
Command (m for help): p

Disk /dev/vda: 120 GiB, 128849018880 bytes, 251658240 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
Disklabel type: gpt
Disk identifier: A6zz1F2A-27A6-4zzF-Bzz9-77CFzzAzz80C

Device Start End Sectors Size Type
/dev/vda1 2048 249561088 249559041 119G Linux filesystem
/dev/vda2 249563136 251658206 2095071 1023M Linux swap

可以看到 /dev/vda2 是 Linux swap 类型分区。

删除分区

通过上面的查询,找到了我们要删除的分区

交互界面输入 d 命令进行删除分区,其中 /dev/vda2 排列位为 2 号则输入 2 来删除它

1
2
3
4
Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

保存更改

输入命令 w 保存更改并退出。

拓展主分区

我们删掉分区后还需要将被删掉的分区容量合并进入主分区内。

变动分区表保存后,先重启一下机器让内核重新识别分区表的变动。

1
sudo reboot

再次进入 fdisk

1
2
3
4
5
6
7
8
9
10
11
sudo fdisk /dev/vda

# sudo fdisk /dev/vda

Welcome to fdisk (util-linux 2.38.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.

再次查看分区表,确认只有一个主分区

1
2
3
4
5
6
7
8
9
10
11
12

Command (m for help): p

Disk /dev/vda: 120 GiB, 128849018880 bytes, 251658240 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
Disklabel type: gpt
Disk identifier: A6zz1F2A-27A6-4zzF-Bzz9-77CFzzAzz80C

Device Start End Sectors Size Type
/dev/vda1 2048 249561088 249559041 119G Linux filesystem

然后我们将主分区表删除,稍后会重新建立主分区,这样可以完成拓展。

1
2
3
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

输入 n 命令,创建新分区。

  1. Partition number(分区号)输入:1
  2. First sector(起始扇区)输入:与之前的一致 我这里是 2048
  3. Last sector(结束扇区)输入:如果使用最大值可以直接回车,或者自定义大小
  4. Do you want to remove the signature? [Y]es/[N]o(是否移除签名 [Y]/[N]): 通常是选择 No
1
2
3
4
5
6
7
8
9
Command (m for help): n
Partition number (1-128, default 1): 1
First sector (34-251658206, default 2048): 2048
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-251658206, default 251656191):

Created a new partition 1 of type 'Linux filesystem' and of size 120 GiB.
Partition #1 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: n

然后直接输入 w 命令保存并退出 fdisk

1
2
3
4
Command (m for help): w

The partition table has been altered.
Syncing disks.

重启系统让系统重新读取分区表信息

1
sudo reboot

重启完毕后,拓展文件系统

1
2
3
4
5
6
7
sudo resize2fs /dev/vda1

# 返回
resize2fs 1.47.0 (5-Feb-2023)
Filesystem at /dev/vda1 is mounted on /; on-line resizing required
old_desc_blocks = 15, new_desc_blocks = 15
The filesystem on /dev/vda1 is now 31456768 (4k) blocks long.

检查文件系统大小是否成功拓展

使用 fdisk -l 查看, End 接近 251658240 sectors 则代表成功。

1
2
3
4
5
6
7
8
9
10
11
12
sudo fdisk -l /dev/vda

# 返回
Disk /dev/vda: 120 GiB, 128849018880 bytes, 251658240 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
Disklabel type: gpt
Disk identifier: A6zz1F2A-27A6-4zzF-Bzz9-77CFzzAzz80C

Device Start End Sectors Size Type
/dev/vda1 2048 251656191 251654144 120G Linux filesystem

使用 df -h 查看总容量

1
2
3
4
5
df -h /dev/vda1

# 返回
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 118G 1.3G 111G 2% /