Wednesday, January 19, 2011

openwrt-x86-ext2.image in qemu on linux

I just downloaded the openwrt firmware and wanted to mount it in my ubuntu laptop. Here's the trick:


Get the starting offset of first partition in the image:
startsector=`fdisk -lu openwrt-x86-ext2.image 2>/dev/null | grep openwrt-x86-ext2.image2 | awk '{print $2}'`
Set-up a loop device to access the file offset as a block device, assuming 512 bytes sector size: 
losetup -o `expr $startsector \* 512` /dev/loop0 openwrt-x86-ext2.image
Now, mount the device to a directory:
mount /dev/loop0 /mnt/
To unmount the device:
umount /dev/loop0
To delete the loop device:
losetup -d /dev/loop0

To run the image under qemu with 128 MB ram and single network card:
sudo qemu -m 128 -hda openwrt-x86-ext2.image -net nic,model=ne2k_pci -net tap,ifname=tap1,script=~/qemu/qemu-ifup.sh

----------------------------------------------------------------------------------
#!/bin/sh
#
# ~/qemu/qemu-ifup.sh
#
echo Creating bridge between $1 and eth0 using br0
/sbin/ifconfig $1 promisc 0.0.0.0
if ! /sbin/ifconfig br0 > /dev/null
then
    /usr/sbin/brctl addbr br0
    /usr/sbin/brctl addif br0 eth0
    /sbin/ifconfig br0 up
    addr=`/sbin/ip addr | grep eth0 | grep inet | sed -e 's/eth0/dev br0/' -e s/inet//`
    /sbin/ip addr add $addr
    # start dhcpd only if your virtual machine needs it
    #dhcpd br0
fi
/usr/sbin/brctl addif br0 $1
/usr/sbin/brctl stp br0 off
/sbin/ip route | grep eth0 | while read route
do
  newroute=`echo $route | sed s/eth0/br0/ `
  /sbin/ip route del $route
  /sbin/ip route add $newroute
done

--------------------------------------------------------------------------------

No comments:

Post a Comment