LoginSignup
6
5

More than 5 years have passed since last update.

CoreOSでswapを追加する

Posted at

はじめに

基本的な考えとしてはSWAPに割り当てる領域を確保し、
systemd経由で起動時にSWAPとして割り当てるようにする感じです。
今回は立ち上げてしまったCoreOSに対してしていますが、
cloud-config.ymlを使う場合でもほぼ同様の考え方でできるはずです。

手順(1GB追加するのを例に)

swap領域を作る

まず、/配下に1GiB.swapというファイルを作ってそれをswap領域にします。

touch /1GiB.swap
chattr +C /1GiB.swap
fallocate -l 1024m /1GiB.swap
chmod 600 /1GiB.swap
mkswap /1GiB.swap

systemdのserviceとして追加する

次にsystemdにswapサービスを認識させるためにファイルを作ります。

vim /etc/systemd/system/swap.service
cat /etc/systemd/system/swap.service
[Unit]
Description=Turn on swap

[Service]
Type=oneshot
Environment="SWAPFILE=/1GiB.swap"
RemainAfterExit=true
ExecStartPre=/usr/sbin/losetup -f ${SWAPFILE}
ExecStart=/usr/bin/sh -c "/sbin/swapon $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
ExecStop=/usr/bin/sh -c "/sbin/swapoff $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"
ExecStopPost=/usr/bin/sh -c "/usr/sbin/losetup -d $(/usr/sbin/losetup -j ${SWAPFILE} | /usr/bin/cut -d : -f 1)"

[Install]
WantedBy=multi-user.target

ここまでできたらあとは有効化して再起動をするだけです。

systemctl enable swap.service
systemctl start swap.service
reboot

再起動したら

free -h

とかで確認して正常に追加されていることを確認しましょう。

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5