Monday, October 17, 2011

Bash substring exclusive replacement

This is a neat little trick I figured out today. I needed to remove all characters from a variable that were not digits or spaces. I've done a lot of variable manipulation in bash, but nothing with more than one condition.

It turns out that you can negate with the ^ character, combined with the | "or" character to get this:
$ MYVAR="4322, #32"
$ echo ${MYVAR//[^[:digit:]|[:space:]]/}
4322 32

Thursday, October 6, 2011

SSH chained port forwarding

I might be late to the party on this, but I just realized that you can chain forwarded ports in ssh!

For example:
ssh -L port1:<ip address>:hostport1 -L port2:<ip address>:hostport2 somewhere.com
I'll admit, I'm guilty of opening multiple ssh sessions to forward each port that I needed... Never again!

Tuesday, October 4, 2011

Convert Xen XP domU to KVM

Recently I needed to take a windows XP VM under Xen from my workstation and move it to my laptop running KVM.

The Xen VM uses an LVM block device. I was able to use qemu-img to convert the raw LVM block filesystem into the compressed qcow2 format:
qemu-img convert -c -f raw -O qcow2 /dev/vgxen/xp xp.qcow2
This took over an hour, since the filesystem is 100GB.

Back over on my laptop, importing the qcow2 image was a breeze. I simply launched "Virtual Machine Manager" and selected "importing existing disk image", and pointed to the image.

XP booted fine, but I did have to re-activate windows due to hardware "changes" that windows saw.

The qcow2 format is nice since it's compressed, but there is definitely a performance hit. I plan on experimenting with moving the VM to an external eSATA drive, on a raw block device, and see how much it helps performance.