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
9 # This script assumes that gen_init_cpio is located in usr/ directory
17 $0 [-o <file>] [-l <dep_list>] [-u <uid>] [-g <gid>] {-d | <cpio_source>} ...
18 -o <file> Create initramfs file named <file> by using gen_init_cpio
19 -l <dep_list> Create dependency list named <dep_list>
20 -u <uid> User ID to map to user ID 0 (root).
21 <uid> is only meaningful if <cpio_source> is a
22 directory. "squash" forces all files to uid 0.
23 -g <gid> Group ID to map to group ID 0 (root).
24 <gid> is only meaningful if <cpio_source> is a
25 directory. "squash" forces all files to gid 0.
26 -d <date> Use date for all file mtime values
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.
31 All options except -o and -l may be repeated and are interpreted
32 sequentially and immediately. -u and -g states are preserved across
33 <cpio_source> options so an explicit "-u 0 -g 0" is required
34 to reset the root/group mapping.
38 # awk style field access
39 # $1 - field number; rest is argument string
47 # symlink test must come before file test
48 if [ -L "${argv1}" ]; then
50 elif [ -f "${argv1}" ]; then
52 elif [ -d "${argv1}" ]; then
54 elif [ -b "${argv1}" -o -c "${argv1}" ]; then
56 elif [ -p "${argv1}" ]; then
58 elif [ -S "${argv1}" ]; then
70 my_mtime=$(find "$1" -printf "%T@\n" | sort -r | head -n 1)
73 echo "# Last modified: ${my_mtime}" >> $cpio_list
78 if [ -z "$dep_list" -o -L "$1" ]; then
81 echo "$1" | sed 's/:/\\:/g; s/$/ \\/' >> $dep_list
84 # for each file print a line in following format
85 # <filetype> <name> <path to file> <octal mode> <uid> <gid>
86 # for links, devices etc the format differs. See gen_init_cpio for details
89 local name="/${location#${srcdir}}"
90 # change '//' into '/'
91 name=$(echo "$name" | sed -e 's://*:/:g')
95 local ftype=$(filetype "${location}")
96 # remap uid/gid to 0 if necessary
97 [ "$root_uid" = "squash" ] && uid=0 || [ "$uid" -eq "$root_uid" ] && uid=0
98 [ "$root_gid" = "squash" ] && gid=0 || [ "$gid" -eq "$root_gid" ] && gid=0
99 local str="${mode} ${uid} ${gid}"
101 [ "${ftype}" = "invalid" ] && return 0
102 [ "${location}" = "${srcdir}" ] && return 0
106 str="${ftype} ${name} ${location} ${str}"
109 local dev="`LC_ALL=C ls -l "${location}"`"
110 local maj=`field 5 ${dev}`
111 local min=`field 6 ${dev}`
114 [ -b "${location}" ] && dev="b" || dev="c"
116 str="${ftype} ${name} ${str} ${dev} ${maj} ${min}"
119 local target=`readlink "${location}"`
120 str="${ftype} ${name} ${target} ${str}"
123 str="${ftype} ${name} ${str}"
127 echo "${str}" >> $cpio_list
133 printf "ERROR: unknown option \"$arg\"\n" >&2
134 printf "If the filename validly begins with '-', " >&2
135 printf "then it must be prefixed\n" >&2
136 printf "by './' so that it won't be interpreted as an option." >&2
143 printf "\n#####################\n# $1\n" >> $cpio_list
146 # process one directory (incl sub-directories)
150 srcdir=$(echo "$1" | sed -e 's://*:/:g')
151 dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n" | LC_ALL=C sort)
153 # If $dirlist is only one line, then the directory is empty
154 if [ "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then
157 echo "${dirlist}" | \
168 # If a regular file is specified, assume it is in
169 # gen_init_cpio format
171 print_mtime "$1" >> $cpio_list
172 cat "$1" >> $cpio_list
173 if [ -n "$dep_list" ]; then
174 echo "$1 \\" >> $dep_list
175 cat "$1" | while read type dir file perm ; do
176 if [ "$type" = "file" ]; then
177 echo "$file \\" >> $dep_list
181 elif [ -d "$1" ]; then
182 # If a directory is specified then add all files in it to fs
185 echo " ${prog}: Cannot open '$1'" >&2
195 cpio_list=$(mktemp ${TMPDIR:-/tmp}/cpiolist.XXXXXX)
198 trap "rm -f $cpio_list" EXIT
200 while [ $# -gt 0 ]; do
204 "-l") # files included in initramfs - used by kbuild
206 echo "deps_initramfs := \\" > $dep_list
209 "-o") # generate cpio image named $1
213 "-u") # map $1 to uid=0 (root)
215 [ "$root_uid" = "-1" ] && root_uid=$(id -u || echo 0)
218 "-g") # map $1 to gid=0 (root)
220 [ "$root_gid" = "-1" ] && root_gid=$(id -g || echo 0)
223 "-d") # date for file mtimes
224 timestamp="$(date -d"$1" +%s || :)"
225 if test -n "$timestamp"; then
226 timestamp="-t $timestamp"
239 *) # input file/dir - process it
247 # If output_file is set we will generate cpio archive
248 # we are careful to delete tmp files
249 usr/gen_init_cpio $timestamp $cpio_list > $output