delete files

May 10th, 2012

Use xargs to remove files from list of names:

1
xarg rm <files.txt

Reference: http://www.unix.com/shell-programming-scripting/36070-read-list-filenames-text-file-remove-these-files-multiple-directories.html

automatically mount zfsonlinux file systems during startup

May 8th, 2012

Change /etc/default/zfs:

1
2
3
4
5
# Automatically run `zfs mount -a` at system startup if set non-empty.
ZFS_MOUNT='yes'
 
# Automatically run `zfs unmount -a` at system shutdown if set non-empty.
ZFS_UNMOUNT='yes'

Add zfs-mount to runlevels:

1
update-rc.d zfs-mount defaults

nfs4 and all_squash

May 6th, 2012

all_squash does not work when directory/share is on the same filesystem. Example:

/etc/exports:

1
2
/media/mount 192.168.0.0/24(rw,async,no_subtree_check,fsid=0)
/media/mount/folder 192.168.0.0/24(all_squash,anonuid=1000,anongid=100,rw,async,no_subtree_check)

We have to mount folder with a bind to get this working. Move folder to new location, here I’m hiding it:

1
2
mv /media/mount/folder /media/mount/.folder
mkdir /media/mount/folder

/etc/fstab:

1
2
/dev/sdb1 /media/mount ext4 defaults 0 0
/media/mount/.folder /media/mount/folder none bind 0 0

Mount and reload nfs exports:

1
2
mount /media/mount/folder
exportfs -r

Reference: http://www.spinics.net/lists/linux-nfs/msg16593.html

Mount partclone image on Ubuntu 12.04 LTS

May 1st, 2012

Install partclone-utils from here: http://www.idealworldinc.com/partclone-utils/

1
sudo dpkg -i partclone-utils_0.1-1_i386.deb

Install nbd-client

1
sudo sudo apt-get install nbd-client

Assign network block device to partclone image

1
sudo imagemount -d /dev/nbd0 -f sda1-partclone.img

Mount

1
sudo mount /dev/nbd0 /media/temp

nVidia HDMI audio

March 25th, 2012

Check which hardware devices are avaiable:

1
sudo aplay -L

Check which device monitor is connected to:

1
2
3
4
speaker-test -c 2 -r 48000 -D hw:1,3
speaker-test -c 2 -r 48000 -D hw:1,7
speaker-test -c 2 -r 48000 -D hw:1,8
speaker-test -c 2 -r 48000 -D hw:1,9

Pulseaudio automatically loads first hardware device(0,3 or 1,3). Manually load another in /etc/pulse/default.pa:

1
load-module module-alsa-sink device=hw:1,7

Resource: ftp://download.nvidia.com/XFree86/gpu-hdmi-audio-document/gpu-hdmi-audio.html

roundcube, sqlite2, ubuntu 11.10 and cherokee

February 15th, 2012

Roundcube only supports sqlite2(RC v0.7.1). Unfortunately Ubuntu 11.10 doesn’t have sqlite2 available in the repos(not sure why, security?), and MySQL/PostgreSQL seems like an overkill for a single user roundcube installation.

Fix: install php5-sqlite and get sqlite2 from natty(11.04). The module sqlite.so is actually loaded by default(/etc/php5/cgi/conf.d/sqlite.ini) but missing in the oneiric package.

1
2
3
4
5
apt-get install php5-sqlite
wget http://security.ubuntu.com/ubuntu/pool/main/p/php5/php5-sqlite_5.3.5-1ubuntu7.7_i386.deb
dpkg -x php5-sqlite_5.3.5-1ubuntu7.7_i386.deb /tmp
cp /tmp/usr/lib/php5/20090626+lfs/sqlite.so /usr/lib/php5/20090626+lfs/
service cherokee restart

Reference: https://bugs.launchpad.net/ubuntu/+source/php5/+bug/875262

Auto update iptables DDWRT

December 15th, 2011

custom script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
wan=`nvram get wan_ipaddr`
last=`cat /tmp/last_wan_ipaddr`
echo "WAN IP: $wan" > /tmp/ipv6forward.log
echo "Last WAN IP: $last" >> /tmp/ipv6forward.log
# Check if IP is updated
if [ "$wan" != "$last" ]; then
  echo "New WAN IP detected." >> /tmp/ipv6forward.log
  # If this is the first time script is adding rules, add a FILTER rule that accept the DNAT FORWARD rule
  if [ -f /tmp/last_wan_ipaddr ]; then
    # Not first time, delete old DNAT
    iptables -t nat -D PREROUTING -j DNAT -d $last -p 41 --to-destination 10.10.10.7 >> /tmp/ipv6forward.log    
  else
    # First time, wait for router to settle (ddwrt will clear iptables on startup)
    sleep 30
    # Remove DROP policy
    iptables -t filter -D FORWARD -j DROP >> /tmp/ipv6forward.log
    # Add DNAT rule
    iptables -t filter -A FORWARD -j ACCEPT -p 41 -d 10.10.10.7 >> /tmp/ipv6forward.log
    # Add DROP polocy (last place)
    iptables -t filter -A FORWARD -j DROP >> /tmp/ipv6forward.log
  fi
  iptables -t nat -A PREROUTING -j DNAT -p 41 -d $wan --to-destination 10.10.10.7 >> /tmp/ipv6forward.log
  # write wan_ipaddr to tmp
  echo $wan > /tmp/last_wan_ipaddr
fi

startup script:

1
2
nvram get rc_custom > /tmp/custom.sh
chmod +x /tmp/custom.sh

cron:

1
* * * * * root /tmp/custom.sh

Timeout never ending command

December 12th, 2011

Sometimes a command never times out by itself. I had this problem with mounting NFS, and when doing it automagically(mount by network location), you get thousands of mount.nfs never timing out.

Fix:
Use timeout from coreutils.

1
timeout 5s mount /path/to/mount/point

Reference: http://stackoverflow.com/questions/687948/timeout-a-command-in-bash-without-unnecessary-delay

XBMC and pulseaudio master volume (Ubuntu 11.10)

October 15th, 2011

If you use XBMC as standalone in Ubuntu(choose it in the login screen), volume control is limited to XBMC as a source in pulseaudio. If you login to Gnome/Unity/whatever, and forget to set sound level to 100% before you logout, XBMC sound volume will suffer from this.

Solution:
You can control sound volume in terminal with

1
pactl set-sink-volume # n%

.
# is a number of a so called sink, which is the same as an output device(analog, HDMI, SPDIF, etc). n% is percentage of volume. This will set sink 0 to 100% volume:

1
pactl set-sink-volume 0 100%

You need to find out which number your default output device is:

1
pactl list sinks

Should output something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Sink #0
	State: RUNNING
	Name: alsa_output.pci-0000_00_1b.0.analog-stereo
	Description: Intern lyd Analog Stereo
	Driver: module-alsa-card.c
	Sample Specification: s16le 2ch 44100Hz
	Channel Map: front-left,front-right
	Owner Module: 4
	Mute: no
	Volume: 0:  90% 1:  90%
	        0: -2,75 dB 1: -2,75 dB
	        balance 0,00
	Base Volume: 100%
	             0,00 dB
...

Play some music and test(music should mute 1 second):

1
pactl set-sink-volume 0 0%; sleep 1; pactl set-sink-volume 0 90%

To get XBMC set volume to 100% before startup we create a new command called xbmc-standalone-max-volume. Do this as root:

1
2
cp /usr/bin/xbmc-standalone /usr/bin/xbmc-standalone-max-volume
nano /usr/bin/xbmc-standalone-max-volume

In xbmc-standalone-max-volume find pulse start section and add three lines after it:

1
2
3
4
5
6
7
8
9
10
11
12
PULSE_START="$(which start-pulseaudio-x11)"
if [ -n "$PULSE_START" ]; then
  $PULSE_START
else
  PULSE_SESSION="$(which pulse-session)"
  if [ -n "$PULSE_SESSION" ]; then
    XBMC="$PULSE_SESSION $XBMC"
  fi
fi
# set volume to 100%
PACTL="$(which pactl)"
$PACTL set-sink-volume 0 100%

Now edit XBMC session:

1
nano /usr/share/xsession/XBMC.desktop

Change Exec and TryExec:

1
2
3
4
5
6
[Desktop Entry]
Name=XBMC
Comment=This session will start XBMC Media Center
Exec=xbmc-standalone-max-volume
TryExec=xbmc-standalone-max-volume
Type=Application

You may also do the same with xbmc -> xbmc-max-volume, but when inside gnome you could simply use your keyboard and Alt-Tab, set volume up, and Alt-Tab back.

Share wired internet connection wireless

July 30th, 2011

This was done with Atheros AR5001 (ath5k, Linux Mint 10, 2.6.35).

Install hostapd and DHCP-server.

1
apt-get install hostapd dhcp3-server

/etc/hostapd/hostapd.conf:

1
2
3
4
5
6
7
8
9
10
interface=wlan0
driver=nl80211
ssid=SSID
hw_mode=g
channel=7
wpa=2
wpa_passphrase=password
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP CCMP
wpa_ptk_rekey=600

/etc/dhcp3/dhcpd.conf:

1
2
3
4
5
subnet 10.10.0.0 netmask 255.255.255.0 {
        range 10.10.0.25 10.10.0.100;
        option domain-name-servers 193.213.112.4, 130.67.15.198;
        option routers 10.10.0.1;
}

Add to /etc/default/hostapd:

1
2
RUN_DAEMON="yes"
DAEMON_CONF="/etc/hostapd/hostapd.conf"

/etc/default/dhcp3-server:

1
INTERFACES="wlan0"

/usr/local/bin/ics:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash
 
case "${1}" in
  start)
	# stop gnome network manager
	service network-manager stop
	# configure wireless IP
	ifconfig wlan0 10.10.0.1 netmask 255.255.255.0 up
	# start AP and DHCP-server
	service hostapd start
	service dhcp3-server start
	# enable NAT
	echo "1" > /proc/sys/net/ipv4/ip_forward
	iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  ;;
  stop)
	# disable NAT
	echo "0" > /proc/sys/net/ipv4/ip_forward
	iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
	# stop DHCP-server and AP
	service dhcp3-server stop
	service hostapd stop
	# start gnome network manager
	service network-manager start
	# wait 1 second and enable scanning on wireless
	sleep 1
	ifconfig wlan0 up
  ;;
esac
 
exit 0

Add with to sudoers(with visudo):

1
2
# Allow admin users to share internet connection
%admin ALL=NOPASSWD:/usr/local/bin/ics

Add shortcut to desktop with command “sudo /usr/local/bin/ics start” and “sudo /usr/local/bin/ics stop”.