Is there any way of detecting if a drive is a SSD?
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 a
and 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; }
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
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.