blob: b56b72f83340a8950bac435cb9fa1e584717f3a6 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/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
Andrew Geissler635e0e42020-08-21 15:58:33 -050012DIRNAME="$(dirname "$0")"
13ROOT_DIR="$(echo "$DIRNAME" | sed -ne 's:/etc/.*::p')"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050014
Andrew Geissler635e0e42020-08-21 15:58:33 -050015[ -e "${ROOT_DIR}/etc/default/rcS" ] && . "${ROOT_DIR}/etc/default/rcS"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016# 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
21CFGDIR="${ROOT_DIR}/etc/default/volatiles"
22TMPROOT="${ROOT_DIR}/var/volatile/tmp"
23COREDEF="00_core"
24
25[ "${VERBOSE}" != "no" ] && echo "Populating volatile Filesystems."
26
27create_file() {
Brad Bishopd7bf8c12018-02-25 22:55:05 -050028 EXEC=""
Andrew Geissler635e0e42020-08-21 15:58:33 -050029 if [ -z "$2" ]; then
Brad Bishopd7bf8c12018-02-25 22:55:05 -050030 EXEC="
31 touch \"$1\";
32 "
Andrew Geissler635e0e42020-08-21 15:58:33 -050033 else
Brad Bishopd7bf8c12018-02-25 22:55:05 -050034 EXEC="
35 cp \"$2\" \"$1\";
36 "
Andrew Geissler635e0e42020-08-21 15:58:33 -050037 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038 EXEC="
Brad Bishopd7bf8c12018-02-25 22:55:05 -050039 ${EXEC}
Patrick Williams213cb262021-08-07 19:21:33 -050040 chown ${TUSER}:${TGROUP} $1 || echo \"Failed to set owner -${TUSER}- for -$1-.\";
41 chmod ${TMODE} $1 || echo \"Failed to set mode -${TMODE}- for -$1-.\" "
Patrick Williamsc124f4f2015-09-15 14:41:29 -050042
43 test "$VOLATILE_ENABLE_CACHE" = yes && echo "$EXEC" >> /etc/volatile.cache.build
44
Andrew Geissler635e0e42020-08-21 15:58:33 -050045 if [ -e "$1" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046 [ "${VERBOSE}" != "no" ] && echo "Target already exists. Skipping."
Andrew Geissler635e0e42020-08-21 15:58:33 -050047 else
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048 if [ -z "$ROOT_DIR" ]; then
Andrew Geissler635e0e42020-08-21 15:58:33 -050049 eval "$EXEC"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050 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.
Andrew Geissler635e0e42020-08-21 15:58:33 -050055 eval "$EXEC" > /dev/null 2>&1
Patrick Williamsc124f4f2015-09-15 14:41:29 -050056 fi
Andrew Geissler635e0e42020-08-21 15:58:33 -050057 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058}
59
60mk_dir() {
61 EXEC="
62 mkdir -p \"$1\";
Patrick Williams213cb262021-08-07 19:21:33 -050063 chown ${TUSER}:${TGROUP} $1 || echo \"Failed to set owner -${TUSER}- for -$1-.\";
64 chmod ${TMODE} $1 || echo \"Failed to set mode -${TMODE}- for -$1-.\" "
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065
66 test "$VOLATILE_ENABLE_CACHE" = yes && echo "$EXEC" >> /etc/volatile.cache.build
Andrew Geissler635e0e42020-08-21 15:58:33 -050067 if [ -e "$1" ]; then
Patrick Williamsc124f4f2015-09-15 14:41:29 -050068 [ "${VERBOSE}" != "no" ] && echo "Target already exists. Skipping."
Andrew Geissler635e0e42020-08-21 15:58:33 -050069 else
Patrick Williamsc124f4f2015-09-15 14:41:29 -050070 if [ -z "$ROOT_DIR" ]; then
Andrew Geissler635e0e42020-08-21 15:58:33 -050071 eval "$EXEC"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072 else
73 # For the same reason with create_file(), failures should
74 # not be logged.
Andrew Geissler635e0e42020-08-21 15:58:33 -050075 eval "$EXEC" > /dev/null 2>&1
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 fi
Andrew Geissler635e0e42020-08-21 15:58:33 -050077 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078}
79
80link_file() {
81 EXEC="
82 if [ -L \"$2\" ]; then
Brad Bishop79641f22019-09-10 07:20:22 -040083 [ \"\$(readlink \"$2\")\" != \"$1\" ] && { rm -f \"$2\"; ln -sf \"$1\" \"$2\"; };
Patrick Williamsc124f4f2015-09-15 14:41:29 -050084 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
Andrew Geissler635e0e42020-08-21 15:58:33 -050099 eval "$EXEC"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100 else
101 # For the same reason with create_file(), failures should
102 # not be logged.
Andrew Geissler635e0e42020-08-21 15:58:33 -0500103 eval "$EXEC" > /dev/null 2>&1
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104 fi
105}
106
107check_requirements() {
108 cleanup() {
109 rm "${TMP_INTERMED}"
110 rm "${TMP_DEFINED}"
111 rm "${TMP_COMBINED}"
112 }
113
114 CFGFILE="$1"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500115
116 TMP_INTERMED="${TMPROOT}/tmp.$$"
117 TMP_DEFINED="${TMPROOT}/tmpdefined.$$"
118 TMP_COMBINED="${TMPROOT}/tmpcombined.$$"
119
Andrew Geissler635e0e42020-08-21 15:58:33 -0500120 sed 's@\(^:\)*:.*@\1@' "${ROOT_DIR}/etc/passwd" | sort | uniq > "${TMP_DEFINED}"
121 grep -v "^#" "${CFGFILE}" | cut -s -d " " -f 2 > "${TMP_INTERMED}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500122 cat "${TMP_DEFINED}" "${TMP_INTERMED}" | sort | uniq > "${TMP_COMBINED}"
Andrew Geissler635e0e42020-08-21 15:58:33 -0500123 NR_DEFINED_USERS="$(wc -l < "${TMP_DEFINED}")"
124 NR_COMBINED_USERS="$(wc -l < "${TMP_COMBINED}")"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500125
126 [ "${NR_DEFINED_USERS}" -ne "${NR_COMBINED_USERS}" ] && {
127 echo "Undefined users:"
128 diff "${TMP_DEFINED}" "${TMP_COMBINED}" | grep "^>"
129 cleanup
130 return 1
131 }
132
133
Andrew Geissler635e0e42020-08-21 15:58:33 -0500134 sed 's@\(^:\)*:.*@\1@' "${ROOT_DIR}/etc/group" | sort | uniq > "${TMP_DEFINED}"
135 grep -v "^#" "${CFGFILE}" | cut -s -d " " -f 3 > "${TMP_INTERMED}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500136 cat "${TMP_DEFINED}" "${TMP_INTERMED}" | sort | uniq > "${TMP_COMBINED}"
137
Andrew Geissler635e0e42020-08-21 15:58:33 -0500138 NR_DEFINED_GROUPS="$(wc -l < "${TMP_DEFINED}")"
139 NR_COMBINED_GROUPS="$(wc -l < "${TMP_COMBINED}")"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500140
141 [ "${NR_DEFINED_GROUPS}" -ne "${NR_COMBINED_GROUPS}" ] && {
142 echo "Undefined groups:"
143 diff "${TMP_DEFINED}" "${TMP_COMBINED}" | grep "^>"
144 cleanup
145 return 1
146 }
147
148 # Add checks for required directories here
149
150 cleanup
151 return 0
152}
153
154apply_cfgfile() {
155 CFGFILE="$1"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800156 SKIP_REQUIREMENTS="$2"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500157
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800158 [ "${VERBOSE}" != "no" ] && echo "Applying ${CFGFILE}"
159
Andrew Geissler635e0e42020-08-21 15:58:33 -0500160 [ "${SKIP_REQUIREMENTS}" = "yes" ] || check_requirements "${CFGFILE}" || {
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500161 echo "Skipping ${CFGFILE}"
162 return 1
163 }
164
Andrew Geissler635e0e42020-08-21 15:58:33 -0500165 sed 's/#.*//' "${CFGFILE}" | \
166 while read -r TTYPE TUSER TGROUP TMODE TNAME TLTARGET; do
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500167 test -z "${TLTARGET}" && continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500168 TNAME=${ROOT_DIR}${TNAME}
169 [ "${VERBOSE}" != "no" ] && echo "Checking for -${TNAME}-."
170
171 [ "${TTYPE}" = "l" ] && {
172 TSOURCE="$TLTARGET"
173 [ "${VERBOSE}" != "no" ] && echo "Creating link -${TNAME}- pointing to -${TSOURCE}-."
174 link_file "${TSOURCE}" "${TNAME}"
175 continue
176 }
177
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500178 [ "${TTYPE}" = "b" ] && {
179 TSOURCE="$TLTARGET"
180 [ "${VERBOSE}" != "no" ] && echo "Creating mount-bind -${TNAME}- from -${TSOURCE}-."
181 mount --bind "${TSOURCE}" "${TNAME}"
182 EXEC="
183 mount --bind \"${TSOURCE}\" \"${TNAME}\""
184 test "$VOLATILE_ENABLE_CACHE" = yes && echo "$EXEC" >> /etc/volatile.cache.build
185 continue
186 }
187
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500188 [ -L "${TNAME}" ] && {
189 [ "${VERBOSE}" != "no" ] && echo "Found link."
Andrew Geissler635e0e42020-08-21 15:58:33 -0500190 NEWNAME=$(ls -l "${TNAME}" | sed -e 's/^.*-> \(.*\)$/\1/')
191 if echo "${NEWNAME}" | grep -v "^/" >/dev/null; then
192 TNAME="$(echo "${TNAME}" | sed -e 's@\(.*\)/.*@\1@')/${NEWNAME}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500193 [ "${VERBOSE}" != "no" ] && echo "Converted relative linktarget to absolute path -${TNAME}-."
Andrew Geissler635e0e42020-08-21 15:58:33 -0500194 else
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500195 TNAME="${NEWNAME}"
196 [ "${VERBOSE}" != "no" ] && echo "Using absolute link target -${TNAME}-."
Andrew Geissler635e0e42020-08-21 15:58:33 -0500197 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500198 }
199
200 case "${TTYPE}" in
201 "f") [ "${VERBOSE}" != "no" ] && echo "Creating file -${TNAME}-."
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500202 TSOURCE="$TLTARGET"
203 [ "${TSOURCE}" = "none" ] && TSOURCE=""
Patrick Williams213cb262021-08-07 19:21:33 -0500204 create_file "${TNAME}" "${TSOURCE}"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500205 ;;
206 "d") [ "${VERBOSE}" != "no" ] && echo "Creating directory -${TNAME}-."
207 mk_dir "${TNAME}"
208 # Add check to see if there's an entry in fstab to mount.
209 ;;
210 *) [ "${VERBOSE}" != "no" ] && echo "Invalid type -${TTYPE}-."
211 continue
212 ;;
213 esac
214 done
215 return 0
216}
217
218clearcache=0
219exec 9</proc/cmdline
Andrew Geissler635e0e42020-08-21 15:58:33 -0500220while read -r line <&9
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500221do
222 case "$line" in
223 *clearcache*) clearcache=1
224 ;;
225 *) continue
226 ;;
227 esac
228done
229exec 9>&-
230
Andrew Geissler635e0e42020-08-21 15:58:33 -0500231if test -e "${ROOT_DIR}/etc/volatile.cache" -a "$VOLATILE_ENABLE_CACHE" = "yes" -a "x$1" != "xupdate" -a "x$clearcache" = "x0"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500232then
Andrew Geissler635e0e42020-08-21 15:58:33 -0500233 sh "${ROOT_DIR}/etc/volatile.cache"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500234else
Andrew Geissler635e0e42020-08-21 15:58:33 -0500235 rm -f "${ROOT_DIR}/etc/volatile.cache" "${ROOT_DIR}/etc/volatile.cache.build"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800236
237 # Apply the core file with out checking requirements. ${TMPROOT} is
238 # needed by check_requirements but is setup by this file, so it must be
239 # processed first and without being checked.
240 [ -e "${CFGDIR}/${COREDEF}" ] && apply_cfgfile "${CFGDIR}/${COREDEF}" "yes"
241
242 # Fast path: check_requirements is slow and most of the time doesn't
243 # find any problems. If there are a lot of config files, it is much
244 # faster to to concatenate them all together and process them once to
245 # avoid the overhead of calling check_requirements repeatedly
246 TMP_FILE="${TMPROOT}/tmp_volatile.$$"
247 rm -f "$TMP_FILE"
248
Andrew Geissler635e0e42020-08-21 15:58:33 -0500249 CFGFILES="$(ls -1 "${CFGDIR}" | grep -v "^${COREDEF}\$" | sort)"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800250 for file in ${CFGFILES}; do
251 cat "${CFGDIR}/${file}" >> "$TMP_FILE"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500252 done
253
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800254 if check_requirements "$TMP_FILE"
255 then
256 apply_cfgfile "$TMP_FILE" "yes"
257 else
258 # Slow path: One or more config files failed requirements.
259 # Process each one individually so the offending one can be
260 # skipped
261 for file in ${CFGFILES}; do
262 apply_cfgfile "${CFGDIR}/${file}"
263 done
264 fi
265 rm "$TMP_FILE"
266
Andrew Geissler635e0e42020-08-21 15:58:33 -0500267 [ -e "${ROOT_DIR}/etc/volatile.cache.build" ] && sync && mv "${ROOT_DIR}/etc/volatile.cache.build" "${ROOT_DIR}/etc/volatile.cache"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500268fi
269
270if [ -z "${ROOT_DIR}" ] && [ -f /etc/ld.so.cache ] && [ ! -f /var/run/ld.so.cache ]
271then
272 ln -s /etc/ld.so.cache /var/run/ld.so.cache
273fi