Skip to content

Instantly share code, notes, and snippets.

@ak9999
Last active January 4, 2018 04:40
Show Gist options
  • Save ak9999/b728aefb2341cc4000b043df8c21c070 to your computer and use it in GitHub Desktop.
Save ak9999/b728aefb2341cc4000b043df8c21c070 to your computer and use it in GitHub Desktop.
Simple script written for fun that creates a macOS High Sierra ISO.
#!/usr/bin/env python3
"""
This script creates a macOS High Sierra bootable ISO.
Requires Python 3.6, macOS, and macOS High Sierra app installed.
Created using info gathered from here: https://gist.github.com/agentsim/00cc38c693e7d0e1b36a2080870d955b
Purpose: I just wanted to try out the subprocess library.
"""
import subprocess
import sys
from getpass import getuser
from os import remove, path
import platform
import shutil
__author__ = "Abdullah Khan"
__email__ = "[email protected]"
__license__ = "GNU GPL v3"
home_dir = f"/home/{getuser()}"
def check_platform():
'''Make sure we are running on macOS before continuing.'''
if platform.system() != "Darwin":
sys.exit("Exiting since this is not a macOS system.")
def run_shell_commands():
"""Runs shell commands to create bootable macOS ISO."""
# Create disk image
print("Creating disk image...")
subprocess.run(
["hdiutil", "create", "-o", "/tmp/HighSierra.cdr", "-size", "7316m",
"-layout", "SPUD", "-fs", "HFS+J"]
)
# Mount disk image
print("Mounting disk image...")
subprocess.run(
["hdiutil", "attach", "/tmp/HighSierra.cdr.dmg", "-noverify",
"-mountpoint", "/Volumes/install_build"]
)
# User will be prompted for password here to create install media.
print("Escalating privileges to create bootable media...")
subprocess.run(
["sudo", "/Applications/Install\ macOS\ High\ Sierra.app/Contents/Resources/createinstallmedia",
"--volume", "/Volumes/install_build", "--applicationpath",
"/Applications/Install\ macOS\ High\ Sierra.app",
"--nointeraction"]
)
# Detach the volume.
print("Detaching the disk image...")
subprocess.run(["hdiutil", "detach", "/Volumes/Install macOS High Sierra"])
# Convert the disk image to ISO.
print("Creating ISO and performing cleanup...")
subprocess.run(["hdiutil", "convert", "/tmp/HighSierra.cdr.dmg", "-format",
"UDTO", "-o", "/tmp/HighSierra.iso"])
# Move the ISO from /tmp/ to ~/Desktop
shutil.move("/tmp/HighSierra.iso.cdr", f"{home_dir}/Desktop/HighSierra.iso")
# Delete the disk image from /tmp/
remove("/tmp/HighSierra.cdr.dmg")
print(f"Finished, your ISO is located in: {home_dir}")
if __name__ == "__main__":
check_platform()
try:
run_shell_commands()
except Exception as e:
print(f"{e}: Failed to create ISO, cleaning up files.")
if path.exists("/Volumes/Install macOS High Sierra"):
subprocess.run(["hdiutil", "detach", "/Volumes/Install macOS High Sierra"])
if path.exists("/tmp/HighSierra.cdr.dmg"):
remove("/tmp/HighSierra.cdr.dmg")
if path.exists("/tmp/HighSierra.iso"):
remove("/tmp/HighSierra.iso")
if path.exists(f"{home_dir}/HighSierra.iso"):
remove(f"{home_dir}/HighSierra.iso")
finally:
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment