DNS 323 - Funplug 0.5 : Move Fun plug to a USB key

Contents[Hide]

dropcap-dns323

If you are running fun_plug on your DNS-323, 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. But with DNS-323 things are not so simple as it doens't handle natively USB mass storage ...

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

  • to install and load a USB mass storage driver
  • to format the USB key with the Ext filesystem used by DNS-323
  • to auto-mount the USB key at boot
  • to reconfigure fun_plug to run from new location

This article will explain how to fully configure a DNS-323 to run a working Fun Plug 0.5 installation from a USB key. It supposes that your DNS-323 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 and FAT formatted. 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.5.
It's pre-requisite installation can be done following DNS 323 - Extend your NAS possibilities with Fun plug 0.5.
All following operations suppose that your are connected to the DNS-323 console thru SSH or Telnet.

1. Prepare the Environment

1.1. Install the USB Storage Module

If you plug a USB key in the USB back port, you will notice that it has been detected, but not as a mass storage.

# dmesg
...
usb 1-1: new high speed USB device using ehci_platform and address 2

So first thing to do is to get the proper USB Mass Storage driver, to install and to load it.

Driver is available from http://www.domaigne.com/download/dns-323/

As Fun Plug provide a bootstrap mecanism, we will install the driver in the default expected directory /mnt/HD_a2/.bootstrap

We will make sure that this directory is accessible to everybody, even thru the Samba share if fun_plug is not running.

# mkdir /mnt/HD_a2/.bootstrap
# chmod 777 /mnt/HD_a2/.bootstrap
# cd /mnt/HD_a2/.bootstrap
# wget http://bernaerts.dyndns.org/download/dns-323/usb-storage.ko
Connecting to bernaerts.dyndns.org (87.88.254.143:80)
usb-storage.ko 100% |***************************************************************************| 44215 --:--:-- ETA
# insmod /mnt/HD_a2/.bootstrap/usb-storage.ko

Wait for 10 seconds and the USB key should be detected as a mass storage

# dmesg
usb 1-1: new high speed USB device using ehci_platform and address 2
Initializing USB Mass Storage driver...
scsi2 : SCSI emulation for USB Mass Storage devices
usb-storage: device found at 2
usb-storage: waiting for device to settle before scanning
usbcore: registered new driver usb-storage
USB Mass Storage support registered.
Vendor: OCZ Model: RALLY2 Rev: 1100
Type: Direct-Access ANSI SCSI revision: 00
SCSI device sdc: 15663104 512-byte hdwr sectors (8020 MB)
sdc: Write Protect is off
sdc: Mode Sense: 43 00 00 00
sdc: assuming drive cache: write through
SCSI device sdc: 15663104 512-byte hdwr sectors (8020 MB)
sdc: Write Protect is off
sdc: Mode Sense: 43 00 00 00
sdc: assuming drive cache: write through
sdc: sdc1
Attached scsi removable disk sdc at scsi2, channel 0, id 0, lun 0
Attached scsi generic sg2 at scsi2, channel 0, id 0, lun 0, type 0
usb-storage: device scan complete

The USB key has been detected as /dev/sdc (this setup is having 2 hard disks).

You can get some detailed information about your USB key :

# ls /proc/scsi/usb-storage
2
# cat /proc/scsi/usb-storage/2
Host scsi2: usb-storage
Vendor: OCZ Technology
Product: RALLY2
Serial Number: AA04012700104543
Protocol: Transparent SCSI
Transport: Bulk
Quirks:

1.2. Format the USB key

As inserted USB key is a plain FAT16 or FAT32 key, we need to :

  • remove the existing partition on the USB key
  • create a new primary partition
  • format it to Linux Ext2

To remove and create partition, we will use fdisk. All data will be lost as the partition will be erased.

One problem is that, if you use the version of fdisk provided with fun_plug, you may get a segmentation fault. So, first thing to do will be to get a new version of fdisk do realise the job :

# cd /mnt/HD_a2/.bootstrap
# wget http://bernaerts.dyndns.org/download/dns-323/fdisk
Connecting to bernaerts.dyndns.org (87.88.254.143:80)
fdisk 100% |***************************************************************************| 177k --:--:-- ETA
# chmod +x /mnt/HD_a2/.bootstrap/fdisk
# /mnt/HD_a2/.bootstrap/fdisk /dev/sdc
The number of cylinders for this disk is set to 1418.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): d
Selected partition 1

Command (m for help): p

Disk /dev/sdc: 8019 MB, 8019509248 bytes
251 heads, 44 sectors/track, 1418 cylinders
Units = cylinders of 11044 * 512 = 5654528 bytes
Disk identifier: 0xc3072e18

Device Boot Start End Blocks Id System

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1418, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1418, default 1418):
Using default value 1418

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
# mke2fs /dev/sdc1
mke2fs 1.41.0 (10-Jul-2008)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
489600 inodes, 1957543 blocks
97877 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2004877312
60 block groups
32768 blocks per group, 32768 fragments per group
8160 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 29 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

1.3. Mount the Drive

Now that the disk is formatted in ext2, we can mount it under /mnt/USB_key

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

# mkdir /mnt/USB_key
# chmod 777 /mnt/USB_key
# mount /dev/sdc1 -t ext2 /mnt/USB_key -o noatime
# df
Filesystem 1k-blocks Used Available Use% Mounted on
rootfs 9911 7762 1637 83% /
/dev/root 9911 7762 1637 83% /
/dev/loop0 5760 5760 0 100% /sys/crfs
...
/dev/sdc1 7707220 17196 7298516 0% /mnt/USB_key

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 -drp /mnt/HD_a2/ffp /mnt/USB_key

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-323 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-323 calls the fun_plug script from first disk under /mnt/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 procedure 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_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_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. check presence of USB mass storage module
  2. load that module
  3. wait for the USB key to be detected
  4. create mount point /mnt/USB_key
  5. mount USB key first partition
  6. check that the key is havong a ffp directory
  7. if needed, backup USB ffp to the first hard disk
  8. 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_a2/ffp.log log file.

Obviously, this script has to be executable.

# mc -e /mnt/HD_a2/.bootstrap/setup.sh
# chmod +x /mnt/HD_a2/.bootstrap/setup.sh

/mnt/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 by Nicolas Bernaerts

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

# Environment variables. You may adjust them
FFP_HD="/mnt/HD_a2"
FFP_USB="/mnt/USB_key"
BCK_FLAG=${FFP_USB}/ffp/home/root/backup.do
USB_MODULE=${FFP_HD}/.bootstrap/usb-storage.ko

# Test presence of USB storage module. If not found, exit else load module
if [ ! -e "${USB_MODULE}" ]; then
  CONTINUE=0
  echo "ERROR - Module ${USB_MODULE} not found"
else
  echo "USB - Module ${USB_MODULE} detected"
fi

# Load USB storage module & check if module has been loaded. If not, exit
if [ "$CONTINUE" -eq 1 ]; then
  insmod ${USB_MODULE}
  TEST_RESULT=`lsmod | grep usb_storage`
  if [ -z "${TEST_RESULT}" ] && [ "$CONTINUE" -eq 1 ]; then
    CONTINUE=0
    echo "ERROR - Module ${USB_MODULE} can't be loaded"
  else
    echo "USB - Module ${USB_MODULE} loaded"
  fi
fi

# Check if a USB removable disk has been detected. If not, exit
if [ "$CONTINUE" -eq 1 ]; then
  # 10 seconds loop to detect new removable drive
  COUNTER=0
  while true
  do
    sleep 1
    COUNTER=`expr $COUNTER + 1`
    TEST_RESULT=`dmesg | grep "Attached scsi removable disk"`
    if [ ! -z "${TEST_RESULT}" ]; then break; fi
    if [ "$COUNTER" -gt 10 ]; then break; fi
  done

  # check if removable drive has been detected
  if [ -z "${TEST_RESULT}" ]; then
    CONTINUE=0
    echo "ERROR - USB device has not been detected as Mass Storage"
  else
    USB_DEVICE=`echo $TEST_RESULT | sed 's/^Attached scsi removable disk \([a-z]*\) .*$/\1/g'`
    USB_PARTITION=/dev/${USB_DEVICE}1
    echo "USB - Mass Storage USB device detected as ${USB_PARTITION} in $COUNTER seconds"
  fi
fi

# Create mount point & mount partition as Ext2
if [ "$CONTINUE" -eq 1 ]; then
  mkdir ${FFP_USB}
  chmod 777 ${FFP_USB}
  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 -drp ${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_a2

# ls -la /ffp
lrwxrwxrwx 1 root root 16 Sep 6 22:36 /ffp -> /mnt/USB_key/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 and print ffp.log :

# cat /mnt/HD_a2/ffp.log
**** fun_plug script for DNS-323 (2008-08-11 This email address is being protected from spambots. You need JavaScript enabled to view it.) ****
Thu Sep 6 22:36:08 GMT 2012
* Running /mnt/HD_a2/.bootstrap/setup.sh ...
USB - Module /mnt/HD_a2/.bootstrap/usb-storage.ko detected
USB - Module /mnt/HD_a2/.bootstrap/usb-storage.ko loaded
USB - Mass Storage USB device detected as /dev/sdc1 in 5 seconds
USB - USB disk mounted under /mnt/USB_key
USB - Directory /mnt/HD_a2/ffp present
USB - Fun_Plug is running from USB key under /mnt/USB_key/ffp, accessible thru /ffp
ln -snf /mnt/USB_key/ffp /ffp
* Running /ffp/etc/fun_plug.init ...
* Running /ffp/etc/rc ...
* /ffp/start/syslogd.sh inactive
...
* /ffp/start/inetd.sh inactive
* OK
# ls -la /ffp
lrwxrwxrwx 1 root root 16 Sep 6 22:36 /ffp -> /mnt/USB_key/ffp

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

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

Bingo !

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