Last active
March 9, 2025 06:55
-
-
Save joevt/a4ef4cd2e1485d0dd595893007bdfd6a to your computer and use it in GitHub Desktop.
Commands for kexts
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
| # joevt Dec 30, 2020 | |
| checkkext () { | |
| local thekextpath="$1" | |
| if [[ -d "$thekextpath" ]]; then | |
| if [[ -f "$thekextpath/Contents/Info.plist" ]]; then | |
| local kextname="$(basename "$thekextpath")" | |
| if [[ "$kextname" =~ ".*\.kext" ]]; then | |
| local kextidentifier="" | |
| kextidentifier="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' "$thekextpath/Contents/Info.plist" 2> /dev/null)" | |
| local theerr=$? | |
| if (( theerr == 0 )); then | |
| return 0 | |
| else | |
| echo "# Missing CFBundleIdentifier" 1>&2 | |
| return 1 | |
| fi | |
| else | |
| echo "# That's not a kext" 1>&2 | |
| return 1 | |
| fi | |
| else | |
| echo "# Missing Info.plist" 1>&2 | |
| return 1 | |
| fi | |
| else | |
| echo "# Expected a kext folder" 1>&2 | |
| return 1 | |
| fi | |
| } | |
| unloadkext () { | |
| local thekextpath="$1" | |
| checkkext "$thekextpath" || return 1 | |
| local kextname="$(basename "$thekextpath")" | |
| local kextsrcdir="$(dirname "$thekextpath")" | |
| local kexttmpdir="$(mktemp -d)" | |
| local kextinstalldir="/Library/Extensions" | |
| local kextidentifier="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' "$thekextpath/Contents/Info.plist")" | |
| echo "# kextname: $kextname" | |
| echo "# kextidentifier: $kextidentifier" | |
| if ( kextstat | grep -q "$kextidentifier" ); then | |
| while ( kextstat | grep -q "$kextidentifier" ); do | |
| echo "# Unloading $kextname" | |
| sleep 1 | |
| sudo kextunload -b "$kextidentifier" | |
| done | |
| echo "# $kextname is unloaded" | |
| else | |
| echo "# $kextname is not loaded" | |
| fi | |
| } | |
| installkext () { | |
| local doload=0 | |
| if [[ "$1" == "-l" ]]; then | |
| doload=1 | |
| shift 1 | |
| fi | |
| local thekextpath="$1" | |
| checkkext "$thekextpath" || return 1 | |
| local kextname="$(basename "$thekextpath")" | |
| local kextsrcdir="$(dirname "$thekextpath")" | |
| local kexttmpdir="$(mktemp -d)" | |
| local kextinstalldir="/Library/Extensions" | |
| local kextidentifier="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' "$thekextpath/Contents/Info.plist")" | |
| if ((doload)); then | |
| unloadkext "$thekextpath" | |
| else | |
| echo "# kextname: $kextname" | |
| echo "# kextidentifier: $kextidentifier" | |
| fi | |
| mount | grep ' on / ' | grep -q 'read-only' && sudo mount -uw / | |
| sudo cp -R "$thekextpath" "$kexttmpdir/$kextname" | |
| sudo chown -R root:wheel "$kexttmpdir/$kextname" | |
| sudo find "$kexttmpdir/$kextname" -type d -exec /bin/chmod 0755 {} \; | |
| sudo find "$kexttmpdir/$kextname" -type f -exec /bin/chmod 0644 {} \; | |
| if [[ -n "$kextinstalldir" ]]; then | |
| [[ -d "/System/Library/Extensions/$kextname" ]] && sudo rm -R "/System/Library/Extensions/$kextname" | |
| [[ -d "/Library/Extensions/$kextname" ]] && sudo rm -R "/Library/Extensions/$kextname" | |
| sudo mv "$kexttmpdir/$kextname" "$kextinstalldir" | |
| if ((doload)); then | |
| sudo kextutil "$kextinstalldir/$kextname" | |
| fi | |
| else | |
| if ((doload)); then | |
| sudo kextutil "$kexttmpdir/$kextname" | |
| fi | |
| fi | |
| if ((doload)); then | |
| kextstat | grep "$kextidentifier" | |
| fi | |
| } | |
| removekext () { | |
| local thekextpath="$1" | |
| [[ -d "$thekextpath" ]] && sudo rm -R "$thekextpath" | |
| } | |
| rebuildkextcache () { | |
| sudo kextcache -i / | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
Load the commands into the terminal like this:
source KextUtil.shUtility Functions
These utility functions are used by the main functions listed below.
getkextidentifierkextpathkextstator to unload the kext.checkkextkextpathstderr.checkkextbefore attempting to do anything.checkkextpermissionskextpathstderr.fixkextpermissionsto see if the permissions need to be fixed.fixkextpermissionskextpathloadkext,unloadkext, andinstallkext.Main Functions
loadkextkextpathunloadkextkextpathinstallkext[-s] [-l] kextpath/Library/Extensions. if-sis specified then the install location is/System/Library/Extensions. Do not use-sfor Big Sur and later.-lis specified. It will first unload previously loaded kext if necessary./System/Library/Extensionsif it exists there. It will mount the root volume as read/write if necessary (works in the case of Catalina; for later macOS it is expected that the kext will not be in/System)./Library/Extensionsif it exists there.removekextkextpath/System).Updates
Jun 12, 2024
kextutil.Feb 10, 2024
/Library/Extensionsif it doesn't exist (as in Mac OS X 10.4 Tiger).May 9, 2023
loadkextandinstallkext.checkkext,installkext, andremovekext.May 8, 2023
unloadkext.checkkextpermissionsso thatchownandchmodare not used unnecessarily.fixkextpermissionsnow returns a status if the permissions needed to be fixed but could not be fixed.loadkextwill now load a kext from its current location if possible. It may modify permissions if necessary. It will revert to using a temporary directory if necessary.mountsystemrwfor mounting the/root volume as read write. This is useful for Catalina.removekextnow mounts the root volume as read write if necessary.Jan 31, 2023
-soption forinstallkextto install to/System/Library/Extensions. This is required to load unsigned kexts in OS X 10.9 Mavericks.Jan 6, 2023
Dec 8, 2022