Testing SSD TRIM (discard) support on Linux with LVM and Ext4 HOWTO

Greg Schenzel <inittab AT unixdev DOT net>

Copyright 2012
GNU Free Documentation License

I have found a lack of good documentation on the internet about testing TRIM with LVM/Ext4 so I decided to put together a script to aide in verifying its operation. It makes the assumption that your LVM VG resides on a single disk partition (PV). Though internally the LV does not need to be contiguous. It should work fine with lvresize - UNTESTED. Enhancements are welcome if anyone wants to put in the time to make it autodetect more, clean up the output, or add support for running it when multiple physical volumes are in use. I just ask that you e-mail me any changes or even if you just found this utility useful or have suggestions.

  1. Verify that your SSD supports TRIM using hdparm. If your drive supports TRIM then you will see a line similar to the one highlighted below.
    root@titleist:~# hdparm -I /dev/sda
    
    /dev/sda:
    
    ATA device, with non-removable media
            Model Number:       INTEL SSDSA2M160G2GC
            Serial Number:      CVPO018304Y2160AGN
            Firmware Revision:  2CV102M3
            Transport:          Serial, ATA8-AST, SATA 1.0a, SATA II Extensions, SATA Rev 2.5, SATA Rev 2.6
    Standards:
            Used: ATA/ATAPI-7 T13 1532D revision 1
            Supported: 7 6 5 4
    Configuration:
            Logical         max     current
            cylinders       16383   16383
            heads           16      16
            sectors/track   63      63
            --
            CHS current addressable sectors:   16514064
            LBA    user addressable sectors:  268435455
            LBA48  user addressable sectors:  312581808
            Logical  Sector size:                   512 bytes
            Physical Sector size:                   512 bytes
            device size with M = 1024*1024:      152627 MBytes
            device size with M = 1000*1000:      160041 MBytes (160 GB)
            cache/buffer size  = unknown
            Nominal Media Rotation Rate: Solid State Device
    Capabilities:
            LBA, IORDY(can be disabled)
            Queue depth: 32
            Standby timer values: spec'd by Standard, no device specific minimum
            R/W multiple sector transfer: Max = 16  Current = 16
            DMA: mdma0 mdma1 mdma2 udma0 udma1 udma2 udma3 udma4 udma5 *udma6
                 Cycle time: min=120ns recommended=120ns
            PIO: pio0 pio1 pio2 pio3 pio4
                 Cycle time: no flow control=120ns  IORDY flow control=120ns
    Commands/features:
            Enabled Supported:
               *    SMART feature set
                    Security Mode feature set
               *    Power Management feature set
               *    Write cache
               *    Look-ahead
               *    Host Protected Area feature set
               *    WRITE_BUFFER command
               *    READ_BUFFER command
               *    NOP cmd
               *    DOWNLOAD_MICROCODE
                    SET_MAX security extension
               *    48-bit Address feature set
               *    Device Configuration Overlay feature set
               *    Mandatory FLUSH_CACHE
               *    FLUSH_CACHE_EXT
               *    SMART error logging
               *    SMART self-test
               *    General Purpose Logging feature set
               *    WRITE_{DMA|MULTIPLE}_FUA_EXT
               *    64-bit World wide name
               *    IDLE_IMMEDIATE with UNLOAD
               *    WRITE_UNCORRECTABLE_EXT command
               *    {READ,WRITE}_DMA_EXT_GPL commands
               *    Segmented DOWNLOAD_MICROCODE
               *    Gen1 signaling speed (1.5Gb/s)
               *    Gen2 signaling speed (3.0Gb/s)
               *    Native Command Queueing (NCQ)
               *    Phy event counters
                    Device-initiated interface power management
               *    Software settings preservation
               *    Data Set Management TRIM supported (limit 8 blocks)
               *    Deterministic read ZEROs after TRIM
    Security:
            Master password revision code = 65534
                    supported
            not     enabled
            not     locked
                    frozen
            not     expired: security count
                    supported: enhanced erase
            2min for SECURITY ERASE UNIT. 2min for ENHANCED SECURITY ERASE UNIT.
    Logical Unit WWN Device Identifier: 500151795933ccfd
            NAA             : 5
            IEEE OUI        : 001517
            Unique ID       : 95933ccfd
    Checksum: correct
    
  2. If you're still using ext3, upgrade to ext4 now. There are numerous guides on the internet to assist you with this task if you are unfamiliar with tune2fs or how to get into single-user mode (can also use a recent Knoppix).
  3. Verify that your filesystem has been mounted using the "discard" and "noatime" options in /etc/fstab. I also highly suggest that you make /tmp a memory filesystem.
  4. Download lvm_ext4_test.pl
  5. Edit the script, adjusting the following lines as necessary:
    my $test_vg = "sysvg01"; -- Change this to your VG.
    my $test_lv = "amd64_var"; -- Change this to the LV in question.
    my $test_disk = "/dev/sda"; -- Change this to match the physical disk that your PV resides on.
    my $test_disk_part = "/dev/sda2"; -- Change this to match the partition that your PV resides on.
    my $test_path = "/var/tmp/tempfile"; -- Change this to a path under $test_lv
        
  6. Run the script as root (or under sudo). If discard is working properly with Ext4 and LVM then you will see output similar to the following with 0x20 bytes in the sector for the first invocation and 0x00 bytes at the end.
    sysvg02 backups 0 1073741824 9:1 268435840
    sysvg01 local 0 209715200 8:2 29360512
    sysvg02 files 0 268435456 9:1 384
    sysvg02 files 268435456 213909504 9:1 1342177664
    sysvg01 amd64_usr 0 24764416 8:2 287310208
    sysvg01 amd64_usr 24764416 401408 8:2 251658624
    sysvg01 amd64_usr 25165824 3792896 8:2 268837248
    sysvg01 amd64_root 0 4194304 8:2 247464320
    sysvg01 amd64_var 0 10485760 8:2 252060032
    sysvg01 amd64_var 10485760 2097152 8:2 264642944
    sysvg01 swap 0 2097152 8:2 2097536
    0000000 2020 2020 2020 2020 2020 2020 2020 2020
    *
    01e8470 2020 2020 2020 2020 2020 2020 2020 2e20
    01e8480
       498015 3465216 252060032
    /dev/sda:
    reading sector 256023263: succeeded
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    2020 2020 2020 2020 2020 2020 2020 2020
    
    /dev/sda:
    reading sector 256023263: 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