スワップ領域を追加する方法

Ec2をケチケチ運用するときに、最近利用している。

スワップファイルの作成

$ sudo fallocate -l 2G /swapfile
$ ls -lh /swapfile
-rw-r–r– 1 root root 2.0G Dec 28 01:07 /swapfile
$ sudo chmod 600 /swapfile
$ ls -lh /swapfile
-rw——- 1 root root 2.0G Dec 28 01:07 /swapfile

スワップファイルへの変換

$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=b1032eeb-27b0-4b29-9790-3a3cc8d75ab5

$ sudo swapon /swapfile
$ sudo swapon –show
NAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -2

確認

$ free -h
total used free shared buff/cache available
Mem: 7.6Gi 919Mi 2.5Gi 28Mi 4.5Gi 6.7Gi
Swap: 2.0Gi 0B 2.0Gi

スワップファイルの永続化

$ sudo cp /etc/fstab /etc/fstab.bak
$ sudo sh -c “echo /swapfile none swap sw 0 0 >> /etc/fstab”
$ 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/ubuntu-vg/ubuntu-lv during curtin installation
/dev/disk/by-id/dm-uuid-LVM-JHI1NcPWWJ7nuHZ1ew5IKrkc6F3e6z8PhuUgAWPSxHcgPDKXlWzvnmZgbcwIZ8gM / ext4 defaults 0 1
# /boot was on /dev/sda2 during curtin installation
/dev/disk/by-uuid/2944fa52-419e-4afc-bbcc-157b86d48890 /boot ext4 defaults 0 1
/swapfile none swap sw 0 0

タイトルとURLをコピーしました