7a2b48baab1fc26f7c085ed7176e1d37f619611b
[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     # Ignore fakeroot.log.
81     if [ "$path" = "./fakeroot.log" ]; then
82         :
83
84     # Kernel modules are always copied in from the host, including all
85     # the dependency information files.
86     elif [[ "$path" =~ '^\./lib/modules/' ]]; 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' ]]; then
95         echo "$path" >&5
96
97     # Always write directory names to both output files.
98     elif [ -d "$path" ]; then
99         echo "$path" >&5
100         echo "$path" >&6
101
102     # Some libraries need fixed version numbers replaced by wildcards.
103     elif [[ "$file" =~ '^ld-[.0-9]+\.so$' ]]; then
104         echo "$dir/ld-*.so" >&6
105
106     # Special case for libbfd
107     elif [[ "$file" =~ '^libbfd-.*\.so$' ]]; then
108         echo "$dir/libbfd-*.so" >&6
109
110     # libfoo-1.2.3.so
111     elif [[ "$file" =~ '^lib(.*)-[-.0-9]+\.so$' ]]; then
112         echo "$dir/lib${BASH_REMATCH[1]}-*.so" >&6
113
114     # libfoo-1.2.3.so.1.2.3 (but NOT '*.so.N')
115     elif [[ "$file" =~ '^lib(.*)-[-.0-9]+\.so\.([0-9]+)\.' ]]; then
116         echo "$dir/lib${BASH_REMATCH[1]}-*.so.${BASH_REMATCH[2]}.*" >&6
117
118     # libfoo.so.1.2.3 (but NOT '*.so.N')
119     elif [[ "$file" =~ '^lib(.*)\.so\.([0-9]+)\.' ]]; then
120         echo "$dir/lib${BASH_REMATCH[1]}.so.${BASH_REMATCH[2]}.*" >&6
121
122     else
123         # Anything else comes from the host directly.
124         echo "$path" >&6
125     fi
126 done
127
128 # Close output files.
129 exec 5>&-
130 exec 6>&-
131
132 # Now run febootstrap-to-initramfs to construct the supermin
133 # appliance.
134 if ! febootstrap-to-initramfs --nocompress --files="$tmpdir/keep" "$1" > "$2"
135 then
136     rm -f "$2"
137 fi