Friday, May 9, 2008

Setting up kgdb using kvm/qemu

I read in the mailing list (lkml) that kgdb has been integrated into the mainline kernel. So i wanted to test it out. Those who are not familiar with kgdb read this . Few years before, we needed to patch the kernel under test and use 2 systems (target and debugger) to debug. This was a big problem and i never was keen on this way of debugging. These days with virtual guest os being common we can do the same stuff on a single machine between host and guest. Its rather simple.

I hope this tutorial will be able to help get the setup ready and working.

Stuff Iam Using:
  • kvm (kernel virtual machine) or qumu with kqemu
  • latest git kernel (2.6.25)
  • fedora 8
Please Note: To use kvm, you need hardware support. To check if you have hardware support
#grep '(vmx|svm)' /proc/cpuinfo
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx constant_tsc arch_perfmon bts pni monitor vmx est tm2 xtpr
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe nx constant_tsc arch_perfmon bts pni monitor vmx est tm2 xtpr

If nothing is printed then you are out of luck. You dont have hardware based virtualization.
Dont worry mate, you can always use software based emulation (qemu, kqemu). If you are using ubuntu, its as simple as doing a 'sudo apt-get install qemu kqemu'.
The kgdb support has been added to 2.6.25 onwards, so iam using the git tree. You can download vanilla kernel from or if you want to test on older kernel you need to apply appropriate patch from kgdb website as stated above. I will not be explaining this as its already available in their website.

The first step will be to create a new vm image (guest). There are mulitiple way to do it. There are pre built FS images available on the internet that can be downloaded. I prefer to create one on my own. I have decided to install fedora 8 as my guest os using kvm. This blog explains how to do this. Since its pretty straightforward i shall not explain this any further. I have just put in steps i used to install.

Create a 5GB iamge for my new OS
#qemu-img create f8.img -f qcow2 5G

Install the new OS (qemu users can simply replace "kvm" with "qemu" in the below command)
#kvm -m 512 -cdrom /home/temp/Fedora-8-i386-DVD.iso -boot d f8.img
This will take you through the regular distro installation procedure. At this point i was faced with need to resize my image. 5GB was not enough for me. This is illustrated here .

Boot the new guest os
#kvm -no-acpi -m 500 f8.img
Done. we have a virtual OS ready.

Few Optional Steps:
These are few optional steps needed to enable networking on guest OS. Ideally to copy some files into the guest image of fedora we need to mount it. Here is the procedure for the same. If the image is of the format vmdk (vmware format) then follow this . I prefer using networking to copy data in and out of the vm image rather than mounting. The below steps will help you boot your guest os with network support. For this you need to install "vtun" package using apt-get. Then start it using 'sudo /etc/init.d/vtun start'.
Give appropriate permission. Its observed that udev alters these permission. So you need to either fix udev rules or set this permission every time.
#chmod a+rw /dev/net/tun
The rest of the procedure is beautifully described here . I just made few alterations to the script to make it work in my environment.


# id of the user running qemu (kvm).Make sure you change it appropriately.

# number of TUN/TAP devices to setup

case $1 in
modprobe tun
/etc/init.d/vtun start
chmod a+rw /dev/net/tun
echo -n "Setting up bridge device br0"
brctl addbr br0
ifconfig br0 netmask up
for ((i=0; i < NUM_OF_DEVICES ; i++)); do
echo -n "Setting up "
tunctl -b -u $USERID -t qtap$i
brctl addif br0 qtap$i
ifconfig qtap$i up promisc
for ((i=0; i < NUM_OF_DEVICES ; i++)); do
ifconfig qtap$i down
brctl delif br0 qtap$i
tunctl -d qtap$i
ifconfig br0 down
brctl delbr br0
/etc/init.d/vtun stop
echo "Usage: $(basename $0) (start|stop)"

This modified script should take care of all the problems. As a root user execute the script.
Note :Dont forget to specify the iptables rules as given in the above howto.

Now boot you guest os using:
#kvm -no-acpi -m 500 f8.img -net nic,model=rtl8139,macaddr=52:54:00:12:34:56 -net tap,ifname=qtap0,script=no
Once you guest it up, configure you network adapter to 192.168.1.x network.
#ifconfig eth0
#route add default gw eth0
ping between two system to make sure its working. Thats it.
Download the latest kernel (>= 2.6.25) untar the file and do 'make menuconfig'
enable kernel hacking -> KGDB: kernel debugging with remote gdb and few additional kgdb options if required. Save and exit. Compile the new kernel and copy the bzImage and initrd.img to /boot of the guest os (fedora 8). Also copy the /lib/modules/2.6.25/ dir to the guest os using scp. Add a new entry in the /boot/grub/menu.lst of the guest os (fedora 8). Shown below is my menu.lst

The Important things to be noted is the additional arguments added to the kernel parameters.
$console kgdbwait kgdboc=ttyS1 selinux=0. These parameters will make the booting wait for gdb remote connection.

Poweroff the guest os and start again using the following options
#kvm -no-acpi -m 500 f8.img -net nic,model=rtl8139,macaddr=52:54:00:12:34:56 -net tap,ifname=qtap0,script=no $debug_args $vga_args -serial "stdio" -serial "pty"
At the time of booting this will give you the tty to which we are connected to. In my case
char device redirected to /dev/pts/5

The system boots and waits for remote connection as shown below.

As shown above the kernel booting stops at waiting for remote gdb.......
Navigate to the place where you have compiled the kernel sources in your host system and you will find a file "vmlinux" in that directory.
Do the following from a terminal in the host system
#gdb vmlinux
#target remote /dev/pts/5 --> Note: /dev/pts/5 is specific to me. In your case you would have to change your settings as per what is displayed after executing "#kvm -no-acpi ..... "as show before.

Done!!! Your are the gdb promt!!! Was'nt it easy? Happy debugging :-) .

Thursday, May 1, 2008

end of reiserfs?

Iam not a file system expert so i cannot comment on the standard of reiserfs, but i used it for quite sometime with opensuse. Why am i saying this......well, not sure but when i read about the murder conviction of Hans Reiser i thought its worth a mention in my blog. As i see it, reiserfs4 has been struggling to get into mainline for quite sometime. Mostly due its radical ideas which were non posix compliant , they were rejected. Now with their main developer and architect out of the picture i see this as end of the road for the revolutionary FS. Will the reiser fanboys carry on with the development of reiser4 is to be seen. Its a sad to loose a really good programmer like reiser.

Installing Ubuntu Gutsy on Presario F500

Recently Leena got a new laptop [ Presario F500] . At its base, there is a 64-bit AMD Turion Processor and 1 GB RAM. To add to this there is a Wide Screen [15.4''] and Altec Lansing Speakers. But this is not good enough for Windows Vista [packed by default] to run.

Ubuntu Installs normally except certains glitches.

1. LiveCD causes the system to hang.

  1. Once you see the splash screen of Ubuntu's main menu press F6
  2. You will get the kernel command line, append vga=792 or vga=791 to that line and press Enter or b
  3. Now X server should start and you will see the Ubuntu Desktop
The reason for this hack is probably because it has a widescreen display.

2. Machine during boot up.
  1. Post Installation, your machine will hang because of the above mentioned problem.
  2. In grub edit the kernel command line and the params mentioned above.
  3. This may not be enough for your system to bootup. It wasn't surely enough for me.
  4. I had to remove the params quiet and splash also.
  5. Make these changes permanent once the system boots up by editing the /boot/grub/menu.lst .
NOTE: Removing quiet and splash, although necessary will not show any splash screen while the OS is booting. You will directly see to the login window.

3. Getting Wireless/Compiz working.
  1. Unfortunately Wireless doesn't work out of the box.
  2. You will need the Broadcom wireless binary driver.
  3. Goto Menu System-> Administrator -> Restricted Driver Manager .
  4. It will say Broadcom driver not in use. Select the checkbox associated with it .
  5. It will ask for a link from which to download firmware . You will get it here .
  6. Once the state changes to in use. You are successful.
  7. Similarly to get Compiz working, select the NVidia Driver which is not is use.
    It will download the driver and again once the state changes to in use. You are successful.
  8. This will need a system reboot.
4. Nonfree Flash doesn't work in Firefox.
  1. The non-free Adobe flash player embedded in Firefox will surely not work although package manager will say its installed, as there is no 64bit version yet.
  2. Use gnash [ open source/free flash player] . It works just fine.
  3. People seem to crib about java plugin for Firefox not working on 64bit boxes, but it worked fine for me.
  4. Else just a 32-bit version of Firefox(with 32-bit plugins) . I found a useful HOWTO

1. Tom On Identity
2. Ubuntu's wireless Docs

Tata Indicom v-data card on Ubuntu Linux

To start using the tata indicom v-data card in ubuntu, follow these steps

1.Run this command
$sudo modprobe usbserial vendor=0x12d1 product=0x1001

2. Edit the /etc/wvdial.conf and add the following lines

$cat /etc/wvdial.conf

[Modem 0]
Modem = /dev/ttyUSB0
Baud = 115200
Dial Command = ATDT
FlowControl = Hardware (CRTSCTS)
Init1 = ATZ
[Dialer Defaults]
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Modem Type = Analog Modem
Phone = #777
ISDN = 0
Password = internet
New PPPD = yes
Username = internet
Stupid Mode = 1
Inherits = Modem 0

3. Run
$sudo wvdial

You will see your primary and secondary DNS getting set.
Now you are set.

Note: The data card is of Huawei make.

Also for the TATA Plug2Surf USB modem to work;
Insert you modem in and do a

You will see some messages with /dev/ttyACM1.
Just replace /dev/ttyUSB0 with this in the above mentioned files.

Thanks to varun for showing me this method