"Safely remove" equivalent in CLI?

Kernels & Hardware, configuring network, installing services

"Safely remove" equivalent in CLI?

Postby thewanderer » 2010-06-06 14:26

Hi,
What GNOME does with pendrives does surprise me. It not only umounts the partitions, but also shuts the device down and turns off the power light. How do I achieve the same effect with CLI? I've tried eject but it does not cut power.
thewanderer
 
Posts: 416
Joined: 2007-03-19 18:11
Location: my desk, Warsaw, Poland

Re: "Safely remove" equivalent in CLI?

Postby edbarx » 2010-06-06 21:24

Try umount as root.
Code: Select all
umount /dev/device-node
Debian == { > 30, 000 packages }; Debian != systemd
The worst infection of all, is a false sense of security!
It is hard to get away from CLI tools.
User avatar
edbarx
 
Posts: 5379
Joined: 2007-07-18 06:19
Location: 35° 50 N, 14 º 35 E

Re: "Safely remove" equivalent in CLI?

Postby thewanderer » 2010-06-06 22:40

Umount does not cut the power. GNOME does, which never ceases to amaze me.
I've found one way this could be done via echoing "suspend" to power/level of the appropriate device, but this has been removed in 2.6.32 kernels and I'm using 2.6.34 since today.
Hdparm does not work with pendrives, either.
However, I have found this which may be a clue (warning: lots of HTML, might hang Firefox):
http://mail.gnome.org/archives/commits- ... 05078.html
http://git.gnome.org/browse/glib/tree/gio/gdrive.c

but I'm having a really hard time cruching through all those abstract layers of interfaces. Where's the code? All I can see are some pointers...
Looks like GLib has a function for discovering the best "drive stop" type, it's worth investigating I think. Everyone claims it's not possible to cut the power from USB devices on Linux, and the above (and the kernel note about suspend in usb power management docs) seems to be an evidence against that, if anyone can only make some sense of it.
thewanderer
 
Posts: 416
Joined: 2007-03-19 18:11
Location: my desk, Warsaw, Poland

Re: "Safely remove" equivalent in CLI?

Postby julian67 » 2010-06-07 11:32

Maybe this is useful: http://elliotli.blogspot.com/2009/01/safely-remove-usb-hard-drive-in-linux.html

The author has written and linked to a script to suspend usb devices so that they comply to the manufacturer's conditions for safe removal.

Code: Select all
#!/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.

#  ACKNOWLEDGEMENTS:
#      Christian Schmitt <chris@ilovelinux.de> for firewire supporting
#      David <d.tonhofer@m-plify.com> for improving parent device
#      search and verbose output message


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
  -v     verbose

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
VERBOSE=0
while getopts "vlh" opt; do
    case "$opt" in
        h)
            usage
            exit 2
            ;;
        l)
            SHOW_DEVICE_ONLY=1
            ;;
        v)
            VERBOSE=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 "the above disk or partition is still mounted, can't suspend device"
    1>&2 echo "unmount it first using umount"
    exit 1
fi

# looking for the parent of the device with type "usb-storage:usb", it
# is the grand-parent device of the SCSI host, and it's devpath is
# like
# /devices/pci0000:00/0000:00:1d.7/usb5/5-8 (or /fw5/fw5-8 for firewire devices)

# without an USB hub, the device path looks like:
# /devices/pci0000:00/0000:00:1d.7/usb2/2-1/2-1:1.0/host5/target5:0:0/5:0:0:0
# here the grand-parent of host5 is 2-1

# when there's a USB HUB, the device path is like:
# /devices/pci0000:00/0000:00:1d.0/usb5/5-2/5-2.2/5-2.2:1.0/host4/target4:0:0/4:0:0:0
# and the grand-parent of host4 is 5-2.2

DEVICE=$(udevadm info --query=path --name=${DEV_NAME} --attribute-walk | \
    egrep "looking at parent device" | head -1 | \
    sed -e "s/.*looking at parent device '\(\/devices\/.*\)\/.*\/host.*/\1/g")

if [ -z $DEVICE ]; then
    1>&2 echo "cannot find appropriate parent USB/Firewire device, "
    1>&2 echo "perhaps ${DEV_NAME} is not an USB/Firewire device?"
    exit 1
fi

# the trailing basename of ${DEVICE} is DEV_BUS_ID ("5-8" in the
# sample above)
DEV_BUS_ID=${DEVICE##*/}

[[ $VERBOSE == 1 ]] && echo "Found device $DEVICE associated to $DEV_NAME; USB bus id is $DEV_BUS_ID"

if [ ${SHOW_DEVICE_ONLY} -eq 1 ]; then
    echo Device: ${DEVICE}
    echo Bus ID: ${DEV_BUS_ID}
    exit 0
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 with "|| true"
[[ $VERBOSE == 1 ]] && echo "Syncing device $DEV_NAME"
sdparm --command=sync "$DEV_NAME" >/dev/null || true
# send SCSI stop command
[[ $VERBOSE == 1 ]] && echo "Stopping device $DEV_NAME"
sdparm --command=stop "$DEV_NAME" >/dev/null

# unbind it; if this yields "no such device", we are trying to unbind the wrong device
[[ $VERBOSE == 1 ]] && echo "Unbinding device $DEV_BUS_ID"
if [[ "${DEV_BUS_ID}" == fw* ]]
then
    echo -n "${DEV_BUS_ID}" > /sys/bus/firewire/drivers/sbp2/unbind
else
    echo -n "${DEV_BUS_ID}" > /sys/bus/usb/drivers/usb/unbind

    # suspend it if it's an USB device (we have no way to suspend a
    # firewire device yet)

    # check if CONFIG_USB_SUSPEND is enabled
    [[ $VERBOSE == 1 ]] && echo "Checking whether $DEVICE can be suspended"
    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/safely-remove-usb-hard-drive-in-linux.html
for an detailed explanation.
EOF
        exit 3
    fi

    [[ $VERBOSE == 1 ]] && echo "Suspending $DEVICE by writing to $POWER_LEVEL_FILE"
    echo 'suspend' > "$POWER_LEVEL_FILE"
fi


Found this in about 2 seconds with google search "linux command safely remove usb" :wink:
Wisdom from my inbox: "do not mock at your pottenocy"
User avatar
julian67
 
Posts: 4648
Joined: 2007-04-06 14:39
Location: Just hanging around

Re: "Safely remove" equivalent in CLI?

Postby thewanderer » 2010-06-07 16:16

Thanks for that script. Guess what? It does not work at all... :P
USB suspend is no longer possible under 2.6.34 and sdparm --command=stop displays:
Code: Select all
    /dev/sdb: Intenso   Business          0.00

then quits, and the power is still there.

GNOME, on the other hand, just shuts the pendrive down when I click "safely remove". I've tried stracing it with -f option to trace children too, but it seems to spawn its own session so I find it impossible to trace its actions.
Hdparm is unable to shut down an USB device, too. There must be some simple way!
(hal is not installed on my machine, that might narrow down the search...)
thewanderer
 
Posts: 416
Joined: 2007-03-19 18:11
Location: my desk, Warsaw, Poland

Safely remove" equivalent in CLI?

Postby llivv » 2010-06-07 16:38

With my minimal gnome ( I can never tell how big or small it really is) ... I have auto mount of smart card (not sure if they work exactly the same)..
The odd thing is that unmount is grayed and (just unplugging) auto umounts.
I'll see if I can get the safely remove option installed and post back ( but it usually takes a while) especially when triing to locate the the trigger(s)
in the kitchen with Julia ....
[...]
Get on the D bus to B Can ....
[...]
User avatar
llivv
 
Posts: 5586
Joined: 2007-02-14 18:10
Location: willo the tree

"Safely remove" equivalent in CLI?

Postby llivv » 2010-07-05 05:23

I'm going to start listing what I think may be involved and maybe we can all figure out how it works as we go....
I'm fairly sure it's a layered set of processes working together with a sequence of "backend - hal,automout,discover" "supporting apps - pmount, usbutils"
and "ui -nautilus" as possible examples....
I came across one app a few weeks ago I thought fit here. But I wasn't organized enough to post it before I'd forgotten what it was....
So when I noticed disk-manager during an upgrade I figured I'de better get started on this even though I haven't had much time to devote to it yet..

disk-manager
disksearch
disktype
discover
discover-data
libdiscover2
libdiscover-dev
hal
hal-storage1
hal-event



gnome-disk-utility
nautilus
service-discover-applet

etc.
in the kitchen with Julia ....
[...]
Get on the D bus to B Can ....
[...]
User avatar
llivv
 
Posts: 5586
Joined: 2007-02-14 18:10
Location: willo the tree

Re: "Safely remove" equivalent in CLI?

Postby bugsbunny » 2010-07-06 17:51

Since you mentioned kernel 2.6.34 I assume that you're self-compiling. See this thread [SOLVED] usb power management (suspend) gone in 2.6.34?!
User avatar
bugsbunny
 
Posts: 5355
Joined: 2008-07-06 17:04

Re: "Safely remove" equivalent in CLI?

Postby mzilikazi » 2010-07-07 15:52

thewanderer wrote:Hi,
What GNOME does with pendrives does surprise me. It not only umounts the partitions, but also shuts the device down and turns off the power light. How do I achieve the same effect with CLI? I've tried eject but it does not cut power.

No Debian system to look at right now but on my Tinycore system ejecting the device does as you say, the device is umounted and the power light goes out. Can't recall if it behaves the same on either of my Debian laptops at the moment...
So instead of umount try eject;
Code: Select all
eject /dev/sda1
Debian Sid Laptops:
AMD Athlon(tm) 64 X2 Dual-Core Processor TK-55 / 1.5G
Intel(R) Pentium(R) Dual CPU T2390 @ 1.86GHz / 3G
User avatar
mzilikazi
Forum Ninja
 
Posts: 3321
Joined: 2004-09-16 02:14
Location: Colorado Springs, CO

Re: "Safely remove" equivalent in CLI?

Postby thewanderer » 2010-07-14 12:07

Eject brings no results besides telling the kernel to unload the partition table from its memory. So I can no longer access the partitions, but the power is still on. Oh how useful. Thanks for checking with TinyCore, anyway.
Well. I am not compiling the kernel myself as on this hardware I haven't yet been able to come up with a working configuration. I'm using stock 2.6.34 from unstable or so.
As I said before, I haven't got hal, so you might exclude it from your list, llivv.

I once tried to dig through Glib and its helpers, but I find Picasso more comprehensible than these layers of abstraction. Oh, and his works do have an aesthetical value.
It seems to me that there is a "switch" or a huge "if-else" block inside glib, as it has some functions for auto discovery of the proper way to eject some removable drive, I have just not been able to make any sense of the codepath to locate it.
thewanderer
 
Posts: 416
Joined: 2007-03-19 18:11
Location: my desk, Warsaw, Poland

Re: "Safely remove" equivalent in CLI?

Postby bash » 2010-07-26 15:07

I've found one way this could be done via echoing "suspend" to power/level of the appropriate device, but this has been removed in 2.6.32 kernels and I'm using 2.6.34 since today.

Hi, I had the same problem and found 2 solutions to it:
1. You should find you device in sysfs and then echo "auto" to power/level. Than you have to unbind you device as described here http://lwn.net/Articles/143397/ For example, I have my external HDD on fifth port of first hub. And than my command looks like this:
Code: Select all
echo "auto" > "/sys/bus/usb/devices/usb1/1-5/power/level"
echo "1-5:1.0" > /sys/bus/usb/devices/1-5\:1.0/driver/unbind


Default timeout is 2 seconds on my machine. To speed things up you may consider using:
Code: Select all
echo "0" > "/sys/bus/usb/devices/usb1/1-5/power/autosuspend"

It will allow device to suspend immediately if there are no pending operations.

2. Simpler way is to use remove attribute of the device:
Code: Select all
 echo "1" > "/sys/bus/usb/devices/usb1/1-5/remove"


Both solutions works for me, but slightly different. In first case device can't be powered down while it is mounted or there is any operations with it. Also, device can be easily bounded back by using bind attribute. In the second case you can run into trouble because it is possible to remove mounted device, WOW! Also, I didn't find a way to power up this device again without replugging it. Maybe these are not the best solutions but they are the only I found so far.
bash
 
Posts: 1
Joined: 2010-07-26 14:33

Re: "Safely remove" equivalent in CLI?

Postby juru_piotr » 2012-02-05 11:31

Sorry, but I have to refresh this old topic - I've found more appropriate solution. It should be done by means of "udisks" command :D
Code: Select all
udisks --unmount /dev/device && udisks --detach /dev/device

Solution found on http://ubuntuforums.org/showthread.php?t=1821645 8)
juru_piotr
 
Posts: 11
Joined: 2012-01-03 19:19

Re: "Safely remove" equivalent in CLI?

Postby thewanderer » 2012-02-05 12:08

Thanks! Hopefully, udisks is easily strace-able or at least has a readable source so that I can actually learn something from it.
thewanderer
 
Posts: 416
Joined: 2007-03-19 18:11
Location: my desk, Warsaw, Poland

Re: "Safely remove" equivalent in CLI?

Postby praka123 » 2012-04-16 16:42

bash wrote:
I've found one way this could be done via echoing "suspend" to power/level of the appropriate device, but this has been removed in 2.6.32 kernels and I'm using 2.6.34 since today.

Hi, I had the same problem and found 2 solutions to it:
1. You should find you device in sysfs and then echo "auto" to power/level. Than you have to unbind you device as described here http://lwn.net/Articles/143397/ For example, I have my external HDD on fifth port of first hub. And than my command looks like this:
Code: Select all
echo "auto" > "/sys/bus/usb/devices/usb1/1-5/power/level"
echo "1-5:1.0" > /sys/bus/usb/devices/1-5\:1.0/driver/unbind


Default timeout is 2 seconds on my machine. To speed things up you may consider using:
Code: Select all
echo "0" > "/sys/bus/usb/devices/usb1/1-5/power/autosuspend"

It will allow device to suspend immediately if there are no pending operations.

2. Simpler way is to use remove attribute of the device:
Code: Select all
 echo "1" > "/sys/bus/usb/devices/usb1/1-5/remove"


Both solutions works for me, but slightly different. In first case device can't be powered down while it is mounted or there is any operations with it. Also, device can be easily bounded back by using bind attribute. In the second case you can run into trouble because it is possible to remove mounted device, WOW! Also, I didn't find a way to power up this device again without replugging it. Maybe these are not the best solutions but they are the only I found so far.


I am facing problem with a Huawei K4505 3G modem when tries to "Safely remove usb device". it threws out error message like this:

Image

So, I used your method to remove from /sys/bus/usb by following the first 2 and the last command. it did remove the device and it disappeared from "computer:" page in nautilus. but, I am confused about the usage of commands you have given. please elaborate if possible. also, is it better to issue "sync" command to flush out any writing pending, before trying above commands, in case of usb drive?

I have the same issue with Kindle e-reader also to safely umount.

Also, in Windows, this 3g modem opens as modem and as a flash drive together. in Gnu/Linux, I cannot access the drive.
Debian (Testing/Unstable)
User avatar
praka123
 
Posts: 183
Joined: 2007-06-23 08:11
Location: Cochin,India

Re: "Safely remove" equivalent in CLI?

Postby Mr_TechGUY » 2013-01-27 02:17

mzilikazi wrote:
thewanderer wrote:Hi,
What GNOME does with pendrives does surprise me. It not only umounts the partitions, but also shuts the device down and turns off the power light. How do I achieve the same effect with CLI? I've tried eject but it does not cut power.

No Debian system to look at right now but on my Tinycore system ejecting the device does as you say, the device is umounted and the power light goes out. Can't recall if it behaves the same on either of my Debian laptops at the moment...
So instead of umount try eject;
Code: Select all
eject /dev/sda1


This Works for me :D
Mr_TechGUY
 
Posts: 1
Joined: 2013-01-25 23:29


Return to System configuration

Who is online

Users browsing this forum: bookworm1232009 and 11 guests

fashionable