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.

26 Jun 2010

Unix like window management under MacOSX

Keep Afloat adds sticky windows, always on top and transparency on OSX.

9 May 2010

ERROR: language "plpgsql" does not exist

A freshly installed PostgreSQL 8.3 on OSX (Snow Leopard) via MacPorts would not accept the plpgsql language, this can be fixed easily:

-- Create a function handler:
CREATE FUNCTION plpgsql_call_handler() RETURNS language_handler AS '$libdir/plpgsql' LANGUAGE C;

-- Create the language 'plpgsql'
CREATE TRUSTED LANGUAGE plpgsql
HANDLER "plpgsql_call_handler";

-- and I don't know if is a good thing to :
GRANT USAGE ON LANGUAGE plpgsql TO public;

Start/Stop/Restart PostgreSQL 8.3

Installed PostgreSQL 8.3 with MacPorts (Snow Leopard). This script wirl make it easy to start/stop/reload your local PostgreSQL server:

#!/bin/bash

echo "You need sudo rights to perform this action ..."

plist="/Library/LaunchDaemons/org.macports.postgresql83-server.plist"
launchctl=$(which launchctl) # if needed, set this to an absolute path

if [[ -z "$1" ]]; then
echo "Usage $(basename $0) start|stop|reload"
exit 1
fi

mode="load"

if [[ "$1" == "start" ]]; then mode="load";
elif [[ "$1" == "stop" ]]; then mode="unload";
elif [[ "$1" == "reload" ]]; then mode="reload";
else
echo "Unknown mode '$1'";
exit 2;
fi

if [[ "$mode" == "reload" ]]; then
exit $(sudo $launchctl unload -w "$plist" && \
sudo $launchctl load -w "$plist")
fi

sudo $launchctl "$mode" -w "$plist"

PostgreSQL 8.3 on OSX with MacPorts

Installing PostgreSQL on OSX Snow Leopard is pretty sinple, just install MacPorts and the issue the command:

$ sudo port install postgresql83 +universal


Start the server with:
$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.postgresql83-server.plist


Stop the Server with:
sudo launchctl unload -w /Library/LaunchDaemons/org.macports.postgresql83-server.plist

Use PgAdmin III for getting started and administering your databases.

PostgreSQL 8.3 Table with last insterted and creation date

This is a template for PostgreSQL 8.3 which includes

  • «id» Primary Key as AUTO_INCREMENT
  • «ts_insert» date/time of creation of this record
  • «ts_lastmod» date/time of last modification of this record
/*
Template with auto increment id, last modified and insert date

NOTE: replace
«$tbl» with your table name and
«$owner» with your owning user

$Id$
*/

-- Function: update_lastmodified_column()

-- DROP FUNCTION update_lastmodified_column();

CREATE OR REPLACE FUNCTION update_lastmodified_column()
RETURNS trigger AS
$BODY$
BEGIN
NEW.ts_lastmod = NOW();
RETURN NEW;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100;
ALTER FUNCTION update_lastmodified_column() OWNER TO $owner;


-- Table: $tbl

-- DROP TABLE $tbl;

CREATE TABLE $tbl
(
id serial NOT NULL,
ts_insert timestamp with time zone DEFAULT now(),
ts_lastmod timestamp with time zone DEFAULT now(),
CONSTRAINT $tbl_id PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE $tbl OWNER TO $owner;

-- Trigger: update_lastmodified_modtime on $tbl

-- DROP TRIGGER update_lastmodified_modtime ON $tbl;

CREATE TRIGGER update_lastmodified_modtime
BEFORE UPDATE
ON $tbl
FOR EACH ROW
EXECUTE PROCEDURE update_lastmodified_column();

24 Apr 2010

postgresql 8 with macports (snow leopard)

Installing postgresql 8.3 on OSX Snow Leopard with MacsPorts (PgAdmin III binary for OSX).

Install

$ sudo port install \
postgresql83 \
postgresql83-server +universal

create first database

$ sudo mkdir -p /opt/local/var/db/postgresql83/defaultdb
$ sudo chown postgres:postgres /opt/local/var/db/postgresql83/defaultdb
$ sudo su postgres
sh-3.2$ cd /opt/local/lib/postgresql83/
sh-3.2$ ./bin/initdb -D /opt/local/var/db/postgresql83/defaultdb

Output
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locales
COLLATE: C
CTYPE: UTF-8
MESSAGES: C
MONETARY: C
NUMERIC: C
TIME: C
The default database encoding has accordingly been set to UTF8.
initdb: could not find suitable text search configuration for locale UTF-8
The default text search configuration will be set to "simple".

fixing permissions on existing directory /opt/local/var/db/postgresql83/defaultdb ... ok
creating subdirectories ... ok
selecting default max_connections ... 20
selecting default shared_buffers/max_fsm_pages ... 1600kB/20000
creating configuration files ... ok
creating template1 database in /opt/local/var/db/postgresql83/defaultdb/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb.

Success. You can now start the database server using:

./bin/postgres -D /opt/local/var/db/postgresql83/defaultdb
or
./bin/pg_ctl -D /opt/local/var/db/postgresql83/defaultdb -l logfile start


Restart the service

(in case it is already running)
$ sudo launchctl unload -w /Library/LaunchDaemons/org.macs.postgresql83-server.plist
$ sudo launchctl load -w /Library/LaunchDaemons/org.macs.postgresql83-server.plist

8 Mar 2010

Ubisoft and their newest DRM at work

The new DRM scheme Ubisoft introduced in «Assassin’s Creed II», «Silent Hunter 5» and «Settlers 7» was not well received by many potential customers. All 3 games are offline games, but they will only work with a permanent online connection.

Savegames, for example, are backedup to ubi servers, but there seem to be permanent online checks going on to see if the game is «genuine». The game will pause if the online connection is lost (in an offline game, WTF). Many internet connections are quiet unreliable (since the introduction of the so called "Unlimmited" contracts).

Critics of this system have pointed out, that this will introduce a single point of failure. Ubisoft was quiet confident that their system is stable. Turns out it's not. Today the authentication servers went down. Ubisoft first reported they are working on the problem and now state (via twitter) that they are victims of a DDoS attack. Whatever is true seems to be irrelevant from the POV of a customer.

Ubisoft has put in quiet a lot of money to get this DRM stuff running (extra development, running distributed servers which are prone to a high load [peak times] and all the attacks which are run against any internet connected server) and it is only a matter of days now until the finished crack will be released. Circumventing the DRM might be legal in many (if not all) all European countries.

Punishing paying customers with this crap is not acceptable. Downloading a (so called) pirated copy is:

  • more convenient (fire up bittorrent, download and play)
  • no check if the CD is in the drive or if some servers are available
  • cheaper
Buying such product involves:
  • go to store (most people still buy a box)
  • install
  • find no-cd/no-drm crack
  • install crack
Downloading illigally (although I do not recommend it) is just plain more convenient. I guess that the young ones will not have the money to buy the game anyway (probably the customers which would make up the highest volume in sales). So they eighter download or let it be. Guess what they do? The downloaded Version has the DRM already stripped. The publisher doesn't get the money anyway. So why punish the legit customer with this is beyond my understanding.

I, for one, have decided long ago not to play any of these games, although I would have bought all of them.

15 Feb 2010

Ubuntu default settings

The first time I used Ubuntu (Version 7 something) it just felt right out of the box. Nowadays, I will have to set quiet some config values after setting up a new one. Especially the default gnome configuration sucks big hairy monkey balls!

Some helpful default settings (for me) are:

# $Id$
# set up sane gnome config on ubuntu.

# update-notifier should stay in tray (pops up anooyingly if it detects updates)
gconftool -s --type bool /apps/update-notifier/auto_launch false

# enter paths manually in nautilus if needed
gconftool -s --type bool /apps/nautilus/preferences/always_use_location_entry true

# we want a browser, no fucking new window on every click
gconftool -s --type bool /apps/nautilus/preferences/always_use_browser true

# Delete baby, i want to delete, not move. if i want to move i use move FTW!
gconftool -s --type bool /apps/nautilus/preferences/enable_delete true

# show unix permissions in nautilus permission dalogs, not this mikey mouse ui
gconftool -s --type bool /apps/nautilus/preferences/show_advanced_permissions true

# default view should be listing in nautilus
gconftool -s --type string /apps/nautilus/preferences/default_folder_viewer "list_view"

# no text below toolbar icons, waste of scree nreal estate
gconftool -s --type string /desktop/gnome/interface/toolbar_style "icons"

# display unix permission, owner and group
gconftool-2 --set --type list --list-type=string /apps/nautilus/list_view/default_visible_columns \
[name,size,type,date_modified,permissions,owner,group]

# don't open inserted media in nautilus
gconftool -s --type bool /apps/nautilus/preferences/media_automount_open false

# do not confirm delete
gconftool -s --type bool  /apps/nautilus/preferences/confirm_trash false

29 Jan 2010

Civilization -- Freeciv as Browser Game (canvas / SVG)

Yay the joy, I have just discovered that Freeciv is now available as SVG / canvas game.

A browser game with reasonable performance (when using a modern browser) and this game is using open web standards.

From some tests ... FF 3.x on a fast computer is pretty laggy, but FF 3.5+ is very responsive. it feels like an X11 app over a not so fast connection. I am sure this will improve once the javascript implementations gain more performance and the canvas tag matures. After some webgames, this technology seems to be much more capable for games than html, css and javascript alone. Maybe a mix of canvas and html could bring some performance boost.

Freeciv.net seems to perform well on safari, chrome (et all), opera and firefox (< 3.5 recommended) but does not work well on IE8 (unusable, older IE versions are worse).

NOTE:
Chrome and Chromium users can use the --kiosk switch to start the browser in fullscreen mode (not well documented), example:
$ chromium --kiosk http://freeciv.net

Unfortunately on my gnome installation I cannot switch to other windows while chromium is running in fullscreen mode :-/









WTF, why can't I unmount a volume?

Sometimes some file locks are preventing unmounting a block devices (especially annoying on removable devices).

Some nice utilities are fuser (list processes using files, sockes or fs) and lsof (list open files).

This combined with kill gets those external block devices quickly unmounted when needed

packing several unix files into one executable shell script (simple installer)

These 2 scripts are able to pack a directory into a shell script. The shell script can then be executed on another system and will extract the packed files into a target directory, unix permissions will be preserved (tar is used).

What you need:
- sh
- readlink
- dirname
- basename
- tar
- gzip
- uuencode
- uudecode
- cat
- chmod

pack.sh

#!/bin/sh

# $1 is source directory or file
if [ -z "$1" -o ! -e "$1" ]; then
echo "Usage: $0 <target-directory-or-file> <destination-file>"
exit 1
fi

# $2 is dst file
if [ -z "$2" ]; then
echo "Usage: $0 <target-directory-or-file> <destination-file>"
exit 1
fi

# Absolute path to this script. /home/user/bin/foo.sh
SCRIPT=$(readlink -f $0)
# Absolute path this script is in. /home/user/bin
SCRIPTPATH=`dirname $SCRIPT`

WORKINGDIR="$1"
TARGET="."
if [ -f "$WORKINGDIR" ]; then
WORKINGDIR=`dirname $WORKINGDIR`
TARGET=`basename $1`
fi

# pack directory or files into a uuencoded tar.gz archive
cat "$SCRIPTPATH/execute.sh" > "$2"
echo "w $WORKINGDIR $TARGET"
tar -c -C "$WORKINGDIR" -zf - "$TARGET" | \
uuencode "embedded-files.tar.gz" >> "$2"

chmod u+x,g+x "$2"


execute.sh
#!/bin/sh

# $1 is target directory
if [ -z "$1" ]; then
echo "Usage: $0 <target-directory>"
exit 1
fi

# create target directory (if not exists) or exit if we fail
mkdir -p "$1"
if [ $? != 0 ]; then
echo "Failed to create target directory"
exit 3
fi

# Absolute path to this script. /home/user/bin/foo.sh
SCRIPT=$(readlink -f $0)
# Absolute path this script is in. /home/user/bin
SCRIPTPATH=`dirname $SCRIPT`

cd $1;
uudecode "$SCRIPTPATH/$(basename $0)"
tar -xzf "embedded-files.tar.gz"
rm "embedded-files.tar.gz"

exit 0;

25 Dec 2009

Fiddling with bridges for kvm on ubuntu (9.04)

For most virtualization technologies, bridged network interfaces are usefull if a host has to be accessible from outside (ssh for example). Bridging makes this possible.

Install bridge-utils

Disable your primary network interface, create a bridge and link the bridge to the original interface:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet manual
#    address 10.0.0.33
#    network 10.0.0.0
#    netmask 255.255.255.0
#    gateway 10.0.0.1

#auto eth1
iface eth1 inet dhcp

auto vnet0
iface vnet0 inet static
bridge_ports eth0
bridge_stp off
bridge_fd 0
bridge_maxwait 0
address 10.0.0.33
network 10.0.0.0
netmask 255.255.255.0
gateway 10.0.0.1

22 Dec 2009

ssh keyboard-interactive with no subsequent questions

Some scripts which bootstrap clients into my network need to do many subsequent ssh requests to servers to determine its state and change server values (dns, dhcp, accounts, etc). It can be cumbersome to enter the admin's ssh password again and again, sshpass to the rescue (bash example):

#!/bin/bash
# ask for root password, use it for any further ssh connection
remoteuser='adm' # could be root|admin|sysadmin|etc.
stty -echo
read -p 'Password for '$remoteuser': ' pw
echo
stty echo
export SSHPASS="$pw" # export, so subcommands can use it
unset pw
alias ssh='sshpass -e ssh'

ssh $remoteuser@someserver  'ls -la /tmp' # no password asked if it matches
ssh $remoteuser@otherserver 'ls -la /tmp' # no password asked if it matches

When the script ends the variable «$SSHPASS» will be destroyed. It makes sense to have user and passwords stored locally (ie. ldap/kerberos/nis/etc.).