LoginSignup
5
0

More than 3 years have passed since last update.

さくらのクラウドUbuntu 20.04でスワップ領域を無効化する手順

Last updated at Posted at 2021-04-30

概要

さくらのクラウドのパブリックアーカイブ「Ubuntu 20.04」で、スワップ領域を無効化する手順です。

解説

スワップ領域を無効化するには、コマンド sudo swapoff -a を実行します。しかし、このコマンド実行は、一時的な対処です。

サーバ再起動後もスワップを使わないためには、一般的に /etc/fstab の swap 行のエントリをコメントアウトもしくは削除します。

また、さくらのクラウドの Ubuntu Server は、 /dev/vda2 にパーティション領域があります。

$ sudo swapon --show
NAME      TYPE      SIZE USED PRIO
/dev/vda2 partition   4G   0B   -2

そのため sudo systemctl mask "dev-vda2.swap" でスワップ設定のマスキングも必要です。

設定手順

Ubuntu Server にログインし、 free コマンドでメモリ領域を確認すると、 Swap: 行にスワップ領域を確認できます。

ubuntu@ubuntu2004:~$ free -h
              total        used        free      shared  buff/cache   available
Mem:          981Mi        71Mi       736Mi       0.0Ki       173Mi       774Mi
Swap:         4.0Gi          0B       4.0Gi

無効化するには sudo swapoff -a です。コマンドを実行した後から、スワップが無効化されます。

ubuntu@ubuntu2004:~$ sudo swapoff -a
ubuntu@ubuntu2004:~$ free -h
              total        used        free      shared  buff/cache   available
Mem:          981Mi        75Mi       732Mi       0.0Ki       173Mi       770Mi
Swap:            0B          0B          0B

ただし、この段階では一時的なものであり、サーバが再起動すると再びスワップが使用されてしまいます。

これを回避するには /etc/fstab のエントリ削除と /dev/vda2 のスワップユニットを削除(マスク)する必要があります。

1. /etc/fstab の編集

sudo vi /etc/fstab などで、 /etc/fstab ファイルを開き、編集します。ファイル末尾、11行目にこのような UUID= で始まるエントリ行があります。これを、コメントアウトもしくは削除します。

# swap was on /dev/vda2 during installation
UUID=2ff82d00-dbd6-4577-9924-3debf3284249 none            swap    sw              0       0

↓こうなっていれば、このようにコメントします。

# swap was on /dev/vda2 during installation
#UUID=2ff82d00-dbd6-4577-9924-3debf3284249 none            swap    sw              0       0

2. /dev/vda2 のスワップユニットを削除

次に、 systemctl --type swap コマンドを実行し、現在のスワップ情報を確認します。

ubuntu@ubuntu2004:~$ systemctl --type swap
  UNIT                                                                      LOAD   ACTIVE SUB    DESCRIPTION
  dev-disk-by\x2duuid-2ff82d00\x2ddbd6\x2d4577\x2d9924\x2d3debf3284249.swap loaded active active /dev/disk/by-uuid/2ff82d00-dbd6-4577-9924-debf3284249

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

1 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

これは、 swapon --show コマンドで確認すると、 /dev/vda2 の swap 用パーティション領域を使っています。

ubuntu@ubuntu2004:~$ swapon --show
NAME      TYPE      SIZE USED PRIO
/dev/vda2 partition   4G   0B   -2

この /dev/vda2 を読み込まないようにするため、systemd でもスワップ領域のマスキングをします。

ubuntu@ubuntu2004:~$ sudo systemctl mask "dev-vda2.swap"
Created symlink /etc/systemd/system/dev-vda2.swap → /dev/null.

それから、もう一度 systemctl --type swap を実行し、 /dev/vda2 のスワップをマスクしているのを確認します。

ubuntu@ubuntu2004:~$ systemctl --type swap
  UNIT          LOAD   ACTIVE SUB    DESCRIPTION
● dev-vda2.swap masked active active /dev/vda2

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

1 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

再起動と動作確認

あとは、 sudo reboot などで再起動し、再起動後に何もしなくてもスワップ領域が発生していないことを確認します。

再起動後に再びログインすると、スワップ領域は発生しておらず、systemd でもスワップがマスクされているのが分かります。

ubuntu@ubuntu2004:~$ free -h
              total        used        free      shared  buff/cache   available
Mem:          981Mi        74Mi       801Mi       0.0Ki       105Mi       784Mi
Swap:            0B          0B          0B
ubuntu@ubuntu2004:~$ systemctl --type swap --all
  UNIT          LOAD   ACTIVE   SUB  DESCRIPTION
● dev-vda2.swap masked inactive dead dev-vda2.swap

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

1 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.

参考

  • man systemd.swap
5
0
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
5
0