#!/bin/bash # erase-install # by Graham Pugh. # # WARNING. This is a self-destruct script. Do not try it out on your own device! # # This script downloads and runs installinstallmacos.py from Greg Neagle, # which expects you to choose a value corresponding to the version of macOS you wish to download. # This script automatically fills in that value so that it can be run remotely. # # See README.md for details on use. # # Requirements: # macOS 10.13.4+ is already installed on the device (for eraseinstall option) # Device file system is APFS # # Version: version="0.17.3" # URL for downloading installinstallmacos.py installinstallmacos_url="https://raw.githubusercontent.com/grahampugh/macadmin-scripts/master/installinstallmacos.py" # Directory in which to place the macOS installer. Overridden with --path installer_directory="/Applications" # Temporary working directory workdir="/Library/Management/erase-install" # place any extra packages that should be installed as part of the erase-install into this folder. The script will find them and install. # https://derflounder.wordpress.com/2017/09/26/using-the-macos-high-sierra-os-installers-startosinstall-tool-to-install-additional-packages-as-post-upgrade-tasks/ extras_directory="$workdir/extras" # Display downloading and erasing messages if this is running on Jamf Pro jamfHelper="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper" if [[ -f "$jamfHelper" ]]; then # Jamf Helper localizations - download window jh_dl_title_en="Downloading macOS" jh_dl_desc_en="We need to download the macOS installer to your computer; this will take several minutes." jh_dl_title_de="Download macOS" jh_dl_desc_de="Der macOS Installer wird heruntergeladen, dies dauert mehrere Minuten." # Jamf Helper localizations - erase lockscreen jh_erase_title_en="Erasing macOS" jh_erase_desc_en="This computer is now being erased and is locked until rebuilt" jh_erase_title_de="macOS Wiederherstellen" jh_erase_desc_de="Der Computer wird jetzt zurückgesetzt und neu gestartet" # Jamf Helper localizations - reinstall lockscreen jh_reinstall_title_en="Upgrading macOS" jh_reinstall_heading_en="Please wait as we prepare your computer for upgrading macOS." jh_reinstall_desc_en="This process will take between 5-30 minutes. Once completed your computer will reboot and begin the upgrade." jh_reinstall_title_de="Upgrading macOS" jh_reinstall_heading_de="Bitte warten, das Upgrade macOS wird ausgeführt." jh_reinstall_desc_de="Dieser Prozess benötigt ungefähr 5-30 Minuten. Der Mac startet anschliessend neu und beginnt mit dem Update." # Jamf Helper localizations - confirmation window jh_confirmation_title_en="Erasing macOS" jh_confirmation_desc_en="Are you sure you want to ERASE ALL DATA FROM THIS DEVICE and reinstall macOS?" jh_confirmation_title_de="macOS Wiederherstellen" jh_confirmation_desc_de="Möchten Sie wirklich ALLE DATEN VON DIESEM GERÄT LÖSCHEN und macOS neu installieren?" jh_confirmation_button_en="Yes" jh_confirmation_button_de="Ja" jh_confirmation_cancel_button_en="Cancel" jh_confirmation_cancel_button_de="Abbrechen" # Jamf Helper localizations - free space check jh_check_desc_en="The macOS upgrade cannot be installed on a computer with less than 30GB disk space." jh_check_desc_de="Die Installation von macOS ist auf einem Computer mit weniger als 30GB freien Festplattenspeicher nicht möglich." # Jamf Helper icon for download window jh_dl_icon="/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/SidebarDownloadsFolder.icns" # Jamf Helper icon for confirmation dialog jh_confirmation_icon="/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertStopIcon.icns" # Grab currently logged in user to set the language for Jamf Helper messages current_user=$(/usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | /usr/bin/awk -F': ' '/[[:space:]]+Name[[:space:]]:/ { if ( $2 != "loginwindow" ) { print $2 }}') language=$(/usr/libexec/PlistBuddy -c 'print AppleLanguages:0' "/Users/${current_user}/Library/Preferences/.GlobalPreferences.plist") if [[ $language = de* ]]; then user_language="de" else user_language="en" fi # set localisation variables jh_dl_title=jh_dl_title_${user_language} jh_dl_desc=jh_dl_desc_${user_language} jh_erase_title=jh_erase_title_${user_language} jh_erase_desc=jh_erase_desc_${user_language} jh_reinstall_title=jh_reinstall_title_${user_language} jh_reinstall_heading=jh_reinstall_heading_${user_language} jh_reinstall_desc=jh_reinstall_desc_${user_language} jh_confirmation_title=jh_confirmation_title_${user_language} jh_confirmation_desc=jh_confirmation_desc_${user_language} jh_confirmation_button=jh_confirmation_button_${user_language} jh_confirmation_cancel_button=jh_confirmation_cancel_button_${user_language} jh_check_desc=jh_check_desc_${user_language} fi # Functions show_help() { echo " [erase-install] by @GrahamRPugh Usage: [sudo] ./erase-install.sh [--list] [--samebuild] [--sameos] [--move] [--path=/path/to] [--build=XYZ] [--overwrite] [--os=X.Y] [--version=X.Y.Z] [--beta] [--fetch-full-installer] [--erase] [--reinstall] [no flags] Finds latest current production, non-forked version of macOS, downloads it. --seedprogram=... Select a non-standard seed program --catalogurl=... Select a non-standard catalog URL (overrides seedprogram) --samebuild Finds the build of macOS that matches the existing system version, downloads it. --sameos Finds the version of macOS that matches the existing system version, downloads it. --os=X.Y Finds a specific inputted OS version of macOS if available and downloads it if so. Will choose the latest matching build. --version=X.Y.Z Finds a specific inputted minor version of macOS if available and downloads it if so. Will choose the latest matching build. --build=XYZ Finds a specific inputted build of macOS if available and downloads it if so. --move If not erasing, moves the downloaded macOS installer to $installer_directory --path=/path/to Overrides the destination of --move to a specified directory --erase After download, erases the current system and reinstalls macOS --confirm Displays a confirmation dialog prior to erasing the current system and reinstalling macOS. Only applicable with --erase argument. --reinstall After download, reinstalls macOS without erasing the current system --overwrite Download macOS installer even if an installer already exists in $installer_directory --list List available updates only (don't download anything) --extras=/path/to Overrides the path to search for extra packages --beta Include beta versions in the search. Works with the no-flag (i.e. automatic), --os and --version arguments. --fetch-full-installer For compatible computers (10.15+) obtain the installer using 'softwareupdate --fetch-full-installer' method instead of using installinstallmacos.py Note: If existing installer is found, this script will not check to see if it matches the installed system version. It will only check whether it is a valid installer. If you need to ensure that the currently installed version of macOS is used to wipe the device, use the --overwrite parameter. " exit } kill_process() { process="$1" if /usr/bin/pgrep -a "$process" >/dev/null ; then /usr/bin/pkill -a "$process" && echo " [erase-install] '$process' ended" || \ echo " [erase-install] '$process' could not be killed" fi } ask_for_shortname() { # required for Silicon Macs /usr/bin/osascript <