Skip to content

Commit 65e00e0

Browse files
committed
initramfs: refactor the initramfs build rules
Currently, usr/gen_initramfs.sh takes care of all the use-cases: [1] generates a cpio file unless CONFIG_INITRAMFS_SOURCE points to a single cpio archive [2] If CONFIG_INITRAMFS_SOURCE is the path to a cpio archive, use it as-is. [3] Compress the cpio file according to CONFIG_INITRAMFS_COMPRESSION_* unless it is passed a compressed archive. To simplify the script, move [2] and [3] to usr/Makefile. If CONFIG_INITRAMFS_SOURCE is the path to a cpio archive, there is no need to run this shell script. For the cpio archive compression, you can re-use the rules from scripts/Makefile.lib . Signed-off-by: Masahiro Yamada <[email protected]>
1 parent 469e87e commit 65e00e0

File tree

5 files changed

+78
-111
lines changed

5 files changed

+78
-111
lines changed

usr/.gitignore

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
#
2-
# Generated files
3-
#
41
gen_init_cpio
52
initramfs_data.cpio
6-
initramfs_data.cpio.gz
7-
initramfs_data.cpio.bz2
8-
initramfs_data.cpio.lzma
9-
initramfs_list
3+
/initramfs_inc_data

usr/Kconfig

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,3 @@ config INITRAMFS_COMPRESSION_LZ4
207207
by default which could cause a build failure.
208208

209209
endchoice
210-
211-
config INITRAMFS_COMPRESSION
212-
string
213-
default "" if INITRAMFS_COMPRESSION_NONE
214-
default ".gz" if INITRAMFS_COMPRESSION_GZIP
215-
default ".bz2" if INITRAMFS_COMPRESSION_BZIP2
216-
default ".lzma" if INITRAMFS_COMPRESSION_LZMA
217-
default ".xz" if INITRAMFS_COMPRESSION_XZ
218-
default ".lzo" if INITRAMFS_COMPRESSION_LZO
219-
default ".lz4" if INITRAMFS_COMPRESSION_LZ4

usr/Makefile

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,86 @@
33
# kbuild file for usr/ - including initramfs image
44
#
55

6-
suffix_y = $(subst $\",,$(CONFIG_INITRAMFS_COMPRESSION))
7-
datafile_y = initramfs_data.cpio$(suffix_y)
8-
datafile_d_y = .$(datafile_y).d
9-
AFLAGS_initramfs_data.o += -DINITRAMFS_IMAGE="usr/$(datafile_y)"
6+
# cmd_bzip2, cmd_lzma, cmd_lzo, cmd_lz4 from scripts/Makefile.lib appends the
7+
# size at the end of the compressed file, which unfortunately does not work
8+
# with unpack_to_rootfs(). Make size_append no-op.
9+
override size_append := :
1010

11-
# clean rules do not have CONFIG_INITRAMFS_COMPRESSION. So clean up after all
12-
# possible compression formats.
13-
clean-files += initramfs_data.cpio*
11+
compress-$(CONFIG_INITRAMFS_COMPRESSION_NONE) := shipped
12+
compress-$(CONFIG_INITRAMFS_COMPRESSION_GZIP) := gzip
13+
compress-$(CONFIG_INITRAMFS_COMPRESSION_BZIP2) := bzip2
14+
compress-$(CONFIG_INITRAMFS_COMPRESSION_LZMA) := lzma
15+
compress-$(CONFIG_INITRAMFS_COMPRESSION_XZ) := xzmisc
16+
compress-$(CONFIG_INITRAMFS_COMPRESSION_LZO) := lzo
17+
compress-$(CONFIG_INITRAMFS_COMPRESSION_LZ4) := lz4
1418

15-
# Generate builtin.o based on initramfs_data.o
1619
obj-$(CONFIG_BLK_DEV_INITRD) := initramfs_data.o
1720

18-
# initramfs_data.o contains the compressed initramfs_data.cpio image.
19-
# The image is included using .incbin, a dependency which is not
20-
# tracked automatically.
21-
$(obj)/initramfs_data.o: $(obj)/$(datafile_y) FORCE
21+
$(obj)/initramfs_data.o: $(obj)/initramfs_inc_data
2222

23-
#####
24-
# Generate the initramfs cpio archive
23+
ramfs-input := $(strip $(shell echo $(CONFIG_INITRAMFS_SOURCE)))
24+
cpio-data :=
25+
26+
# If CONFIG_INITRAMFS_SOURCE is empty, generate a small initramfs with the
27+
# default contents.
28+
ifeq ($(ramfs-input),)
29+
ramfs-input := $(srctree)/$(src)/default_cpio_list
30+
endif
31+
32+
ifeq ($(words $(ramfs-input)),1)
33+
34+
# If CONFIG_INITRAMFS_SOURCE specifies a single file, and it is suffixed with
35+
# .cpio, use it directly as an initramfs.
36+
ifneq ($(filter %.cpio,$(ramfs-input)),)
37+
cpio-data := $(ramfs-input)
38+
endif
39+
40+
# If CONFIG_INITRAMFS_SOURCE specifies a single file, and it is suffixed with
41+
# .cpio.*, use it directly as an initramfs, and avoid double compression.
42+
ifeq ($(words $(subst .cpio.,$(space),$(ramfs-input))),2)
43+
cpio-data := $(ramfs-input)
44+
compress-y := shipped
45+
endif
46+
47+
endif
48+
49+
# For other cases, generate the initramfs cpio archive based on the contents
50+
# specified by CONFIG_INITRAMFS_SOURCE.
51+
ifeq ($(cpio-data),)
52+
53+
cpio-data := $(obj)/initramfs_data.cpio
2554

2655
hostprogs-y := gen_init_cpio
27-
ramfs-input := $(if $(filter-out "",$(CONFIG_INITRAMFS_SOURCE)), \
28-
$(shell echo $(CONFIG_INITRAMFS_SOURCE)),$(srctree)/$(src)/default_cpio_list)
29-
ramfs-args := \
30-
$(if $(CONFIG_INITRAMFS_ROOT_UID), -u $(CONFIG_INITRAMFS_ROOT_UID)) \
31-
$(if $(CONFIG_INITRAMFS_ROOT_GID), -g $(CONFIG_INITRAMFS_ROOT_GID))
3256

33-
# $(datafile_d_y) is used to identify all files included
57+
# .initramfs_data.cpio.d is used to identify all files included
3458
# in initramfs and to detect if any files are added/removed.
3559
# Removed files are identified by directory timestamp being updated
3660
# The dependency list is generated by gen_initramfs.sh -l
37-
ifneq ($(wildcard $(obj)/$(datafile_d_y)),)
38-
include $(obj)/$(datafile_d_y)
39-
endif
40-
41-
quiet_cmd_initfs = GEN $@
42-
cmd_initfs = $(CONFIG_SHELL) $< -o $@ -l $(obj)/$(datafile_d_y) $(ramfs-args) $(ramfs-input)
43-
44-
targets := $(datafile_y)
61+
-include $(obj)/.initramfs_data.cpio.d
4562

4663
# do not try to update files included in initramfs
4764
$(deps_initramfs): ;
4865

66+
quiet_cmd_initfs = GEN $@
67+
cmd_initfs = \
68+
$(CONFIG_SHELL) $< -o $@ -l $(obj)/.initramfs_data.cpio.d \
69+
$(if $(CONFIG_INITRAMFS_ROOT_UID), -u $(CONFIG_INITRAMFS_ROOT_UID)) \
70+
$(if $(CONFIG_INITRAMFS_ROOT_GID), -g $(CONFIG_INITRAMFS_ROOT_GID)) \
71+
$(ramfs-input)
72+
4973
# We rebuild initramfs_data.cpio if:
5074
# 1) Any included file is newer than initramfs_data.cpio
5175
# 2) There are changes in which files are included (added or deleted)
5276
# 3) If gen_init_cpio are newer than initramfs_data.cpio
5377
# 4) Arguments to gen_initramfs.sh changes
54-
$(obj)/$(datafile_y): $(src)/gen_initramfs.sh $(obj)/gen_init_cpio $(deps_initramfs) FORCE
78+
$(obj)/initramfs_data.cpio: $(src)/gen_initramfs.sh $(obj)/gen_init_cpio $(deps_initramfs) FORCE
5579
$(call if_changed,initfs)
5680

81+
endif
82+
83+
$(obj)/initramfs_inc_data: $(cpio-data) FORCE
84+
$(call if_changed,$(compress-y))
85+
86+
targets += initramfs_data.cpio initramfs_inc_data
87+
5788
subdir-$(CONFIG_UAPI_HEADER_TEST) += include

usr/gen_initramfs.sh

Lines changed: 16 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Released under the terms of the GNU GPL
66
#
77
# Generate a cpio packed initramfs. It uses gen_init_cpio to generate
8-
# the cpio archive, and then compresses it.
8+
# the cpio archive.
99
# This script assumes that gen_init_cpio is located in usr/ directory
1010

1111
# error out on errors
@@ -15,8 +15,7 @@ usage() {
1515
cat << EOF
1616
Usage:
1717
$0 [-o <file>] [-l <dep_list>] [-u <uid>] [-g <gid>] {-d | <cpio_source>} ...
18-
-o <file> Create compressed initramfs file named <file> using
19-
gen_init_cpio and compressor depending on the extension
18+
-o <file> Create initramfs file named <file> by using gen_init_cpio
2019
-l <dep_list> Create dependency list named <dep_list>
2120
-u <uid> User ID to map to user ID 0 (root).
2221
<uid> is only meaningful if <cpio_source> is a
@@ -162,20 +161,12 @@ dir_filelist() {
162161
fi
163162
}
164163

165-
# if only one file is specified and it is .cpio file then use it direct as fs
166-
# if a directory is specified then add all files in given direcotry to fs
167-
# if a regular file is specified assume it is in gen_init_cpio format
168164
input_file() {
169165
source="$1"
170166
if [ -f "$1" ]; then
167+
# If a regular file is specified, assume it is in
168+
# gen_init_cpio format
171169
header "$1"
172-
is_cpio="$(echo "$1" | sed 's/^.*\.cpio\(\..*\)\{0,1\}/cpio/')"
173-
if [ $2 -eq 0 -a ${is_cpio} = "cpio" ]; then
174-
cpio_file=$1
175-
echo "$1" | grep -q '^.*\.cpio\..*' && is_cpio_compressed="compressed"
176-
[ -n "$dep_list" ] && echo "$1" >> $dep_list
177-
return 0
178-
fi
179170
print_mtime "$1" >> $cpio_list
180171
cat "$1" >> $cpio_list
181172
if [ -n "$dep_list" ]; then
@@ -187,6 +178,7 @@ input_file() {
187178
done
188179
fi
189180
elif [ -d "$1" ]; then
181+
# If a directory is specified then add all files in it to fs
190182
dir_filelist "$1"
191183
else
192184
echo " ${prog}: Cannot open '$1'" >&2
@@ -198,12 +190,8 @@ prog=$0
198190
root_uid=0
199191
root_gid=0
200192
dep_list=
201-
cpio_file=
202193
cpio_list=$(mktemp ${TMPDIR:-/tmp}/cpiolist.XXXXXX)
203194
output="/dev/stdout"
204-
output_file="/dev/stdout"
205-
is_cpio_compressed=
206-
compr="gzip -n -9 -f"
207195

208196
while [ $# -gt 0 ]; do
209197
arg="$1"
@@ -214,28 +202,8 @@ while [ $# -gt 0 ]; do
214202
echo "deps_initramfs := \\" > $dep_list
215203
shift
216204
;;
217-
"-o") # generate compressed cpio image named $1
218-
output_file="$1"
219-
output=$cpio_list
220-
echo "$output_file" | grep -q "\.gz$" \
221-
&& [ -x "`which gzip 2> /dev/null`" ] \
222-
&& compr="gzip -n -9 -f"
223-
echo "$output_file" | grep -q "\.bz2$" \
224-
&& [ -x "`which bzip2 2> /dev/null`" ] \
225-
&& compr="bzip2 -9 -f"
226-
echo "$output_file" | grep -q "\.lzma$" \
227-
&& [ -x "`which lzma 2> /dev/null`" ] \
228-
&& compr="lzma -9 -f"
229-
echo "$output_file" | grep -q "\.xz$" \
230-
&& [ -x "`which xz 2> /dev/null`" ] \
231-
&& compr="xz --check=crc32 --lzma2=dict=1MiB"
232-
echo "$output_file" | grep -q "\.lzo$" \
233-
&& [ -x "`which lzop 2> /dev/null`" ] \
234-
&& compr="lzop -9 -f"
235-
echo "$output_file" | grep -q "\.lz4$" \
236-
&& [ -x "`which lz4 2> /dev/null`" ] \
237-
&& compr="lz4 -l -9 -f"
238-
echo "$output_file" | grep -q "\.cpio$" && compr="cat"
205+
"-o") # generate cpio image named $1
206+
output="$1"
239207
shift
240208
;;
241209
"-u") # map $1 to uid=0 (root)
@@ -258,34 +226,21 @@ while [ $# -gt 0 ]; do
258226
unknown_option
259227
;;
260228
*) # input file/dir - process it
261-
input_file "$arg" "$#"
229+
input_file "$arg"
262230
;;
263231
esac
264232
;;
265233
esac
266234
done
267235

268-
# If output_file is set we will generate cpio archive and compress it
236+
# If output_file is set we will generate cpio archive
269237
# we are careful to delete tmp files
270-
if [ -z ${cpio_file} ]; then
271-
timestamp=
272-
if test -n "$KBUILD_BUILD_TIMESTAMP"; then
273-
timestamp="$(date -d"$KBUILD_BUILD_TIMESTAMP" +%s || :)"
274-
if test -n "$timestamp"; then
275-
timestamp="-t $timestamp"
276-
fi
238+
timestamp=
239+
if test -n "$KBUILD_BUILD_TIMESTAMP"; then
240+
timestamp="$(date -d"$KBUILD_BUILD_TIMESTAMP" +%s || :)"
241+
if test -n "$timestamp"; then
242+
timestamp="-t $timestamp"
277243
fi
278-
cpio_tfile="$(mktemp ${TMPDIR:-/tmp}/cpiofile.XXXXXX)"
279-
usr/gen_init_cpio $timestamp ${cpio_list} > ${cpio_tfile}
280-
else
281-
cpio_tfile=${cpio_file}
282-
fi
283-
rm ${cpio_list}
284-
if [ "${is_cpio_compressed}" = "compressed" ]; then
285-
cat ${cpio_tfile} > ${output_file}
286-
else
287-
(cat ${cpio_tfile} | ${compr} - > ${output_file}) \
288-
|| (rm -f ${output_file} ; false)
289244
fi
290-
[ -z ${cpio_file} ] && rm ${cpio_tfile}
291-
exit 0
245+
usr/gen_init_cpio $timestamp $cpio_list > $output
246+
rm $cpio_list

usr/initramfs_data.S

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,9 @@
2222
in the ELF header, as required by certain architectures.
2323
*/
2424

25-
#include <linux/stringify.h>
26-
#include <asm-generic/vmlinux.lds.h>
27-
2825
.section .init.ramfs,"a"
2926
__irf_start:
30-
.incbin __stringify(INITRAMFS_IMAGE)
27+
.incbin "usr/initramfs_inc_data"
3128
__irf_end:
3229
.section .init.ramfs.info,"a"
3330
.globl __initramfs_size

0 commit comments

Comments
 (0)