supermin: Ignore more unreadable files found on RHEL 6.
[libguestfs.git] / appliance / libguestfs-supermin-helper.in
1 #!/bin/bash -
2 # @configure_input@
3 # Copyright (C) 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 # Helper script which constructs the supermin appliance at runtime.
20
21 unset CDPATH
22
23 set -e
24
25 # Source directory containing the supermin input files.
26 sourcedir=$(cd "$1" > /dev/null; pwd)
27
28 # Output files.
29 kernel="$2"
30 initrd="$3"
31
32 # Kernel:
33 # Look for the most recent kernel named vmlinuz-*.<arch>* which has a
34 # corresponding directory in /lib/modules/. If the architecture is x86, look
35 # for any x86 kernel.
36 #
37 # RHEL 5 didn't append the arch to the kernel name, so look for kernels
38 # without arch second.
39
40 arch=$(echo "@host_cpu@" | sed 's/^i.86$/i?86/')
41 kernels=$(
42     ls -1dvr /boot/vmlinuz-*.$arch* 2>/dev/null | grep -v xen ||: ;
43     ls -1dvr /boot/vmlinuz-* 2>/dev/null | grep -v xen
44 )
45
46 if [ -z "$kernels" ]; then
47     echo "$0: failed to find a suitable kernel in /boot directory" >&2
48     exit 1
49 fi
50
51 for f in $kernels; do
52     b=$(basename "$f")
53     b=$(echo "$b" | sed 's,vmlinuz-,,')
54     modpath="/lib/modules/$b"
55     if [ -d "$modpath" ]; then
56         ln -sf "$f" "$kernel"
57         break
58     fi
59     modpath=
60 done
61
62 if [ -z "$modpath" ]; then
63     echo "$0: failed to find a suitable kernel" >&2
64     exit 1
65 fi
66
67 # The initrd consists of these components:
68 # (1) The base skeleton appliance that we constructed at build time.
69 #     name = initramfs.@REPO@.@host_cpu@.supermin.img
70 #     format = compressed cpio
71 # (2) The modules from modpath which are on the module whitelist.
72 #     format = plain cpio
73 # (3) The host files which match wildcards in *.supermin.hostfiles.
74 #     format = plain cpio
75
76 cp "$sourcedir"/initramfs.@REPO@.@host_cpu@.supermin.img "$initrd"
77
78 # Kernel modules (2).
79 exec 5<"$sourcedir"/kmod.whitelist
80 whitelist=
81 while read kmod 0<&5; do
82     whitelist="$whitelist -o -name $kmod"
83 done
84 exec 5<&-
85
86 find "$modpath" \( -not -name '*.ko' $whitelist \) -a -print0 |
87   cpio --quiet -o -0 -H newc >> "$initrd"
88
89 # Host files (3).
90
91 (cd / && \
92   ls -1df $(
93       cat "$sourcedir"/initramfs.@REPO@.@host_cpu@.supermin.hostfiles
94     ) 2>/dev/null |
95   cpio -C 65536 --quiet -o -H newc ) >> "$initrd"