Saturday, November 22, 2008

weird bug with ctrl, alt, shift keys going haywire after running vmware on ubuntu

If I use ctrl+alt+enter to full-screen a vm in vmware, then when I return to the host, my special keys - ctrl, alt, and shift - will stop working !

The "xmodmap" command can be used to see the keycodes assigned to the modifiers. When the keys are working fine,
root@vmathew-laptop:~# xmodmap
xmodmap: up to 3 keys per modifier, (keycodes in parentheses):

shift Shift_L (0x32), Shift_R (0x3e)
lock Caps_Lock (0x42)
control Control_L (0x25), Control_R (0x6d)
mod1 Alt_L (0x40), Alt_R (0x71), Meta_L (0x9c)
mod2 Num_Lock (0x4d)
mod3
mod4 Super_L (0x7f), Hyper_L (0x80)
mod5 Mode_switch (0x5d), ISO_Level3_Shift (0x7c)
Now, once I run a vm in vmware and use ctrl-alt-enter, to full-screen it, and then come back to the host (ctrl-alt), the output goes.
root@vmathew-laptop:~# xmodmap
xmodmap: up to 1 keys per modifier, (keycodes in parentheses):

shift
lock
control
mod1
mod2
mod3
mod4
mod5
The fix is to use the "setxkbmap" command. This will restore the keycodes for the modifier keys.

[Ref]
http://ubuntuforums.org/showthread.php?t=792737
http://ubuntuforums.org/showthread.php?p=4990785

.

Friday, November 7, 2008

Remote procedure calls are SO EASY !! (XMLRPC / Python)

I just love python !
And the XMLRPC implementation for python just made me fall in love with it. (Ok, XMLRPC is language independent, but I just loved the python implementation)

A bit of context; we were discussing the paper "Implementing Remote Procedure calls" (Link), in our CS736 / Advanced Operating Systems class. I was skeptical of the whole idea since I was wondering why in the world would anyone use this, when the socket api is so neat and clean.

I was wrong !

My CS740 / Advanced Networks project mate, Ian Rae, pointed me to XMLRPC within the context of our networks project. And I realized how simple and elegant programming RPC can be.

The gist of the code is as follows:
Server:
from SimpleXMLRPCServer import SimpleXMLRPCServer

def poke(n):
print "I was poked %d times" % n
return ("dumb_server", "8000")

server = SimpleXMLRPCServer(("localhost", 8000))
server.register_function(poke, "poke_the_server")
server.serve_forever()
Client:
import xmlrpclib

proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
reply = proxy.poke_the_server(3)
print "The server %s is listening on port %d" % (reply[0], reply[1])
Reference: http://docs.python.org/library/xmlrpclib.html

.

Thursday, November 6, 2008

Using pipes with python.

This lists a bit of code I developed to use pipes with python
import os
import sys

def function():
r1,w1 = os.pipe()
r2,w2 = os.pipe()
if (os.fork() == 0):
# we're the child
os.close(1)
os.dup2(w2,1)
os.close(0)
os.dup2(r1,0)
sys.stdin = os.fdopen(0)
sys.stdout = os.fdopen(1,'w')
os.execl("/bin/bash")
else:
# we're the parent
to_child = os.fdopen(w1,'w')
from_child = os.fdopen(r2)
to_child.write("ls\n")
to_child.flush()
to_child.write("echo hi\n")
to_child.flush()
temp = from_child.readline()
while temp:
print temp
temp = from_child.readline()

function()
The only problem with this code at the moment is, I don't know how to get readline() to return when there's nothing to read. In short, we always end up hanging!

A much simpler way to achieve the effect of the above would be to use the popen3 function.
>>> import os
>>> i,o,u = os.popen3("/bin/bash")
>>> i.write ("ls\n")
>>> i.flush()
>>> o.readline()
'bin\n'
>>> o.readline()
'Desktop\n'
>>> o.readline()
'Documents\n'
>>> o.readline()
'mounting-vmdk.pdf\n'
>>> o.readline()
'order-cardbus-to-express.pdf\n'
>>> o.readline()
'public_html\n'
>>> o.readline()
'vmware\n'
>>> o.readline()
# at this point, again we hung !!
The problem with readline hanging still exists, but the code's much simpler

.

Mouting partitions with losetup

References (previous posts):
Virtual Drives on Linux
Playing with mount and loopback devices
Encrypted Filesystems

One issue I faced with using loopback devices to mount hard disk images was that everything works fine as long as you have the disk image as one single filesystem. But if you use fdisk on the loop back device and created partitions, then I could not quite figure out a way to mount these.

One intuitive way to do this would be as described in http://vytautas.jakutis.lt/node/26 . In brief, what they do is:
# rmmod loop
# modprobe loop max_part=63
# losetup -f /path/to/your/disk/image.raw
# mount /dev/loop0p1 /mnt/path
Unfortunately, that doesn't work for me. The max_part=63 parameter is rejected by modprobe for me. Guess something else needs to be enabled/compiled-into my kernel for me to support this. (My kernel qualifies by the version info they give there, though)

But then, there's a simpler option. One can specify an offset in the file where the loop back device should begin. From what I've gathered, the first sector, sector 0 in a disk contains the partition table I guess. And then, the first partition starts at sector 1, and ends at sector n. The next from n+1 to n+m and so on.

So if we specify the start sector's byte offset (start_sector * 512 normally) as the offset for the loop back device, we can access the particular partition.
Reference: http://www.docunext.com/blog/2007/07/08/losetup-working-with-raw-disk-images/

So say if we have a disk image data.raw, ( say we used qemu_img to create this from a vmware vmdk disk) then we can attach the whole disk to a loop device and run fdisk to get the start sector of the partition
vmathew:/data/vmathew/vmware/dsl # losetup /dev/loop0 data.raw
vmathew:/data/vmathew/vmware/dsl # fdisk /dev/loop0

Command (m for help): p

Disk /dev/loop0: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xb96bb154

Device Boot Start End Blocks Id System
/dev/loop0p1 1 63 506016 83 Linux
/dev/loop0p2 64 130 538177+ 83 Linux

Command (m for help):
But note here that the units displayed is cylinders. Our second partition starts at cylinder 64, but we don't know the exact sector. To find this information
Command (m for help): u
Changing display/entry units to sectors

Command (m for help): p

Disk /dev/loop0: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders, total 2097152 sectors
Units = sectors of 1 * 512 = 512 bytes
Disk identifier: 0xb96bb154

Device Boot Start End Blocks Id System
/dev/loop0p1 63 1012094 506016 83 Linux
/dev/loop0p2 1012095 2088449 538177+ 83 Linux

Command (m for help):
Now we quit the fdisk tool. We have our start sector = 1012095. So our start offset = 1012095*512 = 518192640. Thus to mount this file, we first detach the loopback and create it at this offset, and then just mount it.
Command (m for help): q

vmathew:/data/vmathew/vmware/dsl # losetup -d /dev/loop0
vmathew:/data/vmathew/vmware/dsl # losetup --offset 518192640 /dev/loop0 data.raw
vmathew:/data/vmathew/vmware/dsl # mount -t ext3 /dev/loop0 temp/
vmathew:/data/vmathew/vmware/dsl # ls temp/
jklh.txt lost+found
vmathew:/data/vmathew/vmware/dsl #
Additionally, checkout the manpage on qemu-img (you need qemu installed) to learn to use this gorgeous converter which can convert between some standard disk image types.

.

Monday, November 3, 2008

Merging PDF files

This was something I used hit a dead end on quite often.
Now that I found it, I'm posting it here..

It's basically ghostscript to the rescue
gs -q -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=out.pdf in1.pdf in2.pdf in3.pdf ...
Source (yeah, I'm "plagiarizing" !! )
http://ansuz.sooke.bc.ca/software/pdf-append.php

.