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