Novice's mind blown by an application of one of Unix principles

In Unix, everything is a file (Unless It Isn’t — but that’s another story).

That statement is known widely enough to be recognized even by computer folks who never used Unix-like OS before. But knowing is one thing, applying is another.

So I watched a friend of mine last night who installed Ubuntu and then tried to run Windows under VMWare so he could use his printer and play some games. He knew the principle, he just never had an opportunity to apply it. Before.

“Why, you could just run the OS you already have installed on your HDD!”, I said. After a minute of staring in amazement1, and then some more to set things up, we had everything nice and running.

I found that to be pretty educating, so I’m sharing the experience bundled with some recipes to spread the knowledge.

So what’s going on here? Well, we all are used to the idea of virtual machine (VM) having a file on a disk representing its HDD. It usually has some special format, like qcow2 (used by Qemu), VMDK (VMWare Virtual Machine Disk) or VDI (Virtual Disk Image, used by VirtualBox), often providing additional features like compression and ability to grow as more files are written to it. But some VMs also support raw disk format, which is just a stream of bytes. Keep that fact in mind.

Now we return to the Unix principle I mentioned in the beginning. Since everything is a file, partitions of your HDD — why, even your entire HDD — is just a file somewhere under /dev. If you have a SCSI drive, it’s /dev/sda (the second one would be /dev/sdb, third /dev/sdc and so on), in case of ATA the name would be /dev/hda (with the same rule to get names for second, third and so forth). These are block devices, meaning they aren’t your ordinary files, but they’re still files of some kind! All you need now is a virtual machine that supports them as a disk format.

Before reading any further, please note that booting the same operating system twice (once as a host, second time inside a virtual machine) will most likely damage your files and corrupt the system.

Loading things in read-only mode is okay, though, so you are safe to boot LiveCDs — just don’t mount your /home or any other volume in read-write mode if it’s already mounted somewhere else.

You can also safely boot things that aren’t loaded yet, so if you have dual-boot in place you can load second OS while running the first [yo-dawg poster needed].

As a matter of fact, pretty much any VM solution you might care about does support raw disks. In case of my beloved Qemu, all you need to do is start your virtual machine like that:

$ sudo qemu /dev/sda

That’s what I like about CLI applications: the command feels natural and quite obvious. As we will see next, GUIs tend to cloud things a lot in this case.

There’s one little detail still worth mentioning: you probably won’t have permissions to read and write on /dev/sda (or whatever device you need). That’s exactly why I ran Qemu with sudo a couple of paragraphs ago. The cleanest way to fix this is to look up which group owns the file you need, check if you’re in it and if not, add yourself in: