populate-volatile.sh 6.6 KB

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