248f2e2dc47a29c6e6e6b3626a5e432b0b513f10
[libguestfs.git] / appliance / make-initramfs.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 # Modules take up nearly half of the image.  It's a rough guess that
78 # we don't need many drivers (which take up most of the space).
79 (cd initramfs && find lib/modules/*/kernel \
80   -name '*.ko' \
81   -a ! -name 'virtio.ko' \
82   -a ! -name 'virtio_net.ko' \
83   -a ! -name 'virtio_pci.ko' \
84   -a ! -name 'virtio_ring.ko' \
85   -a ! -name 'ext2.ko' \
86   -a ! -name 'ext3.ko' \
87   -a ! -name 'ext4*.ko' \
88   -a ! -name 'crc16.ko' \
89   -a ! -name 'jbd.ko' \
90   -a ! -name 'jbd2.ko' \
91   -a ! -name 'fuse.ko' \
92   -a ! -name 'vfat.ko' \
93   -a ! -name 'fat.ko' \
94   -a ! -name 'udf.ko' \
95   -a ! -name 'crc_itu_t.ko' \
96   -a ! -name 'nls_utf8.ko' \
97   -a ! -name 'dm-*.ko' \
98   -a -print0 ) |
99   xargs -0 @FEBOOTSTRAP_RUN@ initramfs -- rm
100
101 # Pull the kernel out into the current directory.  We don't want it in
102 # the initramfs image.
103 cp initramfs/boot/vmlinuz* $koutput
104 @FEBOOTSTRAP_RUN@ initramfs -- rm -rf boot
105
106 # Minimize the image.
107 @FEBOOTSTRAP_MINIMIZE@ initramfs
108
109 # Add some missing configuration files.
110 if [ ! -f initramfs/etc/hosts ]; then
111     cat > hosts.new <<'__EOF__'
112 127.0.0.1 guestfs localhost.localdomain localhost
113 ::1       localhost6.localdomain6 localhost6
114 __EOF__
115     @FEBOOTSTRAP_INSTALL@ initramfs hosts.new /etc/hosts 0644 root.root
116     rm hosts.new
117 fi
118
119 if [ ! -f initramfs/etc/fstab ]; then
120     @FEBOOTSTRAP_RUN@ initramfs -- touch /etc/fstab
121 fi
122
123 echo nameserver 10.0.2.3 > resolv.conf.new
124 @FEBOOTSTRAP_INSTALL@ initramfs resolv.conf.new /etc/resolv.conf 0644 root.root
125 rm resolv.conf.new
126
127 # Create the init script.
128 cat > init.new <<'__EOF__'
129 #!/bin/sh
130 echo Starting /init script ...
131 PATH=/sbin:/usr/sbin:$PATH
132 MAKEDEV mem null port zero core full ram tty console fd \
133   hda hdb hdc hdd sda sdb sdc sdd loop sd
134 mount -t proc /proc /proc
135 mount -t sysfs /sys /sys
136 mount -t devpts -o gid=5,mode=620 /dev/pts /dev/pts
137 modprobe virtio_pci
138 modprobe virtio_net
139 modprobe dm_mod ||:
140 /sbin/ifconfig lo 127.0.0.1
141 /sbin/ifconfig eth0 10.0.2.10
142 /sbin/route add default gw 10.0.2.2
143 lvm vgscan --ignorelockingfailure
144 lvm vgchange -ay --ignorelockingfailure
145 exec guestfsd -f
146 __EOF__
147
148 @FEBOOTSTRAP_INSTALL@ initramfs init.new /init 0755 root.root
149 rm init.new
150
151 # Just in case the kernel isn't looking for /init, make /sbin/init
152 # be our script, not the real init.
153 #@FEBOOTSTRAP_RUN@ initramfs -- ln -f /init /sbin/init
154
155 # Copy the daemon into the filesystem.
156 @FEBOOTSTRAP_INSTALL@ initramfs daemon/guestfsd /sbin/guestfsd 0755 root.root
157
158 # Generate final image.
159 @FEBOOTSTRAP_TO_INITRAMFS@ initramfs > $output-t
160 mv $output-t $output
161 ls -lh $output
162 ls -lh $koutput