Creating a RAM disk

 

Information

Create a RAM disk and keep all the frequently accessed log files, ntp.drift and samba's .tdb files there thus reducing disk access.

If you dont't know how a RAM disk works or what is is, I recommend reading the wikipedia article before proceeding.

You're probably wondering, "weren't we supposed to free some memory instead of using it more?" Well that is up to you... my RAM disk eats one megabyte of memory, and it's half empty, so one could probably do with an even smaller ram disk.

Prerequisites

Enabled SSH access.

A text editor and the know-how to use it. I recommend installing nano, but vi will also do just fine.

The how-to

  1. Again, if you don't know your Linux, you might screw up your MBWE big time. so proceed with caution.

    The basic idea is to create a small RAM disk, configure the programs to use it and copy the data on it back to the disk when the disk is spinning.

    In this example, we'll be moving just ntp.drift and samba's files to the RAM disk. ntp.drift, because it is written frequently (if you have ntpd running) and samba, because it spins my MBWE up every time I start my Windows machine up and it mounts the samba share.

    Of course, you could have disabled ntpd and might not have problem with samba spinning the disks up, but you may set up other programs to use the RAM disk, too. From this guide you'll get the basic idea how to do it, though.

  2. Make sure you are in superuser mode.

    # su
  3. First, we'll create a RAM disk manually. Later on, you can do this with a startup script automatically, but let's just do it manually for practise :)

    By default, the busybox linux used in the MBWE has five RAM disk devices, that are by default inactive and not in use. You can list them with

    # ls -l /dev/ram*
  4. Let's format a filesystem to ram1:

    # /sbin/mke2fs -vm 0 /dev/ram1 1024

    The number 1024 defines the size of the RAM disk in kilobytes. One megabyte should be more than enough.

  5. Next, we'll have to mount the disk. We will mount the ramdisk to /mnt/ramdisk/. You may use another path, but then you'll have to remember to configure the programs accordingly.

    # mkdir /mnt/ramdisk
    # /bin/mount -t ext2 /dev/ram1 /mnt/ramdisk/

    And behold, we have a working RAM disk.

  6. Then, let's configure ntpd to keep the drift file on the RAM disk by editing /etc/ntp.conf. The drift file contains the estimated clock frequency error and it is updated whenever the ntpd checks the time from the time server.

    Find the line beginning with driftfile and change it to

    driftfile /mnt/ramdisk/ntp.drift

    Then copy the driftfile to the RAM disk and restart ntpd:

    # cp /etc/ntp.drift /mnt/ramdisk
    # /etc/init.d/ntp.sh restart
  7. Next, configure samba to use the ramdisk.

    Note: if you plan to use the MBWE Web GUI to manipulate your users and shares after you change your samba file paths, you'll have to change the file locations in /usr/www/lib/nasCommon.pm file also. Otherwise, the Web GUI will be editing wrong files thus causing weird problems.

    Edit /var/oxsemi/smb.conf and change the following lines:

    smb passwd file=/mnt/ramdisk/private/smbpasswd
    private dir=/mnt/ramdisk/private
    log file=/mnt/ramdisk/log.%m
    lock directory=/mnt/ramdisk/locks
    pid directory=/mnt/ramdisk/locks
    

    Edit /etc/init.d/samba.sh and set the log directory to the RAM disk by editing the line in the start function:

    /usr/local/samba/sbin/nmbd -D -s/var/oxsemi/smb.conf -l/mnt/ramdisk

    Finally edit /etc/inetd.conf and change the log directory there, too :

    netbios-ssn stream tcp nowait root /usr/local/samba/sbin/smbd smbd -s/var/oxsemi/smb.conf -l/mnt/ramdisk -d0

    Next, copy the files to the RAM disk:

    # mkdir /mnt/ramdisk/locks
    # cp /var/locks/* /mnt/ramdisk/locks/
    # mkdir /mnt/ramdisk/private
    # cp /var/private/* /mnt/ramdisk/private/
    # cp /var/log/log.* /mnt/ramdisk/

    Then, restart samba:

    # /etc/init.d/samba.sh restart
  8. If everything went well, and you got no errors, samba and ntpd are now using the RAM disk.

  9. In case you didn't disable syslogd in the previous tutorial (reducing disk usage), and want it to output it's log on the RAM disk too, edit the file /etc/inittab:

    Change this line:

    ::respawn:/sbin/syslogd -n -m 0

    Into this:

    ::respawn:/sbin/syslogd -n -m 0 -O /mnt/ramdisk/messages

    You could also want to add switches -s [size in kb] and -b [number of rotated logs to keep], depending on your RAM disk size to prevent the log from filling up the disk. I have mine set at -s 100 -b 1.

  10. Now, this process should be made to happen automatically during boot. You can either roll your own startup script or just use mine:

    # wget http://kyyhkynen.net/stuff/mybook/S13ramdisk
    # mv S13ramdisk /etc/init.d	
    # chmod 0700 /etc/init.d/S13ramdisk
    # chown root:root /etc/init.d/S13ramdisk
    # ln -s /etc/init.d/S13ramdisk /etc/init.d/K71ramdisk

    The script looks like this:

    #!/bin/sh
    #
    
    case "$1" in
        start)
    	/sbin/mke2fs -vm 0 /dev/ram1 1024
    	/bin/mount -t ext2 /dev/ram1 /mnt/ramdisk/
    	
    	cp /etc/ntp.drift /mnt/ramdisk/
    	cp /var/log/log.* /mnt/ramdisk/
    	mkdir /mnt/ramdisk/locks
    	cp /var/locks/* /mnt/ramdisk/locks/
    	mkdir /mnt/ramdisk/private
    	cp /var/private/* /mnt/ramdisk/private/
    	
    	;;
    
        backup)
        	cp /mnt/ramdisk/ntp.drift /etc/
        	cp /mnt/ramdisk/locks/* /var/locks/
    	cp /mnt/ramdisk/log.* /var/log/
    	cp /mnt/ramdisk/private/* /var/private/	
    	;;
    
        stop)
    	$0 backup
    	;;
    
        restart|reload)
    	$0 backup
    	;;
    
        *)
    	echo $"Usage: $0 {start|stop|restart|backup}"
    	exit 1
    esac
    
    exit $?
    

    As you an see, with parameter start, the RAM disk is created and the needed files are copied to it. With parameter backup the files are copied back to the disk.

    Note: the order the RAM disk script is run during the startup is important; the RAM disk must be created before any services using it are started.

    Because all the data on the RAM disk is lost in case of a power failure, you probably should copy the files on the RAM disk back to the disk more often than just during a shut down.

    This is achieved easiest by pointing the EXECUTE_ON_SPINUP variable in the smart_spindown script to another shell script (the variable is set to /usr/sbin/smart_spindown_onspinup by default) with the following line in it:

    /etc/init.d/S13ramdisk backup

    This way, the RAM disk is copied back to the disk every time the disk has spun up, thus keeping the files pretty much up to date.

    See the smart spindown guide for further information.

     

    Note: if you have syslogd running and outputting into the RAM disk and don't want to reset its log file on each boot, you should add it into the backup process too. (it is not included by default since I want it to reset)

  11. Of course, if you have installed other non-default services on your MBWE, you might want to check the logging settings for those also. And if you know for certain that you definitely are not going to evr open a log file for some application, you might as well point the log to /dev/null or disable the logging otherwise.

  12. Remember to check your RAM disk usage from time to time with

    # df -k

    If your RAM disk gets full, your programs using it might stop working.