0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Oracle LinuxでSwap領域を確認・削除・永続化する

0
Posted at

前の記事

構築編

環境確認編

Swap領域拡張編

swapメモリ操作

1.スワップメモリの確認

1.1.メモリサイズを確認

swaponコマンドで確認

swapon --showで確認

[opc@test-server ~]$ swapon --show
NAME          TYPE SIZE   USED PRIO
/.swapfile    file 945M 125.5M   -2
/var/swapfile file   2G  34.4M   -3

2.スワップメモリの削除

2.1.特定スワップメモリの削除

swapoffコマンドで削除

swapon状態の確認

[opc@test-server ~]$ swapon --show
NAME          TYPE SIZE   USED PRIO
/.swapfile    file 945M 125.5M   -2
/var/swapfile file   2G  34.4M   -3

swapoffで削除

[opc@test-server ~]$ sudo swapoff /var/swapfile

swapon状態の確認

[opc@test-server ~]$ swapon --show
NAME       TYPE SIZE   USED PRIO
/.swapfile file 945M 125.2M   -2

2.2.全スワップメモリの削除

swapoffコマンドで削除

swapon状態の確認

[opc@test-server ~]$ swapon --show
NAME          TYPE SIZE   USED PRIO
/.swapfile    file 945M 125.5M   -2
/var/swapfile file   2G  34.4M   -3

swapoffで削除

[opc@test-server ~]$ sudo swapoff -a

swapon状態の確認

[opc@test-server ~]$ swapon --show
[opc@test-server ~]$

ないので表示されません・・・

3.スワップメモリの永続化(追加部分編)

作成しただけでは、再起動したときに使えなくなります。
次回も使えるようにするためには設定が必要です。

3.1.追加したスワップメモリが消える!

swaponコマンドで追加を確認後、システム再起動

swapon状態の確認

[opc@test-server ~]$ swapon --show
NAME          TYPE SIZE   USED PRIO
/.swapfile    file 945M 125.5M   -2
/var/swapfile file   2G  34.4M   -3

システム再起動

[opc@test-server ~]$ sudo reboot

システム再起動後の確認

[opc@test-server ~]$ swapon --show
NAME          TYPE SIZE  USED PRIO
/.swapfile    file 945M 57.9M   -2

スワップメモリから/var/swapfileの部分がなくなった!

3.2.追加したスワップメモリの永続化(追加ファイル編)

swapファイルの設定を確認・修正後、システム再起動

設定ファイルの確認

[opc@test-server ~]$ sudo cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jan  8 18:36:00 2026
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/ocivolume-root /                       xfs     defaults        0 0
UUID=7eb9440a-cb64-4a9a-b727-241049bbfbae /boot                   xfs     defaults        0 0
UUID=90CB-E99D          /boot/efi               vfat    defaults,uid=0,gid=0,umask=077,shortname=winnt 0 2
/dev/mapper/ocivolume-oled /var/oled               xfs     defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults,nodev,nosuid,noexec      0 0
######################################
## ORACLE CLOUD INFRASTRUCTURE CUSTOMERS
##
## If you are adding an iSCSI remote block volume to this file you MUST
## include the '_netdev' mount option or your instance will become
## unavailable after the next reboot.
## SCSI device names are not stable across reboots; please use the device UUID instead of /dev path.
##
## Example:
## UUID="94c5aade-8bb1-4d55-ad0c-388bb8aa716a"   /data1    xfs       defaults,noatime,_netdev      0      2
##
## More information:
## https://docs.us-phoenix-1.oraclecloud.com/Content/Block/Tasks/connectingtoavolume.htm
/.swapfile      none    swap    sw      0       0

最後の

/.swapfile      none    swap    sw      0       0

がスワップメモリ領域のファイルです。

/etc/fstabへの追加(teeを使ってみる編)
echoで標準に出力された内容を、|(パイプ)でtee -a(追加)でファイルに追加。権限が必要なのでsudoで実行。

[opc@test-server ~]$ echo '/var/swapfile   none    swap    sw      0       0' |
sudo tee -a /etc/fstab
/var/swapfile   none    swap    sw      0       0
[opc@test-server ~]$ cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jan  8 18:36:00 2026
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/ocivolume-root /                       xfs     defaults        0 0
UUID=7eb9440a-cb64-4a9a-b727-241049bbfbae /boot                   xfs     defaults        0 0
UUID=90CB-E99D          /boot/efi               vfat    defaults,uid=0,gid=0,umask=077,shortname=winnt 0 2
/dev/mapper/ocivolume-oled /var/oled               xfs     defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults,nodev,nosuid,noexec      0 0
######################################
## ORACLE CLOUD INFRASTRUCTURE CUSTOMERS
##
## If you are adding an iSCSI remote block volume to this file you MUST
## include the '_netdev' mount option or your instance will become
## unavailable after the next reboot.
## SCSI device names are not stable across reboots; please use the device UUID instead of /dev path.
##
## Example:
## UUID="94c5aade-8bb1-4d55-ad0c-388bb8aa716a"   /data1    xfs       defaults,noatime,_netdev      0      2
##
## More information:
## https://docs.us-phoenix-1.oraclecloud.com/Content/Block/Tasks/connectingtoavolume.htm
/.swapfile      none    swap    sw      0       0
/var/swapfile   none    swap    sw      0       0

で完成(もちろんviでもOK)

リブートして

[opc@test-server ~]$ sudo reboot

確認

[opc@test-server ~]$ swapon --show
NAME          TYPE SIZE   USED PRIO
/.swapfile    file 945M 125.5M   -2
/var/swapfile file   2G  34.4M   -3

swapon状態の確認

[opc@test-server ~]$ swapon --show
NAME       TYPE SIZE   USED PRIO
/.swapfile file 945M 125.2M   -2

3.3.現時点のスワップメモリ領域のサイズ拡張

swapファイルを解除したのち、ファイルサイズを拡張

swap領域の状態確認

[opc@test-server ~]$ swapon --show
NAME       TYPE SIZE   USED PRIO
/.swapfile file 945M 125.2M   -2

swap領域の解放

[opc@test-server ~]$ sudo swapon -a
[opc@test-server ~]$ swapon --show
[opc@test-server ~]$ free
               total        used        free      shared  buff/cache   available
Mem:          510236      324152        5084        3224      207424      186084
Swap:              0           0           0

なくなったので、表示なし。freeコマンドでも確認。

ddファイルでswapファイルを作成/権限の変更

swapファイルの作成

[opc@test-server ~]$ sudo dd if=/dev/zero of=/.swapfile bs=1M count=4096
4096+0 records in
4096+0 records out
4294967296 bytes (4.3 GB, 4.0 GiB) copied, 84.9195 s, 50.6 MB/s
オプション 内容 備考
if 入力ファイル /dev/zeroを指定し、新規作成領域を0で埋める
of 主力ファイル /var/swapfileを指定し、/var/swapfileを新規スワップファイルとする
bs ブロックサイズ 1M(1MB)を指定
count ブロックサイズ繰り返し ブロックサイズ1MB×4096回(4GB)を指定
がスワップメモリ領域のファイルです。

swapファイルとして作成する

[opc@test-server ~]$ sudo mkswap /.swapfile
mkswap: /.swapfile: insecure permissions 0644, fix with: chmod 0600 /.swapfile
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=85feef6a-989b-41bc-abe6-c338eb62194b

chmodで権限変更し、変更後は確認

[opc@test-server ~]$ sudo chmod 600 /var/swapfile
[opc@test-server ~]$ ll /var/swapfile
-rw-------. 1 root root 4294967296 xxx 999 99:99 /var/swapfile
オプション 内容 備考
600 所有者:6(読取:4=アリ, 書込:2=アリ,実行:0=無し)
所有グループ:0(アクセス権無し)
その他:0(アクセス権無し)

fstabを確認

[opc@test-server ~]$ cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jan  8 18:36:00 2026
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/ocivolume-root /                       xfs     defaults        0 0
UUID=7eb9440a-cb64-4a9a-b727-241049bbfbae /boot                   xfs     defaults        0 0
UUID=90CB-E99D          /boot/efi               vfat    defaults,uid=0,gid=0,umask=077,shortname=winnt 0 2
/dev/mapper/ocivolume-oled /var/oled               xfs     defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults,nodev,nosuid,noexec      0 0
######################################
## ORACLE CLOUD INFRASTRUCTURE CUSTOMERS
##
## If you are adding an iSCSI remote block volume to this file you MUST
## include the '_netdev' mount option or your instance will become
## unavailable after the next reboot.
## SCSI device names are not stable across reboots; please use the device UUID instead of /dev path.
##
## Example:
## UUID="94c5aade-8bb1-4d55-ad0c-388bb8aa716a"   /data1    xfs       defaults,noatime,_netdev      0      2
##
## More information:
## https://docs.us-phoenix-1.oraclecloud.com/Content/Block/Tasks/connectingtoavolume.htm
/.swapfile      none    swap    sw      0       0

リブートして

[opc@test-server ~]$ sudo reboot -h now

再起動して確認

[opc@test-server ~]$ swapon --show
NAME          TYPE SIZE   USED PRIO
/.swapfile    file   4G     0B   -2
[opc@test-server ~]$ free
               total        used        free      shared  buff/cache   available
Mem:          968220      482232      334492        3892      294208      485988
Swap:        4194300           0     4194300

最後に

記事の内容はCC BY-SA 4.0(著作者の情報とCCライセンス継承はお願いします。商用利用・改変・再配布は問題なし)です。

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?