How To Make a Stand-alone QEMU Bundle on Mac OS X

by Mike Levin SEO & Datamaster, 07/29/2010

Despite the fact that the Mac has this wonderful concept of “application bundles” that lets you masquerade a directory as a program to carry around all your resources and dependencies, this version of Q hasn’t gotten it exactly right. Oh, it bundles it alright including everything required to run the virtual machine, I found out. It’s just that double-clicking the resulting icon doesn’t do anything.

The good news is that I found a way around this. But I get ahead of myself. First, let’s make the stand-alone bundle.

If your virtual machine is running, shut it down (shutdown -h now). Next, go to the Guest PC dropdown menu, and select Export Guest PC to Flash Drive:

It will prompt you a couple of times to hit OK, and when it’s done, you should have a Debian bundle sitting on your desktop. If you saved it somewhere else, drag-copy it to your desktop because the next step will be relative to your desktop. This looks like a file, but it’s actually a directory. You don’t need to, but you could actually right-click on it and select “Show Package Contents”.

If you double-click it, it should actually work, but that’s only because the Q program is still on your machine. Now it’s time to quit out of Q and delete both the .dmg file and the virtual drive that the Mac mounted. Drag them both to the trash and empty trash.

The “stand-alone” Q bundle now will stop working. I’m sure this stand-alone Flash drive feature of Q was tested without ACTUALLY deleting all other occurrences of the Q program from the host machine. Anyway, that’s what I did, and it immediately stopped working. So much for stand-alone. Let’s fix that, and give ourselves a little more power in the process.

Open a Terminal. If you’re not familiar with Unix terminals on OS X, well get used to it quick because this is all about these Unix-like type-in interfaces. The Mac’s is very much like Linux. Just go to the Mac Spotlight magnifying glass in the upper-right of your Mac screen and start typing Terminal. It will almost immediately become your first option. Hit Enter, and bam! You’ve got a terminal:

These terminal windows generally default to using your home directory as the starting directory. That means that getting to your desktop is as easy as typing:

cd Desktop

You are now in the same directory as Debian.app. Remember, this is case sensitive. And relative paths matter. By leaving off any initial slashes in the path, it’s looking for Desktop in your current directory, which happens to be there given the Mac’s file structure. Now type:

ls

You will see everything that you have littering your desktop. You should see one named Debian.app. Now, cd into that. You can ls and look around on the way.

After a bit of snooping around the directory structure myself (yes, Debian.app is a directory), I have determined that the QEMU executable is the file Debian.app/Content/MacOS/i386-softmmu and the harddrive.raw file is buried away in Debian.app/Content/Resources/Guest/Debian.qvm/harddrive.raw

So, we can make the educated guess that this command should get this thing running:

cd Debian.app/Contents/MacOS ./i386-softmmu -hda ../Resources/Guest/Debian.qvm/harddrive.raw

…and so, it does. Hooray for my insight. Now for the REALLY tough part (everything is easy until you want to click an icon). Because you see, I have also discovered where we can swap out the reference to the executable in an xml file named Info.plist, and replace it with a Unix shell script, but here’s the punchline: the current directory of this shell is system root, and not the directory it was loaded from.

Think about that. This is a portable bundle of files, sitting on our desktop, and soon to be drag-copied to a USB drive. To make it work, we have to use hard-wired paths. But we can’t go hard-wiring absolute paths, or we lose our portability. So after extensive Googling, I found a way to change the current directory to the location of the, allowing relative paths (the comic book cursing you see below).

It’s time to practice vi again. So, go to your terminal, make sure you’re in the MacOS directory, and type the following. Don’t forget, if it gets strange, hit the [Esc] key, and when you want to start typing again, hit the i key (for insert). And :q! will exit without saving your changes.

vi Debian.sh i #!/bin/bash cd “${0%/*}” ./i386-softmmu -hda ../Resources/Guest/Debian.qvm/harddrive.raw [Esc]

:w :q

The vi command to exit could also be expressed as :wq (write and then quit). I’ve been showing it one command at a time to ease you into it. I think that will be a running theme throughout the site, getting more and more into vi.

The end result should look something like this (right before :q, which exits):

Now we have to edit one file that’s one directory level up, so type:

cd .. vi Info.plist

Now, arrow-key down to where it says i386-softmmu (under where it says CFBundleExecutable). Thank goodness arrow-keys work in the version of vi that ships with OS X, or you’d REALLY be cursing me right now. Once you’re on the “i” of i386-softmmu, hit the x key until it just looks like:

“x” deletes characters in vi without going into insert mode. Now, hit “i” to go into insert mode, and simply type our script name, to make it look like:

Debian.sh

Then, type:

[Esc] :wq

Now try double-clicking the Debian icon. BAM! Now you’ve got a double-clickable, draggable virtual machine on your Mac. And we get the extra little advantage of that one atomic unit on the Mac–a directory that looks and works like a program, so you can drag its dependencies around. It won’t be quite that smooth on the PC, but we’ll try.