Skip to content

Instantly share code, notes, and snippets.

@abakum
Last active April 9, 2016 22:44
Show Gist options
  • Save abakum/c9eaaba313af3c572dda5af4f426ca09 to your computer and use it in GitHub Desktop.
Save abakum/c9eaaba313af3c572dda5af4f426ca09 to your computer and use it in GitHub Desktop.
openWRT CC 15.05.1 with installed block-mount auto mount ntfs cifs nfs for Banana Pi
#!/bin/sh
#place it to /etc/hotplug.d/block/
#opkg update ; opkg install blkid block-mount
#block detect > /etc/config/fstab ; uci set fstab.@global[0].anon_mount=0 ; uci commit fstab
#. /lib/functions/blk.sh
logit(){
$@ | logger -t fstab
}
doit(){
logit echo $@
$@
}
mnt(){
local device="$1"
local target="$2"
local options="$3"
grep -q -e "$device" -e "$target" /proc/mounts && return 0
[ -n "$options" ] && options="-o $options"
doit mkdir -p "$target"
doit chmod 777 "$target"
doit mount $options "$device" "$target"
logit grep -e "$device" -e "$target" /proc/mounts
}
umnt(){
local target="$1"
[ -z "$target" ] && return 0
doit umount -l "$target"
doit rmdir "$target"
}
# from https://gist.github.com/lanceliao/455687b8e9780d8e487d
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 set fstab.$set_section.target="/mnt/$mntpnt"
uci commit fstab
logit uci show fstab.$set_section
}
}
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'`"
# modified to fit for blkid of CC 15.05.1
my_fstype="$(blkid "/dev/$device" -s TYPE -o value)"
[ -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"
logit uci show fstab.@swap[$n]
}
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/$mntpnt"
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";;
esac
logit uci show fstab.@mount[$n]
fi
uci commit fstab
}
}
logit echo ACTION=$ACTION DEVPATH=$DEVPATH
blkdev="$(dirname $DEVPATH)"
[ "$(basename $blkdev)" = "block" ] && exit 0
device="$(basename $DEVPATH)"
[ -z "${device##mmcblk*}" ] && exit 0
case "$ACTION" in
remove)
umnt "$(sed -ne "s|^[^ ]*/$device ||; T; s/ .*//p" /proc/self/mounts)"
;;
add)
mntpnt="$(blkid /dev/$device -s LABEL -o value)"
[ -z "$mntpnt" ] && mntpnt="$device"
#get_uuid=`blkid | grep "/dev/${device}" | awk -F "UUID=" '{print $2}'| awk -F "\"" '{print $2}'`
# look https://dev.openwrt.org/ticket/22140
get_uuid="$(block info | grep "/dev/$device" | awk -F "UUID=" '{print $2}'| awk -F "\"" '{print $2}')"
[ -z "$get_uuid" ] && get_uuid="$(blkid "/dev/$device" -s UUID -o value)"
[ -n "$get_uuid" ] && {
if [ $(uci show fstab | grep -c "$get_uuid") -eq 0 ] ; then
set_fstab
else
set_devices
fi
}
block info | grep -q "/dev/$device" && exit 0
get_section="$(uci show fstab | grep "$get_uuid" | awk -F "." '{print $2}')"
[ $(uci get fstab.$get_section.enabled) -eq 0 ] && exit 0
case "$(uci get fstab.$get_section.fstype)" in
'ntfs')
mnt "/dev/$device" "$(uci get fstab.$get_section.target)" "$(uci get fstab.$get_section.options)"
;;
esac
;;
esac
#!/bin/sh
#place it to /etc/hotplug.d/iface/
#uci set network.lan.force_link=0 ; uci commit network ; /etc/init.d/network reload
#. /lib/functions/blk.sh
logit(){
$@ | logger -t fstab
}
doit(){
logit echo $@
$@
}
mnt(){
local device="$1"
local target="$2"
local options="$3"
grep -q -e "$device" -e "$target" /proc/mounts && return 0
[ -n "$options" ] && options="-o $options"
doit mkdir -p "$target"
doit chmod 777 "$target"
doit mount $options "$device" "$target"
logit grep -e "$device" -e "$target" /proc/mounts
}
umnt(){
local target="$1"
[ -z "$target" ] && return 0
doit umount -l "$target"
doit rmdir "$target"
}
logit echo ACTION=$ACTION INTERFACE=$INTERFACE DEVICE=$DEVICE
[ "$INTERFACE" = "lan" ] || exit 0
uci show fstab | grep -q -e nfs -e cifs || exit 0
let m=$(uci show fstab | grep -c "=mount")-1
for i in $(seq 0 $m)
do
uci get fstab.@mount[$i].fstype | grep -q -e nfs -e cifs && case "$ACTION" in
ifup)
[ $(uci get fstab.@mount[$i].enabled) -eq 1 ] && mnt "$(uci get fstab.@mount[$i].device)" "$(uci get fstab.@mount[$i].target)" "$(uci get fstab.@mount[$i].options)"
;;
ifdown)
umnt "$(uci get fstab.@mount[$i].target)"
;;
esac
done
@abakum
Copy link
Author

abakum commented Apr 4, 2016

/etc/hotplug.d/block/03-fstab for auto mount ntfs and add to /etc/config/fstab new block device and umount on remove
/etc/hotplug.d/iface/30-fstab for auto mount cifs and nfs if it exist in /etc/config/fstab when ifup and umount on ifdown

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