We can create a Linux virtual machine that can run on a Windows, Linux, or Mac OSX host. Our guest can be moved around on a USB drive or even synced via DropBox between machines. Best of all, the OS we will use for the guest is extremely small in size.
OS: Tiny Core Linux
I appreciate how small and simple Tiny Core Linux is. It’s size makes it the perfect guest OS for this project.
As an aside, the Tiny Core Linux group is fantastic. They don’t advertise and don’t ask for donations. They really care about making a great free product.
Create a directory called
tc to store all the files for this project.
Let’s grab the latest Tiny Core Linux iso. Once it is downloaded extract the
core.gz files from the
iso to our
tc folder. You can do this by mounting the
iso or opening it in some sort of archive manager like 7-zip and copying the files out.
You should have something like this after copying those files to our
Those files are our kernel and initramfs, respectively.
VM Platform: QEMU
QEMU is a wonderful, versatile, virtualization platform. It is not as popular (and not quite as powerful) as VirtualBox, but it is very simple and works cross platform.
I found that the builds by Eric Lassauge seemed the best for what I want to accomplish with this project. Download the latest Qemu Windows build
zip file (2.2.0 at the time this was written) from his website.
The most important reason I am going to suggest using the “lassauge builds” for this project is that the files are entirely portable. No installation of any sort is required on Windows. Download and copy the QEMU files to our
tc directory and you will have a simple, portable, working VM platform for Windows.
tc directory should look like this now.
C:\path\to\tc\Qemu-2.2.0-windows C:\path\to\tc\vmlinuz C:\path\to\tc\core.gz
There are a number of
qemu-system-*.exe files in
Qemu-2.2.0-windows that you do not need. You can safely delete all of them except for
qemu-system-i386.exe. That is the only binary we need to run our VM. Deleting the other system binaries will save us a lot of space.
On Linux, QEMU should be installable through your preferred package manager. On OSX, you should be able to use something like brew to install QEMU as easily as you could on Linux.
Building the virtual machine
Let’s create a hard disk for our VM. You may use the
qemu-img command that QEMU provides. This binary will either be in the
Qemu-2.2.0-windows directory we extracted if you’re on Windows, or it should be installed system-wide if you’re using QEMU on a Mac or Linux host.
qemu-img create -f vpc tc.vhd 1G
That command should generate a 1GB sparse virtual hard disk file. The disk should only be a few KB large, but has the ability to grow up to 1GB. You can use whatever size or file format you prefer for the disk image, but I prefer
vhd since Windows can natively handle that format as an added bonus.
tc directory should look like this now.
C:\path\to\tc\Qemu-2.2.0-windows C:\path\to\tc\vmlinuz C:\path\to\tc\core.gz C:\path\to\tc\tc.vhd
Now we can start our VM. You may prefer to script this so you don’t have to memorize the command.
Qemu-windows-2.2.0\qemu-system-i386 -L Qemu-windows-2.2.0\Bios -m 1024 -kernel vmlinuz -initrd core.gz -hda tc.vhd -append "home=sda1 opt=sda1 tce=sda1"
OSX or Linux
qemu-system-i386 -m 1024 -kernel vmlinuz -initrd core.gz -hda tc.vhd -append "home=sda1 opt=sda1 tce=sda1"
On Windows only, we need to specify the absolute path to the QEMU VM executable, and the
-L flag is needed to specify the path to our QEMU
Bios files (incuded with QEMU).
Regardless of the OS, we specify which file is our kernel, which is our initramfs, what to use as the primary hard drive, and finally, we pass some additional boot options to Tiny Core specifying where our home, opt, and tce directories live.
At this point, you should be done, and just need to customize your VM as you see fit.
Here are some neat things you can do with QEMU.
# Port forwarding, some ports require elevated permissions on your host. -redir tcp:80::80 -redir tcp:8000::8000
# Create a "shrunken" copy of your VHD file. # Remove any reserved space from the sparse disk. qemu-img convert -O vpc tc.vhd tc.vhd.copy
Qemu has an awesome way to let you access just about any virtual hard disk using the “Network Block Device” protocol. This will work on a *nix system only.
sudo modprobe nbd max_part=16 sudo qemu-nbd -c /dev/nbd0 /path/to/tc/tc.vhd sudo mount /dev/nbd0p1 /mnt/
10.0.2.2 is an IP address that can be used to access your host VM from within the QEMU guest.
Tiny Core tips
# Change the label of a partition to something like "hd01" sudo tune2fs /dev/sda1 -L hd01
# Mount persistent partitions by label tce=LABEL=hd01
# Specify an ISO image to mount when running the QEMU VM -cdrom /path/to/file.iso
The Computer Ramblings WordPress blog has a lot of helpful information regarding how to persist Tiny Core Linux directories on reboot so that our data isn’t wiped out every time. There are a few different methods when it comes to persistence, but I’ll let you figure out which you prefer.