User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Sat Mar 16, 2013 10:50 pm

Task:

Virtualise Raspbian Wheezy 2013-02-09 on Ubuntu Linux 12.10

Why?/What do I get?

It's a pretend Raspberry Pi, running completely in software, on your desktop, that you can use as if it is a real one (minus the ports and GPIO obviously). LXDE (Desktop) runs, and the internet/network is connected. You can SSH into it, make copies of it, trash it, start a new one from a previous saved copy, and generally do pretty much anything software-wise with it that you would do with a real one.

Drawbacks:

Using X (the desktop) may be a bit slow depending on your hardware. I ran this on an Intel d525mw dual core Atom board with 4GB of RAM and a fresh install of Ubuntu 12.10, and the desktop was sluggish at best, and sometimes unresponsive. The console was very quick, and was like using the real thing.

Qemu is a typical old-school Linux console app - it's pretty terse with error messages, and has esoteric command line switches. I'm going to check out some Qemu managers in the Ubuntu Software Store.

Needed:

Ubuntu 12.10 Desktop installed and running with up-to-date packages/list
- http://www.ubuntu.com/download/desktop

Raspbian Wheezy 2013-02-09
- http://www.raspberrypi.org/downloads

Linux kernel for Qemu
- http://xecdesign.com/downloads/linux-qemu/kernel-qemu

Internet connection

(Note that a Raspberry Pi is not required for this)

Notes:

These instructions may very well work with Ubuntu 12.04 LTS and earlier, as well as Debian, Mint, and other Linux's/Linuxi/Linuxes. If you have a non-Debian distribution, you will need to use your package managers commands instead of apt-get.

Thanks:

http://xecdesign.com/qemu-emulating-ras ... -easy-way/
http://xecdesign.com/working-with-qemu/
http://www.soslug.org/wiki/raspberry_pi_emulation
http://www.raspberrypi.org/phpBB3/membe ... file&u=356 (ShiftPlusOne)

Steps:

$ mkdir ~/qemu_vms/
Download/copy Raspbian Wheezy to ~/qemu_vms/
Download kernel-qemu to ~/qemu_vms/
$ sudo apt-get install qemu-system
$ file ~/qemu_vms/2013-02-09-wheezy-raspbian.img
From the output of the file command, 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/2013-02-09-wheezy-raspbian.img -o offset=62914560 /mnt
$ sudo nano /mnt/etc/ld.so.preload
Comment out the line in the file (use a # as the first character of the line) and save the file (CTRL+X, then "Y" for yes).
$ sudo umount ~/qemu_vms/2013-02-09-wheezy-raspbian.img /mnt
$ cd ~/qemu_vms/
$ 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/2013-02-09-wheezy-raspbian.img -redir tcp:5022::22
Qemu gives you a root shell, run:
$ fsck /dev/sda2
$ shutdown -r now
Login as pi
Password raspberry

Done.

Thanks so much to user/moderator 'ShiftPlusOne' for devoting some time on IRC to helping me get this going.

NOTE: The command list above is pretty terse, and without comment/explanation per command. It is all self explanatory for people vaguely familiar with the command line, and I wanted to keep that section brief, so it looks less scary for newbies (a short set of clear commands!). If anyone needs an explanation of any of this, ask away... :)
Last edited by tonyhughes on Tue Mar 19, 2013 11:25 pm, edited 2 times in total.

exup
Posts: 3
Joined: Sun Aug 19, 2012 5:27 am

Re: HOWTO: Virtual Raspbian Wheezy on Qemu in Ubuntu 12.10

Tue Mar 19, 2013 10:20 pm

Hi

Excellent work and perfect timing, I wanted to emulate pi on my linux box and was looking around at alot of Window methods using QEMU. Perhaps include in your title Linux as it might get picked up better in searches.

I followed the instructions exactly (except I am using an older version of the wheezy Raspbian 2012 10 28)

It worked straight away as per your instructions.
Only comment is right at the very end you have a line that says

$ apt-get shutdown -r now

I suspect these should be separate lines? $apt-get update and then $shutdown now -r
The apt-get update did not work, could not get an internet connection. So ignored it and did it when I got into the desktop GUI.

Once again many thanks.

Peter

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Tue Mar 19, 2013 11:22 pm

Thanks. Good points all round. Have fixed up the typo, and added Linux into the thread title (limited title length on these forums, so you have to be fairly short and concise in them!

I'm glad you found this helpful.

exup
Posts: 3
Joined: Sun Aug 19, 2012 5:27 am

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Wed Mar 20, 2013 3:31 am

Hi,

one other thing, I am not an expert on QEMU but is it possible to increase the screen size of the session. I suspect its to do with the command line -redir tcp:5022::22 but not sure what numbers to plug in.

Also can you increase RAM sizes, is that the -m 256 could you make it -m 1024

Code: Select all

qemu-sysnel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1" -hda /media/it_drive/VirtualBox/qtemu/2012-10-28-wheezy-raspbian.img -redir tcp:5022::22

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Wed Mar 20, 2013 3:38 am

exup wrote:one other thing, I am not an expert on QEMU but is it possible to increase the screen size of the session. I suspect its to do with the command line -redir tcp:5022::22 but not sure what numbers to plug in.
the redir tcp line is to make Qemu accept SSH on port 5022 on the host directed to port 22 on the guest. Nothing to do with screen size.

I'd like a bigger console resolution too, just haven't figured it out yet.
exup wrote:Also can you increase RAM sizes, is that the -m 256 could you make it -m 1024
I assume you could. Just try it. My personal goal is to emulate a Pi, so I only need/want 256MB, as I don't want to have more resources in that respect than a real Pi.

User avatar
ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6452
Joined: Fri Jul 29, 2011 5:36 pm

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Wed Mar 20, 2013 4:47 am

Over 256M won't work. Though if it does, let me know how.

One of the articles he links says you can change the resolution by modifying xorg.conf, though it seems that the limit is 16-bit 800x600.
http://www.soslug.org/wiki/raspberry_pi_emulation

I believe you can use torlus' qemu branch for rpi to bypass the need for a special kernel and the memory and resolution limits. I don't know how stable it is though.
http://www.raspberrypi.org/phpBB3/viewt ... 72&t=26561

portets
Posts: 188
Joined: Sat Oct 29, 2011 6:24 am

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Wed Mar 20, 2013 8:37 am

Is it supposed to be very slow? On my Core 2 Duo it runs about half the speed of a real Pi.

I was hoping to compile programs this way, but it looks like it'll be faster on the Pi. At least with this method.

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Wed Mar 20, 2013 8:39 am

I haven't done any real testing, but day to day usage of command line apps I didn't notice it any slower than real Pi's. On a d525MW dual core Atom board.

I must do some benchmarking.

User avatar
ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6452
Joined: Fri Jul 29, 2011 5:36 pm

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Wed Mar 20, 2013 9:03 am

I have read that qemu is faster than the pi (in terms of cpu performance), but I suppose that really depends on your PC.

If you're just compiling, you don't need to emulate the whole system, you can use user-mode emulation quite well.

However, I believe cross-compiling is the way to go. Scratchbox2 is quite neat since you can install packages almost like you normally would - 'sb2 apt-get install libwhatever-dev', for example. Then you can compile things with or without qemu quite easily.
http://pastebin.com/4Jp1WPTb

exup
Posts: 3
Joined: Sun Aug 19, 2012 5:27 am

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Wed Mar 20, 2013 10:04 am

Hi

thanks I am not worried about the speed at the moment and just thought if its simple to add a bit more RAM (which I have shed loads of) I could adjust it. I did try 1024, 512 but it just hung with no error, black screen and the terminal did not say anything. (One thing I need to check in my host bios that I have virtualisation enabled)

For monitor/screen size I edited the file in the link you suggested but made no difference.
Lastly I edited the /boot/config.txt file to hdmi_mode=16 but that made no difference either. I suspect the QEMU must be virtualising the monitor somehow and may well not be seen as HDMI.

Thanks all I will monitor (get it!!) the thread if anyone has better luck. All in all though this is a quick and easy way to get PI on you Linux box, its going to be a cinch to change the image as they bring out new ones. Dare say occidentals will work as well.

Ultimately this is just to build up my skill level with pi whilst working on my main desktop. I know I could SSH into the real pi or VNC but this is handy when out and about for work. So its a great start for following along the MAGPI tutorials seeing what I can do. Then repeating the exercises and getting my kids to work on their real pi's. Most of which will be programming.

Now to build some robots!!!

Peter

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Wed Mar 20, 2013 11:24 pm

As a sideline, it looks like you got Ubuntu going on the D525MW. :)

I'm trying to think of a good reason to emulate the comparatively crappy Pi experience on something that runs far better natively......

Ahhh, forgot the first rule of geek : because you can.... :lol:
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Wed Mar 20, 2013 11:34 pm

Yeah, I had a crack from a USB stick, and it was perfect. I think maybe my DVD drive that I tried from initially is borked.

I am AMAZED at how well Ubuntu 12.10 runs on a dual core Atom machine.

And with a PicoPSU I'm only drawing around 35 watts of power at the wall.

I had a nice quiet WD Green 2TB drive but it died, so I have a noisy Seagate 500GB in there at the moment. Want to get an SSD so it will be totally silent. I was running Ubuntu straight from a flash drive for about 4 days - the silence was beautiful!

CPU 75degC with no fans running (middle of summer in a very hot region here). (For non d525MW owners, 75degC is perfectly acceptable for these!).

My Macbook PSU just died and this box is good enough to not make me rush out and buy a new PSU just yet.

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Wed Mar 20, 2013 11:36 pm

pluggy wrote:I'm trying to think of a good reason to emulate the comparatively crappy Pi experience on something that runs far better natively......
I am using Qemu to test routines in BRPI. I can trash and bork a system to my hearts content, and just copy a new filesystem over in 10 seconds flat to get it back to a fresh install.

Faster than re-imaging a real Pi :-)

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Thu Mar 21, 2013 12:05 am

I have 12.04 on mine and yes it does run very well. I had it down to about 26 Watts with a laptop drive, but it was a 5400 spin speed and it slowed it down. I have a 3.5" Seagate in there now, but mine is a quiet example, no fans and I can't hear it through the tinnitus. I also have a Pico PSU running mine. Never looked at the CPU temperature on mine, I don't work it hard and it doesn't get anything like warm here most of the time. It only feels warm if I stick my hand on the heatsink. (big hole in the back of the micro ATX case where the PSU used to be).

I've just been contracted to build an I7 desktop with 16GB of RAM with Ubuntu on it for a client. Might make an interesting comparison.....
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Thu Mar 21, 2013 12:09 am

PS. the new sig is a bit 'in your face', the regulars at my forum ( http://www.oneguyfrombarlick.co.uk ) would be up in arms.....
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

tulumbas
Posts: 4
Joined: Mon Feb 25, 2013 9:33 am

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Tue Mar 26, 2013 10:35 pm

tonyhughes, what version of qemu is used in your Ubuntu?... because in Debian wheezy qemu is just 0.9 and doesn't know 1176 cpu...

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Tue Mar 26, 2013 10:42 pm

tulumbas wrote:tonyhughes, what version of qemu is used in your Ubuntu?... because in Debian wheezy qemu is just 0.9 and doesn't know 1176 cpu...

Code: Select all

colleen@colleen-laptop:~$ dpkg -s qemu-system
Package: qemu-system
Status: install ok installed
Priority: optional
Section: misc
Installed-Size: 48930
Maintainer: Ubuntu Developers <[email protected]>
Architecture: i386
Source: qemu-linaro
Version: 1.2.0-2012.09-0ubuntu1

User avatar
Liber
Posts: 51
Joined: Sat Dec 01, 2012 12:31 pm
Location: Czech Republic

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Sun Mar 31, 2013 3:16 pm

Hi,

Is QEMU able to fully emulate GPU of Raspberry Pi ?( I would like to run a game.)

User avatar
ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6452
Joined: Fri Jul 29, 2011 5:36 pm

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Sun Mar 31, 2013 7:48 pm

Liber wrote:Hi,

Is QEMU able to fully emulate GPU of Raspberry Pi ?( I would like to run a game.)
Nope, that won't work.

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Sun Mar 31, 2013 9:28 pm

The Pi's GPU is a mystery even to the foundation I think. Without it being open sourced or substantially reverse engineered (That is probably a dirty word to Broadcom). I doubt very much will ever use it.
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

TheQuestor
Posts: 80
Joined: Wed Nov 28, 2012 9:15 am

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Mon Apr 01, 2013 1:33 am

I did a write up last week of almost the exact same thing

This is what I ended up using and it works perfectly :)

Code: Select all

How to chroot RPI envoronment.

Keep in mind this took me so many hours to figure out it's not even funny. I'm not exactly a nix guru by any means of the word.

The 1st part sucks as it is how you get the img ready.

There are two ways to do it but I found it easier just to do it this way.

The hard way is to use gemu to expand the img file and then some fancy fdisk stuff in the image to expand. Pfft

My Way:

I do this part all in windows so if you're a linux user you will need to know how to do dd and other linuxy things.

Download the latest Raspbian 2013-02-09-wheezy-raspbian.img from http://www.raspberrypi.org/downloads
http://downloads.raspberrypi.org/images/raspbian/2013-02-09-wheezy-raspbian/2013-02-09-wheezy-raspbian.zip

unzip it. 

Write to your SD Card [8 gig is plenty ] I use win32diskimager

Once completed insert SD into your RPI and boot it.

Run through initial setup screen paying attention to the "expand rootfs" as this one is really the only one of two parts that matter.

Run the last option which is like try to update.

Exit and reboot

log in

sudo apt-get install git-core
sudo wget http://goo.gl/1BOfJ -O /usr/bin/rpi-update && sudo chmod +x /usr/bin/rpi-update
sudo rpt-update and let it do it's thing

you CAN do an apt-get update && apt-get dist-upgrade now but it takes forever, much faster to do it once it's chrooted

!!this part is VERY important with 2013-02-09

sudo nano /etc/ld.so.preload
comment out anything in there. If you do not then it will bork when you try to run nearly any command with the following error:

qemu: uncaught target signal 4 (Illegal instruction) - core dumped
Illegal instruction (core dumped)

ok all done with the prepping of the image

sudo shutdown -h now

pull the SD card and put it in your reader.

Again I use win32diskimager
read it to an image. I named it the same as the original 
2013-02-09-wheezy-raspbian.img

when it's done writing the image [and it WILL take a while] you can pull the SD

on the nix box

sudo apt-get install qemu qemu-user qemu-user-static kpartx

I put all my Raspi images in /rpi-images and I mount it in /mnt/temp
so 
sudo mkdir /rpi-images
sudo mkdir /mnt/temp

sudo chmod -R 777 /rpi-images

transfer your newly created image into /rpi-images

now create the following scripts

[mountrpi]
#! /bin/bash
kpartx -a -v /rpi-images/2013-02-09-wheezy-raspbian.img
mount /dev/mapper/loop0p2 /mnt/temp
cp /usr/bin/qemu-arm-static /mnt/temp/usr/bin
mount -o bind /dev /mnt/temp/dev
mount -o bind /dev/pts /mnt/temp/dev/pts
mount -o bind /proc /mnt/temp/proc
mount -o bind /sys /mnt/temp/sys
mount -o bind /run /mnt/temp/run
#sets up the interfaces
cp /etc/network/interfaces /mnt/temp/etc/network/interfaces
#makes networking actually work
cp /etc/resolv.conf /mnt/temp/etc/resolv.conf
#where the magic happens
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/register
chroot /mnt/temp

[umountrpi]
#! /bin/bash
cd /devel
umount -l /mnt/temp/dev/pts
umount -l /mnt/temp/dev
umount -l /mnt/temp/proc
umount -l /mnt/temp/sys
umount -l /mnt/temp/run
umount -l /mnt/temp
kpartx -d -v /rpi-images/2013-02-09-wheezy-raspbian.img

make then executable
sudo chmod +x mountrpi umountrpi

ok you are now ready for the magic. But as a note I had issues trying this with sudo so jus do it as root. ie:su [password]

then just run ./mountrpi

if you followed my directions to a T you are now in your new chrooted raspbian environment
test it by
uname -a

Linux jothee 2.6.32 #42-Ubuntu SMP Fri Mar 8 23:20:06 UTC 2013 armv7l GNU/Linux

you can now do everyhing just as if you are running on your RPI. 
I tend to do a rpi-update just to make sure everything is good to go
and also do a apt-get update && apt-get dist-upgrade now

to unmount just type exit [enter] and then sudo ./umountrpi

Word of warning, you are ROOT when doing this on the rpi chroot so you CAN royally screw up the image if you're not careful. as in do not try rm -rf / because it WILL

at this point you are ready to set up your xbmc build environment which is icky.

I will follow up with that in the next post!

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Mon Apr 01, 2013 2:49 am

Valuable info above, which is absolutely worth its own thread, or people who arent running Linux might never see it...

TheOldMan
Posts: 1
Joined: Mon Apr 01, 2013 4:28 am

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Mon Apr 01, 2013 4:34 am

I am completely stuck on mounting the second partition in order to edit the ldconf file. When I attempt to mount 2013..img -o offset=62914560 /mnt, mount complains wrong fs type, bad superblock, etc... I can successfully mount the first partition. I'm on OpenSuse 10.2. file 2013..img shows startsector 122880 so my offset is correct. Can anyone point out what is wrong here?
Thanks.

**Update

I used OpenSuse 12.2 and the mount worked.

TheQuestor
Posts: 80
Joined: Wed Nov 28, 2012 9:15 am

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Mon Apr 01, 2013 6:57 pm

tonyhughes wrote:Valuable info above, which is absolutely worth its own thread, or people who arent running Linux might never see it...
I'll start a new thread later today with a bit more info on how to do it, such as how to edit the file in etc if you don't do it before creating the image. I'll also add how to use qemu to expand the image in case peeps don't have windows available. Plus I have to remember [read look up] how to do it using qemu as it's been a while since I did it that way.

That IS if you were talking about "my" post to begin with :)

Plus I am in the process of making a clean 8 gig image so people can just download it and use it as is.

Which forum should I post it in?

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: HOWTO: Virtual Raspbian on Qemu in Ubuntu Linux 12.10

Tue Apr 02, 2013 1:06 am

Same forum is fine - I dont think there is a Windows users forum. :)

Return to “Advanced users”