Linux Support, Hosting & Security Hertfordshire

Tuesday, 3 August 2010

Google Font API

I stumbled across this great informative post which gives a nice outline of Google's new Font API. I think most web developers will agree that this could be a game changer.

snip...

For years, we’ve been stuck with the same old fonts: Arial, Georgia, Verdana, Times New Roman — web-safe fonts that a majority of web users have installed on their computer. But lately, the web design community is abuzz — and the source of the excitement is around web fonts. "Web fonts" is a generic term that refers to the method of serving font files — the same type of files you have installed on your computer — to your website visitors so that you can guarantee they’ll have the appropriate type faces you want displayed on your web pages.

...end snip

Read the full article here.

Tuesday, 13 July 2010

HOWTO: Mount KVM/Xen virtual disk image outside guest OS

Problem

Mounting a virtual disk image used by KVM or Xen (.xm file) outside of the guest OS. (e.g on the Host/Dom0 machine)

Background

These images can not be mounted directly as they are not just a formatted partition, but a complete disk image with a boot sector. So if you try to just mount the file it will fail.

Solution

losetup to the rescue! We just need to mount it with losetup, specifying an offset to get past the bootstrap. This way we can also get to different partitions within the disk image.

Steps outlined

  1. Make sure the disk is not in use by the virtual machine! If its a LVM partition then you can take a LVM snapshot of the disk and mount the snapshot.
  2. Mount the virtual image to an lo device using an offset. If your wanting to mount the first partition in the virtual disk, then in most cases the offset will be 32256. If that doesn't work OR its not the first partition then read the instructions for calculating the offset. and use that value instead.
  3. Mount the lo device

Bash commands to mount the image

sudo losetup -o32256 /dev/loop0 /path/to/diskimage.xm
sudo mount /dev/loop0 /mnt/tmp

Clean up when your finished

sudo umount /dev/loop0
sudo losetup -d /dev/loop0

 

 

Calculate the offset steps outlined

If the above did not work, then it maybe because the offset ive suggested (32256) is not correct for your setup. The following shows how to calculate the offset for your setup.

  1. Make sure loop0 is free
  2. Mount image to loop0 without an offset
  3. Use fdisk to list partitions
  4. Find the start number of the partition you want to mount. Multiply that it by the number of bytes per sector (normally 512)

Calculate the offset Bash commands

sudo losetup -d /dev/loop0
sudo losetup /dev/loop0 /path/to/diskimage.xm
sudo fdisk -ul /dev/loop0

Look for the "Units = " value and Start value for the partition you wish to mount. In this example its "512 bytes" and "63". The output should show something like;

Disk /dev/loop0: 10.5 GB, 10485760000 bytes                                                                                                                                                                                                                                    
255 heads, 63 sectors/track, 1274 cylinders, total 20480000 sectors                                                                                                                                                                                                            
Units = sectors of 1 * 512 = 512 bytes                                                                                                                                                                                                                                         
Disk identifier: 0x0007b39a                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                               
      Device Boot      Start         End      Blocks   Id  System                                                                                                                                                                                                              
/dev/loop0p1   *          63    20466809    10233373+  83  Linux

Now multiply those two values to get your offset. (e.g. In this example its 512x63=32256)

Now unmount your lo device

sudo losetup -d /dev/loop0

...and start this HOWTO again but using your calculated offset value instead of 32256.

Did this post help you? Thank us with bitcoin;

Sunday, 18 April 2010

Ubuntu Karmic|Lucid Pure-ftpd hangs when using TLS

Ubuntu Karmic (9.10) and Lucid (10.04) provide pure-ftpd version 1.0.22-1 and 1.0.24-1 by default. These versions of pureftp have a known issue with some FTP clients which cause the LIST command to stall/hang when using TLS. We noticed this with the OSX transmit and coda clients, but ubuntu bugs also shows users also having issues with the Cyberduck FTP client. See ubuntu bug #499854

Upgrading to version 1.0.28-2 resolves this issue. We used the pure-ftpd Debian sid packages successfully and without issue.

In our case we were using the 64bit pure-ftpd-mysql package, however this issue is also known to effect pure-ftpd-ldap

Steps outlined

  1. Remove current pure-ftpd install
  2. Install the required dependencies from ubuntu
  3. Upgrade the openssl package to the required version
  4. Download and install the Debian pure-ftpd packages

Example Commands for amd64 pure-ftpd-mysql packages

/etc/init.d/pure-ftpd-mysql stop
aptitude remove pure-ftpd-mysql
aptitude install libmysqlclient15off openbsd-inetd update-inetd
wget http://ftp.de.debian.org/debian/pool/main/p/pure-ftpd/pure-ftpd-common_1.0.28-2_all.deb
wget http://ftp.de.debian.org/debian/pool/main/p/pure-ftpd/pure-ftpd-mysql_1.0.28-2+b1_amd64.deb
wget http://ftp.de.debian.org/debian/pool/main/o/openssl/libssl0.9.8_0.9.8n-1_amd64.deb
dpkg -i *.deb

Did this post help you? Thank us with bitcoin;

Wednesday, 14 April 2010

BASH/SH Random String/Password Generator

Need to generate a random a-zA-Z0-9 string in bash or sh? Well this little function should help.

You can passover the string length or leave it blank for an 8 character string.

Code

function randompass () {
        local randompassLength
        if [ $1 ]; then
                randompassLength=$1
        else
                randompassLength=8
        fi

        pass=</dev/urandom tr -dc A-Za-z0-9 | head -c $randompassLength
        echo $pass
}

Example Usage

#!/bin/bash
#

function randompass () {
        local randompassLength
        if [ $1 ]; then
                randompassLength=$1
        else
                randompassLength=8
        fi

        pass=</dev/urandom tr -dc A-Za-z0-9 | head -c $randompassLength
        echo $pass
}

fourChrString=`randompass 4`
eightChrString=`randompass`

echo "4 character random string : $fourChrString"
echo "8 character random string : $eightChrString"

Example Output

4 character random string : pnoc
8 character random string : c110EM21

Did this post help you? Thank us with bitcoin;

Thursday, 11 March 2010

PHP: Insert an array into the middle of an array

Array into array

Ever needed to insert an array of items into the middle or at specific position in another php array?
Well this function may help; Download

CODE

<?php

$fruits = array("apple", "pear", "orange", "plum");
$insert = array("grape", "peach");

var_dump($fruits);
var_dump($insert);


array_insert($fruits, $insert, 2);

var_dump($fruits);



function array_insert(&$array, $insert, $position) {
settype($array, "array");
settype($insert, "array");
settype($position, "int");

//if pos is start, just merge them
if($position==0) {
    $array = array_merge($insert, $array);
} else {


    //if pos is end just merge them
    if($position >= (count($array)-1)) {
        $array = array_merge($array, $insert);
    } else {
        //split into head and tail, then merge head+inserted bit+tail
        $head = array_slice($array, 0, $position);
        $tail = array_slice($array, $position);
        $array = array_merge($head, $insert, $tail);
    }
}
}

?>

OUTPUT

array
0 => string 'apple' (length=5)
1 => string 'pear' (length=4)
2 => string 'orange' (length=6)
3 => string 'plum' (length=4)

array
0 => string 'grape' (length=5)
1 => string 'peach' (length=5)

array
0 => string 'apple' (length=5)
1 => string 'pear' (length=4)
2 => string 'grape' (length=5)
3 => string 'peach' (length=5)
4 => string 'orange' (length=6)
5 => string 'plum' (length=4)


One item into array

If you just need to insert one element, then as @Ryan pointed out, you can use array_splice.

$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input is now array("red", "green", "blue", "purple", "yellow");

Did this post help you? Thank us with bitcoin;

Thursday, 4 February 2010

Using PHP sessions in a clustered HA apache setup

So you have a cluster of two or more apache servers and want your php sessions to be shared between the different apache servers?

One great solution is to use memcache. It's a Free & open source, high-performance, distributed memory object caching system and can support the mirroring of sessions to multiple memcache servers (which could be on each apache server). Also your sessions are stored in RAM so they are nice and quick too!

This blog post has a nice setup guide. php session clustering with memcache

More info on memcache.

Did this post help you? Thank us with bitcoin;