Backport supermin hostfiles / bash quoting fix from libguestfs.
[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
85     # Ignore fakeroot.log.
86     if [ "$path" = "./fakeroot.log" ]; then
87         :
88
89     # All we're going to keep are the special files /init, the daemon,
90     # configuration files (/etc), devices and modifiable stuff (/var).
91     elif [ "$path" = "./init" ]; then
92         echo "$path" >&5
93
94     elif [[ "$path" =~ ^\./etc || "$path" =~ ^\./dev || "$path" =~ ^\./var ]]
95     then
96         echo "$path" >&5
97
98     # Kernel modules are always copied in from the host, including all
99     # the dependency information files.
100     elif [[ "$path" =~ ^\./lib/modules/ ]]; then
101         :
102
103     # On mock/Koji, exclude bogus /builddir directory which for some
104     # reason contains some yum temporary files (RHBZ#566512).
105     elif [[ "$path" =~ ^\./builddir ]]; then
106         :
107
108     # Always write directory names to both output files.
109     elif [ -d "$path" ]; then
110         echo "$path" >&5
111         echo "$path" >&6
112
113     # Some libraries need fixed version numbers replaced by wildcards.
114
115     elif [[ "$file" =~ ^ld-[.0-9]+\.so$ ]]; then
116         echo "$dir/ld-*.so" >&6
117
118     # Special case for libbfd
119     elif [[ "$file" =~ ^libbfd-.*\.so$ ]]; then
120         echo "$dir/libbfd-*.so" >&6
121
122     # Special case for libgcc_s-<gccversion>-<date>.so.N
123     elif [[ "$file" =~ ^libgcc_s-.*\.so\.([0-9]+)$ ]]; then
124         echo "$dir/libgcc_s-*.so.${BASH_REMATCH[1]}" >&6
125
126     # libfoo-1.2.3.so
127     elif [[ "$file" =~ ^lib(.*)-[-.0-9]+\.so$ ]]; then
128         echo "$dir/lib${BASH_REMATCH[1]}-*.so" >&6
129
130     # libfoo-1.2.3.so.1.2.3 (but NOT '*.so.N')
131     elif [[ "$file" =~ ^lib(.*)-[-.0-9]+\.so\.([0-9]+)\. ]]; then
132         echo "$dir/lib${BASH_REMATCH[1]}-*.so.${BASH_REMATCH[2]}.*" >&6
133
134     # libfoo.so.1.2.3 (but NOT '*.so.N')
135     elif [[ "$file" =~ ^lib(.*)\.so\.([0-9]+)\. ]]; then
136         echo "$dir/lib${BASH_REMATCH[1]}.so.${BASH_REMATCH[2]}.*" >&6
137
138     else
139         # Anything else comes from the host directly.
140         echo "$path" >&6
141     fi
142 done
143
144 # Close output files.
145 exec 5>&-
146 exec 6>&-
147
148 # Now run febootstrap-to-initramfs to construct the supermin
149 # appliance.
150 if ! febootstrap-to-initramfs --nocompress --files="$tmpdir/keep" "$1" > "$2"
151 then
152     rm -f "$2"
153 fi