Chinaunix首页 | 论坛 | 博客
  • 博客访问: 259394
  • 博文数量: 130
  • 博客积分: 4012
  • 博客等级: 上校
  • 技术积分: 2030
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-10 10:40
文章分类

全部博文(130)

文章存档

2010年(130)

我的朋友

分类: LINUX

2010-01-10 14:20:25

此文出自于linuxsir 中:
usb安全卸载:
4 steps to stop USB device safly:
1.there's no pending I/O request and software cache flushed

2.it's hardware cache flushed

3.it's driver spins down (means "not spinning")

4.(optional) the USB port is put into "suspend" mode
linux 卸载USB设备的时候只是简单的umount。
于是,青年Yan Li <>写了一个脚本可以安全的卸载USB设备。
使用方法:sudo suspend_usb.sh /dev/sdb
注意:第三部分为你的USB设备可以用df -a来查看。因为需要root权限因此用sudo。
我实验了一下,发现u盘的灯在卸载以后果然不闪了。
其实,由于我们的suse内核默认没有支持usb device suspend因此只完成了以上的三步。
此时,我们也可以重新编译内核,让其支持此功能。

#!/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 <>
#
# 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 <>.
#
# To reach the auther, please write an email to the address as stated
# above.

usage()
{
    cat<suspend-usb-device Copyright (C) 2009 Yan Li <>

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<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/safely-remove-usb-hard-drive-in-linux.html
for an detailed explanation.
EOF
    exit 3
fi

echo 'suspend' > "$POWER_LEVEL_FILE"
##---------------------------------------------

阅读(3979) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~