Installing Ubuntu on a USB Drive

 •  Filed under tutorials, linux

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

https://www.ubuntu.com/download

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

Reboot.

If you're trying to do this with a USB 3.0 device, you'll want to 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

This is finicky. There are three different ways I've gotten it to work at different times. Option 3 is the easiest. I've also heard that the first two methods won't work with any USB 3.0 device.

  1. With a flash drive, 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.
  2. With an external SSD, I got it working with Plop Linux Live, as is suggested here.
  3. When neither of these two were working (maybe it was that I installed the VirtualBox extension for USB 3.0?) I got it to work using qemu, with the invocation
sudo qemu-system-x86_64 -enable-kvm -m 4G -usb -device usb-tablet -vga cirrus -drive format=raw,file=/dev/sda

where:

  • -enable-kvm enables CPU virtualization (must have been enabled, see above)
  • 4GB is the amount of memory I'd like to allocate the machine (128MB is the default)
  • -usb -device usb-tablet enable keyboard and mouse
  • -vga cirrus is the graphics driver that, for me, allowed changing display resolution above 800x600
  • -drive format=raw,file=/dev/sda adds my external drive, which is at /dev/sda, as a disk. format=raw is what gets autodetected with a warning if you don't specify that argument.
    Tweak the options as needed.

I hope all this worked for you!