Archive for the 'linux' Category

Using kexec for a fast reboot in Archlinux

If you ever wondered how some distros achieve the trick of bypassing the BIOS when rebooting, it’s done using something called kexec, which wikipedia defines as:

[…]a mechanism of the Linux kernel that allows “live” booting of a new kernel “over” the currently running kernel. kexec skips the bootloader stage (hardware initialization phase by the firmware or BIOS) and directly loads the new kernel into memory, where it starts executing immediately.

In archlinux, you can achieve this with the package kexec-tools, which you can install as usual with:
pacman -S kexec-tools

kexec-tools is integrated with the archlinux initscripts, so that the kexec command line tool will by default go through the normal shutdown procedure before rebooting. You can find more information as ever on the excellent Archlinux Wiki. There are some scripts there which look pretty useful, but I started using kexec direct from the command line after reading the man page, and then wrote a short script to cover the form I was using each time.



exec kexec --type=bzImage --reuse-cmdline --initrd=$imgname.img $krnname

I saved this as ~/bin/kxreboot. You can either just use this without parameters to boot the default kernel, or you can add a single parameter – for example, if you do kxreboot mainline it will boot vmlinuz26-mainline with initrd kernel26-mainline.img

You might also ask, “Why would I want a fast reboot? I never reboot!”. Fair comment. I run the testing repos, so kernel and udev upgrades come along quite frequently. Also, my motherboard doesn’t recognise my USB keyboard on boot, which makes a BIOS boot a bit of a pain as I have to find a PS/2 keyboard. Last but not least, everyone loves faster, right?


Using LXDE or PCManFM? Desktop vanished?

Some changes in the most recent version may have caused your desktop icons and wallpaper to vanish. As the author notes here this is down to major changes going on behind the scenes.

To get get your desktop back, you need to run “pcmanfm –desktop”. To configure the settings which used to be in the Edit->Preferences menu, right-click on the desktop area and select Properties.

If you use openbox or similar, you’ll need to tick the option in there which causes the window manager’s menu to appear when the desktop is clicked. That then hides the pcmanfm desktop config! Luckily there’s an answer to that. Add an option to your WM menu (e.g. Openbox’s rc.xml) with an entry that calls “pcmanfm –desktop-pref”. This will allow you to edit the settings again.

cal – A command line calendar

If you already know about cal then you can move along, but if not please allow me to share this hidden gem with you.

(I find that the *nix command line is peppered with commands like this which are amazingly useful, and if you come from a *nix background they will be second nature to you. If, like me, you are a refugee from another OS, you may not have found some of them yet.)

With cal, it’s easier to show what it does than to explain it.

[~] $ cal
      May 2010      
Mo Tu We Th Fr Sa Su
                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

cal by itself gives you a calendar for the current month.

[~] $ cal -3
     April 2010             May 2010              June 2010     
Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
          1  2  3  4                  1  2      1  2  3  4  5  6
 5  6  7  8  9 10 11   3  4  5  6  7  8  9   7  8  9 10 11 12 13
12 13 14 15 16 17 18  10 11 12 13 14 15 16  14 15 16 17 18 19 20
19 20 21 22 23 24 25  17 18 19 20 21 22 23  21 22 23 24 25 26 27
26 27 28 29 30        24 25 26 27 28 29 30  28 29 30            

cal -3 gives you a 3 month view.

What if you want a different month? No problem. cal mm yyyy will do it. Note that you need to use four digits for the year!

[~] $ cal 05 2009
      May 2009      
Mo Tu We Th Fr Sa Su
             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

Finally, cal -y will give you a year’s calendar. I won’t show an example here for reasons of space, but just try it for yourself. Naturally, you can do something like cal -y 2011 too.

There are a couple more options, so as always read man cal for the gory details.

Copy and paste across machines with ssh + xsel

This is pretty obvious with hindsight but I only just thought of it!
I often have my work laptop on the desk next to my home desktop, and sometimes for whatever reason I need to copy & paste a URL or similar snippet between the machines.

Firstly, if you’ve never used xsel before, it’s very useful. Pipe something into it, and it becomes the current selection. Use it without input and it outputs the current selection buffer. It has a few more options, so check the man page.


grep foo ~/bar | xsel
xsel < ~/.conkyrc
xsel > ~/saved

Secondly, I strongly recommend that you set up public key authentication and ssh-agent if you haven’t already. It will make your life much easier!

Now the important part. To transfer the paste buffer from the current machine to a remote machine:

xsel | ssh -X "DISPLAY=:0.0 xsel -i"

Obviously you can replace the input with a local file or the output of a command just as easily, e.g:

ssh -X "DISPLAY=:0.0 xsel -i" < foo.html

In case you aren’t familiar with the traditional X11 paste method, this means you select something on the source machine, run the command above, and then use the middle mouse button on the remote machine to paste. If you don’t have one, use the left and right buttons simultaneously.

Hope this is useful!

How to remove thumbnails from a directory containing images

Following up from my last post about grabbing images from a squid cache, I needed a better way to remove thumbnails than just looking at file sizes.

I decided to try and get the image dimensions and then look at the mean width/height value.

This was what I came up with:-


for file in *; do
    size=$(identify -format "%w %h" "$file")
    w=$(echo $size | cut -f 1 -d " ")
    h=$(echo $size | cut -f 2 -d " ")
    mean=$(( ($h + $w)/2 ))
    if (( mean < 300 )); then
        echo "$file $h $w $mean"
        rm "$file"

You will need the excellent ImageMagick package, which is available in most distros.

How to extract images from your squid cache

When I searched for this, I found a lot of questions but no real answers.  Based on a couple of hints I found online, I put these scripts together which might be of use to someone else.

First, the script to scan the cache for images we want to extract.


# -- Edit these as needed !
# --


set -u

# Limit to files above 15k size in order to skip thumbnails where possible
find "$cache" -size +15k -type f | while read file
    if head "$file" | grep -q "$match"; then

        content_type="$(head "$file" | grep -ai "Content-Type:" | cut -f 2 -d " " | tr -d '[:cntrl:]')"
        echo "Matched $file ... content type $content_type" >&2

        if [[ "$content_type" = 'image/jpeg' ]]; then
        elif [[ "$content_type" = 'image/png' ]]; then
        elif [[ "$content_type" = 'image/gif' ]]; then

        if [[ "$type" != "" ]]; then
            outfile="$output$(mktemp $filebase).$type"
            echo "Converting $file as $outfile with type $type" >&2
            squid_get_image -q -t $type $file $outfile < /dev/null


Save this as ~/bin/squid_scan or similar (if ~/bin is in your path) and do chmod 755 ~/bin/squid_scan

This uses some fairly rudimentary filtering, which could be improved. To use it to grab all images from, you call: squid_scan mysite

As you can see, this calls squid_get_image to actually do the image extraction. Here’s the code:-


set -u

    echo "squid_get_image [-q] [-t ] infile outfile" >&2
    echo >&2


while getopts ":qt:" OPTION
    case $OPTION in
        echo "Invalid option: -$OPTARG" >&2
        exit 1

shift $(($OPTIND - 1))

if [[ $# != 2 ]]; then
    echo "Expecting 2 parameters, got $#" >&2
    exit 1


$quiet || echo "Coverting $infile to $outfile" >&2
case $im_type in


    bvi -f <(echo -e '\FFD8\,$w' $outfile\\nq) "$infile" &>/dev/null


    bvi -f <(echo -e '\89504E470D0A1A0A\,$w' $outfile\\nq) "$infile" &>/dev/null


    bvi -f <(echo -e '/GIF8[79]a/,$w' $outfile\\nq) "$infile" &>/dev/null


    echo "Bad Image Type." &>2


NOTE: You need the bvi utility (binary vi). In Debian/Ubuntu just sudo apt-get bvi. In Archlinux it’s in aur.

Save this as ~/bin/squid_get_image or similar and do chmod 755 ~/bin/squid_get_image

This uses bvi’s ability to run a script of ex commands. It uses the fact that the squid cache files are some binary data, the HTTP headers, and then the file itself. The gif, jpg and png formats all start with “magic numbers”, and the ex commands look for those before writing out everything that follows to the specified file name.

I hope this is useful to someone!


I have to interact with a lot of Windows systems, so pptp is the norm.  I’ve often tried to configure IPSEC but to honest, I’ve never made it work!  I read up on it, understood it in theory and then sat down to try again.  But heterogenous systems (Linux, my router, their router, etc.) all seem to present a different set of incomprehensible options with different names.  In the case of the routers, add questionable documentation.

Anyway, I just discovered the networkmanager-pptp plugin.  First, I installed network-manager-pptp and pptp-linux.  You need to make sure that your wired interface is configured to roam, otherwise you will not see the options for VPN configuration.

After that, I configured my VPN but couldn’t work out how to make the option appear to connect to it.  After some Googling, I found the answer:-

sudo /etc/dbus-1/event.d/25NetworkManager restart
sudo /etc/dbus-1/event.d/26NetworkManagerDispatcher restart

I suppose a reboot would have done it but I like to avoid those!

Now, I can just click and connect instantly.  Very neat!

Top artists this week from

My Twitter feed