Unfortunately, the 256 MB RAM of my Raspi is barely enough to keep RetroShare running, but even a simple 'sudo apt-get update' can crash it. The RetroShare project itself is very active at the moment, bugs are constantly fixed and features added. But compiling RetroShare requires all resources, so I thought about alternatives.
"Simple", I thought, "Emulation!"
Spoiler alert: Emulating a Raspberry Pi using QEMU is slow.
I have no experience with emulation whatsoever, so there is no real reason for choosing QEMU over other emulators like VirtualBox, I just found instructions and information for this way first.
What we need to get started
The EmulatorMy desktop PC is running Ubuntu 12.04, the rest of the system specifications shouldn't matter. (Yes, I know that this version is a bit outdated, but as a True Believer in Murphy's Law I tend to not change a running system.)
Installing QEMUInstall the package
qemu-kvm-extrasthe way you usually install packages, e.g., using
sudo apt-get install qemu-kvm-extrasThis will also install some dependencies that are needed.
You will also need to download a QEMU-ready linux kernel for the Raspberry Pi, which you can do here:
Alternatively, you can compile your own kernel.
Preparing the environmentCreate a directory in which our experiment will take place. I chose:
Raspbian WheezyI used newest version of Raspbian Wheezy currently available at http://www.raspberrypi.org/downloads:
|Default login||Username: pi Password: raspberry|
Download it and unpack the image file into the directory you prepared in the last step.
Plan A: Running an emulated Raspberry PiIn the directory of the image, run the following command:
qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1" -hda 2012-10-28-wheezy-raspbian.imgThe parameters have the following functions:
- -kernel kernel-qemu
- the QEMU-ready kernel we just downloaded
- -cpu arm1176
- the CPU we want to emulate (ARM1176JZF-S (700 MHz))
- -m 256
- how much RAM should be available (in MB)
- -M versatilepb
- the machine type to emulate
- exit instead of rebooting
- -serial stdio
- redirects the serial port to the standard I/O
- -append "root=/dev/sda2 panic=1"
- where to find the root partition, depends on the image
- -hda 2012-10-28-wheezy-raspbian.img
- what should be used as hard drive, in this case our image
We also shouldn't forget to resize our image, because right now we only have about 200 MB of free space left. Again: There already are many articles on the net covering this, so I'll only quickly describe what I did.
- With QEMU not running, you can use
qemu-imgto resize an image:
qemu-img resize 2012-12-16-wheezy-raspbian.img +1G
- For other reasons, raspian's built-in functionality of growing the partition to fill the SD card won't work here, so I did it the hard way.
- Boot your emulated Raspberry Pi again using QEMU
- Resize the partition using
sudo fdisk /dev/sdaIt should look similar to this:
Device Boot Start End Blocks Id System /dev/sda1 8192 122879 57344 c W95 FAT32 (LBA) /dev/sda2 122880 5885951 2881536 83 LinuxYou need to delete partition 2 and create it again with the same start, but this time with the highest allowed value for end.
- Resize the filesystem using
sudo resize2fs /dev/sda2
It's too slowBefore we start compiling RetroShare again, let's check up on the speed. This is what I get from the QEMU Raspberry Pi:
pi@raspberrypi:~$ cat /proc/cpuinfo | grep MIPS BogoMIPS : 565.24And this is from my actual Raspberry Pi
pi@raspberrypi:~$ cat /proc/cpuinfo | grep MIPS BogoMIPS : 697.95So, my emulated raspi running on a Intel Core i5 is actually slower than the real raspi ... which is not what I wanted. I mean, okay, if I'd have the machine running for something else anyway, that wouldn't be a problem. But I still want it to be faster.
Plan B: Architectural chroot a.k.a. chroot VoodooLooking for solutions to speed up QEMU, I stumbled upon another approach: architectural chroot!
Now, I'm familiar with chroot (at least I though so) and I've used it hundreds of times when my Ubuntu got f*cked up because of an update or some other stuff. And I remember the difficulties when I tried to chroot into a 64 bit system from a 32 bit Live-CD. But it seems like there is a way around this. Coincidentally, we're already close to what we need: a static version of QEMU.
Install it via
apt-get(or build it yourself)
sudo apt-get install qemu-user-staticWe need to mount the image using loopback, but since the image contains multiple partitions, we require
$ sudo kpartx -a -v 2012-12-16-wheezy-raspbian.img add map loop0p1 (252:8): 0 114688 linear /dev/loop0 8192 add map loop0p2 (252:9): 0 5763072 linear /dev/loop0 122880 $ sudo mount /dev/mapper/loop0p2 /mnt/tempNow, copy the static QEMU binary TODO and mount the special directories:
sudo cp /usr/bin/qemu-arm-static /mnt/temp/usr/bin sudo mount -o bind /dev /mnt/temp/dev sudo mount -o bind /proc /mnt/temp/proc sudo mount -o bind /sys /mnt/temp/sysBefore we can enter the chroot environment, it's time for the magic! As root (simple
sudowon't work), do this (all in one line):
echo ':arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-arm-static:' > /proc/sys/fs/binfmt_misc/registerThis registers the static QEMU we copied as arm-interpreter to the kernel. The path specified needs to be the same on both your linux machine and the Raspberry Pi environment.
Now we can chroot:
sudo chroot /mnt/tempDid it work?
$ uname -a Linux localhost 2.6.32 #58-Ubuntu SMP Thu Jan 24 15:28:10 UTC 2013 armv7l GNU/Linux
Hooray! Welcome to your (much faster) Raspberry Pi environment! Now, let's do some compiling ;)
Cleaning upTo avoid inconsistencies, make sure you never use QEMU and chroot at the same time! Even more, you need to completely unmount the image before you start QEMU. Otherwise you might see some undesireable side effects.
sudo umount /mnt/temp/dev sudo umount /mnt/temp/proc sudo umount /mnt/temp/sys sudo umount /mnt/temp
sudo kpartx -d -v 2012-12-16-wheezy-raspbian.img
AcknowledgementsI also want to give credit to the following articles that helped me doing this: