summaryrefslogtreecommitdiffstats
path: root/meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh
diff options
context:
space:
mode:
Diffstat (limited to 'meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh')
-rwxr-xr-xmeta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh222
1 files changed, 222 insertions, 0 deletions
diff --git a/meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh b/meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh
new file mode 100755
index 0000000000..904037eeaa
--- /dev/null
+++ b/meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh
@@ -0,0 +1,222 @@
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
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() {
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
50mk_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
70link_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
97check_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
145apply_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
166 [ -L "${TNAME}" ] && {
167 [ "${VERBOSE}" != "no" ] && echo "Found link."
168 NEWNAME=`ls -l "${TNAME}" | sed -e 's/^.*-> \(.*\)$/\1/'`
169 echo ${NEWNAME} | grep -v "^/" >/dev/null && {
170 TNAME="`echo ${TNAME} | sed -e 's@\(.*\)/.*@\1@'`/${NEWNAME}"
171 [ "${VERBOSE}" != "no" ] && echo "Converted relative linktarget to absolute path -${TNAME}-."
172 } || {
173 TNAME="${NEWNAME}"
174 [ "${VERBOSE}" != "no" ] && echo "Using absolute link target -${TNAME}-."
175 }
176 }
177
178 case "${TTYPE}" in
179 "f") [ "${VERBOSE}" != "no" ] && echo "Creating file -${TNAME}-."
180 create_file "${TNAME}" &
181 ;;
182 "d") [ "${VERBOSE}" != "no" ] && echo "Creating directory -${TNAME}-."
183 mk_dir "${TNAME}"
184 # Add check to see if there's an entry in fstab to mount.
185 ;;
186 *) [ "${VERBOSE}" != "no" ] && echo "Invalid type -${TTYPE}-."
187 continue
188 ;;
189 esac
190 done
191 return 0
192}
193
194clearcache=0
195exec 9</proc/cmdline
196while read line <&9
197do
198 case "$line" in
199 *clearcache*) clearcache=1
200 ;;
201 *) continue
202 ;;
203 esac
204done
205exec 9>&-
206
207if test -e ${ROOT_DIR}/etc/volatile.cache -a "$VOLATILE_ENABLE_CACHE" = "yes" -a "x$1" != "xupdate" -a "x$clearcache" = "x0"
208then
209 sh ${ROOT_DIR}/etc/volatile.cache
210else
211 rm -f ${ROOT_DIR}/etc/volatile.cache ${ROOT_DIR}/etc/volatile.cache.build
212 for file in `ls -1 "${CFGDIR}" | sort`; do
213 apply_cfgfile "${CFGDIR}/${file}"
214 done
215
216 [ -e ${ROOT_DIR}/etc/volatile.cache.build ] && sync && mv ${ROOT_DIR}/etc/volatile.cache.build ${ROOT_DIR}/etc/volatile.cache
217fi
218
219if [ -z "${ROOT_DIR}" ] && [ -f /etc/ld.so.cache ] && [ ! -f /var/run/ld.so.cache ]
220then
221 ln -s /etc/ld.so.cache /var/run/ld.so.cache
222fi