2022-03-22 16:46:35 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
LOGGER() {
|
2022-03-24 15:21:12 +00:00
|
|
|
echo "[$(date +%D-%H:%M:%S)] [$$] $*" >> /tmp/automount.log
|
2022-03-22 16:46:35 +00:00
|
|
|
logger -t "automount[$$]" "$*"
|
|
|
|
}
|
|
|
|
|
2022-03-24 15:21:12 +00:00
|
|
|
if [ "$(uci get fstab.@global[0].auto_mount)" != 1 ]
|
|
|
|
then
|
|
|
|
block mount
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2022-03-22 16:46:35 +00:00
|
|
|
echo "$$" > /tmp/automount.pid
|
2022-03-24 15:21:12 +00:00
|
|
|
touch -f /tmp/automount.log
|
2022-03-22 16:46:35 +00:00
|
|
|
|
|
|
|
case "$HOTPLUG_TYPE,$DEVTYPE" in
|
|
|
|
block,partition)
|
2022-03-24 15:21:12 +00:00
|
|
|
LOGGER "Attached mode: $ACTION"
|
|
|
|
sleep 3
|
|
|
|
if [ "$ACTION" = remove -o "$ACTION" = add ]
|
2022-03-22 16:46:35 +00:00
|
|
|
then
|
2022-03-24 15:21:12 +00:00
|
|
|
for disk in $(mount | awk '{print $1}' | grep /dev | egrep -v "rom|mtdblock|loop|overlay");do
|
2022-03-22 16:46:35 +00:00
|
|
|
part=$(mount | grep $disk | awk '{print $3}')
|
|
|
|
[ ! "$part" ] && continue
|
|
|
|
skip_umount=$(lsblk --list $disk > /dev/null 2>&1; echo $?)
|
2022-03-24 15:21:12 +00:00
|
|
|
if [ "$skip_umount" != 0 ]
|
2022-03-22 16:46:35 +00:00
|
|
|
then
|
2022-03-24 15:21:12 +00:00
|
|
|
umount $part 2> /dev/null || umount -l $part 2> /dev/null
|
|
|
|
LOGGER "Successfully umounted invisible part [$part]"
|
|
|
|
[ ! "$(ls -A $part)" ] && rm -rf $part
|
2022-03-22 16:46:35 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
if [ "$ACTION" = add ]
|
|
|
|
then
|
2022-03-24 15:21:12 +00:00
|
|
|
block mount
|
|
|
|
for part in $(lsblk --list | egrep -v "mtdblock|loop|overlay" | grep part | awk '{print $1}');do
|
2022-03-22 16:46:35 +00:00
|
|
|
skip_mount=$(mount | awk '{print $1}' | grep -q $part ; echo $?)
|
2022-03-24 15:21:12 +00:00
|
|
|
fstype=$(block info /dev/$part 2> /dev/null | egrep -o 'TYPE="[0-9a-zA-Z].+' | awk -F '["]' '/TYPE/{print $2}')
|
|
|
|
if [ "$skip_mount" = 0 ]
|
2022-03-22 16:46:35 +00:00
|
|
|
then
|
2022-03-24 15:21:12 +00:00
|
|
|
LOGGER "Already mounted [/dev/$part] on this device"
|
2022-03-22 16:46:35 +00:00
|
|
|
continue
|
|
|
|
fi
|
2022-03-24 15:21:12 +00:00
|
|
|
if [ -z "$fstype" ]
|
2022-03-22 16:46:35 +00:00
|
|
|
then
|
2022-03-24 15:21:12 +00:00
|
|
|
LOGGER "Unknown filesystem of [/dev/$part]"
|
2022-03-22 16:46:35 +00:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
case "$fstype" in
|
|
|
|
ntfs)
|
2022-03-24 15:21:12 +00:00
|
|
|
extra="-o nls=utf8"
|
2022-03-22 16:46:35 +00:00
|
|
|
;;
|
|
|
|
vfat)
|
2022-03-24 15:21:12 +00:00
|
|
|
extra="-o iocharset=utf8"
|
2022-03-22 16:46:35 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
LOGGER "Try to mount disk [/dev/$part]($fstype) on [/mnt/$part] ..."
|
2022-03-24 15:21:12 +00:00
|
|
|
mkdir -p /mnt/$part
|
|
|
|
chmod 777 /mnt/$part
|
|
|
|
mount -t $fstype /dev/$part /mnt/$part -o rw,defaults,discard $extra
|
|
|
|
if [ "$?" != 0 ]
|
|
|
|
then
|
|
|
|
mount /dev/$part /mnt/$part -o rw,defaults && failed=0 || failed=1
|
|
|
|
else
|
|
|
|
failed=0
|
|
|
|
fi
|
|
|
|
[ "$failed" = 0 ] && LOGGER "Successfully mounted disk [/dev/$part] on [/mnt/$part]" || {
|
2022-03-22 16:46:35 +00:00
|
|
|
LOGGER "Failed to mount disk [/dev/$part] on [/mnt/$part]"
|
|
|
|
}
|
2022-03-24 15:21:12 +00:00
|
|
|
sleep 1 & unset failed
|
2022-03-22 16:46:35 +00:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
rm -f /tmp/automount.pid
|
2022-03-24 15:21:12 +00:00
|
|
|
LOGGER "automount: Finished"
|
2022-03-22 16:46:35 +00:00
|
|
|
exit 0
|