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