Merge remote-tracking branch 'oe_21/master' into vuplus-3.0
[vuplus_openvuplus_3.0] / meta-openvuplus / recipes-core / initscripts / initscripts-1.0 / populate-volatile.sh
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/ttyS0 2>&1;
31         chmod ${TMODE} $1 || echo \"Failed to set mode -${TMODE}- for -$1-.\" >/dev/ttyS0 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/ttyS0 2>&1;
54         chmod ${TMODE} $1 || echo \"Failed to set mode -${TMODE}- for -$1-.\" >/dev/ttyS0 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                 cp -a $2/* $1 2>/dev/null;
76                 cp -a $2/.[!.]* $1 2>/dev/null;
77                 rm -rf \"$2\";
78                 ln -sf \"$1\" \"$2\";
79         else
80                 ln -sf \"$1\" \"$2\";
81         fi
82         "
83
84         test "$VOLATILE_ENABLE_CACHE" = yes && echo "   $EXEC" >> /etc/volatile.cache.build
85
86         if [ -z "$ROOT_DIR" ]; then
87                 eval $EXEC &
88         else
89                 # For the same reason with create_file(), failures should
90                 # not be logged.
91                 eval $EXEC > /dev/null 2>&1
92         fi
93 }
94
95 check_requirements() {
96         cleanup() {
97                 rm "${TMP_INTERMED}"
98                 rm "${TMP_DEFINED}"
99                 rm "${TMP_COMBINED}"
100         }
101
102         CFGFILE="$1"
103         [ `basename "${CFGFILE}"` = "${COREDEF}" ] && return 0
104
105         TMP_INTERMED="${TMPROOT}/tmp.$$"
106         TMP_DEFINED="${TMPROOT}/tmpdefined.$$"
107         TMP_COMBINED="${TMPROOT}/tmpcombined.$$"
108
109         sed 's@\(^:\)*:.*@\1@' ${ROOT_DIR}/etc/passwd | sort | uniq > "${TMP_DEFINED}"
110         cat ${CFGFILE} | grep -v "^#" | cut -s -d " " -f 2 > "${TMP_INTERMED}"
111         cat "${TMP_DEFINED}" "${TMP_INTERMED}" | sort | uniq > "${TMP_COMBINED}"
112         NR_DEFINED_USERS="`cat "${TMP_DEFINED}" | wc -l`"
113         NR_COMBINED_USERS="`cat "${TMP_COMBINED}" | wc -l`"
114
115         [ "${NR_DEFINED_USERS}" -ne "${NR_COMBINED_USERS}" ] && {
116                 echo "Undefined users:"
117                 diff "${TMP_DEFINED}" "${TMP_COMBINED}" | grep "^>"
118                 cleanup
119                 return 1
120         }
121
122
123         sed 's@\(^:\)*:.*@\1@' ${ROOT_DIR}/etc/group | sort | uniq > "${TMP_DEFINED}"
124         cat ${CFGFILE} | grep -v "^#" | cut -s -d " " -f 3 > "${TMP_INTERMED}"
125         cat "${TMP_DEFINED}" "${TMP_INTERMED}" | sort | uniq > "${TMP_COMBINED}"
126
127         NR_DEFINED_GROUPS="`cat "${TMP_DEFINED}" | wc -l`"
128         NR_COMBINED_GROUPS="`cat "${TMP_COMBINED}" | wc -l`"
129
130         [ "${NR_DEFINED_GROUPS}" -ne "${NR_COMBINED_GROUPS}" ] && {
131                 echo "Undefined groups:"
132                 diff "${TMP_DEFINED}" "${TMP_COMBINED}" | grep "^>"
133                 cleanup
134                 return 1
135         }
136
137         # Add checks for required directories here
138
139         cleanup
140         return 0
141 }
142
143 apply_cfgfile() {
144         CFGFILE="$1"
145
146         check_requirements "${CFGFILE}" || {
147                 echo "Skipping ${CFGFILE}"
148                 return 1
149         }
150
151         cat ${CFGFILE} | grep -v "^#" | \
152                 while read LINE; do
153                 eval `echo "$LINE" | sed -n "s/\(.*\)\ \(.*\) \(.*\)\ \(.*\)\ \(.*\)\ \(.*\)/TTYPE=\1 ; TUSER=\2; TGROUP=\3; TMODE=\4; TNAME=\5 TLTARGET=\6/p"`
154                 TNAME=${ROOT_DIR}${TNAME}
155                 [ "${VERBOSE}" != "no" ] && echo "Checking for -${TNAME}-."
156
157                 [ "${TTYPE}" = "l" ] && {
158                         TSOURCE="$TLTARGET"
159                         [ "${VERBOSE}" != "no" ] && echo "Creating link -${TNAME}- pointing to -${TSOURCE}-."
160                         link_file "${TSOURCE}" "${TNAME}"
161                         continue
162                 }
163
164                 [ -L "${TNAME}" ] && {
165                         [ "${VERBOSE}" != "no" ] && echo "Found link."
166                         NEWNAME=`ls -l "${TNAME}" | sed -e 's/^.*-> \(.*\)$/\1/'`
167                         echo ${NEWNAME} | grep -v "^/" >/dev/null && {
168                                 TNAME="`echo ${TNAME} | sed -e 's@\(.*\)/.*@\1@'`/${NEWNAME}"
169                                 [ "${VERBOSE}" != "no" ] && echo "Converted relative linktarget to absolute path -${TNAME}-."
170                         } || {
171                                 TNAME="${NEWNAME}"
172                                 [ "${VERBOSE}" != "no" ] && echo "Using absolute link target -${TNAME}-."
173                         }
174                 }
175
176                 case "${TTYPE}" in
177                         "f")  [ "${VERBOSE}" != "no" ] && echo "Creating file -${TNAME}-."
178                                 create_file "${TNAME}" &
179                                 ;;
180                         "d")  [ "${VERBOSE}" != "no" ] && echo "Creating directory -${TNAME}-."
181                                 mk_dir "${TNAME}"
182                                 # Add check to see if there's an entry in fstab to mount.
183                                 ;;
184                         *)    [ "${VERBOSE}" != "no" ] && echo "Invalid type -${TTYPE}-."
185                                 continue
186                                 ;;
187                 esac
188         done
189         return 0
190 }
191
192 clearcache=0
193 exec 9</proc/cmdline
194 while read line <&9
195 do
196         case "$line" in
197                 *clearcache*)  clearcache=1
198                                ;;
199                 *)             continue
200                                ;;
201         esac
202 done
203 exec 9>&-
204
205 if test -e ${ROOT_DIR}/etc/volatile.cache -a "$VOLATILE_ENABLE_CACHE" = "yes" -a "x$1" != "xupdate" -a "x$clearcache" = "x0"
206 then
207         sh ${ROOT_DIR}/etc/volatile.cache
208 else
209         rm -f ${ROOT_DIR}/etc/volatile.cache ${ROOT_DIR}/etc/volatile.cache.build
210         for file in `ls -1 "${CFGDIR}" | sort`; do
211                 apply_cfgfile "${CFGDIR}/${file}"
212         done
213
214         [ -e ${ROOT_DIR}/etc/volatile.cache.build ] && sync && mv ${ROOT_DIR}/etc/volatile.cache.build ${ROOT_DIR}/etc/volatile.cache
215 fi
216
217 if [ -z "${ROOT_DIR}" ] && [ -f /etc/ld.so.cache ] && [ ! -f /var/run/ld.so.cache ]
218 then
219         ln -s /etc/ld.so.cache /var/run/ld.so.cache
220 fi