安全的移除U盘,移动硬盘(使系统能关闭U盘的电源)
发表于 : 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的方法,但不起作用。
各位如果有更好的方法请无私的贡献出来吧
论坛上也不乏这样的讨论
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的方法,但不起作用。
各位如果有更好的方法请无私的贡献出来吧