gen_initramfs_list.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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$" \
  213. && [ -x "`which gzip 2> /dev/null`" ] \
  214. && compr="gzip -n -9 -f"
  215. echo "$output_file" | grep -q "\.bz2$" \
  216. && [ -x "`which bzip2 2> /dev/null`" ] \
  217. && compr="bzip2 -9 -f"
  218. echo "$output_file" | grep -q "\.lzma$" \
  219. && [ -x "`which lzma 2> /dev/null`" ] \
  220. && compr="lzma -9 -f"
  221. echo "$output_file" | grep -q "\.xz$" \
  222. && [ -x "`which xz 2> /dev/null`" ] \
  223. && compr="xz --check=crc32 --lzma2=dict=1MiB"
  224. echo "$output_file" | grep -q "\.lzo$" \
  225. && [ -x "`which lzop 2> /dev/null`" ] \
  226. && compr="lzop -9 -f"
  227. echo "$output_file" | grep -q "\.lz4$" \
  228. && [ -x "`which lz4 2> /dev/null`" ] \
  229. && compr="lz4 -l -9 -f"
  230. echo "$output_file" | grep -q "\.cpio$" && compr="cat"
  231. shift
  232. ;;
  233. esac
  234. while [ $# -gt 0 ]; do
  235. arg="$1"
  236. shift
  237. case "$arg" in
  238. "-u") # map $1 to uid=0 (root)
  239. root_uid="$1"
  240. shift
  241. ;;
  242. "-g") # map $1 to gid=0 (root)
  243. root_gid="$1"
  244. shift
  245. ;;
  246. "-d") # display default initramfs list
  247. default_list="$arg"
  248. ${dep_list}default_initramfs
  249. ;;
  250. "-h")
  251. usage
  252. exit 0
  253. ;;
  254. *)
  255. case "$arg" in
  256. "-"*)
  257. unknown_option
  258. ;;
  259. *) # input file/dir - process it
  260. input_file "$arg" "$#"
  261. ;;
  262. esac
  263. ;;
  264. esac
  265. done
  266. # If output_file is set we will generate cpio archive and compress it
  267. # we are careful to delete tmp files
  268. if [ ! -z ${output_file} ]; then
  269. if [ -z ${cpio_file} ]; then
  270. timestamp=
  271. if test -n "$KBUILD_BUILD_TIMESTAMP"; then
  272. timestamp="$(date -d"$KBUILD_BUILD_TIMESTAMP" +%s || :)"
  273. if test -n "$timestamp"; then
  274. timestamp="-t $timestamp"
  275. fi
  276. fi
  277. cpio_tfile="$(mktemp ${TMPDIR:-/tmp}/cpiofile.XXXXXX)"
  278. usr/gen_init_cpio $timestamp ${cpio_list} > ${cpio_tfile}
  279. else
  280. cpio_tfile=${cpio_file}
  281. fi
  282. rm ${cpio_list}
  283. if [ "${is_cpio_compressed}" = "compressed" ]; then
  284. cat ${cpio_tfile} > ${output_file}
  285. else
  286. (cat ${cpio_tfile} | ${compr} - > ${output_file}) \
  287. || (rm -f ${output_file} ; false)
  288. fi
  289. [ -z ${cpio_file} ] && rm ${cpio_tfile}
  290. fi
  291. exit 0