b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 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 | |
| 12 | # error out on errors |
| 13 | set -e |
| 14 | |
| 15 | usage() { |
| 16 | cat << EOF |
| 17 | Usage: |
| 18 | $0 [-o <file>] [-u <uid>] [-g <gid>] {-d | <cpio_source>} ... |
| 19 | -o <file> Create compressed initramfs file named <file> using |
| 20 | gen_init_cpio and compressor depending on the extension |
| 21 | -u <uid> User ID to map to user ID 0 (root). |
| 22 | <uid> is only meaningful if <cpio_source> is a |
| 23 | directory. "squash" forces all files to uid 0. |
| 24 | -g <gid> Group ID to map to group ID 0 (root). |
| 25 | <gid> is only meaningful if <cpio_source> is a |
| 26 | directory. "squash" forces all files to gid 0. |
| 27 | <cpio_source> File list or directory for cpio archive. |
| 28 | If <cpio_source> is a .cpio file it will be used |
| 29 | as direct input to initramfs. |
| 30 | -d Output the default cpio list. |
| 31 | |
| 32 | All options except -o and -l may be repeated and are interpreted |
| 33 | sequentially and immediately. -u and -g states are preserved across |
| 34 | <cpio_source> options so an explicit "-u 0 -g 0" is required |
| 35 | to reset the root/group mapping. |
| 36 | EOF |
| 37 | } |
| 38 | |
| 39 | # awk style field access |
| 40 | # $1 - field number; rest is argument string |
| 41 | field() { |
| 42 | shift $1 ; echo $1 |
| 43 | } |
| 44 | |
| 45 | list_default_initramfs() { |
| 46 | # echo usr/kinit/kinit |
| 47 | : |
| 48 | } |
| 49 | |
| 50 | default_initramfs() { |
| 51 | cat <<-EOF >> ${output} |
| 52 | # This is a very simple, default initramfs |
| 53 | |
| 54 | dir /dev 0755 0 0 |
| 55 | nod /dev/console 0600 0 0 c 5 1 |
| 56 | dir /root 0700 0 0 |
| 57 | # file /kinit usr/kinit/kinit 0755 0 0 |
| 58 | # slink /init kinit 0755 0 0 |
| 59 | EOF |
| 60 | } |
| 61 | |
| 62 | list_openwrt_initramfs() { |
| 63 | : |
| 64 | } |
| 65 | |
| 66 | openwrt_initramfs() { |
| 67 | # make sure that /dev/console exists |
| 68 | cat <<-EOF >> ${output} |
| 69 | dir /dev 0755 0 0 |
| 70 | nod /dev/console 0600 0 0 c 5 1 |
| 71 | EOF |
| 72 | } |
| 73 | |
| 74 | filetype() { |
| 75 | local argv1="$1" |
| 76 | |
| 77 | # symlink test must come before file test |
| 78 | if [ -L "${argv1}" ]; then |
| 79 | echo "slink" |
| 80 | elif [ -f "${argv1}" ]; then |
| 81 | echo "file" |
| 82 | elif [ -d "${argv1}" ]; then |
| 83 | echo "dir" |
| 84 | elif [ -b "${argv1}" -o -c "${argv1}" ]; then |
| 85 | echo "nod" |
| 86 | elif [ -p "${argv1}" ]; then |
| 87 | echo "pipe" |
| 88 | elif [ -S "${argv1}" ]; then |
| 89 | echo "sock" |
| 90 | else |
| 91 | echo "invalid" |
| 92 | fi |
| 93 | return 0 |
| 94 | } |
| 95 | |
| 96 | list_print_mtime() { |
| 97 | : |
| 98 | } |
| 99 | |
| 100 | print_mtime() { |
| 101 | local my_mtime="0" |
| 102 | |
| 103 | if [ -e "$1" ]; then |
| 104 | my_mtime=$(find "$1" -printf "%T@\n" | sort -r | head -n 1) |
| 105 | fi |
| 106 | |
| 107 | echo "# Last modified: ${my_mtime}" >> ${output} |
| 108 | echo "" >> ${output} |
| 109 | } |
| 110 | |
| 111 | list_parse() { |
| 112 | if [ -L "$1" ]; then |
| 113 | return |
| 114 | fi |
| 115 | echo "$1" | sed 's/:/\\:/g; s/$/ \\/' |
| 116 | } |
| 117 | |
| 118 | # for each file print a line in following format |
| 119 | # <filetype> <name> <path to file> <octal mode> <uid> <gid> |
| 120 | # for links, devices etc the format differs. See gen_init_cpio for details |
| 121 | parse() { |
| 122 | local location="$1" |
| 123 | local name="/${location#${srcdir}}" |
| 124 | # change '//' into '/' |
| 125 | name=$(echo "$name" | sed -e 's://*:/:g') |
| 126 | local mode="$2" |
| 127 | local uid="$3" |
| 128 | local gid="$4" |
| 129 | local ftype=$(filetype "${location}") |
| 130 | # remap uid/gid to 0 if necessary |
| 131 | [ "$root_uid" = "squash" ] && uid=0 || [ "$uid" -eq "$root_uid" ] && uid=0 |
| 132 | [ "$root_gid" = "squash" ] && gid=0 || [ "$gid" -eq "$root_gid" ] && gid=0 |
| 133 | local str="${mode} ${uid} ${gid}" |
| 134 | |
| 135 | [ "${ftype}" = "invalid" ] && return 0 |
| 136 | [ "${location}" = "${srcdir}" ] && return 0 |
| 137 | |
| 138 | case "${ftype}" in |
| 139 | "file") |
| 140 | str="${ftype} ${name} ${location} ${str}" |
| 141 | ;; |
| 142 | "nod") |
| 143 | local dev="`LC_ALL=C ls -l "${location}"`" |
| 144 | local maj=`field 5 ${dev}` |
| 145 | local min=`field 6 ${dev}` |
| 146 | maj=${maj%,} |
| 147 | |
| 148 | [ -b "${location}" ] && dev="b" || dev="c" |
| 149 | |
| 150 | str="${ftype} ${name} ${str} ${dev} ${maj} ${min}" |
| 151 | ;; |
| 152 | "slink") |
| 153 | local target=`readlink "${location}"` |
| 154 | str="${ftype} ${name} ${target} ${str}" |
| 155 | ;; |
| 156 | *) |
| 157 | str="${ftype} ${name} ${str}" |
| 158 | ;; |
| 159 | esac |
| 160 | |
| 161 | echo "${str}" >> ${output} |
| 162 | |
| 163 | return 0 |
| 164 | } |
| 165 | |
| 166 | unknown_option() { |
| 167 | printf "ERROR: unknown option \"$arg\"\n" >&2 |
| 168 | printf "If the filename validly begins with '-', " >&2 |
| 169 | printf "then it must be prefixed\n" >&2 |
| 170 | printf "by './' so that it won't be interpreted as an option." >&2 |
| 171 | printf "\n" >&2 |
| 172 | usage >&2 |
| 173 | exit 1 |
| 174 | } |
| 175 | |
| 176 | list_header() { |
| 177 | : |
| 178 | } |
| 179 | |
| 180 | header() { |
| 181 | printf "\n#####################\n# $1\n" >> ${output} |
| 182 | } |
| 183 | |
| 184 | # process one directory (incl sub-directories) |
| 185 | dir_filelist() { |
| 186 | ${dep_list}header "$1" |
| 187 | |
| 188 | srcdir=$(echo "$1" | sed -e 's://*:/:g') |
| 189 | dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n" | LANG=C sort) |
| 190 | |
| 191 | # If $dirlist is only one line, then the directory is empty |
| 192 | if [ "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then |
| 193 | ${dep_list}print_mtime "$1" |
| 194 | |
| 195 | ${dep_list}openwrt_initramfs |
| 196 | |
| 197 | echo "${dirlist}" | \ |
| 198 | while read x; do |
| 199 | ${dep_list}parse ${x} |
| 200 | done |
| 201 | fi |
| 202 | } |
| 203 | |
| 204 | # if only one file is specified and it is .cpio file then use it direct as fs |
| 205 | # if a directory is specified then add all files in given direcotry to fs |
| 206 | # if a regular file is specified assume it is in gen_initramfs format |
| 207 | input_file() { |
| 208 | source="$1" |
| 209 | if [ -f "$1" ]; then |
| 210 | ${dep_list}header "$1" |
| 211 | is_cpio="$(echo "$1" | sed 's/^.*\.cpio\(\..*\)\{0,1\}/cpio/')" |
| 212 | if [ $2 -eq 0 -a ${is_cpio} = "cpio" ]; then |
| 213 | cpio_file=$1 |
| 214 | echo "$1" | grep -q '^.*\.cpio\..*' && is_cpio_compressed="compressed" |
| 215 | [ ! -z ${dep_list} ] && echo "$1" |
| 216 | return 0 |
| 217 | fi |
| 218 | if [ -z ${dep_list} ]; then |
| 219 | print_mtime "$1" >> ${output} |
| 220 | cat "$1" >> ${output} |
| 221 | else |
| 222 | echo "$1 \\" |
| 223 | cat "$1" | while read type dir file perm ; do |
| 224 | if [ "$type" = "file" ]; then |
| 225 | echo "$file \\"; |
| 226 | fi |
| 227 | done |
| 228 | fi |
| 229 | elif [ -d "$1" ]; then |
| 230 | dir_filelist "$1" |
| 231 | else |
| 232 | echo " ${prog}: Cannot open '$1'" >&2 |
| 233 | exit 1 |
| 234 | fi |
| 235 | } |
| 236 | |
| 237 | prog=$0 |
| 238 | root_uid=0 |
| 239 | root_gid=0 |
| 240 | dep_list= |
| 241 | cpio_file= |
| 242 | cpio_list= |
| 243 | output="/dev/stdout" |
| 244 | output_file="" |
| 245 | is_cpio_compressed= |
| 246 | compr="gzip -n -9 -f -" |
| 247 | |
| 248 | arg="$1" |
| 249 | case "$arg" in |
| 250 | "-l") # files included in initramfs - used by kbuild |
| 251 | dep_list="list_" |
| 252 | echo "deps_initramfs := $0 \\" |
| 253 | shift |
| 254 | ;; |
| 255 | "-o") # generate compressed cpio image named $1 |
| 256 | shift |
| 257 | output_file="$1" |
| 258 | cpio_list="$(mktemp ${TMPDIR:-/tmp}/cpiolist.XXXXXX)" |
| 259 | output=${cpio_list} |
| 260 | echo "$output_file" | grep -q "\.gz$" \ |
| 261 | && [ -x "`which gzip 2> /dev/null`" ] \ |
| 262 | && compr="gzip -n -9 -f -" |
| 263 | echo "$output_file" | grep -q "\.bz2$" \ |
| 264 | && [ -x "`which bzip2 2> /dev/null`" ] \ |
| 265 | && compr="bzip2 -9 -f -" |
| 266 | echo "$output_file" | grep -q "\.lzma$" \ |
| 267 | && [ -x "`which lzma 2> /dev/null`" ] \ |
| 268 | && compr="lzma e -d20 -lc1 -lp2 -pb2 -eos -si -so" |
| 269 | echo "$output_file" | grep -q "\.xz$" \ |
| 270 | && [ -x "`which xz 2> /dev/null`" ] \ |
| 271 | && compr="xz --check=crc32 --lzma2=dict=1MiB" |
| 272 | echo "$output_file" | grep -q "\.lzo$" \ |
| 273 | && [ -x "`which lzop 2> /dev/null`" ] \ |
| 274 | && compr="lzop -9 -f" |
| 275 | echo "$output_file" | grep -q "\.lz4$" \ |
| 276 | && [ -x "`which lz4 2> /dev/null`" ] \ |
| 277 | && compr="lz4 -l -9 -f" |
| 278 | echo "$output_file" | grep -q "\.cpio$" && compr="cat" |
| 279 | shift |
| 280 | ;; |
| 281 | esac |
| 282 | while [ $# -gt 0 ]; do |
| 283 | arg="$1" |
| 284 | shift |
| 285 | case "$arg" in |
| 286 | "-u") # map $1 to uid=0 (root) |
| 287 | root_uid="$1" |
| 288 | [ "$root_uid" = "-1" ] && root_uid=$(id -u || echo 0) |
| 289 | shift |
| 290 | ;; |
| 291 | "-g") # map $1 to gid=0 (root) |
| 292 | root_gid="$1" |
| 293 | [ "$root_gid" = "-1" ] && root_gid=$(id -g || echo 0) |
| 294 | shift |
| 295 | ;; |
| 296 | "-d") # display default initramfs list |
| 297 | default_list="$arg" |
| 298 | ${dep_list}default_initramfs |
| 299 | ;; |
| 300 | "-h") |
| 301 | usage |
| 302 | exit 0 |
| 303 | ;; |
| 304 | *) |
| 305 | case "$arg" in |
| 306 | "-"*) |
| 307 | unknown_option |
| 308 | ;; |
| 309 | *) # input file/dir - process it |
| 310 | input_file "$arg" "$#" |
| 311 | ;; |
| 312 | esac |
| 313 | ;; |
| 314 | esac |
| 315 | done |
| 316 | |
| 317 | # If output_file is set we will generate cpio archive and compress it |
| 318 | # we are careful to delete tmp files |
| 319 | if [ ! -z ${output_file} ]; then |
| 320 | if [ -z ${cpio_file} ]; then |
| 321 | timestamp= |
| 322 | if test -n "$KBUILD_BUILD_TIMESTAMP"; then |
| 323 | timestamp="$(date -d"$KBUILD_BUILD_TIMESTAMP" +%s || :)" |
| 324 | if test -n "$timestamp"; then |
| 325 | timestamp="-t $timestamp" |
| 326 | fi |
| 327 | fi |
| 328 | cpio_tfile="$(mktemp ${TMPDIR:-/tmp}/cpiofile.XXXXXX)" |
| 329 | usr/gen_init_cpio $timestamp ${cpio_list} > ${cpio_tfile} |
| 330 | else |
| 331 | cpio_tfile=${cpio_file} |
| 332 | fi |
| 333 | rm ${cpio_list} |
| 334 | if [ "${is_cpio_compressed}" = "compressed" ]; then |
| 335 | cat ${cpio_tfile} > ${output_file} |
| 336 | else |
| 337 | (cat ${cpio_tfile} | ${compr} > ${output_file}) \ |
| 338 | || (rm -f ${output_file} ; false) |
| 339 | fi |
| 340 | [ -z ${cpio_file} ] && rm ${cpio_tfile} |
| 341 | fi |
| 342 | exit 0 |