supermin: Prevent multilib corruption (RHBZ#558593).
[libguestfs.git] / appliance / libguestfs-supermin-helper
1 #!/bin/bash -
2 # Copyright (C) 2009-2010 Red Hat Inc.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 # Helper script which constructs the supermin appliance at runtime.
19
20 unset CDPATH
21
22 set -e
23
24 if [ $# -ne 5 ]; then
25     p=`basename $0`
26     echo
27     echo "$p: Create supermin appliance."
28     echo
29     echo "Usage:"
30     echo "$p sourcedir host_cpu repo kernel initrd"
31     echo
32     echo "This script is used by libguestfs to build the supermin appliance"
33     echo "(kernel and initrd output files).  You should NOT need to run this"
34     echo "program directly except if you are debugging tricky supermin"
35     echo "appliance problems."
36     echo
37     echo "NB: The kernel and initrd parameters are OUTPUT parameters.  If"
38     echo "those files exist, they are overwritten by the output."
39     echo
40     echo "Typical usage when debugging supermin appliance problems:"
41     echo "  $p /usr/lib64/guestfs x86_64 fedora-12 /tmp/kernel /tmp/initrd"
42     echo "Note: This will OVERWRITE any existing files called /tmp/kernel"
43     echo "and /tmp/initrd."
44     echo
45     exit 1
46 fi
47
48 # Source directory containing the supermin input files.
49 sourcedir=$(cd "$1" > /dev/null; pwd)
50
51 # Host CPU and repo constants passed from the library (see:
52 # https://bugzilla.redhat.com/show_bug.cgi?id=558593).
53 host_cpu=$2
54 repo=$3
55
56 # Output files.
57 kernel="$4"
58 initrd="$5"
59
60 # Kernel:
61 # Look for the most recent kernel named vmlinuz-*.<arch>* which has a
62 # corresponding directory in /lib/modules/. If the architecture is x86, look
63 # for any x86 kernel.
64 #
65 # RHEL 5 didn't append the arch to the kernel name, so look for kernels
66 # without arch second.
67
68 arch=$(echo $host_cpu | sed 's/^i.86$/i?86/')
69 kernels=$(
70     ls -1dvr /boot/vmlinuz-*.$arch* 2>/dev/null | grep -v xen ||: ;
71     ls -1dvr /boot/vmlinuz-* 2>/dev/null | grep -v xen
72 )
73
74 if [ -z "$kernels" ]; then
75     echo "$0: failed to find a suitable kernel in /boot directory" >&2
76     exit 1
77 fi
78
79 for f in $kernels; do
80     b=$(basename "$f")
81     b=$(echo "$b" | sed 's,vmlinuz-,,')
82     modpath="/lib/modules/$b"
83     if [ -d "$modpath" ]; then
84         ln -sf "$f" "$kernel"
85         break
86     fi
87     modpath=
88 done
89
90 if [ -z "$modpath" ]; then
91     echo "$0: failed to find a suitable kernel" >&2
92     exit 1
93 fi
94
95 # The initrd consists of these components:
96 # (1) The base skeleton appliance that we constructed at build time.
97 #     name = initramfs.$repo.$host_cpu.supermin.img
98 #     format = compressed cpio
99 # (2) The modules from modpath which are on the module whitelist.
100 #     format = plain cpio
101 # (3) The host files which match wildcards in *.supermin.hostfiles.
102 #     format = plain cpio
103
104 cp "$sourcedir"/initramfs.$repo.$host_cpu.supermin.img "$initrd"
105
106 # Kernel modules (2).
107 exec 5<"$sourcedir"/kmod.whitelist
108 whitelist=
109 while read kmod 0<&5; do
110     whitelist="$whitelist -o -name $kmod"
111 done
112 exec 5<&-
113
114 find "$modpath" \( -not -name '*.ko' $whitelist \) -a -print0 |
115   cpio --quiet -o -0 -H newc >> "$initrd"
116
117 # Host files (3).
118
119 (cd / && \
120   ls -1df $(
121       cat "$sourcedir"/initramfs.$repo.$host_cpu.supermin.hostfiles
122     ) 2>/dev/null |
123   cpio -C 65536 --quiet -o -H newc ) >> "$initrd"