Installing Ubuntu on a USB stick with QEMU-KVM

The more I learn about QEMU the more uses I find for it. Today I needed to install Ubuntu onto a USB memory stick. My normal process for doing such a thing is simply to create a bootable usb stick using usb-creator with the command:

usb-creator-gtk

What that gives you is a FAT32 formated usb with the Ubuntu installer and an option to “Try Ubuntu”. What I was after was to use the USB stick as a regular hard drive, formatted as normal with EXT4 and install Ubuntu on that. After a little headscratching I realized KVM would be perfect for this. Tell it to use the USB device file as the hard drive and the Ubuntu install ISO as the CDROM. Here is the command:

sudo kvm -hdb /dev/sdb -boot d -cdrom ~/Downloads/ubuntu-12.04-alternate-amd64.iso

The -boot flag identifies the device it should boot from with “d” being the shorthand for the first CD drive while -cdrom allows you to specify an iso to use as a disk. The really cool part of this is the -hdb flag that points to the device file for the USB drive. Slap the enter button and you are off to the races.

Having the flexibilty to do stuff like this is really awesome and saves me not only time but diskspace too, since I don’t need to keep creating Virtualbox VM’s. I’m going to have fun learning more about this.

Testing a live CD with QEMU-KVM

I read an article recently on Ars Technica about the recent design changes in Gnome. Over all it looks like Gnome is pushing forward with some interesting ideas and the article ended with a note that a live CD was available on the Gnome site that showed off the new interface. Creating a new virtual machine seems a little heavyhanded just to take a peek at the latest Gnome build so I am starting to use QEMU for that sort of thing. Here is all you need to do:

kvm -m 1024 -vga vmware -boot d -cdrom ~/Downloads/GNOME-3.4.iso

This kvm command comes from the qemu-kvm package which would obviously need to be installed. You may notice it resize a bunch of times before it settles down. This is a known bug, that is easily worked around by just giving it a few seconds to work itself out or resizing the window yourself. Anyway, that aside this a pretty useful way to test a live CD. Enjoy!

Update: 2014-06-14: Things have changed a little since I wrote this.
First, this works much better if your processor supports virtualization. You might need to turn this on in your BIOS. If your CPU supports it the following command will tell you. You can see ‘vmx’ highlighted in the output below:

mike@sleepycat:~☺ egrep '(vmx|svm)' /proc/cpuinfo
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm ida arat epb xsaveopt pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms

This means we can pass -enable-kvm to the new qemu-system-x86_64 command to get it to use the Kernel-based Virtual Machine (KVM) which gets the kernel to take advantage of our hardwares virtualization capabilities:

qemu-system-x86_64 -m 6144 -vga std -enable-kvm -boot d -cdrom ~/Downloads/gnome-3.12.iso

You can see I am throwing more RAM at this thing (-m 6144) and using a different vga adapter to get more resolution (-vga std). Don’t forget that if you need to run a 32 bit OS you need to use qemu-system-i386.