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