I bought a **DLink DNS313** on a bargain price some time ago.. its a simple ARM based NAS. The clock battery had run out so that was the first replacement. Then I put in a 500GB drive. Rather than use the DLink software I decided to create a headless server with Debian as there were then tutorials on the net. Here are some notes.\\

First I downloaded some tarballs with **Debian Lenny**. The one that worked was ''Debian_lenny_revB.tgz'' from a site that is no longer up. The tarball has the files inside a couple of directories. I just hooked up the DNS313 via a USB cable to my Ubuntu machine. Follwing advice, I formatted the drive using this script
<code>
#!/bin/sh
#
#Set this 3 parameters
LENNY = Debian_lenny_revB.tgz
DISK = sdh
MOUNT_POINT = /mnt/exh3
echo -e "d\n1\nd\n2\nd\n3\nd\nn\np\n1\n\n+1G\nn\np\n3\n\n+5G\nn\
np\n2\n\n\na\n3\nt\n1\n82\nw\n" | fdisk /dev/${DISK}
umount /dev/${DISK}2
umount /dev/${DISK}3
mkswap /dev/${DISK}1
mkfs.ext2 /dev/${DISK}2
mkfs.ext2 -I 128 /dev/${DISK}3
if [ ! -d $MOUNT_POINT ]; then
mkdir $MOUNT_POINT ;
fi
mount -t ext2/dev/${ DISK }3 $MOUNT_POINT
tar - xzf $LENNY -C $MOUNT_POINT
umount /dev/${DISK}3
echo " Finished "
</code>
I do not now no what the reason for the reason some data is written with the ''echo -e'' line.\\
I partition my disks like this
  * sda1: Swap-partition 1GB
  * sda2: data-partition ext2 or better ext3 (Volume_1), sized to make full use of the disk  (452GB)
  * sda3: root-partition ext2 recommended size 3-5 GB, I made it 6GB
  * sda3: *active (or boot)

I then used ''sudo mc'' to uncompress and copied the tar files over to ''sda3''\\

Using mc, I then edited a few files\\
  * To give it a static address, I edited ''/etc/network interfaces'' to give it a static address.\\
  * Edited ''/etc/resolv.conf'' to put in an OpenDNS address. Take out everything except ''nameserver''
  * Edited ''/etc/timezone'' to my location
  * Edited ''/etc/environment'' to "LANGUAGE=en" and "LC_ALL=C" to get rid of the pesky unset messages during installation and updates of packages.
  * Edited ''/etc/apt/sources.list'' to reflect the new location of the old Lenny system i.e
    *''deb http://archive.debian.org/debian-archive/debian/ lenny main contrib non-free''
    *''deb http://archive.debian.org/debian-security/ lenny/updates main contrib non-free''
Then it was time to umount the USB interface, and shutdown the DNS313 by pressing the front-button briefly\\
Plug in the ethernet network cable and boot up the box with another brief press on the front button.\\
If everything is in order, the HD light will blink and eventually turn green.\\

You can then ''ssh'' into the box by typing ""ssh root@192.168.0.200" if that is the static address you chose before.\\
If successful, you can login with the default password ""passwd".\\

Next we check if the date is correct.  (remember the clock battery was replaced.\\
Do it by following these examples
<code>
date --set 1998-11-02 
date --set 21:08:00

#Set hardware clock to system time
hwclock --systohc
</code>
Change the default root password to something safer by running ''passwd''.\\

I also followed some advice to cut some disk activities by running\\
<code>
tune2fs -c0 -i 0 -m 0 /dev/sda2
</code>

Then run ''apt-get update'' for the new repos. You will get a ''Key Missing" error. To fix run ''apt-get install debian-keyring debian-archive-keyring''.\\
Then update again with ''apt-get update". If there are no errors, do a system update with ''apt-get dist-upgrade''.\\
You also have to install ''lm-sensors'' to get the fancontrol working.\\
''apt-get install lm-sensors''\\

Not necessary but install ''htop'' so you can have a nice utility to see how things are running on your server.\\
''apt-get install htop''\\
Similarly install ''lshw'' to give you some hardware info in case you need it.\\
''apt-get install lshw bzip2''\\

Clean up the cache with ''apt-get autoclean'' then reboot i.e. ''reboot''

=== Setup Pureftp ===
Now we need to create a new system group for pureftpd:\\
''groupadd ftpgroup''\\
Now we add a user for the group and give that user no permission to a home directory or a shell:\\
''useradd -g ftpgroup -d /dev/null -s /etc ftpuser''\\
Create a regular user for ftp\\
''pure-pw useradd transmission -u ftpuser -g ftpgroup -d /mnt/HD_a2/transmission''\\
You have to enter the transmission user's new password twice.\\

Update the preftp user database\\
''pure-pw mkdb''\\
Check the list of users with\\
''pure-pw list''\\

You need to set Pure-FTPd as a standalone server:\\
Edit ''/etc/default/pure-ftpd-common''\\
Replace this:\\
''STANDALONE_OR_INETD=inetd''\\
With this:\\
''STANDALONE_OR_INETD=standalone''\\

Now we want to ensure that the standalone server checks our usernames and passwords against the pureftpd database file:\\
Edit ''/etc/pure-ftpd/conf/PureDB'' and add the following to that file (if it doesn't exist):
''/etc/pure-ftpd/pureftpd.pdb''.\\
Now we need to create a symbolic link to the PureDB file:\\
''cd /etc/pure-ftpd/auth''\\
''ln -s /etc/pure-ftpd/conf/PureDB 50pure''\\
''ls -ls''\\
You should now see a new file ''50pure'' linking to ''../conf/PureDB''.\\
Restart Pure-FTPd:\\
''/etc/init.d/pure-ftpd restart''\\
Pure-FTPd should now startup in daemon mode and you can use Filezilla to login and see the transmission folder. ''htop'' will also show the ''pure-ftp'' daemon running.\\

=== Set up transmission ===
Install transmission by\\
''apt-get install transmission transmission-cli gnutls-bin''\\
The version installed is pretty old 1.22-1 but I find it still useable\\

I use **elinks** to download the torrent files so install that\\
''apt-get install elinks''\\

I make a directory in /mnt/HD_a2/transmission called ''watch''\\
Set the download directory to that directory by editing ''/etc/elinks/elinks.conf''\\
Add this line\\
''set document.download.directory = "/mnt/HD_a2/transmission/watch"''\\

Create a transmission user and associate it with the ftpgroup\\
''useradd -g ftpgroup -c "Transmission User" -s /bin/sh -d /mnt/HD_a2/transmission transmission''\\

Create a file called ''transmission'' in the ''/etc/init.d/'' folder\\
Paste the following code in it
<code>
#!/bin/sh
### BEGIN INIT INFO
# Provides:          transmission-daemon
# Required-Start:    networking
# Required-Stop:     networking
# Default-Start:     2 3 5
# Default-Stop:      0 1 6
# Short-Description: Start the transmission BitTorrent daemon client.
### END INIT INFO

TORRENTFOLDER=/mnt/HD_a2/transmission
TORRENTDOWNLOAD=/mnt/HD_a2/transmission/Downloads
TRANSMISSION_USER=transmission
TRANSMISSION_PORT=49164
DOWNLOADLIMIT=250
UPLOADLIMIT=50

#===============================================================================
function start_transmission {
echo -n "Starting Transmission Daemon..."
su $TRANSMISSION_USER -c "transmission-daemon -g $TORRENTFOLDER"

#Wait a while till the daemon has started...
sleep 20

#Set a few settings
transmission-remote -t daemon -g $TORRENTFOLDER --port $TRANSMISSION_PORT -E -d $DOWNLOADLIMIT -u $UPLOADLIMIT -f $TORRENTDOWNLOAD --start all
}

#===============================================================================
case "$1" in
  start)
        start_transmission
        ;;
  stop)
        echo "Shutting down torrent services:"
        transmission-remote --stop all -g $TORRENTFOLDER
        tdpids=`pidof transmission-daemon`
        while [[ "$tdpids" != "" ]]
        do
            for pid in $tdpids
            do
                /bin/kill -TERM $pid 2>/dev/null 1>/dev/null
                # DEBUG
                # echo /bin/kill -TERM $pid
            done

            tdpids=`pidof transmission-daemon`
        done

        rm -f /var/run/transmission.pid
        echo " done."
        echo -n "Now killing transmission-remote..."
        trpids=`pidof transmission-remote`
        for pid in $trpids; do
            /bin/kill -TERM $pid 2>/dev/null 1>/dev/null
        done
        echo " done."
        ;;
  restart)
        $0 stop
        $0 start
        ;;
  *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac

:

</code>
Make it executable and owned by root
<code>
chmod +x /etc/init.d/transmission
chown root.root  /etc/init.d/transmission
</code>
Edit ''/etc/runlevel.conf''\\
Put this line right after the line for pureftp\\
''20	0,1,6	2,3,4,5		/etc/init.d/transmission''\\
so that transmission will start on boot.\\

Use ''elinks'' to download a torrent file to the watch folder.\\

Make your life easier by making a few shell scripts\\
After adding the torrent file, add it to the queue by making a ''tmadd'' script in ''/usr/local/bin''\\
Add this to it and make it executable\\
<code>
#!/bin/sh
# add torrents from a watch directory then delete them
# Short-Description: Start the transmission BitTorrent daemon client.
###

TORRENTFOLDER=/mnt/HD_a2/transmission
TORRENTDOWNLOAD=/mnt/HD_a2/transmission/Downloads
#TRANSMISSIONPORT=49164
#DOWNLOADLIMIT=250
#UPLOADLIMIT=50
WATCHDIR=/mnt/HD_a2/transmission/watch

for file in $WATCHDIR/*.torrent

do
if [ "$file" != "$WATCHDIR/*.torrent" ]; then
transmission-remote -t daemon -a "$file" -g $TORRENTFOLDER -f $TORRENTDOWNLOAD
echo "Added file $file.."
rm "$file"
sleep 1
fi
done

echo "Done..."

transmission-remote -g /mnt/HD_a2/transmission -l

exit 0
</code>
Make a ''tmlist'' script in ''/usr/local/bin'' to list the torrent queue\\
<code>
#!/bin/sh
# List all torrents
echo "Listing all torrents..."
transmission-remote -g /mnt/HD_a2/transmission -l
echo "That's all folks.."

exit 0
</code>
Make a transmission stop script ''tmstop'' script in ''/usr/local/bin''\\
<code>
#!/bin/sh
# stop transmission
# Short-Description: Stop the transmission BitTorrent daemon client.
###

/etc/init.d/transmission start

exit 0
</code>
And similarly a ''tmstart'' script which is exactly the same except you'd change the stop to start.\\

The stop and start scripts are to clean up the torrents using ''mc'' as there was no way to remove completed torrents that I could determine..\\

There is a suggestion to install the Network Time Daemon. It will keep the time on you DNS-323 in sync with servers on the internet.
<code>
apt-get install ntp
/etc/init.d/ntpd start
</code>
And ''wget'' is also a nice tool to install but you'll have to use [[using_screen_to_run_a_detached_command|screen]] to utilise it\\

I also made a bash script to open ''mc'' in the transmission folder.\\
Create a file call ''mct'' in ''/usr/local/bin''\\
Put this code in it
<code>
#!/bin/sh
# Open mc inside the transmission directory
cd /mnt/HD_a2/transmission
mc
</code>
Make it execuatble by ''chmod +x /usr/local/bin/mct''\\
