Ubuntu20.04调整swap分区大小笔记
Ubuntu20.04调整swap分区大小笔记
参考:https://blog.csdn.net/sunyuhua_keyboard/article/details/142485764
第一步 禁用swap
$ cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda2 during installation
UUID=bbc5a35b-b700-4792-a9ba-7d643a95f9c5 / ext4 errors=remount-ro 0 1
# /boot/efi was on /dev/sda1 during installation
UUID=7CDB-0C3D /boot/efi vfat umask=0077 0 1
/swapfile none swap sw 0 0
UUID=9bfb904b-d2b4-43aa-ba49-916e6c4d6320 /root/workspace ext4 defaults 0 0
$ swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 2G 1G -2
$ free -h
总计 已用 空闲 共享 缓冲/缓存 可用
内存: 15Gi 1.0Gi 7.2Gi 45Mi 7.3Gi 14Gi
交换: 2.0Gi 1.0Gi 983Mi
$ swapoff -a
$ swapon --show
$ free -h
总计 已用 空闲 共享 缓冲/缓存 可用
内存: 15Gi 2.0Gi 6.1Gi 73Mi 7.4Gi 13Gi
交换: 0B 0B 0B
第二步 切换swapfile文件
$ rm /swapfile
$ ls -al / | grep swapfile
$ fallocate -l 16G /swapfile
$ ls -al / | grep swapfile
-rw-r--r-- 1 root root 17179869184 12月 10 14:13 swapfile
$ chmod 600 /swapfile
$ ls -al / | grep swapfile
-rw------- 1 root root 17179869184 12月 10 14:13 swapfile
$ mkswap /swapfile
正在设置交换空间版本 1,大小 = 16 GiB (17179865088 个字节)
无标签, UUID=8127980c-6104-424c-952b-d394bbce95e5
$ ls -al / | grep swapfile
-rw------- 1 root root 17179869184 12月 10 14:16 swapfile
第三步 启用新的swap
$ swapon /swapfile
$ swapon --show
NAME TYPE SIZE USED PRIO
/swapfile file 16G 0B -2
$ free -h
总计 已用 空闲 共享 缓冲/缓存 可用
内存: 15Gi 2.0Gi 6.8Gi 73Mi 6.6Gi 13Gi
交换: 15Gi 0B 15Gi
$ cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/sda2 during installation
UUID=bbc5a35b-b700-4792-a9ba-7d643a95f9c5 / ext4 errors=remount-ro 0 1
# /boot/efi was on /dev/sda1 during installation
UUID=7CDB-0C3D /boot/efi vfat umask=0077 0 1
/swapfile none swap sw 0 0
UUID=9bfb904b-d2b4-43aa-ba49-916e6c4d6320 /root/workspace ext4 defaults 0 0