Group_concat in T-SQL

September 15th, 2011

You may wish group_concat while working with MS SQL Server. Here’s a solution!

Say you have these tables:
a: id, content
b: id, val
rel_a_b: a_id, b_id

The SQL query

1
2
3
4
5
6
7
8
9
select a.*,
stuff(
(
select cast(';' as varchar(max)) + b.val
from b
inner join rel_a_b on b.id = rel_a_b.b_id and rel_a_b.a_id = a.id
for xml path('')
),1,1,'') as vals_combined
from a

dvbloopback in Linux 2.6.38

August 25th, 2011

Compiling and using sasc-ng and dvbloopback with Linux 2.6.38 is a hassle. Here’s how to get it working.

Prequisites:
Ubuntu Lucid
Kernel 2.6.38-11-generic

We need to patch both the kernel dvb module as well as the dvbloopback source.

Create a patch file named linux-2.6.38-dvb-mutex.patch in your home folder with the following contents:

C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
diff -Nur linux-2.6.38/drivers/media/dvb/dvb-core/dvbdev.c linux-2.6.38/drivers/media/dvb/dvb-core/dvbdev.c
--- linux-2.6.38/drivers/media/dvb/dvb-core/dvbdev.c
+++ linux-2.6.38/drivers/media/dvb/dvb-core/dvbdev.c
@@ -83,8 +83,11 @@ static int dvb_device_open(struct inode *inode, struct file *file)
file->f_op = old_fops;
goto fail;
}
- if(file->f_op->open)
+ if(file->f_op->open) {
+ mutex_unlock(&dvbdev_mutex);
err = file->f_op->open(inode,file);
+ mutex_lock(&dvbdev_mutex);
+ }
if (err) {
fops_put(file->f_op);
file->f_op = fops_get(old_fops);
--

$ sudo apt-get install linux-headers-`uname -r`
Now let’s get the kernel source, patch it, and recompile the dvb-core module.
$ cd /usr/src
$ sudo apt-get source kernel-image-`uname -r`
$ cd linux-2.6.38
$ sudo patch -p1 < ~/linux-2.6.38-dvb-mutex.patch
$ sudo make mrproper
$ sudo cp /boot/config-$(uname -r) .config
$ sudo cp /usr/src/linux-headers-`uname -r`/Module.symvers /usr/src/linux-2.6.38/
$ sudo make oldconfig
$ sudo make prepare
$ sudo make scripts
$ sudo make M=drivers/media/dvb/dvb-core
$ sudo cp /usr/src/linux-2.6.38/drivers/media/dvb/dvb-core/dvb-core.ko /lib/modules/`uname -r`/kernel/drivers/media/dvb/dvb-core/

Next, head to your sasc-ng source directory and apply the following patch

C
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
--- contrib/sasc-ng/dvbloopback/module/dvblb_forward.c 2011-04-25 02:44:10.511089600 +0300
+++ contrib/sasc-ng/dvbloopback/module/dvblb_forward.c 2011-04-24 21:04:17.000000000 +0300
@@ -166,9 +166,9 @@
struct file *ftmp = find_forwardmap(lbdev, f-&gt;private_data);
if (!ftmp || IS_ERR(ftmp))
return -EFAULT;
- if (lbdev-&gt;forward_dev-&gt;fops &amp;&amp;lbdev-&gt;forward_dev-&gt;fops-&gt;ioctl)
- return lbdev-&gt;forward_dev-&gt;fops-&gt;ioctl(
- ftmp-&gt;f_dentry-&gt;d_inode, ftmp, cmd, arg);
+ if (lbdev-&gt;forward_dev-&gt;fops &amp;&amp;lbdev-&gt;forward_dev-&gt;fops-&gt;unlocked_ioctl)
+ return lbdev-&gt;forward_dev-&gt;fops-&gt;unlocked_ioctl(
+ ftmp, cmd, arg);
return -EFAULT;
}</code>
--- contrib/sasc-ng/dvbloopback/module/dvb_loopback.c 2011-04-25 02:44:10.511089600 +0300
+++ contrib/sasc-ng/dvbloopback/module/dvb_loopback.c 2011-04-25 00:42:54.234135688 +0300
@@ -118,9 +118,9 @@
/* This is a copy of dvb_usercopy. We need to do this because it isn't exported
by dvbdev
*/
-static int dvblb_usercopy(struct inode *inode, struct file *file,
+static int dvblb_usercopy(struct file *file,
unsigned int cmd, unsigned long arg,
- int (*func)(struct inode *inode, struct file *file,
+ int (*func)(struct file *file,
unsigned int cmd, void *arg))
{
char sbuf[128];
@@ -180,7 +180,7 @@
}
/* call driver */
- if ((err = func(inode, file, cmd, parg)) == -ENOIOCTLCMD)
+ if ((err = func(file, cmd, parg)) == -ENOIOCTLCMD)
err = -EINVAL;
if (err &lt; 0)
@@ -663,7 +663,7 @@
dvb_generic_ioctl) which is called by dvblb_ioctl for device-0. It is
used to forward ioctl commands back to the userspace application
*/
-static int dvblb_looped_ioctl(struct inode *inode, struct file *f,
+static int dvblb_looped_ioctl(struct file *f,
unsigned int cmd, void *parg)
{
int ret;
@@ -692,7 +692,7 @@
return ret;
}
-static int dvblb_ioctl(struct inode *inode, struct file *f,
+static long dvblb_ioctl(struct file *f,
unsigned int cmd, unsigned long arg)
{
void * parg = (void *)arg;
@@ -723,7 +723,7 @@
if (lbdev-&gt;forward_dev)
return dvblb_forward_ioctl(lbdev, f, cmd, arg);
- return dvblb_usercopy (inode, f, cmd, arg,
+ return dvblb_usercopy (f, cmd, arg,
dvbdev-&gt;kernel_ioctl);
}
/* This is the userspace control device */
@@ -978,7 +978,7 @@
.write = dvblb_write,
.poll = dvblb_poll,
.mmap = dvblb_mmap,
- .ioctl = dvblb_ioctl,
+ .unlocked_ioctl = dvblb_ioctl,
};
static struct dvb_device dvbdev_looped = {
@@ -998,7 +998,7 @@
.write = dvblb_write,
.poll = dvblb_poll,
.mmap = dvblb_mmap,
- .ioctl = dvblb_ioctl,
+ .unlocked_ioctl = dvblb_ioctl,
};
static struct dvb_device dvbdev_userspace = {
--- contrib/sasc-ng/Makefile 2011-04-25 02:44:10.507089818 +0300
+++ contrib/sasc-ng/Makefile 2011-04-25 00:40:31.390323663 +0300
@@ -8,7 +8,7 @@
CC ?= gcc
CXX ?= g++
-CXXFLAGS ?= -Wall -D__user= -Werror
+CXXFLAGS ?= -Wall -D__user= #-Werror
CFLAGS ?= -Wall -D__user=
ifdef DVB_DIR
@@ -17,7 +17,7 @@
endif
DEFINES += -DRELEASE_VERSION=\"$(VERSION)\" -D__KERNEL_STRICT_NAMES
-INCLUDES += -Idvbloopback/module -I/lib/modules/$(shell uname -r)/build/include
+INCLUDES += -Idvbloopback/module -I/usr/include
LBDIR = dvbloopback/src
SCDIR = sc/PLUGINS/src/$(SCVER)
SC_FLAGS = -O2 -fPIC -Wall -Woverloaded-virtual
@@ -57,7 +57,7 @@
INC_DEPS := $(shell ls $(LBDIR)/*.h) dvbloopback/module/dvbloopback.h
INC_DEPS_LB := $(shell ls dvblb_plugins/*.h)
-LIBS = -lpthread -lcrypto -lcrypt
+LIBS = -lpthread -lcrypto -lcrypt -lv4l1
all: $(TOOL) libscanwrap.so
--- contrib/sasc-ng/sc/dvbdevice.cpp 2011-04-25 02:44:10.519089162 +0300
+++ contrib/sasc-ng/sc/dvbdevice.cpp 2011-04-24 21:04:17.000000000 +0300
@@ -10,7 +10,7 @@
#include "include/vdr/dvbdevice.h"
#include
#include -#include +#include #include #include #include diff -u -r systems/constcw/constcw.c systems/constcw/constcw.c
--- systems/constcw/constcw.c 2011-04-25 02:44:10.567086537 +0300
+++ systems/constcw/constcw.c 2011-04-25 02:39:59.527575519 +0300
@@ -70,7 +70,7 @@
bool cPlainKeyConstCw::Matches(const cEcmInfo *ecm)
{
- return ecm-&gt;prgId==prgId &amp;&amp; ecm-&gt;source==source &amp;&amp; ecm-&gt;transponder==transponder;
+ return ecm-&gt;prgId==prgId;
}
bool cPlainKeyConstCw::Parse(const char *line)

I.e. save as sasc-2.6.38.patch in your home directory, cd to the sasc source dir and apply patch with
$ patch -p0 < ~/sasc-2.6.38.patch
Refer to the official sasc-ng installation page for more info http://dolot.kipdola.com/wiki/Install_SASC-NG

Thanks to http://www.vanbest.org/drupal6/content/installing-sasc-ng-linux-2.6.38 and http://www.eurocardsharing.com/f273/ubuntu-11-04-sasc-ng-dvbloopback-348184

Kiting video

July 26th, 2011

Kiting on Vimeo.

Restoring a single database from a MySQL dump

February 21st, 2011

If you, like me, happen to end up with a mysqldump –all-databases dump file and need to restore a single database, here’s how (replace Target with your database name):

1
$ sed -n '/^-- Current Database: `Target`/,/^-- Current Database: `/p' -o Target.sql dump.sql

Note the use of -o instead of output redirection (>), since it’s locale independent.

If you have seem to have a charset mismatch, say UTF-8 – ISO-8859-1 issues, convert the dump using iconv.

1
$ iconv -f UTF-8 -t ISO-8859-1 -c -o Target.iso.sql Target.sql

Extending an LVM volume

July 15th, 2010

The following commands adds a new partition to an existing lvm volume. Tested on Ubuntu 10.04.

First, find out your volume name using: sudo vgdisplay

  1. sudo cfdisk /dev/sda <- create the new partition in free space here. note the partition number (sdaX).
  2. sudo pvcreate /dev/sdaX <- creates a new physical volume with this new partition (X is the partition number)
  3. vgextend VOLUME /dev/sdaX
  4. sudo vgdisplay <- find out the amount of free space in lvm
  5. sudo lvextend -L+1G /dev/mapper/VOLUME-root
  6. sudo resize2fs /dev/mapper/VOLUME-root
  7. df -h

With a little help from: http://serverfault.com/questions/148239/expand-disk-space-on-ubuntu-10-04-vmware-guest

Time lapse

July 1st, 2010

Afatech 9015 in Linux

June 8th, 2010

I recently got a noname USB DVB-T receiver which I’ve been trying to get to work on my Debian GNU/Linux laptop.

While inserting the device, here’s syslog output:

Jun  8 20:11:28 utskottet kernel: [ 5540.744100] usb 1-4: new high speed USB device using ehci_hcd and address 5
Jun  8 20:11:28 utskottet kernel: [ 5540.881274] usb 1-4: New USB device found, idVendor=15a4, idProduct=9016
Jun  8 20:11:28 utskottet kernel: [ 5540.881307] usb 1-4: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Jun  8 20:11:28 utskottet kernel: [ 5540.881316] usb 1-4: Product: DVB-T 2
Jun  8 20:11:28 utskottet kernel: [ 5540.881322] usb 1-4: Manufacturer: Afatech
Jun  8 20:11:28 utskottet kernel: [ 5540.881327] usb 1-4: SerialNumber: 010101010600001
Jun  8 20:11:28 utskottet kernel: [ 5540.881557] usb 1-4: configuration #1 chosen from 1 choice
Jun  8 20:11:28 utskottet kernel: [ 5540.895370] af9015: tuner id:179 not supported, please report!
Jun  8 20:11:38 utskottet kernel: [ 5551.519942] usb 1-4: USB disconnect, address 5

I followed these instructions and now I at least have channel lock. Still struggling to get picture in mplayer..

hg clone -r 0f41fd7df85d http://linuxtv.org/hg/~anttip/af9015/

Patch with this patch, make, copy the firmware, reboot and voilá.

Adding vhosts to Apache, the scripted way

May 25th, 2010

This script adds a directory in /var/www and a vhost file in /etc/apache2/sites-available. Tested on Debian and Ubuntu.


#!/bin/bash

# usage $ sudo ./addvhost.sh mydomain.com

/bin/mkdir /var/www/$1

/bin/echo ‘
<VirtualHost *>
ServerAdmin webmaster@localhost
ServerName ‘$1′
DocumentRoot /var/www/’$1′
ErrorLog /var/log/apache2/’$1′-error.log
LogLevel warn
CustomLog /var/log/apache2/’$1′-access.log combined
ServerSignature On
</VirtualHost>
‘ > /etc/apache2/sites-available/$1

/bin/ln -s /etc/apache2/sites-available/$1 /etc/apache2/sites-enabled/$1

ffmpeg: jpegs to h.264 video

March 28th, 2010

Create stopmotion on Linux?

Resize all jpeg’s in the current folder and rename them sequencially:

counter=0; for file in *jpg; do counter=`expr $counter + 1`; newfile=`printf %04d $counter`; mv $file $newfile.jpg; done; mogrify -resize 1920×1920 -crop 1920×1080+0+200 *jpg

This command will turn all .jpg’s (001.jpg, 002.jpg…) in current directory into an x264 encoded full HD 6 Mbit/s mp4 video.

$ ffmpeg -i %03d.jpg -vcodec libx264 -vpre hq -r 25 -s 1920×1080 -acodec libfaac -ab 96k -b 10M output.mp4

Update: For editing, DNxHD may be more suitable.

$ ffmpeg -i %03d.jpg -vcodec dnxhd -b 10M -an output.mov

Dovecot process gone bezerk

February 24th, 2010

Today I’ve had some serious battles with Dovecot running on a wild cpu time spending spree. I recently made an apt-get upgrade on my Debian system. Somwhere in there things went awkward. An imap process simply wouldn’t stop on /etc/init.d/dovecot stop nor kill -9 [dovecot-pid]

The process took 100% cpu and eventually the whole system, including kernel, froze. Had to cold reboot it via IPMI three times! Thank god for IPMI.. Anyways I found this mailing thread with the same issue.

What seems to have solved to problem for me was to upgrade Debian from lenny to unstable, including the latest Debian kernel (2.6.32.2-686) and Dovecot 1:1.2.10-1 and keeping my fingers crossed while rebooting. I also noticed some complaints for a missing “cpusieve” plugin. Uncommenting plugin = cmusieve in dovecot.conf was necessary.