Showing posts with label Raspbian. Show all posts
Showing posts with label Raspbian. Show all posts

Wednesday, January 15, 2014

Virtual Raspbian on QEMU in Ubuntu


How to run virtual Raspbian on QEMU in Ubuntu:

1. Compile QEMU for ARM (1176).

$ sudo apt-get install git zlib1g-dev libsdl1.2-dev libpixman-1-dev
$ cd tmp
$ git clone git://git.qemu-project.org/qemu.git && cd qemu
$ ./configure --target-list="arm-softmmu arm-linux-user" --enable-sdl --prefix=/usr/local
$ make
$ sudo make install
2. Prepare QEMU environment and raspbian image.
$ mkdir ~/qemu-vms/ && cd ~/qemu-vms/
Download Raspbian Wheezy from http://www.raspberrypi.org/downloads and Linux kernel for QEMU from http://xecdesign.com/downloads/linux-qemu/kernel-qemu.
$ file ~/qemu-vms/2014-01-07-wheezy-raspbian.img
~/qemu-vms/2014-01-07-wheezy-raspbian.img: x86 boot sector; partition 1: ID=0xc, starthead 130, startsector 8192, 114688 sectors; partition 2: ID=0x83, starthead 165, startsector 122880, 5662720 sectors, code offset 0xb8
Take the partition 2 'startsector' value an multiply by 512, and use this figure as the offset value in the mount command below:
$ sudo mount ~/qemu-vms/2014-01-07-wheezy-raspbian.img -o offset=$((122880*512)) /mnt
Comment out the line in the /mnt/etc/ld.so.preload file and unmount:
$ sudo emacs -nw /mnt/etc/ld.so.preload
$ sudo umount /mnt
3. Run simulation.
$ qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1" -hda ~/qemu-vms/2014-01-07-wheezy-raspbian.img -redir tcp:5022::22
QEMU gives you a root shell, run:
$ fsck /dev/sda2
$ shutdown -r now
UPDATE: how to open ssh connection (see -redir option):
$ ssh -p 5022 pi@localhost
UPDATE: how expand free space:

1. Create /etc/udev/rules.d/90-qemu.rules with the following content and shutdown the system:
KERNEL=="sda", SYMLINK+="mmcblk0"
KERNEL=="sda?", SYMLINK+="mmcblk0p%n"
KERNEL=="sda2", SYMLINK+="root"
2. Resize the image:
$ qemu-img resize /qemu-vms/2014-01-07-wheezy-raspbian.img +4G
3. Boot up your emulator again and do:

$ sudo ln -snf mmcblk0p2 /dev/root
$ sudo raspi-config

Choose the first option to resize your disk, and it will tell you to reboot.

UPDATE: update keyboard mappings if necessary:

$ sudo dpkg-reconfigure keyboard-configuration