Automatic TRIM enabling on third party SSD’s

What’s this about?

Where I work we are upgrading some of the older MBP devices with SSDs to make them more useful. As the disks being used are all happy to run TRIM it makes sense to ensure this is set on automatically after they are rebuilt.

That’s easy isn’t it?

Well yes of course it is,

sudo trimforce enable

is all it takes. Of course this means the people building the machines have to open terminal and type it in and not forget to do it! And of course it restarts imediately.

So put it in the build then!

We use jamf to do the thin imaging and run policies to put down all the settings and programs, sticking the unattended script version of this command

yes | trimforce enable

certainly enables TRIM but it restarts the machine before the policy finishes which causes a few issues.

So what do we do?

Well use a launchDaemon of course! But we don’t want to waste time switching this on on every device that either doesn’t have an SSD or has an Apple SSD so lets be a bit selective.

hasSSD=$(diskutil info / | awk '/Solid State/{print $NF}')
echo "has SSD = $hasSSD"
if [ "$hasSSD" = "Yes" ]; then
	# check if it is an apple SSD
	isAppleSSD=$(diskutil info / | grep "Media Name:" | grep -c APPLE)
	if [ "$isAppleSSD" != "1" ]; then
		# If not apple then check if TRIM is on anyway
		isTRIMOn=$(system_profiler SPSerialATADataType | grep 'TRIM' | grep -c "Yes")
			if [ "$isTRIMOn" != "1" ]; then
			echo "Non Apple SSD and no TRIM detected"
			echo "Setting launchDaemon"
			else
			echo "Non Apple SSD with TRIM enabled detected"
			fi
	else
		echo "Apple SSD fitted. No action"
	fi
else
	echo "No SSD fitted. No action"
fi

This checks all we need to.

So how do we turn TRIM on?

A few extra bits are needed. I use the jamf binaries scheduledTask option to create the launch daemon

jamf scheduledTask -command "sh -c /Library/Management/trimforce.sh" -name trimforce -minute '*/.25/'

This creates a laounch daemon that runs our script 15 secs after the restart

-minute '*/.25/'

I did find that this daemon loaded and ran… which I didn’t want so I added an unload command straight afterwards which sorted that!

launchctl unload /Library/LaunchDaemons/com.jamfsoftware.task.trimforce.plist

You still haven’t turned TRIM on!

Correct, so to do that we need a script for the launch daemon to run. This can be created from within this script but what does it actually need to do?

  1. Delete itself and the launch daemon plist so it doesn’t keep running.. (and it really does!)
  2. Display a message to let the techie know it’s doing it’s thing.
  3. Do it’s thing!

So to do this I used this little routine

createScript() {
# create the script that launchdaemon will run
cat > /Library/Management/trimforce.sh << EOF #!/bin/bash  # delete the files to prevent rerunning rm -f /Library/LaunchDaemons/com.jamfsoftware.task.trimforce.plist rm -f /Library/Management/trimforce.sh  # display message, spawn as separate process "/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper" -windowType hud -title "Warning" -heading "TRIM is enabling" -alignHeading center -description "TRIM is enabling on this device. It will restart very shortly. Please do nothing until it completes." -alignDescription center -icon "/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/Resources/Message.png" -iconSize 75 -button1 ok -defaultButton 0 & echo "**************** running trim setup ***************" >> "/var/log/my.log"
yes | trimforce enable

EOF

chmod +x /Library/Management/trimforce.sh
}

We stick all our management script etc in a standard folder that I reference in here as /library/management/. I use jamf helper to display the message.

Put it all together then.

Ok, so the full section for when it’s required looks like this

isTRIMOn=$(system_profiler SPSerialATADataType | grep 'TRIM' | grep -c "Yes")
			if [ "$isTRIMOn" != "1" ]; then
			echo "Non Apple SSD and no TRIM detected"
			echo "Setting launchDaemon"
			createScript
			jamf scheduledTask -command "sh -c /Library/Management/trimforce.sh" -name trimforce -minute '*/.25/'
			# force unload to prevent running
			launchctl unload /Library/LaunchDaemons/com.jamfsoftware.task.trimforce.plist

Where can I find a full version of this?

As usual it’s on my github. That version also includes the full central logging function that all my current scripts use. It’s adapted from the logging in patchoo

observation from testing.

When running a whole load of tests on various hardware I found that some 2012 MBA disks didn’t say they were Apple but they did show TRIM was enabled so didn’t try re-enabling it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s