swapon

Section: System Calls (2)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

swapon, swapoff - ファイル/デバイスへのスワップを開始/停止する  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <sys/swap.h>

int swapon(const char *path, int swapflags);
int swapoff(const char *path);
 

説明

swapon() は path で指定されたファイルやブロックデバイスにスワップ領域を設定する。 swapoff() は path で指定されたファイルやブロックデバイスへのスワップを停止する。

swapon() の swapflags 引数に SWAP_FLAG_PREFER フラグが指定された場合、 新しいスワップ領域はデフォルトよりも高い優先度を持つ。 優先度は以下のように変換されて swapflags に指定する。

 (prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK

SWAP_FLAG_DISCARD フラグが swapon() の swapflags 引数に指定された場合、 スワップデバイスが破棄 (discard) 操作や trim 操作をサポートしている場合には、 解放されたスワップページは再利用される前に破棄される (これにより、SSD (Solid State Device) によっては性能が向上することがあるが、 たいていは性能の向上はない)。 「注意」も参照のこと。

これらの関数は特権プロセス (CAP_SYS_ADMIN ケーパビリティ (capability) を持つプロセス) のみが使用できる。  

優先度

それぞれのスワップ領域は高 (high) と低 (low) のどちらかの優先度を持つ。 デフォルトの優先度は低である。 低い優先度の領域において、新しい領域は古い領域よりさらに低い 優先度を持つ。

swapflags が設定されたものは全て高い優先度となり、デフォルトよりも高い優先度を持つ。 使用者はそれらに負でない値が指定できる。 大きな数字は高い優先度を意味する。

高い優先度の領域から順にスワップページとして使用される。 より低い優先度の領域を使用する前により高い優先度の 領域を使い切る。もし二つ以上の領域が同じ優先度を持ち、 使える中で一番高い優先度であれば、それらのページは間で ラウンドロビン方式で配分される。

Linux 1.3.6 において、カーネルは通常はこれらの規則に従っている。 しかし例外も存在している。  

返り値

成功すると、0 を返す。 エラーの場合、-1 を返し、 errno にエラーを示す値を設定する。  

エラー

EBUSY
(swapon() において) 指定された path が既にスワップ領域として使用されている。
EINVAL
ファイル path は存在するが、通常のファイルもブロックデバイスも参照していない。
EINVAL
(swapon()) 指定されたパスが有効なスワップの署名 (signature) を含んでいないか、tmpfs(5) のようなインメモリーファイルシステム上にある。
EINVAL (Linux 3.4 以降)
(swapon()) swapflags に無効なフラグ値が指定された。
EINVAL
(swapoff()) path が現在スワップ領域ではない。
ENFILE
オープンされたファイルの総数がシステム全体の上限に達していた。
ENOENT
ファイル path が存在しない。
ENOMEM
スワップを開始するのに十分なメモリーがシステムにない。
EPERM
使用者が CAP_SYS_ADMIN ケーパビリティを持っていない。 もしくは、最大数のスワップファイルがすでに使用されている (下記の「注意」の節を参照)。
 

標準規格

Linux.  

歴史

The swapflags argument was introduced in Linux 1.3.2.  

注意

パーティションやパスは mkswap(8) によって準備されていなければならない。

There is an upper limit on the number of swap files that may be used, defined by the kernel constant MAX_SWAPFILES. Before Linux 2.4.10, MAX_SWAPFILES has the value 8; since Linux 2.4.10, it has the value 32. Since Linux 2.6.18, the limit is decreased by 2 (thus 30), since Linux 5.19, the limit is decreased by 3 (thus: 29) if the kernel is built with the CONFIG_MIGRATION option (which reserves two swap table entries for the page migration features of mbind(2) and migrate_pages(2)). Since Linux 2.6.32, the limit is further decreased by 1 if the kernel is built with the CONFIG_MEMORY_FAILURE option. Since Linux 5.14, the limit is further decreased by 4 if the kernel is built with the CONFIG_DEVICE_PRIVATE option. Since Linux 5.19, the limit is further decreased by 1 if the kernel is built with the CONFIG_PTE_MARKER option.

Discard of swap pages was introduced in Linux 2.6.29, then made conditional on the SWAP_FLAG_DISCARD flag in Linux 2.6.36, which still discards the entire swap area when swapon() is called, even if that flag bit is not set.  

関連項目

mkswap(8), swapoff(8), swapon(8)


 

Index

名前
ライブラリ
書式
説明
優先度
返り値
エラー
標準規格
歴史
注意
関連項目

This document was created by man2html, using the manual pages.