有什么方法可以检测驱动器是否是SSD?

发布于 2024-12-27 07:28:52 字数 110 浏览 2 评论 0原文

我正准备发布一个仅对普通硬盘有效的工具,对 SSD(固态硬盘)无效。事实上,它不应该与 SSD 一起使用,因为它会导致大量读/写,但没有真正的效果。

有人知道检测给定驱动器是否固态的方法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

抚笙 2025-01-03 07:28:52

终于有了一个可靠的解决方案!实际上是两个!

检查 /sys/block/sdX/queue/rotational,其中 sdX 是驱动器名称。如果它是 0,则您正在处理 SSD,而 1 则表示普通旧 HDD。

我无法确定它是在哪个 Linux 版本中引入的,但它存在于 Ubuntu 的 Linux 3.2 和 vanilla Linux 3.6 中,但不存在于 vanilla 2.6.38 中。 Oracle 还将其向后移植到其 Unbreakable Enterprise 内核 5.5,这是基于 2.6.32 的。

自 Linux 3.3 起,还有一个 ioctl 用于检查驱动器是否可旋转,由 这个提交。不过,使用 sysfs 通常更方便。

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.

伴我老 2025-01-03 07:28:52

实际上,您可以相当容易地确定旋转延迟——我在大学项目中做过一次。 本报告对此进行了描述。您需要跳到第 7 页,在那里您会看到一些漂亮的延迟图表。它从大约 9.3 毫秒减少到 1.1 毫秒,减少了 8.2 毫秒。这直接对应于60 s / 8.2 ms = 7317 RPM

它是用简单的 C 代码完成的——这是测量位置之间的部分临时文件中的 ab。我们使用越来越大的 b 值来做到这一点,直到我们一直绕着圆柱体徘徊:

/* 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;
}

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;
}
漫漫岁月 2025-01-03 07:28:52

此命令 lsblk -d -o name,rota 列出您的驱动器,如果是旋转磁盘,则 ROTA 为 1;如果是 SSD,则 ROTA 为 0。
输出示例:

NAME ROTA
sda     1
sdb     0

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
债姬 2025-01-03 07:28:52

检测 SSD 并不像 dseifert 所说的那么不可能。 Linux 的 libata 已经取得了一些进展(http://linux .derkeiler.com/Mailing-Lists/Kernel/2009-04/msg03625.html),虽然看起来并不用户已经准备好。

我绝对理解为什么需要这样做。这基本上是链表和数组之间的区别。对 SSD 进行碎片整理等通常会适得其反。

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.

囍笑 2025-01-03 07:28:52

Smartmontools运行可能会很幸运

smartctl -i sda

。几乎所有的SSD在Model字段中都有SSD。但不能保证。

You could get lucky by running

smartctl -i sda

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

巷子口的你 2025-01-03 07:28:52

我的两分钱回答这个古老但非常重要的问题...如果通过 SCSI 访问磁盘,那么您将(可能)能够使用 SCSI INQUIRY 命令来请求其旋转速率。 VPD(重要产品数据)页面称为块设备特性,编号为0xB1。本页的字节 4 和 5 包含一个数字,其含义为:

  • 0000h“不报告介质旋转速率”
  • 0001h“非旋转介质(例如固态)”
  • 0002h - 0400h“保留”
  • 0401h - FFFEh“标称介质旋转速率每分钟旋转数(即
    rpm) (例如,7 200 rpm = 1C20h、10 000 rpm = 2710h、15 000 rpm = 3A98h)"
  • FFFFh "保留"

因此,SSD 在此字段中必须有 0001hT10.org 关于此页面的文档可以找到此处

但是,我不清楚该标准的实施情况 。 。

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.

陪你到最终 2025-01-03 07:28:52

我编写了以下 JavaScript 代码。我需要确定机器是否使用 SSD 驱动器以及是否是启动驱动器。该解决方案使用 MSFT_PhysicalDisk WMI 接口。

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();

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();
最舍不得你 2025-01-03 07:28:52

SSD设备模拟硬盘设备接口,因此它们可以像硬盘一样使用。这也意味着没有通用的方法来检测它们是什么。

您可能可以使用驱动器的某些特征(延迟、速度、大小),尽管这并不适用于所有驱动器。另一种可能性可能是查看 SMART 数据,看看是否可以通过此确定磁盘类型(通过型号名称、某些值),但是除非您保留所有驱动器的数据库,否则这不会是 100%准确。

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.

回忆凄美了谁 2025-01-03 07:28:52

写入文本文件
读取文本文件

重复 10000 次...

SSD 的10000/elapsed

会高得多,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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文