de minutiae

de minutiae

cabbages, kings, and all things in between

09 Jun 2020

Booting into a Linux Flash Drive on a Z83 Mini PC

Z83-II Mini PC

A while back I bought a Z83 Mini PC which I used as an OPNsense router. This actually worked out great for a while (I highly recommend this solution over more expensive routers), but some time afterwards my ISP started requiring responses to ICMP echoes which my firewall blocked by default. At the time there was a bug in OPNsense which I couldn’t work around with the configuration I was using, so I was forced to give up on that. So my Mini PC started gathering dust… until I decided to repurpose it as a Pi Hole. Pi Hole was originally designed for use on a Raspberry Pi, but that seemed unnecessary since I already had a low-power device capable of running it.

The Problem

One of the troublesome things about the Z83 machines is that while it supports 64bit Operating Systems, it uses 32bit UEFI. This is problematic because 64bit OSes usually don’t support 32bit UEFI out-of-the-box. And there aren’t many 32bit Linux distros that even support UEFI. So if you try any modern random Live Installer, it’s likely that it simply won’t boot. Or rather, it will just boot directly into the Windows 10 OS that ships with the device. To get around this, I could have installed Docker on the Windows OS and run Pi Hole in a container, but I prefer to the leave the Windows Partition untouched.

You can read more information about why many devices use 32 bit UEFI when they support 64bit systems over here

A Solution (but not really)

Thankfully, 64bit Live Linux distros that support 32bit UEFI do exist. Fedora is one. Another option is to download one of the many releases created by users and developers of these devices. There are builds for Mint, Xubuntu, Lubuntu and others posted on various forums and blogs around the web. For this exercise, I used the Ubuntu 18.04 build posted on the Minix forums. Seems like this should solve our issue, right? Wrong. While these builds do manage to boot the device, you’re stuck with installing to the internal storage, which is what I wanted to avoid. You can still install to a USB, but then that USB won’t boot on the device. Super frustrating. After spending a day browsing various Wiki pages and StackOverflow questions related to EFI and trying different futile approaches, I stumbled on the answer.

Enter rEFInd

refind

rEFInd is a Boot Manager with tons of useful features. I won’t go into all of them here but you can browse the creator’s site linked above - it’s a veritable tome. The beauty of using rEFInd is that using the default configuration, most things will just work. Once you’ve run the install command against a properly prepared drive, you will be able to boot into the flash drive from the graphical boot menu. No further configuration is needed, but if you want to, you can also customise virtually everything about the boot manager. If you’re interested in the entire process, keep reading. But if you only want to know about installing rEFInd, feel free to skip to the end.

Creating the bootable Linux USB

Requirements

You’ll need:

  • A USB stick for the Live Installer. 8GB is fine
  • A USB stick to install to. I used a 16GB stick
  • A suitable Linux ISO that can boot on the device
  • A Z83 Mini PC connected to the internet
  • A keyboard, monitor, and mouse for convenience (you might be surprised at how easy it is to forget these!)

Preparing the Live USB Installer

Once you’ve downloaded a suitable ISO, write it to a USB stick. If want a simple way to do this then try Balena Etcher. Just select the ISO, the destination, and click Flash.

Etcher

Once the USB stick is ready, insert into Mini PC and power it on. Ensure that you’ve changed the BIOS settings to give USB drives boot priority over the internal drive and LAN. Select the option from the boot menu that lets you “try” the distro. This will boot you into the “Live” environment. Subsequent commands will assume you’re using some flavour of Ubuntu, but you can tailor them to suit your needs.

Preparing the Flash Drive

At this point you’re going to want to start being a bit more cautious. Selecting the wrong device or partition here can mess up your existing installation. In order to mitigate that, confirm the device name of the destintion USB stick before continuing.

Before inserting your target USB stick open a terminal (press Ctrl+Alt+T), and run lsblk -o +TRAN | grep usb

Your terminal should look similar to this after executing:

user@ubuntu:~$ lsblk -o +TRAN | grep usb
sda    8:16    1  14.9G  0 disk            usb

What you want to note here is all the entries that start with “sd”. These entries refer to the inserted USB drives. The naming scheming uses the device prefix (“sd” in this case), with a letter suffixes in lexicographic order. i.e. the first device will be “sda”. The next “sdb” etc.

On other machines the “sd” devices can also refer to internal hard-drives, but the Z83 Mini PCs use a memory card, indicated by “mmcblk”. You can read here for information on the naming conventions.

Once you have this info, insert the destination flash drive and run previous command again (press the UP arrow in your terminal, and then enter). You should see an extra “sd” device listed.

user@ubuntu:~$ lsblk -o +TRAN | grep usb
sda    8:16    1  14.9G  0 disk            usb
sdb    8:32    1  14.9G  0 disk            usb

Checking for a new “sd” device (“sdb” in this case - it may be different on your machine). This new device refers to the target flash drive. Remember this name. Write it down if necessary. For the rest of this post, I’m going to use the name “sdX” to prevent copy-paste errors from trashing your installation. Wherever you see sdX, replace it with your device name.

WARNING: The following commands are very dangerous if used incorrectly. Double-check that you are entering them correctly and are using the correct device name!

If you want to, you can start a root session with sudo su if you don’t want to prefix every command with sudo.

Run sudo sgdisk --zap-all /dev/sdX to clear the flash drive:

user@ubuntu:~$ sudo sgdisk --zap-all /dev/sdX
Creating new GPT entries in memory
GPT data structures destroyed! You may now partition the disk using fdisk or
other utilities

Clear any existing file system signatures by running sudo wipefs --all --force /dev/sdX

Run sudo sgdisk -n 0:0:250M -c 1:EFI -t 1:ef00 /dev/sdX to create the EFI partition. This is where rEFInd will live.

user@ubuntu:~$ sudo sgdisk -n 0:0:250M -c 1:EFI -t 1:ef00 /dev/sdX
Setting name!
partNum is 0
The operation completed successfully.

Run sudo mkfs.vfat -F32 /dev/sdX1 to format the partition with a FAT32 file system.

user@ubuntu:~$ sudo mkfs.vfat -F32 /dev/sdX1
mkfs.fat 4.1 (2017-01-24)

Run sudo sgdisk -n 0:0:0 -c 2:EXT4 -t 2:8300 /dev/sdX to create the ext4 partition.

user@ubuntu:~$ sudo sgdisk -n 0:0:0 -c 2:EXT4 -t 2:8300 /dev/sdX
Setting name!
partNum is 1
The operation completed successfully.

Run sudo mkfs.ext4 /dev/sdX2 to format the partition with the ext4 file system.

user@ubuntu:~$ sudo mkfs.ext4 /dev/sdX2
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 3843323 4k blocks and 960992 inodes
Filesystem UUID: 7911b31d-3fe5-4a71-951e-789588756bd3
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208

Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

We are actually duplicating work by creating the ext4 partition in this step because the installation procedure will recreate this partition. In fact, you could also create the EFI partition with the installer. However, not every distro installs in the same way as Ubuntu so it may be better to create it up front, especially since it will be easier and more convenient to catch things like flash drive errors before we start the Linux installation.

Confirm that everything is configured correctly by running sudo fdisk -lu /dev/sdX. If you are using a 16GB Flash Drive then your output should look similar to this:

user@ubuntu:~$ sudo fdisk -lu /dev/sdX
Disk /dev/sdX: 14.93 GiB, 16005464064 bytes, 31260672 sectors
Disk model: Flash Drive Model Name
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: F11984B6-8DDF-4D3C-979C-20893A5FFE70

Device      Start      End  Sectors  Size Type
/dev/sdX1    2048   512000   509953  249M EFI System
/dev/sdX2  514048 31260638 30746591 14.7G Linux filesystem

If you do not see two partitions formatted as “EFI System” and “Linux filesystem” then you’ve either made a mistake somewhere or a command did not complete successfully. Confirm all your entries and responses in the terminal. Check again that you are using the correct device name. Then start again from the first sgdisk command.

Click on the “Install Ubuntu XX.XX LTS” icon when you’re ready to proceed.

Installing Ubuntu

Installing Ubuntu from a Live system is a pretty standard affair, so I’ll only be covering the Flash Drive configuration.

Checkout the online documentation if you need more information.

When you reach the following screen, select Something else and click Continue: Ubuntu Installation Type

On this screen, ensure you’ve selected the correct USB device and partition:

Choose correctly or be sad for the rest of the day

Choose correctly or be sad for the rest of the day

Double-click on the ext4 partition to open the Edit partition modal. Select “Ext4 journaling file system”, tick “Format the partition”, and select “/” as the Mount point. There is no need to adjust the size.

Edit partition

Once you Click OK, the device list on the Installation Type screen will update showing that you’ve elected to format the ext4 partition, and it will show the mount point. Double-check that the details are correct, and that you’ve selected the correct USB device, then click “Install Now”. Read the Warning message, then Continue if you’re satisfied, or Go Back to make changes.

Enter the appropriate details on the next few screens and wait for the installation to complete. Do not restart the system when asked - select “Continue Testing”.

Installing rEFInd

Package installation

Enter the following commands at the terminal to install rEFInd:

  • sudo apt install software-properties-common

  • sudo add-apt-repository ppa:rodsmith/refind

  • sudo apt update

  • sudo apt install refind -y

Press ENTER if you receive messages about confirming the installation of packages. Near the end of the installation, rEFInd will ask you if you want to install it to your EFI System Partition. Select “No”. You don’t need to install it to your device.

rEFInd ESP installation confirmation

Once the package installation has completed you can proceed to install rEFInd to your flash drive.

Flash drive installation

Run sudo refind-install --usedefault /dev/sdX1 --alldrivers to install rEFInd to the EFI partition on your flash drive.

user@ubuntu:~$ sudo refind-install --usedefault /dev/sdX1 --alldrivers
ShimSource is none
Installing rEFInd on Linux....
Note: IA32 (x86) binary not installed!
Copied rEFInd binary files

Copying sample configuration file as refind.conf; edit this file to configure
rEFInd.

That’s it! Restart the device and remove the USB stick containing the Live Installer. On startup you should be greeted with the graphical rEFInd boot manager. Your Ubuntu Flash Drive should appear as an Ubuntu Logo with flash drive overlay. Select it and press enter to boot into it.

By the way, once you’ve booted into your OS, you can do a full distribution upgrade without issue. Your rEFInd configuration will persist, and you’ll still be able to boot without issue.

Optional configuration

NOTE: It’s likely that the flash drive was assigned a new device name at this point. Confirm the new name before continuing

If you do want to configure some aspects of rEFInd, you can mount the EFI partition into a directory and edit the file directly. Personally I don’t change much about the configuration except dropping the timeout from 20 to 8 seconds. In the following example I use vi but you can use nano or whatever text editor you prefer.

Run mkdir efipart to create a mount point for the partition in your home directory (enter cd ~ if you’re not sure about your current directory).

Mount the partition with sudo mount /dev/sdX1 efipart.

Edit the file using sudo vi efipart/EFI/BOOT/refind.conf.

Once you’ve made all your changes, unmount the partition and remove the directory with sudo umount /dev/sdX1 && rmdir efipart

user@ubuntu:~$ mkdir efipart
user@ubuntu:~$ sudo mount /dev/sdX1 efipart
user@ubuntu:~$ sudo vi efipart/EFI/BOOT/refind.conf
user@ubuntu:~$ sudo umount /dev/sdX1 && rmdir efipart

You can read more about configuring rEFInd over here.