Archive for the 'Linux' Category

Easy subdomains with mod_rewrite

Because I like to play with a lot of projects and code, I create subdomains for felixdv.com almost every day. Instead of going through the hassle of creating a CNAME or A record for every subdomain, creating an Apache vhost file and enabling it and creating the necessary directory structure, I figured there’s an easier way to do so.

First thing to do is to create an *.example.com wildcard record for your domain. This way, you don’t need to create a DNS record every time you need a new subdomain. When this is done, check for the record for example using dig, and you should see something like this:

$ dig *.example.com
# .. skipping some output
;; ANSWER SECTION:
*.example.com.          3600    IN      A       192.0.32.10

Once that’s done and it’s pointing to your server, we need to create an Apache vhost which handles the requests with mod_rewrite, which is a rules-based url rewriting module for apache, and can easily handle different subdomains and actions based on the subdomain.

We’re assuming you’re replacing the domain name, IP address and paths with your own, and you have a directory structure like this:

/var/www/example.com/
/var/www/example.com/subdomains/
/var/www/example.com/subdomains/www/
/var/www/example.com/subdomains/<any subdomain>/

Apache vhost (stripped version):

<VirtualHost 192.0.32.10:80>
        ServerName example.com
        ServerAlias *.example.com
        DocumentRoot "/var/www/example.com"
 
        RewriteEngine on
        # Rewrite everything for example.com to the
        # subdomains/www/ subdirectory
        RewriteCond %{HTTP_HOST} ^example\.com
        RewriteRule ^(.*) /subdomains/www/$1 [L]
 
        # Rewrite all other example.com subdomains
        # to their own directory
        RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com
        RewriteCond /var/www/example.com/subdomains/%1 -d
        RewriteRule ^(.*) /subdomains/%1/$1 [L]
</VirtualHost>

This way, you can just create a directory like /var/www/example.com/subdomains/blog/ to make http://blog.example.com work. Easy like that!

Fosdem 2008

I'm going to FOSDEM, the Free and Open Source Software Developers' European Meeting

iTunes music sharing in Linux with Banshee and DAAP

This one is actually much simpler than I’ve anticipated. It’s what needs to be done if you are on Linux and want to share your music with the ‘Bonjour’ protocol from Apple, to make your music appear on another computer in iTunes as shared music. You don’t need the iTunes application for sharing, a music player / manager supporting the DAAP protocol will do just fine. In this example, I use the Banshee music player for gnome under Ubuntu 7.10.

It basically comes down to this line:

# apt-get install banshee banshee-daap

Then, go to the Edit -> Plugins menu item, set the ‘Music Sharing’ plugin active and have a look at the settings of the plugin to see how it can be configured (Not much to it, though).

About attaching and detaching Xen domU mounts

If you ever need to replace a mount of a Xen domU guest without rebooting the server, you can’t just remount it and be done. In my case, I needed to increase the size of the swap file a certain DomU was using.

Independent from mounting or unmounting a partition inside the DomU, Xen saves its virtual device information inside a logical structure in an information storage called the XenStore. So to change partition information without rebooting the image, you need to inform Xen that you’ll be changing things, and want to detach the virtual device (in our case, a swap file) from the DomU. This is the way:

In the domU that needs to be changed, turn off the swap space:
# swapoff /dev/sda2

Then, in Dom0, list the domains for an overview:
xen1:~# xm list
Name ID Mem(MiB) VCPUs State Time(s)
myVM 17 2000 1 -b---- 134487.4

List the virtual block devices that are coupled to the specified domain:
xen1:~# xm block-list 17
Vdev BE handle state evt-ch ring-ref BE-path
2049 0 0 4 6 8 /local/domain/0/backend/vbd/17/2049
2050 0 0 4 7 9 /local/domain/0/backend/vbd/17/2050

Read what vdev is coupled to what device name inside the domU:
xen1:~# xenstore-read /local/domain/0/backend/vbd/17/2050/dev
sda2

Detach the swap partition:
xen1:~# xm block-detach 17 2050

Create the new swap space (to create a 1G swap file):
xen1:~# dd if=/dev/zero of=/xen/swap/myVM.swap bs=1024k count=100

Attach the new swap partition back to the domain:
xen1:~# xm block-attach 17 file:/xen/swap/myVM.swap sda2 w

Then, with this in place, go back to the domU console, create the swap filesystem and activate it:
# mkswap /dev/sda2
# swapon /dev/sda2

That’s about it for updating a mount point inside the DomU. This can be done for every block device that a Xen DomU may use, as long as it isn’t system-critical, such as the root filesystem).

This procedure also makes use of some lesser known commands such as xm block-attach, xm block-detach and xenstore-read.
Although there isn’t much documentation about these commands, they are worth checking out in case you would need them.