Skip to content

Instantly share code, notes, and snippets.

@supercoffee
Last active October 7, 2017 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save supercoffee/a1fe4e22cc005479aa5d to your computer and use it in GitHub Desktop.
Save supercoffee/a1fe4e22cc005479aa5d to your computer and use it in GitHub Desktop.
Create Linux USB installers from ISOs to boot on Mac hardware
#! /bin/bash
#usage: sudo ./convertiso.sh /path/to/iso/file /dev/path/to/usb
#converts a regular ISO file to Apple UDRW file for writing to USB
#then flashes then new image to the specified USB device
#check is script has been run as root /sudo
if [[ $UID != 0 ]]; then
echo "Please run the script as root."
exit 1
fi
ISOPATH=$1
if [[ ! -f $ISOPATH ]]; then
echo "ISO not found. Aborting."
exit 1
fi
DEVPATH=$2
if [[ ! -b $DEVPATH ]]; then
echo "Device not found. Aborting"
exit 1
fi
#check if device is USB. Exit if device is not USB.
DEVINFO=$(diskutil info $DEVPATH | grep Protocol)
DEVINFO=$(echo $DEVINFO | cut -d' ' -f2)
if [[ $DEVINFO != "USB" ]]; then
echo "Non-USB device specified. This is dangerous."
echo "Aborting."
exit 1
fi
#convert the ISO to Mac format
hdiutil convert -format UDRW -o ${ISOPATH}.dmg $ISOPATH
#change the ownership of the new file to match the owner of the
#current directory
OWNER=$(ls -dl . | cut -d' ' -f4)
GROUP=$(ls -dl . | cut -d' ' -f6)
chown ${OWNER}:${GROUP} ${ISOPATH}.dmg
#unmount the device target device
if [[ $(mount | grep $DEVPATH) ]]; then
diskutil unmountdisk $DEVPATH
#echo "Unmounted $DEVPATH"
sleep 2
if [[ $(mount | grep $DEVPATH) ]]; then
echo "Unable to unmount target device."
echo "Aborting."
exit 1
fi
fi
#add an 'r' to the beginning of the device name
#rdisk or raw disks write much faster
echo "Writing image to device. This may take a while."
DEVNAME=$(echo $DEVPATH | cut -d'/' -f3)
RDISKPATH="/dev/r$DEVNAME"
#start the image writing process in the background
dd if=${ISOPATH}.dmg of=$RDISKPATH bs=1m &
PID=$(ps | grep dd | grep -v grep)
PID=$(echo $PID | cut -d' ' -f1)
while [[ $(ps | grep $PID | grep -v grep) ]]
do
kill -SIGINFO $PID
sleep 10
done
echo "Finished. You may now eject the disk."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment