LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Debian (https://www.linuxquestions.org/questions/debian-26/)
-   -   Ejecting eSATA & SATA drives. (https://www.linuxquestions.org/questions/debian-26/ejecting-esata-and-sata-drives-4175508755/)

Archy1 06-21-2014 09:25 PM

Ejecting eSATA & SATA drives.
 
I have a SATA to SATA drive caddy taking up one of my 5.25" bays so that I can hot-swap my SATA drives on the fly.

Since Linux saw the drive as internal (how would it know?) normal users can't mount any of the partitions. I fixed that with udev:
Code:

udevadm info -q path -n /dev/sdb
# Replace sdb with the relevant drive.
# The stuff after "host4/" is not relevent and will be replaced with a wildcard.

nano /etc/udev/rules.d/10-esata.rules
        # [nano]
        DEVPATH=="/devices/pci0000:00/0000:00:1f.2/host4/*", ENV{UDISKS_SYSTEM_INTERNAL}="0"
        # Replace "/devices/pci0000:00/0000:00:1f.2/host4/*" with the output of "udevadm info -q path -n /dev/sdb"
        # [/nano]

udevadm control --reload-rules

Yay, now Linux will list it as an external device and I'll have the option to safely eject my drives! Except while that fixed the "standard user mounting problem," it didn't fix the safely eject problem.

After doing some research on the Internet I came across this set of commands to safely eject an eSATA/SATA drive:
Code:

# replace all instances of sdb with the relevant drive
# unmount everything (is there a command to unmount all partitions of a drive?)
sync
hdparm -y /dev/sdb
echo offline > /sys/block/sdb/device/state
echo 1 > /sys/block/sdb/device/delete
# take drive out of caddy

That is great but of course standard users can't eject drives that way.

Is there any way to eject a SATA drive that a standard user could utilize without sudo privileges? Kinda like with USB flash drives?

Relevant installed software:
Debian 7.5 amd64
LXDE desktop (full install)
pcmanfm
gnome-disk-utility

dijetlo 06-22-2014 11:32 AM

Hey Archy,


From the eject man page
Quote:

Eject first SCSI device:

eject sda
Though you'd want to use" eject -n (no ops, test only) -v (verbose) sdb" to test the process.

widget 06-22-2014 02:24 PM

Quote:

Originally Posted by dijetlo (Post 5192098)
Hey Archy,


From the eject man page

Though you'd want to use" eject -n (no ops, test only) -v (verbose) sdb" to test the process.

And if that works the way you want you should be able to add it to the menu options in your file manager.

Archy1 06-22-2014 05:16 PM

Code:

$ eject -v /dev/sdb
eject: device name is `/dev/sdb'
eject: expanded name is `/dev/sdb'
eject: `/dev/sdb' is not mounted
eject: `/dev/sdb' is not a mount point
eject: `/dev/sdb' is a multipartition device
eject: using device name `/dev/sdb' for ioctls
eject: unable to open `/dev/sdb'

As root:
Code:

# eject -v /dev/sdb
eject: device name is `/dev/sdb'
eject: expanded name is `/dev/sdb'
eject: `/dev/sdb' is not mounted
eject: `/dev/sdb' is not a mount point
eject: `/dev/sdb' is a multipartition device
eject: using device name `/dev/sdb' for ioctls
eject: trying to eject `/dev/sdb' using CD-ROM eject command
eject: CD-ROM eject command failed
eject: trying to eject `/dev/sdb' using SCSI commands
eject: SCSI eject failed
eject: trying to eject `/dev/sdb' using floppy eject command
eject: floppy eject command failed
eject: trying to eject `/dev/sdb' using tape offline command
eject: tape offline command failed
eject: unable to eject, last error: Inappropriate ioctl for device


dijetlo 06-22-2014 07:07 PM

Hi Archy

Could you post the output of the mount command (just "#mount", you don't have to sudo to run it) as well as your /etc/fstab? How many partitions on this disk have mount points in the local file system?
The other thing I'm wondering is, mounting and umounting are one thing, removing a drive from powered bay is another. Once you've umounted the partitions on sdb, the files system wont care what happens to them however the hard drive may not react well to being powered down like that. If you've already thought of that and made allowances (tolerant drives, for example) don't worry about it.

Archy1 06-27-2014 12:51 PM

Output of mount with sdb inserted, powed on, and unmounted:
Code:

sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
udev on /dev type devtmpfs (rw,relatime,size=10240k,nr_inodes=2052916,mode=755)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,noexec,relatime,size=1643716k,mode=755)
/dev/mapper/d--t-lvroot on / type ext4 (rw,relatime,errors=remount-ro,user_xattr,barrier=1,data=ordered)
tmpfs on /run/lock type tmpfs (rw,nosuid,nodev,noexec,relatime,size=5120k)
tmpfs on /run/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=3287420k)
/dev/sda2 on /boot type ext2 (rw,relatime,errors=continue)
/dev/sda1 on /boot/efi type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=cp437,iocharset=utf8,shortname=mixed,errors=remount-ro)
/dev/mapper/d--t-lvhome on /home type ext4 (rw,relatime,user_xattr,barrier=1,data=ordered)
/dev/mapper/d--t-tmp on /tmp type ext4 (rw,relatime,user_xattr,barrier=1,stripe=4,data=ordered)
/dev/mapper/d--t-usr on /usr type ext4 (rw,relatime,user_xattr,barrier=1,data=ordered)
/dev/mapper/d--t-var on /var type ext4 (rw,relatime,user_xattr,barrier=1,data=ordered)
rpc_pipefs on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
/dev/sr0 on /media/cdrom0 type iso9660 (ro,nosuid,nodev,noexec,relatime,user=archy)

Output of /etc/fstab:
Code:

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>  <type>  <options>      <dump>  <pass>
/dev/mapper/d--t-lvroot /              ext4    errors=remount-ro 0      1
# /boot was on /dev/sda2 during installation
UUID=bafae4a0-3a0e-41b5-9847-30a92590087a /boot          ext2    defaults        0      2
# /boot/efi was on /dev/sda1 during installation
UUID=C93D-3E54  /boot/efi      vfat    defaults        0      1
/dev/mapper/d--t-lvhome /home          ext4    defaults        0      2
/dev/mapper/d--t-tmp /tmp            ext4    defaults        0      2
/dev/mapper/d--t-usr /usr            ext4    defaults        0      2
/dev/mapper/d--t-var /var            ext4    defaults        0      2
/dev/mapper/d--t-lvswap none            swap    sw              0      0
/dev/sr0        /media/cdrom0  udf,iso9660 user,noauto    0      0

Quote:

Originally Posted by dijetlo (Post 5192259)
How many partitions on this disk have mount points in the local file system?

What are you asking? The SATA HDD in the external bay is mounted like a flash drive (when it is mounted) in a sub-folder of /media.

Quote:

Originally Posted by dijetlo (Post 5192259)
The other thing I'm wondering is, mounting and umounting are one thing, removing a drive from powered bay is another. Once you've umounted the partitions on sdb, the files system wont care what happens to them however the hard drive may not react well to being powered down like that. If you've already thought of that and made allowances (tolerant drives, for example) don't worry about it.

Before removing the HDD from the external bay, I always first unmount all partitions on the drive, execute the script "sync; hdparm -y /dev/sdb; echo offline > /sys/block/sdb/device/state; echo 1 > /sys/block/sdb/device/delete" to spin the drive down and remove the driver from the system, I then power down the hard drive via external SATA bay power switch, and then physically remove the drive. This works fine and is safe, but the problem is not drive safety, it's letting normal users safely eject SATA drives without sudo privileges just like they can do with flash drive. When I connect SATA drives to the computer with a USB "converter" I can from gnome-disk-utility (safely) power down the drive without root access or sudo privileges. This is what I want to do but with SATA drives connected to SATA ports (though it doesn't necessarily have to be done via gnome-disk-utility). The HDD, the HDD bay, and the motherboard all support hot-plugging so there are no hardware or firmware issues.

dijetlo 06-27-2014 05:36 PM

It's seeing the drive as a multi-partition, so I was just wondering are we trying to umount a single mount point of a multi-mount drive (sdb1 is /home, sdb2 is /usr/local/, sdb3 is...etc) and that's why it's failing.
Did you try to run eject against the mount point in /media?
The reason I keep coming back to eject is hdparm and ioctls are both going to require root access, so it seems like we're going to have to find a way to do this with "eject" if you're going to get it working for average users.

Archy1 06-27-2014 07:42 PM

Quote:

Originally Posted by dijetlo (Post 5195146)
It's seeing the drive as a multi-partition, so I was just wondering are we trying to umount a single mount point of a multi-mount drive (sdb1 is /home, sdb2 is /usr/local/, sdb3 is...etc) and that's why it's failing.

Standard users can eject the partitions just fine, it's powering down the device for safe removal that's the problem.

QUESTIONS: If all partitions are unmounted, would it be safe to just cut the power to the drive and then remove it?

Quote:

Originally Posted by dijetlo (Post 5195146)
Did you try to run eject against the mount point in /media?

Code:

$ eject -v /media/63537a60-caf0-4c35-9de7-78c8540828f4/
eject: device name is `/media/63537a60-caf0-4c35-9de7-78c8540828f4'
eject: expanded name is `/media/63537a60-caf0-4c35-9de7-78c8540828f4'
eject: `/dev/sdb1' is mounted at `/media/63537a60-caf0-4c35-9de7-78c8540828f4'
eject: unmounting device `/dev/sdb1' from `/media/63537a60-caf0-4c35-9de7-78c8540828f4'
eject: `/dev/sdb1' is a multipartition device
eject: using device name `/dev/sdb' for ioctls
eject: unable to open `/dev/sdb'

Whoh! Eject just unmounted a partition! But this doesn't power down the device for safe removal :(

Example of USB eject:
Code:

$ eject -v /dev/sdc
eject: device name is `/dev/sdc'
eject: expanded name is `/dev/sdc'
eject: `/dev/sdc' is not mounted
eject: `/dev/sdc' is not a mount point
eject: `/dev/sdc' is a multipartition device
eject: unmounting `/media/A0DA-F24F'
eject: unmounting `/media/root'
eject: using device name `/dev/sdc' for ioctls
eject: trying to eject `/dev/sdc' using CD-ROM eject command
eject: CD-ROM eject command succeeded

Quote:

Originally Posted by dijetlo (Post 5195146)
The reason I keep coming back to eject is hdparm and ioctls are both going to require root access, so it seems like we're going to have to find a way to do this with "eject" if you're going to get it working for average users.

I just discovered a command called "udisks" which can power down drives with the command "udisks --detach /dev/sdx" and does not require root access.

Let's try it on a SATA drive:
Code:

$ udisks --detach /dev/sdb
Detach failed: Device is not detachable

On a USB drive:
Code:

$ udisks --detach /dev/sdc

$ #No text displayed.

$ ls /dev/sdc?
ls: cannot access /dev/sdc?: No such file or directory

$ #Yay!

I wonder if it is possible to make udev view the hot-pluggable SATA port as detachable just as it was possible to make udev allow drives connected to that SATA port to be mounted, unmounted, and formatted by non root users without sudo access with:
Code:

udevadm info -q path -n /dev/sdb
# Replace sdb with the relevant drive.
# The stuff after "host4/" is not relevent and will be replaced with a wildcard.

nano /etc/udev/rules.d/10-esata.rules
        # [nano]
        DEVPATH=="/devices/pci0000:00/0000:00:1f.2/host4/*", ENV{UDISKS_SYSTEM_INTERNAL}="0"
        # Replace "/devices/pci0000:00/0000:00:1f.2/host4/*" with the output of "udevadm info -q path -n /dev/sdb"
        # [/nano]

udevadm control --reload-rules



All times are GMT -5. The time now is 01:54 AM.