Blame view

usr/gen_initramfs.sh 5.71 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
65e00e04e   Masahiro Yamada   initramfs: refact...
8
  # the cpio archive.
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
9
10
11
12
13
14
15
16
  # This script assumes that gen_init_cpio is located in usr/ directory
  
  # error out on errors
  set -e
  
  usage() {
  cat << EOF
  Usage:
966809759   Masahiro Yamada   initramfs: genera...
17
  $0 [-o <file>] [-l <dep_list>] [-u <uid>] [-g <gid>] {-d | <cpio_source>} ...
65e00e04e   Masahiro Yamada   initramfs: refact...
18
  	-o <file>      Create initramfs file named <file> by using gen_init_cpio
966809759   Masahiro Yamada   initramfs: genera...
19
  	-l <dep_list>  Create dependency list named <dep_list>
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
20
  	-u <uid>       User ID to map to user ID 0 (root).
4c6f2eb97   Mike Frysinger   kbuild: add suppo...
21
22
  		       <uid> is only meaningful if <cpio_source> is a
  		       directory.  "squash" forces all files to uid 0.
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
23
  	-g <gid>       Group ID to map to group ID 0 (root).
4c6f2eb97   Mike Frysinger   kbuild: add suppo...
24
25
  		       <gid> is only meaningful if <cpio_source> is a
  		       directory.  "squash" forces all files to gid 0.
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
26
  	<cpio_source>  File list or directory for cpio archive.
f6112ec27   Oleg Verych   [PATCH] kbuild sc...
27
  		       If <cpio_source> is a .cpio file it will be used
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
28
  		       as direct input to initramfs.
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
29
30
31
32
33
34
35
  
  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...
36
37
38
39
40
  # awk style field access
  # $1 - field number; rest is argument string
  field() {
  	shift $1 ; echo $1
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  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
  }
  
  print_mtime() {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
  	local my_mtime="0"
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
65
66
67
  	if [ -e "$1" ]; then
  		my_mtime=$(find "$1" -printf "%T@
  " | sort -r | head -n 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
68
  	fi
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
69

469e87e89   Masahiro Yamada   gen_initramfs.sh:...
70
71
  	echo "# Last modified: ${my_mtime}" >> $cpio_list
  	echo "" >> $cpio_list
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
72
73
74
  }
  
  list_parse() {
966809759   Masahiro Yamada   initramfs: genera...
75
  	if [ -z "$dep_list" -o -L "$1" ]; then
590abbdd2   Michal Marek   initramfs: Escape...
76
77
  		return
  	fi
966809759   Masahiro Yamada   initramfs: genera...
78
  	echo "$1" | sed 's/:/\\:/g; s/$/ \\/' >> $dep_list
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
79
  }
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
80
81
82
  # 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
83
84
  parse() {
  	local location="$1"
153f01147   Jamey Sharp   scripts/gen_initr...
85
  	local name="/${location#${srcdir}}"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
  	# change '//' into '/'
153f01147   Jamey Sharp   scripts/gen_initr...
87
  	name=$(echo "$name" | sed -e 's://*:/:g')
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88
89
90
91
92
  	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...
93
94
  	[ "$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
95
  	local str="${mode} ${uid} ${gid}"
153f01147   Jamey Sharp   scripts/gen_initr...
96
97
  	[ "${ftype}" = "invalid" ] && return 0
  	[ "${location}" = "${srcdir}" ] && return 0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
99
100
101
102
103
  
  	case "${ftype}" in
  		"file")
  			str="${ftype} ${name} ${location} ${str}"
  			;;
  		"nod")
cc976614f   Masahiro Yamada   gen_initramfs_lis...
104
  			local dev="`LC_ALL=C ls -l "${location}"`"
f6112ec27   Oleg Verych   [PATCH] kbuild sc...
105
106
107
108
109
110
111
  			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
112
113
  			;;
  		"slink")
b5a5e4c73   Felix Fietkau   kbuild: fix unpor...
114
  			local target=`readlink "${location}"`
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115
116
117
118
119
120
  			str="${ftype} ${name} ${target} ${str}"
  			;;
  		*)
  			str="${ftype} ${name} ${str}"
  			;;
  	esac
469e87e89   Masahiro Yamada   gen_initramfs.sh:...
121
  	echo "${str}" >> $cpio_list
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
123
124
  
  	return 0
  }
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
125
126
127
128
129
130
131
132
133
134
135
136
  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
  }
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
137
  header() {
469e87e89   Masahiro Yamada   gen_initramfs.sh:...
138
139
140
141
  	printf "
  #####################
  # $1
  " >> $cpio_list
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
142
143
144
145
  }
  
  # process one directory (incl sub-directories)
  dir_filelist() {
966809759   Masahiro Yamada   initramfs: genera...
146
  	header "$1"
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
147
148
  
  	srcdir=$(echo "$1" | sed -e 's://*:/:g')
f55f2328b   Andrzej Pietrasiewicz   kbuild: make sort...
149
150
  	dirlist=$(find "${srcdir}" -printf "%p %m %U %G
  " | LANG=C sort)
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
151
152
153
  
  	# If $dirlist is only one line, then the directory is empty
  	if [  "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then
966809759   Masahiro Yamada   initramfs: genera...
154
  		print_mtime "$1"
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
155
156
157
  
  		echo "${dirlist}" | \
  		while read x; do
966809759   Masahiro Yamada   initramfs: genera...
158
159
  			list_parse $x
  			parse $x
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
160
161
  		done
  	fi
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
162
  }
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
163
164
165
  input_file() {
  	source="$1"
  	if [ -f "$1" ]; then
65e00e04e   Masahiro Yamada   initramfs: refact...
166
167
  		# If a regular file is specified, assume it is in
  		# gen_init_cpio format
966809759   Masahiro Yamada   initramfs: genera...
168
  		header "$1"
469e87e89   Masahiro Yamada   gen_initramfs.sh:...
169
170
  		print_mtime "$1" >> $cpio_list
  		cat "$1"         >> $cpio_list
966809759   Masahiro Yamada   initramfs: genera...
171
172
  		if [ -n "$dep_list" ]; then
  		        echo "$1 \\"  >> $dep_list
46ed981d5   Sam Ravnborg   kbuild: fix gen_i...
173
  			cat "$1" | while read type dir file perm ; do
153f01147   Jamey Sharp   scripts/gen_initr...
174
  				if [ "$type" = "file" ]; then
966809759   Masahiro Yamada   initramfs: genera...
175
  					echo "$file \\" >> $dep_list
46ed981d5   Sam Ravnborg   kbuild: fix gen_i...
176
177
  				fi
  			done
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178
  		fi
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
179
  	elif [ -d "$1" ]; then
65e00e04e   Masahiro Yamada   initramfs: refact...
180
  		# If a directory is specified then add all files in it to fs
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
181
  		dir_filelist "$1"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
  	else
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
183
  		echo "  ${prog}: Cannot open '$1'" >&2
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
185
186
  		exit 1
  	fi
  }
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
187
  prog=$0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188
189
  root_uid=0
  root_gid=0
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
190
  dep_list=
469e87e89   Masahiro Yamada   gen_initramfs.sh:...
191
  cpio_list=$(mktemp ${TMPDIR:-/tmp}/cpiolist.XXXXXX)
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
192
  output="/dev/stdout"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
193

7168965ec   Masahiro Yamada   gen_initramfs.sh:...
194
  trap "rm -f $cpio_list" EXIT
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
195
196
197
198
  while [ $# -gt 0 ]; do
  	arg="$1"
  	shift
  	case "$arg" in
966809759   Masahiro Yamada   initramfs: genera...
199
200
201
202
203
  		"-l")	# files included in initramfs - used by kbuild
  			dep_list="$1"
  			echo "deps_initramfs := \\" > $dep_list
  			shift
  			;;
65e00e04e   Masahiro Yamada   initramfs: refact...
204
205
  		"-o")	# generate cpio image named $1
  			output="$1"
966809759   Masahiro Yamada   initramfs: genera...
206
207
  			shift
  			;;
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
208
  		"-u")	# map $1 to uid=0 (root)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209
  			root_uid="$1"
595a22ace   Rob Landley   scripts/gen_initr...
210
  			[ "$root_uid" = "-1" ] && root_uid=$(id -u || echo 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
211
212
  			shift
  			;;
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
213
  		"-g")	# map $1 to gid=0 (root)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214
  			root_gid="$1"
595a22ace   Rob Landley   scripts/gen_initr...
215
  			[ "$root_gid" = "-1" ] && root_gid=$(id -g || echo 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
216
217
  			shift
  			;;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
219
220
221
222
223
224
  		"-h")
  			usage
  			exit 0
  			;;
  		*)
  			case "$arg" in
  				"-"*)
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
225
  					unknown_option
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
226
  					;;
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
227
  				*)	# input file/dir - process it
65e00e04e   Masahiro Yamada   initramfs: refact...
228
  					input_file "$arg"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229
230
231
232
233
  					;;
  			esac
  			;;
  	esac
  done
65e00e04e   Masahiro Yamada   initramfs: refact...
234
  # If output_file is set we will generate cpio archive
25985edce   Lucas De Marchi   Fix common misspe...
235
  # we are careful to delete tmp files
65e00e04e   Masahiro Yamada   initramfs: refact...
236
237
238
239
240
  timestamp=
  if test -n "$KBUILD_BUILD_TIMESTAMP"; then
  	timestamp="$(date -d"$KBUILD_BUILD_TIMESTAMP" +%s || :)"
  	if test -n "$timestamp"; then
  		timestamp="-t $timestamp"
c299ec2d8   Alex Landau   kbuild: handle co...
241
  	fi
d39a206bc   Sam Ravnborg   kbuild: rebuild i...
242
  fi
65e00e04e   Masahiro Yamada   initramfs: refact...
243
  usr/gen_init_cpio $timestamp $cpio_list > $output