> trial and stderr

Installing Ubuntu on a USB Drive


After many, many failed attempts, I finally got this working, and nonetheless in a very convenient fashion. I also got the flash drive Ubuntu running within VirtualBox, so I can administer it from the comfort of my host machine's OS. Awesome.

If you're not interested in knowing what attempts failed, just jump down to Doing it Right. Or jump to the section on booting the drive in VirtualBox.

What Went Wrong

I made a number of attempts using two flash drives, one as the install disk, one as the target disk. None of these attempts worked.

  • Following this guide: Ubuntu Desktop with manual partitioning. 16GB FAT32 mounted to /UDISK as the first partition, 16GB ext4 mounted to / as the second partition. "Device for boot loader installation" set to the disk itself, /dev/sdb. Failed to produce a bootable drive.
  • Same thing, but with "Device for boot loader installation" set to the second partition, the one mounted at /. Same result.
  • Following this kid (you really should watch a bit of the linked video, it's kind of awesome): Ubuntu Desktop with manual partitioning. Whole 32GB drive allocated ext4, mounted to /, bootloader installed to /dev/sdb. Failed to produce a bootable drive.
  • Following this excellent guide, but using two flash drives, to install Ubuntu Server actually worked! And no manual partitioning was required, I just hit "Use entire disk and set up LVM" as specified in the guide. But it also messed up the UEFI settings on the host machine, which was fun to try to fix (for the curious, this is a fantastic piece of writing about UEFI). In the comments of this guide the guy mentions that he used a virtual machine, which led me to
  • Following this guide to install from ISO running on QEMU as a virtual machine. This was my first attempt at doing a single-USB install. I wasn't, however, ever able to penetrate the labrynth of arcane errors QEMU produced en route to actually running the installer ISO.

Doing it Right

I'm going to explain how to install Ubuntu to a flash drive, and then how to administer it, all using VirtualBox.

You only need one computer, and one flash drive. I'm running Ubuntu 18.04, and installing Ubuntu Server 16.04 on a USB 2.0 flash drive. YMMV with other combinations, but I wouldn't be surprised if this works for many other OS permutations. Note that, once you're set up with Ubuntu Server, you can easily switch to Ubuntu Desktop.

Also, I hear it can be done with a USB 3.0 flash drive, but VirtualBox won't like it, so you'll need a 3.0-to-2.0 adapter.

Download your ISO


Set up VirtualBox

We'll need to install virtualbox and add your user to two groups, vboxusers and disk.

Security note, verbatim from here: Adding your user to the disk group can be dangerous and a security risk. It is as dangerous as having sudo access to write to a raw disk.

vboxusers is needed to connect to USB drive at all. disk will be needed later, when we want VirtualBox to boot from the drive. If you don't intend to boot from the drive in VirtualBox, omit that command.

sudo apt-get install virtualbox
sudo usermod -aG vboxusers $USER
sudo usermod -aG disk $USER  # see security note above


Install the VirtualBox Extension Pack for support for USB 2.0 and 3.0. USB 2.0 devices will work without it, USB 3.0 won't.

Open VirtualBox and follow the first 1:55 of this video. If you added the extension pack, make sure to tick "USB 3.0" in the USB section of the VM settings. This will work even if you're not using a USB 3.0 device.

If your machine boots and works like in the video, amazing! That didn't happen for me. I ran into a lot of problems.

The first problem for me was that Virtualbox coudn't find my USB devices, which we've mitigated already by adding your user to the vboxusers group.

I don't remember what order the next two problems came in, but I imagine you'll see both:

  • Kernel driver not installed. As seen here. I followed these instructions, which worked flawlessly for me, allowing me to eventually run sudo modprobe vboxdrv.
  • VT-x is disabled. I guess that's shorthand for virtualization. Stack Overflow says we need to enable virtualization, or turn down the memory allocation setting for our box. I did the former, which I found out how to do by Googling "enable virtualization on HP Spectre." Substitute your computer make and model as appropriate.

Okay, at this point, after like six restarts, I had VirtualBox booting the installer.

Install Ubuntu to your flash drive

This is actually really straightforward. Once VirtualBox was set up and running the installer, I just followed the instructions in the aforelinked guide exactly. VirtualBox doesn't have access to your host machine's drive, so the Ubuntu installer can't possibly screw it up.

Congrats, you have Ubuntu on a flash drive now.

Booting in VirtualBox

I tried and failed to get this to work using Plop Linux Live, as is suggested here. It's an amazingly annoying OS, and it invariably failed to boot from my USB drive with failure writing sector <some hex code> to hd0. Don't bother! You don't need to download anything!

I got it working by following this guide. It's the secret Ubuntu version of equivalent instructions for Windows that are plastered around the internet. If you followed the instructions above, your user is already in the vboxusers and disk groups, which are mentioned in the guide.

I hope all this worked for you!

Ubuntu Training Exercise


Here's a fun Ubuntu exercise:

  1. In a running terminal, restart GNOME using gnome-shell --replace.
  2. Realize that it's going to be running your GUI shell out of the foreground of a terminal, decide not to worry about it.
  3. Forget about that terminal for a while, then come back to it.
  4. Wonder what's producing all this weird output about GTK, whatever, it's probably not important
  5. Ctrl-C

So from here, your goal is to get the gnome-shell going again without losing the windows you had open.

Okay, ya got me, this is actually just a screw-up I made that was somewhat interesting to fix.

The key steps, in Ubuntu Desktop 18.04, are...

  • Use ctrl + alt + F<#> to switch TTY. This produced some bizarre visual glitches, but after switching back and forth a few times, they resolved into a nice command-line login prompt.
  • Enter your username and password
  • Execute gnome-shell --replace
  • GNOME is probably running again, somewhere. Go find it (with ctrl + alt + F<#>). Log in.
  • Now that you've recovered the windows you were working on, save and close everything and restart your computer. Your nice new GNOME shell might get screwed up by something like the system suspending (mine did).

New Job


I got a new job as an OpenMRS implementer / general tech guy for CompaƱeros en Salud, aka Partners in Health Mexico. You're gonna start seeing posts about OpenMRS and Java development. Be warned.

I've also finally ditched Satan and come back to Linux/Ubuntu. It's not Arch or anything, but there's certainly some hilarity to be shared here.

Managing Global Styles with styled-jsx


There's a bug, it might exist in styled-jsx but it certainly exists in styled-jsx as used by nextjs, where you can't have multiple <style jsx global> tags -- only the first one gets used. A nice, not very hacky solution, (assuming you're using inline-loader or raw-loader for CSS):

// global-styles.js
import foo from 'foo/style.css'
import bar from 'bar/style.css'
export default foo + bar
import GlobalStyles from './global-styles'
// in component
<style global jsx>{ GlobalStyles }</style>