gen_initramfs_list.sh 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. list_default_initramfs() {
  36. # echo usr/kinit/kinit
  37. :
  38. }
  39. default_initramfs() {
  40. cat <<-EOF >> ${output}
  41. # This is a very simple, default initramfs
  42. dir /dev 0755 0 0
  43. nod /dev/console 0600 0 0 c 5 1
  44. dir /root 0700 0 0
  45. # file /kinit usr/kinit/kinit 0755 0 0
  46. # slink /init kinit 0755 0 0
  47. EOF
  48. }
  49. filetype() {
  50. local argv1="$1"
  51. # symlink test must come before file test
  52. if [ -L "${argv1}" ]; then
  53. echo "slink"
  54. elif [ -f "${argv1}" ]; then
  55. echo "file"
  56. elif [ -d "${argv1}" ]; then
  57. echo "dir"
  58. elif [ -b "${argv1}" -o -c "${argv1}" ]; then
  59. echo "nod"
  60. elif [ -p "${argv1}" ]; then
  61. echo "pipe"
  62. elif [ -S "${argv1}" ]; then
  63. echo "sock"
  64. else
  65. echo "invalid"
  66. fi
  67. return 0
  68. }
  69. list_print_mtime() {
  70. :
  71. }
  72. print_mtime() {
  73. local my_mtime="0"
  74. if [ -e "$1" ]; then
  75. my_mtime=$(find "$1" -printf "%T@\n" | sort -r | head -n 1)
  76. fi
  77. echo "# Last modified: ${my_mtime}" >> ${output}
  78. echo "" >> ${output}
  79. }
  80. list_parse() {
  81. echo "$1 \\"
  82. }
  83. # for each file print a line in following format
  84. # <filetype> <name> <path to file> <octal mode> <uid> <gid>
  85. # for links, devices etc the format differs. See gen_init_cpio for details
  86. parse() {
  87. local location="$1"
  88. local name="${location/${srcdir}//}"
  89. # change '//' into '/'
  90. name="${name//\/\///}"
  91. local mode="$2"
  92. local uid="$3"
  93. local gid="$4"
  94. local ftype=$(filetype "${location}")
  95. # remap uid/gid to 0 if necessary
  96. [ "$uid" -eq "$root_uid" ] && uid=0
  97. [ "$gid" -eq "$root_gid" ] && gid=0
  98. local str="${mode} ${uid} ${gid}"
  99. [ "${ftype}" == "invalid" ] && return 0
  100. [ "${location}" == "${srcdir}" ] && return 0
  101. case "${ftype}" in
  102. "file")
  103. str="${ftype} ${name} ${location} ${str}"
  104. ;;
  105. "nod")
  106. local dev_type=
  107. local maj=$(LC_ALL=C ls -l "${location}" | \
  108. gawk '{sub(/,/, "", $5); print $5}')
  109. local min=$(LC_ALL=C ls -l "${location}" | \
  110. gawk '{print $6}')
  111. if [ -b "${location}" ]; then
  112. dev_type="b"
  113. else
  114. dev_type="c"
  115. fi
  116. str="${ftype} ${name} ${str} ${dev_type} ${maj} ${min}"
  117. ;;
  118. "slink")
  119. local target=$(LC_ALL=C ls -l "${location}" | \
  120. gawk '{print $11}')
  121. str="${ftype} ${name} ${target} ${str}"
  122. ;;
  123. *)
  124. str="${ftype} ${name} ${str}"
  125. ;;
  126. esac
  127. echo "${str}" >> ${output}
  128. return 0
  129. }
  130. unknown_option() {
  131. printf "ERROR: unknown option \"$arg\"\n" >&2
  132. printf "If the filename validly begins with '-', " >&2
  133. printf "then it must be prefixed\n" >&2
  134. printf "by './' so that it won't be interpreted as an option." >&2
  135. printf "\n" >&2
  136. usage >&2
  137. exit 1
  138. }
  139. list_header() {
  140. echo "deps_initramfs := \\"
  141. }
  142. header() {
  143. printf "\n#####################\n# $1\n" >> ${output}
  144. }
  145. # process one directory (incl sub-directories)
  146. dir_filelist() {
  147. ${dep_list}header "$1"
  148. srcdir=$(echo "$1" | sed -e 's://*:/:g')
  149. dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n" 2>/dev/null)
  150. # If $dirlist is only one line, then the directory is empty
  151. if [ "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then
  152. ${dep_list}print_mtime "$1"
  153. echo "${dirlist}" | \
  154. while read x; do
  155. ${dep_list}parse ${x}
  156. done
  157. fi
  158. }
  159. # if only one file is specified and it is .cpio file then use it direct as fs
  160. # if a directory is specified then add all files in given direcotry to fs
  161. # if a regular file is specified assume it is in gen_initramfs format
  162. input_file() {
  163. source="$1"
  164. if [ -f "$1" ]; then
  165. ${dep_list}header "$1"
  166. is_cpio="$(echo "$1" | sed 's/^.*\.cpio/cpio/')"
  167. if [ $2 -eq 0 -a ${is_cpio} == "cpio" ]; then
  168. cpio_file=$1
  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. cat "$1" | while read type dir file perm ; do
  177. if [ "$type" == "file" ]; then
  178. echo "$file \\";
  179. fi
  180. done
  181. fi
  182. elif [ -d "$1" ]; then
  183. dir_filelist "$1"
  184. else
  185. echo " ${prog}: Cannot open '$1'" >&2
  186. exit 1
  187. fi
  188. }
  189. prog=$0
  190. root_uid=0
  191. root_gid=0
  192. dep_list=
  193. cpio_file=
  194. cpio_list=
  195. output="/dev/stdout"
  196. output_file=""
  197. arg="$1"
  198. case "$arg" in
  199. "-l") # files included in initramfs - used by kbuild
  200. dep_list="list_"
  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