blob: 3cf3243cf03616d0dbc07c5acdd19b65f9f64d92 [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
12DIRNAME=`dirname $0`
13ROOT_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
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=""
29 [ -z "$2" ] && {
30 EXEC="
31 touch \"$1\";
32 "
33 } || {
34 EXEC="
35 cp \"$2\" \"$1\";
36 "
37 }
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038 EXEC="
Brad Bishopd7bf8c12018-02-25 22:55:05 -050039 ${EXEC}
Brad Bishopc4ea0752018-11-15 14:30:15 -080040 chown ${TUSER}:${TGROUP} $1 || echo \"Failed to set owner -${TUSER}- for -$1-.\" >/dev/tty0 2>&1;
Patrick Williamsc124f4f2015-09-15 14:41:29 -050041 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 Bishop6e60e8b2018-02-01 10:27:11 -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.
55 eval $EXEC > /dev/null 2>&1
56 fi
57 }
58}
59
60mk_dir() {
61 EXEC="
62 mkdir -p \"$1\";
Brad Bishopc4ea0752018-11-15 14:30:15 -080063 chown ${TUSER}:${TGROUP} $1 || echo \"Failed to set owner -${TUSER}- for -$1-.\" >/dev/tty0 2>&1;
Patrick Williamsc124f4f2015-09-15 14:41:29 -050064 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
80link_file() {
81 EXEC="
82 if [ -L \"$2\" ]; then
Brad Bishop6e60e8b2018-02-01 10:27:11 -050083 [ \"\$(readlink -f \"$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
Brad Bishop6e60e8b2018-02-01 10:27:11 -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.
103 eval $EXEC > /dev/null 2>&1
104 fi
105}
106
107check_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
155apply_cfgfile() {
156 CFGFILE="$1"
157
158 check_requirements "${CFGFILE}" || {
159 echo "Skipping ${CFGFILE}"
160 return 1
161 }
162
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500163 cat ${CFGFILE} | sed 's/#.*//' | \
164 while read TTYPE TUSER TGROUP TMODE TNAME TLTARGET; do
165 test -z "${TLTARGET}" && continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500166 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 Williamsd8c66bc2016-06-20 12:57:21 -0500176 [ "${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 Williamsc124f4f2015-09-15 14:41:29 -0500186 [ -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 Bishopd7bf8c12018-02-25 22:55:05 -0500200 TSOURCE="$TLTARGET"
201 [ "${TSOURCE}" = "none" ] && TSOURCE=""
202 create_file "${TNAME}" "${TSOURCE}" &
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500203 ;;
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
216clearcache=0
217exec 9</proc/cmdline
218while read line <&9
219do
220 case "$line" in
221 *clearcache*) clearcache=1
222 ;;
223 *) continue
224 ;;
225 esac
226done
227exec 9>&-
228
229if test -e ${ROOT_DIR}/etc/volatile.cache -a "$VOLATILE_ENABLE_CACHE" = "yes" -a "x$1" != "xupdate" -a "x$clearcache" = "x0"
230then
231 sh ${ROOT_DIR}/etc/volatile.cache
232else
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
239fi
240
241if [ -z "${ROOT_DIR}" ] && [ -f /etc/ld.so.cache ] && [ ! -f /var/run/ld.so.cache ]
242then
243 ln -s /etc/ld.so.cache /var/run/ld.so.cache
244fi