Command line and interactive shell parsing, prompts etc.
[libguestfs.git] / 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 set -e
22
23 # If you want to do some extra debugging and diagnosis of the
24 # initramfs image, then uncomment this line.  This makes the image
25 # larger.
26 debug=yes
27
28 modules="-i kernel -i bash -i coreutils -i lvm2 -i ntfs-3g -i util-linux-ng -i MAKEDEV -i net-tools"
29
30 if [ "x$debug" = "xyes" ]; then
31     modules="$modules -i module-init-tools -i procps -i strace -i iputils"
32 fi
33
34 # Decide on names for the final output.  These have to match Makefile.am.
35 output=initramfs.@REPO@.@host_cpu@.img
36 koutput=vmlinuz.@REPO@.@host_cpu@
37 rm -f $output
38 rm -f $koutput
39
40 # Create the basic initramfs.
41 @FEBOOTSTRAP@ $modules @REPO@ initramfs @MIRROR@
42
43 # /sysroot is where the guest root filesystem will be mounted.
44 mkdir initramfs/sysroot
45
46 # Make a safe 'install_file' function to install files into the
47 # filesystem.  XXX We need a 'febootstrap-install' utility.
48 # Usage: install_file <local-file> <target-pathname> mode owner[.group]
49 install_file ()
50 {
51     cp "$1" initramfs/"$2"
52     # Get the inode of the new file and remove it from fakeroot.log.
53     ino=$(ls -i initramfs/"$2" | awk '{print $1}')
54     cp initramfs/fakeroot.log fakeroot.log.old
55     grep -v "ino=$ino," fakeroot.log.old > initramfs/fakeroot.log
56     rm fakeroot.log.old
57     # Set mode, owner and group as desired.
58     @FEBOOTSTRAP_RUN@ initramfs -- chmod "$3" "$2"
59     @FEBOOTSTRAP_RUN@ initramfs -- chown "$4" "$2"
60 }
61
62 # Nuke some stuff.  The kernel pulls mkinitrd and plymouth which pulls in
63 # all of Python.  Sheez.
64 find initramfs -name '*plymouth*' -print0 | xargs -0 rm -rf
65 find initramfs -name '*python*' -print0 | xargs -0 rm -rf
66
67 # Modules take up nearly half of the image.  It's a rough guess that
68 # we don't need any drivers (which take up most of the space).
69 #rm -rf initramfs/lib/modules/*/kernel/{drivers,sound}
70 rm -rf initramfs/lib/modules/*/kernel/arch/x86/kvm
71
72 # Pull the kernel out into the current directory.  We don't want it in
73 # the initramfs image.
74 mv initramfs/boot/vmlinuz* $koutput
75 rm -rf initramfs/boot
76
77 # Minimize the image.
78 @FEBOOTSTRAP_MINIMIZE@ initramfs
79
80 # Add some missing configuration files.
81 if [ ! -f initramfs/etc/hosts ]; then
82     cat > hosts.new <<'__EOF__'
83 127.0.0.1 guestfs localhost.localdomain localhost
84 ::1       localhost6.localdomain6 localhost6
85 __EOF__
86     install_file hosts.new /etc/hosts 0644 root.root
87     rm hosts.new
88 fi
89
90 if [ ! -f initramfs/etc/fstab ]; then
91     @FEBOOTSTRAP_RUN@ initramfs -- touch /etc/fstab
92 fi
93
94 echo nameserver 10.0.2.3 > resolv.conf.new
95 install_file resolv.conf.new /etc/resolv.conf 0644 root.root
96 rm resolv.conf.new
97
98 # Create the init script.
99 cat > init.new <<'__EOF__'
100 #!/bin/sh
101 PATH=/sbin:/usr/sbin:$PATH
102 MAKEDEV mem null port zero core full ram tty console fd \
103   hda hdb hdc hdd sda sdb sdc sdd loop sd
104 mount -t proc /proc /proc
105 mount -t sysfs /sys /sys
106 mount -t devpts -o gid=5,mode=620 /dev/pts /dev/pts
107 modprobe 8139cp
108 /sbin/ifconfig lo 127.0.0.1
109 /sbin/ifconfig eth0 10.0.2.10
110 /sbin/route add default gw 10.0.2.2
111 lvm vgscan --ignorelockingfailure
112 lvm vgchange -ay --ignorelockingfailure
113 __EOF__
114
115 if [ "x$debug" != "xyes" ]; then
116     echo exec guestfsd -f >> init.new
117 else
118     echo guestfsd >> init.new
119     echo exec bash -i >> init.new
120 fi
121
122 install_file init.new /init 0755 root.root
123 rm init.new
124
125 # Copy the daemon into the filesystem.
126 install_file @abs_builddir@/daemon/guestfsd /sbin/guestfsd 0755 root.root
127
128 # Generate final image.
129 @FEBOOTSTRAP_TO_INITRAMFS@ initramfs > $output-t
130 mv $output-t $output
131 ls -lh $output
132 ls -lh $koutput