helper: Print /modules when verbose >= 2
[febootstrap.git] / febootstrap-supermin-helper.sh
1 #!/bin/bash -
2 # febootstrap-supermin-helper
3 # (C) Copyright 2009 Red Hat Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #
19 # Written by Richard W.M. Jones <rjones@redhat.com>
20
21 unset CDPATH
22
23 TEMP=`getopt \
24         -o '' \
25         --long help,kmods: \
26         -n febootstrap-supermin-helper -- "$@"`
27 if [ $? != 0 ]; then
28     echo "febootstrap-supermin-helper: problem parsing the command line arguments"
29     exit 1
30 fi
31 eval set -- "$TEMP"
32
33 usage ()
34 {
35     echo "Usage: febootstrap-supermin-helper supermin.img hostfiles.txt host_cpu kernel initrd"
36     echo "Please read febootstrap-supermin-helper(8) man page for more information."
37 }
38
39 kmods=""
40
41 while true; do
42     case "$1" in
43         --help)
44             usage
45             exit 0;;
46         --kmods)
47             kmods=$2
48             shift 2;;
49         --)
50             shift
51             break;;
52         *)
53             echo "Internal error!"
54             exit 1;;
55     esac
56 done
57
58 if [ $# -ne 5 ]; then
59     usage
60     exit 1
61 fi
62
63 set -e
64
65 # Input files.
66 supermin="$1"
67 hostfiles="$2"
68
69 host_cpu=$3
70
71 # Output files.
72 kernel="$4"
73 initrd="$5"
74
75 rm -f "$kernel" "$initrd"
76
77 # Kernel:
78 # Look for the most recent kernel named vmlinuz-*.<arch>* which has a
79 # corresponding directory in /lib/modules/. If the architecture is x86, look
80 # for any x86 kernel.
81 #
82 # RHEL 5 didn't append the arch to the kernel name, so look for kernels
83 # without arch second.
84
85 arch=$(echo $host_cpu | sed 's/^i.86$/i?86/')
86 kernels=$(
87     ls -1dvr /boot/vmlinuz-*.$arch* 2>/dev/null | grep -v xen ||: ;
88     ls -1dvr /boot/vmlinuz-* 2>/dev/null | grep -v xen
89 )
90
91 if [ -z "$kernels" ]; then
92     echo "$0: failed to find a suitable kernel in /boot directory" >&2
93     exit 1
94 fi
95
96 for f in $kernels; do
97     b=$(basename "$f")
98     b=$(echo "$b" | sed 's,vmlinuz-,,')
99     modpath="/lib/modules/$b"
100     if [ -d "$modpath" ]; then
101         ln -sf "$f" "$kernel"
102         break
103     fi
104     modpath=
105 done
106
107 if [ -z "$modpath" ]; then
108     echo "$0: failed to find a suitable kernel" >&2
109     exit 1
110 fi
111
112 # The initrd consists of these components:
113 # (1) The base skeleton appliance that we constructed at build time.
114 #     format = plain cpio (could be compressed cpio)
115 # (2) The modules from modpath which are on the module whitelist.
116 #     format = plain cpio
117 # (3) The host files which match wildcards in hostfiles.
118 #     format = plain cpio
119
120 cp "$supermin" "$initrd" ;# (1)
121
122 # Kernel modules (2).
123
124 if [ -n "$kmods" ]; then
125     exec 5<"$kmods"
126     whitelist=
127     while read kmod 0<&5; do
128         whitelist="$whitelist -o -name $kmod"
129     done
130     exec 5<&-
131 else
132     whitelist="-o -name *.ko"
133 fi
134
135 find "$modpath" \( -not -name '*.ko' $whitelist \) -a -print0 |
136   cpio --quiet -o -0 -H newc >> "$initrd"
137
138 # Host files (3).
139
140 hostfiles=$(readlink -f "$hostfiles")
141 (cd / &&
142     ls -1df $(cat "$hostfiles") 2>/dev/null |
143     cpio -C 65536 --quiet -o -H newc ) >> "$initrd"