安全的移除U盘,移动硬盘(使系统能关闭U盘的电源)

CPU/显卡/打印机/USB设备等硬件问题
zenglanmu
帖子: 9
注册时间: 2009-03-31 9:26

安全的移除U盘,移动硬盘(使系统能关闭U盘的电源)

#1

帖子 zenglanmu » 2009-04-08 21:26

我的8.10U盘卸载后灯还亮,在windows下安全删除硬件后灯是灭的
论坛上也不乏这样的讨论
viewtopic.php?f=48&t=161607&p=1004336
viewtopic.php?t=26759

关于U盘卸载后灯还亮的问题,这样分析下:
U盘在卸载后虽然不挂载在文件系统上了,但还是连上电脑的,用fdisk -l就还能看到U盘的设备名,就/dev/sdx之类的。
可以说U盘的控制芯片并没认为已从系统上离开。
有的网友认为从数据安全的角度讲,这时文件读写完成了,不会对数据造成损害了。而且USB设备应该支持热插拨功能,这时U盘就能直接拔下了。但我觉得还是能把U盘的电源断开比较好
http://code.google.com/p/wdleds/wiki/Pr ... ementation
此贴分析了一下windows下安全删除硬件都做了哪些事情。linux要使U盘卸载后灯不亮还要给U盘送停止等指令才行

http://elliotli.blogspot.com/2009/01/sa ... linux.html
这个帖子作者写了一个脚本,在我的威宝U盘上试验成功。在一个80G刀锋移动硬盘上试时虽然LED灯还亮,但fdisk已经看不到了

#!/bin/bash
#
# suspend-usb-device: an easy-to-use script to properly put an USB
# device into suspend mode that can then be unplugged safely
#
# Copyright (C) 2009, Yan Li <elliot.li.tech@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# To reach the auther, please write an email to the address as stated
# above.

usage()
{
cat<<EOF
suspend-usb-device Copyright (C) 2009 Yan Li <elliot.li.tech@gmail.com>

This script is designed to properly put an USB device into suspend
mode that can then be unplugged safely. It sends a SYNCHRONIZE CACHE
command followed by a START-STOP command (if the device supports it),
unbinds the device from the driver and then suspends the USB
port. After that you can disconnect your USB device safely.

usage:
$0 [options] dev

sample:
$0 /dev/sde

options:
-l show the device and USB bus ID only
-h print this usage

This program comes with ABSOLUTELY NO WARRANTY. This is free
software, and you are welcome to redistribute it under certain
conditions; for details please read the licese at the beginning of the
source code file.
EOF
}

set -e -u

SHOW_DEVICE_ONLY=0
while getopts "l" opt; do
case "$opt" in
h)
usage
exit 2
;;
l)
SHOW_DEVICE_ONLY=1
;;
?)
echo
usage
exit 2
;;
esac
done

DEV_NAME=${!OPTIND:-}


if [ -z ${DEV_NAME} ]; then
usage
exit 2
fi

# mount checking
if mount | grep "^$DEV_NAME[[:digit:]]* "; then
1>&2 echo
1>&2 echo "detected above partition is still mounted, can't suspend device"
1>&2 echo "umount them first"
exit 1
fi

# looking for the parent device with name like
# /devices/pci0000:00/0000:00:1d.7/usb5/5-8

DEVICE=$(udevinfo --query=path --name=${DEV_NAME} --attribute-walk | \
egrep "parent device.*usb[[:digit:]]+/[[:digit:]]+-[[:digit:]]+'" | \
head -n 1 | cut -d"'" -f2)

# the trailing basename of ${DEVICE} is USB_BUS_ID
USB_BUS_ID=${DEVICE##*/}

if [ ${SHOW_DEVICE_ONLY} -eq 1 ]; then
echo Device: ${DEVICE}
echo USB bus ID: ${USB_BUS_ID}
exit 0
fi


if [ -z $DEVICE ]; then
if [ -z $DEVICE ]; then
1>&2 echo "cannot find it's parent USB device, "
1>&2 echo "perhaps it's not an USB device?"
exit 1
fi
fi

# flush all buffers
sync

# root check
if [ `id -u` -ne 0 ]; then
1>&2 echo error, must be run as root, exiting...
exit 1
fi


# send SCSI sync command, some devices don't support this so we just
# ignore errors
sdparm --command=sync "$DEV_NAME" >/dev/null || true
# send SCSI stop command
sdparm --command=stop "$DEV_NAME" >/dev/null

# unbind it
echo -n "${USB_BUS_ID}" > /sys/bus/usb/drivers/usb/unbind

# check if CONFIG_USB_SUSPEND is enabled
POWER_LEVEL_FILE=/sys${DEVICE}/power/level
if [ ! -f "$POWER_LEVEL_FILE" ]; then
1>&2 cat<<EOF
It's safe to remove the USB device now but better can be done. The
power level control file $POWER_LEVEL_FILE
doesn't exist on the system so I have no way to put the USB device
into suspend mode, perhaps you don't have CONFIG_USB_SUSPEND enabled
in your running kernel.

Read
http://elliotli.blogspot.com/2009/01/sa ... linux.html
for an detailed explanation.
EOF
exit 3
fi

echo 'suspend' > "$POWER_LEVEL_FILE"


这个附件的用法是sudo bash usb /dev/sdx(用前要先卸载掉U盘)
这时我目前找到唯一能行的办法。我曾经试过sdparm -command=stop /dev/sdx的方法,但不起作用。
各位如果有更好的方法请无私的贡献出来吧
头像
wangdu2002
帖子: 13284
注册时间: 2008-12-13 19:39
来自: 物华天宝人杰地灵

Re: 安全的移除U盘,移动硬盘(使系统能关闭U盘的电源)

#2

帖子 wangdu2002 » 2009-04-08 21:30

:em09
行到水穷处,坐看云起时。
海内生明月,天涯共此夕。
--------------------吾本独!
头像
wangdu2002
帖子: 13284
注册时间: 2008-12-13 19:39
来自: 物华天宝人杰地灵

Re: 安全的移除U盘,移动硬盘(使系统能关闭U盘的电源)

#3

帖子 wangdu2002 » 2009-04-08 21:30

:em09
行到水穷处,坐看云起时。
海内生明月,天涯共此夕。
--------------------吾本独!
feng8899
帖子: 716
注册时间: 2008-10-31 3:18
联系:

Re: 安全的移除U盘,移动硬盘(使系统能关闭U盘的电源)

#4

帖子 feng8899 » 2009-08-10 4:00

其它的USB设备不知道可不可以直接拔出
Ubuntu 桌面培训
Duo T6400 GeForce 9300MGS 4G win10 Lubuntu18.04 64bit双系统奔跑中
P3 667 512M GeForce MX200 Lbuntu10.04
从零开始学游泳 萨顶顶
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 安全的移除U盘,移动硬盘(使系统能关闭U盘的电源)

#5

帖子 eexpress » 2009-08-10 8:26

只要卸载了,就拔出就是。不管是不是-l 强制卸载与否。
● 鸣学
东风唯笑
帖子: 64
注册时间: 2008-12-03 11:00

Re: 安全的移除U盘,移动硬盘(使系统能关闭U盘的电源)

#6

帖子 东风唯笑 » 2009-08-11 13:03

反正。移动硬盘是一定不能热拔插的,数据丢失是很要命的 :em04
头像
hcym
帖子: 15634
注册时间: 2007-05-06 2:46

Re: 安全的移除U盘,移动硬盘(使系统能关闭U盘的电源)

#7

帖子 hcym » 2009-08-11 13:05

瞎折腾啥

9.10默认就这功能
头像
pxw816
帖子: 296
注册时间: 2007-10-16 19:29

Re: 安全的移除U盘,移动硬盘(使系统能关闭U盘的电源)

#8

帖子 pxw816 » 2009-08-11 13:16

只有XP或者更老的才是这样,删除后灯会灭。但VISTA已经改进了!VISTA删除U盘后和LINUX一样,灯不会灭!
cpu:Intel(R) Core(TM)2 Quad CPU Q6600
mem:2GB DDR2 800 *4
disk:ide320GB+(640GB+640GB)raid
video:Nvidia GTX 260+
network: Atheros AR8121/AR8113
monitor: BENQ FP92W
OS: ubuntu
头像
longdeng
帖子: 51
注册时间: 2009-08-13 21:39

Re: 安全的移除U盘,移动硬盘(使系统能关闭U盘的电源)

#9

帖子 longdeng » 2009-08-25 17:38

:em06 一直都是直接插拔的!
借我三千龙骑,复我浩荡中华!饮马恒河畔,剑指天山西;碎叶城揽月,库叶岛赏雪;黑海之滨钓鲸,贝加尔湖射雕;中南半岛访古,东京废墟遥祭华夏列祖。汉旗指处,望尘逃遁——敢犯中华天威者,虽远必诛!
minoru_harvest
帖子: 83
注册时间: 2008-07-11 11:47

Re: 安全的移除U盘,移动硬盘(使系统能关闭U盘的电源)

#10

帖子 minoru_harvest » 2009-08-25 22:22

这个。。。真不好说呢。
我一个闪盘,在win下,即使弹出后不亮了,还是发热得厉害。我觉得亮不亮没有区别,对于我这个盘。
头像
swindh
帖子: 39
注册时间: 2010-02-23 8:04

Re: 安全的移除U盘,移动硬盘(使系统能关闭U盘的电源)

#11

帖子 swindh » 2010-03-02 8:20

金剑银剑各有所见啊...
google了一下就到了这个贴子了...
我也不知如何是好啊...继续搜...!

代码: 全选

Windows control...Control you...
头像
syrano
帖子: 4313
注册时间: 2007-10-06 18:40

Re: 安全的移除U盘,移动硬盘(使系统能关闭U盘的电源)

#12

帖子 syrano » 2010-03-02 14:03

hcym 写了:瞎折腾啥

9.10默认就这功能
E=m c^2
头像
大宝
帖子: 3050
注册时间: 2008-06-30 22:32
联系:

Re: 安全的移除U盘,移动硬盘(使系统能关闭U盘的电源)

#13

帖子 大宝 » 2010-03-03 10:32

卸了直接拔,未见小U、小硬有何不良反应~
头像
hubert_star
论坛版主
帖子: 5373
注册时间: 2007-10-29 22:12
系统: OSX 10.9 + Ub 1304
来自: 江苏南京

Re: 安全的移除U盘,移动硬盘(使系统能关闭U盘的电源)

#14

帖子 hubert_star » 2010-03-03 11:44

未挂载状态虽然通电,但是硬盘没有读写操作,直接拔下来没问题

想想scsi的热插拔硬盘,通电过程直接拔
佛经说,人有八苦: 生、老、病、死、求不得、怨憎、爱别离、五阴盛 故我苦!
圣经说,人有七罪: 饕餮、贪婪、懒惰、淫欲、傲慢、嫉妒和暴怒  故我有罪!

我这篇帖子里面没有任何攻击我们伟大的中华人民共和国政府和任劳任怨的人民公仆(和本论坛高素质的版主)的文字和含义;

特此声明!

有些事,我们明知道是错的,也要去坚持,因为不甘心;有些人,我们明知道是爱的,也要去放弃,因为没结局;有时候,我们明知道没路了,却还在前行,因为习惯了。

欢迎来我的新浪微博@me
frankvista
帖子: 177
注册时间: 2010-06-16 20:55

Re: 安全的移除U盘,移动硬盘(使系统能关闭U盘的电源)

#15

帖子 frankvista » 2010-07-31 15:45

我在gnome-disk-utility,就是"磁盘实用工具",有一个叫做"安全移除"的点一下,USB的LED灯就灭了,不知道调用什么Terminal指令也可以做到?
回复