Monday, July 4, 2011

Software Installation on DD-WRT

One great thing about this router is its loaded hardware specifications: 400MHz processor, 32MB flash ROM, 64MB RAM and most important of all, a USB port. All these make the router a very versatile little Linux box with tons of capabilities. 

However, a confusing thing that inexperienced users may not know, the CPU in the router is MIPS (MIPS in big-endian), while the popular Broadcom routers(such as Linksys WRT54G) are MIPSEL (MIPS in little-endian). I don’t want to go into technical detail here, but just remember, even though both are MIPS, they’re totally different and software compiled for Broadcom routers WILL NOT work on this and other Atheros based devices.

Thanks to the OpenWrt guys, with some effort, we can now use the OpenWRT's optware software installer.

Here we come to another downfall of DD-WRT. OpenWRT will allow you to edit the on-board flash ROM and thus install anything you want directly to your router. DD-WRT doesn't provide this type of access, so we have to install programs to an external USB device. The previous blog poster claims that the USB is faster than the internal flash. This may be true for very high-speed drives, but I'd like to see numbers before also making that claim.

Let's get started & keep in mind that this set of instructions is for DD-WRT ONLY as it is not needed for OpenWRT.
  1. Prepare the USB disk
    You must format your USB device to ext2 or ext3 (recommended) before plugging into the router. Any Linux live CD should do the trick and I personally recommend GParted, which can be installed in CD or USB disk, and is also available on Ubuntu live CDs.

  2. Set up DD-WRT accordingly
    A
    . Enable SSH access.
         Under Services > Service > Secure Shell.
         * Enable SSHd.
         * Click “Apply Settings”.

    ssh-enable

    B. Set USB Support
         Under Services > USB.
         * Enable Core USB Support
         * Disable USB 1.1 Support (UHCI)
         * Disable USB 1.1 Support (OHCI)
         * Enable USB 2.0 Support
         * Disable USB Printer Support (you can enable it if you want)
         * Enable USB Storage Support
         * Enable Automatic Drive Mount
         * Set Disk Mount Point to /mnt     * Click “Apply Settings”.

    C. Disable the jffs partition
         Under Administration > Management
         * Disable “JFFS2 Support”.
         * Click “Apply Settings”.

  3. Now plug in the USB drive and reboot the router.

  4. After rebooting, login the router with PuTTY.
    Fill in router’s IP (192.168.1.1 in most cases), leave everything else on default (port 22 and Connection type SSH) and click “Open”.
    The login username is always root and password is the web password that you set.

  5. Now create and prepare the necessary folders
    Type the commands below in PuTTY

    cd /mnt
    mkdir etc opt root
    touch optware.enable
    chmod 755 etc opt root
    mkdir opt/lib
    chmod 755 opt/lib
    cp -a /etc/* /mnt/etc/
    mount -o bind /mnt/etc /etc
    mount -o bind /mnt/opt /jffs

    So now we have writable /etc and /jffs folders, but only while they both are on the USB drive. All other folders -- especially /bin, /usr and /sbin – will be left untouched to keep the change minimal. The /etc folder will also be handled with extra caution.

  6. Install the required libraries for the router and opkg 
    Run:
    cd /tmp
    wget http://http://downloads.openwrt.org/backfire/10.03.1/ar71xx/packages/libc_0.9.30.1-43.10_ar71xx.ipk
    wget http://downloads.openwrt.org/backfire/10.03.1/ar71xx/packages/opkg_576-1_ar71xx.ipk
    ipkg install libc_0.9.30.1-43.10_ar71xx.ipk opkg_576-1_ar71xx.ipk


    Don't worry if you get a string of errors like below, it is fine as long as the last 2 lines are present:

    ERROR: File not found: //usr/local/lib/ipkg/lists/whiterussian
    You probably want to run `ipkg update'
    ERROR: File not found: //usr/local/lib/ipkg/lists/non-free
    You probably want to run `ipkg update'
    ERROR: File not found: //usr/local/lib/ipkg/lists/backports
    You probably want to run `ipkg update'
    /bin/ipkg: line 1184: sort: not found
    Unpacking libc...Done.
    Configuring libc...Done.
    ERROR: File not found: //usr/local/lib/ipkg/lists/whiterussian
    You probably want to run `ipkg update'
    ERROR: File not found: //usr/local/lib/ipkg/lists/non-free
    You probably want to run `ipkg update'
    ERROR: File not found: //usr/local/lib/ipkg/lists/backports
    You probably want to run `ipkg update'
    /bin/ipkg: line 1184: sort: not found
    Unpacking opkg...Done.
    Configuring opkg...Done.


    Run these commands to create the configuration file for our newly installed opkg:

    cat > /etc/opkg.conf << EOF
    src/gz snapshots http://downloads.openwrt.org/backfire/10.03.1-rc5/ar71xx/packages/

    dest root /opt
    dest ram /tmp
    lists_dir ext /tmp/var/opkg-lists
    EOF

    Notice the underlined /opt. In the future, all software will be installed to the /opt folder. The /jffs folder is only necessary for initial setup.

    Let's make sure everything works properly:

    umount /jffs
    mount -o bind /mnt/root /tmp/root
    mount -o bind /mnt/opt /opt
    export LD_LIBRARY_PATH='/opt/lib:/opt/usr/lib:/lib:/usr/lib'
    opkg update


    You should get as a result:

    Downloading http://downloads.openwrt.org/backfire/10.03.1-rc4/ar71xx/packages/Packages.gz.
    Inflating http://downloads.openwrt.org/backfire/10.03.1-rc4/ar71xx/packages/Packages.gz.
    Updated list of available packages in /tmp/var/opkg-lists/snapshots.


  7. Set the startup script to make the changes take effect each time upon boot
    Under DD-WRT’s web interface, Administration > Commands, copy the following script into the field and click 'Save Startup'. Make sure to set the correct drive mounting.

    #!/bin/sh

    sleep 5

    #mount -t ext3 -o noatime /dev/discs/disc0/disc /mnt
    #
    mount -t ext3 -o noatime /dev/sda /mnt
    #
    mount -t ext3 -o noatime /dev/sda1 /mnt
    #mount -t ext3 -o noatime /dev/discs/disc0/part1 /mnt
    #!!!! This is only needed for manual drive mounting 
    !!!

    sleep 2
    if [ -f /mnt/optware.enable ]; then
    mount -o bind /mnt/etc /etc
    mount -o bind /mnt/root /tmp/root
    mount -o bind /mnt/opt /opt
    else
    exit
    fi

    if [ -d /opt/usr ]; then
    export LD_LIBRARY_PATH='/opt/lib:/opt/usr/lib:/lib:/usr/lib'
    export PATH='/opt/bin:/opt/usr/bin:/opt/sbin:/opt/usr/sbin:/bin:/sbin:/usr/sbin:/usr/bin'
    else
    exit
    fi


    Reboot to make the change take effect.

  8. Set the initial variables
    Login with PuTTY then copy/paste the commands below to PuTTY window to create a script running each time when user root logins.

    cat > /mnt/root/.profile << EOF
    export LD_LIBRARY_PATH='/opt/lib:/opt/usr/lib:/lib:/usr/lib:/opt/usr/local/lib'
    export PATH='/sbin:/opt/bin:/opt/usr/bin:/opt/sbin:/opt/usr/sbin:/bin:/usr/bin:/usr/sbin:/opt/usr/local/bin'
    export PS1='\[\033[01;31m\]\u@\h \[\033[01;34m\]\W \$ \[\033[00m\]'
    export TERMINFO='/opt/usr/share/terminfo'
    EOF

    The above script will set the variables for us and also provide a nice colored command line prompt. Now exit the current PuTTY window and connect again, if you see a colored prompt then it's working correctly. Make sure the line breaks set correctly, each line between cat and EOF should start with export and end with '.

    Run opkg update directly and it should work now - no need to set the lengthy variables each time.

    Also, if you're not familar with vi, time to get the nano editor:
    opkg install nano
    Then type nano to launch it.

    Extra: Running installed software automatically at boot

    Since our DD-WRT startup script does nothing more than mounting several folders. We don't use it to launch any programs, simply because its not necessary as DD-WRT has a feature that will run any script during startup once it's in a correct location with correct name.

    To run a script, just put the scripts in /opt/etc/init.d and name them in the format S##ScriptName. The ## is two digit number from 00 to 99. Scripts with a smaller number will run first. For example, to run Samba file server and p910nd printer server as services, we just need to create two startup scripts:
    /opt/etc/init.d/p910nd
    /opt/etc/init.d/samba

    then make symbolic links:
    ln -s /opt/etc/init.d/p910nd /opt/etc/init.d/S30p910nd
    ln -s /opt/etc/init.d/samba /opt/etc/init.d/S50samba

     

    In this example, p910nd will start up before samba. If you no longer need a program to run at startup, just delete the symbolic links (S30p910nd/S50samba), but keep the original startup script so it can be easily recovered should you want to enable them again. The program can also be launched directly by running its corresponding script (/opt/etc/init.d/p910nd and /opt/etc/init.d/samba).
    Guides for Samba server and the p910nd printer server will be detailed in future articles. This is just to get you an idea how the startup control system works.
     --------------------------------------------------------------------------------------------------
    I'd also like to thank dynek @ DD-WRT forums for pieces taken from his thread: http://www.dd-wrt.com/phpBB2/viewtopic.php?t=86912 also a great place for any issues.

9 comments:

Hello,

I guess this is an old post, but I hope to get some help here. I hava a Netgear WNDR3700 router with DD-WRT v24sp2. I'm trying to install minidlna,and before doing so I have to go on through the steps you describe here. The problem I have is on step 6. After I put the commnad: "opkg update" I got this error:
"Couldn't stat /etc/opkg.conf: No such file or directory".

And ofcourse there is no such file there, but the directory is not writeable and I can't add it.

Any advices?

chmod 755 etc opt root
^this command should make your /mnt/etc folder writeable

cp -a /etc/* /mnt/etc/
^and this one should copy opkg.conf from /etc to /mnt/etc

Try running all the commands in step 5 again, in order.

Awesome instructions. I think the line:
cat > etc/opkg.conf << EOF

needs changed to:
cat > /etc/opkg.conf << EOF

because your're still in the /tmp directory. Am I mistaken?

This comment has been removed by the author.

Iskren, did you get it working?

Hi, this will not work with my Buffalo G300NH2.
The folders on the drive are deleted after the reboot in step 7.
Everything is like the tutorial except the last line in step 6. There´s no "opt" before the "tmp". It looks like:
Updated list of available packages in /tmp/var/opkg-lists/snapshots.
I think this is the failure, but i don´t know how to fix it :-(

The tmp folder is for the opkg snapshot that lists available packages, and is stored on the router's internal memory - It is updating to the correct folder.

However, the folders on the drive should remain, DD-WRT nor the script should delete anything off of the drive *ever*. I'm thinking that the drive isn't actually mounted. Try mounting the drive manually with one of the following commands:

mount -t ext3 -o noatime /dev/sda1 /mnt
mount -t ext3 -o noatime /dev/sda /mnt

The 'sda' part depends on how you have the drive partitioned, and the number of drives connected. 'sda' is drive 1, 'sdb' would be drive 2 if you have more than one drive attached. 'sda1' means the first partition on drive 1, 'sda2' means the second partition on drive 1, etc. If you only have one drive with one partition the correct format can be '/dev/sda' or '/dev/sda1' - it depends on how the kernel treats the drive. In my experience a flash drive is usually '/dev/sda' and a hard drive is usually '/dev/sda1'. Check the connected drives by listing the contents of /dev with:

ls /dev

If you have an 'sda' folder, but no 'sda1', mount with '/dev/sda' (if 'sda1' exists, use '/dev/sda1'). After submitting the mount command, check to see if the drive mounted correctly by listing the contents of the folder.

ls /mnt

If one drive (sda1 or sda) doesn't work, try the other one. (You may also be able to list the contents of the drive without mounting it - I don't recall) The command for that is:

ls /dev/sda

Sorry if this is too long, but I like to provide as much information as possible so readers can learn more. :)

The path for the wget statements in step 6 have changed. You get a 404 error with them as is.

Post a Comment