blob: e6808a8c3b2bcd998ef2abffb753738dfc54e020 [file] [log] [blame]
Jamey Sharp153f0112011-05-05 12:03:47 -07001#!/bin/sh
Linus Torvalds1da177e2005-04-16 15:20:36 -07002# Copyright (C) Martin Schlemmer <azarah@nosferatu.za.org>
Oleg Verychf6112ec2007-02-06 02:18:20 +01003# Copyright (C) 2006 Sam Ravnborg <sam@ravnborg.org>
Sam Ravnborgd39a206b2006-04-11 13:24:32 +02004#
Linus Torvalds1da177e2005-04-16 15:20:36 -07005# Released under the terms of the GNU GPL
6#
Sam Ravnborgd39a206b2006-04-11 13:24:32 +02007# Generate a cpio packed initramfs. It uses gen_init_cpio to generate
Alain Knaffa26ee602009-01-07 00:10:27 -08008# the cpio archive, and then compresses it.
Sam Ravnborgd39a206b2006-04-11 13:24:32 +02009# 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:
Masahiro Yamada96680972020-01-05 00:02:34 +090018$0 [-o <file>] [-l <dep_list>] [-u <uid>] [-g <gid>] {-d | <cpio_source>} ...
Alain Knaffa26ee602009-01-07 00:10:27 -080019 -o <file> Create compressed initramfs file named <file> using
20 gen_init_cpio and compressor depending on the extension
Masahiro Yamada96680972020-01-05 00:02:34 +090021 -l <dep_list> Create dependency list named <dep_list>
Sam Ravnborgd39a206b2006-04-11 13:24:32 +020022 -u <uid> User ID to map to user ID 0 (root).
Mike Frysinger4c6f2eb2007-05-10 22:44:28 -070023 <uid> is only meaningful if <cpio_source> is a
24 directory. "squash" forces all files to uid 0.
Sam Ravnborgd39a206b2006-04-11 13:24:32 +020025 -g <gid> Group ID to map to group ID 0 (root).
Mike Frysinger4c6f2eb2007-05-10 22:44:28 -070026 <gid> is only meaningful if <cpio_source> is a
27 directory. "squash" forces all files to gid 0.
Sam Ravnborgd39a206b2006-04-11 13:24:32 +020028 <cpio_source> File list or directory for cpio archive.
Oleg Verychf6112ec2007-02-06 02:18:20 +010029 If <cpio_source> is a .cpio file it will be used
Sam Ravnborgd39a206b2006-04-11 13:24:32 +020030 as direct input to initramfs.
Sam Ravnborgd39a206b2006-04-11 13:24:32 +020031
32All options except -o and -l may be repeated and are interpreted
33sequentially and immediately. -u and -g states are preserved across
34<cpio_source> options so an explicit "-u 0 -g 0" is required
35to reset the root/group mapping.
36EOF
37}
38
Oleg Verychf6112ec2007-02-06 02:18:20 +010039# awk style field access
40# $1 - field number; rest is argument string
41field() {
42 shift $1 ; echo $1
43}
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045filetype() {
46 local argv1="$1"
47
48 # symlink test must come before file test
49 if [ -L "${argv1}" ]; then
50 echo "slink"
51 elif [ -f "${argv1}" ]; then
52 echo "file"
53 elif [ -d "${argv1}" ]; then
54 echo "dir"
55 elif [ -b "${argv1}" -o -c "${argv1}" ]; then
56 echo "nod"
57 elif [ -p "${argv1}" ]; then
58 echo "pipe"
59 elif [ -S "${argv1}" ]; then
60 echo "sock"
61 else
62 echo "invalid"
63 fi
64 return 0
65}
66
Sam Ravnborgd39a206b2006-04-11 13:24:32 +020067print_mtime() {
68 local my_mtime="0"
69
70 if [ -e "$1" ]; then
71 my_mtime=$(find "$1" -printf "%T@\n" | sort -r | head -n 1)
72 fi
73
74 echo "# Last modified: ${my_mtime}" >> ${output}
75 echo "" >> ${output}
76}
77
78list_parse() {
Masahiro Yamada96680972020-01-05 00:02:34 +090079 if [ -z "$dep_list" -o -L "$1" ]; then
Michal Marek590abbd2016-09-23 09:56:05 +020080 return
81 fi
Masahiro Yamada96680972020-01-05 00:02:34 +090082 echo "$1" | sed 's/:/\\:/g; s/$/ \\/' >> $dep_list
Sam Ravnborgd39a206b2006-04-11 13:24:32 +020083}
84
85# for each file print a line in following format
86# <filetype> <name> <path to file> <octal mode> <uid> <gid>
87# for links, devices etc the format differs. See gen_init_cpio for details
Linus Torvalds1da177e2005-04-16 15:20:36 -070088parse() {
89 local location="$1"
Jamey Sharp153f0112011-05-05 12:03:47 -070090 local name="/${location#${srcdir}}"
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 # change '//' into '/'
Jamey Sharp153f0112011-05-05 12:03:47 -070092 name=$(echo "$name" | sed -e 's://*:/:g')
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 local mode="$2"
94 local uid="$3"
95 local gid="$4"
96 local ftype=$(filetype "${location}")
97 # remap uid/gid to 0 if necessary
Mike Frysinger4c6f2eb2007-05-10 22:44:28 -070098 [ "$root_uid" = "squash" ] && uid=0 || [ "$uid" -eq "$root_uid" ] && uid=0
99 [ "$root_gid" = "squash" ] && gid=0 || [ "$gid" -eq "$root_gid" ] && gid=0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 local str="${mode} ${uid} ${gid}"
101
Jamey Sharp153f0112011-05-05 12:03:47 -0700102 [ "${ftype}" = "invalid" ] && return 0
103 [ "${location}" = "${srcdir}" ] && return 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 case "${ftype}" in
106 "file")
107 str="${ftype} ${name} ${location} ${str}"
108 ;;
109 "nod")
Masahiro Yamadacc976612019-12-30 22:20:06 +0900110 local dev="`LC_ALL=C ls -l "${location}"`"
Oleg Verychf6112ec2007-02-06 02:18:20 +0100111 local maj=`field 5 ${dev}`
112 local min=`field 6 ${dev}`
113 maj=${maj%,}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Oleg Verychf6112ec2007-02-06 02:18:20 +0100115 [ -b "${location}" ] && dev="b" || dev="c"
116
117 str="${ftype} ${name} ${str} ${dev} ${maj} ${min}"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 ;;
119 "slink")
Felix Fietkaub5a5e4c72008-04-02 14:50:05 +0200120 local target=`readlink "${location}"`
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 str="${ftype} ${name} ${target} ${str}"
122 ;;
123 *)
124 str="${ftype} ${name} ${str}"
125 ;;
126 esac
127
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200128 echo "${str}" >> ${output}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 return 0
131}
132
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200133unknown_option() {
134 printf "ERROR: unknown option \"$arg\"\n" >&2
135 printf "If the filename validly begins with '-', " >&2
136 printf "then it must be prefixed\n" >&2
137 printf "by './' so that it won't be interpreted as an option." >&2
138 printf "\n" >&2
139 usage >&2
140 exit 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200143header() {
144 printf "\n#####################\n# $1\n" >> ${output}
145}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200147# process one directory (incl sub-directories)
148dir_filelist() {
Masahiro Yamada96680972020-01-05 00:02:34 +0900149 header "$1"
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200150
151 srcdir=$(echo "$1" | sed -e 's://*:/:g')
Andrzej Pietrasiewiczf55f2322018-08-17 14:03:19 +0200152 dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n" | LANG=C sort)
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200153
154 # If $dirlist is only one line, then the directory is empty
155 if [ "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then
Masahiro Yamada96680972020-01-05 00:02:34 +0900156 print_mtime "$1"
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200157
158 echo "${dirlist}" | \
159 while read x; do
Masahiro Yamada96680972020-01-05 00:02:34 +0900160 list_parse $x
161 parse $x
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200162 done
163 fi
164}
165
166# if only one file is specified and it is .cpio file then use it direct as fs
167# if a directory is specified then add all files in given direcotry to fs
Masahiro Yamadaa4c968e2020-01-05 00:02:28 +0900168# if a regular file is specified assume it is in gen_init_cpio format
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200169input_file() {
170 source="$1"
171 if [ -f "$1" ]; then
Masahiro Yamada96680972020-01-05 00:02:34 +0900172 header "$1"
Michael Forney1fe7d2b2018-02-06 22:41:17 -0800173 is_cpio="$(echo "$1" | sed 's/^.*\.cpio\(\..*\)\{0,1\}/cpio/')"
Jamey Sharp153f0112011-05-05 12:03:47 -0700174 if [ $2 -eq 0 -a ${is_cpio} = "cpio" ]; then
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200175 cpio_file=$1
Alex Landauc299ec22007-04-26 00:17:29 -0700176 echo "$1" | grep -q '^.*\.cpio\..*' && is_cpio_compressed="compressed"
Masahiro Yamada96680972020-01-05 00:02:34 +0900177 [ -n "$dep_list" ] && echo "$1" >> $dep_list
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200178 return 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 fi
Masahiro Yamada96680972020-01-05 00:02:34 +0900180 print_mtime "$1" >> ${output}
181 cat "$1" >> ${output}
182 if [ -n "$dep_list" ]; then
183 echo "$1 \\" >> $dep_list
Sam Ravnborg46ed9812006-04-30 23:56:33 +0200184 cat "$1" | while read type dir file perm ; do
Jamey Sharp153f0112011-05-05 12:03:47 -0700185 if [ "$type" = "file" ]; then
Masahiro Yamada96680972020-01-05 00:02:34 +0900186 echo "$file \\" >> $dep_list
Sam Ravnborg46ed9812006-04-30 23:56:33 +0200187 fi
188 done
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200189 fi
190 elif [ -d "$1" ]; then
191 dir_filelist "$1"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 else
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200193 echo " ${prog}: Cannot open '$1'" >&2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 exit 1
195 fi
196}
197
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200198prog=$0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199root_uid=0
200root_gid=0
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200201dep_list=
202cpio_file=
203cpio_list=
204output="/dev/stdout"
205output_file=""
Alex Landauc299ec22007-04-26 00:17:29 -0700206is_cpio_compressed=
Michal Marek6ae9ecb2011-03-31 15:47:55 +0200207compr="gzip -n -9 -f"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209while [ $# -gt 0 ]; do
210 arg="$1"
211 shift
212 case "$arg" in
Masahiro Yamada96680972020-01-05 00:02:34 +0900213 "-l") # files included in initramfs - used by kbuild
214 dep_list="$1"
215 echo "deps_initramfs := \\" > $dep_list
216 shift
217 ;;
218 "-o") # generate compressed cpio image named $1
219 output_file="$1"
220 cpio_list="$(mktemp ${TMPDIR:-/tmp}/cpiolist.XXXXXX)"
221 output=${cpio_list}
222 echo "$output_file" | grep -q "\.gz$" \
223 && [ -x "`which gzip 2> /dev/null`" ] \
224 && compr="gzip -n -9 -f"
225 echo "$output_file" | grep -q "\.bz2$" \
226 && [ -x "`which bzip2 2> /dev/null`" ] \
227 && compr="bzip2 -9 -f"
228 echo "$output_file" | grep -q "\.lzma$" \
229 && [ -x "`which lzma 2> /dev/null`" ] \
230 && compr="lzma -9 -f"
231 echo "$output_file" | grep -q "\.xz$" \
232 && [ -x "`which xz 2> /dev/null`" ] \
233 && compr="xz --check=crc32 --lzma2=dict=1MiB"
234 echo "$output_file" | grep -q "\.lzo$" \
235 && [ -x "`which lzop 2> /dev/null`" ] \
236 && compr="lzop -9 -f"
237 echo "$output_file" | grep -q "\.lz4$" \
238 && [ -x "`which lz4 2> /dev/null`" ] \
239 && compr="lz4 -l -9 -f"
240 echo "$output_file" | grep -q "\.cpio$" && compr="cat"
241 shift
242 ;;
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200243 "-u") # map $1 to uid=0 (root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 root_uid="$1"
Rob Landley595a22a2017-07-06 15:35:43 -0700245 [ "$root_uid" = "-1" ] && root_uid=$(id -u || echo 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 shift
247 ;;
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200248 "-g") # map $1 to gid=0 (root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 root_gid="$1"
Rob Landley595a22a2017-07-06 15:35:43 -0700250 [ "$root_gid" = "-1" ] && root_gid=$(id -g || echo 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 shift
252 ;;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 "-h")
254 usage
255 exit 0
256 ;;
257 *)
258 case "$arg" in
259 "-"*)
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200260 unknown_option
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 ;;
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200262 *) # input file/dir - process it
263 input_file "$arg" "$#"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 ;;
265 esac
266 ;;
267 esac
268done
269
Alain Knaffa26ee602009-01-07 00:10:27 -0800270# If output_file is set we will generate cpio archive and compress it
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300271# we are careful to delete tmp files
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200272if [ ! -z ${output_file} ]; then
273 if [ -z ${cpio_file} ]; then
Michal Mareka8b80172011-03-31 23:16:42 +0200274 timestamp=
275 if test -n "$KBUILD_BUILD_TIMESTAMP"; then
276 timestamp="$(date -d"$KBUILD_BUILD_TIMESTAMP" +%s || :)"
277 if test -n "$timestamp"; then
278 timestamp="-t $timestamp"
279 fi
280 fi
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200281 cpio_tfile="$(mktemp ${TMPDIR:-/tmp}/cpiofile.XXXXXX)"
Michal Mareka8b80172011-03-31 23:16:42 +0200282 usr/gen_init_cpio $timestamp ${cpio_list} > ${cpio_tfile}
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200283 else
284 cpio_tfile=${cpio_file}
285 fi
286 rm ${cpio_list}
Alex Landauc299ec22007-04-26 00:17:29 -0700287 if [ "${is_cpio_compressed}" = "compressed" ]; then
288 cat ${cpio_tfile} > ${output_file}
289 else
Alain Knaffab59d3b2009-02-19 13:39:21 -0800290 (cat ${cpio_tfile} | ${compr} - > ${output_file}) \
291 || (rm -f ${output_file} ; false)
Alex Landauc299ec22007-04-26 00:17:29 -0700292 fi
Sam Ravnborgd39a206b2006-04-11 13:24:32 +0200293 [ -z ${cpio_file} ] && rm ${cpio_tfile}
294fi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295exit 0