gen_initramfs_list.sh 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #!/bin/sh
  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 then compresses 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 compressed initramfs file named <file> using
  18. gen_init_cpio and compressor depending on the extension
  19. -u <uid> User ID to map to user ID 0 (root).
  20. <uid> is only meaningful if <cpio_source> is a
  21. directory. "squash" forces all files to uid 0.
  22. -g <gid> Group ID to map to group ID 0 (root).
  23. <gid> is only meaningful if <cpio_source> is a
  24. directory. "squash" forces all files to gid 0.
  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. [ ! -L "$1" ] && 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=$(echo "$name" | sed -e 's://*:/:g')
  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. [ "$root_uid" = "squash" ] && uid=0 || [ "$uid" -eq "$root_uid" ] && uid=0
  102. [ "$root_gid" = "squash" ] && gid=0 || [ "$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=`readlink "${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")
  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. echo "$1" | grep -q '^.*\.cpio\..*' && is_cpio_compressed="compressed"
  169. [ ! -z ${dep_list} ] && echo "$1"
  170. return 0
  171. fi
  172. if [ -z ${dep_list} ]; then
  173. print_mtime "$1" >> ${output}
  174. cat "$1" >> ${output}
  175. else
  176. echo "$1 \\"
  177. cat "$1" | while read type dir file perm ; do
  178. if [ "$type" = "file" ]; then
  179. echo "$file \\";
  180. fi
  181. done
  182. fi
  183. elif [ -d "$1" ]; then
  184. dir_filelist "$1"
  185. else
  186. echo " ${prog}: Cannot open '$1'" >&2
  187. exit 1
  188. fi
  189. }
  190. prog=$0
  191. root_uid=0
  192. root_gid=0
  193. dep_list=
  194. cpio_file=
  195. cpio_list=
  196. output="/dev/stdout"
  197. output_file=""
  198. is_cpio_compressed=
  199. compr="gzip -n -9 -f"
  200. arg="$1"
  201. case "$arg" in
  202. "-l") # files included in initramfs - used by kbuild
  203. dep_list="list_"
  204. echo "deps_initramfs := $0 \\"
  205. shift
  206. ;;
  207. "-o") # generate compressed cpio image named $1
  208. shift
  209. output_file="$1"
  210. cpio_list="$(mktemp ${TMPDIR:-/tmp}/cpiolist.XXXXXX)"
  211. output=${cpio_list}
  212. echo "$output_file" | grep -q "\.gz$" && compr="gzip -n -9 -f"
  213. echo "$output_file" | grep -q "\.bz2$" && compr="bzip2 -9 -f"
  214. echo "$output_file" | grep -q "\.lzma$" && compr="lzma -9 -f"
  215. echo "$output_file" | grep -q "\.xz$" && \
  216. compr="xz --check=crc32 --lzma2=dict=1MiB"
  217. echo "$output_file" | grep -q "\.lzo$" && compr="lzop -9 -f"
  218. echo "$output_file" | grep -q "\.cpio$" && compr="cat"
  219. shift
  220. ;;
  221. esac
  222. while [ $# -gt 0 ]; do
  223. arg="$1"
  224. shift
  225. case "$arg" in
  226. "-u") # map $1 to uid=0 (root)
  227. root_uid="$1"
  228. shift
  229. ;;
  230. "-g") # map $1 to gid=0 (root)
  231. root_gid="$1"
  232. shift
  233. ;;
  234. "-d") # display default initramfs list
  235. default_list="$arg"
  236. ${dep_list}default_initramfs
  237. ;;
  238. "-h")
  239. usage
  240. exit 0
  241. ;;
  242. *)
  243. case "$arg" in
  244. "-"*)
  245. unknown_option
  246. ;;
  247. *) # input file/dir - process it
  248. input_file "$arg" "$#"
  249. ;;
  250. esac
  251. ;;
  252. esac
  253. done
  254. # If output_file is set we will generate cpio archive and compress it
  255. # we are careful to delete tmp files
  256. if [ ! -z ${output_file} ]; then
  257. if [ -z ${cpio_file} ]; then
  258. timestamp=
  259. if test -n "$KBUILD_BUILD_TIMESTAMP"; then
  260. timestamp="$(date -d"$KBUILD_BUILD_TIMESTAMP" +%s || :)"
  261. if test -n "$timestamp"; then
  262. timestamp="-t $timestamp"
  263. fi
  264. fi
  265. cpio_tfile="$(mktemp ${TMPDIR:-/tmp}/cpiofile.XXXXXX)"
  266. usr/gen_init_cpio $timestamp ${cpio_list} > ${cpio_tfile}
  267. else
  268. cpio_tfile=${cpio_file}
  269. fi
  270. rm ${cpio_list}
  271. if [ "${is_cpio_compressed}" = "compressed" ]; then
  272. cat ${cpio_tfile} > ${output_file}
  273. else
  274. (cat ${cpio_tfile} | ${compr} - > ${output_file}) \
  275. || (rm -f ${output_file} ; false)
  276. fi
  277. [ -z ${cpio_file} ] && rm ${cpio_tfile}
  278. fi
  279. exit 0