# 进入 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):
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 forhelp): 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.
Device Start End Sectors Size Type /dev/vda1 2048 249561088 249559041 119G Linux filesystem
然后我们将主分区表删除,稍后会重新建立主分区,这样可以完成拓展。
1 2 3
Command (m forhelp): d Selected partition 1 Partition 1 has been deleted.
输入 n 命令,创建新分区。
Partition number(分区号)输入:1
First sector(起始扇区)输入:与之前的一致 我这里是 2048
Last sector(结束扇区)输入:如果使用最大值可以直接回车,或者自定义大小
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 forhelp): 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 forhelp): 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.