Is there any way of detecting if a drive is a SSD?
Asked Answered
C

9

37

I'm getting ready to release a tool that is only effective with regular hard drives, not SSD (solid state drive). In fact, it shouldn't be used with SSD's because it will result in a lot of read/writes with no real effectiveness.

Anyone knows of a way of detecting if a given drive is solid-state?

Conscientious answered 25/5, 2009 at 21:59 Comment(2)
This question has an answer on Unix & Linux.Teresita
How to check if my Ubuntu is placed on SSD?Blackpoll
M
4

Detecting SSDs is not as impossible as dseifert makes out. There is already some progress in linux's libata (http://linux.derkeiler.com/Mailing-Lists/Kernel/2009-04/msg03625.html), though it doesn't seem user-ready yet.

And I definitely understand why this needs to be done. It's basically the difference between a linked list and an array. Defragmentation and such is usually counter-productive on a SSD.

Martlet answered 25/5, 2009 at 22:23 Comment(0)
S
42

Finally a reliable solution! Two of them, actually!

Check /sys/block/sdX/queue/rotational, where sdX is the drive name. If it's 0, you're dealing with an SSD, and 1 means plain old HDD.

I can't put my finger on the Linux version where it was introduced, but it's present in Ubuntu's Linux 3.2 and in vanilla Linux 3.6 and not present in vanilla 2.6.38. Oracle also backported it to their Unbreakable Enterprise kernel 5.5, which is based on 2.6.32.

There's also an ioctl to check if the drive is rotational since Linux 3.3, introduced by this commit. Using sysfs is usually more convenient, though.

Stubbed answered 19/10, 2012 at 18:4 Comment(1)
This doesn't account for hybrid drives. What if I were to defrag a hybrid drive by such an algo -- would it potentially shorten the life of it?Normie
I
9

You can actually fairly easily determine the rotational latency -- I did this once as part of a university project. It is described in this report. You'll want to skip to page 7 where you see some nice graphs of the latency. It goes from about 9.3 ms to 1.1 ms -- a drop of 8.2 ms. That corresponds directly to 60 s / 8.2 ms = 7317 RPM.

It was done with simple C code -- here's the part that measures the between positions aand b in a scratch file. We did this with larger and larger b values until we have been wandered all the way around a cylinder:

/* Measure the difference in access time between a and b.  The result
 * is measured in nanoseconds. */
int measure_latency(off_t a, off_t b) {
  cycles_t ta, tb;

  overflow_disk_buffer();

  lseek(work_file, a, SEEK_SET);
  read(work_file, buf, KiB/2);

  ta = get_cycles();
  lseek(work_file, b, SEEK_SET);
  read(work_file, buf, KiB/2);
  tb = get_cycles();

  int diff = (tb - ta)/cycles_per_ns;
  fprintf(stderr, "%i KiB to %i KiB: %i nsec\n", a / KiB, b / KiB, diff);
  return diff;
}
Iridissa answered 25/5, 2009 at 22:22 Comment(2)
Not C code! Jeff with never be able to handle that! PS: Get this mentioned on the pod cast and we get to drink!Buckthorn
Hehe :-) I just saw the "delphi" tag... sorry about that :-) I trust you guy guys to convert it to Pascal, I haven't used Delphi in many years.Iridissa
V
7

This command lsblk -d -o name,rota lists your drives and has a 1 at ROTA if it's a rotational disk and a 0 if it's an SSD. Example output :

NAME ROTA
sda     1
sdb     0
Vaca answered 7/9, 2016 at 11:51 Comment(0)
M
4

Detecting SSDs is not as impossible as dseifert makes out. There is already some progress in linux's libata (http://linux.derkeiler.com/Mailing-Lists/Kernel/2009-04/msg03625.html), though it doesn't seem user-ready yet.

And I definitely understand why this needs to be done. It's basically the difference between a linked list and an array. Defragmentation and such is usually counter-productive on a SSD.

Martlet answered 25/5, 2009 at 22:23 Comment(0)
E
3

You could get lucky by running

smartctl -i sda

from Smartmontools. Almost all SSDs has SSD in the Model field. No guarantee though.

Ecumenical answered 25/5, 2009 at 22:22 Comment(1)
You mean smartctl -i /dev/sdaPinxit
D
2

My two cents to answering this old but very important question... If a disk is accessed via SCSI, then you will (potentially) be able to use SCSI INQUIRY command to request its rotational rate. VPD (Vital Product Data) page for that is called Block Device Characteristics and has a number 0xB1. Bytes 4 and 5 of this page contain a number with meaning:

  • 0000h "Medium rotation rate is not reported"
  • 0001h "Non-rotating medium (e.g., solid state)"
  • 0002h - 0400h "Reserved"
  • 0401h - FFFEh "Nominal medium rotation rate in rotations per minute (i.e., rpm) (e.g., 7 200 rpm = 1C20h, 10 000 rpm = 2710h, and 15 000 rpm = 3A98h)"
  • FFFFh "Reserved"

So, SSD must have 0001h in this field. The T10.org document about this page can be found here.

However, the implementation status of this standard is not clear to me.

Disobedient answered 24/7, 2014 at 15:56 Comment(0)
T
2

I wrote the following javascript code. I needed to determine if machine was ussing SSD drive and if it was boot drive. The solution uses MSFT_PhysicalDisk WMI interface.

function main()
{
    var retval= false;
    // MediaType - 0 Unknown, 3 HDD, 4 SSD
    // SpindleSpeed - -1 has rotational speed, 0 has no rotational speed (SSD)
    // DeviceID - 0 boot device
    var objWMIService = GetObject("winmgmts:\\\\.\\root\\Microsoft\\Windows\\Storage");
    var colItems = objWMIService.ExecQuery("select * from MSFT_PhysicalDisk");  
    var enumItems = new Enumerator(colItems);
    for (; !enumItems.atEnd(); enumItems.moveNext()) 
    {
        var objItem = enumItems.item();
        if (objItem.MediaType == 4 && objItem.SpindleSpeed == 0)
        {
            if (objItem.DeviceID ==0)
            {
                retval=true;
            }
        }
    }
    if (retval)
    {
        WScript.Echo("You have SSD Drive and it is your boot drive.");
    }
    else
    {
        WScript.Echo("You do not have SSD Drive");
    }
    return retval;
}
main();
Tracheo answered 12/5, 2016 at 16:26 Comment(3)
This is not an answer.Kinard
Actually, the WMI accessing MSFT_PhysicalDisk is correct for Windows. I wonder however how is that available to a JavaScript engine (I cannot imagine this API available for browsers).Anselma
Also, why to check for SpindleSpeed if MediaType==4 already ensure SSD type.Anselma
P
0

SSD devices emulate a hard disk device interface, so they can just be used like hard disks. This also means that there is no general way to detect what they are.

You probably could use some characteristics of the drive (latency, speed, size), though this won't be accurate for all drives. Another possibility may be to look at the S.M.A.R.T. data and see whether you can determine the type of disk through this (by model name, certain values), however unless you keep a database of all drives out there, this is not gonna be 100% accurate either.

Prudi answered 25/5, 2009 at 22:13 Comment(3)
I agree, I think the only way is figure out how manufacturers generate the serial key for their SSD devices as opposed to HDD ones. Then build a list of all manufacturers and their SSD string patterns. I used to have to look for some devices serial numbers to determine if they are right for my needs (iPods for US market, Linksys routers with Linux fw)Dismissal
It's not right to say the serial key is the only possible way. Some drives do usefully report their RPM as 1 or 0 (linux.derkeiler.com/Mailing-Lists/Kernel/2009-04/msg02562.html)Martlet
Maybe you are right, but I'm not sure. "Some drives" --> there's the problem, it has to be all drives, no speculation.Dismissal
G
-2

write text file
read text file

repeat 10000 times...

10000/elapsed

for an ssd will be much higher, python3:

def ssd_test():

    doc = 'ssd_test.txt'
    start = time.time()
    for i in range(10000):
        with open(doc, 'w+') as f:
            f.write('ssd test')
            f.close()
        with open(doc, 'r') as f:
            ret = f.read()
            f.close()
    stop = time.time()
    elapsed = stop - start
    ios = int(10000/elapsed)
    hd = 'HDD'
    if ios > 6000: # ssd>8000; hdd <4000
        hd = 'SSD'
    print('detecting hard drive type by read/write speed')
    print('ios', ios, 'hard drive type', hd)
    return hd
Gorblimey answered 9/3, 2019 at 17:40 Comment(4)
Heard about hybrid drives?Accidental
@Marcin Orlowski in the case of searching for hybrid drives by write speed, a quantitative approach would be to write a larger (20GB) json'ified numpy array to disk instead of just "ssd test"; and benchmark from there as hybrids have small ssd capacity. perhaps perform both tests small and large file then you would be able to discern hdd, hybrid, and ssd. For my work, repeatedly read/write small text files for IPC, a hybrid would be satisfactory; it would act as an ssd for this purpose. The op asked needed hdd specifically... in which case ios<1000 would all but ensure such.Gorblimey
My point is that the whole test based of the read/write speed is flawed from ground up. By checking speeds you cannot authoritatively tell what type of medium is that - only thing you can tell for sure is how fast/slow it is. That's it. You can guess and sometimes even get results that look like make sense, but I wouldn't build anything on that.Accidental
Also, a big problem is called cacheAnselma

© 2022 - 2025 — McMap. All rights reserved.