絖綛 N@i.jp  昨日:00021300
 今日:00015367
 総計:00054024
keywords
管理者専用
  Post   Add link   Control Panel 































新しいトピック
最新:03/19 10:05


新しいコメント
最新:07/28 16:47






管理人へMAIL

プライバシーポリシー

Raspberry Pi4(Raspbian Buster)のセットアップ(続き)

Raspberry Piではお馴染の MicroSDカード延命措置


 Raspberry Piではお馴染の MicroSDカード延命措置を行いました。

1. swap無効化

 swapとは、メモリが足りなくなった際に使用されていないメモリの内容をswapファイルに書き出して(スワップアウト)空きメモリを作り、必要になったらswapファイルから読み込んでメモリに配置(スワップイン)することで、物理メモリサイズ以上のメモリ容量を使用できるようにする仕組みです。
 少ないメモリでも多くのプログラムを動作させることができる反面、メモリが足りなくなってswapが発生するとファイル入出力が行われるので、その際には大きく速度低下してしまうし、Raspberry Piの場合 swapファイルも MicroSDカード上に置かれるので、swapファイルの書き換えによって MicroSDの寿命を縮めてしまう可能性があります。
 そのためスワップを無効化して物理メモリの範囲内で動作させることで、MicroSDカードを延命させようという考えです。Raspberry Pi4には4GBものメモリが搭載されましたから、スワップなしでも十分でしょう。

 Raspbian Busterが起動しただけの状態ですが、メモリの使用状況はこんな感じ。

# free -h
              total        used        free      shared  buff/cache   available
Mem:          3.8Gi       100Mi       3.4Gi       8.0Mi       332Mi       3.6Gi
Swap:          99Mi          0B        99Mi

 メモリが4GBもあるので、当然ながらswapは全然使われていませんね。では、スワップを停止させます。作業はrootユーザで行います。

# swapoff --all
# free -h
              total        used        free      shared  buff/cache   available
Mem:          3.8Gi       100Mi       3.4Gi       8.0Mi       332Mi       3.6Gi
Swap:            0B          0B          0B

 これでswapが使われなくなりましたが、再起動するとswapが復活してしまいます。恒久的にswapを無効化するには dphys-swapfileの起動を停止させる必要があります。

# systemctl stop dphys-swapfile
# systemctl disable dphys-swapfile
Synchronizing state of dphys-swapfile.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable dphys-swapfile
Removed /etc/systemd/system/multi-user.target.wants/dphys-swapfile.service.
# systemctl status dphys-swapfile
● dphys-swapfile.service - dphys-swapfile - set up, mount/unmount, and delete a swap file
   Loaded: loaded (/lib/systemd/system/dphys-swapfile.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:dphys-swapfile(8)
# reboot

2. 頻繁に書き換えられる場所をRAMディスク(tmpfs)化

 頻繁な書き換えが行われる場所をRAMディスク(tmpfs)にして MicroSDへの書き込みを無くすため、"/etc/fstab"を書き換えます。RAMディスク(tmpfs)化する場所は以下です。

  • /tmp
  • /var/tmp
  • /var/log
# cp -p /etc/fstab /etc/fstab.org  ※元のファイルをバックアップしておきます
# vi /etc/fstab

 書き加えたのは以下の赤字の部分。

PARTUUID=5e3da3da-01  /boot           vfat    defaults          0       2
PARTUUID=5e3da3da-02  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that
#
# tmp files put on RAM disk
tmpfs   /tmp        tmpfs   defaults,size=32m,noatime,mode=1777  0       0
tmpfs   /var/tmp    tmpfs   defaults,size=16m,noatime,mode=1777  0       0
#
# Log files write on RAM disk
tmpfs   /var/log    tmpfs   defaults,size=32m,noatime,mode=0755  0       0

 RAMディスク(tmpfs)にするディレクトリ配下のものは用がないので削除してしまいますが、"/var/log"配下にディレクトリやファイルが無いとブート時に起動が失敗するDaemonもあるので、どんなディレクトリやファイルがあるか記録しておき、まだ"/var/log/"配下はお掃除しないようにします。

# cd /tmp
# rm -rf *
# cd /var/tmp
# rm -rf *
# cd /var/log
# ls -la
合計 1724
drwxr-xr-x  6 root root   4096 12月  7 16:54 .
drwxr-xr-x 11 root root   4096 12月  6 13:38 ..
-rw-r--r--  1 root root   6454 12月  7 16:54 Xorg.0.log
-rw-r--r--  1 root root   6454 12月  7 16:54 Xorg.0.log.old
-rw-r--r--  1 root root   1304 12月  6 14:26 alternatives.log
drwxr-xr-x  2 root root   4096 12月  7 16:46 apt
-rw-r-----  1 root adm   24345 12月  7 16:55 auth.log
-rw-------  1 root root  47089 12月  7 16:54 boot.log
-rw-r--r--  1 root root      0  9月 26 09:46 bootstrap.log
-rw-rw----  1 root utmp    384 12月  6 13:38 btmp
-rw-r-----  1 root adm  279264 12月  7 16:55 daemon.log
-rw-r-----  1 root adm   26663 12月  7 16:54 debug
-rw-r--r--  1 root root 136778 12月  7 16:46 dpkg.log
-rw-r--r--  1 root root  24048 12月  7 16:18 faillog
drwxr-xr-x  2 root root   4096  2月  8  2019 firebird
-rw-r--r--  1 root root   5503 12月  6 15:37 fontconfig.log
-rw-r-----  1 root adm  254818 12月  7 16:54 kern.log
-rw-rw-r--  1 root utmp 292584 12月  7 16:54 lastlog
drwx--x--x  2 root root   4096 12月  7 16:54 lightdm
-rw-r-----  1 root adm  254135 12月  7 16:54 messages
drwx------  2 root root   4096  9月 26 09:46 private
-rw-r-----  1 root adm   65089 12月  7 16:55 syslog
-rw-r-----  1 root adm  488355 12月  7 16:13 syslog.1
-rw-r-----  1 root adm   14145 12月  7 16:54 user.log
-rw-rw-r--  1 root utmp  28800 12月  7 16:54 wtmp
-rw-r-----  1 root adm    3661 12月  7 16:54 xrdp-sesman.log
-rw-r-----  1 xrdp adm   14897 12月  7 16:54 xrdp.log

3. /var/log配下にディレクトリ、ファイルが無いと困るDaemon用に、ブート時に作成するスクリプトを作る

 今のところ"/var/log/apt/", "/var/log/lastlog", "/var/log/btmp", "/var/log/wtmp", "/var/log/xrdp.log"くらいを作っておけば良いかな?今後新しいものをインストールしたら、その都度確認して増やしていく必要があるのが少々面倒だけど。スクリプトは "/etc/init.d/"配下に "make-varlog"という名前で作成しました。

# cd /etc/init.d
# vi make-varlog
# chmod +x make-varlog 
#!/bin/sh
### BEGIN INIT INFO
# Provides:          setup-tempfs-logs
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Setup tempfs log files
### END INIT INFO
case "$1" in
    'start')
    ##########################################################
    # prepare /var/log subdir for ramdisk init on every boot #
    ##########################################################
    mkdir -p /var/log/apt
    chown root.root /var/log/apt

    #
    # Create log file
    #
    touch /var/log/lastlog
    touch /var/log/wtmp
    touch /var/log/btmp
    touch /var/log/xrdp.log

    chown root.utmp /var/log/lastlog
    chown root.utmp /var/log/wtmp
    chown root.utmp /var/log/btmp
    chown xrdp.adm  /var/log/xrdp.log

    chmod 664 /var/log/lastlog
    chmod 660 /var/log/btmp
    chmod 664 /var/log/wtmp
    chmod 640 /var/log/xrdp.log
    ;;
    'stop')
    ;;
    'restart')
    ;;
    'reload'|'force-reload')
    ;;
    'status')
    ;;
    *)
    echo "Usage: $0 start"
    exit 1
    ;;
esac

exit 0

 上記以外には"/var/log/fsck", "/var/log/ntpstats"などを作っている事例がありますね。
 作成した"make-varlog"スクリプトを配置します。

# update-rc.d make-varlog defaults
# ls -l /etc/rc*.d/*make-varlog
lrwxrwxrwx 1 root root 21 12月  7 17:39 /etc/rc0.d/K01make-varlog -> ../init.d/make-varlog
lrwxrwxrwx 1 root root 21 12月  7 17:39 /etc/rc1.d/K01make-varlog -> ../init.d/make-varlog
lrwxrwxrwx 1 root root 21 12月  7 17:39 /etc/rc2.d/S01make-varlog -> ../init.d/make-varlog
lrwxrwxrwx 1 root root 21 12月  7 17:39 /etc/rc3.d/S01make-varlog -> ../init.d/make-varlog
lrwxrwxrwx 1 root root 21 12月  7 17:39 /etc/rc4.d/S01make-varlog -> ../init.d/make-varlog
lrwxrwxrwx 1 root root 21 12月  7 17:39 /etc/rc5.d/S01make-varlog -> ../init.d/make-varlog
lrwxrwxrwx 1 root root 21 12月  7 17:39 /etc/rc6.d/K01make-varlog -> ../init.d/make-varlog

4. ログ出力を減らすため設定を変更

 ログの出力先(/var/log)の容量が少なくなったので、極力ログを吐かせようにします。設定は"/etc/rsyslog.conf"ファイルです。

# cp -p rsyslog.conf rsyslog.conf.org  ※元のファイルをバックアップしておきます
# vi /etc/rsyslog.conf
# /etc/rsyslog.conf configuration file for rsyslog
#
# For more information install rsyslog-doc and see
# /usr/share/doc/rsyslog-doc/html/configuration/index.html
〜略〜
###############
#### 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

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

5. ログを全部消して再起動

 これで準備が整ったので、ログを全部消して再起動します。

# cd /var/log
# rm -rf *
# reboot


< 過去の記事 [ 12月の 全てのカテゴリ リスト ] 新しい記事 >

2019 calendar
12月
1234567
891011121314
15161718192021
22232425262728
293031


掲示板
最新:08/15 17:19


GsBlog was developed by GUSTAV, Copyright(C) 2003, Web Application Factory All Rights Reserved.