Wednesday, December 3, 2014

Buffalo Linkstation LS-XHL as a bitorrent sync node

A long time ago I bought an LS-X1.0TL Linkstation Pro (LS-XHL) from Buffalo.  The reason I (and
many others) bought it is that it is an 1.2GHz Marvell ARMv5 with 256MB of RAM, a SATA connection, a USB 2.0 port and a gigabit wired ethernet port that you can run linux on. All for about 20W.

The great news is that dusting it off after finding it in a box I find that it is better supported than ever! There is a standard Debian wheezy disk image and kernel 3.14.4 without patches running on the SoC.

Obviously the best use is to install Bitorrent Sync on it and hide it on another network at a different physical location to provide an extra node for security and bandwidth! (grin)

Here is how I did it (based on the work of fjen from nas-central.org, original threads here and here and the lsmonitor script fix is based off Dennis82's post).

1. Cracking the case: https://www.youtube.com/watch?v=GLtY7Rb6s24. Really don't try this without seeing the video - I broke quite a lot of my case getting it open before this existed.

2. Extract the hdd and connect it to a linux system or a linux VM (this is very hard to do on windows). Using dmesg or similar locate the disk device node, e.g. /dev/sdb. Make sure you have the right disk or you might destroy your operating system hard drive.

3. Change the disklabel to GPT (GNU parted)
3.1 Create primary partition 1 at 0 to 128MB, type ext3
3.2 Create primary partition 2 at 128MB to -1024MB (yes -1024MB), type ext4
3.3 Create a swap partition 2 at -1024MB to -1s, type Linux swap

4. Format the filesystems (replace X with the letter of the ls-xhl hard disk you found earlier):
4.1 mkfs.ext3 /dev/sdX1 (the boot partition of 128MB)
4.2 mkfs.ext4 /dev/sdX2 (the main filesystem partition)

5. Mount the filesystems somewhere convenient (as root):
5.1 mkdir ls-xhl_img
5.2 mount /dev/sdX2 ./ls-xhl_img
5.3 mkdir ls-xhl_img/boot
5.4 mount /dev/sdX1 ./ls-xhl_img/boot
5.5 check that ls ./ls-xhl_img shows "boot"

6. Download the wheezy root filesystem from http://neboola.de/fjen/lschlv2/debian/wheezy/
6.1 Unpack as root using GNU tar to the ls-xhl_img/ directory (not boot/)
6.2 Unmount the partitions (ls-xhl_img/boot first)

7. Install the hard drive back onto the ls-xhl's circuit, connect the ethernet cable to a network with a DHCP server where you can find the logs (your home router typically has a page in the web interface where you can find the DHCP logs) and power on.
7.1 The LED on the front should blink blue for a while before going solid on.
7.2 Check the DHCP logs for the IP address that the LS-XHL has acquired using the name linkstation.

8. ssh into that IP address as root (password toor) and run the following commands:
dpkg-reconfigure locales
dpkg-reconfigure tzdata
rm /etc/ssh/ssh_host_* && dpkg-reconfigure openssh-server
apt-get update

8.1 Install the debian updates to ensure you have a safe, secure system.

9. Update the kernel:
http://forum.buffalo.nas-central.org/viewtopic.php?f=71&t=25971

10. Update the lsmonitor script in /etc/init.d/lsmonitor included at the end  (thanks Dennis82 for the pointer to this solution).
10.1 Install the following packages: apt-get install hddtemp lm-sensors fancontrol
10.2 Restart the lsmonitor script by /etc/init.d/lsmonitor restart 

11. Create a user called btsync (or similar) to own the btsync processes and disk space for files.
11.1 Install Bitorrent Sync server packages using the unoffical .deb packages from here: http://forum.bittorrent.com/topic/18974-debian-and-ubuntu-server-unofficial-packages-for-bittorrent-sync/
11.2 Configure packages to run as user btsync.
11.3 Direct your web browser to the IP address:8888 and the Bitorrent Sync GUI should greet you.

12. Profit!

#!/bin/sh

### BEGIN INIT INFO
# Provides:          lsmonitor
# Required-Start:    $syslog
# Required-Stop:     $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Monitor Linkstation LS-CHLv2 and LS-XHL
# Description:       Enable service provided by daemon.
### END INIT INFO

#
# lsmonitor:
# - handle ls complete power-on
# - monitor hdd temperature & control fan speed
# - monitor hdd status & contol function led
# - monitor function button (todo)
# - monitor power switch (todo)
#
# Changelog:
# - Modified to work with a Debian kernel on a LS-CHLv2
# - Added hdd status monitoring
# - Modified to work with a vanilla kernel >= 3.6
# - Modified to work with a vanilla kernel >= 3.8
#
# Copyright (C) 2013 Sergej Wildemann
##
# Based on:
#  blstools - Copyright (C) 2010 Michele Manzato
#
# Credits:
#  Thanks to archonfx on Buffalo NAS Central forum for HDD
#  temperature monitoring command.
##
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
##

# Location of pid file
PIDFILE=/var/run/lsmonitor_daemon.pid

# Control interfaces
LINFO="/sys/devices/gpio_leds.3/leds/lsxl:amper:info"
LPOWER="/sys/devices/gpio_leds.3/leds/lsxl:blue:power"
LBFUNC="/sys/devices/gpio_leds.3/leds/lsxl:blue:func"
LAFUNC="/sys/devices/gpio_leds.3/leds/lsxl:red:func"
LALARM="/sys/devices/gpio_leds.3/leds/lsxl:red:alarm"

#FAN_CTRL=/sys/bus/platform/drivers/gpio-fan/gpio_fan.3
FAN_CTRL=/sys/bus/platform/drivers/gpio-fan/gpio_fan.4/hwmon/hwmon0
FAN_TARGET=${FAN_CTRL}/fan1_target
FAN_INPUT=${FAN_CTRL}/fan1_input

# Fan speeds
SPEED_STOP=0
SPEED_SLOW=1500
SPEED_HIGH=3250
SPEED_FULL=5000

# HDD temperature limits for fancontrol
HDDTEMP0=45
HDDTEMP1=50
HDDTEMP2=55

# Load settings
. /etc/default/lsmonitor

# Set and get current fan speed
fan_speed()
{
 if [ $1 = "get" ]; then
  cat $FAN_INPUT
 elif [ $1 = "set" ]; then
  echo $2 > $FAN_TARGET
 fi
}

# Monitor HDD temperature & control fan speed
monitor_temperature()
{
 # Retrieve HDD temp
 HDDTEMP=$(smartctl /dev/sda --all | awk '$1 == "194" {print $10}')

 # Change fan speed accordingly
 if [ $HDDTEMP -le $HDDTEMP0 ]; then
  fan_speed set $SPEED_STOP
 elif [ $HDDTEMP -le $HDDTEMP1 ]; then
  fan_speed set $SPEED_SLOW
 elif [ $HDDTEMP -le $HDDTEMP2 ]; then
  fan_speed set $SPEED_HIGH
 else
  fan_speed set $SPEED_FULL
 fi
}

# Monitor HDD status and deactivate power led if idle
monitor_hdd()
{
 HDDSTATUS=$(hdparm -C /dev/sda | grep "standby")
 if [ -z "$HDDSTATUS" ]; then
  echo default-on > $LBFUNC/trigger
  if [ $HDDSTATUSOLD -eq 0 ]; then
   echo `date` active >> /var/log/hddstatus.log
   HDDSTATUSOLD=1
  fi
 else
  echo none > $LBFUNC/trigger
  if [ $HDDSTATUSOLD -eq 1 ]; then
   echo `date` standby >> /var/log/hddstatus.log
   HDDSTATUSOLD=0
  fi
 fi
}

# Control LS switch status to power down the unit
lsmonitor_daemon()
{
 COUNT=12
 HDDSTATUSOLD=0

 echo 1 > $FAN_CTRL/pwm1_enable

 fan_speed set $SPEED_STOP

 while [ true ]; do
  # Check switch status
  #PWR_SW=`cat /sys/class/gpio/gpio${GPIO_PWR_SW}/value`
  #AUT_SW=`cat /sys/class/gpio/gpio${GPIO_AUT_SW}/value`

  # Check HDD status
  monitor_hdd

  # Terminate when in OFF state
  #if [ "$PWR_SW" -eq 1 ] && [ "$AUT_SW" -eq 1 ]; then
  # break
  #fi

  # Once per minute monitor HDD temperature
  if [ $COUNT -eq 12 ]; then
   COUNT=0
   monitor_temperature
  else
   COUNT=$(( $COUNT + 1 ))
  fi

  sleep 5
 done

 # Run the fan at low speed while halting, just in case halt hangs the unit
 fan_speed set $SPEED_LOW

 # blink power led
 echo timer > $LPOWER/trigger
 echo 100   > $LPOWER/delay_on
 echo 100   > $LPOWER/delay_off

 # Initiate unit shutdown
 halt
}

# Kill the lsmonitor daemon
kill_lsmonitor_daemon()
{
 PID=`cat $PIDFILE`
 if [ "$PID" != "" ] ; then
  kill $PID
  rm $PIDFILE
 fi
}


case $1 in
 start)
  # Start the lsmonitor daemon
  lsmonitor_daemon &
  echo $! > $PIDFILE
  ;;
 stop)
  # Kill the lsmonitor daemon
  kill_lsmonitor_daemon
  ;;

 restart)
  $0 stop && sleep 2 && $0 start
  ;;

 *)
  echo "Usage: $0 {start|stop|restart}"
  exit 2
  ;;
esac

No comments:

Post a Comment