Blame view

scripts/gen_initramfs_list.sh 7.38 KB
153f01147   Jamey Sharp   scripts/gen_initr...
1
  #!/bin/sh
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
  # Copyright (C) Martin Schlemmer <azarah@nosferatu.za.org>
f6112ec27   Oleg Verych   [PATCH] kbuild sc...
3
  # Copyright (C) 2006 Sam Ravnborg <sam@ravnborg.org>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
  #
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
5
  # Released under the terms of the GNU GPL
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
6
  #
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
7
  # Generate a cpio packed initramfs. It uses gen_init_cpio to generate
a26ee60f9   Alain Knaff   bzip2/lzma: fix b...
8
  # the cpio archive, and then compresses it.
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
9
10
11
12
13
14
15
16
17
18
  # The script may also be used to generate the inputfile used for gen_init_cpio
  # This script assumes that gen_init_cpio is located in usr/ directory
  
  # error out on errors
  set -e
  
  usage() {
  cat << EOF
  Usage:
  $0 [-o <file>] [-u <uid>] [-g <gid>] {-d | <cpio_source>} ...
a26ee60f9   Alain Knaff   bzip2/lzma: fix b...
19
20
  	-o <file>      Create compressed initramfs file named <file> using
  		       gen_init_cpio and compressor depending on the extension
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
21
  	-u <uid>       User ID to map to user ID 0 (root).
4c6f2eb97   Mike Frysinger   kbuild: add suppo...
22
23
  		       <uid> is only meaningful if <cpio_source> is a
  		       directory.  "squash" forces all files to uid 0.
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
24
  	-g <gid>       Group ID to map to group ID 0 (root).
4c6f2eb97   Mike Frysinger   kbuild: add suppo...
25
26
  		       <gid> is only meaningful if <cpio_source> is a
  		       directory.  "squash" forces all files to gid 0.
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
27
  	<cpio_source>  File list or directory for cpio archive.
f6112ec27   Oleg Verych   [PATCH] kbuild sc...
28
  		       If <cpio_source> is a .cpio file it will be used
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
29
30
31
32
33
34
35
36
37
  		       as direct input to initramfs.
  	-d             Output the default cpio list.
  
  All options except -o and -l may be repeated and are interpreted
  sequentially and immediately.  -u and -g states are preserved across
  <cpio_source> options so an explicit "-u 0 -g 0" is required
  to reset the root/group mapping.
  EOF
  }
f6112ec27   Oleg Verych   [PATCH] kbuild sc...
38
39
40
41
42
  # awk style field access
  # $1 - field number; rest is argument string
  field() {
  	shift $1 ; echo $1
  }
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
43
44
45
46
  list_default_initramfs() {
  	# echo usr/kinit/kinit
  	:
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
48
  
  default_initramfs() {
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
49
  	cat <<-EOF >> ${output}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50
51
52
53
54
  		# This is a very simple, default initramfs
  
  		dir /dev 0755 0 0
  		nod /dev/console 0600 0 0 c 5 1
  		dir /root 0700 0 0
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
55
56
  		# file /kinit usr/kinit/kinit 0755 0 0
  		# slink /init kinit 0755 0 0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  	EOF
  }
  
  filetype() {
  	local argv1="$1"
  
  	# symlink test must come before file test
  	if [ -L "${argv1}" ]; then
  		echo "slink"
  	elif [ -f "${argv1}" ]; then
  		echo "file"
  	elif [ -d "${argv1}" ]; then
  		echo "dir"
  	elif [ -b "${argv1}" -o -c "${argv1}" ]; then
  		echo "nod"
  	elif [ -p "${argv1}" ]; then
  		echo "pipe"
  	elif [ -S "${argv1}" ]; then
  		echo "sock"
  	else
  		echo "invalid"
  	fi
  	return 0
  }
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
81
82
83
  list_print_mtime() {
  	:
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
84
  print_mtime() {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
  	local my_mtime="0"
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
86
87
88
  	if [ -e "$1" ]; then
  		my_mtime=$(find "$1" -printf "%T@
  " | sort -r | head -n 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
  	fi
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
90
91
92
93
94
95
  
  	echo "# Last modified: ${my_mtime}" >> ${output}
  	echo "" >> ${output}
  }
  
  list_parse() {
9e5ec8615   Maxime Bizon   kbuild: fix spuri...
96
  	[ ! -L "$1" ] && echo "$1 \\" || :
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
  }
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
98
99
100
  # for each file print a line in following format
  # <filetype> <name> <path to file> <octal mode> <uid> <gid>
  # for links, devices etc the format differs. See gen_init_cpio for details
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
102
  parse() {
  	local location="$1"
153f01147   Jamey Sharp   scripts/gen_initr...
103
  	local name="/${location#${srcdir}}"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
  	# change '//' into '/'
153f01147   Jamey Sharp   scripts/gen_initr...
105
  	name=$(echo "$name" | sed -e 's://*:/:g')
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106
107
108
109
110
  	local mode="$2"
  	local uid="$3"
  	local gid="$4"
  	local ftype=$(filetype "${location}")
  	# remap uid/gid to 0 if necessary
4c6f2eb97   Mike Frysinger   kbuild: add suppo...
111
112
  	[ "$root_uid" = "squash" ] && uid=0 || [ "$uid" -eq "$root_uid" ] && uid=0
  	[ "$root_gid" = "squash" ] && gid=0 || [ "$gid" -eq "$root_gid" ] && gid=0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113
  	local str="${mode} ${uid} ${gid}"
153f01147   Jamey Sharp   scripts/gen_initr...
114
115
  	[ "${ftype}" = "invalid" ] && return 0
  	[ "${location}" = "${srcdir}" ] && return 0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
117
118
119
120
121
  
  	case "${ftype}" in
  		"file")
  			str="${ftype} ${name} ${location} ${str}"
  			;;
  		"nod")
f6112ec27   Oleg Verych   [PATCH] kbuild sc...
122
123
124
125
126
127
128
129
  			local dev=`LC_ALL=C ls -l "${location}"`
  			local maj=`field 5 ${dev}`
  			local min=`field 6 ${dev}`
  			maj=${maj%,}
  
  			[ -b "${location}" ] && dev="b" || dev="c"
  
  			str="${ftype} ${name} ${str} ${dev} ${maj} ${min}"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
130
131
  			;;
  		"slink")
b5a5e4c73   Felix Fietkau   kbuild: fix unpor...
132
  			local target=`readlink "${location}"`
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
134
135
136
137
138
  			str="${ftype} ${name} ${target} ${str}"
  			;;
  		*)
  			str="${ftype} ${name} ${str}"
  			;;
  	esac
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
139
  	echo "${str}" >> ${output}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140
141
142
  
  	return 0
  }
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  unknown_option() {
  	printf "ERROR: unknown option \"$arg\"
  " >&2
  	printf "If the filename validly begins with '-', " >&2
  	printf "then it must be prefixed
  " >&2
  	printf "by './' so that it won't be interpreted as an option." >&2
  	printf "
  " >&2
  	usage >&2
  	exit 1
  }
  
  list_header() {
a26d79ca8   Thomas Chou   [PATCH] initramfs...
157
  	:
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  }
  
  header() {
  	printf "
  #####################
  # $1
  " >> ${output}
  }
  
  # process one directory (incl sub-directories)
  dir_filelist() {
  	${dep_list}header "$1"
  
  	srcdir=$(echo "$1" | sed -e 's://*:/:g')
eda890af7   Michael Ellerman   kbuild: propagate...
172
173
  	dirlist=$(find "${srcdir}" -printf "%p %m %U %G
  ")
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
174
175
176
177
178
179
180
181
182
183
  
  	# If $dirlist is only one line, then the directory is empty
  	if [  "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then
  		${dep_list}print_mtime "$1"
  
  		echo "${dirlist}" | \
  		while read x; do
  			${dep_list}parse ${x}
  		done
  	fi
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
  }
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
185
186
187
188
189
190
191
  # if only one file is specified and it is .cpio file then use it direct as fs
  # if a directory is specified then add all files in given direcotry to fs
  # if a regular file is specified assume it is in gen_initramfs format
  input_file() {
  	source="$1"
  	if [ -f "$1" ]; then
  		${dep_list}header "$1"
c299ec2d8   Alex Landau   kbuild: handle co...
192
  		is_cpio="$(echo "$1" | sed 's/^.*\.cpio\(\..*\)\?/cpio/')"
153f01147   Jamey Sharp   scripts/gen_initr...
193
  		if [ $2 -eq 0 -a ${is_cpio} = "cpio" ]; then
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
194
  			cpio_file=$1
c299ec2d8   Alex Landau   kbuild: handle co...
195
  			echo "$1" | grep -q '^.*\.cpio\..*' && is_cpio_compressed="compressed"
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
196
197
198
199
200
201
  			[ ! -z ${dep_list} ] && echo "$1"
  			return 0
  		fi
  		if [ -z ${dep_list} ]; then
  			print_mtime "$1" >> ${output}
  			cat "$1"         >> ${output}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
202
  		else
b83419363   Jason Gunthorpe   kbuild: Include g...
203
  		        echo "$1 \\"
46ed981d5   Sam Ravnborg   kbuild: fix gen_i...
204
  			cat "$1" | while read type dir file perm ; do
153f01147   Jamey Sharp   scripts/gen_initr...
205
  				if [ "$type" = "file" ]; then
46ed981d5   Sam Ravnborg   kbuild: fix gen_i...
206
207
208
  					echo "$file \\";
  				fi
  			done
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209
  		fi
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
210
211
  	elif [ -d "$1" ]; then
  		dir_filelist "$1"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
212
  	else
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
213
  		echo "  ${prog}: Cannot open '$1'" >&2
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214
215
216
  		exit 1
  	fi
  }
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
217
  prog=$0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
219
  root_uid=0
  root_gid=0
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
220
221
222
223
224
  dep_list=
  cpio_file=
  cpio_list=
  output="/dev/stdout"
  output_file=""
c299ec2d8   Alex Landau   kbuild: handle co...
225
  is_cpio_compressed=
6ae9ecb86   Michal Marek   kbuild: Call gzip...
226
  compr="gzip -n -9 -f"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227

d39a206bc   Sam Ravnborg   kbuild: rebuild i...
228
229
230
231
  arg="$1"
  case "$arg" in
  	"-l")	# files included in initramfs - used by kbuild
  		dep_list="list_"
b83419363   Jason Gunthorpe   kbuild: Include g...
232
  		echo "deps_initramfs := $0 \\"
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
233
234
  		shift
  		;;
a26ee60f9   Alain Knaff   bzip2/lzma: fix b...
235
  	"-o")	# generate compressed cpio image named $1
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
236
237
238
239
  		shift
  		output_file="$1"
  		cpio_list="$(mktemp ${TMPDIR:-/tmp}/cpiolist.XXXXXX)"
  		output=${cpio_list}
6ae9ecb86   Michal Marek   kbuild: Call gzip...
240
  		echo "$output_file" | grep -q "\.gz$" && compr="gzip -n -9 -f"
a26ee60f9   Alain Knaff   bzip2/lzma: fix b...
241
242
  		echo "$output_file" | grep -q "\.bz2$" && compr="bzip2 -9 -f"
  		echo "$output_file" | grep -q "\.lzma$" && compr="lzma -9 -f"
3ebe12439   Lasse Collin   decompressors: ad...
243
244
  		echo "$output_file" | grep -q "\.xz$" && \
  				compr="xz --check=crc32 --lzma2=dict=1MiB"
2a2a400f6   Albin Tonnerre   initramfs: add su...
245
  		echo "$output_file" | grep -q "\.lzo$" && compr="lzop -9 -f"
a26ee60f9   Alain Knaff   bzip2/lzma: fix b...
246
  		echo "$output_file" | grep -q "\.cpio$" && compr="cat"
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
247
248
249
  		shift
  		;;
  esac
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
250
251
252
253
  while [ $# -gt 0 ]; do
  	arg="$1"
  	shift
  	case "$arg" in
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
254
  		"-u")	# map $1 to uid=0 (root)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255
256
257
  			root_uid="$1"
  			shift
  			;;
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
258
  		"-g")	# map $1 to gid=0 (root)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
259
260
261
  			root_gid="$1"
  			shift
  			;;
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
262
  		"-d")	# display default initramfs list
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
263
  			default_list="$arg"
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
264
  			${dep_list}default_initramfs
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
265
266
267
268
269
270
271
272
  			;;
  		"-h")
  			usage
  			exit 0
  			;;
  		*)
  			case "$arg" in
  				"-"*)
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
273
  					unknown_option
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
274
  					;;
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
275
276
  				*)	# input file/dir - process it
  					input_file "$arg" "$#"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
277
278
279
280
281
  					;;
  			esac
  			;;
  	esac
  done
a26ee60f9   Alain Knaff   bzip2/lzma: fix b...
282
  # If output_file is set we will generate cpio archive and compress it
25985edce   Lucas De Marchi   Fix common misspe...
283
  # we are careful to delete tmp files
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
284
285
  if [ ! -z ${output_file} ]; then
  	if [ -z ${cpio_file} ]; then
a8b8017c3   Michal Marek   initramfs: Use KB...
286
287
288
289
290
291
292
  		timestamp=
  		if test -n "$KBUILD_BUILD_TIMESTAMP"; then
  			timestamp="$(date -d"$KBUILD_BUILD_TIMESTAMP" +%s || :)"
  			if test -n "$timestamp"; then
  				timestamp="-t $timestamp"
  			fi
  		fi
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
293
  		cpio_tfile="$(mktemp ${TMPDIR:-/tmp}/cpiofile.XXXXXX)"
a8b8017c3   Michal Marek   initramfs: Use KB...
294
  		usr/gen_init_cpio $timestamp ${cpio_list} > ${cpio_tfile}
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
295
296
297
298
  	else
  		cpio_tfile=${cpio_file}
  	fi
  	rm ${cpio_list}
c299ec2d8   Alex Landau   kbuild: handle co...
299
300
301
  	if [ "${is_cpio_compressed}" = "compressed" ]; then
  		cat ${cpio_tfile} > ${output_file}
  	else
ab59d3b70   Alain Knaff   bzip2/lzma: don't...
302
303
  		(cat ${cpio_tfile} | ${compr}  - > ${output_file}) \
  		|| (rm -f ${output_file} ; false)
c299ec2d8   Alex Landau   kbuild: handle co...
304
  	fi
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
305
306
  	[ -z ${cpio_file} ] && rm ${cpio_tfile}
  fi
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
  exit 0