This site looks a lot better with CSS turned on. Note: IE<10 is not supported.

an article is displayed.

Creating a bootable USB from an ISO image in OSX (High Sierra)

A compact disk and a USB stick

Introduction

Once upon a time I was a .NET developer, and a lot of my development work took place in a Windows environment. I owned a machine that ran Windows and Visual Studio at a blistering rate, and everything in the kingdom was happy until one day I became more interested in open source development, bought a Macbook Pro and banished my trusty old workhorse to the shadows of the spare bedroom.

It's a sad thing to see a faithful and once glorious machine just rusting away like that, and as it was still very useable for pretty much everything other than modern gaming, I decided to breath life back into it with a copy of Linux.

My NVIDIA card seemed to hate everything but Linux Mint (even Ubuntu, which seems a bit weird since it's the foundation of Mint), and it took me quite a lot of experimentation and installation time before I finally settled on a Linux flavour that suited my needs.

The steps that I describe in this article aren't new at all, but there were a few gotches that I didn't see mentioned in the other articles that I read, so I've written about them here.

Goals

By the end of this article, you should be able to:

  • Create a bootable USB stick using an ISO image
  • Boot a machine from the USB stick *

* ....hopefully! This procedure will vary depending upon your target machine, but you should be able to use my article to point yourself in the right direction.

Prerequisites

  • OSX (High Sierra)
  • an ISO image that can be used to boot a compatible machine (so most likely an operating system of some sort)
  • a USB stick that is formatted so that is big enough to hold your ISO image

The same commands will probably run on versions of OSX other than High Sierra, but I can't say for definite

Make sure that OSX can read your USB stick

If you can browse the contents of your USB stick in Finder then you're all good to go, and you can skip ahead to the next section. Otherwise, you'll need to format your stick:

This will erase the contents of your USB stick, so make sure you don't have anything valuable on there.

Open up Disk Utility, click the View option on the main menu bar, and ensure that Show All Devices is selected.

In the left-hand menu, right-click on your USB device (which should appear under the External heading) and choose the Erase option.

Give your stick a title if you want, and then choose the Mac OS Extended and Master Boot Record options.

Convert your ISO to UDRW

If OSX can read your USB stick then you're all set to proceed.

In order to write the ISO file to a USB stick in such a way that it will become bootable, we must first convert it to a format that is mountable in OSX.

UDRW is a UDIF (Universal Disk Image Format) read/write image that can be mounted on OSX, and we can use the built-in hdiutil to perform this conversion.

First of all, open a terminal in the directory that contains your ISO image.

Run the following command, replacing nameOfYourInputFileISO with the path to your ISO image, and nameYourOutputFile with the location and filename that you wish to use for output.

hdiutil convert -format UDRW -o nameYourOutputFile nameOfYourInputFileISO

You don't need to specify a file extension for your output file.

I was running hdiutil against a Linux Mint ISO, so my commands looking something like: hdiutil convert -format UDRW -o mint-64bit linuxmint-18.3-cinnamon-64bit.iso

Take a look in your output directory, and you should see that hdiutil has created the output file that you specified, and that a .dmg extension has been appended

My output file was named mint-64bit.dmg

Prepare your USB stick

The following process will overwrite all of the existing data on your USB stick, so be sure to back-up any hilarious lolcats that might be on there, or they'll be lost forever.

Hilarious lolcat

Now you need to find out where your USB stick is mounted, so run the following command to list all the mounted disks:

diskutil list

I got the followong ouput on my machine (my 8gb USB stick is mounted at /dev/disk2):

/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *251.0 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:                 Apple_APFS Container disk1         250.8 GB   disk0s2

/dev/disk1 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +250.8 GB   disk1
                                 Physical Store disk0s2
   1:                APFS Volume Macintosh HD            158.0 GB   disk1s1
   2:                APFS Volume Preboot                 20.4 MB    disk1s2
   3:                APFS Volume Recovery                509.8 MB   disk1s3
   4:                APFS Volume VM                      3.2 GB     disk1s4

/dev/disk2 (external, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *8.0 GB     disk2
   1:                  Apple_HFS Untitled                8.0 GB     disk2s1

Find the name of your USB stick's mount point in the left hand column of the terminal output, and run the following command.

Replace /dev/disk2 with your mount point if necessary.

diskutil partitionDisk /dev/disk2 1 "Free Space" "unused" "100%"

I got the following output:

Started partitioning on disk2
Unmounting disk
Creating the partition map
Waiting for partitions to activate
Finished partitioning on disk2
/dev/disk2 (external, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *8.0 GB     disk2
   1:                        EFI EFI                     209.7 MB   disk2s1

Now it's time to run the dd command, which will create a bit-by-bit copy of your .dmg image and dump it onto your USB stick. Before you run the command below, have a quick scroll down to the A few gotchas section so that you'll know what to expect.

Replace pathToYourOutputFile.dmg with the path to the .dmg file that we created in the previous steps, and replace /dev/disk2 with your mount point again if necessary.

dd if=pathToYourOutputFile.dmg of=/dev/disk2 bs=1m

A few gotchas

This process might be slooooooow, depending on the size of your image and the speed of your USB stick, and the dd command doesn't give any output. Initially I stopped the process after a few minutes because it showed as being in a "stuck" state when I looked at what was going on at an OS level, and I assumed that something had gone wrong.

If you need to see what's happening while dd is running, simply press CTRL+T and you can see how many bytes have been processed so far.

When the dd process has finished, OSX will display a dialog complaining that the disk is unreadable.

This is to be expected, and is not an indication that anything has gone wrong. Choose the "ignore" option on the dialog.

Using your image to boot a machine

This is where things get very fuzzy, because it all depends on the type of motherboard you're using. I'll describe the steps I took to boot my old desktop from a USB stick containing Linux Mint, but you'll need to adapt this for your specific situation.

Insert the USB stick into the target machine and switch it on.

Access the BIOS on your machine's motherboard.

This is often done by pressing the delete key at the very beginning of the boot sequence, but it might be different on your machine.

Find the section of the BIOS that controls the boot order of the hard-drives, and make sure that your USB stick is set to be used first.

Save the changes to the BIOS, and reboot your machine.

Hopefully the machine will now boot from your USB stick, and you can use it in the way you were planning.

When you're done, remove the USB stick, reboot the machine and access the BIOS again.

Ensure that the order of the hard-drives is set back to how you want it.

This will depend on why you wanted to boot from the USB stick in the first place, but if you installed an operating system on the target machine then it's likely you'll need to set the drive that contains the new OS to be first in the boot order.

Summary

So that's pretty much all for this month's exciting episode. You should now have a USB stick that you can use to boot a compatible machine.

It's a bit of a bummer that you'll need to wing the BIOS setup to a certain extent, but hopefully I've given you something of a head start in that direction.

If you liked that article, then why not try:

Passing environment variables into a Dockerised NGINX configuration

I was recently working on a small project that required environment variables to be passed into an NGINX Docker container from the host system. I quickly discovered that NGINX configs don't handle this in quite the way that I expected, so I decided to document the solution that I eventually came up with.