Created
January 20, 2019 17:57
-
-
Save ak9999/cc4949bdd54ed718ff257c3bbfd86be2 to your computer and use it in GitHub Desktop.
Simple script written for fun that creates a macOS Mojave ISO.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
This script creates a macOS Mojave bootable ISO. | |
Requires Python 3.6, macOS, and macOS Mojave 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/Mojave.cdr", "-size", "7316m", | |
"-layout", "SPUD", "-fs", "HFS+J"] | |
) | |
# Mount disk image | |
print("Mounting disk image...") | |
subprocess.run( | |
["hdiutil", "attach", "/tmp/Mojave.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\ Mojave.app/Contents/Resources/createinstallmedia", | |
"--volume", "/Volumes/install_build", "--applicationpath", | |
"/Applications/Install\ macOS\ Mojave.app", | |
"--nointeraction"] | |
) | |
# Detach the volume. | |
print("Detaching the disk image...") | |
subprocess.run(["hdiutil", "detach", "/Volumes/Install macOS Mojave"]) | |
# Convert the disk image to ISO. | |
print("Creating ISO and performing cleanup...") | |
subprocess.run(["hdiutil", "convert", "/tmp/Mojave.cdr.dmg", "-format", | |
"UDTO", "-o", "/tmp/Mojave.iso"]) | |
# Move the ISO from /tmp/ to ~/Desktop | |
shutil.move("/tmp/Mojave.iso.cdr", f"{home_dir}/Desktop/Mojave.iso") | |
# Delete the disk image from /tmp/ | |
remove("/tmp/Mojave.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/Mojave.cdr.dmg"): | |
remove("/tmp/Mojave.cdr.dmg") | |
if path.exists("/tmp/Mojave.iso"): | |
remove("/tmp/Mojave.iso") | |
if path.exists(f"{home_dir}/Mojave.iso"): | |
remove(f"{home_dir}/Mojave.iso") | |
finally: | |
sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment