有没有办法检查USB驱动器是否停止?

时间:2023-01-26 21:43:05

I've written a script to backup my server's HD every night. At the end of the script, I sync, wait a couple of minutes, sync and then I issue sg_start --stop to stop the device. The idea is to extend the lifetime of the device by switching the HD off after ten minutes of incremental backup (desktop disks will survive several thousand on/off cycles but only a few hundred hours of continuous running).

我每天晚上都写了一个脚本来备份服务器的HD。在脚本结束时,我同步,等待几分钟,同步,然后我发出sg_start --stop来停止设备。我们的想法是通过在增量备份十分钟后关闭HD来延长设备的使用寿命(桌面磁盘将经历数千次开/关循环,但只能连续运行几百小时)。

This doesn't always work; I often find the drive still spinning the next morning. Is there a shell command which I can use to check that the drive has stopped (so I can issue the stop command again [EDIT2]or write a script to create a process list when the drive is running so I can debug this[/EDIT2])?

这并不总是有效;我经常发现第二天早上驱动器还在旋转。是否有一个shell命令可用于检查驱动器是否已停止(因此我可以再次发出停止命令[EDIT2]或编写脚本以在驱动器运行时创建进程列表,因此我可以调试此[/ EDIT2] ])?

[EDIT] I've tried sg_inq (as suggested by the sg_start man page) but this command always returns 0.

[编辑]我尝试过sg_inq(由sg_start手册页建议),但此命令始终返回0。

I've tried hdparm but it always returns "drive state is: unknown" for USB drives (attached via /dev/sdX) and when trying to spin down the drive, I get "HDIO_DRIVE_CMD(setidle1) failed: Input/output error".

我已经尝试过hdparm但它总是返回“驱动器状态为:未知”的USB驱动器(通过/ dev / sdX连接),当试图关闭驱动器时,我得到“HDIO_DRIVE_CMD(setidle1)失败:输入/输出错误” 。

sdparm seems to support to set the idle timer on the drive (see "Power mode condition page") but the IDLE option has "Changeable: n" and I haven't found an option which tells me the drive power state.

sdparm似乎支持在驱动器上设置空闲计时器(参见“电源模式条件页面”),但IDLE选项有“可更改:n”,我没有找到一个选项,它告诉我驱动器电源状态。

[EDIT2] Note: I can stop the drive with sg_start --stop from the console. That always works; it just doesn't always stay down until midnight. The sever is in the basement (where it's nice and cool) and I'd rather have a way to check whether the drive is up or not from the warm living room :) If I had a command which told me the status of the drive, I could write a script to alert me when it spins up (check every minute) and then I could try to figure out what might be causing this.

[EDIT2]注意:我可以使用sg_start --stop从控制台停止驱动器。这总是有效的;它并不总是一直持续到午夜。服务器位于地下室(这里很好很酷)我宁愿有办法从温暖的客厅检查驱动器是否启动:)如果我有一个命令告诉我驱动器的状态,我可以写一个脚本来提醒我旋转时(每分钟检查一次),然后我可以试着找出可能导致这种情况的原因。

If that matters: I'm using openSUSE 11.1.

如果重要:我正在使用openSUSE 11.1。

5 个解决方案

#1


When you say you've tried hdparm, you haven't said what you have tried. I have some usb hard drives in an enclosure, and some of the commands work for it and others don't, but I guess it all depends on all facets of the transport mechanism.

当你说你尝试过hdparm时,你还没有说过你尝试过的东西。我在机箱中有一些USB硬盘驱动器,有些命令适用于它而其他命令不适用,但我想这一切都取决于传输机制的所有方面。

  hdparm  -S 120  /dev/sda  

Should tell the drive to sleep itself after ~10 minutes of inactivity.

在大约10分钟不活动后,应该告诉驱动器自己睡觉。

I'm guessing you have already tried this, but its not obvious, and writing this as an answer may help a future reader.

我猜你已经尝试了这个,但它并不明显,把它写成答案可能有助于未来的读者。

Nothing accesses the drive but the backup script.

This is nice in theory, but I have found on odd occasions this is not enough. There are lots of processes and tasks that if they even look at the drive a spin up may occur if the lookup is for some reason out of the disk-cache.

理论上这很好,但我发现在奇怪的情况下这还不够。有许多进程和任务,如果它们甚至查看驱动器,如果由于某种原因从磁盘缓存中查找,则可能会发生旋转。

Common culprits are tools like updatedb scanning all mounts for files, and fam or gamin doing funky stuff to monitor disks for changes.

常见的罪魁祸首是像updatedb扫描文件的所有安装工具,以及使用时髦的东西来监控磁盘的变化。

If in doubt, add a layer of certainty by mounting the device before executing the script, and unmounting it when you are done.

如果有疑问,请在执行脚本之前通过挂载设备添加一层确定性,并在完成后卸载它。

Seeing things that could cause a wakeup

看到可能导致唤醒的事情

 lsof +D /mountpoint

You should probably parse the output of this as well before attempting to unmount to be sure that nothing is still trying to use it.

在尝试卸载之前,你应该解析它的输出,以确保没有任何东西仍在尝试使用它。

You should probably also be doing a lazy umount,

你可能也应该做一个懒惰的umount,

 umount -l /mountpoint 

so if anything accesses it between you doing lsof| grep and calling umount, it will still unmount the drive and stop things being able to read it.

所以如果有什么东西在你做lsof |之间访问它grep和调用umount,它仍然会卸载驱动器并阻止它能够读取它。

HAL and friends

哈尔和朋友们

Its also possible that HAL and friends are doing wakeups to the drive probing for connect/removal state. I really hope it isn't, but it does that on some device types. It seems an unlikely cause, but I'll consider anything possible.

它也可能是HAL和朋友正在对探测连接/移除状态的驱动器进行唤醒。我真的希望它不是,但它在某些设备类型上做到了。这似乎不太可能,但我会考虑任何可能的事情。

Try fun stuff like

尝试有趣的东西

lsof /dev/devicehere 

and

lsof /dev/devicehere1 

Which appears to get a comprehensive list of all things that would be accessing the handle either directly or indirectly.

这似乎可以获得直接或间接访问句柄的所有内容的完整列表。

#2


You also need to check the mount options and use "noatime" as a mount option, otherwise the kernel still updates the access times periodically. So this may be the cause of your problem too.

您还需要检查挂载选项并使用“noatime”作为挂载选项,否则内核仍会定期更新访问时间。所以这也可能是你问题的原因。

#3


I often find the drive still spinning the next morning.

我经常发现第二天早上驱动器还在旋转。

Couldn't this just be because it was spun up again when the server eg. wrote to a log file or something during the night? You might try sdparm, which can return status information on a drive. But I think it's better to just set the option in your BIOS that lets your HD automatically spin down after an amount of inactivity, it's easier.

这不可能是因为它在服务器上再次被旋转,例如。在夜间写入日志文件或其他内容?您可以尝试sdparm,它可以返回驱动器上的状态信息。但我认为最好只在你的BIOS中设置一个选项,让你的HD在一定程度的不活动后自动旋转,这样更容易。

#4


Have you tried stopping the drive with the following command:

您是否尝试使用以下命令停止驱动器:

eject -t /dev/yourHD

This works quite good for my USB hard drives.

这对我的USB硬盘非常有用。

#5


If hdparm -C /dev/sda said drive state is: unknown than it's not supported for your drive and there is no way to tell

如果hdparm -C / dev / sda说驱动器状态是:未知它不支持你的驱动器,没有办法告诉

Your additional questions (answered by others already)

您的其他问题(已由其他人回答)

  • what's using a drive?
    • lsof
    • fuser
    • triggers
  • 什么在使用驱动器? lsof fuser触发器

  • how to force a drive to stay sleeping?
    • hdparm
    • umount
    • your device can still be woken when unmounted but likely only by something you do (smartctl, blkid, etc)
    • 你的设备在卸载时仍然会被唤醒,但可能只是你做的事情(smartctl,blkid等)

  • 如何强迫驱动器保持睡眠? hdparm umount你的设备仍然可以在卸载时被唤醒,但可能只有你做的事情(smartctl,blkid等)

Related: you can also automatically pause backup or whatever for an hour if your drives get to hot.

相关:如果驱动器变热,您还可以自动暂停备份或一小时。

#1


When you say you've tried hdparm, you haven't said what you have tried. I have some usb hard drives in an enclosure, and some of the commands work for it and others don't, but I guess it all depends on all facets of the transport mechanism.

当你说你尝试过hdparm时,你还没有说过你尝试过的东西。我在机箱中有一些USB硬盘驱动器,有些命令适用于它而其他命令不适用,但我想这一切都取决于传输机制的所有方面。

  hdparm  -S 120  /dev/sda  

Should tell the drive to sleep itself after ~10 minutes of inactivity.

在大约10分钟不活动后,应该告诉驱动器自己睡觉。

I'm guessing you have already tried this, but its not obvious, and writing this as an answer may help a future reader.

我猜你已经尝试了这个,但它并不明显,把它写成答案可能有助于未来的读者。

Nothing accesses the drive but the backup script.

This is nice in theory, but I have found on odd occasions this is not enough. There are lots of processes and tasks that if they even look at the drive a spin up may occur if the lookup is for some reason out of the disk-cache.

理论上这很好,但我发现在奇怪的情况下这还不够。有许多进程和任务,如果它们甚至查看驱动器,如果由于某种原因从磁盘缓存中查找,则可能会发生旋转。

Common culprits are tools like updatedb scanning all mounts for files, and fam or gamin doing funky stuff to monitor disks for changes.

常见的罪魁祸首是像updatedb扫描文件的所有安装工具,以及使用时髦的东西来监控磁盘的变化。

If in doubt, add a layer of certainty by mounting the device before executing the script, and unmounting it when you are done.

如果有疑问,请在执行脚本之前通过挂载设备添加一层确定性,并在完成后卸载它。

Seeing things that could cause a wakeup

看到可能导致唤醒的事情

 lsof +D /mountpoint

You should probably parse the output of this as well before attempting to unmount to be sure that nothing is still trying to use it.

在尝试卸载之前,你应该解析它的输出,以确保没有任何东西仍在尝试使用它。

You should probably also be doing a lazy umount,

你可能也应该做一个懒惰的umount,

 umount -l /mountpoint 

so if anything accesses it between you doing lsof| grep and calling umount, it will still unmount the drive and stop things being able to read it.

所以如果有什么东西在你做lsof |之间访问它grep和调用umount,它仍然会卸载驱动器并阻止它能够读取它。

HAL and friends

哈尔和朋友们

Its also possible that HAL and friends are doing wakeups to the drive probing for connect/removal state. I really hope it isn't, but it does that on some device types. It seems an unlikely cause, but I'll consider anything possible.

它也可能是HAL和朋友正在对探测连接/移除状态的驱动器进行唤醒。我真的希望它不是,但它在某些设备类型上做到了。这似乎不太可能,但我会考虑任何可能的事情。

Try fun stuff like

尝试有趣的东西

lsof /dev/devicehere 

and

lsof /dev/devicehere1 

Which appears to get a comprehensive list of all things that would be accessing the handle either directly or indirectly.

这似乎可以获得直接或间接访问句柄的所有内容的完整列表。

#2


You also need to check the mount options and use "noatime" as a mount option, otherwise the kernel still updates the access times periodically. So this may be the cause of your problem too.

您还需要检查挂载选项并使用“noatime”作为挂载选项,否则内核仍会定期更新访问时间。所以这也可能是你问题的原因。

#3


I often find the drive still spinning the next morning.

我经常发现第二天早上驱动器还在旋转。

Couldn't this just be because it was spun up again when the server eg. wrote to a log file or something during the night? You might try sdparm, which can return status information on a drive. But I think it's better to just set the option in your BIOS that lets your HD automatically spin down after an amount of inactivity, it's easier.

这不可能是因为它在服务器上再次被旋转,例如。在夜间写入日志文件或其他内容?您可以尝试sdparm,它可以返回驱动器上的状态信息。但我认为最好只在你的BIOS中设置一个选项,让你的HD在一定程度的不活动后自动旋转,这样更容易。

#4


Have you tried stopping the drive with the following command:

您是否尝试使用以下命令停止驱动器:

eject -t /dev/yourHD

This works quite good for my USB hard drives.

这对我的USB硬盘非常有用。

#5


If hdparm -C /dev/sda said drive state is: unknown than it's not supported for your drive and there is no way to tell

如果hdparm -C / dev / sda说驱动器状态是:未知它不支持你的驱动器,没有办法告诉

Your additional questions (answered by others already)

您的其他问题(已由其他人回答)

  • what's using a drive?
    • lsof
    • fuser
    • triggers
  • 什么在使用驱动器? lsof fuser触发器

  • how to force a drive to stay sleeping?
    • hdparm
    • umount
    • your device can still be woken when unmounted but likely only by something you do (smartctl, blkid, etc)
    • 你的设备在卸载时仍然会被唤醒,但可能只是你做的事情(smartctl,blkid等)

  • 如何强迫驱动器保持睡眠? hdparm umount你的设备仍然可以在卸载时被唤醒,但可能只有你做的事情(smartctl,blkid等)

Related: you can also automatically pause backup or whatever for an hour if your drives get to hot.

相关:如果驱动器变热,您还可以自动暂停备份或一小时。