kbuild/usr: initramfs list fixed and simplified
[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 # Generate a newline separated list of entries from the file/directory pointed
6 # out by the environment variable: CONFIG_INITRAMFS_SOURCE
7 #
8 # If CONFIG_INITRAMFS_SOURCE is non-existing then generate a small dummy file.
9 #
10 # The output is suitable for gen_init_cpio as found in usr/Makefile.
11 #
12 # TODO:  Add support for symlinks, sockets and pipes when gen_init_cpio
13 #        supports them.
14
15 simple_initramfs() {
16         cat <<-EOF
17                 # This is a very simple initramfs
18
19                 dir /dev 0755 0 0
20                 nod /dev/console 0600 0 0 c 5 1
21                 dir /root 0700 0 0
22         EOF
23 }
24
25 filetype() {
26         local argv1="$1"
27
28         if [ -f "${argv1}" ]; then
29                 echo "file"
30         elif [ -d "${argv1}" ]; then
31                 echo "dir"
32         elif [ -b "${argv1}" -o -c "${argv1}" ]; then
33                 echo "nod"
34         else
35                 echo "invalid"
36         fi
37         return 0
38 }
39
40 parse() {
41         local location="$1"
42         local name="${location/${srcdir}//}"
43         local mode="$2"
44         local uid="$3"
45         local gid="$4"
46         local ftype=$(filetype "${location}")
47         local str="${mode} ${uid} ${gid}"
48
49         [ "${ftype}" == "invalid" ] && return 0
50         [ "${location}" == "${srcdir}" ] && return 0
51
52         case "${ftype}" in
53                 "file")
54                         str="${ftype} ${name} ${location} ${str}"
55                         ;;
56                 "nod")
57                         local dev_type=
58                         local maj=$(LC_ALL=C ls -l "${location}" | \
59                                         gawk '{sub(/,/, "", $5); print $5}')
60                         local min=$(LC_ALL=C ls -l "${location}" | \
61                                         gawk '{print $6}')
62
63                         if [ -b "${location}" ]; then
64                                 dev_type="b"
65                         else
66                                 dev_type="c"
67                         fi
68                         str="${ftype} ${name} ${str} ${dev_type} ${maj} ${min}"
69                         ;;
70                 *)
71                         str="${ftype} ${name} ${str}"
72                         ;;
73         esac
74
75         echo "${str}"
76
77         return 0
78 }
79
80 if [ -z $1 ]; then
81         simple_initramfs
82 elif [ -f $1 ]; then
83         cat $1
84 elif [ -d $1 ]; then
85         srcdir=$(echo "$1" | sed -e 's://*:/:g')
86         dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n" 2>/dev/null)
87
88         # If $dirlist is only one line, then the directory is empty
89         if [  "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then
90                 echo "${dirlist}" | \
91                 while read x; do
92                         parse ${x}
93                 done
94         else
95                 # Failsafe in case directory is empty
96                 simple_initramfs
97         fi
98 else
99         echo "  $0: Cannot open '$1' (CONFIG_INITRAMFS_SOURCE)" >&2
100         exit 1
101 fi
102
103 exit 0