SSD-Trim-Test Linux
With the following script you can test whether the operating system is set up correctly for operation with SSDs under Linux. Among other things, a modern file system (e.g. ext4) and correct parameter when mounting (discard).
The script first creates a file containing the first sector of the file from the LBA. It then deletes the file and returns this sector in the LBA. If the operating system is configured for Trim correctly, the output of the sector should now only include zeroes.
#!/bin/bash
# 2012 by Tim
# Tests if Trim is working by directly OS
#
FILE=testfile
DEVICE=/dev/sda
echo ""
echo "Creating test file..."
seq 1 10000 >$FILE
sync
BEGIN=`hdparm --fibmap $FILE |egrep "^[ ]+0" |awk -F" " '{print $2}'`
PARTITION=`df $FILE |egrep "^/dev" |awk -F " " '{print $1}'`
# Fetch partition by uuid or directly
echo $PARTITION |grep -q "by-uuid"
RVAL=$?
if [ $RVAL -eq 0 ]; then
DEVICE=`ls -l $PARTITION |awk -F"->" '{print $2}'`
else
DEVICE=$PARTITION
fi
# Fetch device from partition string
DEVICE=`echo $DEVICE |sed 's/^.*\/\([a-zA-Z]\)[0-9]$//'`
DEVICE=/dev/$DEVICE
echo ""
echo "Found begin of file in LBA at offset: $BEGIN"
echo "Found device for file: $DEVICE"
echo ""
echo ""
echo "------------------------------------"
echo "Sector $BEGIN before deletion."
hdparm --read-sector $BEGIN $DEVICE
echo ""
echo "Deleting test file..."
rm $FILE
sync
echo ""
echo "------------------------------------"
echo "Sector $BEGIN after deletion."
hdparm --read-sector $BEGIN $DEVICE
The following sample output shows that Trim works correctly.
tim@corellia /home/tim
:] ~/test_trim.sh
Creating test file...
Found begin of file in LBA at offset: 100950272
Found device for file: /dev/sdb
------------------------------------
Sector 100950272 before deletion.
/dev/sdb:
reading sector 100950272: succeeded
0a31 0a32 0a33 0a34 0a35 0a36 0a37 0a38
0a39 3031 310a 0a31 3231 310a 0a33 3431
310a 0a35 3631 310a 0a37 3831 310a 0a39
3032 320a 0a31 3232 320a 0a33 3432 320a
0a35 3632 320a 0a37 3832 320a 0a39 3033
330a 0a31 3233 330a 0a33 3433 330a 0a35
3633 330a 0a37 3833 330a 0a39 3034 340a
0a31 3234 340a 0a33 3434 340a 0a35 3634
340a 0a37 3834 340a 0a39 3035 350a 0a31
3235 350a 0a33 3435 350a 0a35 3635 350a
0a37 3835 350a 0a39 3036 360a 0a31 3236
360a 0a33 3436 360a 0a35 3636 360a 0a37
3836 360a 0a39 3037 370a 0a31 3237 370a
0a33 3437 370a 0a35 3637 370a 0a37 3837
370a 0a39 3038 380a 0a31 3238 380a 0a33
3438 380a 0a35 3638 380a 0a37 3838 380a
0a39 3039 390a 0a31 3239 390a 0a33 3439
390a 0a35 3639 390a 0a37 3839 390a 0a39
3031 0a30 3031 0a31 3031 0a32 3031 0a33
3031 0a34 3031 0a35 3031 0a36 3031 0a37
3031 0a38 3031 0a39 3131 0a30 3131 0a31
3131 0a32 3131 0a33 3131 0a34 3131 0a35
3131 0a36 3131 0a37 3131 0a38 3131 0a39
3231 0a30 3231 0a31 3231 0a32 3231 0a33
3231 0a34 3231 0a35 3231 0a36 3231 0a37
3231 0a38 3231 0a39 3331 0a30 3331 0a31
3331 0a32 3331 0a33 3331 0a34 3331 0a35
3331 0a36 3331 0a37 3331 0a38 3331 0a39
3431 0a30 3431 0a31 3431 0a32 3431 0a33
3431 0a34 3431 0a35 3431 0a36 3431 0a37
3431 0a38 3431 0a39 3531 0a30 3531 0a31
3531 0a32 3531 0a33 3531 0a34 3531 0a35
Deleting test file...
------------------------------------
Sector 100950272 after deletion.
/dev/sdb:
reading sector 100950272: succeeded
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000