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