Skip to content

Instantly share code, notes, and snippets.

@JohnnyonFlame
Last active February 5, 2022 13:23
Show Gist options
  • Save JohnnyonFlame/9896d7474dd7aa19d5ab to your computer and use it in GitHub Desktop.
Save JohnnyonFlame/9896d7474dd7aa19d5ab to your computer and use it in GitHub Desktop.
OpenWRT '/sbin/block hotplug' substitute- works around missing ntfs.c from libblkid-tiny, also does generic automounting.
#!/bin/sh
# /sbin/block hotplug
# Syntax: query_uci_fstab option value
query_uci_fstab () {
blkindex=$(uci show fstab)
blkindex=$(echo $blkindex | grep fstab.@mount | grep .${1}= | grep ${2})
blkindex=$(echo $blkindex | sed -ne 's/.*\.\@mount\[\([^]]*\).*/\1/p')
blkindex=$(echo $blkindex | tail -1)
}
# Syntax: mount_device [options] path target
mount_device () {
if [ -n "$3" ]; then
mkdir -p $3
mount -o $1 $2 $3
else
mkdir -p $2
mount $1 $2
fi
}
if [ $(basename $(dirname $DEVPATH)) == "block" ]; then
exit
fi
device=$(basename $DEVPATH)
dev="/dev/$device"
# Query blkid for device information
blkuuid=$(blkid $dev | sed -ne 's/.* UUID="\([^"]*\).*/\1/p')
blktype=$(blkid $dev | sed -ne 's/.* TYPE="\([^"]*\).*/\1/p')
blklabel=$(blkid $dev | sed -ne 's/.* LABEL="\([^"]*\).*/\1/p')
if [ "$blktype" == "swap" ]; then
case "$ACTION" in
add)
swapon $dev
;;
remove)
swapoff $dev
;;
esac
else
case "$ACTION" in
add)
if [ -z "$blkuuid" ]; then
echo "10-mount, Error: Device not found."
exit
fi
# Use device information to query UCI
query_uci_fstab uuid $blkuuid
if [ -z "$blkindex" ] && [ -n "$blklabel" ]; then
query_uci_fstab label $blklabel
fi
if [ -z "$blkindex" ]; then
query_uci_fstab device $dev
fi
# Be done with it
if [ -z "$blkindex" ]; then
# Mount device using UUID path & default options
mount_device $dev /mnt/$blkuuid
else
# Mount device with known target & options from fstab
blktarget=$(uci -q get fstab.@mount[${blkindex}].target)
blkoptions=$(uci -q get fstab.@mount[${blkindex}].options)
if [ -z "$blktarget" ]; then
echo "10-mount, Warning: No target on fstab, using defaults"
blktarget=/mnt/$blkuuid
fi
mount_device $blkoptions $dev $blktarget
fi
;;
remove)
umount $dev
;;
esac
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment