Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <masahiroy@kernel.org>

+78 -111
+1 -7
usr/.gitignore
··· 1 - # 2 - # Generated files 3 - # 4 1 gen_init_cpio 5 2 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
-10
usr/Kconfig
··· 207 207 by default which could cause a build failure. 208 208 209 209 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
+60 -29
usr/Makefile
··· 3 3 # kbuild file for usr/ - including initramfs image 4 4 # 5 5 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 := : 10 10 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 14 18 15 - # Generate builtin.o based on initramfs_data.o 16 19 obj-$(CONFIG_BLK_DEV_INITRD) := initramfs_data.o 17 20 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 22 22 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 25 54 26 55 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)) 32 56 33 - # $(datafile_d_y) is used to identify all files included 57 + # .initramfs_data.cpio.d is used to identify all files included 34 58 # in initramfs and to detect if any files are added/removed. 35 59 # Removed files are identified by directory timestamp being updated 36 60 # 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 45 62 46 63 # do not try to update files included in initramfs 47 64 $(deps_initramfs): ; 65 + 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) 48 72 49 73 # We rebuild initramfs_data.cpio if: 50 74 # 1) Any included file is newer than initramfs_data.cpio 51 75 # 2) There are changes in which files are included (added or deleted) 52 76 # 3) If gen_init_cpio are newer than initramfs_data.cpio 53 77 # 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 55 79 $(call if_changed,initfs) 80 + 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 56 87 57 88 subdir-$(CONFIG_UAPI_HEADER_TEST) += include
+16 -61
usr/gen_initramfs.sh
··· 5 5 # Released under the terms of the GNU GPL 6 6 # 7 7 # Generate a cpio packed initramfs. It uses gen_init_cpio to generate 8 - # the cpio archive, and then compresses it. 8 + # the cpio archive. 9 9 # This script assumes that gen_init_cpio is located in usr/ directory 10 10 11 11 # error out on errors ··· 15 15 cat << EOF 16 16 Usage: 17 17 $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 20 19 -l <dep_list> Create dependency list named <dep_list> 21 20 -u <uid> User ID to map to user ID 0 (root). 22 21 <uid> is only meaningful if <cpio_source> is a ··· 161 162 fi 162 163 } 163 164 164 - # if only one file is specified and it is .cpio file then use it direct as fs 165 - # if a directory is specified then add all files in given direcotry to fs 166 - # if a regular file is specified assume it is in gen_init_cpio format 167 165 input_file() { 168 166 source="$1" 169 167 if [ -f "$1" ]; then 168 + # If a regular file is specified, assume it is in 169 + # gen_init_cpio format 170 170 header "$1" 171 - is_cpio="$(echo "$1" | sed 's/^.*\.cpio\(\..*\)\{0,1\}/cpio/')" 172 - if [ $2 -eq 0 -a ${is_cpio} = "cpio" ]; then 173 - cpio_file=$1 174 - echo "$1" | grep -q '^.*\.cpio\..*' && is_cpio_compressed="compressed" 175 - [ -n "$dep_list" ] && echo "$1" >> $dep_list 176 - return 0 177 - fi 178 171 print_mtime "$1" >> $cpio_list 179 172 cat "$1" >> $cpio_list 180 173 if [ -n "$dep_list" ]; then ··· 178 187 done 179 188 fi 180 189 elif [ -d "$1" ]; then 190 + # If a directory is specified then add all files in it to fs 181 191 dir_filelist "$1" 182 192 else 183 193 echo " ${prog}: Cannot open '$1'" >&2 ··· 190 198 root_uid=0 191 199 root_gid=0 192 200 dep_list= 193 - cpio_file= 194 201 cpio_list=$(mktemp ${TMPDIR:-/tmp}/cpiolist.XXXXXX) 195 202 output="/dev/stdout" 196 - output_file="/dev/stdout" 197 - is_cpio_compressed= 198 - compr="gzip -n -9 -f" 199 203 200 204 while [ $# -gt 0 ]; do 201 205 arg="$1" ··· 202 214 echo "deps_initramfs := \\" > $dep_list 203 215 shift 204 216 ;; 205 - "-o") # generate compressed cpio image named $1 206 - output_file="$1" 207 - output=$cpio_list 208 - echo "$output_file" | grep -q "\.gz$" \ 209 - && [ -x "`which gzip 2> /dev/null`" ] \ 210 - && compr="gzip -n -9 -f" 211 - echo "$output_file" | grep -q "\.bz2$" \ 212 - && [ -x "`which bzip2 2> /dev/null`" ] \ 213 - && compr="bzip2 -9 -f" 214 - echo "$output_file" | grep -q "\.lzma$" \ 215 - && [ -x "`which lzma 2> /dev/null`" ] \ 216 - && compr="lzma -9 -f" 217 - echo "$output_file" | grep -q "\.xz$" \ 218 - && [ -x "`which xz 2> /dev/null`" ] \ 219 - && compr="xz --check=crc32 --lzma2=dict=1MiB" 220 - echo "$output_file" | grep -q "\.lzo$" \ 221 - && [ -x "`which lzop 2> /dev/null`" ] \ 222 - && compr="lzop -9 -f" 223 - echo "$output_file" | grep -q "\.lz4$" \ 224 - && [ -x "`which lz4 2> /dev/null`" ] \ 225 - && compr="lz4 -l -9 -f" 226 - echo "$output_file" | grep -q "\.cpio$" && compr="cat" 217 + "-o") # generate cpio image named $1 218 + output="$1" 227 219 shift 228 220 ;; 229 221 "-u") # map $1 to uid=0 (root) ··· 226 258 unknown_option 227 259 ;; 228 260 *) # input file/dir - process it 229 - input_file "$arg" "$#" 261 + input_file "$arg" 230 262 ;; 231 263 esac 232 264 ;; 233 265 esac 234 266 done 235 267 236 - # If output_file is set we will generate cpio archive and compress it 268 + # If output_file is set we will generate cpio archive 237 269 # we are careful to delete tmp files 238 - if [ -z ${cpio_file} ]; then 239 - timestamp= 240 - if test -n "$KBUILD_BUILD_TIMESTAMP"; then 241 - timestamp="$(date -d"$KBUILD_BUILD_TIMESTAMP" +%s || :)" 242 - if test -n "$timestamp"; then 243 - timestamp="-t $timestamp" 244 - fi 270 + timestamp= 271 + if test -n "$KBUILD_BUILD_TIMESTAMP"; then 272 + timestamp="$(date -d"$KBUILD_BUILD_TIMESTAMP" +%s || :)" 273 + if test -n "$timestamp"; then 274 + timestamp="-t $timestamp" 245 275 fi 246 - cpio_tfile="$(mktemp ${TMPDIR:-/tmp}/cpiofile.XXXXXX)" 247 - usr/gen_init_cpio $timestamp ${cpio_list} > ${cpio_tfile} 248 - else 249 - cpio_tfile=${cpio_file} 250 276 fi 251 - rm ${cpio_list} 252 - if [ "${is_cpio_compressed}" = "compressed" ]; then 253 - cat ${cpio_tfile} > ${output_file} 254 - else 255 - (cat ${cpio_tfile} | ${compr} - > ${output_file}) \ 256 - || (rm -f ${output_file} ; false) 257 - fi 258 - [ -z ${cpio_file} ] && rm ${cpio_tfile} 259 - exit 0 277 + usr/gen_init_cpio $timestamp $cpio_list > $output 278 + rm $cpio_list
+1 -4
usr/initramfs_data.S
··· 22 22 in the ELF header, as required by certain architectures. 23 23 */ 24 24 25 - #include <linux/stringify.h> 26 - #include <asm-generic/vmlinux.lds.h> 27 - 28 25 .section .init.ramfs,"a" 29 26 __irf_start: 30 - .incbin __stringify(INITRAMFS_IMAGE) 27 + .incbin "usr/initramfs_inc_data" 31 28 __irf_end: 32 29 .section .init.ramfs.info,"a" 33 30 .globl __initramfs_size