gen_initramfs_list.sh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. if [ -L "$1" ]; then
  87. return
  88. fi
  89. echo "$1" | sed 's/:/\\:/g; s/$/ \\/'
  90. }
  91. # for each file print a line in following format
  92. # <filetype> <name> <path to file> <octal mode> <uid> <gid>
  93. # for links, devices etc the format differs. See gen_init_cpio for details
  94. parse() {
  95. local location="$1"
  96. local name="/${location#${srcdir}}"
  97. # change '//' into '/'
  98. name=$(echo "$name" | sed -e 's://*:/:g')
  99. local mode="$2"
  100. local uid="$3"
  101. local gid="$4"
  102. local ftype=$(filetype "${location}")
  103. # remap uid/gid to 0 if necessary
  104. [ "$root_uid" = "squash" ] && uid=0 || [ "$uid" -eq "$root_uid" ] && uid=0
  105. [ "$root_gid" = "squash" ] && gid=0 || [ "$gid" -eq "$root_gid" ] && gid=0
  106. local str="${mode} ${uid} ${gid}"
  107. [ "${ftype}" = "invalid" ] && return 0
  108. [ "${location}" = "${srcdir}" ] && return 0
  109. case "${ftype}" in
  110. "file")
  111. str="${ftype} ${name} ${location} ${str}"
  112. ;;
  113. "nod")
  114. local dev="`LC_ALL=C ls -l "${location}"`"
  115. local maj=`field 5 ${dev}`
  116. local min=`field 6 ${dev}`
  117. maj=${maj%,}
  118. [ -b "${location}" ] && dev="b" || dev="c"
  119. str="${ftype} ${name} ${str} ${dev} ${maj} ${min}"
  120. ;;
  121. "slink")
  122. local target=`readlink "${location}"`
  123. str="${ftype} ${name} ${target} ${str}"
  124. ;;
  125. *)
  126. str="${ftype} ${name} ${str}"
  127. ;;
  128. esac
  129. echo "${str}" >> ${output}
  130. return 0
  131. }
  132. unknown_option() {
  133. printf "ERROR: unknown option \"$arg\"\n" >&2
  134. printf "If the filename validly begins with '-', " >&2
  135. printf "then it must be prefixed\n" >&2
  136. printf "by './' so that it won't be interpreted as an option." >&2
  137. printf "\n" >&2
  138. usage >&2
  139. exit 1
  140. }
  141. list_header() {
  142. :
  143. }
  144. header() {
  145. printf "\n#####################\n# $1\n" >> ${output}
  146. }
  147. # process one directory (incl sub-directories)
  148. dir_filelist() {
  149. ${dep_list}header "$1"
  150. srcdir=$(echo "$1" | sed -e 's://*:/:g')
  151. dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n" | LANG=C sort)
  152. # If $dirlist is only one line, then the directory is empty
  153. if [ "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then
  154. ${dep_list}print_mtime "$1"
  155. echo "${dirlist}" | \
  156. while read x; do
  157. ${dep_list}parse ${x}
  158. done
  159. fi
  160. }
  161. # if only one file is specified and it is .cpio file then use it direct as fs
  162. # if a directory is specified then add all files in given direcotry to fs
  163. # if a regular file is specified assume it is in gen_initramfs format
  164. input_file() {
  165. source="$1"
  166. if [ -f "$1" ]; then
  167. ${dep_list}header "$1"
  168. is_cpio="$(echo "$1" | sed 's/^.*\.cpio\(\..*\)\{0,1\}/cpio/')"
  169. if [ $2 -eq 0 -a ${is_cpio} = "cpio" ]; then
  170. cpio_file=$1
  171. echo "$1" | grep -q '^.*\.cpio\..*' && is_cpio_compressed="compressed"
  172. [ ! -z ${dep_list} ] && echo "$1"
  173. return 0
  174. fi
  175. if [ -z ${dep_list} ]; then
  176. print_mtime "$1" >> ${output}
  177. cat "$1" >> ${output}
  178. else
  179. echo "$1 \\"
  180. cat "$1" | while read type dir file perm ; do
  181. if [ "$type" = "file" ]; then
  182. echo "$file \\";
  183. fi
  184. done
  185. fi
  186. elif [ -d "$1" ]; then
  187. dir_filelist "$1"
  188. else
  189. echo " ${prog}: Cannot open '$1'" >&2
  190. exit 1
  191. fi
  192. }
  193. prog=$0
  194. root_uid=0
  195. root_gid=0
  196. dep_list=
  197. cpio_file=
  198. cpio_list=
  199. output="/dev/stdout"
  200. output_file=""
  201. is_cpio_compressed=
  202. compr="gzip -n -9 -f"
  203. arg="$1"
  204. case "$arg" in
  205. "-l") # files included in initramfs - used by kbuild
  206. dep_list="list_"
  207. echo "deps_initramfs := $0 \\"
  208. shift
  209. ;;
  210. "-o") # generate compressed cpio image named $1
  211. shift
  212. output_file="$1"
  213. cpio_list="$(mktemp ${TMPDIR:-/tmp}/cpiolist.XXXXXX)"
  214. output=${cpio_list}
  215. echo "$output_file" | grep -q "\.gz$" \
  216. && [ -x "`which gzip 2> /dev/null`" ] \
  217. && compr="gzip -n -9 -f"
  218. echo "$output_file" | grep -q "\.bz2$" \
  219. && [ -x "`which bzip2 2> /dev/null`" ] \
  220. && compr="bzip2 -9 -f"
  221. echo "$output_file" | grep -q "\.lzma$" \
  222. && [ -x "`which lzma 2> /dev/null`" ] \
  223. && compr="lzma -9 -f"
  224. echo "$output_file" | grep -q "\.xz$" \
  225. && [ -x "`which xz 2> /dev/null`" ] \
  226. && compr="xz --check=crc32 --lzma2=dict=1MiB"
  227. echo "$output_file" | grep -q "\.lzo$" \
  228. && [ -x "`which lzop 2> /dev/null`" ] \
  229. && compr="lzop -9 -f"
  230. echo "$output_file" | grep -q "\.lz4$" \
  231. && [ -x "`which lz4 2> /dev/null`" ] \
  232. && compr="lz4 -l -9 -f"
  233. echo "$output_file" | grep -q "\.cpio$" && compr="cat"
  234. shift
  235. ;;
  236. esac
  237. while [ $# -gt 0 ]; do
  238. arg="$1"
  239. shift
  240. case "$arg" in
  241. "-u") # map $1 to uid=0 (root)
  242. root_uid="$1"
  243. [ "$root_uid" = "-1" ] && root_uid=$(id -u || echo 0)
  244. shift
  245. ;;
  246. "-g") # map $1 to gid=0 (root)
  247. root_gid="$1"
  248. [ "$root_gid" = "-1" ] && root_gid=$(id -g || echo 0)
  249. shift
  250. ;;
  251. "-d") # display default initramfs list
  252. default_list="$arg"
  253. ${dep_list}default_initramfs
  254. ;;
  255. "-h")
  256. usage
  257. exit 0
  258. ;;
  259. *)
  260. case "$arg" in
  261. "-"*)
  262. unknown_option
  263. ;;
  264. *) # input file/dir - process it
  265. input_file "$arg" "$#"
  266. ;;
  267. esac
  268. ;;
  269. esac
  270. done
  271. # If output_file is set we will generate cpio archive and compress it
  272. # we are careful to delete tmp files
  273. if [ ! -z ${output_file} ]; then
  274. if [ -z ${cpio_file} ]; then
  275. timestamp=
  276. if test -n "$KBUILD_BUILD_TIMESTAMP"; then
  277. timestamp="$(date -d"$KBUILD_BUILD_TIMESTAMP" +%s || :)"
  278. if test -n "$timestamp"; then
  279. timestamp="-t $timestamp"
  280. fi
  281. fi
  282. cpio_tfile="$(mktemp ${TMPDIR:-/tmp}/cpiofile.XXXXXX)"
  283. usr/gen_init_cpio $timestamp ${cpio_list} > ${cpio_tfile}
  284. else
  285. cpio_tfile=${cpio_file}
  286. fi
  287. rm ${cpio_list}
  288. if [ "${is_cpio_compressed}" = "compressed" ]; then
  289. cat ${cpio_tfile} > ${output_file}
  290. else
  291. (cat ${cpio_tfile} | ${compr} - > ${output_file}) \
  292. || (rm -f ${output_file} ; false)
  293. fi
  294. [ -z ${cpio_file} ] && rm ${cpio_tfile}
  295. fi
  296. exit 0