This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.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 print_mtime() {
41         local argv1="$1"
42         local my_mtime="0"
43
44         if [ -e "${argv1}" ]; then
45                 my_mtime=$(find "${argv1}" -printf "%T@\n" | sort -r | head -n 1)
46         fi
47         
48         echo "# Last modified: ${my_mtime}"
49         echo
50 }
51
52 parse() {
53         local location="$1"
54         local name="${location/${srcdir}//}"
55         local mode="$2"
56         local uid="$3"
57         local gid="$4"
58         local ftype=$(filetype "${location}")
59         local str="${mode} ${uid} ${gid}"
60
61         [ "${ftype}" == "invalid" ] && return 0
62         [ "${location}" == "${srcdir}" ] && return 0
63
64         case "${ftype}" in
65                 "file")
66                         str="${ftype} ${name} ${location} ${str}"
67                         ;;
68                 "nod")
69                         local dev_type=
70                         local maj=$(LC_ALL=C ls -l "${location}" | \
71                                         gawk '{sub(/,/, "", $5); print $5}')
72                         local min=$(LC_ALL=C ls -l "${location}" | \
73                                         gawk '{print $6}')
74
75                         if [ -b "${location}" ]; then
76                                 dev_type="b"
77                         else
78                                 dev_type="c"
79                         fi
80                         str="${ftype} ${name} ${str} ${dev_type} ${maj} ${min}"
81                         ;;
82                 *)
83                         str="${ftype} ${name} ${str}"
84                         ;;
85         esac
86
87         echo "${str}"
88
89         return 0
90 }
91
92 if [ -z "$1" ]; then
93         simple_initramfs
94 elif [ -f "$1" ]; then
95         print_mtime "$1"
96         cat "$1"
97 elif [ -d "$1" ]; then
98         srcdir=$(echo "$1" | sed -e 's://*:/:g')
99         dirlist=$(find "${srcdir}" -printf "%p %m %U %G\n" 2>/dev/null)
100
101         # If $dirlist is only one line, then the directory is empty
102         if [  "$(echo "${dirlist}" | wc -l)" -gt 1 ]; then
103                 print_mtime "$1"
104                 
105                 echo "${dirlist}" | \
106                 while read x; do
107                         parse ${x}
108                 done
109         else
110                 # Failsafe in case directory is empty
111                 simple_initramfs
112         fi
113 else
114         echo "  $0: Cannot open '$1' (CONFIG_INITRAMFS_SOURCE)" >&2
115         exit 1
116 fi
117
118 exit 0