check for Debian tools
[libguestfs.git] / appliance / make.sh.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 # This is called from the Makefile to build the initramfs.
20
21 unset CDPATH
22
23 set -e
24
25 cd @top_builddir@
26
27 modules="
28 -i augeas-libs
29 -i bash
30 -i binutils
31 -i coreutils
32 -i dosfstools
33 -i file
34 -i grub
35 -i iputils
36 -i kernel
37 -i lvm2
38 -i MAKEDEV
39 -i module-init-tools
40 -i net-tools
41 -i ntfs-3g
42 -i ntfsprogs
43 -i procps
44 -i strace
45 -i util-linux-ng
46 -i zerofree
47 "
48
49 # Decide on names for the final output.  These have to match Makefile.am.
50 output=appliance/initramfs.@REPO@.@host_cpu@.img
51 koutput=appliance/vmlinuz.@REPO@.@host_cpu@
52 rm -f $output
53 rm -f $koutput
54
55 # Create the basic initramfs.
56 @FEBOOTSTRAP@ $modules -u @UPDATES@ @REPO@ initramfs @MIRROR@
57
58 # /sysroot is where the guest root filesystem will be mounted.
59 @FEBOOTSTRAP_RUN@ initramfs -- mkdir -p --mode=0777 /sysroot
60
61 # Create /tmp if it is missing.
62 @FEBOOTSTRAP_RUN@ initramfs -- mkdir -p --mode=0777 /tmp
63
64 # Nuke some stuff.  The kernel pulls mkinitrd and plymouth which pulls in
65 # all of Python.  Sheez.
66 (cd initramfs && find -name '*plymouth*' -print0) |
67   xargs -0 @FEBOOTSTRAP_RUN@ initramfs -- rm -rf
68 (cd initramfs && find -name '*python*' -print0) |
69   xargs -0 @FEBOOTSTRAP_RUN@ initramfs -- rm -rf
70
71 # In Fedora >= 11, it pulls in all of Perl from somewhere.  Nuke from orbit.
72 @FEBOOTSTRAP_RUN@ initramfs -- rm -rf /usr/lib/perl5 /usr/lib64/perl5
73
74 # Anaconda?  JPEG images?
75 @FEBOOTSTRAP_RUN@ initramfs -- rm -rf /usr/lib/anaconda-runtime
76
77 # Don't need any firmware.
78 @FEBOOTSTRAP_RUN@ initramfs -- rm -rf /lib/firmware
79
80 # Don't need any keyboard maps.
81 @FEBOOTSTRAP_RUN@ initramfs -- rm -rf /lib/kbd
82
83 # Remove anything in home directory.  Because this is potentially
84 # liable to monstrous fuck-ups, we don't put a slash before 'home'.
85 (cd initramfs && echo home/*) |
86   xargs @FEBOOTSTRAP_RUN@ initramfs -- rm -rf
87
88 # Remove /var/lib/yum stuff.
89 @FEBOOTSTRAP_RUN@ initramfs -- rm -rf /var/lib/yum
90
91 # Remove some unreadable binaries which are incompatible with
92 # the supermin appliance.  Since these binaries can't be read
93 # from the host filesystem, they generate warnings like:
94 #   cpio: ./usr/bin/chfn: Cannot open: Permission denied
95 # These binaries are not needed for operation of the appliance.
96 @FEBOOTSTRAP_RUN@ initramfs -- rm -f \
97   /usr/bin/chfn \
98   /usr/bin/chsh \
99   /usr/libexec/pt_chown \
100   /usr/sbin/groupdel \
101   /usr/sbin/groupadd \
102   /usr/sbin/useradd \
103   /usr/sbin/tzdata-update \
104   /usr/sbin/userdel \
105   /usr/sbin/usermod \
106   /usr/sbin/groupmod \
107   /usr/sbin/groupmems \
108   /sbin/unix_update \
109   $(cd initramfs && echo usr/sbin/glibc_post_upgrade.*)
110
111 # Kernel modules take up nearly half of the image.  Only include ones
112 # which are on the whitelist.
113 exec 5<appliance/kmod.whitelist
114 whitelist=
115 while read kmod 0<&5; do
116     whitelist="$whitelist -a -not -name $kmod"
117 done
118 exec 5<&-
119
120 (cd initramfs && \
121   find lib/modules/*/kernel -name '*.ko' $whitelist -a -print0 ) |
122   xargs -0 febootstrap-run initramfs -- rm
123
124 # Pull the kernel out into the current directory.  We don't want it in
125 # the initramfs image.
126 cp initramfs/boot/vmlinuz* $koutput
127 @FEBOOTSTRAP_RUN@ initramfs -- rm -rf boot
128
129 # Minimize the image.
130 @FEBOOTSTRAP_MINIMIZE@ initramfs
131
132 # Add some missing configuration files.
133 if [ ! -f initramfs/etc/hosts ]; then
134     cat > hosts.new <<'__EOF__'
135 127.0.0.1 guestfs localhost.localdomain localhost
136 ::1       localhost6.localdomain6 localhost6
137 __EOF__
138     @FEBOOTSTRAP_INSTALL@ initramfs hosts.new /etc/hosts 0644 root.root
139     rm hosts.new
140 fi
141
142 if [ ! -f initramfs/etc/fstab ]; then
143     @FEBOOTSTRAP_RUN@ initramfs -- touch /etc/fstab
144 fi
145
146 echo nameserver 10.0.2.3 > resolv.conf.new
147 @FEBOOTSTRAP_INSTALL@ initramfs resolv.conf.new /etc/resolv.conf 0644 root.root
148 rm resolv.conf.new
149
150 # Create the init script.
151 cat > init.new <<'__EOF__'
152 #!/bin/sh
153 echo Starting /init script ...
154 PATH=/sbin:/usr/sbin:$PATH
155 mount -t tmpfs none /dev
156 mkdir /dev/pts /dev/shm /dev/mapper
157 MAKEDEV mem null port zero core full ram tty console fd \
158   hda hdb hdc hdd sda sdb sdc sdd loop sd
159 mknod /dev/ptmx c 5 2; chmod 0666 /dev/ptmx
160 mknod /dev/random c 1 8;  chmod 0666 /dev/random
161 mknod /dev/urandom c 1 9; chmod 0444 /dev/urandom
162 mount -t proc /proc /proc
163 mount -t sysfs /sys /sys
164 mount -t devpts -o gid=5,mode=620 /dev/pts /dev/pts
165 ln -sf /proc/self/fd/0 /dev/stdin
166 ln -sf /proc/self/fd/1 /dev/stdout
167 ln -sf /proc/self/fd/2 /dev/stderr
168 modprobe virtio_pci
169 modprobe virtio_net
170 modprobe dm_mod ||:
171 /sbin/ifconfig lo 127.0.0.1
172 /sbin/ifconfig eth0 10.0.2.10
173 /sbin/route add default gw 10.0.2.2
174 lvm vgscan --ignorelockingfailure
175 lvm vgchange -ay --ignorelockingfailure
176 if grep -sq guestfs_rescue=1 /proc/cmdline; then
177   bash -i
178 fi
179 exec guestfsd -f
180 __EOF__
181
182 @FEBOOTSTRAP_INSTALL@ initramfs init.new /init 0755 root.root
183 rm init.new
184
185 # Just in case the kernel isn't looking for /init, make /sbin/init
186 # be our script, not the real init.
187 #@FEBOOTSTRAP_RUN@ initramfs -- ln -f /init /sbin/init
188
189 ls -lh $koutput
190
191 # Now directly run the update script to copy/update the daemon in the
192 # initramfs.
193 cd appliance && bash update.sh