]>
git.ipfire.org Git - thirdparty/linux.git/blob - usr/gen_initramfs.sh
2 # Copyright (C) Martin Schlemmer <azarah@nosferatu.za.org>
3 # Copyright (C) 2006 Sam Ravnborg <sam@ravnborg.org>
5 # Released under the terms of the GNU GPL
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
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.
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.
39 # awk style field access
40 # $1 - field number; rest is argument string
48 # symlink test must come before file test
49 if [ -L "${argv1}" ]; then
51 elif [ -f "${argv1}" ]; then
53 elif [ -d "${argv1}" ]; then
55 elif [ -b "${argv1}" -o -c "${argv1}" ]; then
57 elif [ -p "${argv1}" ]; then
59 elif [ -S "${argv1}" ]; then
71 my_mtime
=$
(find "$1" -printf "%T@\n" |
sort -r |
head -n 1)
74 echo "# Last modified: ${my_mtime}" >> ${output}
79 if [ -z "$dep_list" -o -L "$1" ]; then
82 echo "$1" |
sed 's/:/\\:/g; s/$/ \\/' >> $dep_list
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
90 local name
="/${location#${srcdir}}"
91 # change '//' into '/'
92 name
=$
(echo "$name" |
sed -e 's://*:/:g')
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}"
102 [ "${ftype}" = "invalid" ] && return 0
103 [ "${location}" = "${srcdir}" ] && return 0
107 str
="${ftype} ${name} ${location} ${str}"
110 local dev
="`LC_ALL=C ls -l "${location}"`"
111 local maj
=`field 5 ${dev}`
112 local min
=`field 6 ${dev}`
115 [ -b "${location}" ] && dev
="b" || dev
="c"
117 str
="${ftype} ${name} ${str} ${dev} ${maj} ${min}"
120 local target
=`readlink "${location}"`
121 str
="${ftype} ${name} ${target} ${str}"
124 str
="${ftype} ${name} ${str}"
128 echo "${str}" >> ${output}
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
144 printf "\n#####################\n# $1\n" >> ${output}
147 # process one directory (incl sub-directories)
151 srcdir
=$
(echo "$1" |
sed -e 's://*:/:g')
152 dirlist
=$
(find "${srcdir}" -printf "%p %m %U %G\n" | LANG
=C
sort)
154 # If $dirlist is only one line, then the directory is empty
155 if [ "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then
158 echo "${dirlist}" | \
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
173 is_cpio
="$(echo "$1" | sed 's/^.*\.cpio\(\..*\)\{0,1\}/cpio/')"
174 if [ $2 -eq 0 -a ${is_cpio} = "cpio" ]; then
176 echo "$1" |
grep -q '^.*\.cpio\..*' && is_cpio_compressed
="compressed"
177 [ -n "$dep_list" ] && echo "$1" >> $dep_list
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
190 elif [ -d "$1" ]; then
193 echo " ${prog}: Cannot open '$1'" >&2
207 compr
="gzip -n -9 -f"
209 while [ $# -gt 0 ]; do
213 "-l") # files included in initramfs - used by kbuild
215 echo "deps_initramfs := \\" > $dep_list
218 "-o") # generate compressed cpio image named $1
220 cpio_list
="$(mktemp ${TMPDIR:-/tmp}/cpiolist.XXXXXX)"
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"
243 "-u") # map $1 to uid=0 (root)
245 [ "$root_uid" = "-1" ] && root_uid
=$
(id
-u ||
echo 0)
248 "-g") # map $1 to gid=0 (root)
250 [ "$root_gid" = "-1" ] && root_gid
=$
(id
-g ||
echo 0)
262 *) # input file/dir - process it
263 input_file
"$arg" "$#"
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
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"
281 cpio_tfile
="$(mktemp ${TMPDIR:-/tmp}/cpiofile.XXXXXX)"
282 usr
/gen_init_cpio
$timestamp ${cpio_list} > ${cpio_tfile}
284 cpio_tfile
=${cpio_file}
287 if [ "${is_cpio_compressed}" = "compressed" ]; then
288 cat ${cpio_tfile} > ${output_file}
290 (cat ${cpio_tfile} | ${compr} - > ${output_file}) \
291 ||
(rm -f ${output_file} ; false
)
293 [ -z ${cpio_file} ] && rm ${cpio_tfile}