X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=scripts%2Fgen_initramfs_list.sh;h=4c723fd18648daba8926e8b73e727b45c90fc1d9;hb=refs%2Fheads%2Fvserver;hp=6d411169bfa62be23a244e2bf8f1a110c1936715;hpb=76828883507a47dae78837ab5dec5a5b4513c667;p=linux-2.6.git diff --git a/scripts/gen_initramfs_list.sh b/scripts/gen_initramfs_list.sh index 6d411169b..4c723fd18 100644 --- a/scripts/gen_initramfs_list.sh +++ b/scripts/gen_initramfs_list.sh @@ -1,22 +1,55 @@ #!/bin/bash # Copyright (C) Martin Schlemmer -# Released under the terms of the GNU GPL -# -# Generate a newline separated list of entries from the file/directory -# supplied as an argument. -# -# If a file/directory is not supplied then generate a small dummy file. +# Copyright (c) 2006 Sam Ravnborg # -# The output is suitable for gen_init_cpio built from usr/gen_init_cpio.c. +# Released under the terms of the GNU GPL # +# Generate a cpio packed initramfs. It uses gen_init_cpio to generate +# the cpio archive, and gzip to pack it. +# The script may also be used to generate the inputfile used for gen_init_cpio +# This script assumes that gen_init_cpio is located in usr/ directory + +# error out on errors +set -e + +usage() { +cat << EOF +Usage: +$0 [-o ] [-u ] [-g ] {-d | } ... + -o Create gzipped initramfs file named using + gen_init_cpio and gzip + -u User ID to map to user ID 0 (root). + is only meaningful if + is a directory. + -g Group ID to map to group ID 0 (root). + is only meaningful if + is a directory. + File list or directory for cpio archive. + If is a .cpio file it will be used + as direct input to initramfs. + -d Output the default cpio list. + +All options except -o and -l may be repeated and are interpreted +sequentially and immediately. -u and -g states are preserved across + options so an explicit "-u 0 -g 0" is required +to reset the root/group mapping. +EOF +} + +list_default_initramfs() { + # echo usr/kinit/kinit + : +} default_initramfs() { - cat <<-EOF + cat <<-EOF >> ${output} # This is a very simple, default initramfs dir /dev 0755 0 0 nod /dev/console 0600 0 0 c 5 1 dir /root 0700 0 0 + # file /kinit usr/kinit/kinit 0755 0 0 + # slink /init kinit 0755 0 0 EOF } @@ -42,18 +75,28 @@ filetype() { return 0 } +list_print_mtime() { + : +} + print_mtime() { - local argv1="$1" local my_mtime="0" - if [ -e "${argv1}" ]; then - my_mtime=$(find "${argv1}" -printf "%T@\n" | sort -r | head -n 1) + if [ -e "$1" ]; then + my_mtime=$(find "$1" -printf "%T@\n" | sort -r | head -n 1) fi - - echo "# Last modified: ${my_mtime}" - echo + + echo "# Last modified: ${my_mtime}" >> ${output} + echo "" >> ${output} +} + +list_parse() { + echo "$1 \\" } +# for each file print a line in following format +# +# for links, devices etc the format differs. See gen_init_cpio for details parse() { local location="$1" local name="${location/${srcdir}//}" @@ -99,80 +142,117 @@ parse() { ;; esac - echo "${str}" + echo "${str}" >> ${output} return 0 } -usage() { - printf "Usage:\n" - printf "$0 [ [-u ] [-g ] [-d | ] ] . . .\n" - printf "\n" - printf -- "-u User ID to map to user ID 0 (root).\n" - printf " is only meaningful if \n" - printf " is a directory.\n" - printf -- "-g Group ID to map to group ID 0 (root).\n" - printf " is only meaningful if \n" - printf " is a directory.\n" - printf " File list or directory for cpio archive.\n" - printf " If is not provided then a\n" - printf " a default list will be output.\n" - printf -- "-d Output the default cpio list. If no \n" - printf " is given then the default cpio list will be output.\n" - printf "\n" - printf "All options may be repeated and are interpreted sequentially\n" - printf "and immediately. -u and -g states are preserved across\n" - printf " options so an explicit \"-u 0 -g 0\" is required\n" - printf "to reset the root/group mapping.\n" +unknown_option() { + printf "ERROR: unknown option \"$arg\"\n" >&2 + printf "If the filename validly begins with '-', " >&2 + printf "then it must be prefixed\n" >&2 + printf "by './' so that it won't be interpreted as an option." >&2 + printf "\n" >&2 + usage >&2 + exit 1 } -build_list() { - printf "\n#####################\n# $cpio_source\n" - - if [ -f "$cpio_source" ]; then - print_mtime "$cpio_source" - cat "$cpio_source" - elif [ -d "$cpio_source" ]; then - srcdir=$(echo "$cpio_source" | sed -e 's://*:/:g') - dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n" 2>/dev/null) - - # If $dirlist is only one line, then the directory is empty - if [ "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then - print_mtime "$cpio_source" - - echo "${dirlist}" | \ - while read x; do - parse ${x} - done +list_header() { + : +} + +header() { + printf "\n#####################\n# $1\n" >> ${output} +} + +# process one directory (incl sub-directories) +dir_filelist() { + ${dep_list}header "$1" + + srcdir=$(echo "$1" | sed -e 's://*:/:g') + dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n" 2>/dev/null) + + # If $dirlist is only one line, then the directory is empty + if [ "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then + ${dep_list}print_mtime "$1" + + echo "${dirlist}" | \ + while read x; do + ${dep_list}parse ${x} + done + fi +} + +# if only one file is specified and it is .cpio file then use it direct as fs +# if a directory is specified then add all files in given direcotry to fs +# if a regular file is specified assume it is in gen_initramfs format +input_file() { + source="$1" + if [ -f "$1" ]; then + ${dep_list}header "$1" + is_cpio="$(echo "$1" | sed 's/^.*\.cpio/cpio/')" + if [ $2 -eq 0 -a ${is_cpio} == "cpio" ]; then + cpio_file=$1 + [ ! -z ${dep_list} ] && echo "$1" + return 0 + fi + if [ -z ${dep_list} ]; then + print_mtime "$1" >> ${output} + cat "$1" >> ${output} else - # Failsafe in case directory is empty - default_initramfs + cat "$1" | while read type dir file perm ; do + if [ "$type" == "file" ]; then + echo "$file \\"; + fi + done fi + elif [ -d "$1" ]; then + dir_filelist "$1" else - echo " $0: Cannot open '$cpio_source'" >&2 + echo " ${prog}: Cannot open '$1'" >&2 exit 1 fi } - +prog=$0 root_uid=0 root_gid=0 +dep_list= +cpio_file= +cpio_list= +output="/dev/stdout" +output_file="" +arg="$1" +case "$arg" in + "-l") # files included in initramfs - used by kbuild + dep_list="list_" + echo "deps_initramfs := \\" + shift + ;; + "-o") # generate gzipped cpio image named $1 + shift + output_file="$1" + cpio_list="$(mktemp ${TMPDIR:-/tmp}/cpiolist.XXXXXX)" + output=${cpio_list} + shift + ;; +esac while [ $# -gt 0 ]; do arg="$1" shift case "$arg" in - "-u") + "-u") # map $1 to uid=0 (root) root_uid="$1" shift ;; - "-g") + "-g") # map $1 to gid=0 (root) root_gid="$1" shift ;; - "-d") + "-d") # display default initramfs list default_list="$arg" - default_initramfs + ${dep_list}default_initramfs ;; "-h") usage @@ -181,23 +261,27 @@ while [ $# -gt 0 ]; do *) case "$arg" in "-"*) - printf "ERROR: unknown option \"$arg\"\n" >&2 - printf "If the filename validly begins with '-', then it must be prefixed\n" >&2 - printf "by './' so that it won't be interpreted as an option." >&2 - printf "\n" >&2 - usage >&2 - exit 1 + unknown_option ;; - *) - cpio_source="$arg" - build_list + *) # input file/dir - process it + input_file "$arg" "$#" ;; esac ;; esac done -# spit out the default cpio list if a source hasn't been specified -[ -z "$cpio_source" -a -z "$default_list" ] && default_initramfs - +# If output_file is set we will generate cpio archive and gzip it +# we are carefull to delete tmp files +if [ ! -z ${output_file} ]; then + if [ -z ${cpio_file} ]; then + cpio_tfile="$(mktemp ${TMPDIR:-/tmp}/cpiofile.XXXXXX)" + usr/gen_init_cpio ${cpio_list} > ${cpio_tfile} + else + cpio_tfile=${cpio_file} + fi + rm ${cpio_list} + cat ${cpio_tfile} | gzip -f -9 - > ${output_file} + [ -z ${cpio_file} ] && rm ${cpio_tfile} +fi exit 0