59b810396c7b8994396b5e03c7656722d9e3915d
[linux-flexiantxendom0-3.2.10.git] / scripts / gen_initramfs_list.sh
1 #!/bin/bash
2 # Copyright (C) Martin Schlemmer <azarah@nosferatu.za.org>
3 # Released under the terms of the GNU GPL
4 #
5 # A script to generate newline separated entries (to stdout) from a directory's
6 # contents suitable for use as a cpio_list for gen_init_cpio.
7 #
8 # Arguements: $1 -- the source directory
9 #
10 # TODO:  Add support for symlinks, sockets and pipes when gen_init_cpio
11 #        supports them.
12
13 usage() {
14         echo "Usage: $0 initramfs-source-dir"
15         exit 1
16 }
17
18 srcdir=$(echo "$1" | sed -e 's://*:/:g')
19
20 if [ "$#" -gt 1 -o ! -d "${srcdir}" ]; then
21         usage
22 fi
23
24 filetype() {
25         local argv1="$1"
26
27         if [ -f "${argv1}" ]; then
28                 echo "file"
29         elif [ -d "${argv1}" ]; then
30                 echo "dir"
31         elif [ -b "${argv1}" -o -c "${argv1}" ]; then
32                 echo "nod"
33         else
34                 echo "invalid"
35         fi
36         return 0
37 }
38
39 parse() {
40         local location="$1"
41         local name="${location/${srcdir}//}"
42         local mode="$2"
43         local uid="$3"
44         local gid="$4"
45         local ftype=$(filetype "${location}")
46         local str="${mode} ${uid} ${gid}"
47
48         [ "${ftype}" == "invalid" ] && return 0
49         [ "${location}" == "${srcdir}" ] && return 0
50
51         case "${ftype}" in
52                 "file")
53                         str="${ftype} ${name} ${location} ${str}"
54                         ;;
55                 "nod")
56                         local dev_type=
57                         local maj=$(LC_ALL=C ls -l "${location}" | \
58                                         gawk '{sub(/,/, "", $5); print $5}')
59                         local min=$(LC_ALL=C ls -l "${location}" | \
60                                         gawk '{print $6}')
61
62                         if [ -b "${location}" ]; then
63                                 dev_type="b"
64                         else
65                                 dev_type="c"
66                         fi
67                         str="${ftype} ${name} ${str} ${dev_type} ${maj} ${min}"
68                         ;;
69                 *)
70                         str="${ftype} ${name} ${str}"
71                         ;;
72         esac
73
74         echo "${str}"
75
76         return 0
77 }
78
79 find "${srcdir}" -printf "%p %m %U %G\n" | \
80 while read x; do
81         parse ${x}
82 done
83
84 exit 0