ia64/linux-2.6.18-xen.hg

annotate scripts/gen_initramfs_list.sh @ 562:66faefe721eb

pvSCSI backend driver

Signed-off-by: Tomonari Horikoshi <t.horikoshi@jp.fujitsu.com>
Signed-off-by: Jun Kamada <kama@jp.fujitsu.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Jun 02 09:58:27 2008 +0100 (2008-06-02)
parents 831230e53067
children
rev   line source
ian@0 1 #!/bin/bash
ian@0 2 # Copyright (C) Martin Schlemmer <azarah@nosferatu.za.org>
ian@0 3 # Copyright (c) 2006 Sam Ravnborg <sam@ravnborg.org>
ian@0 4 #
ian@0 5 # Released under the terms of the GNU GPL
ian@0 6 #
ian@0 7 # Generate a cpio packed initramfs. It uses gen_init_cpio to generate
ian@0 8 # the cpio archive, and gzip to pack it.
ian@0 9 # The script may also be used to generate the inputfile used for gen_init_cpio
ian@0 10 # This script assumes that gen_init_cpio is located in usr/ directory
ian@0 11
ian@0 12 # error out on errors
ian@0 13 set -e
ian@0 14
ian@0 15 usage() {
ian@0 16 cat << EOF
ian@0 17 Usage:
ian@0 18 $0 [-o <file>] [-u <uid>] [-g <gid>] {-d | <cpio_source>} ...
ian@0 19 -o <file> Create gzipped initramfs file named <file> using
ian@0 20 gen_init_cpio and gzip
ian@0 21 -u <uid> User ID to map to user ID 0 (root).
ian@0 22 <uid> is only meaningful if <cpio_source>
ian@0 23 is a directory.
ian@0 24 -g <gid> Group ID to map to group ID 0 (root).
ian@0 25 <gid> is only meaningful if <cpio_source>
ian@0 26 is a directory.
ian@0 27 <cpio_source> File list or directory for cpio archive.
ian@0 28 If <cpio_source> is a .cpio file it will be used
ian@0 29 as direct input to initramfs.
ian@0 30 -d Output the default cpio list.
ian@0 31
ian@0 32 All options except -o and -l may be repeated and are interpreted
ian@0 33 sequentially and immediately. -u and -g states are preserved across
ian@0 34 <cpio_source> options so an explicit "-u 0 -g 0" is required
ian@0 35 to reset the root/group mapping.
ian@0 36 EOF
ian@0 37 }
ian@0 38
ian@0 39 list_default_initramfs() {
ian@0 40 # echo usr/kinit/kinit
ian@0 41 :
ian@0 42 }
ian@0 43
ian@0 44 default_initramfs() {
ian@0 45 cat <<-EOF >> ${output}
ian@0 46 # This is a very simple, default initramfs
ian@0 47
ian@0 48 dir /dev 0755 0 0
ian@0 49 nod /dev/console 0600 0 0 c 5 1
ian@0 50 dir /root 0700 0 0
ian@0 51 # file /kinit usr/kinit/kinit 0755 0 0
ian@0 52 # slink /init kinit 0755 0 0
ian@0 53 EOF
ian@0 54 }
ian@0 55
ian@0 56 filetype() {
ian@0 57 local argv1="$1"
ian@0 58
ian@0 59 # symlink test must come before file test
ian@0 60 if [ -L "${argv1}" ]; then
ian@0 61 echo "slink"
ian@0 62 elif [ -f "${argv1}" ]; then
ian@0 63 echo "file"
ian@0 64 elif [ -d "${argv1}" ]; then
ian@0 65 echo "dir"
ian@0 66 elif [ -b "${argv1}" -o -c "${argv1}" ]; then
ian@0 67 echo "nod"
ian@0 68 elif [ -p "${argv1}" ]; then
ian@0 69 echo "pipe"
ian@0 70 elif [ -S "${argv1}" ]; then
ian@0 71 echo "sock"
ian@0 72 else
ian@0 73 echo "invalid"
ian@0 74 fi
ian@0 75 return 0
ian@0 76 }
ian@0 77
ian@0 78 list_print_mtime() {
ian@0 79 :
ian@0 80 }
ian@0 81
ian@0 82 print_mtime() {
ian@0 83 local my_mtime="0"
ian@0 84
ian@0 85 if [ -e "$1" ]; then
ian@0 86 my_mtime=$(find "$1" -printf "%T@\n" | sort -r | head -n 1)
ian@0 87 fi
ian@0 88
ian@0 89 echo "# Last modified: ${my_mtime}" >> ${output}
ian@0 90 echo "" >> ${output}
ian@0 91 }
ian@0 92
ian@0 93 list_parse() {
ian@0 94 echo "$1 \\"
ian@0 95 }
ian@0 96
ian@0 97 # for each file print a line in following format
ian@0 98 # <filetype> <name> <path to file> <octal mode> <uid> <gid>
ian@0 99 # for links, devices etc the format differs. See gen_init_cpio for details
ian@0 100 parse() {
ian@0 101 local location="$1"
ian@0 102 local name="${location/${srcdir}//}"
ian@0 103 # change '//' into '/'
ian@0 104 name="${name//\/\///}"
ian@0 105 local mode="$2"
ian@0 106 local uid="$3"
ian@0 107 local gid="$4"
ian@0 108 local ftype=$(filetype "${location}")
ian@0 109 # remap uid/gid to 0 if necessary
ian@0 110 [ "$uid" -eq "$root_uid" ] && uid=0
ian@0 111 [ "$gid" -eq "$root_gid" ] && gid=0
ian@0 112 local str="${mode} ${uid} ${gid}"
ian@0 113
ian@0 114 [ "${ftype}" == "invalid" ] && return 0
ian@0 115 [ "${location}" == "${srcdir}" ] && return 0
ian@0 116
ian@0 117 case "${ftype}" in
ian@0 118 "file")
ian@0 119 str="${ftype} ${name} ${location} ${str}"
ian@0 120 ;;
ian@0 121 "nod")
ian@0 122 local dev_type=
ian@0 123 local maj=$(LC_ALL=C ls -l "${location}" | \
ian@0 124 gawk '{sub(/,/, "", $5); print $5}')
ian@0 125 local min=$(LC_ALL=C ls -l "${location}" | \
ian@0 126 gawk '{print $6}')
ian@0 127
ian@0 128 if [ -b "${location}" ]; then
ian@0 129 dev_type="b"
ian@0 130 else
ian@0 131 dev_type="c"
ian@0 132 fi
ian@0 133 str="${ftype} ${name} ${str} ${dev_type} ${maj} ${min}"
ian@0 134 ;;
ian@0 135 "slink")
ian@0 136 local target=$(LC_ALL=C ls -l "${location}" | \
ian@0 137 gawk '{print $11}')
ian@0 138 str="${ftype} ${name} ${target} ${str}"
ian@0 139 ;;
ian@0 140 *)
ian@0 141 str="${ftype} ${name} ${str}"
ian@0 142 ;;
ian@0 143 esac
ian@0 144
ian@0 145 echo "${str}" >> ${output}
ian@0 146
ian@0 147 return 0
ian@0 148 }
ian@0 149
ian@0 150 unknown_option() {
ian@0 151 printf "ERROR: unknown option \"$arg\"\n" >&2
ian@0 152 printf "If the filename validly begins with '-', " >&2
ian@0 153 printf "then it must be prefixed\n" >&2
ian@0 154 printf "by './' so that it won't be interpreted as an option." >&2
ian@0 155 printf "\n" >&2
ian@0 156 usage >&2
ian@0 157 exit 1
ian@0 158 }
ian@0 159
ian@0 160 list_header() {
ian@0 161 echo "deps_initramfs := \\"
ian@0 162 }
ian@0 163
ian@0 164 header() {
ian@0 165 printf "\n#####################\n# $1\n" >> ${output}
ian@0 166 }
ian@0 167
ian@0 168 # process one directory (incl sub-directories)
ian@0 169 dir_filelist() {
ian@0 170 ${dep_list}header "$1"
ian@0 171
ian@0 172 srcdir=$(echo "$1" | sed -e 's://*:/:g')
ian@0 173 dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n" 2>/dev/null)
ian@0 174
ian@0 175 # If $dirlist is only one line, then the directory is empty
ian@0 176 if [ "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then
ian@0 177 ${dep_list}print_mtime "$1"
ian@0 178
ian@0 179 echo "${dirlist}" | \
ian@0 180 while read x; do
ian@0 181 ${dep_list}parse ${x}
ian@0 182 done
ian@0 183 fi
ian@0 184 }
ian@0 185
ian@0 186 # if only one file is specified and it is .cpio file then use it direct as fs
ian@0 187 # if a directory is specified then add all files in given direcotry to fs
ian@0 188 # if a regular file is specified assume it is in gen_initramfs format
ian@0 189 input_file() {
ian@0 190 source="$1"
ian@0 191 if [ -f "$1" ]; then
ian@0 192 ${dep_list}header "$1"
ian@0 193 is_cpio="$(echo "$1" | sed 's/^.*\.cpio/cpio/')"
ian@0 194 if [ $2 -eq 0 -a ${is_cpio} == "cpio" ]; then
ian@0 195 cpio_file=$1
ian@0 196 [ ! -z ${dep_list} ] && echo "$1"
ian@0 197 return 0
ian@0 198 fi
ian@0 199 if [ -z ${dep_list} ]; then
ian@0 200 print_mtime "$1" >> ${output}
ian@0 201 cat "$1" >> ${output}
ian@0 202 else
ian@0 203 cat "$1" | while read type dir file perm ; do
ian@0 204 if [ "$type" == "file" ]; then
ian@0 205 echo "$file \\";
ian@0 206 fi
ian@0 207 done
ian@0 208 fi
ian@0 209 elif [ -d "$1" ]; then
ian@0 210 dir_filelist "$1"
ian@0 211 else
ian@0 212 echo " ${prog}: Cannot open '$1'" >&2
ian@0 213 exit 1
ian@0 214 fi
ian@0 215 }
ian@0 216
ian@0 217 prog=$0
ian@0 218 root_uid=0
ian@0 219 root_gid=0
ian@0 220 dep_list=
ian@0 221 cpio_file=
ian@0 222 cpio_list=
ian@0 223 output="/dev/stdout"
ian@0 224 output_file=""
ian@0 225
ian@0 226 arg="$1"
ian@0 227 case "$arg" in
ian@0 228 "-l") # files included in initramfs - used by kbuild
ian@0 229 dep_list="list_"
ian@0 230 shift
ian@0 231 ;;
ian@0 232 "-o") # generate gzipped cpio image named $1
ian@0 233 shift
ian@0 234 output_file="$1"
ian@0 235 cpio_list="$(mktemp ${TMPDIR:-/tmp}/cpiolist.XXXXXX)"
ian@0 236 output=${cpio_list}
ian@0 237 shift
ian@0 238 ;;
ian@0 239 esac
ian@0 240 while [ $# -gt 0 ]; do
ian@0 241 arg="$1"
ian@0 242 shift
ian@0 243 case "$arg" in
ian@0 244 "-u") # map $1 to uid=0 (root)
ian@0 245 root_uid="$1"
ian@0 246 shift
ian@0 247 ;;
ian@0 248 "-g") # map $1 to gid=0 (root)
ian@0 249 root_gid="$1"
ian@0 250 shift
ian@0 251 ;;
ian@0 252 "-d") # display default initramfs list
ian@0 253 default_list="$arg"
ian@0 254 ${dep_list}default_initramfs
ian@0 255 ;;
ian@0 256 "-h")
ian@0 257 usage
ian@0 258 exit 0
ian@0 259 ;;
ian@0 260 *)
ian@0 261 case "$arg" in
ian@0 262 "-"*)
ian@0 263 unknown_option
ian@0 264 ;;
ian@0 265 *) # input file/dir - process it
ian@0 266 input_file "$arg" "$#"
ian@0 267 ;;
ian@0 268 esac
ian@0 269 ;;
ian@0 270 esac
ian@0 271 done
ian@0 272
ian@0 273 # If output_file is set we will generate cpio archive and gzip it
ian@0 274 # we are carefull to delete tmp files
ian@0 275 if [ ! -z ${output_file} ]; then
ian@0 276 if [ -z ${cpio_file} ]; then
ian@0 277 cpio_tfile="$(mktemp ${TMPDIR:-/tmp}/cpiofile.XXXXXX)"
ian@0 278 usr/gen_init_cpio ${cpio_list} > ${cpio_tfile}
ian@0 279 else
ian@0 280 cpio_tfile=${cpio_file}
ian@0 281 fi
ian@0 282 rm ${cpio_list}
ian@0 283 cat ${cpio_tfile} | gzip -f -9 - > ${output_file}
ian@0 284 [ -z ${cpio_file} ] && rm ${cpio_tfile}
ian@0 285 fi
ian@0 286 exit 0