NETGEAR is aware of a growing number of phone and online scams. To learn how to stay safe click here.

Forum Discussion

ColinG's avatar
ColinG
Aspirant
Mar 13, 2021

Eject USB Drive from command line (ReadyNAS 102) [UPDATE]

Hardware: Netgear ReadyNAS 102. (firmware 6.10.4)

UPDATE:
This is an update to the script in my original post (https://community.netgear.com/t5/Using-your-ReadyNAS-in-Business/Eject-USB-Drive-from-command-line-ReadyNAS-102/td-p/1031498).

I originally wrote this because I could not get umount to work consistently. This script uses the same USB unmount process as the admin web front-end, so is very reliable.


With shell access to your NAS, you can list the USB mounted drives using the following:

# ls -l /media/
total 8
drwxrwxrwx 1 root root 8192 Feb 24 22:19 USB_HDD_3

To unmount using the script, do the following:

# ./usb_eject.sh /media/USB_HDD_3
USB_device=sdc1

Now the drive is unmounted:

# ls -l /media/
total 0

usb_eject.sh

#!/bin/bash

envecho() {
	echo ${1}=${!1}
}

usb_pattern=${1%/}

[[ -z ${usb_pattern} ]] && {
	echo
	echo   "need path to USB device"
	echo   "  eg  $ usb_eject.sh /media/USB_FLASH_6"
	echo
	exit 2
}

# Get device from mountpoint eg /media/USB_FLASH_6
USB_device=$(lsblk -P -o NAME,MOUNTPOINT  |sed -n "s|^NAME=\"\([^\"]*\)\"  *MOUNTPOINT=\"${usb_pattern}\"|\1|p")

[[ -z ${USB_device} ]] && {
	# Ok, that didn't work; how about /run/nfs4/media/USB_FLASH_6?
	USB_device=$(lsblk -P -o NAME,MOUNTPOINT  |sed -n "s|^NAME=\"\([^\"]*\)\"  *MOUNTPOINT=\"/run/nfs4${usb_pattern}\"|\1|p")

	# Out of ideas, exit
	[[ -z ${USB_device} ]] && {
		echo "Cannot detect USB device using lsblk -P -o NAME,MOUNTPOINT"
		exit 1
	}
}

envecho USB_device

# Call event_push to eject the USB device as done from admin console
# event_push logs to /var/log/readynasd/event_push.log
event_push shell readynasd "<xs:delete-s resource-type=\"HotPlug\" resource-id=\"UsbStorage\" action=\"eject\" device=\"${USB_device}\"></xs:delete-s>" 1 1

exit $?

Hope this helps

1 Reply

Replies have been turned off for this discussion

NETGEAR Academy

Boost your skills with the Netgear Academy - Get trained, certified and stay ahead with the latest Netgear technology! 

Join Us!

ProSupport for Business

Comprehensive support plans for maximum network uptime and business peace of mind.

 

Learn More