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