diff options
Diffstat (limited to 'package/base-files/files/lib')
49 files changed, 2083 insertions, 0 deletions
diff --git a/package/base-files/files/lib/firstboot/05_firstboot_skip b/package/base-files/files/lib/firstboot/05_firstboot_skip new file mode 100644 index 000000000..5f44df621 --- /dev/null +++ b/package/base-files/files/lib/firstboot/05_firstboot_skip @@ -0,0 +1,10 @@ +#!/bin/sh + +check_skip() { + if [ "$firstboot_skip_next" = "true" ]; then + return 0 + else + return 1 + fi +} + diff --git a/package/base-files/files/lib/firstboot/10_determine_parts b/package/base-files/files/lib/firstboot/10_determine_parts new file mode 100644 index 000000000..3f56e32a8 --- /dev/null +++ b/package/base-files/files/lib/firstboot/10_determine_parts @@ -0,0 +1,46 @@ +#!/bin/sh + +set_mtd_part() { + partname="rootfs_data" + mtdpart="$(find_mtd_part $partname)" +} + +set_rom_part() { + rom=$(awk '/squashfs/ {print $2}' /proc/mounts) +} + +set_jffs_part() { + jffs=$(awk '/jffs2/ {print $2}' /proc/mounts) +} + +determine_mtd_part() { + set_mtd_part + if [ -z "$mtdpart" ]; then + echo "MTD partition not found." + exit 1 + fi +} + +determine_rom_part() { + check_skip || { + set_rom_part + if [ -z "$rom" ]; then + echo "You do not have a squashfs partition; aborting" + echo "(firstboot cannot be run on jffs2 based firmwares)" + exit 1 + fi + } +} + +determine_jffs2_part() { + check_skip || { + set_jffs_part + } +} + +boot_hook_add switch2jffs determine_mtd_part +boot_hook_add jffs2reset determine_mtd_part +boot_hook_add switch2jffs determine_rom_part +boot_hook_add jffs2reset determine_rom_part +boot_hook_add switch2jffs determine_jffs2_part +boot_hook_add jffs2reset determine_jffs2_part diff --git a/package/base-files/files/lib/firstboot/10_no_fo_clear_overlay b/package/base-files/files/lib/firstboot/10_no_fo_clear_overlay new file mode 100644 index 000000000..8a7d9b066 --- /dev/null +++ b/package/base-files/files/lib/firstboot/10_no_fo_clear_overlay @@ -0,0 +1,17 @@ +#!/bin/sh + +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +no_fo_clear_overlay() { + # switch back to squashfs temporarily + pivot /rom /mnt + + # get rid of the old overlay + umount -l /mnt + + # another umount to get rid of the bind from /tmp/root + umount -l /mnt +} + +boot_hook_add no_fo no_fo_clear_overlay diff --git a/package/base-files/files/lib/firstboot/10_reset_has_mini_fo b/package/base-files/files/lib/firstboot/10_reset_has_mini_fo new file mode 100644 index 000000000..4e285840a --- /dev/null +++ b/package/base-files/files/lib/firstboot/10_reset_has_mini_fo @@ -0,0 +1,12 @@ +#!/bin/sh + +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +reset_check_for_overlay() { + if grep -qE '(mini_fo|overlay)' /proc/filesystems; then + reset_has_fo=true + fi +} + +boot_hook_add jffs2reset reset_check_for_overlay diff --git a/package/base-files/files/lib/firstboot/20_has_mini_fo b/package/base-files/files/lib/firstboot/20_has_mini_fo new file mode 100644 index 000000000..8ca6a4fe5 --- /dev/null +++ b/package/base-files/files/lib/firstboot/20_has_mini_fo @@ -0,0 +1,13 @@ +#!/bin/sh + +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +check_for_overlay() { + if ! grep -qE '(mini_fo|overlay)' /proc/filesystems; then + boot_run_hook no_fo + exit 1 + fi +} + +boot_hook_add switch2jffs check_for_overlay diff --git a/package/base-files/files/lib/firstboot/20_no_fo_mount_jffs b/package/base-files/files/lib/firstboot/20_no_fo_mount_jffs new file mode 100644 index 000000000..8a1d4e06d --- /dev/null +++ b/package/base-files/files/lib/firstboot/20_no_fo_mount_jffs @@ -0,0 +1,15 @@ +#!/bin/sh + +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +no_fo_mount_jffs() { + # initialize jffs2 + mount "$mtdpart" /overlay -t jffs2 || exit + + # workaround to ensure that union can attach properly + sync + ls /overlay >/dev/null +} + +boot_hook_add no_fo no_fo_mount_jffs diff --git a/package/base-files/files/lib/firstboot/20_reset_clear_jffs b/package/base-files/files/lib/firstboot/20_reset_clear_jffs new file mode 100644 index 000000000..a3cd24fe9 --- /dev/null +++ b/package/base-files/files/lib/firstboot/20_reset_clear_jffs @@ -0,0 +1,14 @@ +#!/bin/sh + +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +reset_clear_jffs() { + [ "$reset_has_fo" = "true" ] && { + rm -rf $jffs/* 2>&- + mount -o remount $jffs / 2>&- + exit 0 + } || reset_has_fo=false +} + +boot_hook_add jffs2reset reset_clear_jffs diff --git a/package/base-files/files/lib/firstboot/30_is_rootfs_mounted b/package/base-files/files/lib/firstboot/30_is_rootfs_mounted new file mode 100644 index 000000000..e2ba01d50 --- /dev/null +++ b/package/base-files/files/lib/firstboot/30_is_rootfs_mounted @@ -0,0 +1,10 @@ +#!/bin/sh + +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +skip_if_rootfs_mounted() { + mount "$mtdpart" /rom/overlay -t jffs2 || exit +} + +boot_hook_add switch2jffs skip_if_rootfs_mounted diff --git a/package/base-files/files/lib/firstboot/30_no_fo_pivot b/package/base-files/files/lib/firstboot/30_no_fo_pivot new file mode 100644 index 000000000..b5c2601ee --- /dev/null +++ b/package/base-files/files/lib/firstboot/30_no_fo_pivot @@ -0,0 +1,11 @@ +#!/bin/sh + +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +no_fo_pivot() { + # switch to the new (empty) jffs2 + fopivot /overlay /rom 1 +} + +boot_hook_add no_fo no_fo_pivot diff --git a/package/base-files/files/lib/firstboot/30_reset_copy_rom b/package/base-files/files/lib/firstboot/30_reset_copy_rom new file mode 100644 index 000000000..d91c68947 --- /dev/null +++ b/package/base-files/files/lib/firstboot/30_reset_copy_rom @@ -0,0 +1,13 @@ +#!/bin/sh + +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +reset_copy_rom() { + [ "$reset_has_fo" != "true" ] && { + dupe $jffs $rom + exit 0 + } +} + +boot_hook_add jffs2reset reset_copy_rom diff --git a/package/base-files/files/lib/firstboot/40_copy_ramoverlay b/package/base-files/files/lib/firstboot/40_copy_ramoverlay new file mode 100644 index 000000000..39c2edacb --- /dev/null +++ b/package/base-files/files/lib/firstboot/40_copy_ramoverlay @@ -0,0 +1,15 @@ +#!/bin/sh + +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +copy_ramoverlay() { + # try to avoid fs changing while copying + mount -o remount,ro none / 2>&- + # copy ramoverlay to jffs2 + echo -n "copying files ... " + cp -a /tmp/root/* /rom/overlay 2>&- + echo "done" +} + +boot_hook_add switch2jffs copy_ramoverlay diff --git a/package/base-files/files/lib/firstboot/40_no_fo_copy_ramoverlay b/package/base-files/files/lib/firstboot/40_no_fo_copy_ramoverlay new file mode 100644 index 000000000..ced7c1b66 --- /dev/null +++ b/package/base-files/files/lib/firstboot/40_no_fo_copy_ramoverlay @@ -0,0 +1,14 @@ +#!/bin/sh + +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +no_fo_copy_ramoverlay() { + # copy ramoverlay to jffs2, must be done after switching + # to the new rootfs to avoid creating opaque directories + echo -n "copying files ... " + cp -a /tmp/root/* / >/dev/null 2>&1 + sync +} + +boot_hook_add no_fo no_fo_ramoverlay diff --git a/package/base-files/files/lib/firstboot/50_pivot b/package/base-files/files/lib/firstboot/50_pivot new file mode 100644 index 000000000..53801d7a8 --- /dev/null +++ b/package/base-files/files/lib/firstboot/50_pivot @@ -0,0 +1,17 @@ +#!/bin/sh + +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +with_fo_pivot() { + # switch back to squashfs (temporarily) + # and park the ramdisk ontop of /tmp/root + pivot /rom /mnt + mount -o move /mnt /tmp/root + + # /overlay is the overlay + # /rom is the readonly + fopivot /overlay /rom +} + +boot_hook_add switch2jffs with_fo_pivot diff --git a/package/base-files/files/lib/firstboot/99_10_no_fo_cleanup b/package/base-files/files/lib/firstboot/99_10_no_fo_cleanup new file mode 100644 index 000000000..6dedcb1e8 --- /dev/null +++ b/package/base-files/files/lib/firstboot/99_10_no_fo_cleanup @@ -0,0 +1,13 @@ +#!/bin/sh + +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +no_fo_cleanup() { + echo "done" + umount -l /overlay + umount -l /tmp/root + exit 0 +} + +boot_hook_add no_fo no_fo_cleanup diff --git a/package/base-files/files/lib/firstboot/99_10_with_fo_cleanup b/package/base-files/files/lib/firstboot/99_10_with_fo_cleanup new file mode 100644 index 000000000..0181b39d4 --- /dev/null +++ b/package/base-files/files/lib/firstboot/99_10_with_fo_cleanup @@ -0,0 +1,25 @@ +#!/bin/sh + +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +with_fo_cleanup() { + # try to get rid of /tmp/root + # this will almost always fail + umount /tmp/root 2>&- + grep -q overlay /proc/filesystems && { + cd / + ( + cd /overlay + find -type l + ) | while read FILE; do + [ -z "$FILE" ] && break + if ls -la "$FILE" 2>&- | grep -q '(overlay-whiteout)'; then + rm -f "$FILE" + fi + done + } + exit 0 +} + +boot_hook_add switch2jffs with_fo_cleanup diff --git a/package/base-files/files/lib/functions.sh b/package/base-files/files/lib/functions.sh new file mode 100755 index 000000000..b3a3885f2 --- /dev/null +++ b/package/base-files/files/lib/functions.sh @@ -0,0 +1,340 @@ +#!/bin/sh +# Copyright (C) 2006-2011 OpenWrt.org +# Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de> + + +debug () { + ${DEBUG:-:} "$@" +} +mount() { + busybox mount "$@" +} + +# newline +N=" +" + +_C=0 +NO_EXPORT=1 +LOAD_STATE=1 +LIST_SEP=" " + +hotplug_dev() { + env -i ACTION=$1 INTERFACE=$2 /sbin/hotplug-call net +} + +append() { + local var="$1" + local value="$2" + local sep="${3:- }" + + eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\"" +} + +list_contains() { + local var="$1" + local str="$2" + local val + + eval "val=\" \${$var} \"" + [ "${val%% $str *}" != "$val" ] +} + +list_remove() { + local var="$1" + local remove="$2" + local val + + eval "val=\" \${$var} \"" + val1="${val%% $remove *}" + [ "$val1" = "$val" ] && return + val2="${val##* $remove }" + [ "$val2" = "$val" ] && return + val="${val1## } ${val2%% }" + val="${val%% }" + eval "export ${NO_EXPORT:+-n} -- \"$var=\$val\"" +} + +config_load() { + [ -n "$IPKG_INSTROOT" ] && return 0 + uci_load "$@" +} + +reset_cb() { + config_cb() { return 0; } + option_cb() { return 0; } + list_cb() { return 0; } +} +reset_cb + +package() { + return 0 +} + +config () { + local cfgtype="$1" + local name="$2" + + export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1)) + name="${name:-cfg$CONFIG_NUM_SECTIONS}" + append CONFIG_SECTIONS "$name" + [ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name" + export ${NO_EXPORT:+-n} CONFIG_SECTION="$name" + export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype" +} + +option () { + local varname="$1"; shift + local value="$*" + + export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_${varname}=$value" + [ -n "$NO_CALLBACK" ] || option_cb "$varname" "$*" +} + +list() { + local varname="$1"; shift + local value="$*" + local len + + config_get len "$CONFIG_SECTION" "${varname}_LENGTH" 0 + [ $len = 0 ] && append CONFIG_LIST_STATE "${CONFIG_SECTION}_${varname}" + len=$(($len + 1)) + config_set "$CONFIG_SECTION" "${varname}_ITEM$len" "$value" + config_set "$CONFIG_SECTION" "${varname}_LENGTH" "$len" + append "CONFIG_${CONFIG_SECTION}_${varname}" "$value" "$LIST_SEP" + list_cb "$varname" "$*" +} + +config_rename() { + local OLD="$1" + local NEW="$2" + local oldvar + local newvar + + [ -n "$OLD" -a -n "$NEW" ] || return + for oldvar in `set | grep ^CONFIG_${OLD}_ | \ + sed -e 's/\(.*\)=.*$/\1/'` ; do + newvar="CONFIG_${NEW}_${oldvar##CONFIG_${OLD}_}" + eval "export ${NO_EXPORT:+-n} \"$newvar=\${$oldvar}\"" + unset "$oldvar" + done + export ${NO_EXPORT:+-n} CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , $NEW ,")" + + [ "$CONFIG_SECTION" = "$OLD" ] && export ${NO_EXPORT:+-n} CONFIG_SECTION="$NEW" +} + +config_unset() { + config_set "$1" "$2" "" +} + +config_clear() { + local SECTION="$1" + local oldvar + + list_remove CONFIG_SECTIONS "$SECTION" + export ${NO_EXPORT:+-n} CONFIG_SECTIONS="${SECTION:+$CONFIG_SECTIONS}" + + for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \ + sed -e 's/\(.*\)=.*$/\1/'` ; do + unset $oldvar + done +} + +# config_get <variable> <section> <option> [<default>] +# config_get <section> <option> +config_get() { + case "$3" in + "") eval echo "\${CONFIG_${1}_${2}:-\${4}}";; + *) eval export ${NO_EXPORT:+-n} -- "${1}=\${CONFIG_${2}_${3}:-\${4}}";; + esac +} + +# config_get_bool <variable> <section> <option> [<default>] +config_get_bool() { + local _tmp + config_get _tmp "$2" "$3" "$4" + case "$_tmp" in + 1|on|true|enabled) _tmp=1;; + 0|off|false|disabled) _tmp=0;; + *) _tmp="$4";; + esac + export ${NO_EXPORT:+-n} "$1=$_tmp" +} + +config_set() { + local section="$1" + local option="$2" + local value="$3" + local old_section="$CONFIG_SECTION" + + CONFIG_SECTION="$section" + option "$option" "$value" + CONFIG_SECTION="$old_section" +} + +config_foreach() { + local ___function="$1" + [ "$#" -ge 1 ] && shift + local ___type="$1" + [ "$#" -ge 1 ] && shift + local section cfgtype + + [ -z "$CONFIG_SECTIONS" ] && return 0 + for section in ${CONFIG_SECTIONS}; do + config_get cfgtype "$section" TYPE + [ -n "$___type" -a "x$cfgtype" != "x$___type" ] && continue + eval "$___function \"\$section\" \"\$@\"" + done +} + +config_list_foreach() { + [ "$#" -ge 3 ] || return 0 + local section="$1"; shift + local option="$1"; shift + local function="$1"; shift + local val + local len + local c=1 + + config_get len "${section}" "${option}_LENGTH" + [ -z "$len" ] && return 0 + while [ $c -le "$len" ]; do + config_get val "${section}" "${option}_ITEM$c" + eval "$function \"\$val\" \"$@\"" + c="$(($c + 1))" + done +} + +load_modules() { + [ -d /etc/modules.d ] && { + cd /etc/modules.d + sed 's/^[^#]/insmod &/' $* | ash 2>&- || : + } +} + +include() { + local file + + for file in $(ls $1/*.sh 2>/dev/null); do + . $file + done +} + +find_mtd_part() { + local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')" + local PREFIX=/dev/mtdblock + + PART="${PART##mtd}" + [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/ + echo "${PART:+$PREFIX$PART}" +} + +strtok() { # <string> { <variable> [<separator>] ... } + local tmp + local val="$1" + local count=0 + + shift + + while [ $# -gt 1 ]; do + tmp="${val%%$2*}" + + [ "$tmp" = "$val" ] && break + + val="${val#$tmp$2}" + + export ${NO_EXPORT:+-n} "$1=$tmp"; count=$((count+1)) + shift 2 + done + + if [ $# -gt 0 -a -n "$val" ]; then + export ${NO_EXPORT:+-n} "$1=$val"; count=$((count+1)) + fi + + return $count +} + + +jffs2_mark_erase() { + local part="$(find_mtd_part "$1")" + [ -z "$part" ] && { + echo Partition not found. + return 1 + } + echo -e "\xde\xad\xc0\xde" | mtd -qq write - "$1" +} + +uci_apply_defaults() { + cd /etc/uci-defaults || return 0 + files="$(ls)" + [ -z "$files" ] && return 0 + mkdir -p /tmp/.uci + for file in $files; do + ( . "./$(basename $file)" ) && rm -f "$file" + done + uci commit +} + +group_add() { + local name="$1" + local gid="$2" + local rc + [ -f "${IPKG_INSTROOT}/etc/group" ] || return 1 + [ -n "$IPKG_INSTROOT" ] || lock /var/lock/group + echo "${name}:x:${gid}:" >> ${IPKG_INSTROOT}/etc/group + rc=$? + [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/group + return $rc +} + +group_exists() { + grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/group +} + +user_add() { + local name="${1}" + local uid="${2}" + local gid="${3:-$2}" + local desc="${4:-$1}" + local home="${5:-/var/run/$1}" + local shell="${6:-/bin/false}" + local rc + [ -f "${IPKG_INSTROOT}/etc/passwd" ] || return 1 + [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd + echo "${name}:x:${uid}:${gid}:${desc}:${home}:${shell}" >> ${IPKG_INSTROOT}/etc/passwd + echo "${name}:x:0:0:99999:7:::" >> ${IPKG_INSTROOT}/etc/shadow + rc=$? + [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd + return $rc +} + +user_exists() { + grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/passwd +} + + +pi_include() { + if [ -f "/tmp/overlay/$1" ]; then + . "/tmp/overlay/$1" + elif [ -f "$1" ]; then + . "$1" + elif [ -d "/tmp/overlay/$1" ]; then + if [ -n "$(ls /tmp/overlay/$1/*.sh 2>/dev/null)" ]; then + for src_script in /tmp/overlay/$1/*.sh; do + . "$src_script" + done + fi + elif [ -d "$1" ]; then + if [ -n "$(ls $1/*.sh 2>/dev/null)" ]; then + for src_script in $1/*.sh; do + . "$src_script" + done + fi + else + echo "WARNING: $1 not found" + return 1 + fi + return 0 +} + +[ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh diff --git a/package/base-files/files/lib/functions/boot.sh b/package/base-files/files/lib/functions/boot.sh new file mode 100644 index 000000000..8c3f27ba4 --- /dev/null +++ b/package/base-files/files/lib/functions/boot.sh @@ -0,0 +1,150 @@ +#!/bin/sh +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +mount() { + /bin/busybox mount -o noatime "$@" +} + +boot_hook_splice_start() { + export -n PI_HOOK_SPLICE=1 +} + +boot_hook_splice_finish() { + local hook + for hook in $PI_STACK_LIST; do + local v; eval "v=\${${hook}_splice:+\$${hook}_splice }$hook" + export -n "${hook}=${v% }" + export -n "${hook}_splice=" + done + export -n PI_HOOK_SPLICE= +} + +boot_hook_init() { + local hook="${1}_hook" + export -n "PI_STACK_LIST=${PI_STACK_LIST:+$PI_STACK_LIST }$hook" + export -n "$hook=" +} + +boot_hook_add() { + local hook="${1}_hook${PI_HOOK_SPLICE:+_splice}" + local func="${2}" + + [ -n "$func" ] && { + local v; eval "v=\$$hook" + export -n "$hook=${v:+$v }$func" + } +} + +boot_hook_shift() { + local hook="${1}_hook" + local rvar="${2}" + + local v; eval "v=\$$hook" + [ -n "$v" ] && { + local first="${v%% *}" + + [ "$v" != "${v#* }" ] && \ + export -n "$hook=${v#* }" || \ + export -n "$hook=" + + export -n "$rvar=$first" + return 0 + } + + return 1 +} + +boot_run_hook() { + local hook="$1" + local func + + while boot_hook_shift "$hook" func; do + local ran; eval "ran=\$PI_RAN_$func" + [ -n "$ran" ] || { + export -n "PI_RAN_$func=1" + $func "$1" "$2" + } + done +} + +find_mtd_part() { + local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')" + local PREFIX=/dev/mtdblock + + PART="${PART##mtd}" + [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/ + echo "${PART:+$PREFIX$PART}" +} + +jffs2_ready () { + mtdpart="$(find_mtd_part rootfs_data)" + [ -z "$mtdpart" ] && return 1 + magic=$(hexdump $mtdpart -n 4 -e '4/1 "%02x"') + [ "$magic" != "deadc0de" ] +} + +dupe() { # <new_root> <old_root> + cd $1 + echo -n "creating directories... " + { + cd $2 + find . -xdev -type d + echo "./dev ./overlay ./mnt ./proc ./tmp" + # xdev skips mounted directories + cd $1 + } | xargs mkdir -p + echo "done" + + echo -n "setting up symlinks... " + for file in $(cd $2; find . -xdev -type f;); do + case "$file" in + ./rom/note) ;; #nothing + ./etc/config*|\ + ./usr/lib/opkg/info/*) cp -af $2/$file $file;; + *) ln -sf /rom/${file#./*} $file;; + esac + done + for file in $(cd $2; find . -xdev -type l;); do + cp -af $2/${file#./*} $file + done + echo "done" +} + +pivot() { # <new_root> <old_root> + mount -o move /proc $1/proc && \ + pivot_root $1 $1$2 && { + mount -o move $2/dev /dev + mount -o move $2/tmp /tmp + mount -o move $2/sys /sys 2>&- + mount -o move $2/overlay /overlay 2>&- + return 0 + } +} + +fopivot() { # <rw_root> <ro_root> <dupe?> + root=$1 + { + if grep -q overlay /proc/filesystems; then + mount -t overlayfs -olowerdir=/,upperdir=$1 "overlayfs:$1" /mnt && root=/mnt + elif grep -q mini_fo /proc/filesystems; then + mount -t mini_fo -o base=/,sto=$1 "mini_fo:$1" /mnt 2>&- && root=/mnt + else + mount --bind / /mnt + mount --bind -o union "$1" /mnt && root=/mnt + fi + } || { + [ "$3" = "1" ] && { + mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1 + dupe $1 $rom + } + } + pivot $root $2 +} + +ramoverlay() { + mkdir -p /tmp/root + mount -t tmpfs -o mode=0755 root /tmp/root + fopivot /tmp/root /rom 1 +} + diff --git a/package/base-files/files/lib/functions/network.sh b/package/base-files/files/lib/functions/network.sh new file mode 100644 index 000000000..a4652569b --- /dev/null +++ b/package/base-files/files/lib/functions/network.sh @@ -0,0 +1,170 @@ +. /usr/share/libubox/jshn.sh + +__network_ipaddr() +{ + local __var="$1" + local __iface="$2" + local __family="$3" + local __prefix="${4:-0}" + + local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)" + + json_load "${__tmp:-{}}" + json_get_type __tmp "ipv${__family}_address" + + if [ "$__tmp" = array ]; then + + json_select "ipv${__family}_address" + json_get_type __tmp 1 + + if [ "$__tmp" = object ]; then + + json_select 1 + json_get_var $__var address + + [ $__prefix -gt 0 ] && { + json_get_var __tmp mask + eval "export -- \"$__var=\${$__var}/$__tmp\"" + } + + return 0 + fi + fi + + return 1 +} + +network_get_ipaddr() { __network_ipaddr "$1" "$2" 4 0; } +network_get_ipaddr6() { __network_ipaddr "$1" "$2" 6 0; } + +network_get_subnet() { __network_ipaddr "$1" "$2" 4 1; } +network_get_subnet6() { __network_ipaddr "$1" "$2" 6 1; } + + +__network_gateway() +{ + local __var="$1" + local __iface="$2" + local __family="$3" + + local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)" + local __idx=1 + + json_load "${__tmp:-{}}" + + if json_get_type __tmp route && [ "$__tmp" = array ]; then + + json_select route + + while json_get_type __tmp "$__idx" && [ "$__tmp" = object ]; do + + json_select "$((__idx++))" + json_get_var __tmp target + + case "${__family}/${__tmp}" in + 4/0.0.0.0|6/::) + json_get_var "$__var" nexthop + return $? + ;; + esac + + json_select ".." + + done + fi + + return 1 +} + +network_get_gateway() { __network_gateway "$1" "$2" 4; } +network_get_gateway6() { __network_gateway "$1" "$2" 6; } + + +__network_dns() { + local __var="$1" + local __iface="$2" + local __field="$3" + + local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)" + local __dns="" + local __idx=1 + + json_load "${__tmp:-{}}" + + if json_get_type __tmp "$__field" && [ "$__tmp" = array ]; then + + json_select "$__field" + + while json_get_type __tmp "$__idx" && [ "$__tmp" = string ]; do + + json_get_var __tmp "$((__idx++))" + __dns="${__dns:+$__dns }$__tmp" + + done + fi + + eval "export -- \"$__var=$__dns\"" + [ -n "$__dns" ] +} + +network_get_dnsserver() { __network_dns "$1" "$2" dns_server; } +network_get_dnssearch() { __network_dns "$1" "$2" dns_search; } + + +__network_wan() { + local __var="$1" + local __family="$2" + local __iface + + for __iface in $(ubus list | sed -ne 's/^network\.interface\.//p'); do + if __network_gateway "$__var" "$__iface" "$__family"; then + eval "export -- \"$__var=$__iface\"" + return 0 + fi + done + + eval "export -- \"$__var=\"" + return 1 +} + +network_find_wan() { __network_wan "$1" 4; } +network_find_wan6() { __network_wan "$1" 6; } + + +__network_device() +{ + local __var="$1" + local __iface="$2" + local __field="$3" + + local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)" + [ -n "$__tmp" ] || return 1 + + json_load "$__tmp" + json_get_var "$__var" "$__field" +} + +network_is_up() +{ + local __up + __network_device __up "$1" up && [ $__up -eq 1 ] +} + +network_get_device() { __network_device "$1" "$2" l3_device; } +network_get_physdev() { __network_device "$1" "$2" device; } + + +__network_defer() +{ + local __device="$1" + local __defer="$2" + + json_init + json_add_string name "$__device" + json_add_boolean defer "$__defer" + + ubus call network.device set_state "$(json_dump)" 2>/dev/null +} + +network_defer_device() { __network_defer "$1" 1; } +network_ready_device() { __network_defer "$1" 0; } diff --git a/package/base-files/files/lib/functions/service.sh b/package/base-files/files/lib/functions/service.sh new file mode 100644 index 000000000..cbb717d4d --- /dev/null +++ b/package/base-files/files/lib/functions/service.sh @@ -0,0 +1,129 @@ +# +# service: simple wrapper around start-stop-daemon +# +# Usage: service ACTION EXEC ARGS... +# +# Action: +# -C check if EXEC is alive +# -S start EXEC, passing it ARGS as its arguments +# -K kill EXEC, sending it a TERM signal if not specified otherwise +# +# Environment variables exposed: +# SERVICE_DAEMONIZE run EXEC in background +# SERVICE_WRITE_PID create a pid-file and use it for matching +# SERVICE_MATCH_EXEC use EXEC command-line for matching (default) +# SERVICE_MATCH_NAME use EXEC process name for matching +# SERVICE_USE_PID assume EXEC create its own pid-file and use it for matching +# SERVICE_NAME process name to use (default to EXEC file part) +# SERVICE_PID_FILE pid file to use (default to /var/run/$SERVICE_NAME.pid) +# SERVICE_SIG signal to send when using -K +# SERVICE_SIG_RELOAD default signal used when reloading +# SERVICE_SIG_STOP default signal used when stopping +# SERVICE_STOP_TIME time to wait for a process to stop gracefully before killing it +# SERVICE_UID user EXEC should be run as +# SERVICE_GID group EXEC should be run as +# +# SERVICE_DEBUG don't do anything, but show what would be done +# SERVICE_QUIET don't print anything +# + +SERVICE_QUIET=1 +SERVICE_SIG_RELOAD="HUP" +SERVICE_SIG_STOP="TERM" +SERVICE_STOP_TIME=5 +SERVICE_MATCH_EXEC=1 + +service() { + local ssd + local exec + local name + local start + ssd="${SERVICE_DEBUG:+echo }start-stop-daemon${SERVICE_QUIET:+ -q}" + case "$1" in + -C) + ssd="$ssd -K -t" + ;; + -S) + ssd="$ssd -S${SERVICE_DAEMONIZE:+ -b}${SERVICE_WRITE_PID:+ -m}" + start=1 + ;; + -K) + ssd="$ssd -K${SERVICE_SIG:+ -s $SERVICE_SIG}" + ;; + *) + echo "service: unknown ACTION '$1'" 1>&2 + return 1 + esac + shift + exec="$1" + [ -n "$exec" ] || { + echo "service: missing argument" 1>&2 + return 1 + } + [ -x "$exec" ] || { + echo "service: file '$exec' is not executable" 1>&2 + return 1 + } + name="${SERVICE_NAME:-${exec##*/}}" + [ -z "$SERVICE_USE_PID$SERVICE_WRITE_PID$SERVICE_PID_FILE" ] \ + || ssd="$ssd -p ${SERVICE_PID_FILE:-/var/run/$name.pid}" + [ -z "$SERVICE_MATCH_NAME" ] || ssd="$ssd -n $name" + ssd="$ssd${SERVICE_UID:+ -c $SERVICE_UID${SERVICE_GID:+:$SERVICE_GID}}" + [ -z "$SERVICE_MATCH_EXEC$start" ] || ssd="$ssd -x $exec" + shift + $ssd${1:+ -- "$@"} +} + +service_check() { + service -C "$@" +} + +service_signal() { + SERVICE_SIG="${SERVICE_SIG:-USR1}" service -K "$@" +} + +service_start() { + service -S "$@" +} + +service_stop() { + local try + SERVICE_SIG="${SERVICE_SIG:-$SERVICE_SIG_STOP}" service -K "$@" || return 1 + while [ $((try++)) -lt $SERVICE_STOP_TIME ]; do + service -C "$@" || return 0 + sleep 1 + done + SERVICE_SIG="KILL" service -K "$@" + sleep 1 + ! service -C "$@" +} + +service_reload() { + SERVICE_SIG="${SERVICE_SIG:-$SERVICE_SIG_RELOAD}" service -K "$@" +} + +service_kill() { + cat 1>&2 << __END_OF_WARNING__ +# +# WARNING: the 'service_kill' function is now deprecated and might be +# removed soon. Consider using the other new service_* wrappers instead. +# +__END_OF_WARNING__ + local name="${1}" + local pid="${2:-$(pidof "$name")}" + local grace="${3:-5}" + + [ -f "$pid" ] && pid="$(head -n1 "$pid" 2>/dev/null)" + + for pid in $pid; do + [ -d "/proc/$pid" ] || continue + local try=0 + kill -TERM $pid 2>/dev/null && \ + while grep -qs "$name" "/proc/$pid/cmdline" && [ $((try++)) -lt $grace ]; do sleep 1; done + kill -KILL $pid 2>/dev/null && \ + while grep -qs "$name" "/proc/$pid/cmdline"; do sleep 1; done + done +} + + + diff --git a/package/base-files/files/lib/functions/uci-defaults.sh b/package/base-files/files/lib/functions/uci-defaults.sh new file mode 100644 index 000000000..477c00cfd --- /dev/null +++ b/package/base-files/files/lib/functions/uci-defaults.sh @@ -0,0 +1,230 @@ +#!/bin/sh +# Copyright (C) 2011 OpenWrt.org + +UCIDEF_LEDS_CHANGED=0 + +ucidef_set_led_netdev() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local dev=$4 + + uci -q get system.$cfg && return 0 + + uci batch <<EOF +set system.$cfg='led' +set system.$cfg.name='$name' +set system.$cfg.sysfs='$sysfs' +set system.$cfg.trigger='netdev' +set system.$cfg.dev='$dev' +set system.$cfg.mode='link tx rx' +EOF + UCIDEF_LEDS_CHANGED=1 +} + +ucidef_set_led_usbdev() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local dev=$4 + + uci -q get system.$cfg && return 0 + + uci batch <<EOF +set system.$cfg='led' +set system.$cfg.name='$name' +set system.$cfg.sysfs='$sysfs' +set system.$cfg.trigger='usbdev' +set system.$cfg.dev='$dev' +set system.$cfg.interval='50' +EOF + UCIDEF_LEDS_CHANGED=1 +} + +ucidef_set_led_wlan() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local trigger=$4 + + uci -q get system.$cfg && return 0 + + uci batch <<EOF +set system.$cfg='led' +set system.$cfg.name='$name' +set system.$cfg.sysfs='$sysfs' +set system.$cfg.trigger='$trigger' +EOF + UCIDEF_LEDS_CHANGED=1 +} + +ucidef_set_led_switch() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local trigger=$4 + local port_mask=$5 + + uci -q get system.$cfg && return 0 + + uci batch <<EOF +set system.$cfg='led' +set system.$cfg.name='$name' +set system.$cfg.sysfs='$sysfs' +set system.$cfg.trigger='$trigger' +set system.$cfg.port_mask='$port_mask' +EOF + UCIDEF_LEDS_CHANGED=1 +} + +ucidef_set_led_default() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local default=$4 + + uci -q get system.$cfg && return 0 + + uci batch <<EOF +set system.$cfg='led' +set system.$cfg.name='$name' +set system.$cfg.sysfs='$sysfs' +set system.$cfg.default='$default' +EOF + UCIDEF_LEDS_CHANGED=1 +} + +ucidef_set_led_rssi() { + local cfg="led_$1" + local name=$2 + local sysfs=$3 + local iface=$4 + local minq=$5 + local maxq=$6 + local offset=$7 + local factor=$8 + + uci -q get system.$cfg && return 0 + + uci batch <<EOF +set system.$cfg='led' +set system.$cfg.name='$name' +set system.$cfg.sysfs='$sysfs' +set system.$cfg.trigger='rssi' +set system.$cfg.iface='rssid_$iface' +set system.$cfg.minq='$minq' +set system.$cfg.maxq='$maxq' +set system.$cfg.offset='$offset' +set system.$cfg.factor='$factor' +EOF + UCIDEF_LEDS_CHANGED=1 +} + +ucidef_set_rssimon() { + local dev="$1" + local refresh="$2" + local threshold="$3" + + local cfg="rssid_$dev" + + uci -q get system.$cfg && return 0 + + uci batch <<EOF +set system.$cfg='rssid' +set system.$cfg.dev='$dev' +set system.$cfg.refresh='$refresh' +set system.$cfg.threshold='$threshold' +EOF + UCIDEF_LEDS_CHANGED=1 +} + +ucidef_commit_leds() +{ + [ "$UCIDEF_LEDS_CHANGED" == "1" ] && uci commit system +} + +ucidef_set_interface_loopback() { + uci batch <<EOF +set network.loopback='interface' +set network.loopback.ifname='lo' +set network.loopback.proto='static' +set network.loopback.ipaddr='127.0.0.1' +set network.loopback.netmask='255.0.0.0' +EOF +} + +ucidef_set_interface_raw() { + local cfg=$1 + local ifname=$2 + + uci batch <<EOF +set network.$cfg='interface' +set network.$cfg.ifname='$ifname' +set network.$cfg.proto='none' +EOF +} + +ucidef_set_interface_lan() { + local ifname=$1 + + uci batch <<EOF +set network.lan='interface' +set network.lan.ifname='$ifname' +set network.lan.type='bridge' +set network.lan.proto='static' +set network.lan.ipaddr='192.168.1.1' +set network.lan.netmask='255.255.255.0' +EOF +} + +ucidef_set_interface_wan() { + local ifname=$1 + + uci batch <<EOF +set network.wan='interface' +set network.wan.ifname='$ifname' +set network.wan.proto='dhcp' +EOF +} + +ucidef_set_interfaces_lan_wan() { + local lan_ifname=$1 + local wan_ifname=$2 + + ucidef_set_interface_lan "$lan_ifname" + ucidef_set_interface_wan "$wan_ifname" +} + +ucidef_set_interface_macaddr() { + local ifname=$1 + local mac=$2 + + uci batch <<EOF +set network.$ifname.macaddr='$mac' +EOF +} + +ucidef_add_switch() { + local name=$1 + local reset=$2 + local enable=$3 + uci batch <<EOF +add network switch +set network.@switch[-1].name='$name' +set network.@switch[-1].reset='$reset' +set network.@switch[-1].enable_vlan='$enable' +EOF +} + +ucidef_add_switch_vlan() { + local device=$1 + local vlan=$2 + local ports=$3 + uci batch <<EOF +add network switch_vlan +set network.@switch_vlan[-1].device='$device' +set network.@switch_vlan[-1].vlan='$vlan' +set network.@switch_vlan[-1].ports='$ports' +EOF +} + diff --git a/package/base-files/files/lib/preinit/02_default_set_state b/package/base-files/files/lib/preinit/02_default_set_state new file mode 100644 index 000000000..df4339572 --- /dev/null +++ b/package/base-files/files/lib/preinit/02_default_set_state @@ -0,0 +1,7 @@ +#!/bin/sh + +define_default_set_state() { + . /etc/diag.sh +} + +boot_hook_add preinit_main define_default_set_state diff --git a/package/base-files/files/lib/preinit/05_mount_skip b/package/base-files/files/lib/preinit/05_mount_skip new file mode 100644 index 000000000..c2b7ee79d --- /dev/null +++ b/package/base-files/files/lib/preinit/05_mount_skip @@ -0,0 +1,13 @@ +#!/bin/sh + +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +check_skip() { + if [ "$pi_mount_skip_next" = "true" ]; then + return 0 + else + return 1 + fi +} + diff --git a/package/base-files/files/lib/preinit/10_check_for_mtd b/package/base-files/files/lib/preinit/10_check_for_mtd new file mode 100644 index 000000000..66f185e1a --- /dev/null +++ b/package/base-files/files/lib/preinit/10_check_for_mtd @@ -0,0 +1,20 @@ +#!/bin/sh + +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +mount_no_mtd() { + mtd unlock rootfs + mount -o remount,rw /dev/root / +} + +check_for_mtd() { + check_skip || { + grep -qs rootfs_data /proc/mtd || { + mount_no_mtd && pi_mount_skip_next=true + } + } +} + +boot_hook_add preinit_mount_root check_for_mtd + diff --git a/package/base-files/files/lib/preinit/10_essential_fs b/package/base-files/files/lib/preinit/10_essential_fs new file mode 100644 index 000000000..ab317dc20 --- /dev/null +++ b/package/base-files/files/lib/preinit/10_essential_fs @@ -0,0 +1,25 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +do_mount_procfs() { + mount proc /proc -t proc +} + +do_mount_sysfs() { + mount sysfs /sys -t sysfs +} + +calc_tmpfs_size() { + pi_size=$(awk '/MemTotal:/ {l=5242880;mt=($2*1024);print((s=mt/2)<l)&&(mt>l)?mt-l:s}' /proc/meminfo) +} + +do_mount_tmpfs() { + calc_tmpfs_size + mount tmpfs /tmp -t tmpfs -o size=$pi_size,nosuid,nodev,mode=1777 +} + +boot_hook_add preinit_essential do_mount_procfs +boot_hook_add preinit_essential do_mount_sysfs +boot_hook_add preinit_essential do_mount_tmpfs + diff --git a/package/base-files/files/lib/preinit/10_indicate_failsafe b/package/base-files/files/lib/preinit/10_indicate_failsafe new file mode 100644 index 000000000..7761fd3be --- /dev/null +++ b/package/base-files/files/lib/preinit/10_indicate_failsafe @@ -0,0 +1,18 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +# commands for emitting messages to network in failsafe mode + +indicate_failsafe_led () { + set_state failsafe +} + +indicate_failsafe() { + echo "- failsafe -" + preinit_net_echo "Entering Failsafe!\n" + indicate_failsafe_led +} + +boot_hook_add failsafe indicate_failsafe + diff --git a/package/base-files/files/lib/preinit/10_indicate_preinit b/package/base-files/files/lib/preinit/10_indicate_preinit new file mode 100644 index 000000000..ab6930518 --- /dev/null +++ b/package/base-files/files/lib/preinit/10_indicate_preinit @@ -0,0 +1,52 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +preinit_ip() { + # if the preinit interface isn't specified and ifname is set in + # preinit.arch use that interface + if [ -z "$pi_ifname" ]; then + pi_ifname=$ifname + fi + + [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && { + ifconfig $pi_ifname $pi_ip netmask $pi_netmask broadcast $pi_broadcast up + } +} + +preinit_ip_deconfig() { + [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && { + ifconfig $pi_ifname 0.0.0.0 down + } +} + +preinit_net_echo() { + [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && { + { + [ "$pi_preinit_net_messages" = "y" ] || { + [ "$pi_failsafe_net_message" = "true" ] && + [ "$pi_preinit_no_failsafe_netmsg" != "y" ] + } + + } && netmsg $pi_broadcast "$1" + } +} + +preinit_echo() { + preinit_net_echo $1 + echo $1 +} + +pi_indicate_led() { + set_state preinit +} + +pi_indicate_preinit() { + echo "- preinit -" + preinit_net_echo "Doing OpenWRT Preinit\n" + pi_indicate_led +} + +boot_hook_add preinit_main preinit_ip +boot_hook_add preinit_main pi_indicate_preinit + diff --git a/package/base-files/files/lib/preinit/20_check_jffs2_ready b/package/base-files/files/lib/preinit/20_check_jffs2_ready new file mode 100644 index 000000000..3c5cf67e8 --- /dev/null +++ b/package/base-files/files/lib/preinit/20_check_jffs2_ready @@ -0,0 +1,19 @@ +#!/bin/sh +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +mount_no_jffs2() { + echo "jffs2 not ready yet; using ramdisk" + ramoverlay +} + +check_for_jffs2() { + check_skip || { + jffs2_ready || { + mount_no_jffs2 && pi_mount_skip_next=true + } + } +} + +boot_hook_add preinit_mount_root check_for_jffs2 + diff --git a/package/base-files/files/lib/preinit/20_device_fs_mount b/package/base-files/files/lib/preinit/20_device_fs_mount new file mode 100644 index 000000000..832b1cf39 --- /dev/null +++ b/package/base-files/files/lib/preinit/20_device_fs_mount @@ -0,0 +1,37 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +do_move_devtmpfs() { + local mnt="$(grep devtmpfs /proc/mounts)" + mnt="${mnt#* }"; mnt="${mnt%% *}" + + [ "$mnt" = "/dev" ] || mount -o move "$mnt" /dev +} + +do_mount_devfs() { + mount devfs /dev -t devfs +} + +do_mount_hotplug() { + mount -t tmpfs tmpfs /dev -o mode=0755,size=512K +} + +do_mount_udev() { + mount -n -t tmpfs -o mode=0755 udev /dev +} + +choose_device_fs() { + if grep -q devtmpfs /proc/mounts; then + do_move_devtmpfs + elif grep -q devfs /proc/filesystems; then + do_mount_devfs + elif [ -x /sbin/hotplug2 ]; then + do_mount_hotplug + elif [ -x /sbin/udevd ]; then + do_mount_udev + fi +} + +boot_hook_add preinit_essential choose_device_fs + diff --git a/package/base-files/files/lib/preinit/30_device_fs_daemons b/package/base-files/files/lib/preinit/30_device_fs_daemons new file mode 100644 index 000000000..5b3e2ad00 --- /dev/null +++ b/package/base-files/files/lib/preinit/30_device_fs_daemons @@ -0,0 +1,36 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +init_devfs() { + HOTPLUG=/sbin/hotplug-call +} + +init_hotplug2() { + [ -c /dev/console ] || mknod /dev/console c 5 1 + /sbin/hotplug2 --set-worker /lib/hotplug2/worker_fork.so --set-rules-file /etc/hotplug2-init.rules --no-persistent --set-coldplug-cmd /sbin/udevtrigger + /sbin/hotplug2 --set-worker /lib/hotplug2/worker_fork.so --set-rules-file /etc/hotplug2-init.rules --persistent & +} + +init_udev() { + [ -d /lib/udev/devices ] && cp -af /lib/udev/devices/* /dev/ + [ -c /dev/console ] || mknod -m 0600 /dev/console c 5 1 + [ -c /dev/null ] || mknod -m 0666 /dev/null c 1 3 + /sbin/udevd --daemon --resolve-names=never + /sbin/udevadm trigger + /sbin/udevadm settle +} + +init_device_fs() { + HOTPLUG= + if grep -q devfs /proc/filesystems; then + init_devfs + elif [ -x /sbin/hotplug2 ]; then + init_hotplug2 + elif [ -x /sbin/udevd ]; then + init_udev + fi +} + +boot_hook_add preinit_essential init_device_fs + diff --git a/package/base-files/files/lib/preinit/30_failsafe_wait b/package/base-files/files/lib/preinit/30_failsafe_wait new file mode 100644 index 000000000..04dc57df2 --- /dev/null +++ b/package/base-files/files/lib/preinit/30_failsafe_wait @@ -0,0 +1,85 @@ +#!/bin/sh +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +fs_wait_for_key () { + local timeout=$3 + local timer + local do_failsafe + local keypress_true="$(mktemp)" + local keypress_wait="$(mktemp)" + local keypress_sec="$(mktemp)" + if [ -z "$keypress_wait" ]; then + keypress_wait=/tmp/.keypress_wait + touch $keypress_wait + fi + if [ -z "$keypress_true" ]; then + keypress_true=/tmp/.keypress_true + touch $keypress_true + fi + if [ -z "$keypress_sec" ]; then + keypress_sec=/tmp/.keypress_sec + touch $keypress_sec + fi + + trap "echo 'true' >$keypress_true; lock -u $keypress_wait ; rm -f $keypress_wait" INT + trap "echo 'true' >$keypress_true; lock -u $keypress_wait ; rm -f $keypress_wait" USR1 + + [ -n "$timeout" ] || timeout=1 + [ $timeout -ge 1 ] || timeout=1 + timer=$timeout + lock $keypress_wait + { + while [ $timer -gt 0 ]; do + echo "$timer" >$keypress_sec + timer=$(($timer - 1)) + sleep 1 + done + lock -u $keypress_wait + rm -f $keypress_wait + } & + + echo "Press the [$1] key and hit [enter] $2" + # if we're on the console we wait for input + { + while [ -r $keypress_wait ]; do + timer="$(cat $keypress_sec)" + + [ -n "$timer" ] || timer=1 + timer="${timer%%\ *}" + [ $timer -ge 1 ] || timer=1 + do_failsafe="" + { + read -t "$timer" do_failsafe + if [ "$do_failsafe" = "$1" ]; then + echo "true" >$keypress_true + lock -u $keypress_wait + rm -f $keypress_wait + fi + } + done + } + lock -w $keypress_wait + + trap - INT + trap - USR1 + + keypressed=1 + [ "$(cat $keypress_true)" = "true" ] && keypressed=0 + rm -f $keypress_true + rm -f $keypress_wait + rm -f $keypress_sec + + return $keypressed +} + +failsafe_wait() { + FAILSAFE= + pi_failsafe_net_message=true + preinit_net_echo "Please press button now to enter failsafe" + pi_failsafe_net_message=false + fs_wait_for_key f 'to enter failsafe mode' $fs_failsafe_wait_timeout && FAILSAFE=true && export FAILSAFE +} + +boot_hook_add preinit_main failsafe_wait + diff --git a/package/base-files/files/lib/preinit/40_init_shm b/package/base-files/files/lib/preinit/40_init_shm new file mode 100644 index 000000000..8971116a4 --- /dev/null +++ b/package/base-files/files/lib/preinit/40_init_shm @@ -0,0 +1,10 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +init_shm() { + [ -d /dev/shm ] || mkdir -p /dev/shm +} + +boot_hook_add preinit_essential init_shm + diff --git a/package/base-files/files/lib/preinit/40_mount_devpts b/package/base-files/files/lib/preinit/40_mount_devpts new file mode 100644 index 000000000..2d5010b91 --- /dev/null +++ b/package/base-files/files/lib/preinit/40_mount_devpts @@ -0,0 +1,15 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +init_devpts() { + [ -d /dev/pts ] || mkdir -p /dev/pts +} + +do_mount_devpts() { + mount devpts /dev/pts -t devpts +} + +boot_hook_add preinit_essential init_devpts +boot_hook_add preinit_essential do_mount_devpts + diff --git a/package/base-files/files/lib/preinit/40_mount_jffs2 b/package/base-files/files/lib/preinit/40_mount_jffs2 new file mode 100644 index 000000000..00f45a890 --- /dev/null +++ b/package/base-files/files/lib/preinit/40_mount_jffs2 @@ -0,0 +1,26 @@ +#!/bin/sh +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +find_mount_jffs2() { + mkdir -p /tmp/overlay + mount "$(find_mtd_part rootfs_data)" /tmp/overlay -t jffs2 + mtd -qq unlock rootfs_data +} + +jffs2_not_mounted() { + if [ "$pi_jffs2_mount_success" != "true" ]; then + return 0 + else + return 1 + fi +} + +do_mount_jffs2() { + check_skip || { + find_mount_jffs2 && pi_jffs2_mount_success=true + } +} + +boot_hook_add preinit_mount_root do_mount_jffs2 + diff --git a/package/base-files/files/lib/preinit/40_run_failsafe_hook b/package/base-files/files/lib/preinit/40_run_failsafe_hook new file mode 100644 index 000000000..faa043c17 --- /dev/null +++ b/package/base-files/files/lib/preinit/40_run_failsafe_hook @@ -0,0 +1,13 @@ +#!/bin/sh +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +run_failsafe_hook() { + if [ "$FAILSAFE" = "true" ]; then + boot_run_hook failsafe + lock -w /tmp/.failsafe + fi +} + +boot_hook_add preinit_main run_failsafe_hook + diff --git a/package/base-files/files/lib/preinit/41_merge_overlay_hooks b/package/base-files/files/lib/preinit/41_merge_overlay_hooks new file mode 100644 index 000000000..4ca6877be --- /dev/null +++ b/package/base-files/files/lib/preinit/41_merge_overlay_hooks @@ -0,0 +1,24 @@ +#!/bin/sh +# Copyright (C) 2010 OpenWrt.org + +merge_overlay_hooks() { + jffs2_not_mounted || [ ! -d /tmp/overlay/lib/preinit ] || { + echo "- merge overlay components -" + + mkdir -p /tmp/preinit-hook-merge + ln -sf /lib/preinit/* /tmp/overlay/lib/preinit/[0-9][0-9]_* /tmp/preinit-hook-merge/ + + boot_hook_splice_start + + local pipart + for pipart in /tmp/preinit-hook-merge/*; do + . $pipart + done + + boot_hook_splice_finish + + rm -rf /tmp/preinit-hook-merge + } +} + +boot_hook_add preinit_mount_root merge_overlay_hooks diff --git a/package/base-files/files/lib/preinit/50_choose_console b/package/base-files/files/lib/preinit/50_choose_console new file mode 100644 index 000000000..ecbc2eaf4 --- /dev/null +++ b/package/base-files/files/lib/preinit/50_choose_console @@ -0,0 +1,31 @@ +#!/bin/sh +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +choose_console() { + # the shell really doesn't like having stdin/out closed + # that's why we use /dev/pty/m0 and m1 (or equivalent) as replacement + # for /dev/console if there's no serial console available + + if grep -q devfs /proc/filesystems; then + M0=/dev/pty/m0 + M1=/dev/pty/m1 + M2=/dev/pty/m1 + elif [ -x /sbin/hotplug2 ]; then + M0=/dev/ptmx + M1=/dev/ptmx + M2=/dev/ptmx + elif [ -x /sbin/udevd ]; then + M0=/dev/pty/ptmx + M1=/dev/pty/ptmx + M2=/dev/pty/ptmx + fi + dd if=/dev/console of=/dev/null bs=1 count=0 >/dev/null 2>/dev/null && { + M0=/dev/console + M1=/dev/console + M2=/dev/console + } +} + +boot_hook_add preinit_essential choose_console + diff --git a/package/base-files/files/lib/preinit/50_indicate_regular_preinit b/package/base-files/files/lib/preinit/50_indicate_regular_preinit new file mode 100644 index 000000000..9cc01f219 --- /dev/null +++ b/package/base-files/files/lib/preinit/50_indicate_regular_preinit @@ -0,0 +1,12 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +indicate_regular_preinit() { + echo "- regular preinit -" + preinit_net_echo "Continuing with Regular Preinit\n" + pi_indicate_led +} + +boot_hook_add preinit_main indicate_regular_preinit + diff --git a/package/base-files/files/lib/preinit/60_init_hotplug b/package/base-files/files/lib/preinit/60_init_hotplug new file mode 100644 index 000000000..dc05e0ed0 --- /dev/null +++ b/package/base-files/files/lib/preinit/60_init_hotplug @@ -0,0 +1,10 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +init_hotplug() { + echo "$HOTPLUG" > /proc/sys/kernel/hotplug +} + +boot_hook_add preinit_main init_hotplug + diff --git a/package/base-files/files/lib/preinit/70_initramfs_test b/package/base-files/files/lib/preinit/70_initramfs_test new file mode 100644 index 000000000..15c0dcd90 --- /dev/null +++ b/package/base-files/files/lib/preinit/70_initramfs_test @@ -0,0 +1,14 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +initramfs_test() { + if [ -n "$INITRAMFS" ]; then + boot_run_hook initramfs + preinit_ip_deconfig + break + fi +} + +boot_hook_add preinit_main initramfs_test + diff --git a/package/base-files/files/lib/preinit/70_pivot_jffs2_root b/package/base-files/files/lib/preinit/70_pivot_jffs2_root new file mode 100644 index 000000000..821bfb14f --- /dev/null +++ b/package/base-files/files/lib/preinit/70_pivot_jffs2_root @@ -0,0 +1,14 @@ +#!/bin/sh +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +rootfs_pivot() { + check_skip || jffs2_not_mounted || { + echo "switching to jffs2" + mount -o move /tmp/overlay /overlay 2>&- + fopivot /overlay /rom && pi_mount_skip_next=true + } +} + +boot_hook_add preinit_mount_root rootfs_pivot + diff --git a/package/base-files/files/lib/preinit/80_mount_root b/package/base-files/files/lib/preinit/80_mount_root new file mode 100644 index 000000000..cc8781687 --- /dev/null +++ b/package/base-files/files/lib/preinit/80_mount_root @@ -0,0 +1,10 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +do_mount_root() { + boot_run_hook preinit_mount_root +} + +boot_hook_add preinit_main do_mount_root + diff --git a/package/base-files/files/lib/preinit/90_init_console b/package/base-files/files/lib/preinit/90_init_console new file mode 100644 index 000000000..ca05755fd --- /dev/null +++ b/package/base-files/files/lib/preinit/90_init_console @@ -0,0 +1,14 @@ +#!/bin/sh +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +init_console() { + if [ "$pi_suppress_stderr" = "y" ]; then + exec <$M0 >$M1 2>&0 + else + exec <$M0 >$M1 2>$M2 + fi +} + +boot_hook_add preinit_essential init_console + diff --git a/package/base-files/files/lib/preinit/90_mount_no_jffs2 b/package/base-files/files/lib/preinit/90_mount_no_jffs2 new file mode 100644 index 000000000..d8ad4ae11 --- /dev/null +++ b/package/base-files/files/lib/preinit/90_mount_no_jffs2 @@ -0,0 +1,12 @@ +#!/bin/sh +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +do_mount_no_jffs2() { + check_skip || { + mount_no_jffs2 && pi_mount_skip_next=true + } +} + +boot_hook_add preinit_mount_root do_mount_no_jffs2 + diff --git a/package/base-files/files/lib/preinit/90_restore_config b/package/base-files/files/lib/preinit/90_restore_config new file mode 100644 index 000000000..210bf6184 --- /dev/null +++ b/package/base-files/files/lib/preinit/90_restore_config @@ -0,0 +1,17 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +restore_config() { + [ -f /sysupgrade.tgz ] && { + echo "- config restore -" + cd / + mv sysupgrade.tgz /tmp + tar xzf /tmp/sysupgrade.tgz + rm -f /tmp/sysupgrade.tgz + sync + } +} + +boot_hook_add preinit_main restore_config + diff --git a/package/base-files/files/lib/preinit/99_10_failsafe_login b/package/base-files/files/lib/preinit/99_10_failsafe_login new file mode 100644 index 000000000..05d6b7787 --- /dev/null +++ b/package/base-files/files/lib/preinit/99_10_failsafe_login @@ -0,0 +1,18 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +failsafe_netlogin () { + telnetd -l /bin/login.sh <> /dev/null 2>&1 +} + +failsafe_shell() { + lock /tmp/.failsafe + ash --login + echo "Please reboot system when done with failsafe network logins" +} + + +boot_hook_add failsafe failsafe_netlogin +boot_hook_add failsafe failsafe_shell + diff --git a/package/base-files/files/lib/preinit/99_10_mount_no_mtd b/package/base-files/files/lib/preinit/99_10_mount_no_mtd new file mode 100644 index 000000000..c4f38e415 --- /dev/null +++ b/package/base-files/files/lib/preinit/99_10_mount_no_mtd @@ -0,0 +1,12 @@ +#!/bin/sh +# Copyright (C) 2006-2010 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +do_mount_no_mtd() { + check_skip || { + mount_no_mtd + } +} + +boot_hook_add preinit_mount_root do_mount_no_mtd + diff --git a/package/base-files/files/lib/preinit/99_10_run_init b/package/base-files/files/lib/preinit/99_10_run_init new file mode 100644 index 000000000..fef3a503e --- /dev/null +++ b/package/base-files/files/lib/preinit/99_10_run_init @@ -0,0 +1,16 @@ +#!/bin/sh +# Copyright (C) 2006 OpenWrt.org +# Copyright (C) 2010 Vertical Communications + +run_init() { + preinit_echo "- init -" + preinit_ip_deconfig + if [ "$pi_init_suppress_stderr" = "y" ]; then + exec env - PATH=$pi_init_path $pi_init_env $pi_init_cmd 2>&0 + else + exec env - PATH=$pi_init_path $pi_init_env $pi_init_cmd + fi +} + +boot_hook_add preinit_main run_init + diff --git a/package/base-files/files/lib/upgrade/common.sh b/package/base-files/files/lib/upgrade/common.sh new file mode 100644 index 000000000..e6de34849 --- /dev/null +++ b/package/base-files/files/lib/upgrade/common.sh @@ -0,0 +1,230 @@ +#!/bin/sh + +RAM_ROOT=/tmp/root + +ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; } +libs() { ldd $* | awk '{print $3}'; } + +install_file() { # <file> [ <file> ... ] + for file in "$@"; do + dest="$RAM_ROOT/$file" + [ -f $file -a ! -f $dest ] && { + dir="$(dirname $dest)" + mkdir -p "$dir" + cp $file $dest + } + done +} + +install_bin() { # <file> [ <symlink> ... ] + src=$1 + files=$1 + [ -x "$src" ] && files="$src $(libs $src)" + install_file $files + [ -e /lib/ld-linux.so.3 ] && { + install_file /lib/ld-linux.so.3 + } + shift + for link in "$@"; do { + dest="$RAM_ROOT/$link" + dir="$(dirname $dest)" + mkdir -p "$dir" + [ -f "$dest" ] || ln -s $src $dest + }; done +} + +pivot() { # <new_root> <old_root> + mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1 + mkdir -p $1$2 $1/proc $1/sys $1/dev $1/tmp $1/overlay && \ + mount -o move /proc $1/proc && \ + pivot_root $1 $1$2 || { + umount $1 $1 + return 1 + } + + mount -o move $2/sys /sys + mount -o move $2/dev /dev + mount -o move $2/tmp /tmp + mount -o move $2/overlay /overlay 2>&- + return 0 +} + +run_ramfs() { # <command> [...] + install_bin /bin/busybox /bin/ash /bin/sh /bin/mount /bin/umount \ + /sbin/pivot_root /usr/bin/wget /sbin/reboot /bin/sync /bin/dd \ + /bin/grep /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" \ + /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump \ + /bin/sleep /bin/zcat /usr/bin/bzcat /usr/bin/printf /usr/bin/wc + + install_bin /sbin/mtd + for file in $RAMFS_COPY_BIN; do + install_bin $file + done + install_file /etc/resolv.conf /lib/functions.sh /lib/functions.sh /lib/upgrade/*.sh $RAMFS_COPY_DATA + + pivot $RAM_ROOT /mnt || { + echo "Failed to switch over to ramfs. Please reboot." + exit 1 + } + + mount -o remount,ro /mnt + umount -l /mnt + + grep /overlay /proc/mounts > /dev/null && { + mount -o remount,ro /overlay + umount -l /overlay + } + + # spawn a new shell from ramdisk to reduce the probability of cache issues + exec /bin/busybox ash -c "$*" +} + +kill_remaining() { # [ <signal> ] + local sig="${1:-TERM}" + echo -n "Sending $sig to remaining processes ... " + + local stat + for stat in /proc/[0-9]*/stat; do + [ -f "$stat" ] || continue + + local pid name state ppid rest + read pid name state ppid rest < $stat + name="${name#(}"; name="${name%)}" + + local cmdline + read cmdline < /proc/$pid/cmdline + + # Skip kernel threads + [ -n "$cmdline" ] || continue + + case "$name" in + # Skip essential services + *ash*|*init*|*watchdog*|*ssh*|*dropbear*|*telnet*|*login*|*hostapd*|*wpa_supplicant*) : ;; + + # Killable process + *) + if [ $pid -ne $$ ] && [ $ppid -ne $$ ]; then + echo -n "$name " + kill -$sig $pid 2>/dev/null + fi + ;; + esac + done + echo "" +} + +run_hooks() { + local arg="$1"; shift + for func in "$@"; do + eval "$func $arg" + done +} + +ask_bool() { + local default="$1"; shift; + local answer="$default" + + [ "$INTERACTIVE" -eq 1 ] && { + case "$default" in + 0) echo -n "$* (y/N): ";; + *) echo -n "$* (Y/n): ";; + esac + read answer + case "$answer" in + y*) answer=1;; + n*) answer=0;; + *) answer="$default";; + esac + } + [ "$answer" -gt 0 ] +} + +v() { + [ "$VERBOSE" -ge 1 ] && echo "$@" +} + +rootfs_type() { + mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }' +} + +get_image() { # <source> [ <command> ] + local from="$1" + local conc="$2" + local cmd + + case "$from" in + http://*|ftp://*) cmd="wget -O- -q";; + *) cmd="cat";; + esac + if [ -z "$conc" ]; then + local magic="$(eval $cmd $from | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')" + case "$magic" in + 1f8b) conc="zcat";; + 425a) conc="bzcat";; + esac + fi + + eval "$cmd $from ${conc:+| $conc}" +} + +get_magic_word() { + get_image "$@" | dd bs=2 count=1 2>/dev/null | hexdump -v -n 2 -e '1/1 "%02x"' +} + +get_magic_long() { + get_image "$@" | dd bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"' +} + +refresh_mtd_partitions() { + mtd refresh rootfs +} + +jffs2_copy_config() { + if grep rootfs_data /proc/mtd >/dev/null; then + # squashfs+jffs2 + mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data + else + # jffs2 + mtd jffs2write "$CONF_TAR" rootfs + fi +} + +default_do_upgrade() { + sync + if [ "$SAVE_CONFIG" -eq 1 -a -z "$USE_REFRESH" ]; then + get_image "$1" | mtd -j "$CONF_TAR" write - "${PART_NAME:-image}" + else + get_image "$1" | mtd write - "${PART_NAME:-image}" + fi +} + +do_upgrade() { + v "Performing system upgrade..." + if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then + platform_do_upgrade "$ARGV" + else + default_do_upgrade "$ARGV" + fi + + [ "$SAVE_CONFIG" -eq 1 -a -n "$USE_REFRESH" ] && { + v "Refreshing partitions" + if type 'platform_refresh_partitions' >/dev/null 2>/dev/null; then + platform_refresh_partitions + else + refresh_mtd_partitions + fi + if type 'platform_copy_config' >/dev/null 2>/dev/null; then + platform_copy_config + else + jffs2_copy_config + fi + } + v "Upgrade completed" + [ -n "$DELAY" ] && sleep "$DELAY" + ask_bool 1 "Reboot" && { + v "Rebooting system..." + reboot -f + sleep 5 + echo b 2>/dev/null >/proc/sysrq-trigger + } +} diff --git a/package/base-files/files/lib/upgrade/keep.d/base-files-essential b/package/base-files/files/lib/upgrade/keep.d/base-files-essential new file mode 100644 index 000000000..2c611d29b --- /dev/null +++ b/package/base-files/files/lib/upgrade/keep.d/base-files-essential @@ -0,0 +1,9 @@ +# Essential files that will be always kept +/etc/hosts +/etc/inittab +/etc/group +/etc/passwd +/etc/profile +/etc/shells +/etc/sysctl.conf +/etc/rc.local |