3 # Copyright (C) Michal Sojka <sojkam1@fel.cvut.cz>
5 # Includes pieces from gen_initramfs_list.sh:
7 # Copyright (C) Martin Schlemmer <azarah@nosferatu.za.org>
8 # Copyright (C) 2006 Sam Ravnborg <sam@ravnborg.org>
10 # Released under the terms of the GNU GPL
12 # Usage: gen_cpio rootfs.cpio "myapp->/bin/" "start.sh->/etc/init.d/S90startmyapp"
13 # gen_cpio --mkimage="-T ramdisk -A powerpc -O linux" ...
23 $0 [--mkimage="<mkimage params>"] [-u <uid>] [-g <gid>] {<cpio_source>}...
24 --gzip Compress the resulting image with gzip
25 --mkimage=... Pack the resulting image with U-Boot's "mkimage ..."
26 -u <uid> User ID to map to user ID 0 (root).
27 <uid> is only meaningful if <cpio_source> is a
28 directory. "squash" forces all files to uid 0.
29 Default: $root_uid (you)
30 -g <gid> Group ID to map to group ID 0 (root).
31 <gid> is only meaningful if <cpio_source> is a
32 directory. "squash" forces all files to gid 0.
33 Default: $root_gid (you)
35 - *.cpio: Included directly into the output
36 - directory: Content of the directory is included
38 - src/file->/dst/file: src/file appear in the output
40 - src/file->/dst/dir/: src/file appear in the output
43 All options except --mkimage may be repeated and are interpreted
44 sequentially and immediately. -u and -g states are preserved across
45 <cpio_source> options so an explicit "-u 0 -g 0" is required to reset
46 the root/group mapping.
50 while [ $# -gt 0 ]; do
56 mkimage=${1#--mkimage=}
67 if [ -n "$mkimage" -o -n "$gzip" ]; then
72 gen_cpio_from_list() {
73 [ "$cpio_list" ] || return 0
74 echo "$cpio_list" | LANG=C sort -u | gen_init_cpio -
78 # awk style field access
79 # $1 - field number; rest is argument string
87 # symlink test must come before file test
88 if [ -L "${argv1}" ]; then
90 elif [ -f "${argv1}" ]; then
92 elif [ -d "${argv1}" ]; then
94 elif [ -b "${argv1}" -o -c "${argv1}" ]; then
96 elif [ -p "${argv1}" ]; then
98 elif [ -S "${argv1}" ]; then
106 # for each file print a line in following format
107 # <filetype> <name> <path to file> <octal mode> <uid> <gid>
108 # for links, devices etc the format differs. See gen_init_cpio for details
111 local name="/${location#${srcdir}}"
112 # change '//' into '/'
113 name=$(echo "$name" | sed -e 's://*:/:g')
117 local ftype=$(filetype "${location}")
118 # remap uid/gid to 0 if necessary
119 [ "$root_uid" = "squash" ] && uid=0 || [ "$uid" -eq "$root_uid" ] && uid=0
120 [ "$root_gid" = "squash" ] && gid=0 || [ "$gid" -eq "$root_gid" ] && gid=0
121 local str="${mode} ${uid} ${gid}"
123 [ "${ftype}" = "invalid" ] && return 0
124 [ "${location}" = "${srcdir}" ] && return 0
128 str="${ftype} ${name} ${location} ${str}"
131 local dev=`LC_ALL=C ls -l "${location}"`
132 local maj=`field 5 ${dev}`
133 local min=`field 6 ${dev}`
136 [ -b "${location}" ] && dev="b" || dev="c"
138 str="${ftype} ${name} ${str} ${dev} ${maj} ${min}"
141 local target=`readlink "${location}"`
142 str="${ftype} ${name} ${target} ${str}"
145 str="${ftype} ${name} ${str}"
154 # process one directory (incl sub-directories)
156 srcdir=$(echo "$1" | sed -e 's://*:/:g')
157 dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n")
159 # If $dirlist is only one line, then the directory is empty
160 if [ "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then
161 echo "${dirlist}" | \
169 ends_with() { [ x"${1}" != x"${1%$2}" ]; }
175 if ends_with "$dst" "/"; then
176 dst="$dst$(basename $src)"
178 local dir=$(dirname $dst)
179 while [ -n "$dir" -a "$dir" != "." -a "$dir" != "/" ]; do
180 echo "dir $dir 0755 0 0"
183 echo "file $dst $src $(find $src -printf '%m') 0 0"
186 while [ $# -gt 0 ]; do
190 "-u") # map $1 to uid=0 (root)
194 "-g") # map $1 to gid=0 (root)
201 cpio_list=$(echo "$cpio_list"; gen_filelist $src $dst)
204 if [ -d "$arg" ]; then
205 cpio_list="$cpio_list$(gen_dirlist $arg)"
206 elif [ -f "$arg" ]; then
207 case "$(file -L $arg)" in
213 echo >&2 "Unsuported file format: $arg"
219 echo >&2 "No file: $arg"
227 if [ -n "$gzip" ]; then
228 if [ -n "$mkimage" ]; then
238 if [ -n "$mkimage" ]; then
241 mkimage $mkimage -d $tmp $tmp2 >&2