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