Skip to content

Instantly share code, notes, and snippets.

@bfricka
Forked from slagdang/enable_trim.sh
Created July 6, 2014 01:27
Show Gist options
  • Save bfricka/3533d93e5a0e0b0fa4c5 to your computer and use it in GitHub Desktop.
Save bfricka/3533d93e5a0e0b0fa4c5 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Enable TRIM support for 3rd Party SSDs. Works for Mountain Lion, should work on earlier OSes too.
# Tested on 10.8.2, 10.8.3, 10.8.5, 10.9.0, 10.9.1, 10.9.2, 10.9.3, and 10.9.4
#
# You may have to re-apply the fix after some system updates, including but not limited to those below:
# 10.9.X to 10.9.4
# 10.9.X to 10.9.3
# 10.9.X to 10.9.2
# 10.8.X to 10.8.3
#
# Checked for proper operation on 10.8.0, but never booted 10.8.0 with the modified kext.
#
# Original source: http://digitaldj.net/2011/07/21/trim-enabler-for-lion/
#
# To use this, put the contents of this into a file called enable_trim.sh in your home directory
# Then, open a Terminal window. In this window, type 'bash enable_trim.sh' (omitting the quotes)
# and press return. It will ask for your administrator password. Type this and it will patch the file.
# Then simply reboot.
#
# You can verify that this worked (or if it hasn't been run that it is unneeded) by selecting
# "About This Mac" from the Apple menu, then clicking the "More Info..." button, in the new window
# click the "System Report" button. In the System Information that opens, select "Serial-ATA" under
# "Hardware" in the list on the left. Then click on the item which is your SSD. You will see an item
# "TRIM Support:" in the text in the lower right part of the window. If it says "Yes", then TRIM is
# working.
#
set -e
set -x
# Back up the file we're patching
sudo cp \
/System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage \
/System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage.original
# Patch the file to enable TRIM support
# This nulls out the string "APPLE SSD" so that string compares will always pass.
# on 10.9.4 the sequence is WakeKey\x0a\0APPLE SSD\0Time To Ready\0
# on 10.8.3 to 10.8.5 and 10.9.0 to 10.9.3, the sequence is Rotational\0APPLE SSD\0Time To Ready\0
# on 10.8.2, the sequence is Rotational\0APPLE SSD\0MacBook5,1\0
# on 10.8.0, the sequence is Rotational\0\0APPLE SSD\0\0\0Queue Depth\0
# The APPLE SSD is to be replaced with a list of nulls of equal length (9).
sudo perl -p0777i -e 's@((?:Rotational|WakeKey\x0a)\x00{1,20})APPLE SSD(\x00{1,20}[QMT])@$1\x00\x00\x00\x00\x00\x00\x00\x00\x00$2@' \
/System/Library/Extensions/IOAHCIFamily.kext/Contents/PlugIns/IOAHCIBlockStorage.kext/Contents/MacOS/IOAHCIBlockStorage
# Force a reboot of the system's kernel extension cache
sudo touch /System/Library/Extensions/
echo "Now reboot!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment