Virtually Free Rapsberry Pis

One of the nice things about the Raspberry Pi is that it runs Linux and you can do a lot of development right on the board. The converse of that is you can do a lot of development on a Linux desktop and then move things over to the Pi once you get the biggest bugs out. However, sometimes you really need to run code on the actual platform.

There is, however, an in-between solution that has the added benefit of upping your skills: emulate a Pi on your desktop. If you use Linux or Windows on your desktop, you can use QEMU to execute Raspberry Pi software virtually. This might be useful if you don’t have a Pi (or, at least, don’t have it with you). Or you just want to leverage your large computer to simplify development. Of course we would be delighted to see you build the Pi equivalent of the Tamagotchi Singularity but that’s a bit beyond the scope of this article.

Since I use Linux, I’m going to focus on that. If you insist on using Windows, you can find a ready-to-go project on Sourceforge. For the most part, you should find the process similar. The method I’ll talk about works on Kubuntu, but should also work on most other Debian-based systems, including Ubuntu.

Step 1. Tools

Your PC probably has some sort of Intel processor in it. The Pi has an ARM chip. QEMU is the emulator that lets you run–among other things–ARM executables on the PC. You need a static copy and the binfmt-support package. This combination will let Linux recognize alien executables and run them transparently. Here’s how you install what you need:

apt-get install qemu qemu-user-static binfmt-support

Step 2. Raspberry Pi Image

Make a working directory and download a Pi image in zip format. You can then unzip it, to create an IMG file. Here are the steps:

wget https://downloads.raspberrypi.org/raspbian/images/raspbian-2015-11-24/2015-11-21-raspbian-jessie.zip

unzip 2015-11-21-raspbian-jessie.zip

Step 3. Get Data

We need a few numbers from the img file and yours may be different than mine, depending on what you downloaded. The command you want to run is:

fdisk -lu 2015-11-21-raspbian-jessie.img

Here’s the output, with the important parts in bold:

Disk 2015-11-21-raspbian-jessie.img: 3.7 GiB, 3934257152 bytes, 7684096 sectors 
Units: sectors of 1 * 512 = 512 bytes 
Sector size (logical/physical): 512 bytes / 512 bytes 
I/O size (minimum/optimal): 512 bytes / 512 bytes 
Disklabel type: dos 
Disk identifier: 0xea0e7380 
 
Device                          Boot  Start     End Sectors  Size Id Type 
2015-11-21-raspbian-jessie.img1        8192  131071  122880   60M  c W95 FAT32 (LBA) 
2015-11-21-raspbian-jessie.img2      131072 7684095 7553024  3.6G 83 Linux

The first partition is the boot partition and the second is the filesystem.

Step 4: Increase Hard Drive Size

The base filesystem isn’t very large, so let’s make it bigger:

dd if=/dev/zero bs=1M count=2048 >>2015-11-21-raspbian-jessie.img

This will add 2 GB to the image. However, the filesystem won’t know about it yet. First, we need to mount the image as though it were a set of disks:

losetup -f --show 2015-11-21-raspbian-jessie.img

losetup -f --show -o $((131072*512)) 2015-11-21-raspbian-jessie.img

The numbers (131072 and 512) must match the ones highlighted in step 3. The 512 probably won’t change, but the start of the filesystem can and does change.

Unless you already had loopback devices, this should give you a /dev/loop0 that corresponds to the whole “disk” and /dev/loop1 that is the filesystem (the part we want to expand). Recreate the partition table to know about the extra space (the mkpart command will need some numbers you can read from the output and those numbers are marked in bold):

$ sudo parted /dev/loop0
GNU Parted 3.2 
Using /dev/loop0
Welcome to GNU Parted! Type 'help' to view a list of commands. 
(parted) print                                                             
Model: Loopback device (loopback) 
Disk /dev/loop0: 6082MB 
Sector size (logical/physical): 512B/512B 
Partition Table: msdos 
Disk Flags:  
 
Number  Start   End     Size    Type     File system  Flags 
 1      4194kB  67.1MB  62.9MB  primary  fat16        lba 
 2      67.1MB  3934MB  3867MB  primary  ext4
 
(parted) print

(parted) rm 2

(parted) mkpart primary 67.1 6082

(parted) quit

$ e2fsk -f /dev/loop1
$ resize2fs /deve/loop1
$ losetup -d /dev/loop[01]

Step 5: Mount Filesystem

Make a directory to use as a mount point. I usually just make a directory called mnt in the current directory. You’ll need to be root to mount and use the offsets found in step 3 (8192 and 131072, in this case):

mkdir mnt
sudo mount 2015-11-21-raspian-jessie.img -o loop,offset=$((131072*512)),rw mnt
sudo mount 2015-11-21-raspbian-jessie.img -o loop,offset=$((8192*512)),rw mnt/boot
sudo mount --bind /dev mnt/dev
sudo mount --bind /sys mnt/sys
sudo mount --bind /proc mnt/proc
sudo mount --bind /dev/pts mnt/dev/pts

Step 6: Modify ld.so.preload and Copy Executable

As root, you need to edit mnt/etc/ld.so.preload with your favorite editor (which is, of course, emacs). You could, if you wanted to, use vi or some other editor of your choice. Just make sure you are root. Just put a hash mark (#) in front of the line you’ll find there. Failure to do this will cause illegal instruction errors to pop up later.

In addition, you need the qemu-arm-static executable available to the new fake Raspberry Pi:

sudo cp /usr/bin/qemu-arm-static mnt/usr/bin

Step 7: Chroot!

The chroot command will cause the Pi’s filesystem to look like the root directory (in one shell window). Then you can change user to pi (or any other user you want to configure):

cd mnt
sudo chroot . bin/bash
su pi

Using It

How do you know it is working? Try a uname -a command. You should see an armv7l processor type. You can run executables just like on the Pi. If you want to use X, set DISPLAY=:0 to use your main screen. For example:

DISPLAY=:0 dillo

Or you can set the display for all programs:

export DISPLAY=:0

Optional: Flash It

Now you are ready to go. After some development, you might want to save your work. All you need to do is to exit the chroot (type exit) and change /etc/ld.so/preload back (remove the # character you added). Then you should unmount everything that you mounted. The image file will remain and will contain all your changes. You can flash it, and you can reload it by repeating the chroot and mount instructions.

In Summary

There are lots of ways to do cross development on the PC for the Raspberry Pi. This is one of them. However, it also points out the power of using QEMU to run alien executables. That’s a trick you could use for lots of different development boards and platforms. There are QEMU emulators for several CPU types ranging from Microblaze to other ARM variants to PowerPC, to an S390. A quick glance at the /usr/bin directory for qemu-*-static will give you a list.

Sure, it is easy enough to get a Pi and use that. But this is yet another tool to do cross development when you need it.

54 thoughts on “Virtually Free Rapsberry Pis

  1. I did this a few years back before buying my first Pi (of now many). Was a great way to experiment and learn before committing. I wrote some python scripts and other doo-das to simulate a home alarm web interface I wanted to create, and once I did I bought the actual hardware. I had written something in Windows and while it ran it would hang up/not respond at inopportune times. It’s been great on the Pi, probably the most reliable piece of equipment I’ve had, it just runs and allows me to arm/disarm my alarm from my cell, get text’s when events occur and frankly it just never misses a beat. Good stuff!

    1. As with all things, it depends somewhat on your hardware. The nice thing with Linux (I think) is that there are many desktop environments, each with different demands. With Mint, Cinnamon will be a little heavier on resources, but very pretty. XFCE is on the other end of the spectrum – lightweight, but not whiz-bang.

    2. If your hardware is not Linux-friendly, you will have bad time. Even with proper hardware Linux might just blow in your face for no apparent reason. Few days ago I read a comment by someone, who broke his Linux by changing wallpaper. If you want to check it out though, then grab VirtualBox and run it that way first, to see if you like it at all.
      One other interesting point: Raspbian on RPi 3B was my first Linux distro that worked out of the box with no tinkering at all…

      1. This +1,
        I’ve purchased Asus, Acer, IBM, and Dell specifically for this reason. However, we always check that there is a documented history of a Ubuntu Mint/Debian/Gentoo install success. Intel/nVidia GPU, popular network/wifi/sound-card chips, and BIOS that doesn’t require OEM software to configure.

        Sony, and a few Acer(LCD)/Asus(gigabit Lan) laptop models are probably our worst experience.
        Notably, I purchased the Brother network laser printer because it supports Mac and Debian variants like Mint.

        For most people, dual-booting with linux as the default is like quitting smoking unless you already use open source software. After about a year, you will find the only time you’ll load windows is for some special hardware driver win10 doesn’t support anyway, old PC games, or a CAD/CAM package from the 1990’s.

        Most tend to prefer a performance tweaked classic gnome desktop (a bit like macOS and XP combined), but one can load multiple GUI versions to find one that suites your usage needs:
        https://askubuntu.com/questions/58172/how-to-revert-to-gnome-classic-desktop

        1. You’re wrong. I use Linux for a lot of my software development and experimentation, but I use Windows for writing and doing non-computer work because it has slightly better power management. My laptop is quite capable of dual booting, and it took me about 3 hours to get it running in full. Nothing like what you’re claiming is the “typical use case.”

          Lenovo by the way.

    3. The computer is a Dell from 2002. XP of course still on it. Backup Everything Day is coming soon. I want it to look like XP because I’m an ass and stuck in my ways. I will do command line when I know I wont miss what I might lose.
      I’ve heard it’s more stable but eccentric. Maybe that’s the wrong word, but I wouldn’t know. I’ll have extra HDD’s just in case. :)

      1. I have always been partial to debian-based distributions, as that is what a learned back in the compile you own kernel overnight to get linux hopefully working on a Pentium I 300MHZ laptop days…

        Grab a Live CD ISO of Mint or Ubuntu and give it a shot. I have had relatively few issues with DELL PCs and the live CDs because people often re-purpose “old” (I probably have a GX280 P4 out in the shed with linux on it and a dead PSU) DELL boxes for linux with the OEM hardware.

        Getting linux to look like XP can be done, but I’m not sure it’s worth it unless it’s a distro that looks that way out of the box.

        And by all means, I’m not saying stick to ONLY Mint or Ubuntu, those are just the ones I am familiar with. Once I learned Debian, I personally had a harder time with other distros that don’t use apt-get because I have those command line controls permanently etched into my brain. I’m sure there are more user-friendly distros out there these days.

        Just try out a couple Live CD/DVDs, or if supported, use the Live “CD” on a bootable USB stick. Worst case, you format the stick or use the CD/DVD as a coffee cup coaster.

        1. For that age computer Mint would be a better choice. Maybe even one of the lighter desktops. The standard Ubuntu has moved away from being useful on older machines – they want the latest and greatest graphics, touch etc.

          1. I ran LXLE on EeePC netbook and in VirtualBox. It’s quite nice and lightweight. Unfortunately it wasn’t working too well on an older laptop that was XP-native. After RAM upgrade it just refused to boot into GUI. On related note none of Linuxes tested would boot into GUI first time because graphics chipset requires some modifications to X-Window server because it is obsolete and needs vesa-openchrome. In my opinion Linuxai are either for specific people or for specific uses. They are great as embedded systems or servers, but as desktop OS alternatives they sometimes fail spectacularly, and sometimes just die like Windows ME…

      2. To get it to *look* like XP, there are themes for KDE and Xfce that will do the trick. To get it to *act* like XP does on the desktop is another matter. KDE 4 most closely follows the Windows paradigm but the subtle differences will be annoying. FVWM can be made to emulate the XP experience almost perfectly, but it’s a hassle to set up.

        In other words, it may be easier to just try the different Linux desktops and settle on one that meets your needs. They are all customizable.

      3. Here is the truth: Linux does not have any good graphical operating system. All of them are amateurish and unpolished.

        The people who keep claiming that Linux is sooo superior to your pathetic XP simply have no idea what they are talking about. They are really comparing the command line interface of preview-release Windows 95 to bash, because somehow that is all they ever used. You’ll be surprised to find that most of all those Linux “power users” never cared to learn about hotkeys, use Bold instead of format templates in their word processor, and are content to declare their window environment superior to yours because theirs spin their nude anime desktop wallpaper in three dimensions. Oh shiny!

        Windows XP with the latest service packs (including the unofficial ones) is a great operating system, an incredible amount of attention to detail has gone into Windows, and the operating system has had 3 decades to mature. You would probably be very happy with Windows 10, unless you care about being spied on. It is simply the graphical user interface that sucks the least.

        If you want to adopt to Linux, you will be much happier if you learn to use the command line. It is simply not realistic to use Linux without it. The least painful way to do that is to start in a virtual machine like Virtualbox and set up something on Linux useful to you. In my case that was a web server. Learning to configure and administrate Apache from the Linux command line was a great way to learn about Linux, because it never felt stupid.

        There are Linux distributions that are designed to look like XP, but they will only frustrate you, because the neckbeards that create Linux distros never do more than the easiest 10% of the work on anything, before they all erupt into fighting amongst each other and abandon the project, with one deranged russian keeping the project half-alive enough for the next decade to lure in unsuspecting passers-by who didn’t get the memo, and thus waste everybody’s time.

        For those reasons, those “perfect XP clones” will look superficially like XP to the ignorant (them) but in the end you will have to use it like a regular semi-functioning Linux desktop environment with lots and lots of command line intervention, only most things will be hidden from you behind layers and layers of undocumented “frameworks” (half baked scripts that make it look somewhat like XP).

        In the end, the only thing “Libre” development has shown itself good for is to make simple clones of established proprietary software that some company has already spent the required resources to actually design and develop. Thus, if you are happy with Emacs, vi and bash, you are in luck; Linux is great for those. Graphical user interfaces are too complex for the nerds to copy, which is why the most decent Linux graphical environments are Google’s Android and ChromiumOS.

        What you want is TinyCore Linux. It will run great on your old machine, and you will have a minimal system that you can understand piece by piece. Download their CorePlus iso and chose JWM as the window manager. Install Firefox (or Dillo), and then it’s time to start browsing Linux command line tutorials.

        You can also chose Lubuntu, but it will be harder to learn.

        1. The real joke here is that you managed to write nine paragraphs defending the beauty and functionality of a modern Windows system… and you chose XP.

          Do you really think all Linux users “spin nude anime desktops” in their free time? Please check the date; it’s 2016. 1996 came and went.

          Man, are you out of touch.

          1. From notarealemail who I was replying to: “The computer is a Dell from 2002. XP of course still on it.”

            My post also explicitly said that Windows 10 is better. Whether Vista, 7 or 8 was better than XP is debatable, but 10 is.

            In any case, my point was that good old Windows XP is so much better than any current Linux desktop environment that it tends to come as a shock for anybody switching to Linux. You see, if you have stuck to Windows XP for 15 years, you know it very well and you have come to expect a lot of things from a DE. Like, it actually working. On the other hand, you are constantly reminded that you run an severely outdated system, and all the Linux hype makes it sound like Linux will be a million times better, faster, more polished, “just work”, etc. And you believe this, because obviously XP is very old. Thus the shock and utter disbelief when finally making the jump.

            And you are right about the spinning anime, I apologize. These days it’s probably nude ponies.

        2. Lubuntu is also ridiculously easy to learn. Good joke there with the “deranged Russian” and “neck beard” comments; shows how much you respect the kind of people that built our network infrastructure and let you download p0rn to your “beautiful Windows XP” desktop folder (did you give it an invisible icon?).

          Honestly, have you even tried Ubuntu lately? Xenial Xerus? It’s way more slick than XP ever was, and it’s maintained by Canonical (an actual big-boy corporation!).

        3. Obvious troll is obvious! Thanks for the impassioned defense of XP, though. lol No, seriously. I’ve still got two OEM licenses for XP Pro x64 Edition. That was my go-to OS all the way through Vista’s (a.k.a. Windows AIDS Edition) lifetime.

          Also, +1 for “…and are content to declare their window environment superior to yours because theirs spin their nude anime desktop wallpaper in three dimensions.” I got a good laugh out of that one. :]

      4. Here is the truth: Linux does not have any good graphical operating system. All of them are amateurish and unpolished.

        The people who keep claiming that Linux is sooo superior to your pathetic XP simply have no idea what they are talking about. They are really comparing the command line interface of preview-release Windows 95 to bash, because somehow that is all they ever used. You’ll be surprised to find that most of all those Linux “power users” never cared to learn about hotkeys, use Bold instead of format templates in their word processor, and are content to declare their window environment superior to yours because theirs spin their nude anime desktop wallpaper in three dimensions. Oh shiny!

        Windows XP with the latest service packs (including the unofficial ones) is a great operating system, stable, fast, comprehensive, logical (mostly). An incredible amount of attention to detail has gone into Windows, and the operating system has had 3 decades to mature. You would probably be very happy with Windows 10, unless you care about being spied on. Windows is quite simply the graphical user interface that sucks the least.

        If you want to adopt to Linux, you will be much happier if you learn to use the command line. It is simply not realistic to use Linux without it. The least painful way to do that is to start in a virtual machine like Virtualbox and set up something on Linux useful to you. In my case that was a web server. Learning to configure and administrate Apache from the Linux command line was a great way to learn about Linux, because it never felt stupid.

        There are Linux distributions that are designed to look like XP, but they will only frustrate you, because the neckbeards that create Linux distros never do more than the easiest 10% of the work on anything, before they all erupt into fighting amongst each other and abandon the project, with one deranged russian keeping the project half-alive enough for the next decade to lure in unsuspecting passers-by who didn’t get the memo, and thus waste everybody’s time.

        For those reasons, those “perfect XP clones” will look superficially like XP to the ignorant (them) but in the end you will have to use it like a regular semi-functioning Linux desktop environment with lots and lots of command line intervention, only most things will be hidden from you behind layers and layers of undocumented “frameworks” (half baked scripts that make it look somewhat like XP).

        In the end, the only thing “Libre” development has shown itself good for is to make simple clones of established proprietary software that some company has already spent the required resources to actually design and develop. Thus, if you are happy with Emacs, vi and bash, you are in luck; Linux is great for those. Graphical user interfaces are too complex for the nerds to copy, which is why the most decent Linux graphical environments are Google’s Android and ChromiumOS.

        What you want is TinyCore Linux. It will run great on your old machine, and you will have a minimal system that you can understand piece by piece. Download their CorePlus iso and chose JWM as the window manager. Install Firefox (or Dillo), and then it’s time to start browsing Linux command line tutorials.

        You can also chose Lubuntu, but it will be harder to learn.

        1. Someone on the Internet is wrong!!!!

          Come on over, and drool over my Linux desktop, why don’t you. It’s really pretty. Oh, and I did no special work to get any drivers or anything…everything I plug in just works (and I plug in a lot of non-standard stuff, too!) unlike Windows, where you may or may not be able to install a driver, and the auto-install of drivers may or may not work. As for software, let’s see…Mozilla Firefox/Netscape was released one year before Internet Explorer. Yes, it was not open-source *then*, but it is now. So at least it is not a “simple clones of established proprietary software”… Oh, and same story with Openoffice/Staroffice…except it beat MSOffice by more than 1 year!

          1. A bit too true to be polite?

            I am not trolling, just giving a helping hand to another poor soul who held out with XP and is now abandoning ship for Linux.

            Microsoft spent a lot of resources and man-years to massage their big mess of a clusterf*** into a very usable operating system.

            If only the open-source community would have put 10% of this effort into doing the same for Linux! But, no, they decided to spend 10000% of that effort into producing 1000+ different incompatible half-baked mockups of a GUI, all forked because of some tiny insignificant detail didn’t sit nicely with their PMS that particular day, and instead of contributing a nice cleanly documented patch, they all think “lol none of those dorks know sh*t about programming unlike me I can hack up a GUI a million times better than Wimpd00ze in a weekend. And I code much faster than those f*ggots because I save typing time by not writing any code comments”.

            Firefox, OpenOffice and Apache are all great examples of great open source software… …whose development was all paid for by megacorps in order to destroy the market for other megacorps.

            Every new Linux user has to spend at least 6 months of their time jumping between every single one of the million different desktop environments, thinking “oh man RTFWM sure s*cks, how stupid I am not to try this great LXWTF that everybody’s talking about” ad infinitum. The truth is, they all s*ck! And you need to learn to use the command line, because the “standard” CLI utilities were actually _developed_, _on purpose_ back in the Unix days, and so they are actually useful.

            I am not trolling, just jaded by disappointment at Linux, because I believed the hype from all you beards.

    4. My main development machine has uptime of 190 days currently, last boot was because of power outage. Not sure if rebooting one-twice per year is often, but I’m OK with that. It is the cheapest hardware bought three years ago, running Linux Mint.

        1. But beware that migration from Windows to Linux is not trivial. You’ll have to change a lot of your habits – been there, done that. It took me five times to try, return to Windows and finally settle at Linux side, but nowadays I wouldn’t return back.

          1. This. The only reason I have Windows these days is for my gaming PC, and my particular Nvidia card (GTX 750 Ti) has issues with the Linux driver (but then, the Windows driver has failed me a few times recently, as well). All the other systems in my home and office are Linux or OpenBSD.

        2. Linux doesn’t need rebooted EVER, except for kernel (and MAYBE graphics, I don’t recall) updates, and most distros will let you continue using the old kernel until you reboot at your leisure.

          My linux system consist of Arch w/ LXDE desktop. Light and fast as can be, but it is considered “bleeding edge”. The install is extremely manual, but there are hundreds of walk-throughs online to make it simple.

          I also have a home built home server running debian headless. It shuts down and boots up on a schedule, just because of power usage concerns, but it has been running for about 6 years without issue.

    5. I switched to linux after my three year old budget laptop suffered a fragged harddrive and stopped spinning. I’ve used linux mint on decade old laptops, broken hunks of crap with no screen, damaged keyboards and less than a gig of ram. It ran like a dream. I’m now using it on my 4 core I5 desktop with 8 gigs of ram.. in more than five years, linux has never hung on me once. Not even when i ran it for a year off a usb. Make the switch. You’ll be gad you did.

  2. I did it to compile some big libraries I had to customize (OpenCV and other stuffs) because it take far less time than compiling on a real pi. After that I just copied files on the pi and they worked great. Saved many hours of compilation time :)

    1. With a bit (!) of work, there is no reason that they shouldn’t be (with limitations). [Warning: I’ve not tried any of the following, I just did a web search]:

      For the basic concepts, but on another board (“Bifferboard, 486 with GPIO): https://sites.google.com/site/bifferboard/Home/howto/qemu and https://lists.gnu.org/archive/html/qemu-devel/2009-09/msg00023.html

      At least one person has had a go for the ‘Pi itself: RPi GPIO emulator https://sourceforge.net/projects/rpigpioemulator/

      For more direct connection to real hardware, instead of a GUI emulating the pins, a connection could be made to a board such as RTK.GPIO (warning: a KickStarter link follows): https://www.kickstarter.com/projects/ryanteckltd/rtkgpio-a-usb-raspberry-pi-compatible-gpio-header

      The limitations are obviously going to be timing based mainly (and the RTK.GPIO is described as having interfaces it doesn’t handle, if you went that far) but that is only to be expected.

  3. I’m STILL trying to figure out exactly what a “Rapsberry” pi is….? Is it like a Raspberry pi, but starts rapping when you boot? Or has rap lyrics silkscreened on the PCB?

    ;)

  4. Given that /etc/ld.so.preload doesn’t exist on a jessie image, what is the line we’re supposed to be removing?

    Also, it doesn’t work for me on a Debian Sid host, I just get:

    # chroot . bin/bash
    qemu: uncaught target signal 4 (Illegal instruction) – core dumped
    Illegal instruction

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s