summaryrefslogtreecommitdiff
path: root/mkinitramfs
blob: 2134a2d27be7491e3eb5033593a144d34627e83f (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
#!/bin/sh

# Takes a file containing a list of modules to be added as an argument
# Figures out dependancies and adds it in.
manual_add_modules()
{
	# Sanity check
	if [ ! -e ${1} ]; then
		return
	fi

	for x in $(sed -e '/^#/d' ${1}); do
		for y in $(modprobe --set-version=${version} --show-depends ${x} | awk '{ print $2 }'); do
			# Prune duplicates
			if [ -e ${TMPDIR}/modules/$(basename ${y}) ]; then
				continue
			fi
	
			ln -s ${y} ${TMPDIR}/modules
			echo $(basename ${y}) >>${TMPDIR}/conf/modules
		done
	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"

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}"
		;;
	v)
		version="${OPTARG}"
		;;
	k)
		keep="y"
		;;
	esac
done

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

. ${CONFDIR}/initramfs.conf

if [ x${outfile} = x ] || [ ${#} -ne 1 ]; then
	usage
fi

version=${1}

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

TMPDIR=$(mktemp -d) || exit 1
mkdir -p ${TMPDIR}/modules ${TMPDIR}/conf ${TMPDIR}/etc
mkdir -p ${TMPDIR}/bin ${TMPDIR}/lib ${TMPDIR}/scripts

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

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

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

# Hack until udev is built with klibc
ln -s /sbin/udev ${TMPDIR}/bin
ln -s /sbin/udevstart ${TMPDIR}/bin
ln -s /lib/libc.so.* ${TMPDIR}/lib
ln -s /lib/ld*.so.* ${TMPDIR}/lib
rm ${TMPDIR}/lib/*lsb*

# Busybox
if [ "x${BUSYBOX}" = "xy" ]; then
	rm ${TMPDIR}/bin/sh
	ln -s /bin/busybox ${TMPDIR}/bin/sh
fi

# Raid
ln -s /sbin/mdadm ${TMPDIR}/bin
ln -s /sbin/mdrun ${TMPDIR}/bin
ln -s /bin/grep ${TMPDIR}/bin
ln -s /bin/sed ${TMPDIR}/bin

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

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