Skip to content

Instantly share code, notes, and snippets.

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 bluzky/c643ecc3f8194b53f76df27c4ce259a3 to your computer and use it in GitHub Desktop.
Save bluzky/c643ecc3f8194b53f76df27c4ce259a3 to your computer and use it in GitHub Desktop.
How to create Apple's Bootable MacOS Rescue Image from Linux

See bootableinstaller.com

How to create a Bootable MacOS Recovery USB from Linux

If your Mac is out-of-order or you otherwise cannot download macOS from the App Store, you can still create a bootable OS X recovery USB, and you can use that to create an Installer USB.

The downloads used in this process are legal and freely avaliable - including disk images directly from Apple's IT support pages, and open source utilities for extracting and converting pkg, dmg, and HFS+.

No hackery. No hackintosh-ery.

This process works for

  • macOS Catalina (10.15)
  • macOS Mojave (10.14)
  • macOS High Sierra (10.13)
  • macOS Sierra (10.12)
  • OS X El Capitan (10.11)
  • OS X Yosemite (10.11)

In all cases you should first download the El Capitan Installer, as a direct download from Apple (no App Store).

The OS X Yosemite and macOS Sierra images should also work, but you might have trouble getting macOS Sierra to boot in VirtualBox.

Recovery USB vs Installer USB

This is a 3-step process:

  1. Create a Recovery ISO with Linux
  2. Create an El Capitan Installer ISO with VirtualBox from the Recovery image
  3. Create other Installer USBs from El Capitan in VirtualBox

Create the Recovery ISO

The Apple download contains a recovery image called BaseSystem.dmg which needs to be copied to a correctly partitions and formatted recovery USB or ISO.

  1. Download the OS X El Capitan installer
    • Note for Windows users running Linux from VirtualBox:
      • you already downloaded InstallOSX.dmg to your USB drive, so skip this step
    • Visit Apple's official "How to upgrade to El Capitan" documentation
    • Click "Download OS X El Capitan" in Step 4
    • InstallOSX.dmg will be about 6GB in your Downloads folder
  2. Install HFS+ tools for Linux
    • See install-mac-tools.sh below
    • Note for Windows users running Linux from VirtualBox:
      • choose to download to your USB drive, NOT Downloads
      • run the script from your USB drive, NOT Downloads
    • Right-Click on the view Raw link, choose Save as, and select the Downloads folder
    • Open a Terminal and run bash install-mac-tools.sh from the Downloads folder
    pushd ~/Downloads
    bash install-mac-tools.sh
  3. Create el-capitan-recue.iso
    • See linux-create-bootable-macos-recovery-image.sh below
    • Note for Windows users running Linux from VirtualBox:
      • choose to download to your USB drive, NOT Downloads
      • run the script from your USB drive, NOT Downloads
    • Right-Click on the view Raw link, choose Save as, and select the Downloads folder
    • Open a Terminal and run bash linux-create-bootable-macos-recovery-image.sh from the Downloads folder
    pushd ~/Downloads
    bash linux-create-bootable-macos-recovery-image.sh

You can of course run each command of the scripts by hand, but since it's deeply nested (.dmg containing a .pkg containing another .dmg with another .dmg inside), and requires loopback mounts, it's a rather tedious and mundane process.

Boot the Recovery ISO

You will need a 32GB+ USB drive, ExFAT formatted.

You should copy ElCapitanInstallESD.dmg from Downloads to your USB drive.

########################################################################################################################
# bootableinstaller.com #
########################################################################################################################
set -e
set -u
set -x
# Install HFS+ and dmg dependencies
if ! [ $(command -v mkfs.hfsplus) ] || ! [ $(command -v dmg2img) ]; then
# Note: hfsplus and hfsutils are old and not necessary
# Note: mac-fdisk (mac-fdisk-cross) is useful for debugging, but not required
sudo apt install -y hfsprogs dmg2img
fi
# Install xar
if ! [ -f "xar/xar/src/xar" ]; then
echo "Installing XAR from https://github.com/mackyle/xar.."
sudo apt install -y build-essential autoconf
sudo apt install -y libxml2-dev libssl-dev git
rm -rf xar/
git clone https://github.com/mackyle/xar
pushd xar/xar
sed -i.bak 's/OpenSSL_add_all_ciphers/OPENSSL_init_crypto/g' configure.ac
./autogen.sh --prefix=/usr/local
make
sudo make install
popd
fi
########################################################################################################################
# bootableinstaller.com #
########################################################################################################################
# Put bash in "strict mode"
set -u
set -e
if [ -f "./el-capitan-rescue.iso" ]; then
echo "'el-capitan-rescue.iso' already exists"
exit 0
fi
# Show commands as they are executed
set -x
my_installesd="./ElCapitanInstallESD.img"
if ! [ -f "./InstallESD.img" ]; then
my_installesd="./InstallESD.img"
fi
if ! [ -f "$my_installesd" ]; then
if ! [ -f "./InstallMacOSX/InstallMacOSX.pkg/InstallESD.dmg" ]; then
if ! [ -f "./InstallMacOSX.img" ]; then
if ! [ -f "./InstallMacOSX.dmg" ]; then
echo "Error: 'InstallMacOSX.dmg' doest not exist."
echo " Go to https://support.apple.com/en-us/HT206886"
echo " In step 4 click 'Download OS X El Capitan'"
fi
# decompresses into a dd-like image
dmg2img InstallMacOSX.dmg -o ./InstallMacOSX.img
chmod a-w ./InstallMacOSX.img
# rm InstallMacOSX.dmg
fi
my_fullosx=$(sudo losetup --list | (grep InstallMacOSX.img || true))
if ! [ -f "/mnt/InstallMacOSX/InstallMacOSX.pkg" ]; then
my_fullosx=$(sudo losetup --partscan --show --find InstallMacOSX.img)
echo "$my_fullosx"
sudo fdisk -l "$my_fullosx"
ls -l "$my_fullosx"p*
sudo partprobe $my_fullosx
sudo mkdir -p /mnt/InstallMacOSX
sudo mount "$my_fullosx"p2 -o ro,noatime /mnt/InstallMacOSX
fi
echo "Extracting /mnt/InstallMacOSX/InstallMacOSX.pkg"
mkdir -p ./InstallMacOSX.tmp.d/
pushd ./InstallMacOSX.tmp.d/
#LD_LIBRARY_PATH=../xar/xar/lib ../xar/xar/src/xar -xvf /mnt/InstallMacOSX/InstallMacOSX.pkg
# TODO maybe use pzip / 7zip instead?
xar -xvf /mnt/InstallMacOSX/InstallMacOSX.pkg
popd
mv ./InstallMacOSX.tmp.d ./InstallMacOSX
sudo umount /mnt/InstallMacOSX
sudo losetup -d "$my_fullosx"
fi
if ! [ -f "./ElCapitanInstallESD.img" ]; then
dmg2img ./InstallMacOSX/InstallMacOSX.pkg/InstallESD.dmg -o ./ElCapitanInstallESD.img
chmod a-w ./ElCapitanInstallESD.img
fi
my_installesd=./ElCapitanInstallESD.img
# TODO it's now safe to remove the big fat InstallMacOSX.*mg and ./InstallMacOSX/
fi
my_esd=$(sudo losetup --list | (grep InstallESD.img || true) | cut -d' ' -f1)
if ! [ -f "/mnt/InstallESD/BaseSystem.dmg" ]; then
my_esd=$(sudo losetup --partscan --show --find "$my_installesd")
echo "$my_esd"
sudo fdisk -l "$my_esd"
ls -l "$my_esd"p*
sudo partprobe $my_esd
sudo mkdir -p /mnt/InstallESD
sudo mount "$my_esd"p2 -o ro,noatime /mnt/InstallESD
fi
if ! [ -f "./BaseSystem.img" ]; then
dmg2img /mnt/InstallESD/BaseSystem.dmg -o ./BaseSystem.img
chmod a-w ./BaseSystem.img
fi
my_base=$(sudo losetup --list | grep BaseSystem.img | cut -d' ' -f1)
if [ -z "$my_base" ]; then
my_base=$(sudo losetup --partscan --show --find ./BaseSystem.img)
echo "$my_base"
sudo fdisk -l "$my_base"
ls -l "$my_base"p*
sudo partprobe $my_base
fi
my_empty=$(ls empty*img.bz2 | sort | head -1)
cp -rp "$my_empty" el-capitan-rescue.dd.img.bz2
bunzip2 el-capitan-rescue.dd.img.bz2
my_dd=$(sudo losetup --partscan --show --find el-capitan-rescue.dd.img)
echo "$my_dd"
sudo mac-fdisk -l "$my_dd"
ls -l "$my_dd"p*
sudo dd if="$my_base"p1 of="$my_dd"p2 bs=128M status=progress
sudo losetup -d "$my_dd"
mv el-capitan-rescue.dd.img el-capitan-rescue.iso
sudo umount /mnt/InstallESD
sudo losetup -d "$my_esd"
chmod a-w ./el-capitan-rescue.iso
echo "el-capitan-rescue.iso"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment