11 Aug 2010

Qnap NAS -- dnsmasq via ipkg

dnsmasq is a lightweight dns and dhcp server, well suited for a NAS and available through ipkg (Optware plugin) on Qnap devices.

First of all, make sure the dhcp server on the system is deactivated before using dnsmasq as dhcp server. Setting up a DHCP server the qnap way is a little bit hidden, you may check: System Administration » Network  » TCP/IP » IP Address » Edit.

there is a linux like package manager (similar to deb or rpm) which provides the most convenient way to install dnsmasq.

Before being able to install ipkg packages a qnap package (which contains ipkg) must be installed. The package is called Optware. Download it and install it via the Qnap admin interface (Application » QPKG Plugins » Insallation, upload the unzipped .qpkg file).

Make sure that init scripts from /opt/init.d are executed, this might be prevented by a bug in the 3.x firmware (this is the location where ipkg stores it's init scripts). Edit /etc/init.d/Optware.sh an add the following code snipped:

case "$1" in
  start)
               ...[elided]...
 # adding Ipkg apps into system path ...
 /bin/cat /etc/profile | /bin/grep "PATH" | /bin/grep "/opt/bin" 1>>/dev/null 2>>/dev/null
# Patch per http://wiki.qnap.com/wiki/Install_Optware_IPKG
#       [ $? -ne 0 ] && /bin/echo "export PATH=$PATH":/opt/bin:/opt/sbin >> /etc/profile
        # Bug fix for following: put IPKG first, per http://forum.qnap.com/viewtopic.php?f=124&t=15663
        # was [ $? -ne 0 ] && /bin/echo "export PATH=$PATH":/opt/bin:/opt/sbin >> /etc/profile
        [ $? -ne 0 ] && /bin/echo "export PATH=/opt/bin:/opt/sbin:\$PATH" >> /etc/profile

  /bin/echo "Run Optware/ipkg /opt/etc/init.d/*"
   source /etc/profile
        # Start all init scripts in /opt/etc/init.d
 # executing them in numerical order.
 #
 for i in /opt/etc/init.d/S??* ;do
     # Ignore dangling symlinks (if any).
         #[ ! -f "$i" ] && continue

            case "$i" in
                *.sh)
             # Source shell script for speed.
             (
          trap - INT QUIT TSTP
   set start
          . $i
          )
         ;;
         *)
          # No sh extension, so fork subprocess.
          $i start
                ;;          
            esac            
        done                
# End patch

 ;;
  stop)
               ...[elided]...
Then make sure, that the dnsmasq init script file name ends in .sh (which is not the case at the time of this writing).
ln -s /opt/etc/init.d/S56dnsmasq  /opt/etc/init.d/S200dnsmasq.sh
Make sure to disable the local dhcpd server when using dnsmasq as dhcp server (/opt/etc/init.d/S??*dnsmasq*):
#!/bin/sh

# disable the local dhcpd server if dnsmasq is to be used as dhcp server
/etc/init.d/dhcpd.sh stop

if [ -f /var/run/dnsmasq.pid ] ; then
  kill -9 `cat /var/run/dnsmasq.pid`
  rm -f /var/run/dnsmasq.pid
fi

sleep 2
/opt/sbin/dnsmasq
I have created a share System with subfolder etc pointing to /share/System/etc. /opt/etc/dnsmasq.conf
conf-file=/share/System/etc/dnsmasq.conf
/share/System/etc/dnsmasq.conf
################################################################################
## server settings

# The DHCP server needs somewhere on disk to keep its lease database.
# This defaults to a sane location, but if you want to change it, use
# the line below.
dhcp-leasefile=/tmp/dnsmasq.leases

# If you don't want dnsmasq to read /etc/resolv.conf or any other
# file, getting its servers from this file instead (see below), then
# uncomment this.
# CAREFUL: this willprevent dnsmasq to query exteral dns servers.
#no-resolv

# If you don't want dnsmasq to poll /etc/resolv.conf or other resolv
# files for changes and re-read them then uncomment this.
no-poll

# The following two options make you a better netizen, since they
# tell dnsmasq to filter out queries which the public DNS cannot
# answer, and which load the servers (especially the root servers)
# uneccessarily. If you have a dial-on-demand link they also stop
# these requests from bringing up the link uneccessarily. 

# Never forward plain names (without a dot or domain part)
domain-needed
# Never forward addresses in the non-routed address spaces.
bogus-priv
 
# By  default,  dnsmasq  will  send queries to any of the upstream
# servers it knows about and tries to favour servers to are  known
# to  be  up.  Uncommenting this forces dnsmasq to try each query
# with  each  server  strictly  in  the  order  they   appear   in
# /etc/resolv.conf
#strict-order

# Set this (and domain: see below) if you want to have a domain
# automatically added to simple names in a hosts-file.
#expand-hosts

# Set the NTP time server addresses to 192.168.0.4 and 10.10.0.5
dhcp-option=option:ntp-server,92.42.186.250,192.33.96.102,195.216.64.208

# Set the NIS domain name 
#dhcp-option=40,wundinet

# Disable NetBIOS over TCP/IP
# NOTE: this will probably cause problems for clients < Win2K
dhcp-option=43,01:04:00:00:00:02

################################################################################
## domain settings

# Set the domain for dnsmasq. this is optional, but if it is set, it# Add 
# local-only domains here, queries in these domains are answered
# from /etc/hosts or DHCP only.
# TODO: not sure if this is needed
local=/example.com/

# does the following things.
# 1) Allows DHCP hosts to have fully qualified domain names, as long
#     as the domain part matches this setting.
# 2) Sets the "domain" DHCP option thereby potentially setting the
#    domain of all systems configured by DHCP
# 3) Provides the domain part for "expand-hosts"
domain=example.com

# Uncomment this to enable the integrated DHCP server, you need
# to supply the range of addresses available for lease and optionally
# a lease time. If you have more than one network, you will need to
# repeat this for each network on which you want to supply DHCP
# service.
dhcp-range=10.0.0.150,10.0.0.180,12h

# Specify a subnet which can't be used for dynamic address allocation,
# is available for hosts with matching --dhcp-host lines. Note that
# dhcp-host declarations will be ignored unless there is a dhcp-range
# of some type for the subnet in question.
# In this case the netmask is implied (it comes from the network
# configuration on the machine running dnsmasq) it is possible to give 
# an explict netmask instead.
dhcp-range=10.0.0.0,static

# Override the default route supplied by dnsmasq, which assumes the
# router is the same machine as the one running dnsmasq.
dhcp-option=3,10.0.0.1

# Do the same thing, but using the option name
dhcp-option=option:router,10.0.0.1
dhcp-option=option:dns-server,10.0.0.48,10.0.0.1
dhcp-option=option:domain-name,example.com

# Send RFC-3397 DNS domain search DHCP option. WARNING: Your DHCP client
# probably doesn't support this......
dhcp-option=option:domain-search,example.com

# Set the DHCP server to authoritative mode. In this mode it will barge in
# and take over the lease for any client which broadcasts on the network,
# whether it has a record of the lease or not. This avoids long timeouts
# when a machine wakes up on a new network. DO NOT enable this if there's
# the slighest chance that you might end up accidentally configuring a DHCP
# server for your campus/company accidentally. The ISC server uses
# the same option, and this URL provides more information:
# http://www.isc.org/index.pl?/sw/dhcp/authoritative.php
dhcp-authoritative
/share/System/etc/dnsmasq.hosts.conf
################################################################################
# cannonical names
cname=www.example.com,infra3.example.com
 
## hosts with static ips
address=/splatter.example.com/10.0.0.33
ptr-record=33.0.0.10.in-addr.arpa,splatter.example.com
dhcp-host=00:17:9a:7b:c2:02,splatter.example.com,10.0.0.33,infinite
Substitute example.com with your own domain and 10.0.0.* with your ip range in the various config files.

Time to restart theservice
/opt/etc/init.d/S??*dnsmasq*
(the dnsmasq init script should really be named /opt/etc/init.d/S200dnsmasq.sh by now, this will make automatic start after reboot possible).

If you want to be 100% sure that Qnap's own dhcp server is not bindingthe port you want to use with  dnsmasq as dhcp server, use the following startup script (/opt/etc/init.s/S200dnsmasq):
#!/bin/sh

# disable the local dhcpd server
/etc/init.d/dhcpd.sh stop

if [ -f /var/run/dnsmasq.pid ] ; then
  kill -9 `cat /var/run/dnsmasq.pid`
  rm -f /var/run/dnsmasq.pid
fi

sleep 2
/opt/sbin/dnsmasq
.

9 Aug 2010

selecting text in vim from an xterm

I have line numbers turned on in vim. this makes selecting text awkward, since the line numbers are selected and copied too.

Fortunately, there is a helpful vimrc setting as long as you are using some sort of xterm:

set number
set mouse=a
Left-clicking and dragging in a terminal in which vim is running will result in text a only selection.