gen_initramfs_list.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #!/bin/bash
  2. # Copyright (C) Martin Schlemmer <azarah@nosferatu.za.org>
  3. # Copyright (C) 2006 Sam Ravnborg <sam@ravnborg.org>
  4. #
  5. # Released under the terms of the GNU GPL
  6. #
  7. # Generate a cpio packed initramfs. It uses gen_init_cpio to generate
  8. # the cpio archive, and gzip to pack it.
  9. # The script may also be used to generate the inputfile used for gen_init_cpio
  10. # This script assumes that gen_init_cpio is located in usr/ directory
  11. # error out on errors
  12. set -e
  13. usage() {
  14. cat << EOF
  15. Usage:
  16. $0 [-o <file>] [-u <uid>] [-g <gid>] {-d | <cpio_source>} ...
  17. -o <file> Create gzipped initramfs file named <file> using
  18. gen_init_cpio and gzip
  19. -u <uid> User ID to map to user ID 0 (root).
  20. <uid> is only meaningful if <cpio_source>
  21. is a directory.
  22. -g <gid> Group ID to map to group ID 0 (root).
  23. <gid> is only meaningful if <cpio_source>
  24. is a directory.
  25. <cpio_source> File list or directory for cpio archive.
  26. If <cpio_source> is a .cpio file it will be used
  27. as direct input to initramfs.
  28. -d Output the default cpio list.
  29. All options except -o and -l may be repeated and are interpreted
  30. sequentially and immediately. -u and -g states are preserved across
  31. <cpio_source> options so an explicit "-u 0 -g 0" is required
  32. to reset the root/group mapping.
  33. EOF
  34. }
  35. # awk style field access
  36. # $1 - field number; rest is argument string
  37. field() {
  38. shift $1 ; echo $1
  39. }
  40. list_default_initramfs() {
  41. # echo usr/kinit/kinit
  42. :
  43. }
  44. default_initramfs() {
  45. cat <<-EOF >> ${output}
  46. # This is a very simple, default initramfs
  47. dir /dev 0755 0 0
  48. nod /dev/console 0600 0 0 c 5 1
  49. dir /root 0700 0 0
  50. # file /kinit usr/kinit/kinit 0755 0 0
  51. # slink /init kinit 0755 0 0
  52. EOF
  53. }
  54. filetype() {
  55. local argv1="$1"
  56. # symlink test must come before file test
  57. if [ -L "${argv1}" ]; then
  58. echo "slink"
  59. elif [ -f "${argv1}" ]; then
  60. echo "file"
  61. elif [ -d "${argv1}" ]; then
  62. echo "dir"
  63. elif [ -b "${argv1}" -o -c "${argv1}" ]; then
  64. echo "nod"
  65. elif [ -p "${argv1}" ]; then
  66. echo "pipe"
  67. elif [ -S "${argv1}" ]; then
  68. echo "sock"
  69. else
  70. echo "invalid"
  71. fi
  72. return 0
  73. }
  74. list_print_mtime() {
  75. :
  76. }
  77. print_mtime() {
  78. local my_mtime="0"
  79. if [ -e "$1" ]; then
  80. my_mtime=$(find "$1" -printf "%T@\n" | sort -r | head -n 1)
  81. fi
  82. echo "# Last modified: ${my_mtime}" >> ${output}
  83. echo "" >> ${output}
  84. }
  85. list_parse() {
  86. echo "$1 \\"
  87. }
  88. # for each file print a line in following format
  89. # <filetype> <name> <path to file> <octal mode> <uid> <gid>
  90. # for links, devices etc the format differs. See gen_init_cpio for details
  91. parse() {
  92. local location="$1"
  93. local name="${location/${srcdir}//}"
  94. # change '//' into '/'
  95. name="${name//\/\///}"
  96. local mode="$2"
  97. local uid="$3"
  98. local gid="$4"
  99. local ftype=$(filetype "${location}")
  100. # remap uid/gid to 0 if necessary
  101. [ "$uid" -eq "$root_uid" ] && uid=0
  102. [ "$gid" -eq "$root_gid" ] && gid=0
  103. local str="${mode} ${uid} ${gid}"
  104. [ "${ftype}" == "invalid" ] && return 0
  105. [ "${location}" == "${srcdir}" ] && return 0
  106. case "${ftype}" in
  107. "file")
  108. str="${ftype} ${name} ${location} ${str}"
  109. ;;
  110. "nod")
  111. local dev=`LC_ALL=C ls -l "${location}"`
  112. local maj=`field 5 ${dev}`
  113. local min=`field 6 ${dev}`
  114. maj=${maj%,}
  115. [ -b "${location}" ] && dev="b" || dev="c"
  116. str="${ftype} ${name} ${str} ${dev} ${maj} ${min}"
  117. ;;
  118. "slink")
  119. local target=`field 11 $(LC_ALL=C ls -l "${location}")`
  120. str="${ftype} ${name} ${target} ${str}"
  121. ;;
  122. *)
  123. str="${ftype} ${name} ${str}"
  124. ;;
  125. esac
  126. echo "${str}" >> ${output}
  127. return 0
  128. }
  129. unknown_option() {
  130. printf "ERROR: unknown option \"$arg\"\n" >&2
  131. printf "If the filename validly begins with '-', " >&2
  132. printf "then it must be prefixed\n" >&2
  133. printf "by './' so that it won't be interpreted as an option." >&2
  134. printf "\n" >&2
  135. usage >&2
  136. exit 1
  137. }
  138. list_header() {
  139. :
  140. }
  141. header() {
  142. printf "\n#####################\n# $1\n" >> ${output}
  143. }
  144. # process one directory (incl sub-directories)
  145. dir_filelist() {
  146. ${dep_list}header "$1"
  147. srcdir=$(echo "$1" | sed -e 's://*:/:g')
  148. dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n" 2>/dev/null)
  149. # If $dirlist is only one line, then the directory is empty
  150. if [ "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then
  151. ${dep_list}print_mtime "$1"
  152. echo "${dirlist}" | \
  153. while read x; do
  154. ${dep_list}parse ${x}
  155. done
  156. fi
  157. }
  158. # if only one file is specified and it is .cpio file then use it direct as fs
  159. # if a directory is specified then add all files in given direcotry to fs
  160. # if a regular file is specified assume it is in gen_initramfs format
  161. input_file() {
  162. source="$1"
  163. if [ -f "$1" ]; then
  164. ${dep_list}header "$1"
  165. is_cpio="$(echo "$1" | sed 's/^.*\.cpio/cpio/')"
  166. if [ $2 -eq 0 -a ${is_cpio} == "cpio" ]; then
  167. cpio_file=$1
  168. [ ! -z ${dep_list} ] && echo "$1"
  169. return 0
  170. fi
  171. if [ -z ${dep_list} ]; then
  172. print_mtime "$1" >> ${output}
  173. cat "$1" >> ${output}
  174. else
  175. cat "$1" | while read type dir file perm ; do
  176. if [ "$type" == "file" ]; then
  177. echo "$file \\";
  178. fi
  179. done
  180. fi
  181. elif [ -d "$1" ]; then
  182. dir_filelist "$1"
  183. else
  184. echo " ${prog}: Cannot open '$1'" >&2
  185. exit 1
  186. fi
  187. }
  188. prog=$0
  189. root_uid=0
  190. root_gid=0
  191. dep_list=
  192. cpio_file=
  193. cpio_list=
  194. output="/dev/stdout"
  195. output_file=""
  196. arg="$1"
  197. case "$arg" in
  198. "-l") # files included in initramfs - used by kbuild
  199. dep_list="list_"
  200. echo "deps_initramfs := \\"
  201. shift
  202. ;;
  203. "-o") # generate gzipped cpio image named $1
  204. shift
  205. output_file="$1"
  206. cpio_list="$(mktemp ${TMPDIR:-/tmp}/cpiolist.XXXXXX)"
  207. output=${cpio_list}
  208. shift
  209. ;;
  210. esac
  211. while [ $# -gt 0 ]; do
  212. arg="$1"
  213. shift
  214. case "$arg" in
  215. "-u") # map $1 to uid=0 (root)
  216. root_uid="$1"
  217. shift
  218. ;;
  219. "-g") # map $1 to gid=0 (root)
  220. root_gid="$1"
  221. shift
  222. ;;
  223. "-d") # display default initramfs list
  224. default_list="$arg"
  225. ${dep_list}default_initramfs
  226. ;;
  227. "-h")
  228. usage
  229. exit 0
  230. ;;
  231. *)
  232. case "$arg" in
  233. "-"*)
  234. unknown_option
  235. ;;
  236. *) # input file/dir - process it
  237. input_file "$arg" "$#"
  238. ;;
  239. esac
  240. ;;
  241. esac
  242. done
  243. # If output_file is set we will generate cpio archive and gzip it
  244. # we are carefull to delete tmp files
  245. if [ ! -z ${output_file} ]; then
  246. if [ -z ${cpio_file} ]; then
  247. cpio_tfile="$(mktemp ${TMPDIR:-/tmp}/cpiofile.XXXXXX)"
  248. usr/gen_init_cpio ${cpio_list} > ${cpio_tfile}
  249. else
  250. cpio_tfile=${cpio_file}
  251. fi
  252. rm ${cpio_list}
  253. cat ${cpio_tfile} | gzip -f -9 - > ${output_file}
  254. [ -z ${cpio_file} ] && rm ${cpio_tfile}
  255. fi
  256. exit 0