summaryrefslogtreecommitdiff
path: root/mkinitramfs
blob: 8092a1443c0644e9a06165d252c3ee1ce56b7156 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/bin/sh

# Takes a file containing a list of modules to be added as an
# argument, figures out dependancies, and adds them.
#
# Input file syntax:
#
#   # comment
#   modprobe_module_name [args ...]
#   [...]
#
add_modules_from_file()
{
	# Sanity check
	if [ ! -e ${1} ]; then
		return
	fi

	sed -e '/^#/d' ${1} | while read module rest; do
		manual_add_modules ${module}
		echo ${module}.ko "${rest}" >>${DESTDIR}/conf/modules
	done
}

manual_add_modules()
{
	for mam_x in $(modprobe --set-version=${version} --show-depends ${1} | awk '{ print $2 }'); do
		# Prune duplicates
		if [ -e ${DESTDIR}/${mam_x} ]; then
			continue
		fi

		mkdir -p ${DESTDIR}/$(dirname ${mam_x})
		ln -s ${mam_x} ${DESTDIR}/$(dirname ${mam_x})
		depmod -b ${DESTDIR} ${version}
	done
}

# $1 is source
# $2 is relative destination
copy_exec() {
	ln -s ${1} ${DESTDIR}/${2}  

	# Copy the dependant libraries
	for x in $(ldd ${1} 2>/dev/null | sed -e '
	    /\//!d;
	    /linux-gate/d;
	    /=>/ {s/.*=>[[:blank:]]*\([^[:blank:]]*\).*/\1/};
	    s/[[:blank:]]*\([^[:blank:]]*\) (.*)/\1/' 2>/dev/null); do
		libname=$(basename ${x})
		dirname=$(dirname ${x})
		mkdir -p ${DESTDIR}/${dirname}
		if [ ! -e ${DESTDIR}/${dirname}/${libname} ]; then
			ln -s ${x} ${DESTDIR}/${dirname}
		fi
	done
}

# Copy entire subtrees to the initramfs
copy_modules_dir()
{
	tmpdir_modbase=${DESTDIR}/lib/modules/${version}
	mkdir -p $(dirname ${tmpdir_modbase}/${1})
	cp -a /lib/modules/${version}/${1} ${tmpdir_modbase}/${1}
}

# Modules that we always add to the initramfs
auto_add_modules()
{
	# base
	for x in md raid0 raid1 raid5 raid6 ehci-hcd ohci-hcd uhci-hcd usbhid usb-storage ext2 ext3 isofs nfs reiserfs xfs af_packet dm_mod; do
		manual_add_modules ${x}
	done

	# Ethernet
	for x in 3c59x 8139cp 8139too 8390 b44 bmac bnx2 defxx dl2k e1000 e100 epic100 eql fealnx famachi hp100 mace mv643xx_eth natsemi ne2k-pci netconsole ns83820 pcnet32 r8169 s2io sis900 skge slhc starfire sundance sungem sungem_phy sunhme tg3 tlan de2104x de4x5 dmfe tulip winbond-840 xircom_cb xircom_tulip_cb typhon via-rhine via-velocity yellowfin; do
		manual_add_modules ${x}
	done

	# ide
	for x in ide-cd ide-disk ide-generic aec62xx cmd64x generic hpt34x hpt366 ns87415 pdc202xx_new pdc202xx_old piix sc1200 siimage slc82c105 trm290 via82cxxx; do
		manual_add_modules ${x}
	done

	# scsi
	for x in 3w-9xxx 3w-xxxx a100u2x aacraid ahci aic79xx aic7xxx atp870u BusLogic ch dc395x dmx3191d dpt_i2o eata fdomain initio ipr ips lpfc mac53c94 megaraid megaraid_mbox megaraid_mm mesh nsp32 osst qla1280 qla2100 qla2200 qla2300 qla2322 qla2xxx qla6312 qlogicfas408 qlogicfc sata_promise sata_nv sata_qstor sata_sil sata_sis sata_svw sata_sx4 sata_uli sata_via sata_vsc scsi_mod scsi_transport_fc scsi_transport_iscsi scsi_transport_spi sd_mod sym53c8xx tmscsim; do
		manual_add_modules ${x}
	done

}

usage()
{
	cat >&2 << EOF

Usage: ${0} [OPTION]... <-o outfile> [version]

Options:
  -d confdir  Specify an alternative configuration directory.
  -k          Keep temporary directory used to make the image.
  -o outfile  Write to outfile.
  -r root     Override ROOT setting in mkinitrd.conf.

See ${0}(8) for further details.
EOF
	exit 1

}

# Defaults
keep="n"
CONFDIR="/etc/mkinitramfs"
verbose="n"
errors_to="2>/dev/null"

while getopts "d:ko:r:" flag; do
	case $flag in
	d)
		CONFDIR="${OPTAGS}"
		if [ ! d "${CONFDIR}" ]; then
			echo "${0}: ${CONFDIR}: Not a directory" >&2
			exit 1
		fi
		;;
	o)
		outfile="${OPTARG}"
		;;
	k)
		keep="y"
		;;
	esac
done

shift $((${OPTIND} - 1))

# For dependency ordered mkinitramfs hook scripts.
. /usr/share/initramfs-tools/scripts/functions

. ${CONFDIR}/initramfs.conf

if [ x${outfile} = x ]; then
	usage
fi

# And by "version" we really mean path to kernel modules
# This is braindead, and exists to preserve the interface with mkinitrd
if [ ${#} -ne 1 ]; then
	version=$(uname -r)
else 
	version="${1}"
fi

case ${version} in
/lib/modules/*/[!/]*)
        ;;
/lib/modules/[!/]*)
        version=${version#/lib/modules/}
        version=${version%%/*}
        ;;
esac

case ${version} in
*/*)
        echo $PROG: ${version} is not a valid kernel version >&2
        exit 1
        ;;
esac

if [ -d ${outfile} ]; then
	echo "${outfile} is a directory"
	exit 1
fi

if [ ! -e /lib/modules/${version} ]; then
	echo "Cannot find /lib/modules/${version}"
	exit 1
fi

DESTDIR=$(mktemp -t -d mkinitramfs_XXXXXX) || exit 1
__TMPCPIOGZ=$(mktemp -t mkinitramfs-OL_XXXXXX) || exit 1

# Export environment for hook scripts.
#
export version
export CONFDIR
export DESTDIR

# Private, used by 'catenate_cpiogz'.
export __TMPCPIOGZ

for d in bin conf etc lib modules sbin scripts; do
    mkdir -p ${DESTDIR}/${d}
done

for x in ${CONFDIR}/modules /usr/share/initramfs-tools/modules.d/*; do
	add_modules_from_file ${x}
done

auto_add_modules

# Have to do each file, because cpio --dereference doesn't recurse down
# symlinks.

ln -s /usr/lib/klibc/bin/* ${DESTDIR}/bin
ln -s /usr/lib/klibc/lib/* ${DESTDIR}/lib
copy_exec /usr/share/initramfs-tools/init /init
cp -a /usr/share/initramfs-tools/scripts/* ${DESTDIR}/scripts
copy_exec ${CONFDIR}/initramfs.conf /conf
cp -a /etc/udev ${DESTDIR}/etc

# Hack until udev is built with klibc
copy_exec /sbin/udev /sbin
copy_exec /sbin/udevstart /sbin

# Busybox
rm ${DESTDIR}/bin/sh
ln -s /usr/lib/initramfs-tools/bin/busybox ${DESTDIR}/bin/sh
# This is ugly, but needed atm to make the builtins work =(
ln -s /usr/lib/initramfs-tools/bin/busybox ${DESTDIR}/bin/busybox

# Modutils
copy_exec /sbin/modprobe /sbin
copy_exec /sbin/depmod /sbin
copy_exec /sbin/rmmod /sbin
mkdir -p ${DESTDIR}/etc/modprobe.d
copy_exec /etc/modprobe.d/aliases /etc/modprobe.d

# Raid
copy_exec /sbin/mdadm /sbin
copy_exec /sbin/mdrun /sbin

# LVM
copy_exec /lib/lvm-200/vgchange /sbin

run_scripts /usr/share/initramfs-tools/hooks
run_scripts /etc/mkinitramfs/hooks

# Apply DSDT to initramfs
if [ -e ${CONFDIR}/DSDT.aml ]; then
	copy_exec ${CONFDIR}/DSDT.aml /
fi

(cd ${DESTDIR} && find . | cpio --quiet --dereference -o -H newc | gzip -9 >${outfile})

if [ -s ${__TMPCPIOGZ} ]; then
    cat ${__TMPCPIOGZ} >>${outfile}
fi

if [ "${keep}" = "y" ]; then
	echo "Working files in ${DESTDIR} and overlay in ${__TMPCPIOGZ}"
else
	rm -rf "${DESTDIR}"
	rm -rf "${__TMPCPIOGZ}"
fi

exit 0