Flash

From www.chip-community.org
Jump to: navigation, search

CHIP uses flash memory as a solid-state disc replacement. It is based on NAND technology.


Extend life

All flash memory has a limited lifetime. Flash is best used in a "read frequently, write rarely" model because repeated writing tends to "wear out" the memory. (Also, writing speed is much slower than reading speed.)

Here are some steps to extend the life of CHIP's flash by minimizing writes to it.

Create ramdisk for application logging

Thanks to MEitelwein's project for this idea.

Many applications you might run on CHIP generate logs of their activity. For example, web servers log every access to the server. If you have a particularly "chatty" application, you can arrange to have its logs written to a "ram disk", which is kept in system RAM instead of writing it to flash. Be aware however that if CHIP reboots, the contents of your ramdisk file system is lost forever. (Although you could write a shutdown script which backs up the contents to flash prior to shutdown.)

Here's how to create a "ram disk" file system:

sudo mkdir /ramdisk
sudo vim /etc/fstab  # or use your own favorite editor, maybe pico.

In the editor, add the following line to the end of the file:

tmpfs   /ramdisk   tmpfs  defaults  0  1

Write the file, exit the editor, and enable the file system by entering:

sudo mount -a

Check to make sure it worked:

df

Included in the list should be the line:

tmpfs             253552      0    253552   0% /ramdisk

(Some of your numbers might be different.)

Since the file system is empty, both now and at boot time, you probably want to write a script to populate it with properly-owned directories. For example, to redirect apache's logs:

#!/bin/sh
mkdir /ramdisk/apache_log
chown root:adm /ramdisk/apache_log
rm -rf /var/log/apache2
ln -s /ramdisk/apache_log /var/log/apache2

This script can be included in the systemd framework as a one-shot unit, and apache can be set to depend upon it.

Finally, remember that files on ramdisk are limited in size. If they grow too large, Unix will start to use swapspace, with both slows the system way down and also defeats the purpose since it writes to flash. So keep the files small by periodically cleaning them up. (e.g. with apache, see this post.)


Minimize syslog chattiness

rsyslogd-2007: action 'action 17' suspended

Due to a bug in Linux syslog, CHIP's /var/log/messages file will sometime start filling up with once-per-minute messages of the form (timestamps omitted):

chip rsyslogd-2007: action 'action 17' suspended, next retry is ... [try http://www.rsyslog.com/e/2007 ]

I followed these instructions:

sudo vi /etc/rsyslog.conf   # or any other editor, like nano.

Near the bottom should be the lines:

daemon.*;mail.*;\
	news.err;\
	*.=debug;*.=info;\
	*.=notice;*.=warn	|/dev/xconsole

Comment these lines out by putting a "#" in front of each line:

#daemon.*;mail.*;\
#	news.err;\
#	*.=debug;*.=info;\
#	*.=notice;*.=warn	|/dev/xconsole

Write the file, exit the editor, and enter:

sudo service rsyslog restart

I haven't seen the message since.


Login/logout

By default, Linux writes a number of log messages to /var/log/daemon.log every time you log into CHIP and log back out. Here's an example of one of the messages:

chip systemd[1]: Starting user-1000.slice.

(timestamp omitted) There are other messages associated with it.

According to this, you can disable these message by entering:

sudo loginctl enable-linger chip

It doesn't eliminate all message logging, but it greatly reduces it. And by the way, the command appears to be "sticky". I.e. it is remembered after a reboot. So there is no need to re-execute the command with each reboot.


Read-Only Root Filesystem

Steps below (tested on headless 4.4 chip with Debian Jessie) will reconfigure root filesystem to be read-only @ boot. Inspired by Raspberry pi tutorials: https://hallard.me/raspberry-pi-read-only/ http://petr.io/en/blog/2015/11/09/read-only-raspberry-pi-with-jessie/

Setup apps

apt-get remove cron logrotate -y --force-yes
apt-get install busybox-syslogd -y --force-yes

mount /tmp as tmpfs

systemctl enable tmp.mount

Setup /var

rm -r /var/run /var/spool /var/lock /var/log /var/tmp
echo "D /tmp/var/run 1777 root root -" > /etc/tmpfiles.d/rovar.conf
echo "D /tmp/var/spool 1777 root root -" >> /etc/tmpfiles.d/rovar.conf
echo "D /tmp/var/lock 1777 root root -" >> /etc/tmpfiles.d/rovar.conf
echo "D /tmp/var/log 1777 root root -" >> /etc/tmpfiles.d/rovar.conf
echo "D /tmp/var/tmp 1777 root root -" >> /etc/tmpfiles.d/rovar.conf
echo "D /tmp/var/lib/dhcp 1777 root root -" >> /etc/tmpfiles.d/rovar.conf
echo "D /tmp/var/lib/NetworkManager 1777 root root -" >> /etc/tmpfiles.d/rovar.conf
echo "D /tmp/var/lib/systemd/rfkill 1777 root root -" >> /etc/tmpfiles.d/rovar.conf
ln -s /tmp/var/run /var/run
ln -s /tmp/var/spool /var/spool
ln -s /tmp/var/lock /var/lock
ln -s /tmp/var/log /var/log
ln -s /tmp/var/tmp /var/tmp
rm -r /var/lib/dhcp
ln -s /tmp/var/lib/dhcp /var/lib/dhcp
rm -r /var/lib/NetworkManager
ln -s /tmp/var/lib/NetworkManager /var/lib/NetworkManager
rm -r /var/lib/systemd/rfkill
ln -s /tmp/var/lib/systemd/rfkill /var/lib/systemd/rfkill

Deal with special files

rm /var/lib/systemd/random-seed
ln -s /tmp/random-seed /var/lib/systemd/random-seed

touch /tmp/dhcpcd.resolv.conf
rm /etc/resolv.conf
ln -s /tmp/dhcpcd.resolv.conf /etc/resolv.conf

echo "f   /tmp/random-seed -    -    -    -  -" > /etc/tmpfiles.d/ro.conf
echo "f   /tmp/dhcpcd.resolv.conf -    -    -    -  -" >> /etc/tmpfiles.d/ro.conf

fix random-seed dependency

sed -i '/^After=/ s/$/ systemd-tmpfiles-setup.service/' /lib/systemd/system/systemd-random-seed.service

NTP daemon driftfile location

sed -i  's/\(driftfile \)\(.*\)/\1\/tmp\/ntp.drift/'  /etc/ntp.conf

UBI watchdog -> write stats to writable location

sed -i 's/var\/cache/tmp/g' /etc/systemd/system/ubihealthd.service

Finally, switch to Read-Only filesystem

echo "/dev/mtdblock3 0 0x400000 0x4000" | sudo tee -a /etc/fw_env.config
fw_setenv bootargs root=ubi0:rootfs rootfstype=ubifs ro fastboot noswap earlyprintk ubi.mtd=4

Reboot and enjoy!