Raspberry Pi

Raspberry Pi 2 Model B でSDカード寿命を延ばすためにRAMディスクを使う

Raspberry_Pi_logo

Raspberry Pi 2を24時間稼働させるサーバーとして使うことはSDカードを酷使することになります。

ご存じの方も多いと思いますが、SDカードはNANDフラッシュが使われており、このNANDフラッシュには書き換え寿命が存在するのです。

実際、SDカードが突然死ぬ症状に会われた方も少なくないようなので、延命処置として、SDカードへの書込みを極力減らす措置をしたいと思います。

① SWAPを使わないようにする

freeコマンドでswapサイズを確認してみると、99MBが確保されています。usedは0ですが、これを無効化します。

[bash]
pi@raspberrypi ~ $ free -m
total used free shared buffers cached
Mem: 927 75 852 0 6 31
-/+ buffers/cache: 37 889
Swap: 99 0 99
[/bash]

chkconfigを使ってswapファイルを作らないよう設定します。

[bash]
pi@raspberrypi ~ $ chkconfig dphys-swapfile --list
dphys-swapfile 0:off 1:off 2:on 3:on 4:on 5:on 6:off
pi@raspberrypi ~ $ sudo chkconfig dphys-swapfile off
pi@raspberrypi ~ $ chkconfig dphys-swapfile --list
dphys-swapfile 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[/bash]

②RAMディスクの設定

RAMディスクに/tmpと/var/tmpと/var/logを移動します。以下のようにfstabの最後の3行を追加しました。

[bash]
pi@raspberrypi ~ $ sudo vi /etc/fstab
proc /proc proc defaults 0 0
/dev/mmcblk0p5 /boot vfat defaults 0 2
/dev/mmcblk0p6 / ext4 defaults,noatime 0 1
# a swapfile is not a swap partition, so no using swapon|off from here on, use
dphys-swapfile swap[on|off] for that
tmpfs /tmp tmpfs defaults,size=32m,noatime,mode=1777 0 0
tmpfs /var/tmp tmpfs defaults,size=16m,noatime,mode=1777 0 0
tmpfs /var/log tmpfs defaults,size=32m,noatime,mode=0755  0 0
[/bash]

再起動して、dfコマンドで正しくマウントされているかを確認します。下の3行が新たに追加となった設定です。

[bash]
pi@raspberrypi ~ $ df -h
ファイルシス サイズ 使用 残り 使用% マウント位置
rootfs 14G 2.6G 11G 21% /
/dev/root 14G 2.6G 11G 21% /
devtmpfs 460M 0 460M 0% /dev
tmpfs 93M 248K 93M 1% /run
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 186M 0 186M 0% /run/shm
/dev/mmcblk0p5 60M 19M 42M 32% /boot
tmpfs 32M 1.7M 31M 6% /tmp
tmpfs 16M 0 16M 0% /var/tmp
tmpfs 16M 112K 16M 1% /var/log
[/bash]

不要なログは作成しないようにする

rsyslog.confを変更して不要なログを作成しないようにします。不要な行の頭にコメントアウトするだけです。

[bash]
pi@raspberrypi ~ $ sudo vi /etc/rsyslog.conf
###############
#### RULES ####
###############

#
# First some standard log files. Log by facility.
#
auth,authpriv.* /var/log/auth.log
*.*;auth,authpriv.none -/var/log/syslog
#cron.* /var/log/cron.log
#daemon.* -/var/log/daemon.log
#kern.* -/var/log/kern.log
#lpr.* -/var/log/lpr.log
#mail.* -/var/log/mail.log
#user.* -/var/log/user.log

#
# Logging for the mail system. Split it up so that
# it is easy to write scripts to parse these files.
#
#mail.info -/var/log/mail.info
#mail.warn -/var/log/mail.warn
#mail.err /var/log/mail.err

#
# Logging for INN news system.
#
#news.crit /var/log/news/news.crit
#news.err /var/log/news/news.err
#news.notice -/var/log/news/news.notice

#
# Some "catch-all" log files.
#
#*.=debug;\
# auth,authpriv.none;\
# news.none;mail.none -/var/log/debug
*.=info;*.=notice;*.=warn;\
auth,authpriv.none;\
cron,daemon.none;\
mail,news.none -/var/log/messages

#
# Emergencies are sent to everybody logged in.
#
*.emerg :omusrmsg:*

#
# I like to have messages displayed on the console, but only on a virtual
# console I usually leave idle.
#
#daemon,mail.*;\
# news.=crit;news.=err;news.=notice;\
# *.=debug;*.=info;\
# *.=notice;*.=warn /dev/tty8

# The named pipe /dev/xconsole is for the `xconsole' utility. To use it,
# you must invoke `xconsole' with the `-file' option:
#
# $ xconsole -file /dev/xconsole [...]
#
# NOTE: adjust the list below, or you'll go crazy if you have a reasonably
# busy site..
#
daemon.*;mail.*;\
news.err;\
*.=debug;*.=info;\
*.=notice;*.=warn |/dev/xconsole
[/bash]

一部のプログラムはディレクトリが無いとエラーになるため、OS起動時にディレクトリを作るようにします。

[bash]
pi@raspberrypi ~ $ sudo vi /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi

mkdir -p /var/log/ConsoleKit
mkdir -p /var/log/samba
mkdir -p /var/log/fsck
mkdir -p /var/log/apt
mkdir -p /var/log/ntpstats
chown root.ntp /var/log/ntpstats
chown root.adm /var/log/samba

touch /var/log/lastlog
touch /var/log/wtmp
touch /var/log/btmp
chown root.utmp /var/log/lastlog
chown root.utmp /var/log/wtmp
chown root.utmp /var/log/btmp

exit 0
[/bash]

再起動して、/var/logの下にログができていることを確認します。

[bash]
pi@raspberrypi ~ $ ls -la /var/log/
合計 92
drwxr-xr-x 7 root root 280 7月 12 00:01 .
drwxr-xr-x 11 root root 4096 5月 7 08:29 ..
drwxr-xr-x 2 root root 40 7月 12 00:01 ConsoleKit
drwxr-xr-x 2 root root 40 7月 12 00:01 apt
-rw-r----- 1 root adm 640 7月 12 00:02 auth.log
-rw-r--r-- 1 root utmp 0 7月 12 00:01 btmp
-rw-r--r-- 1 root adm 14615 7月 12 00:00 dmesg
drwxr-xr-x 2 root root 80 7月 12 00:01 fsck
-rw-r--r-- 1 root utmp 292292 7月 12 00:02 lastlog
-rw-r----- 1 root adm 22925 7月 12 00:01 messages
drwxr-xr-x 2 root ntp 40 7月 12 00:01 ntpstats
drwxr-xr-x 2 root adm 40 7月 12 00:01 samba
-rw-r----- 1 root adm 31211 7月 12 00:01 syslog
-rw-r--r-- 1 root utmp 6912 7月 12 00:02 wtmp
[/bash]

参考にさせていただいたページ

http://www.pc-links.com/blog/raspberrypi/ramdisk/

よく読まれている記事

-Raspberry Pi

© 2024 がちゃのーと。