DNS 325 - Funplug 0.7 : Move Fun plug to a USB key

Contents[Hide]

dropcap-dns325

If you are running fun_plug on your DNS-325, you may have noticed that since your NAS is running the fun_plug script from a filesystem located on the first hard disk, this disk is not able to go into power saving mode anymore.

To allow your hard disk to enter power saving mode as often as possible, one elegant solution is to run Fun plug and its services from the filesystem of a connected USB key.

To be able to run fun_plug from a USB key filesystem, it means that you need :

  • fix a bad chmod 777 behaviour of the DNS-325
  • to format a USB key with Ext2 filesystem
  • to auto-mount USB key at boot
  • to reconfigure fun_plug to run from new location

This article will explain how to fully configure a DNS-325 to run a working Fun Plug 0.7 installation from a USB key. It supposes that your DNS-325 is already running Fun plug.

As a result, your NAS first hard disk will regain the possibility to enter power saving mode as soon as user data are not accessed.

The USB key should be at least a 1 Gb. A key of 2 or 4 Gb will be more confortable in case you need to install many Fun plug packages.

This guide is based on fun_plug 0.7. It's pre-requisite installation can be done following DNS 325 - Extend your NAS possibilities with Fun plug 0.7.
All following operations suppose that your are connected to the DNS-325 console thru SSH or Telnet.

This article has been tested on DNS 325 with firmware 1.03.
I've not succeeded to make it work under firmware 1.04. So, please downgrade to 1.03 if you really need this feature.

1. Prepare the Environment

1.1. Remove Chmod 777 upon Key Insertion

DNS-325 recognise automatically USB key and automount them under /mnt/USB/HD_c1

This behaviour could be very interesting if it was not extended by a stupid one : if the disk is having an Ext2 or Ext3 partition, it does a chmod 777 on the whole partition after mounting it !

That means that all access control on the directory structure of the key is lost. As the USB key is supposed to host /ffp and the SSH keys, that's a big trouble. SSH will refuse to start if the key files are not having 700 rights.

Hopefully, one fix has been found by kylek to intercept the chmod call and do nothing in case of 777 on the USB mounting directory.

This fix has been packed by uli and can be easily installed thru slacker :

# slacker -a uli:uwchmod
fetch: rsync -q 'rsync://funplug.wolf-u.li/funplug/0.7/arm/packages/uwchmod-1.0-arm-1.txz' '/ffp/funpkg/cache/uli'
/ffp/funpkg/cache/uli/uwchmod-1.0-arm-1.txz: OK

/ffp/bin/funpkg -i /ffp/funpkg/cache/uli/uwchmod-1.0-arm-1.txz

Scanning /ffp/funpkg/cache/uli/uwchmod-1.0-arm-1.txz ...
# This fixes a bug in the D-Link software where the firmware sets a
# "chmod -R 777" to all disks
Installing uwchmod-1.0-arm-1
Running doinst.sh ...
Running installer for UWCHMOD
Checking for correct installation
Seems to be good, executing installer
Done.

Searching for *.new files: /ffp/etc /ffp/start
Done.

1.2. Format the USB key

You now need to format a USB key with a primary ext2 partition. This can be done easily under any Linux workstation. Any live CD will do the job.

You should now format your USB key as a primary partition with ext3 filesystem and USBFUNPLUG label.

Once the is key formatted, insert it in the USB back port of DNS-325. The key is automatically detected and mounted under /mnt/USB/HD_c1

# df
Filesystem 1K-blocks Used Available Use% Mounted on
%root% 9911 4490 4909 48% /
/dev/ram0 9911 4490 4909 48% /
...
/dev/sdc1 982096 926308 55788 95% /mnt/USB/HD_c1

It seems that USB key is mounted under /mnt/HD_c1 or /mnt/HD_c depending on firmware version (thanks to Mike for the info).
This guide has been done on a DNS-325 mounting it under /mnt/HD_c1.
In case your device is mounting under /mnt/HD_c you need to ajust that path in the following sections.

1.3. Mount the Drive

Now that the disk is formatted in Ext3, we can mount it under the default mount point /mnt/USB/HD_c1.

We will mount it with the noatime option, to avoid unnecessary write operations to the key.

# umount /dev/sdc1
# mount /dev/sdc1 -t ext3 /mnt/USB/HD_c1 -o noatime
# df
Filesystem 1K-blocks Used Available Use% Mounted on
%root% 9911 4490 4909 48% /
/dev/ram0 9911 4490 4909 48% /
...
/dev/sdc1 967576 1204 917220 1% /mnt/USB/HD_c1

The drive is mounted !

1.4.  Copy Fun plug ffp Directory

Now that the USB mass storage is accessible, first thing to do is to copy the full ffp directory structure to the USB key.

The copy needs to be done while preserving all the file attributes.

It also need to preserve the root access only for SSH key files.

# cp -a /mnt/HD/HD_a2/ffp /mnt/USB/HD_c1

The complete Fun plug filesystem has been copied to the USB key.

We are now ready to set up the NAS boot procedure for Fun plug to start from the USB key filesystem.

2.  Setup Boot Procedure

Now that the environment has been prepared and that ffp files have been copied to the USB key, we need to adapt the boot procedure for Fun plug to run from the USB key filesystem at every boot.

In fact, as DNS-325 Operating System is running from a ramdisk filesystem, we have to repeat previous steps at every boot.

2.1. Main principles

At the end of the boot process, when ramdisk has been fully populated, DNS-325 calls the fun_plug script from first disk under /mnt/HD/HD_a2.

To be able to execute Fun plug from the USB key filesystem, we need to execute some specific operations at a very early stage of the fun_plug script. The main goal of all these operations will be to prepare a safe environment for Fun plug to run from the connected USB key.

Not to leave fun_plug in an unstable state because of a broken environment, this bootstrap procedure should be very secure. In case of any problem detected, fun_plug execution should fall-back to the default environment on the first hard disk.

As a target, Fun plug will be running from the USB key. It means that all updates will be done on the USB key filesystem. As the first hard disk is always used as a fallback in case of trouble (USB key unplugged, ...), we will also setup a mecanism to keep both ffp folders synchronised. This synchronisation will be triggered by the presence of /ffp/home/root/backup.do flag and will be done during the boot process.

2.2. Bootstrap /mnt/HD/HD_a2/.bootstrap/setup.sh

All the job described above will be done by a script called by fun_plug at a very early stage during its boot process.

In fact, as soon as fun_plug starts, if available, it runs a bootstrap procedure thru a script named /mnt/HD/HD_a2/.bootstrap/setup.sh.

This script main role is to prepare fun_plug specific execution environment. So it can obviously used to setup the USB key execution environment.

As this script is sourced, it can modify the original fun_plug variables.

This script main job will be :

  1. remount USB key first partition with noatime option
  2. check that the key is havong a ffp directory
  3. if needed, backup USB ffp to the first hard disk
  4. declare fun_plug root filesystem as the USB key ffp directory

At the end, if everything goes fine fun_plug boot will carry on within the USB key filesystem.

All steps will be logged in the standard /mnt/HD/HD_a2/ffp.log log file.

Obviously, the script /mnt/HD/HD_a2/.bootstrap/setup.sh has to be executable.

# mkdir /mnt/HD/HD_a2/.bootstrap
# chmod 777 /mnt/HD/HD_a2/.bootstrap
# wget --no-check-certificate -O /mnt/HD/HD_a2/.bootstrap/setup.sh https://raw.githubusercontent.com/NicolasBernaerts/dns325-scripts/master/usb-setup.sh
# chmod +x /mnt/HD/HD_a2/.bootstrap/setup.sh

/mnt/HD/HD_a2/.bootstrap/setup.sh

#!/bin/sh
#
# Script .bootstrap/setup.sh
#
# Called by fun_plug to prepare USB key execution environment
# If everything is ok, fun_plug will run from the USB key
#
# 05/09/2012 - V1.0 creation by Nicolas Bernaerts
# 24/09/2015 - V1.1 add 60s loop to wait for USB key mount (thanks to Albirew report)

# Set default path
PATH=/usr/sbin:/sbin:/usr/bin:/bin
CONTINUE=1

# Environment variables. You may adjust them
FFP_HD="/mnt/HD/HD_a2"
FFP_USB="/mnt/USB/HD_c1"
BCK_FLAG=${FFP_USB}/ffp/home/root/backup.do

MOUNT_WAIT=0
while [ ${MOUNT_WAIT} -lt 60 ]
do
# wait for 5 seconds
sleep 5

# check if USB key is mounted
USB_PARTITION=`df | grep "${FFP_USB}" | sed 's/^\([a-z0-9\/]*\).*$/\1/g'`

# if usb key not mounted, add 5 seconds, else over
[ -z "${USB_PARTITION}" ] && let MOUNT_WAIT=MOUNT_WAIT+5 || MOUNT_WAIT=60
done

# If a USB removable disk has not been mounted,
if [ -z "${USB_PARTITION}" ]; then
CONTINUE=0
echo "ERROR - USB device has not been detected as Mass Storage"

# else, remount it with noatime option
else
umount ${USB_PARTITION}
mount ${USB_PARTITION} -t ext2 ${FFP_USB} -o noatime 2>/dev/null
echo "USB - USB disk mounted under ${FFP_USB}"
fi

# Check presence of ffp directory at the USB key root. If not present, exit
if [ "$CONTINUE" -eq 1 ]; then
if [ ! -d "${FFP_USB}/ffp" ] ; then
CONTINUE=0
echo "ERROR - Directory ${FFP_PATH} doesn't exist"
else
echo "USB - Directory ${FFP_PATH} present"
fi
fi

# Check if a backup of USB key ffp directory is needed
if [ "$CONTINUE" -eq 1 ]; then
if [ -f ${BCK_FLAG} ] ; then
echo "USB - Backup of ${FFP_USB}/ffp needed. Target is ${FFP_HD}/ffp"
rm -r ${FFP_HD}/ffp
cp -a ${FFP_USB}/ffp ${FFP_HD}/ffp
rm ${BCK_FLAG}
echo "USB - Backup of ${FFP_USB}/ffp done. Backup flag deleted"
fi
fi

# Declare USB key ffp directory as ffp root
if [ "$CONTINUE" -eq 1 ]; then
FFP_PATH=${FFP_USB}/ffp
echo "USB - Fun_Plug is running from USB key under ${FFP_PATH}, accessible thru /ffp"
else
echo "ERROR - Fun_Plug root can't be moved to USB Key"
fi


2.3. First Reboot

Everything is now ready. You just need to reboot to get Fun plug running from your USB key.

If you notice, before the reboot, /ffp link to the first hard disk under /mnt/HD/HD_a2

# ls -la /ffp
lrwxrwxrwx 1 root root 16 Sep 6 22:36 /ffp -> /mnt/HD/HD_a2/ffp
# reboot

Wait for some time for the reboot process to complete (it can take up to 2 minutes ...).

Connect to the console thru SSH :

# ls -la /ffp
lrwxrwxrwx 1 root root 16 Sep 6 22:36 /ffp -> /mnt/USB/HD_c1/ffp

/ffp now links to the USB key under /mnt/USB/HD_c1

ffp.log also clearly shows that Fun plug is now running from the USB key.

Done !

Your first hard disk can now quietly go back to power saving mode while your Fun plug services are running from the USB key filesystem.

If you wait for the hibernation delay configured in the administration web interface, you should be able to connect to the console thru SSH and to get :

# dmesg
...
***************************************
* HD0 stand by now! *
***************************************

***************************************

* HD1 stand by now! *
***************************************
#

Your SSH connexion has not waken-up your first hard disk from hibernation ... nice, isn't it ?

You can now enjoy the power of Fun plug without any stress for your hard disk.

3. Synchronise Original Fun plug

As Fun plug is now running from your USB key, all your package installations and updates will be done on the USB key.

As original Fun plug environment is still present on the hard disk, il can be interesting to keep it synchronised with the USB key.

The above setup.sh script takes care of it. When you need to synchronise it, you just need to :

  1. create a backup flag in your root home directory
  2. reboot

# touch /ffp/home/root/backup.do
# reboot

During next reboot, the setup.sh bootstrap script will do the synchronisation job and remove the flag.

Your /mnt/HD_a2/ffp directory should now be the exact copy of /mnt/USB_key/ffp.

The day you remove your USB key, your DNS-323 will boot back from the fully updated hard disk Fun plug environment.

 

Hope it helps

Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus