Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Auto mount USB storage device by uuid on OpenWrt
  1. Install USB device support;
opkg install kmod-usb-core kmod-usb-ohci kmod-usb2 kmod-usb-storage e2fsprogs fdisk usbutils mount-utils block-mount kmod-fs-ext4 kmod-fs-vfat kmod-nls-utf-8 kmod-nls-cp437 kmod-nls-iso8859-1

reboot
  1. Install blkid, run opkg update && opkg install blkid;
  2. Copy block.sh to directory /lib/functions;
  3. Copy 10-mount and 20-swap to directory /etc/hotplug.d/block;
  4. That's it! run logread -f command then plug in a USB stick to test.
#!/bin/sh
. /lib/functions/block.sh
set_devices(){
set_section="$(uci show fstab | grep "$get_uuid" | awk -F "." '{print $2}')"
old_device=$(uci get fstab.${set_section}.device)
[ "$old_device" != "/dev/$device" ]&&{
uci set fstab.${set_section}.device="/dev/$device"
uci commit fstab
}
}
set_fstab(){
# modified to fit for blkid of barrier breaker
my_fstype="`blkid | grep "/dev/$device" | awk -F 'TYPE="' '{print $2}' | sed 's/\" //' | awk -F 'PARTUUID=' '{print $1}' | sed 's/"//g'`"
[ -n "$my_fstype" ] && {
if [ "$my_fstype" = 'swap' ]; then
n=$(uci show fstab | grep "fstab.@swap" | grep -c "=swap")
[ $n -gt 0 ] && {
for i in $(seq 0 $n)
do
old_swap="$(uci get fstab.@swap[$i].device)"
[ "$old_swap" = "/dev/$device" ] && {
FLAG="SKIP"
break
}
done
}
[ "$FLAG" != "SKIP" ] && {
uci add fstab swap
uci set fstab.@swap[$n]="swap"
uci set fstab.@swap[$n].enabled='1'
uci set fstab.@swap[$n].device="/dev/$device"
}
else
n=$(uci show fstab | grep "fstab.@mount" | grep -c "=mount")
uci add fstab mount
uci set fstab.@mount[$n]="mount"
uci set fstab.@mount[$n].enabled=1
uci set fstab.@mount[$n].device="/dev/$device"
uci set fstab.@mount[$n].uuid="${get_uuid}"
uci set fstab.@mount[$n].target="/mnt/$device"
uci set fstab.@mount[$n].fstype="$my_fstype"
case "$my_fstype" in
ext*)
uci set fstab.@mount[$n].options="rw";;
'ntfs')
uci set fstab.@mount[$n].options="noatime";;
'exfat')
uci set fstab.@mount[$n].options="noatime";;
'vfat')
uci set fstab.@mount[$n].options="utf8=1,umask=0000,dmask=0000,fmask=0000";;
esac
fi
uci commit fstab
}
}
blkdev=`dirname $DEVPATH`
if [ `basename $blkdev` != "block" ]; then
device=`basename $DEVPATH`
mountpoint=`sed -ne "s|^[^ ]*/$device ||; T; s/ .*//p" /proc/self/mounts`
case "$ACTION" in
add)
get_uuid=`blkid | grep "/dev/${device}" | awk -F "UUID=" '{print $2}'| awk -F "\"" '{print $2}'`
[ -n "$get_uuid" ] && {
have_uuid=$(uci show fstab | grep -c "$get_uuid")
[ "$have_uuid" = "0" ] && set_fstab
[ "$have_uuid" != "0" ] && set_devices
}
local from_fstab
local anon_mount
local anon_swap
local anon_fsck
local mds_mount_target
local mds_mount_device
local mds_mount_fstype
local sds_swap_device
local use_device
local autoswap_from_fstab
local automount_from_fstab
mount_dev_section_cb() {
mds_mount_target="$2"
mds_mount_device="$3"
mds_mount_fstype="$4"
mds_mount_options="$5"
mds_mount_enabled="$6"
}
swap_dev_section_cb() {
sds_swap_device="$2"
return 0
}
config_get_automount
automount_from_fstab="$from_fstab"
[ "$automount_from_fstab" -eq 1 ] && {
config_get_mount_section_by_device "/dev/$device"
use_device="$mds_mount_device"
[ "$mds_mount_enabled" -eq 1 ] && {
if [ -n "$mds_mount_target" ]; then
grep -q "/dev/$device" /proc/mounts || {
mkdir -p "$mds_mount_target"
logger -t 'fstab' "Chang permission of mount point ${mds_mount_target}"
chmod 777 "$mds_mount_target"
case "$mds_mount_fstype" in
'ntfs')
env -i /bin/mount -o "$mds_mount_options" /dev/$device "$mds_mount_target" 2>&1 | tee /proc/self/fd/2 | logger -t 'fstab'
;;
*)
env -i /bin/mount -t "$mds_mount_fstype" -o "$mds_mount_options" /dev/$device "$mds_mount_target" 2>&1 | tee /proc/self/fd/2 | logger -t 'fstab';;
esac
}
else
logger -t 'fstab' "Mount enabled for $mds_mount_device but it doesn't have a defined mountpoint (target)"
fi
}
pgrep aria2c >/dev/null 2>&1 || /etc/init.d/aria2 start
pgrep smbd >/dev/null 2>&1 || /etc/init.d/samba start
# hack for 2.5 inch hard drive delay
#[ -n "`which hdparm`" -a -n "hdparm -I /dev/$device | grep 5400" ] && hdparm -B 254 /dev/$device
}
[ -z "$use_device" ] && {
config_get_autoswap
autoswap_from_fstab="$from_fstab"
[ "$autoswap_from_fstab" -eq 1 ] && {
config_get_swap_section_by_device "/dev/$device"
use_device="$sds_swap_device"
}
}
grep -q "/dev/$device" /proc/mounts || {
[ "$anon_mount" -eq 1 -a -z "$use_device" ] && {
case "$device" in
mtdblock*) ;;
*)
[ -z "`blkid | grep "/dev/$device" | awk -F 'TYPE="' '{print $2}' | sed 's/\" //'`" ] && {
logger -t 'fstab' "/dev/$device is a Logic Disk or has not supported filesystem "
return 0
}
( mkdir -p /mnt/$device && chmod 777 /mnt/$device && mount /dev/$device /mnt/$device ) 2>&1 | tee /proc/self/fd/2 | logger -t 'fstab'
;;
esac
}
}
;;
remove)
umount $mountpoint
rm -rf $mountpoint
;;
esac
fi
#!/bin/sh
# Copyright (C) 2009-2010 OpenWrt.org
# Copyright (C) 2010 Vertical Communications
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
. /lib/functions/block.sh
blkdev=`dirname $DEVPATH`
if [ `basename $blkdev` != "block" ]; then
device=`basename $DEVPATH`
case "$ACTION" in
add)
local autoswap_from_fstab
local automount_from_fstab
local from_fstab
local anon_mount
local anon_swap
local anon_fsck
local mds_mount_device
local sds_swap_device
local sds_swap_enabled
local use_device
local do_swap=0
mount_dev_section_cb() {
mds_mount_device="$3"
}
swap_dev_section_cb() {
sds_swap_device="$2"
sds_swap_enabled="$3"
return 0
}
config_get_automount
automount_from_fstab="$from_fstab"
[ "$automount_from_fstab" -eq 1 ] && {
config_get_mount_section_by_device "/dev/$device"
}
# skip trying swap if this device is defined as a mount point
[ -z "$mds_mount_device" ] && {
config_get_autoswap
autoswap_from_fstab="$from_fstab"
[ "$autoswap_from_fstab" -eq 1 ] && {
config_get_swap_section_by_device "/dev/$device"
use_device="$sds_swap_device"
do_swap="$sds_swap_enabled"
}
[ -z "$use_device" ] && [ "$anon_swap" -eq 1 ] && {
use_device="/dev/$device" && do_swap=1
}
}
[ -n "$use_device" ] && [ "$do_swap" -eq 1 ] && {
grep -q "$use_device" /proc/swaps || grep -q "$use_device" /proc/mounts || {
swapon "$use_device"
}
}
;;
remove)
grep -q "/dev/$device" /proc/swaps && {
swapoff "/dev/$device"
}
;;
esac
fi
#!/bin/sh
# Copyright 2010 Vertical Communications
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
. /lib/functions.sh
config_get_mount() {
local gm_cfg="$1"
local gm_param="$2"
local gm_target
local gm_device
local gm_fstype
local gm_options
local gm_enabled
local gm_enabled_fsck
local gm_uuid
local gm_label
local gm_is_rootfs
config_get gm_target "$1" target
config_get gm_device "$1" device
config_get gm_fstype "$1" fstype 'auto'
config_get gm_options "$1" options 'rw'
config_get_bool gm_enabled "$1" enabled 1
config_get_bool gm_enabled_fsck "$1" enabled_fsck 0
config_get gm_uuid "$1" uuid
config_get gm_label "$1" label
config_get_bool gm_is_rootfs "$1" is_rootfs 0
mount_cb "$gm_cfg" "$gm_param" "$gm_target" "$gm_device" "$gm_fstype" "$gm_options" "$gm_enabled" "$gm_enabled_fsck" "$gm_uuid" "$gm_label" "$gm_is_rootfs"
}
config_get_swap() {
local gs_cfg="$1"
local gs_param="$2"
local gs_device
local gs_enabled
local gs_uuid
local gs_label
config_get gs_device "$1" device
config_get_bool gs_enabled "$1" enabled 1
config_get gs_uuid "$1" uuid
config_get gs_label "$1" label
swap_cb "$gs_cfg" "$gs_param" "$gs_device" "$gs_enabled" "$gs_uuid" "$gs_label"
}
config_get_automount() {
config_load fstab
config_get_bool from_fstab "automount" from_fstab 1
config_get_bool anon_mount "automount" anon_mount 1
config_get_bool anon_fsck "automount" anon_fsck 0
}
config_get_autoswap() {
config_load fstab
config_get_bool from_fstab "autoswap" from_fstab 1
config_get_bool anon_swap "autoswap" anon_swap 0
}
config_create_swap_fstab_entry() {
local device="$1"
local enabled="$2"
[ -n "$device" ] || return 0
local fstabnew
mkdir -p /var/lock
lock /var/lock/fstab.lck
fstabnew="$(mktemp -t '.fstab.XXXXXXXX')"
cat /tmp/fstab | grep -E -v "^$device[[:blank:]]" >>"$fstabnew"
[ "$enabled" -eq 1 ] && echo "$device none swap sw 0 0" >> "$fstabnew"
cat "$fstabnew" >/tmp/fstab
rm -f $fstabnew
lock -u /var/lock/fstab.lck
}
config_create_mount_fstab_entry() {
local device="$1"
local target="$2"
local fstype="$3"
local options="$4"
local enabled="$5"
options="${options:-rw}"
[ "$enabled" -eq 0 ] && options="noauto,$options"
[ -n "$target" ] || return 0
[ -n "$device" ] || return 0
local fstabnew
mkdir -p /var/lock
lock /var/lock/fstab.lck
fstabnew="$(mktemp -t '.fstab.XXXXXXXX')"
cat /tmp/fstab | grep -E -v "^$device[[:blank:]]" | grep -v "$target" >>"$fstabnew"
echo "$device $target $fstype $options 0 0" >>"$fstabnew"
cat "$fstabnew" >/tmp/fstab
rm -f $fstabnew
lock -u /var/lock/fstab.lck
}
libmount_find_token() {
local token="$1"
local value="$2"
local device
device="$(blkid -w /dev/null -c /dev/null | grep "$token=\"$value\"" | cut -f1 -d:)"
echo "$device"
}
libmount_find_device_by_id() {
local uuid="$1"
local label="$2"
local device="$3"
local cfg_device="$4"
local found_device
if [ -n "$uuid" ]; then
found_device="$(libmount_find_token "UUID" "$uuid")"
elif [ -n "$label" ]; then
found_device="$(libmount_find_token "LABEL" "$label")"
elif [ "$device" = "$cfg_device" ]; then
found_device="$device"
elif [ -z "$device" ] && [ -e "$cfg_device" ]; then
found_device="$cfg_device"
fi
[ -n "$device" ] && [ "$device" != "$found_device" ] && {
found_device=""
}
echo "$found_device"
}
config_get_mount_section_by_device() {
local msbd_device="$1"
local msbd_mount_cfg=
local msbd_target=
local msbd_mount_device=
local msbd_fstype=
local msbd_options=
local msbd_enabled=
local msbd_enabled_fsck=
local msbd_uuid=
local msbd_label=
local msbd_is_rootfs
local msbd_blkid_fstype_match=
mount_cb() {
local mc_cfg="$1"
local mc_device="$2"
shift
local mc_target="$2"
local mc_cfgdevice="$3"
local mc_fstype="$4"
local mc_uuid="$8"
local mc_label="$9"
shift
local mc_is_rootfs="$9"
local mc_found_device=""
mc_found_device="$(libmount_find_device_by_id "$mc_uuid" "$mc_label" "$mc_device" "$mc_cfgdevice")"
if [ -n "$mc_found_device" ]; then
msbd_mount_cfg="$mc_cfg"
msbd_target="$mc_target"
msbd_mount_device="$mc_found_device"
msbd_fstype="$mc_fstype"
msbd_options="$4"
msbd_enabled="$5"
msbd_enabled_fsck="$6"
msbd_uuid="$7"
msbd_label="$8"
msbd_is_rootfs="$9"
fi
return 0
}
config_foreach config_get_mount mount "$msbd_device"
[ -n "$msbd_mount_device" ] && config_create_mount_fstab_entry "$msbd_mount_device" "$msbd_target" "$msbd_fstype" "$msbd_options" "$msbd_enabled"
mount_dev_section_cb "$msbd_mount_cfg" "$msbd_target" "$msbd_mount_device" "$msbd_fstype" "$msbd_options" "$msbd_enabled" "$msbd_enabled_fsck" "$msbd_uuid" "$msbd_label" "$msbd_is_rootfs"
}
config_get_swap_section_by_device() {
local ssbd_device="$1"
local ssbd_swap_cfg=
local ssbd_swap_device=
local ssbd_enabled=
local ssbd_uuid=
local ssbd_label=
swap_cb() {
local sc_cfg="$1"
local sc_device="$2"
local sc_uuid="$5"
local sc_label="$6"
local sc_cfgdevice="$3"
local sc_found_device
sc_found_device="$(libmount_find_device_by_id "$sc_uuid" "$sc_label" "$sc_device" "$sc_cfgdevice")"
if [ -n "$sc_found_device" ]; then
ssbd_swap_cfg="$sc_cfg"
ssbd_swap_device="$sc_found_device"
ssbd_enabled="$4"
ssbd_uuid="$5"
ssbd_label="$6"
fi
return 0
}
config_foreach config_get_swap swap "$ssbd_device"
[ -n "$ssbd_swap_device" ] && config_create_swap_fstab_entry "$ssbd_swap_device" "$ssbd_enabled"
swap_dev_section_cb "$ssbd_swap_cfg" "$ssbd_swap_device" "$ssbd_enabled" "$ssbd_uuid" "$ssbd_label"
}
@ffox
Copy link

ffox commented Jan 21, 2015

装了这个脚本后,ext2,ext3格式的可以自动挂载了,但是ntfs的不行,我装的有ntfs-3g啊,不知道为什么?谢谢你的脚本!

Loading

@Interforce
Copy link

Interforce commented Feb 2, 2015

Does not work with NTFS!
HDD not recognised after boot.

Loading

@Abdullbasit
Copy link

Abdullbasit commented Mar 25, 2016

You're the boss !!!
That's a very nice and brilliant trick and it worked great.
Whenever I plug a USB stick it will be auto mounted to /mnt and shared by samba (i set the path to share to /mnt).
Thank you very much

Loading

@indam
Copy link

indam commented Dec 28, 2017

kmod-nls-utf-8 应该是 kmod-nls-utf8

Loading

@AlexTOOT
Copy link

AlexTOOT commented Oct 23, 2018

Hi, did as told. installed all support with no error, installed blkid after reboot and copied thosed files.
When i read log, it says "usb 1-2: new high-speed USB device number 3 using xhci-mtk"
But I cannot get it mounted, in I cannot see my HDD in
What shall I do? I'm using OpenWrt 18.06.1 r7258, the HDD is Ext4.

Loading

@DessertArbiter
Copy link

DessertArbiter commented May 4, 2019

whenever i try to access the drive it says i dont have permission, and when i try to change permission it says read-only

edit: fixed it by changing /bin/mount on line 123 of 10-mount to /usr/bin/ntfs-3g

Loading

@novski
Copy link

novski commented May 31, 2019

since i applied this i get more log info through serial connected console:
before: [ 817.536987] sd 0:0:0:0: [sda] Attached SCSI removable disk
new:Fri May 31 07:39:33 2019 kern.info kernel: [ 817.523934] sda: sda1
i now see both displaying at the same time in the console.
Why?

Loading

@weison620
Copy link

weison620 commented Dec 11, 2019

thanks its work!
but always get message below when booting
[ 82.558982] UBI error: ubi_open_volume: cannot open device 0, volume 2, error -16
[ 85.222975] UBI error: ubi_open_volume: cannot open device 0, volume 2, error -16
[ 88.017642] UBI error: ubi_open_volume: cannot open device 0, volume 2, error -16
[ 90.672307] UBI error: ubi_open_volume: cannot open device 0, volume 2, error -16
[ 93.342541] UBI error: ubi_open_volume: cannot open device 0, volume 2, error -16

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment