Linux adds a new partition as a swap partition

Today I plan to upgrade Ubuntu from 20.04 to 20.04.1, and found that the size of the boot partition is not enough. Since the boot partition is connected to the swap partition, I deleted the swap partition, and then expanded the boot partition. After the expansion, I found that it could no longer be allocated as a swap partition. The tutorials on the Internet also split the space from the root partition to the swap partition. In this case, this blank storage cannot be used, and the root partition will be reduced a lot. After some attempts, this problem was finally solved, and I will record it.

1. Format the partition as ext4

Here I mount this new partition to /media/xxx/Swap. In addition, it must be set to automatically mount at startup:

sudo gedit /etc/fstab

In the fstab file add

UUID=可通过磁盘管理器查看 /media/xxx/Swap            ext4    defaults        0       2

2. Create partition files

Most of the online tutorials are through

cd /xxxxx/swap
sudo dd if=/dev/zero of=swapfile bs=1K count=4M

Create, this does not match my scenario, so use the following command to create a swapfile

cd /mdeia/xxx/Swap
# 在当前目录下创建 swapfile
sudo fallocate -l 8G ./swapfile
# 更改swapfile文件属性避免误用
sudo chmod 600 swapfile
# 初始化swapfile为swap文件
sudo mkswap swapfile
# 启用交换文件
sudo swapon swapfile

Then you can pass

swapon -s

or

free -m

Come check it out.

3. Set the swapfile to start automatically at boot

echo "/xxx/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab

4. Summary

In this way, the swap partition can work normally. If the ext4 partition in the first step is completely allocated to swapfile, the system will prompt that this partition has insufficient space, just ignore it, and this partition will not store any other files.

Guess you like

Origin blog.csdn.net/niuzhucedenglu/article/details/126686620