Saturday, June 22, 2013

Volume Image Transfers in OS X

I've been frequently faced with the need to copy an image (usually a CD iso from some sort of installer) into a thumb drive, mostly due to the fact that I have a few machines that don't have a disk reader at all.  Yet this doesn't happen frequently enough that I actually remember all the steps, so every time I have to search and find this info again, which is not very efficient.  Hence this short note to myself here, where it might end up being useful to others as well.

In this case I want to copy a recently downloaded copy of a Linux installer .iso image to a USB thumb drive, so I can install it in a laptop.  We'll use the terminal in OS X, and the command line tools dd and diskutil. The symbol ">" indicates the command line prompt in my system.

From the command line:

> diskutil list

This will list all available volumes in the system, by device.  A typical output might look like this:

/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *1.0 TB     disk0
   1:                        EFI                         209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            899.0 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
   4:       Microsoft Basic Data BOOTCAMP                100.3 GB   disk0s4
/dev/disk1

   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *1.0 GB     disk2
   1:                 DOS_FAT_16 DISK_IMG                1.0 GB     disk2s1


In our scenario, disk1 is the device that will host the image.  In order to access the disk directly for a low level copy—i.e., we're not copying the iso file here, we're loading that image into the device itself, so we can actually boot from it—we'll need to unmount it first.

> diskutil unmountDisk /dev/disk1
Unmount of all volumes on disk1 was successful

The above succeeds whether the volume had been mounted or not.  Next, we copy the disk image to the thumb drive using dd.

> dd if=/Users/beto/Downloads/ubuntu-12.04-desktop-amd64.iso of=/dev/rdisk1 bs=1m

The parameter "if" indicates the input file, "of" the output file (in this case a device), and "bs" the block size at the destination.  Notice I used "rdisk1" instead of "disk1".  In OS X, the rdisk device is the "raw" device, which can be accessed without buffering or alignment checks.  That removes I/O overhead from the process, making it orders of magnitude faster than if you were to use the buffered device.  Obviously, if in this case points to the iso file I am copying in my scenario.  This could also be another device, say if you were making an image backup of a disk.

The results of the copy above are:

698+1 records in
698+1 records out
732213248 bytes transferred in 237.283433 secs (3085817 bytes/sec)


As soon as the process finishes OS X will attempt to mount the new volume.  If the image's file system is not supported OS X will balk at this, showing a warning dialog complaining that the disk is not readable, and showing the actions "Initialize...", "Ignore", and "Eject".  Simply select Eject, and you're done.  Another option is:

> diskutil eject /dev/disk1

No comments: