Sunday, June 23, 2013

Get your QNap to Call Home with VTun

My QNap moves around, occasionally, and I like to keep the configuration on my backups consistent.  So, I have a crappy VTun link to keep it where I want it.  When a device comes up, it calls a host and opens a channel so I can get back to it easily.

(Ideally, I'd like it to use the central server like a lookup server, to make other connections, like skype does, but that's a story for another time and some other coder)
  1. install Optware
  2. ipkg update
    # ipkg update
    Downloading
    http://ipkg.nslu2-linux.org/feeds/optware/cs08q1armel/cross/unstable/Packages.gz
    Inflating
    http://ipkg.nslu2-linux.org/feeds/optware/cs08q1armel/cross/unstable/Packages.gz
    Updated list of available packages in /opt/lib/ipkg/lists/cs08q1armel
    Successfully terminated.
    
    
  3. ipkg install vtun
    # ipkg install vtun
    Installing vtun (3.0.2-2) to root...
    Downloading
    http://ipkg.nslu2-linux.org/feeds/optware/cs08q1armel/cross/unstable/vtun_3.0.2-2_arm.ipk
    Installing lzo (2.03-1) to root...
    Downloading
    http://ipkg.nslu2-linux.org/feeds/optware/cs08q1armel/cross/unstable/lzo_2.03-1_arm.ipk
    Installing openssl (0.9.8v-2) to root...
    Downloading
    http://ipkg.nslu2-linux.org/feeds/optware/cs08q1armel/cross/unstable/openssl_0.9.8v-2_arm.ipk
    Installing zlib (1.2.5-1) to root...
    Downloading
    http://ipkg.nslu2-linux.org/feeds/optware/cs08q1armel/cross/unstable/zlib_1.2.5-1_arm.ipk
    Configuring lzo
    Configuring openssl
    Configuring vtun
    Configuring zlib
    Successfully terminated.
    
    
  4. configure your VPN connection
So far, so good.  But how do you make it start on boot?  That's tricky, as the QNap is largely flushed with each reboot, and we want something that is resilient and persistent -- so it's got to use something like inittab (or, if you hate Unix, maybe upstart).

So you create what they call an autorun.sh , a script.  It runs on boot due to some trickery, and we use that to make our box a little more chatty.  This hack lets you run the script you want at boot, but the timing can be a little racey.
  1. qpkg.conf
    cat << EOF >> /etc/config/qpkg.conf
    [autorun]
    Name = autorun
    Version = 0.1
    Author = neomilium
    Date = 2013-05-06
    Enable = TRUE
    Install_Path = /share/Public/../.qpkg/autorun
    Shell = /share/Public/../.qpkg/autorun/autorun.sh
    EOF
    
    
  2. autorun.sh
    cat << EOF > /share/Public/../.qpkg/autorun/autorun.sh
    lsmod | grep -q ^tun || insmod /lib/modules/others/tun.ko
    cat << EOT >>/etc/inittab
    null::respawn:/opt/sbin/vtund -n -f /opt/etc/vtund.conf profile server 2>/dev/null
    EOT
    EOF
    (sleep 60 ; /bin/kill -1 1) &
  3. tweak
    mkdir -p /share/Public/../.qpkg/autorun/
    chmod a+x /share/Public/../.qpkg/autorun/autorun.sh
    
And that's kinda it.

We use the funky filename because it seems to be immune to changes between one QNap and the next -- moreso, immune to differences in drive configuration.  I think.  Let me know if it's not.