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