Debian - Install a server on a Compact Flash memory

Contents[Hide]

Debian

This article will explain how to install Debian on a server using a Compact Flash memory card as a primary drive. All the above procedures are based on the Lenny distribution, but they should work on other Debian flavours and even for Ubuntu.

The first question to ask is : Why to do that ?

When you install such a server, after sometimes you realize that it has become a backbone in your house as it hosts :

  • a family web site
  • a music server for appliances like Roku Soundbridge
  • a file server for other PCs
  • a web based torrent manager & amule manager
  • an access filter proxy for the kids surf
  • an asterisk telephony exchange
  • ...

Then, you start to have nightmares about the fact that your server's hard disk may fail and that you will need to reinstall everything.

One simple, efficient & cheap solution is to replace your hard disk by a Compact Flash memory in association with an IDE to CF adapter. As CF memory have no mecanical parts, they are known to be much more reliable.

But, on the other side, they are known to have limited write cycles (between 100 000 and 1 million times).

IDE to Compact Flash adapter 4Gb compact flash card

One answer would de to install your system with a read-only filesystem. But it has lot of drawbacks, the main one being that any upgrade becomes a headache.

My prefered approach has been to tune a normal installation so that it minimizes at a maximum the write operations on the compact flash disk.

With a debian system, things are quite easy to do if you follow some simple steps. The main concept about it is to have enough RAM to place all the parts of the filesystem which are accessed regularly. Depending on the data handled, they can be loaded from the disk to RAM at startup and saved back from RAM to disk at shutdown time.

With the debian installation procedure described below, you will need :

  • A Compact Flash card of 2 Gb minimum.
  • 512 Mb of RAM minimum to be able to remove the swap & to handle the tmpfs filesystems.

As an example, to run all the services previously listed, my server uses 1,4 Gb of disk and its RAM usage is usually around 300 Mb.

All the following tweaks can be used with a system using SSD drives. It will also extend its life.

Install the system

Start a server netinstall from a CD-Rom.

When you have to define the partitions, select :

  • only one root partition formated with Ext2
  • no swap partition

We are using Ext2 as it is not a journalised filesystem, a better choice when you want to minimize the disk write operations.

Do not select any server profile, just install the minimum system. Later, we will install all the needed packages, and only them, with aptitude.

Once the system is installed, the first thing is to update the packages.

# apt-get  update
# apt-get  upgrade
# apt-get  install aptitude
# aptitude install deborphan apt-show-versions

1. Install RSync

RSync will be used to load and save all the filesystem portions which will be in RAM. It will be used as it is very fast, it minimises the data traffic and it preserves the ACL.

# aptitude install rsync

2. Tune the system for flash drive

By default, the kernel writes a new atime for each file that has been read, which generates one write for each read. This can be disabled by mounting the filesystem with the noatime option.

The /tmp & /var/tmp filesystem parts which are modified frequently by temporary files can be mounted as tmpfs filesystem.
These data don't need to be saved at shutdown time.

If you don't care about keeping your log files after a server reboot, the /var/log directory can also be mounted as a tmpfs filesystem.

As I don't use mail daemon, I also added /var/mail to the list of tmpfs filesystems.

So, finally, your /etc/fstab should look as follow :

/etc/fstab

/dev/hda1      /              ext2       noatime                     0     1
tmpfs          /tmp           tmpfs      defaults,noatime            0     0
tmpfs          /var/tmp       tmpfs      defaults,noatime            0     0
tmpfs          /var/log       tmpfs      defaults,noatime            0     0
tmpfs          /var/mail      tmpfs      defaults,noatime            0     0

The same way, we will set the /var/run and /var/lock directory in RAM. To do that, you must modify /etc/default/rcS to get :

/etc/default/rcS

RAMRUN=yes
RAMLOCK=yes

In addition, syslogd likes to write lines to log files every 20 minutes to show that syslog is still running.
This can be disabled by changing /etc/default/syslogd so that it reads :

/etc/default/syslogd

SYSLOGD="-m 0"

We will also create a symlink to replace /etc/mtab

# rm /etc/mtab
# ln –s /proc/mounts /etc/mtab

3. Tune the kernel behavior

The kernel also has some settings to tweak the way it caches and writes to the disks. A number of files under /proc/sys/vm controls how this works:

  • /proc/sys/vm/laptop_mode
    How many seconds after a read should a writeout of changed files start (this is based on the assumption that a read will cause an otherwise spun down disk to spin up again). If you don’t need to write dirty data after every read, you sould disable it by setting it to 0.
  • /proc/sys/vm/dirty_writeback_centisecs
    How often the kernel should check if there is "dirty" (changed) data to write out to disk (in centiseconds).
  • /proc/sys/vm/dirty_expire_centisecs
    How old "dirty" data should be before the kernel considers it old enough to be written to disk. It is in general a good idea to set this to the same value as dirty_writeback_centisecs above.
  • /proc/sys/vm/dirty_ratio
    The maximum amount of memory (in percent) to be used to store dirty data before the process that generates the data will be forced to write it out. Setting this to a high value should not be a problem as writeouts will also occur if the system is low on memory.
  • /proc/sys/vm/dirty_background_ratio
    The lower amount of memory (in percent) where a writeout of dirty data to disk is allowed to stop. This should be quite a bit lower than the above dirty_ratio to allow the kernel to write out chunks of dirty data in one go.

All these parameters should be modified in the /etc/sysctl.conf file to be taken into account a every boot :

/etc/sysctl.conf

# specific tuning for flash drive
vm.swappiness = 0
vm.laptop_mode = 0
vm.dirty_writeback_centisecs = 12000
vm.dirty_expire_centisecs = 12000
vm.dirty_ratio = 10
vm.dirty_background_ratio = 1

4. Disable the Bash history

By default, Bash logs every command that you type. You can stop that behaviour by adding these lines to the file /root/.bashrc :

/root/.bashrc

#stop bash history log
unset HISTFILE
unset HISTFILESIZE
unset HISTSIZE

5. Setup the backup directory structure

To be able to load at startup and save at shutdown tmpfs filesystems which need to be backed-up, I created a specific directory /permanent to receive all these data.

# mkdir /permanent
# chmod 777 /permanent

6. Daily cleaning of /tmp

We configured most of the services (php, ...) to generate temporary data in /tmp.

As the server won't reboot every day, we have to setup a small cleaning script to be executed everyday, that keeps only the 4 lastly created files.

It should be placed in /etc/cron.daily/clean-tmp

/etc/cron.daily/clean-tmp

#!/bin/sh
# Cleanup the /tmp directory and keep the 4 last files only
#
# 01/09/2008 - Creation by N. Bernaerts
# 13/07/2012 - No error if /tmp holds less than 4 files (thanks to Konstantin Laufer)
# 14/07/2012 - Properly handle filenames with specific caracters like '(' and ')'

ls /tmp/test/* -t1 | sed '1,4d' | sed 's/\(.*\)/"\1"/g' | xargs rm -f $$.dummy

Don't forget to give execution permission to the script :

# chmod +x /etc/cron.daily/clean-tmp

7. Setup APT environment

As /var/log is placed in tmpfs, we will need to create at every startup some log structure needed by apt.

These operations will be done by a specific script /etc/init.d/apt-tmpfs

/etc/init.d/apt-tmpfs

#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          apt-tmpfs
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Create /var/log/apt on tmpfs at startup
# Description:       Create /var/log/apt needed by APT.
### END INIT INFO
#
# main()
#
case "${1:-''}" in
  'start')
   # create the /var/log/apt needed by apt
   mkdir /var/log/apt
   chmod 777 /var/log/apt
   ;;
  'stop')
   ;;
  'restart')
   ;;
  'reload'|'force-reload')
   ;;
  'status')
   ;;
  *)
   echo "Usage: $SELF start"
   exit 1
   ;;
esac

Then, make it executable and declare it for startup (S90) & shutdown (K10)

# chmod +x /etc/init.d/apt-tmpfs
# update-rc.d apt-tmpfs defaults 90 10

You should now have a fully functionnal debian server optimized to run from a Compact Flash drive.

As your root filesystem is not read-only, you can install any package you need with aptitude.

Another article explains How to tune Apache/PHP/MySQL to run on a Compact Flash system

Stay tuned ...

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus