优化脚本

This commit is contained in:
Hyy2001X 2022-03-24 23:21:12 +08:00
parent 1264ecf4f6
commit 4c47ed5814
1 changed files with 38 additions and 27 deletions

View File

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