gen_initramfs_list.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #!/bin/bash
  2. # Copyright (C) Martin Schlemmer <azarah@nosferatu.za.org>
  3. # Released under the terms of the GNU GPL
  4. #
  5. # Generate a newline separated list of entries from the file/directory
  6. # supplied as an argument.
  7. #
  8. # If a file/directory is not supplied then generate a small dummy file.
  9. #
  10. # The output is suitable for gen_init_cpio built from usr/gen_init_cpio.c.
  11. #
  12. default_initramfs() {
  13. cat <<-EOF
  14. # This is a very simple, default initramfs
  15. dir /dev 0755 0 0
  16. nod /dev/console 0600 0 0 c 5 1
  17. dir /root 0700 0 0
  18. EOF
  19. }
  20. filetype() {
  21. local argv1="$1"
  22. # symlink test must come before file test
  23. if [ -L "${argv1}" ]; then
  24. echo "slink"
  25. elif [ -f "${argv1}" ]; then
  26. echo "file"
  27. elif [ -d "${argv1}" ]; then
  28. echo "dir"
  29. elif [ -b "${argv1}" -o -c "${argv1}" ]; then
  30. echo "nod"
  31. elif [ -p "${argv1}" ]; then
  32. echo "pipe"
  33. elif [ -S "${argv1}" ]; then
  34. echo "sock"
  35. else
  36. echo "invalid"
  37. fi
  38. return 0
  39. }
  40. print_mtime() {
  41. local argv1="$1"
  42. local my_mtime="0"
  43. if [ -e "${argv1}" ]; then
  44. my_mtime=$(find "${argv1}" -printf "%T@\n" | sort -r | head -n 1)
  45. fi
  46. echo "# Last modified: ${my_mtime}"
  47. echo
  48. }
  49. parse() {
  50. local location="$1"
  51. local name="${location/${srcdir}//}"
  52. # change '//' into '/'
  53. name="${name//\/\///}"
  54. local mode="$2"
  55. local uid="$3"
  56. local gid="$4"
  57. local ftype=$(filetype "${location}")
  58. # remap uid/gid to 0 if necessary
  59. [ "$uid" -eq "$root_uid" ] && uid=0
  60. [ "$gid" -eq "$root_gid" ] && gid=0
  61. local str="${mode} ${uid} ${gid}"
  62. [ "${ftype}" == "invalid" ] && return 0
  63. [ "${location}" == "${srcdir}" ] && return 0
  64. case "${ftype}" in
  65. "file")
  66. str="${ftype} ${name} ${location} ${str}"
  67. ;;
  68. "nod")
  69. local dev_type=
  70. local maj=$(LC_ALL=C ls -l "${location}" | \
  71. gawk '{sub(/,/, "", $5); print $5}')
  72. local min=$(LC_ALL=C ls -l "${location}" | \
  73. gawk '{print $6}')
  74. if [ -b "${location}" ]; then
  75. dev_type="b"
  76. else
  77. dev_type="c"
  78. fi
  79. str="${ftype} ${name} ${str} ${dev_type} ${maj} ${min}"
  80. ;;
  81. "slink")
  82. local target=$(LC_ALL=C ls -l "${location}" | \
  83. gawk '{print $11}')
  84. str="${ftype} ${name} ${target} ${str}"
  85. ;;
  86. *)
  87. str="${ftype} ${name} ${str}"
  88. ;;
  89. esac
  90. echo "${str}"
  91. return 0
  92. }
  93. usage() {
  94. printf "Usage:\n"
  95. printf "$0 [ [-u <root_uid>] [-g <root_gid>] [-d | <cpio_source>] ] . . .\n"
  96. printf "\n"
  97. printf -- "-u <root_uid> User ID to map to user ID 0 (root).\n"
  98. printf " <root_uid> is only meaningful if <cpio_source>\n"
  99. printf " is a directory.\n"
  100. printf -- "-g <root_gid> Group ID to map to group ID 0 (root).\n"
  101. printf " <root_gid> is only meaningful if <cpio_source>\n"
  102. printf " is a directory.\n"
  103. printf "<cpio_source> File list or directory for cpio archive.\n"
  104. printf " If <cpio_source> is not provided then a\n"
  105. printf " a default list will be output.\n"
  106. printf -- "-d Output the default cpio list. If no <cpio_source>\n"
  107. printf " is given then the default cpio list will be output.\n"
  108. printf "\n"
  109. printf "All options may be repeated and are interpreted sequentially\n"
  110. printf "and immediately. -u and -g states are preserved across\n"
  111. printf "<cpio_source> options so an explicit \"-u 0 -g 0\" is required\n"
  112. printf "to reset the root/group mapping.\n"
  113. }
  114. build_list() {
  115. printf "\n#####################\n# $cpio_source\n"
  116. if [ -f "$cpio_source" ]; then
  117. print_mtime "$cpio_source"
  118. cat "$cpio_source"
  119. elif [ -d "$cpio_source" ]; then
  120. srcdir=$(echo "$cpio_source" | sed -e 's://*:/:g')
  121. dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n" 2>/dev/null)
  122. # If $dirlist is only one line, then the directory is empty
  123. if [ "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then
  124. print_mtime "$cpio_source"
  125. echo "${dirlist}" | \
  126. while read x; do
  127. parse ${x}
  128. done
  129. else
  130. # Failsafe in case directory is empty
  131. default_initramfs
  132. fi
  133. else
  134. echo " $0: Cannot open '$cpio_source'" >&2
  135. exit 1
  136. fi
  137. }
  138. root_uid=0
  139. root_gid=0
  140. while [ $# -gt 0 ]; do
  141. arg="$1"
  142. shift
  143. case "$arg" in
  144. "-u")
  145. root_uid="$1"
  146. shift
  147. ;;
  148. "-g")
  149. root_gid="$1"
  150. shift
  151. ;;
  152. "-d")
  153. default_list="$arg"
  154. default_initramfs
  155. ;;
  156. "-h")
  157. usage
  158. exit 0
  159. ;;
  160. *)
  161. case "$arg" in
  162. "-"*)
  163. printf "ERROR: unknown option \"$arg\"\n" >&2
  164. printf "If the filename validly begins with '-', then it must be prefixed\n" >&2
  165. printf "by './' so that it won't be interpreted as an option." >&2
  166. printf "\n" >&2
  167. usage >&2
  168. exit 1
  169. ;;
  170. *)
  171. cpio_source="$arg"
  172. build_list
  173. ;;
  174. esac
  175. ;;
  176. esac
  177. done
  178. # spit out the default cpio list if a source hasn't been specified
  179. [ -z "$cpio_source" -a -z "$default_list" ] && default_initramfs
  180. exit 0