diff options
author | Felix Fietkau <nbd@openwrt.org> | 2010-02-26 22:45:39 +0000 |
---|---|---|
committer | Felix Fietkau <nbd@openwrt.org> | 2010-02-26 22:45:39 +0000 |
commit | bfeb96126dedec0133b8fc5138225394a3ab3af3 (patch) | |
tree | 15fc6051ae24767ae1d830fb96f8df8b7064e2a8 /package/block-mount/files | |
parent | 87298f0de2bf3cfb615c0e6cdeefee58b0534707 (diff) | |
download | mtk-20170518-bfeb96126dedec0133b8fc5138225394a3ab3af3.zip mtk-20170518-bfeb96126dedec0133b8fc5138225394a3ab3af3.tar.gz mtk-20170518-bfeb96126dedec0133b8fc5138225394a3ab3af3.tar.bz2 |
add the block-mount package by Daniel Dickinson (cshore), replacing existing automount functionality
SVN-Revision: 19877
Diffstat (limited to 'package/block-mount/files')
-rw-r--r-- | package/block-mount/files/10-swap | 78 | ||||
-rw-r--r-- | package/block-mount/files/20-fsck | 83 | ||||
-rw-r--r-- | package/block-mount/files/40-mount | 84 | ||||
-rw-r--r-- | package/block-mount/files/block.sh | 262 | ||||
-rw-r--r-- | package/block-mount/files/fsck.sh | 36 | ||||
-rw-r--r-- | package/block-mount/files/fstab.config | 20 | ||||
-rw-r--r-- | package/block-mount/files/fstab.init | 57 | ||||
-rw-r--r-- | package/block-mount/files/mount.sh | 89 |
8 files changed, 709 insertions, 0 deletions
diff --git a/package/block-mount/files/10-swap b/package/block-mount/files/10-swap new file mode 100644 index 0000000..2837d27 --- /dev/null +++ b/package/block-mount/files/10-swap @@ -0,0 +1,78 @@ +#!/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" + } + } + reset_dev_section_cb + ;; + remove) + grep -q "/dev/$device" /proc/swaps && { + swapoff "/dev/$device" + } + ;; + esac +fi + diff --git a/package/block-mount/files/20-fsck b/package/block-mount/files/20-fsck new file mode 100644 index 0000000..c59e5ab --- /dev/null +++ b/package/block-mount/files/20-fsck @@ -0,0 +1,83 @@ +#!/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 +. /lib/functions/fsck.sh + +blkdev=`dirname $DEVPATH` + +if [ `basename $blkdev` != "block" ]; then + + device=`basename $DEVPATH` + + case "$ACTION" in + add) + 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 mds_mount_enabled_fsck + local sds_swap_device + local use_device + local do_fsck=0 + local fsck_type + + local autoswap_from_fstab + local automount_from_fstab + local hotplug_blkid_fstype_match + + mount_dev_section_cb() { + mds_mount_device="$3" + mds_mount_fstype="$4" + mds_mount_enabled_fsck="$7" + } + + 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_fsck" -eq 1 ] && { + do_fsck=1 + fsck_type="$mds_mount_fstype" + } + } + + [ -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" && do_fsck=0 + } + } + + grep -q "/dev/$device" /proc/swaps || grep -q "/dev/$device" /proc/mounts || { + [ "$anon_fsck" -eq 1 ] && [ -z "$use_device" ] && { + use_device="/dev/$device" && do_fsck=1 && mds_mount_fstype="" && hotplug_blkid_fstype_match=0 + } + } + + [ "$do_fsck" -eq 1 ] && { + libmount_fsck "$use_device" "$fsck_type" "$do_fsck" + } + + reset_dev_section_cb + ;; + esac +fi + diff --git a/package/block-mount/files/40-mount b/package/block-mount/files/40-mount new file mode 100644 index 0000000..0d4f669 --- /dev/null +++ b/package/block-mount/files/40-mount @@ -0,0 +1,84 @@ +#!/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 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 do_fsck=0 + local fsck_type + + 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_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/swaps || grep -q "/dev/$device" /proc/mounts || { + ( mkdir -p "$mds_mount_target" && mount "$mds_mount_target" ) 2>&1 | tee /proc/self/fd/2 | logger -t 'fstab' + } + else + logger -t 'fstab' "Mount enabled for $mds_mount_device but it doesn't have a defined mountpoint (target)" + fi + } + } + + [ -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/swaps || grep -q "/dev/$device" /proc/mounts || { + [ "$anon_mount" -eq 1 ] && [ -z "$use_device" ] && { + ( mkdir -p /mnt/$device && mount /dev/$device /mnt/$device ) 2>&1 | tee /proc/self/fd/2 | logger -t 'fstab' + } + } + reset_dev_section_cb + ;; + remove) + umount /dev/$device + ;; + esac + +fi + diff --git a/package/block-mount/files/block.sh b/package/block-mount/files/block.sh new file mode 100644 index 0000000..b8a473a --- /dev/null +++ b/package/block-mount/files/block.sh @@ -0,0 +1,262 @@ +#!/bin/sh +# Copyright 2010 Vertical Communications +# This is free software, licensed under the GNU General Public License v2. +# See /LICENSE for more information. +# + +. /etc/functions.sh + +reset_block_cb() { + mount_cb() { + dmc_mount_cfg="$1" + shift # skip optional param + dmc_target="$2" + dmc_mount_device="$3" + dmc_fstype="$4" + dmc_options="$5" + dmc_enabled="$6" + dmc_enabled_fsck="$7" + dmc_uuid="$8" + dmc_label="$9" + shift + dmc_is_rootfs="$9" + return 0 + } + swap_cb() { + dsc_swap_cfg="$1" + shift # skip optional param + dsc_swap_device="$2" + dsc_enabled="$3" + dsc_uuid="$4" + dsc_label="$5" + return 0 + } +} +reset_block_cb + +reset_dev_section_cb() { + mount_dev_section_cb() { + dmds_mount_cfg="$1" + dmds_mount_target="$2" + dmds_mount_device="$3" + dmds_mount_fstype="$4" + dmds_mount_options="$5" + dmds_mount_enabled="$6" + dmds_mount_enabled_fsck="$7" + dmds_mount_uuid="$8" + dmds_mount_label="$9" + shift + dmds_is_rootfs="$9" + return 0 + } + swap_dev_section_cb() { + dsds_swap_cfg="$1" + dsds_swap_device="$2" + dsds_swap_enabled="$3" + dsds_swap_uuid="$4" + dsds_swap_label="$5" + return 0 + } +} +reset_dev_section_cb + +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="$(mktemp -t '.fstab.XXXXXXXX')" + + mkdir -p /var/lock + lock /var/lock/fstab.lck + cat /tmp/fstab | grep -E -v "^$device[[:blank:]]" >>"$fstabnew" + [ "$enabled" -eq 1 ] && echo "$device none swap sw 0 0" >> "$fstabnew" + cat "$fstabnew" >/tmp/fstab + lock -u /var/lock/fstab.lck + rm -f $fstabnew +} + +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="$(mktemp -t '.fstab.XXXXXXXX')" + + mkdir -p /var/lock + lock /var/lock/fstab.lck + cat /tmp/fstab | grep -E -v "^$device[[:blank:]]" | grep -v "$target" >>"$fstabnew" + echo "$device $target $fstype $options 0 0" >>"$fstabnew" + cat "$fstabnew" >/tmp/fstab + lock -u /var/lock/fstab.lck + rm -f $fstabnew +} + +libmount_find_token() { + local token="$1" + local value="$2" + local device + device="$(blkid | 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" + reset_block_cb +} + +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" + reset_block_cb +} + + diff --git a/package/block-mount/files/fsck.sh b/package/block-mount/files/fsck.sh new file mode 100644 index 0000000..d80e716 --- /dev/null +++ b/package/block-mount/files/fsck.sh @@ -0,0 +1,36 @@ +#!/bin/sh +# Copyright 2010 Vertical Communications +# This is free software, licensed under the GNU General Public License v2. +# See /LICENSE for more information. +# + +libmount_fsck() { + local device="$1" + local fsck_fstype="$2" + local fsck_enabled="$3" + local known_type + local found_fsck=0 + + + [ -n "$fsck_type" ] && [ "$fsck_type" != "swap" ] && { + grep -q "$device" /proc/swaps || grep -q "$device" /proc/mounts || { + [ -e "$device" ] && [ "$fsck_enabled" -eq 1 ] && { + for known_type in $libmount_known_fsck; do + if [ "$known_type" = "$fsck_fstype" ]; then + fsck_${known_type} "$device" + found_fsck=1 + break + fi + done + if [ "$found_fsck" -ne 1 ]; then + logger -t 'fstab' "Unable to check/repair $device; no known fsck for filesystem type $fstype" + fi + } + } + } +} + +libmount_known_fsck="" + +include /lib/functions/fsck + diff --git a/package/block-mount/files/fstab.config b/package/block-mount/files/fstab.config new file mode 100644 index 0000000..f672ef4 --- /dev/null +++ b/package/block-mount/files/fstab.config @@ -0,0 +1,20 @@ +config global automount + option from_fstab 1 + option anon_mount 1 + +config global autoswap + option from_fstab 1 + option anon_swap 0 + +config mount + option target /home + option device /dev/sda1 + option fstype ext3 + option options rw,sync + option enabled 0 + option enabled_fsck 0 + +config swap + option device /dev/sda2 + option enabled 0 + diff --git a/package/block-mount/files/fstab.init b/package/block-mount/files/fstab.init new file mode 100644 index 0000000..bd2bf24 --- /dev/null +++ b/package/block-mount/files/fstab.init @@ -0,0 +1,57 @@ +#!/bin/sh /etc/rc.common +# Copyright (C) 2007 OpenWrt.org +# Copyright (C) 2010 Vertical Communications +# This is free software, licensed under the GNU General Public License v2. +# See /LICENSE for more information. +# + + +START=20 + +. /lib/functions/mount.sh + +do_mount() { + local cfg="$1" + config_mount_by_section "$cfg" +} + +do_swapon() { + local cfg="$1" + config_swapon_by_section "$cfg" +} + +do_unmount() { + local cfg="$1" + + config_get target "$cfg" target + config_get_bool enabled "$cfg" "enabled" '1' + [ -n "$target" -a "$enabled" -gt 0 ] || return 0 + umount $target +} + +do_swapoff() { + local cfg="$1" + + config_get device "$cfg" device + config_get_bool enabled "$cfg" "enabled" '1' + [ -n "$device" -a "$enabled" -gt 0 ] && type swapoff >/dev/null || return 0 + swapoff $device +} + +start() { + config_load fstab + mkdir -p /var/lock + lock /var/lock/fstab.lck + echo '# WARNING: this is an auto generated file, please use uci to set defined filesystems' > /etc/fstab + lock -u /var/lock/fstab.lck + config_foreach do_mount mount + config_foreach do_swapon swap +} + +stop() { + config_load fstab + config_foreach do_unmount mount + config_foreach do_swapoff swap + swapoff -a +} + diff --git a/package/block-mount/files/mount.sh b/package/block-mount/files/mount.sh new file mode 100644 index 0000000..1616ca7 --- /dev/null +++ b/package/block-mount/files/mount.sh @@ -0,0 +1,89 @@ +#!/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/block.sh +. /lib/functions/fsck.sh + +config_mount_by_section() { + local cfg="$1" + local find_rootfs="$2" + + mount_cb() { + local cfg="$1" + local device="$2" + shift + local target="$2" + local cfgdevice="$3" + local fstype="$4" + local options="$5" + local enabled="$6" + local enabled_fsck="$7" + local uuid="$8" + local label="$9" + shift + local is_rootfs="$9" + shift + local found_device="" + local fsck_type="" + + found_device="$(libmount_find_device_by_id "$uuid" "$label" "$device" "$cfgdevice")" + if [ -n "$found_device" ]; then + if [ -z "$find_rootfs" ] || [ "$find_rootfs" -eq 0 ] || [ "$is_rootfs" -eq 1 ]; then + [ "$enabled_fsck" -eq 1 ] && { + grep -q "$found_device" /proc/swaps || grep -q "$found_device" /proc/mounts || { + libmount_fsck "$found_device" "$fsck_type" "$enabled_fsck" + } + } + + [ "$is_rootfs" -eq 1 ] && [ "$find_rootfs" -eq 1 ] && { + target=/overlay + } + config_create_mount_fstab_entry "$found_device" "$target" "$fstype" "$options" "$enabled" + grep -q "$found_device" /proc/swaps || grep -q "$found_device" /proc/mounts || { + [ "$enabled" -eq 1 ] && mkdir -p "$target" && mount "$target" 2>&1 | tee /proc/self/fd/2 | logger -t 'fstab' + } + + fi + fi + [ "$is_rootfs" -eq 1 ] && [ "$find_rootfs" -eq 1 ] && { + rootfs_found=1 + } + return 0 + } + config_get_mount "$cfg" + reset_block_cb +} + +config_swapon_by_section() { + local cfg="$1" + + swap_cb() { + local cfg="$1" + local device="$2" + local cfgdevice="$3" + local enabled="$4" + local uuid="$5" + local label="$6" + local uuid + local label + + local found_device="" + local fsck_type="" + + found_device="$(libmount_find_device_by_id "$uuid" "$label" "$device" "$cfgdevice")" + + if [ -n "$found_device" ]; then + config_create_swap_fstab_entry "$found_device" "$enabled" + grep -q "$found_device" /proc/swaps || grep -q "$found_device" /proc/mounts || { + [ "$enabled" -eq 1 ] && swapon "$found_device" | tee /proc/self/fd/2 | logger -t 'fstab' + } + fi + return 0 + } + config_get_swap "$cfg" + reset_block_cb +} |