Back to home page

LXR

 
 

    


0001 #!/bin/sh
0002 # Copyright (C) Martin Schlemmer <azarah@nosferatu.za.org>
0003 # Copyright (C) 2006 Sam Ravnborg <sam@ravnborg.org>
0004 #
0005 # Released under the terms of the GNU GPL
0006 #
0007 # Generate a cpio packed initramfs. It uses gen_init_cpio to generate
0008 # the cpio archive, and then compresses it.
0009 # The script may also be used to generate the inputfile used for gen_init_cpio
0010 # This script assumes that gen_init_cpio is located in usr/ directory
0011 
0012 # error out on errors
0013 set -e
0014 
0015 usage() {
0016 cat << EOF
0017 Usage:
0018 $0 [-o <file>] [-u <uid>] [-g <gid>] {-d | <cpio_source>} ...
0019         -o <file>      Create compressed initramfs file named <file> using
0020                        gen_init_cpio and compressor depending on the extension
0021         -u <uid>       User ID to map to user ID 0 (root).
0022                        <uid> is only meaningful if <cpio_source> is a
0023                        directory.  "squash" forces all files to uid 0.
0024         -g <gid>       Group ID to map to group ID 0 (root).
0025                        <gid> is only meaningful if <cpio_source> is a
0026                        directory.  "squash" forces all files to gid 0.
0027         <cpio_source>  File list or directory for cpio archive.
0028                        If <cpio_source> is a .cpio file it will be used
0029                        as direct input to initramfs.
0030         -d             Output the default cpio list.
0031 
0032 All options except -o and -l may be repeated and are interpreted
0033 sequentially and immediately.  -u and -g states are preserved across
0034 <cpio_source> options so an explicit "-u 0 -g 0" is required
0035 to reset the root/group mapping.
0036 EOF
0037 }
0038 
0039 # awk style field access
0040 # $1 - field number; rest is argument string
0041 field() {
0042         shift $1 ; echo $1
0043 }
0044 
0045 list_default_initramfs() {
0046         # echo usr/kinit/kinit
0047         :
0048 }
0049 
0050 default_initramfs() {
0051         cat <<-EOF >> ${output}
0052                 # This is a very simple, default initramfs
0053 
0054                 dir /dev 0755 0 0
0055                 nod /dev/console 0600 0 0 c 5 1
0056                 dir /root 0700 0 0
0057                 # file /kinit usr/kinit/kinit 0755 0 0
0058                 # slink /init kinit 0755 0 0
0059         EOF
0060 }
0061 
0062 filetype() {
0063         local argv1="$1"
0064 
0065         # symlink test must come before file test
0066         if [ -L "${argv1}" ]; then
0067                 echo "slink"
0068         elif [ -f "${argv1}" ]; then
0069                 echo "file"
0070         elif [ -d "${argv1}" ]; then
0071                 echo "dir"
0072         elif [ -b "${argv1}" -o -c "${argv1}" ]; then
0073                 echo "nod"
0074         elif [ -p "${argv1}" ]; then
0075                 echo "pipe"
0076         elif [ -S "${argv1}" ]; then
0077                 echo "sock"
0078         else
0079                 echo "invalid"
0080         fi
0081         return 0
0082 }
0083 
0084 list_print_mtime() {
0085         :
0086 }
0087 
0088 print_mtime() {
0089         local my_mtime="0"
0090 
0091         if [ -e "$1" ]; then
0092                 my_mtime=$(find "$1" -printf "%T@\n" | sort -r | head -n 1)
0093         fi
0094 
0095         echo "# Last modified: ${my_mtime}" >> ${output}
0096         echo "" >> ${output}
0097 }
0098 
0099 list_parse() {
0100         if [ -L "$1" ]; then
0101                 return
0102         fi
0103         echo "$1" | sed 's/:/\\:/g; s/$/ \\/'
0104 }
0105 
0106 # for each file print a line in following format
0107 # <filetype> <name> <path to file> <octal mode> <uid> <gid>
0108 # for links, devices etc the format differs. See gen_init_cpio for details
0109 parse() {
0110         local location="$1"
0111         local name="/${location#${srcdir}}"
0112         # change '//' into '/'
0113         name=$(echo "$name" | sed -e 's://*:/:g')
0114         local mode="$2"
0115         local uid="$3"
0116         local gid="$4"
0117         local ftype=$(filetype "${location}")
0118         # remap uid/gid to 0 if necessary
0119         [ "$root_uid" = "squash" ] && uid=0 || [ "$uid" -eq "$root_uid" ] && uid=0
0120         [ "$root_gid" = "squash" ] && gid=0 || [ "$gid" -eq "$root_gid" ] && gid=0
0121         local str="${mode} ${uid} ${gid}"
0122 
0123         [ "${ftype}" = "invalid" ] && return 0
0124         [ "${location}" = "${srcdir}" ] && return 0
0125 
0126         case "${ftype}" in
0127                 "file")
0128                         str="${ftype} ${name} ${location} ${str}"
0129                         ;;
0130                 "nod")
0131                         local dev=`LC_ALL=C ls -l "${location}"`
0132                         local maj=`field 5 ${dev}`
0133                         local min=`field 6 ${dev}`
0134                         maj=${maj%,}
0135 
0136                         [ -b "${location}" ] && dev="b" || dev="c"
0137 
0138                         str="${ftype} ${name} ${str} ${dev} ${maj} ${min}"
0139                         ;;
0140                 "slink")
0141                         local target=`readlink "${location}"`
0142                         str="${ftype} ${name} ${target} ${str}"
0143                         ;;
0144                 *)
0145                         str="${ftype} ${name} ${str}"
0146                         ;;
0147         esac
0148 
0149         echo "${str}" >> ${output}
0150 
0151         return 0
0152 }
0153 
0154 unknown_option() {
0155         printf "ERROR: unknown option \"$arg\"\n" >&2
0156         printf "If the filename validly begins with '-', " >&2
0157         printf "then it must be prefixed\n" >&2
0158         printf "by './' so that it won't be interpreted as an option." >&2
0159         printf "\n" >&2
0160         usage >&2
0161         exit 1
0162 }
0163 
0164 list_header() {
0165         :
0166 }
0167 
0168 header() {
0169         printf "\n#####################\n# $1\n" >> ${output}
0170 }
0171 
0172 # process one directory (incl sub-directories)
0173 dir_filelist() {
0174         ${dep_list}header "$1"
0175 
0176         srcdir=$(echo "$1" | sed -e 's://*:/:g')
0177         dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n")
0178 
0179         # If $dirlist is only one line, then the directory is empty
0180         if [  "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then
0181                 ${dep_list}print_mtime "$1"
0182 
0183                 echo "${dirlist}" | \
0184                 while read x; do
0185                         ${dep_list}parse ${x}
0186                 done
0187         fi
0188 }
0189 
0190 # if only one file is specified and it is .cpio file then use it direct as fs
0191 # if a directory is specified then add all files in given direcotry to fs
0192 # if a regular file is specified assume it is in gen_initramfs format
0193 input_file() {
0194         source="$1"
0195         if [ -f "$1" ]; then
0196                 ${dep_list}header "$1"
0197                 is_cpio="$(echo "$1" | sed 's/^.*\.cpio\(\..*\)\?/cpio/')"
0198                 if [ $2 -eq 0 -a ${is_cpio} = "cpio" ]; then
0199                         cpio_file=$1
0200                         echo "$1" | grep -q '^.*\.cpio\..*' && is_cpio_compressed="compressed"
0201                         [ ! -z ${dep_list} ] && echo "$1"
0202                         return 0
0203                 fi
0204                 if [ -z ${dep_list} ]; then
0205                         print_mtime "$1" >> ${output}
0206                         cat "$1"         >> ${output}
0207                 else
0208                         echo "$1 \\"
0209                         cat "$1" | while read type dir file perm ; do
0210                                 if [ "$type" = "file" ]; then
0211                                         echo "$file \\";
0212                                 fi
0213                         done
0214                 fi
0215         elif [ -d "$1" ]; then
0216                 dir_filelist "$1"
0217         else
0218                 echo "  ${prog}: Cannot open '$1'" >&2
0219                 exit 1
0220         fi
0221 }
0222 
0223 prog=$0
0224 root_uid=0
0225 root_gid=0
0226 dep_list=
0227 cpio_file=
0228 cpio_list=
0229 output="/dev/stdout"
0230 output_file=""
0231 is_cpio_compressed=
0232 compr="gzip -n -9 -f"
0233 
0234 arg="$1"
0235 case "$arg" in
0236         "-l")   # files included in initramfs - used by kbuild
0237                 dep_list="list_"
0238                 echo "deps_initramfs := $0 \\"
0239                 shift
0240                 ;;
0241         "-o")   # generate compressed cpio image named $1
0242                 shift
0243                 output_file="$1"
0244                 cpio_list="$(mktemp ${TMPDIR:-/tmp}/cpiolist.XXXXXX)"
0245                 output=${cpio_list}
0246                 echo "$output_file" | grep -q "\.gz$" \
0247                 && [ -x "`which gzip 2> /dev/null`" ] \
0248                 && compr="gzip -n -9 -f"
0249                 echo "$output_file" | grep -q "\.bz2$" \
0250                 && [ -x "`which bzip2 2> /dev/null`" ] \
0251                 && compr="bzip2 -9 -f"
0252                 echo "$output_file" | grep -q "\.lzma$" \
0253                 && [ -x "`which lzma 2> /dev/null`" ] \
0254                 && compr="lzma -9 -f"
0255                 echo "$output_file" | grep -q "\.xz$" \
0256                 && [ -x "`which xz 2> /dev/null`" ] \
0257                 && compr="xz --check=crc32 --lzma2=dict=1MiB"
0258                 echo "$output_file" | grep -q "\.lzo$" \
0259                 && [ -x "`which lzop 2> /dev/null`" ] \
0260                 && compr="lzop -9 -f"
0261                 echo "$output_file" | grep -q "\.lz4$" \
0262                 && [ -x "`which lz4 2> /dev/null`" ] \
0263                 && compr="lz4 -l -9 -f"
0264                 echo "$output_file" | grep -q "\.cpio$" && compr="cat"
0265                 shift
0266                 ;;
0267 esac
0268 while [ $# -gt 0 ]; do
0269         arg="$1"
0270         shift
0271         case "$arg" in
0272                 "-u")   # map $1 to uid=0 (root)
0273                         root_uid="$1"
0274                         shift
0275                         ;;
0276                 "-g")   # map $1 to gid=0 (root)
0277                         root_gid="$1"
0278                         shift
0279                         ;;
0280                 "-d")   # display default initramfs list
0281                         default_list="$arg"
0282                         ${dep_list}default_initramfs
0283                         ;;
0284                 "-h")
0285                         usage
0286                         exit 0
0287                         ;;
0288                 *)
0289                         case "$arg" in
0290                                 "-"*)
0291                                         unknown_option
0292                                         ;;
0293                                 *)      # input file/dir - process it
0294                                         input_file "$arg" "$#"
0295                                         ;;
0296                         esac
0297                         ;;
0298         esac
0299 done
0300 
0301 # If output_file is set we will generate cpio archive and compress it
0302 # we are careful to delete tmp files
0303 if [ ! -z ${output_file} ]; then
0304         if [ -z ${cpio_file} ]; then
0305                 timestamp=
0306                 if test -n "$KBUILD_BUILD_TIMESTAMP"; then
0307                         timestamp="$(date -d"$KBUILD_BUILD_TIMESTAMP" +%s || :)"
0308                         if test -n "$timestamp"; then
0309                                 timestamp="-t $timestamp"
0310                         fi
0311                 fi
0312                 cpio_tfile="$(mktemp ${TMPDIR:-/tmp}/cpiofile.XXXXXX)"
0313                 usr/gen_init_cpio $timestamp ${cpio_list} > ${cpio_tfile}
0314         else
0315                 cpio_tfile=${cpio_file}
0316         fi
0317         rm ${cpio_list}
0318         if [ "${is_cpio_compressed}" = "compressed" ]; then
0319                 cat ${cpio_tfile} > ${output_file}
0320         else
0321                 (cat ${cpio_tfile} | ${compr}  - > ${output_file}) \
0322                 || (rm -f ${output_file} ; false)
0323         fi
0324         [ -z ${cpio_file} ] && rm ${cpio_tfile}
0325 fi
0326 exit 0