view · edit · print · history

Diversion Script Information

Diversion scripts are used to start packages, set variables or function definitions at the time of the NSLU2 boot. They "divert" the normal boot scripts to perform the needed action(s) and then can either "return 1" to continue normal factory script progress or "return 0" to abort the factory script.

The normal boot scripts check for the presence of Diversion scripts in /unslung, however, it's not quite as simple as it sounds. During the boot process, if external storage is detected and contains the unslung device, the root filesystem will be changed over to the external storage. /unslung will now contain the scripts stored on the external device (harddrive or flash). If no external storage device is attached, then /unslung will continue to have the scripts that are stored in the internal flash.

Therefore, it is important that no scripts that are stored on the internal flash require any programs or functionality stored on external storage. So, how do you tell which is which? The login message when you log in via ssh or telnet tells you whether you are running on internal flash or not, and the "Home" page on the web interface has the same information at the bottom. If you are running on external storage, /unslung will be on the external storage, and when not running from disk, /unslung will be the internal flash.

Additionally, you can access the internal flash when running from external storage, from /initrd/unslung/. You can also access your flash or harddrive's /unslung if you booted to internal flash but later attached harddrive, the location will be either /share/hdd/data/unslung (hard drive) or /share/flash/data/unslung (flash). This comes in handy if you make a mistake with your scripts.

If you always want a diversion script to run - independent of what disks/flash drives are plugged in, then put the diversion script into both locations. WARNING: if you make a bad mistake with a diversion script on the internal flash, you may have to reflash to fix the error.

Note: In the old README and older versions of Unslung (anything prior to 3.16), /unslung was a symlinked directory to the /share/hdd/conf/unslung. With unslung 3.16 and later, that symlink DOES NOT need to exist (and should not exist) as any diversion script located in the pluggable locations will be run IF the hard drive or flash unit is plugged in and recognized at boot time.

You may divert as many or as few scripts as you like. Simply add the name of the standard rc script into the appropriate /unslung directory and it will be run.

Diversion Script Programming

Example: I have a script /unslung/rc.local:

  #! /bin/sh
  return 1

That will run at the beginning of the normal /etc/rc.d/rc.local, and then the rest of the factory rc.local will be executed. If I do NOT want to run the factory rc.local, my script would be:

  #! /bin/sh
  return 0

That is, if the diversion script returns with something other than 0, it will run the rest of the factory script.

Note that any variable definitions or function declarations are allowed to happen before the diversion script is called. This allows you to use the variables and functions defined by the factory script.

Specific Diversion Script Examples

[Append further script examples to bottom of page]

Enable Telnet On Boot diversion script (/unslung/rc.xinetd):

#! /bin/sh
if ( [ ! -f /etc/inetd.conf ] || !(grep telnetd /etc/inetd.conf -q) ) then
echo "telnet stream tcp nowait root /usr/sbin/telnetd" >> /etc/inetd.conf
return 1

Modify the Samba Config file - a working example diversion script (/unslung/rc.samba):

The default smb.conf file contains an erroneous entry username map = no which can safely be removed. The only effect of leaving this erroneus entry is regular error messages complaining about this line logged to the /var/log/samba/log.smbd file. This example makes use of the script text editor - sed - to 'search and delete' the erroneous line. Sed is also very useful for 'search and replace' and 'search and insert' operations.

#! /bin/sh
# Diversion script: to modify the Samba smb.conf file
# First - make sure we have a backup copy of /etc/samba/smb.conf
if [ ! -e /etc/samba/smb.conf.orig ]; then 
cp -p /etc/samba/smb.conf  /etc/samba/smb.conf.orig 
# Script section to delete the faulty username map line
sed -i -e '/username map \= no/d' /etc/samba/smb.conf
return 1

NOTE: You shouldn't use this script or remove this line from the file if you plan to implement Samba user authentication and map Windows usernames to Unix usernames. In that case, you will need the username map file (maybe like '/etc/samba/smbusers'). See http://www.samba.org for documentation of usermap.

view · edit · print · history · Last edited by ShadowJK.
Based on work by BrianZhou, AdamG, gothick, Bob Quinn rcqsocketscom, Peter J Holmes, rcsuk, radale, Dale Hanych, RobHam, and ByronT.
Originally by ByronT.
Page last modified on December 23, 2007, at 09:28 AM