Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | ### BEGIN INIT INFO |
| 3 | # Provides: volatile |
| 4 | # Required-Start: $local_fs |
| 5 | # Required-Stop: $local_fs |
| 6 | # Default-Start: S |
| 7 | # Default-Stop: |
| 8 | # Short-Description: Populate the volatile filesystem |
| 9 | ### END INIT INFO |
| 10 | |
| 11 | # Get ROOT_DIR |
| 12 | DIRNAME=`dirname $0` |
| 13 | ROOT_DIR=`echo $DIRNAME | sed -ne 's:/etc/.*::p'` |
| 14 | |
| 15 | [ -e ${ROOT_DIR}/etc/default/rcS ] && . ${ROOT_DIR}/etc/default/rcS |
| 16 | # When running populate-volatile.sh at rootfs time, disable cache. |
| 17 | [ -n "$ROOT_DIR" ] && VOLATILE_ENABLE_CACHE=no |
| 18 | # If rootfs is read-only, disable cache. |
| 19 | [ "$ROOTFS_READ_ONLY" = "yes" ] && VOLATILE_ENABLE_CACHE=no |
| 20 | |
| 21 | CFGDIR="${ROOT_DIR}/etc/default/volatiles" |
| 22 | TMPROOT="${ROOT_DIR}/var/volatile/tmp" |
| 23 | COREDEF="00_core" |
| 24 | |
| 25 | [ "${VERBOSE}" != "no" ] && echo "Populating volatile Filesystems." |
| 26 | |
| 27 | create_file() { |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 28 | EXEC="" |
| 29 | [ -z "$2" ] && { |
| 30 | EXEC=" |
| 31 | touch \"$1\"; |
| 32 | " |
| 33 | } || { |
| 34 | EXEC=" |
| 35 | cp \"$2\" \"$1\"; |
| 36 | " |
| 37 | } |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | EXEC=" |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 39 | ${EXEC} |
Brad Bishop | c4ea075 | 2018-11-15 14:30:15 -0800 | [diff] [blame] | 40 | chown ${TUSER}:${TGROUP} $1 || echo \"Failed to set owner -${TUSER}- for -$1-.\" >/dev/tty0 2>&1; |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | chmod ${TMODE} $1 || echo \"Failed to set mode -${TMODE}- for -$1-.\" >/dev/tty0 2>&1 " |
| 42 | |
| 43 | test "$VOLATILE_ENABLE_CACHE" = yes && echo "$EXEC" >> /etc/volatile.cache.build |
| 44 | |
| 45 | [ -e "$1" ] && { |
| 46 | [ "${VERBOSE}" != "no" ] && echo "Target already exists. Skipping." |
| 47 | } || { |
| 48 | if [ -z "$ROOT_DIR" ]; then |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 49 | eval $EXEC |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 50 | else |
| 51 | # Creating some files at rootfs time may fail and should fail, |
| 52 | # but these failures should not be logged to make sure the do_rootfs |
| 53 | # process doesn't fail. This does no harm, as this script will |
| 54 | # run on target to set up the correct files and directories. |
| 55 | eval $EXEC > /dev/null 2>&1 |
| 56 | fi |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | mk_dir() { |
| 61 | EXEC=" |
| 62 | mkdir -p \"$1\"; |
Brad Bishop | c4ea075 | 2018-11-15 14:30:15 -0800 | [diff] [blame] | 63 | chown ${TUSER}:${TGROUP} $1 || echo \"Failed to set owner -${TUSER}- for -$1-.\" >/dev/tty0 2>&1; |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 | chmod ${TMODE} $1 || echo \"Failed to set mode -${TMODE}- for -$1-.\" >/dev/tty0 2>&1 " |
| 65 | |
| 66 | test "$VOLATILE_ENABLE_CACHE" = yes && echo "$EXEC" >> /etc/volatile.cache.build |
| 67 | [ -e "$1" ] && { |
| 68 | [ "${VERBOSE}" != "no" ] && echo "Target already exists. Skipping." |
| 69 | } || { |
| 70 | if [ -z "$ROOT_DIR" ]; then |
| 71 | eval $EXEC |
| 72 | else |
| 73 | # For the same reason with create_file(), failures should |
| 74 | # not be logged. |
| 75 | eval $EXEC > /dev/null 2>&1 |
| 76 | fi |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | link_file() { |
| 81 | EXEC=" |
| 82 | if [ -L \"$2\" ]; then |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 83 | [ \"\$(readlink -f \"$2\")\" != \"$1\" ] && { rm -f \"$2\"; ln -sf \"$1\" \"$2\"; }; |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 84 | elif [ -d \"$2\" ]; then |
| 85 | if awk '\$2 == \"$2\" {exit 1}' /proc/mounts; then |
| 86 | cp -a $2/* $1 2>/dev/null; |
| 87 | cp -a $2/.[!.]* $1 2>/dev/null; |
| 88 | rm -rf \"$2\"; |
| 89 | ln -sf \"$1\" \"$2\"; |
| 90 | fi |
| 91 | else |
| 92 | ln -sf \"$1\" \"$2\"; |
| 93 | fi |
| 94 | " |
| 95 | |
| 96 | test "$VOLATILE_ENABLE_CACHE" = yes && echo " $EXEC" >> /etc/volatile.cache.build |
| 97 | |
| 98 | if [ -z "$ROOT_DIR" ]; then |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 99 | eval $EXEC |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | else |
| 101 | # For the same reason with create_file(), failures should |
| 102 | # not be logged. |
| 103 | eval $EXEC > /dev/null 2>&1 |
| 104 | fi |
| 105 | } |
| 106 | |
| 107 | check_requirements() { |
| 108 | cleanup() { |
| 109 | rm "${TMP_INTERMED}" |
| 110 | rm "${TMP_DEFINED}" |
| 111 | rm "${TMP_COMBINED}" |
| 112 | } |
| 113 | |
| 114 | CFGFILE="$1" |
| 115 | [ `basename "${CFGFILE}"` = "${COREDEF}" ] && return 0 |
| 116 | |
| 117 | TMP_INTERMED="${TMPROOT}/tmp.$$" |
| 118 | TMP_DEFINED="${TMPROOT}/tmpdefined.$$" |
| 119 | TMP_COMBINED="${TMPROOT}/tmpcombined.$$" |
| 120 | |
| 121 | sed 's@\(^:\)*:.*@\1@' ${ROOT_DIR}/etc/passwd | sort | uniq > "${TMP_DEFINED}" |
| 122 | cat ${CFGFILE} | grep -v "^#" | cut -s -d " " -f 2 > "${TMP_INTERMED}" |
| 123 | cat "${TMP_DEFINED}" "${TMP_INTERMED}" | sort | uniq > "${TMP_COMBINED}" |
| 124 | NR_DEFINED_USERS="`cat "${TMP_DEFINED}" | wc -l`" |
| 125 | NR_COMBINED_USERS="`cat "${TMP_COMBINED}" | wc -l`" |
| 126 | |
| 127 | [ "${NR_DEFINED_USERS}" -ne "${NR_COMBINED_USERS}" ] && { |
| 128 | echo "Undefined users:" |
| 129 | diff "${TMP_DEFINED}" "${TMP_COMBINED}" | grep "^>" |
| 130 | cleanup |
| 131 | return 1 |
| 132 | } |
| 133 | |
| 134 | |
| 135 | sed 's@\(^:\)*:.*@\1@' ${ROOT_DIR}/etc/group | sort | uniq > "${TMP_DEFINED}" |
| 136 | cat ${CFGFILE} | grep -v "^#" | cut -s -d " " -f 3 > "${TMP_INTERMED}" |
| 137 | cat "${TMP_DEFINED}" "${TMP_INTERMED}" | sort | uniq > "${TMP_COMBINED}" |
| 138 | |
| 139 | NR_DEFINED_GROUPS="`cat "${TMP_DEFINED}" | wc -l`" |
| 140 | NR_COMBINED_GROUPS="`cat "${TMP_COMBINED}" | wc -l`" |
| 141 | |
| 142 | [ "${NR_DEFINED_GROUPS}" -ne "${NR_COMBINED_GROUPS}" ] && { |
| 143 | echo "Undefined groups:" |
| 144 | diff "${TMP_DEFINED}" "${TMP_COMBINED}" | grep "^>" |
| 145 | cleanup |
| 146 | return 1 |
| 147 | } |
| 148 | |
| 149 | # Add checks for required directories here |
| 150 | |
| 151 | cleanup |
| 152 | return 0 |
| 153 | } |
| 154 | |
| 155 | apply_cfgfile() { |
| 156 | CFGFILE="$1" |
| 157 | |
| 158 | check_requirements "${CFGFILE}" || { |
| 159 | echo "Skipping ${CFGFILE}" |
| 160 | return 1 |
| 161 | } |
| 162 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 163 | cat ${CFGFILE} | sed 's/#.*//' | \ |
| 164 | while read TTYPE TUSER TGROUP TMODE TNAME TLTARGET; do |
| 165 | test -z "${TLTARGET}" && continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 166 | TNAME=${ROOT_DIR}${TNAME} |
| 167 | [ "${VERBOSE}" != "no" ] && echo "Checking for -${TNAME}-." |
| 168 | |
| 169 | [ "${TTYPE}" = "l" ] && { |
| 170 | TSOURCE="$TLTARGET" |
| 171 | [ "${VERBOSE}" != "no" ] && echo "Creating link -${TNAME}- pointing to -${TSOURCE}-." |
| 172 | link_file "${TSOURCE}" "${TNAME}" |
| 173 | continue |
| 174 | } |
| 175 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 176 | [ "${TTYPE}" = "b" ] && { |
| 177 | TSOURCE="$TLTARGET" |
| 178 | [ "${VERBOSE}" != "no" ] && echo "Creating mount-bind -${TNAME}- from -${TSOURCE}-." |
| 179 | mount --bind "${TSOURCE}" "${TNAME}" |
| 180 | EXEC=" |
| 181 | mount --bind \"${TSOURCE}\" \"${TNAME}\"" |
| 182 | test "$VOLATILE_ENABLE_CACHE" = yes && echo "$EXEC" >> /etc/volatile.cache.build |
| 183 | continue |
| 184 | } |
| 185 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 186 | [ -L "${TNAME}" ] && { |
| 187 | [ "${VERBOSE}" != "no" ] && echo "Found link." |
| 188 | NEWNAME=`ls -l "${TNAME}" | sed -e 's/^.*-> \(.*\)$/\1/'` |
| 189 | echo ${NEWNAME} | grep -v "^/" >/dev/null && { |
| 190 | TNAME="`echo ${TNAME} | sed -e 's@\(.*\)/.*@\1@'`/${NEWNAME}" |
| 191 | [ "${VERBOSE}" != "no" ] && echo "Converted relative linktarget to absolute path -${TNAME}-." |
| 192 | } || { |
| 193 | TNAME="${NEWNAME}" |
| 194 | [ "${VERBOSE}" != "no" ] && echo "Using absolute link target -${TNAME}-." |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | case "${TTYPE}" in |
| 199 | "f") [ "${VERBOSE}" != "no" ] && echo "Creating file -${TNAME}-." |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 200 | TSOURCE="$TLTARGET" |
| 201 | [ "${TSOURCE}" = "none" ] && TSOURCE="" |
| 202 | create_file "${TNAME}" "${TSOURCE}" & |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 203 | ;; |
| 204 | "d") [ "${VERBOSE}" != "no" ] && echo "Creating directory -${TNAME}-." |
| 205 | mk_dir "${TNAME}" |
| 206 | # Add check to see if there's an entry in fstab to mount. |
| 207 | ;; |
| 208 | *) [ "${VERBOSE}" != "no" ] && echo "Invalid type -${TTYPE}-." |
| 209 | continue |
| 210 | ;; |
| 211 | esac |
| 212 | done |
| 213 | return 0 |
| 214 | } |
| 215 | |
| 216 | clearcache=0 |
| 217 | exec 9</proc/cmdline |
| 218 | while read line <&9 |
| 219 | do |
| 220 | case "$line" in |
| 221 | *clearcache*) clearcache=1 |
| 222 | ;; |
| 223 | *) continue |
| 224 | ;; |
| 225 | esac |
| 226 | done |
| 227 | exec 9>&- |
| 228 | |
| 229 | if test -e ${ROOT_DIR}/etc/volatile.cache -a "$VOLATILE_ENABLE_CACHE" = "yes" -a "x$1" != "xupdate" -a "x$clearcache" = "x0" |
| 230 | then |
| 231 | sh ${ROOT_DIR}/etc/volatile.cache |
| 232 | else |
| 233 | rm -f ${ROOT_DIR}/etc/volatile.cache ${ROOT_DIR}/etc/volatile.cache.build |
| 234 | for file in `ls -1 "${CFGDIR}" | sort`; do |
| 235 | apply_cfgfile "${CFGDIR}/${file}" |
| 236 | done |
| 237 | |
| 238 | [ -e ${ROOT_DIR}/etc/volatile.cache.build ] && sync && mv ${ROOT_DIR}/etc/volatile.cache.build ${ROOT_DIR}/etc/volatile.cache |
| 239 | fi |
| 240 | |
| 241 | if [ -z "${ROOT_DIR}" ] && [ -f /etc/ld.so.cache ] && [ ! -f /var/run/ld.so.cache ] |
| 242 | then |
| 243 | ln -s /etc/ld.so.cache /var/run/ld.so.cache |
| 244 | fi |