Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # Copyright (C) 2011 O.S. Systems Software LTDA. |
| 3 | # Licensed on MIT |
| 4 | # |
| 5 | # Provides the API to be used by the initramfs modules |
| 6 | # |
| 7 | # Modules need to provide the following functions: |
| 8 | # |
| 9 | # <module>_enabled : check if the module ought to run (return 1 to skip) |
| 10 | # <module>_run : do what is need |
| 11 | # |
| 12 | # Boot parameters are available on environment in the as: |
| 13 | # |
| 14 | # 'foo=value' as 'bootparam_foo=value' |
| 15 | # 'foo' as 'bootparam_foo=true' |
| 16 | # 'foo.bar[=value] as 'foo_bar=[value|true]' |
| 17 | |
| 18 | # Register a function to be called before running a module |
| 19 | # The hook is called as: |
| 20 | # <function> pre <module> |
| 21 | add_module_pre_hook() { |
| 22 | MODULE_PRE_HOOKS="$MODULE_PRE_HOOKS $1" |
| 23 | } |
| 24 | |
| 25 | # Register a function to be called after running a module |
| 26 | # The hook is called as: |
| 27 | # <function> post <module> |
| 28 | add_module_post_hook() { |
| 29 | MODULE_POST_HOOKS="$MODULE_POST_HOOKS $1" |
| 30 | } |
| 31 | |
| 32 | # Load kernel module |
| 33 | load_kernel_module() { |
| 34 | if modprobe $1 >/dev/null 2>&1; then |
| 35 | info "Loaded module $1" |
| 36 | else |
| 37 | debug "Failed to load module $1" |
| 38 | fi |
| 39 | } |
| 40 | |
| 41 | # Prints information |
| 42 | msg() { |
| 43 | echo "$@" >/dev/console |
| 44 | } |
| 45 | |
| 46 | # Prints information if verbose bootparam is used |
| 47 | info() { |
| 48 | [ -n "$bootparam_verbose" ] && echo "$@" >/dev/console |
| 49 | } |
| 50 | |
| 51 | # Prints information if debug bootparam is used |
| 52 | debug() { |
| 53 | [ -n "$bootparam_debug" ] && echo "DEBUG: $@" >/dev/console |
| 54 | } |
| 55 | |
| 56 | # Prints a message and start a endless loop |
| 57 | fatal() { |
| 58 | echo $1 >/dev/console |
| 59 | echo >/dev/console |
| 60 | |
| 61 | if [ -n "bootparam_init_fatal_sh" ]; then |
| 62 | sh |
| 63 | else |
| 64 | while [ "true" ]; do |
| 65 | sleep 3600 |
| 66 | done |
| 67 | fi |
| 68 | } |
| 69 | |
| 70 | # Variables shared amoung modules |
| 71 | ROOTFS_DIR="/rootfs" # where to do the switch root |
| 72 | MODULE_PRE_HOOKS="" # functions to call before running each module |
| 73 | MODULE_POST_HOOKS="" # functions to call after running each module |
| 74 | MODULES_DIR=/init.d # place to look for modules |
| 75 | |
| 76 | # make mount stop complaining about missing /etc/fstab |
| 77 | touch /etc/fstab |
| 78 | |
| 79 | # initialize /proc, /sys and /var/lock |
| 80 | mkdir -p /proc /sys /var/lock |
| 81 | mount -t proc proc /proc |
| 82 | mount -t sysfs sysfs /sys |
| 83 | |
| 84 | # populate bootparam environment |
| 85 | for p in `cat /proc/cmdline`; do |
| 86 | opt=`echo $p | cut -d'=' -f1` |
| 87 | opt=`echo $opt | tr '.-' '__'` |
| 88 | if [ "`echo $p | cut -d'=' -f1`" = "$p" ]; then |
| 89 | eval "bootparam_${opt}=true" |
| 90 | else |
| 91 | value="`echo $p | cut -d'=' -f2-`" |
| 92 | eval "bootparam_${opt}=\"${value}\"" |
| 93 | fi |
| 94 | done |
| 95 | |
| 96 | # use /dev with devtmpfs |
| 97 | if grep -q devtmpfs /proc/filesystems; then |
| 98 | mkdir -p /dev |
| 99 | mount -t devtmpfs devtmpfs /dev |
| 100 | else |
| 101 | if [ ! -d /dev ]; then |
| 102 | fatal "ERROR: /dev doesn't exist and kernel doesn't has devtmpfs enabled." |
| 103 | fi |
| 104 | fi |
| 105 | |
| 106 | mkdir $ROOTFS_DIR |
| 107 | |
| 108 | # Load and run modules |
| 109 | for m in $MODULES_DIR/*; do |
| 110 | # Skip backup files |
| 111 | if [ "`echo $m | sed -e 's/\~$//'`" != "$m" ]; then |
| 112 | continue |
| 113 | fi |
| 114 | |
| 115 | module=`basename $m | cut -d'-' -f 2` |
| 116 | debug "Loading module $module" |
| 117 | |
| 118 | # pre hooks |
| 119 | for h in $MODULE_PRE_HOOKS; do |
| 120 | debug "Calling module hook (pre): $h" |
| 121 | eval "$h pre $module" |
| 122 | debug "Finished module hook (pre): $h" |
| 123 | done |
| 124 | |
| 125 | # process module |
| 126 | . $m |
| 127 | |
| 128 | if ! eval "${module}_enabled"; then |
| 129 | debug "Skipping module $module" |
| 130 | continue |
| 131 | fi |
| 132 | |
| 133 | debug "Running ${module}_run" |
| 134 | eval "${module}_run" |
| 135 | |
| 136 | # post hooks |
| 137 | for h in $MODULE_POST_HOOKS; do |
| 138 | debug "Calling module hook (post): $h" |
| 139 | eval "$h post $module" |
| 140 | debug "Finished module hook (post): $h" |
| 141 | done |
| 142 | done |
| 143 | |
| 144 | # Catch all |
| 145 | fatal "ERROR: Initramfs failed to initialize the system." |