「Tmpfs」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
(ページの作成:「{{Lowercase title}} Category:ファイルシステム en:tmpfs tmpfs はメモリやスワップパーティションに存在する一時フ...」)
 
8行目: 8行目:
 
== 使用方法 ==
 
== 使用方法 ==
   
Some directories where tmpfs is commonly used are [http://www.pathname.com/fhs/2.2/fhs-3.15.html /tmp], [http://www.pathname.com/fhs/2.2/fhs-5.9.html /var/lock] and [http://www.pathname.com/fhs/2.2/fhs-5.13.html /var/run]. Do '''not''' use it on [http://www.pathname.com/fhs/2.2/fhs-5.15.html /var/tmp], because that folder is meant for temporary files that are preserved across reboots.
+
tmpfs が使われる主なディレクトリは [http://www.pathname.com/fhs/2.2/fhs-3.15.html /tmp], [http://www.pathname.com/fhs/2.2/fhs-5.9.html /var/lock], [http://www.pathname.com/fhs/2.2/fhs-5.13.html /var/run] です。tmpfs [http://www.pathname.com/fhs/2.2/fhs-5.15.html /var/tmp] で使ってはいけません。このフォルダは、再起動しても消えない一時ファイルを置くところだからです。
   
{{Pkg|glibc}} 2.2 and above expects tmpfs to be mounted at {{ic|/dev/shm}} for [[wikipedia:Shared_memory#Support_on_UNIX_platforms|POSIX shared memory]]. Mounting tmpfs at {{ic|/dev/shm}} is handled automatically by [[systemd]], so manual configuration in [[fstab]] is no longer necessary.
+
{{Pkg|glibc}} 2.2 以上では tmpfs [[wikipedia:ja:共有メモリ#UNIXでのサポート|POSIX shared memory]] {{ic|/dev/shm}} にマウントされることになっています。{{ic|/dev/shm}} への tmpfs のマウントは [[systemd]] によって自動的に行われるため、[[fstab]] における手動設定は必要なくなりました。
   
Arch uses a tmpfs {{ic|/run}} directory, with {{ic|/var/run}} and {{ic|/var/lock}} simply existing as symlinks for compatibility. It is also used for {{ic|/tmp}} by the default systemd setup and does not require an entry in [[fstab]] unless a specific configuration is needed.
+
Arch tmpfs {{ic|/run}} ディレクトリを使っており、{{ic|/var/run}} {{ic|/var/lock}} は互換性維持のため単なるシンボリックリンクとして存在しています。また、systemd のデフォルト設定で {{ic|/tmp}} で使用されるため、特別な設定が必要ないかぎり [[fstab]] にエントリを記述する必要はありません。
   
  +
一般的に、tmpfs フォルダを使う意味があるのは、読み書き操作を頻繁に実行するような I/O の処理が重い作業やプログラムです。アプリケーションによっては、部分的な (または全ての) データを共有メモリにオフロードすることで体感することができるほどの変化が現れることもあります。例えば、[[Firefox_Ramdisk|Firefox のプロファイルを RAM に再配置]]するとパフォーマンスが著しく上昇します。
Generally, I/O intensive tasks and programs that run frequent read/write operations can benefit from using a tmpfs folder. Some applications can even receive a substantial gain by offloading some (or all) of their data onto the shared memory. For example, [[Firefox_Ramdisk|relocating the Firefox profile into RAM]] shows a significant improvement in performance.
 
   
 
== サンプル ==
 
== サンプル ==
   
  +
デフォルトでは、tmpfs パーティションの最大容量は合計 RAM の半分に設定されていますが、この値はカスタマイズすることができます。実際に使用されるメモリ/スワップは使用する量によって変わり、必要とされるまで tmpfs パーティションはメモリを消費しないので注意してください。
By default, a tmpfs partition has its maximum size set to half your total RAM, but this can be customized. Note that the actual memory/swap consumption depends on how much you fill it up, as tmpfs partitions do not consume any memory until it is actually needed.
 
   
  +
最大容量を明示的に設定するために、以下の例ではデフォルトの {{ic|/tmp}} マウントを上書きして、{{ic|size}} マウントオプションを使っています:
To explicitly set a maximum size, in this example to override the default {{ic|/tmp}} mount, use the {{ic|size}} mount option:
 
   
 
{{hc|/etc/fstab|2=
 
{{hc|/etc/fstab|2=
34行目: 34行目:
 
Reboot for the changes to take effect. Note that although it may be tempting to simply run {{ic|mount -a}} to make the changes effective immediately, this will make any files currently residing in these directories inaccessible (this is especially problematic for running programs with lockfiles, for example). However, if all of them are empty, it should be safe to run {{ic|mount -a}} instead of rebooting (or mount them individually).
 
Reboot for the changes to take effect. Note that although it may be tempting to simply run {{ic|mount -a}} to make the changes effective immediately, this will make any files currently residing in these directories inaccessible (this is especially problematic for running programs with lockfiles, for example). However, if all of them are empty, it should be safe to run {{ic|mount -a}} instead of rebooting (or mount them individually).
   
  +
変更の適用後、{{ic|findmnt}} を使って {{ic|/proc/mounts}} を見ることで効果が現れているかどうか確認することができます:
After applying changes, you may want to verify that they took effect by looking at {{ic|/proc/mounts}} and using {{ic|findmnt}}:
 
   
 
{{hc|$ findmnt --target /tmp|
 
{{hc|$ findmnt --target /tmp|
40行目: 40行目:
 
/tmp tmpfs tmpfs rw,nosuid,nodev,relatime}}
 
/tmp tmpfs tmpfs rw,nosuid,nodev,relatime}}
   
  +
また、tmpfs は再起動せずとも一時的にサイズを変更することが可能です。いますぐ巨大なコンパイルを行う必要があるときなどに有用です。その場合、以下を実行します:
The tmpfs can also be temporarily resized without the need to reboot, for example when a large compile job needs to run soon. In this case, you can run:
 
   
 
# mount -o remount,size=4G,noatime /tmp
 
# mount -o remount,size=4G,noatime /tmp
 
=== Ramdisk ===
 
 
To create a directory whose files are actually stored in RAM, we may adapt the tmpfs example:
 
 
{{hc|/etc/fstab|2=
 
tmpfs /home/archie/Ramdisk tmpfs nodev,nosuid,size=2G 0 0}}
 
   
 
== 自動マウントの無効化 ==
 
== 自動マウントの無効化 ==

2015年3月7日 (土) 12:44時点における版

tmpfs はメモリやスワップパーティションに存在する一時ファイルシステムです。ディレクトリを tmpfs としてマウントすることでファイルへのアクセスを高速化させたり、再起動時に自動的に中身が消去されるようにすることができます。

ノート: systemd を使っている場合、tmpfiles.d を使って tmpfs ディレクトリの一時ファイルを起動時に再作成することができます。

使用方法

tmpfs が使われる主なディレクトリは /tmp, /var/lock, /var/run です。tmpfs を /var/tmp で使ってはいけません。このフォルダは、再起動しても消えない一時ファイルを置くところだからです。

glibc 2.2 以上では tmpfs は POSIX shared memory/dev/shm にマウントされることになっています。/dev/shm への tmpfs のマウントは systemd によって自動的に行われるため、fstab における手動設定は必要なくなりました。

Arch は tmpfs の /run ディレクトリを使っており、/var/run/var/lock は互換性維持のため単なるシンボリックリンクとして存在しています。また、systemd のデフォルト設定で /tmp で使用されるため、特別な設定が必要ないかぎり fstab にエントリを記述する必要はありません。

一般的に、tmpfs フォルダを使う意味があるのは、読み書き操作を頻繁に実行するような I/O の処理が重い作業やプログラムです。アプリケーションによっては、部分的な (または全ての) データを共有メモリにオフロードすることで体感することができるほどの変化が現れることもあります。例えば、Firefox のプロファイルを RAM に再配置するとパフォーマンスが著しく上昇します。

サンプル

デフォルトでは、tmpfs パーティションの最大容量は合計 RAM の半分に設定されていますが、この値はカスタマイズすることができます。実際に使用されるメモリ/スワップは使用する量によって変わり、必要とされるまで tmpfs パーティションはメモリを消費しないので注意してください。

最大容量を明示的に設定するために、以下の例ではデフォルトの /tmp マウントを上書きして、size マウントオプションを使っています:

/etc/fstab
tmpfs   /tmp         tmpfs   nodev,nosuid,size=2G          0  0

Here is a more advanced example showing how to add tmpfs mounts for users. This is useful for websites, mysql tmp files, ~/.vim/, and more. It's important to try and get the ideal mount options for what you are trying to accomplish. The goal is to have as secure settings as possible to prevent abuse. Limiting the size, and specifying uid and gid + mode is very secure. For more information on this subject, follow the links listed in the #See also section.

/etc/fstab
tmpfs   /www/cache    tmpfs  rw,size=1G,nr_inodes=5k,noexec,nodev,nosuid,uid=648,gid=648,mode=1700   0  0

See the mount command man page for more information. One useful mount option in the man page is the default option. At least understand that.

Reboot for the changes to take effect. Note that although it may be tempting to simply run mount -a to make the changes effective immediately, this will make any files currently residing in these directories inaccessible (this is especially problematic for running programs with lockfiles, for example). However, if all of them are empty, it should be safe to run mount -a instead of rebooting (or mount them individually).

変更の適用後、findmnt を使って /proc/mounts を見ることで効果が現れているかどうか確認することができます:

$ findmnt --target /tmp
TARGET SOURCE FSTYPE OPTIONS
/tmp   tmpfs  tmpfs  rw,nosuid,nodev,relatime

また、tmpfs は再起動せずとも一時的にサイズを変更することが可能です。いますぐ巨大なコンパイルを行う必要があるときなどに有用です。その場合、以下を実行します:

# mount -o remount,size=4G,noatime /tmp

自動マウントの無効化

systemd 下では、/etc/fstab にエントリを記述してなくても /tmp は自動的に tmpfs としてマウントされます。

自動マウントを無効にするには、次を実行:

# systemctl mask tmp.mount

ファイルは tmpfs ではなく、ブロックデバイスに保存されるようになります。/tmp の中身は再起動しても消去されないようになるので、問題が起こる可能性があります。前の挙動に戻して再起動で /tmp フォルダが自動的に消去されるようにするには、以下の tmpfiles.d(5) を使ってください:

/etc/tmpfiles.d/tmp.conf
# see tmpfiles.d(5)
# always enable /tmp folder cleaning
D! /tmp 1777 root root 0

# remove files in /var/tmp older than 10 days
D /var/tmp 1777 root root 10d

# namespace mountpoints (PrivateTmp=yes) are excluded from removal
x /tmp/systemd-private-*
x /var/tmp/systemd-private-*
X /tmp/systemd-private-*/tmp
X /var/tmp/systemd-private-*/tmp

トラブルシューティング

root で tmpfs のシンボリックリンクを開けない

Considering /tmp is using tmpfs, change the current directory to /tmp, then create a file and create a symlink to that file in the same /tmp directory. If you try to open the file you created via the symlink, you will get a permission denied error. This is expected as /tmp has the sticky bit set.

この挙動は /proc/sys/fs/protected_symlinks や sysctl で変更できます: sysctl -w fs.protected_symlinks=0。設定を永続化させる方法は Sysctl#設定 を見て下さい。

警告: Changing this behaviour can lead to security issues! Disable it only if you know what you are doing.

参照