| Debian - Install a server on a Compact Flash memory |
|
|
|
| Écrit par Nicolas Bernaerts | |
| Lundi, 08 Septembre 2008 00:00 | |
|
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 :
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).
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 :
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. Install the systemStart a server netinstall from a CD-Rom. When you have to define the partitions, select :
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 Install RSyncRSync 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 Tune the system for flash driveBy 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 Tune the kernel behaviorThe 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:
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 Disable the Bash historyBy 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 Setup the backup directory structureTo 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 Daily cleaning of /tmpI configured most of the services (php, ...) to generate temporary data in /tmp. As the server won't reboot every day, I've created a small cleaning script to be executed everyday, that keeps only the 4 lastly created files. Place it in /etc/cron.daily/clean-tmp : /etc/cron.daily/clean-tmp #!/bin/sh # Cleanup the /tmp directory and keep the 4 last files only ls /tmp/* -t1 | sed '1,4d' | xargs rm Don't forget to give execution permission to the script : # chmod +x /etc/cron.daily/clean-tmp Setup APT environmentAs /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 # 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. Stay tuned ...
|



