helper: Print /modules when verbose >= 2
[febootstrap.git] / febootstrap-to-supermin.sh
1 #!/bin/bash -
2 # febootstrap-to-supermin
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 \
26         -n febootstrap-to-supermin -- "$@"`
27 if [ $? != 0 ]; then
28     echo "febootstrap-to-supermin: problem parsing the command line arguments"
29     exit 1
30 fi
31 eval set -- "$TEMP"
32
33 usage ()
34 {
35     echo "Usage: febootstrap-to-supermin DIR supermin.img hostfiles.txt"
36     echo "Please read febootstrap-to-supermin(8) man page for more information."
37 }
38
39 while true; do
40     case "$1" in
41         --help)
42             usage
43             exit 0;;
44         --)
45             shift
46             break;;
47         *)
48             echo "Internal error!"
49             exit 1;;
50     esac
51 done
52
53 if [ $# -ne 3 ]; then
54     usage
55     exit 1
56 fi
57
58 set -e
59
60 # Create a temporary directory, make sure it gets cleaned up at the end.
61 tmpdir=$(mktemp -d)
62 remove_tmpdir ()
63 {
64   status=$?
65   rm -rf "$tmpdir" && exit $status
66 }
67 trap remove_tmpdir EXIT
68
69 # Get the complete list of files and directories in the appliance.
70 (cd "$1" > /dev/null && find) > "$tmpdir/files"
71
72 exec 5>"$tmpdir/keep"           # Files/dirs we will keep in supermin.img
73 exec 6>$3                       # hostfiles.txt (output)
74 exec 7<"$tmpdir/files"
75
76 while read path <&7; do
77     dir=$(dirname "$path")
78     file=$(basename "$path")
79
80     # For quoting problems with the bash =~ operator, see bash FAQ
81     # question E14 here http://tiswww.case.edu/php/chet/bash/FAQ and
82     # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=487387#25
83     # (RHBZ#566511).
84     p_etc='^\./etc'
85     p_dev='^\./dev'
86     p_var='^\./var'
87     p_lib_modules='^\./lib/modules/'
88     p_builddir='^\./builddir'
89     p_ld_so='^ld-[.0-9]+\.so$'
90     p_libbfd='^libbfd-.*\.so$'
91     p_libgcc='^libgcc_s-.*\.so\.([0-9]+)$'
92     p_libntfs3g='^libntfs-3g\.so\..*$'
93     p_lib123so='^lib(.*)-[-.0-9]+\.so$'
94     p_lib123so123='^lib(.*)-[-.0-9]+\.so\.([0-9]+)\.'
95     p_libso123='^lib(.*)\.so\.([0-9]+)\.'
96
97     # Ignore fakeroot.log.
98     if [ "$path" = "./fakeroot.log" ]; then
99         :
100
101     # All we're going to keep are the special files /init, the daemon,
102     # configuration files (/etc), devices and modifiable stuff (/var).
103     elif [ "$path" = "./init" ]; then
104         echo "$path" >&5
105
106     # Get timezone configuration from local system.
107     elif [ "$path" = "./etc/localtime" ]; then
108         echo "$path" >&6
109
110     elif [[ "$path" =~ $p_etc || "$path" =~ $p_dev || "$path" =~ $p_var ]]
111     then
112         echo "$path" >&5
113
114     # Kernel modules are always copied in from the host, including all
115     # the dependency information files.
116     elif [[ "$path" =~ $p_lib_modules ]]; then
117         :
118
119     # On mock/Koji, exclude bogus /builddir directory which for some
120     # reason contains some yum temporary files (RHBZ#566512).
121     elif [[ "$path" =~ $p_builddir ]]; then
122         :
123
124     # Always write directory names to both output files.
125     elif [ -d "$path" ]; then
126         echo "$path" >&5
127         echo "$path" >&6
128
129     # Some libraries need fixed version numbers replaced by wildcards.
130
131     elif [[ "$file" =~ $p_ld_so ]]; then
132         echo "$dir/ld-*.so" >&6
133
134     # Special case for libbfd
135     elif [[ "$file" =~ $p_libbfd ]]; then
136         echo "$dir/libbfd-*.so" >&6
137
138     # Special case for libgcc_s-<gccversion>-<date>.so.N
139     elif [[ "$file" =~ $p_libgcc ]]; then
140         echo "$dir/libgcc_s-*.so.${BASH_REMATCH[1]}" >&6
141
142     # Special case for libntfs-3g.so.*
143     elif [[ "$file" =~ $p_libntfs3g ]]; then
144        [ -n "$libntfs3g_once" ] || echo "$dir/libntfs-3g.so.*" >&6
145        libntfs3g_once=1
146
147     # libfoo-1.2.3.so
148     elif [[ "$file" =~ $p_lib123so ]]; then
149         echo "$dir/lib${BASH_REMATCH[1]}-*.so" >&6
150
151     # libfoo-1.2.3.so.1.2.3 (but NOT '*.so.N')
152     elif [[ "$file" =~ $p_lib123so123 ]]; then
153         echo "$dir/lib${BASH_REMATCH[1]}-*.so.${BASH_REMATCH[2]}.*" >&6
154
155     # libfoo.so.1.2.3 (but NOT '*.so.N')
156     elif [[ "$file" =~ $p_libso123 ]]; then
157         echo "$dir/lib${BASH_REMATCH[1]}.so.${BASH_REMATCH[2]}.*" >&6
158
159     else
160         # Anything else comes from the host directly.
161         echo "$path" >&6
162     fi
163 done
164
165 # Close output files.
166 exec 5>&-
167 exec 6>&-
168
169 # Now run febootstrap-to-initramfs to construct the supermin
170 # appliance.
171 if ! febootstrap-to-initramfs --nocompress --files="$tmpdir/keep" "$1" > "$2"
172 then
173     rm -f "$2"
174 fi