summaryrefslogtreecommitdiffstats
path: root/mkinitfs.in
blob: 8246d0781d68094cfa503bc47e000d291734cf5c (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
#!/bin/sh

VERSION=@VERSION@
sysconfdir=@sysconfdir@
datadir=@datadir@

config="$sysconfdir"/mkinitfs.conf
init="$datadir"/initramfs-init
fstab="$datadir"/fstab
passwd="$datadir"/passwd
group="$datadir"/group

startdir=$PWD

_cp() {
	local i
	if [ -n "$list_sources" ]; then
		for i in "$@"; do
			echo "$basedir"/$i
		done
		return
	fi

	for i in "$@"; do
		local dest="$tmpdir"/${i%/*}
		mkdir -p "$dest"
		cp -flLpR "$basedir"/"$i" "$dest" 2>/dev/null || cp -pR "$basedir"/"$i" "$dest"
	done
}

feature_files() {
	local dir="$1"
	local glob file 
	for f in $features; do
		if [ ! -f "$dir/$f" ]; then
			continue
		fi
		for glob in $(sed 's:^/\+::' "$dir/$f"); do
			for file in $glob; do
				if [ -d $file ]; then
					find $file -type f
				else
					echo $file
				fi
			done
		done
	done
}

initfs_base() {
	local i= dirs= glob= file=
	for i in dev proc sys sbin bin .modloop lib/modules media/cdrom \
	    etc/apk media/floppy media/usb newroot; do
		dirs="$dirs $tmpdir/$i"
	done
	[ -z "$list_sources" ] && mkdir -p $dirs

	cd "$basedir"
	for file in $(feature_files "$filelists_dir"); do
		_cp $file
	done

	# copy init
	if [ -z "$list_sources" ]; then
		cd $startdir
		install -m755 "$init" "$tmpdir"/init || return 1
		for i in "$fstab" "$passwd" "$group"; do
			install -Dm644 "$i" "$tmpdir"/etc/${i##*/} || return 1
		done
	else
		echo "$init"
		echo "$fstab"
		echo "$passwd"
		echo "$group"
	fi
}

find_kmod_deps() {
	awk -v prepend="/lib/modules/$kernel/" -v modulesdep="$basedir/lib/modules/$kernel/modules.dep" '
function recursedeps(k,		j, dep) {
	if (k in visited)
		return;
	visited[k] = 1;
	split(deps[k], dep, " ");
	for (j in dep)
		recursedeps(dep[j]);
	print(prepend k);
}

BEGIN {
	if (modulesdep == "")
		modulesdep="modules.dep";
	FS = ": ";
	while ( (getline < modulesdep) > 0) {
		if (substr($0,1,1) == "/") {
			gsub(prepend, "", $1);
			gsub(prepend, "", $2);
		}
		deps[$1] = $2;
	}
}

{
	mod[$0] = 1;
}

END {
	for (i in mod)
		recursedeps(i);
}' 
}

find_kmods() {
	cd "$kerneldir" || return 1
	for file in $(feature_files $modulelists_dir); do
		if ! [ -e ${file} ]; then
			echo "$file: No such file or directory" >&2
			continue
		fi
		echo $file
	done | find_kmod_deps
}

initfs_kmods() {
	local glob= file= files= dirs=
	[ -z "$list_sources" ] && rm -rf "$tmpdir"/lib/modules
	files=$(find_kmods)
	for file in $files; do
		_cp $file
	done
	[ -z "$list_sources" ] && depmod $kernel -b "$tmpdir"
}

initfs_apk_keys() {
	[ -n "$list_sources" ] && return
	mkdir -p "$tmpdir"/etc/apk/keys
	cp "$basedir"/etc/apk/keys/* "$tmpdir"/etc/apk/keys/
}

initfs_cpio() {
	[ -n "$list_sources" ] && return
	(cd "$tmpdir" && find . | cpio -o -H newc | gzip) > $outfile
}

usage() {
	cat <<EOF
usage: mkinitfs [-hkLl] [-b basedir] [-c configfile] [-F features] [-f fstab]
		[-i initfile ] [-o outfile] [-t tempdir] [kernelversion]"
options:
	-b  prefix files and kernel modules with basedir
	-c  use configfile instead of $config
	-f  use fstab instead of $fstab
	-F  use specified features
	-h  print this help
	-i  use initfile as init instead of $init
	-k  keep tempdir
	-l  only list files that would have been used
	-L  list available features
	-o  set another outfile
	-q  Quiet mode
	-t  use tempdir when creating initramfs image

EOF
	exit 1
}

# main


while getopts "b:c:f:F:hi:kLlo:qt:" opt; do
	case "$opt" in
		b) basedir="$OPTARG";;
		c) config="$OPTARG";;
		F) myfeatures="$OPTARG";;
		f) fstab="$OPTARG";;
		h) usage;;
		i) init=$OPTARG;;
		k) keeptmp=1;;
		L) list_features=1;;
		l) list_sources=1;;
		o) outfile="$OPTARG";;
		q) quiet=1;;
		t) tmpdir="$OPTARG";;
		*) usage;;
	esac
done
shift $(( $OPTIND - 1 ))

. $(readlink -f "$config")
filelists_dir=${filelists_dir:-"$sysconfdir/files.d"}
modulelists_dir=${modules_dir:-"$sysconfdir/modules.d"}
[ -n "$myfeatures" ] && features="$myfeatures"

if [ -n "$list_features" ]; then
	for i in $filelists_dir $modulelists_dir; do
		( [ -d "$i" ] && cd $i && ls )
	done | sort | uniq
	exit 0
fi

[ -n "$1" ] && kernel="$1"
[ -z "$kernel" ] && kernel=$(uname -r)
kerneldir="$basedir/lib/modules/$kernel"

imagename=${kernel##*-}
[ "$imagename" = "$kernel" ] && imagename=initrd

if [ -z "$outfile" ]; then
	outfile="$basedir"/boot/${imagename}.gz
fi

if [ ! -d "$kerneldir" ]; then
	echo "$kerneldir does not exist or is not a directory"
	exit 1
fi

if [ -n "$DEBUG_KMOD" ]; then
	find_kmods
	exit 0
fi

if [ -z "$tmpdir" ]; then
	tmpdir=$(mktemp -d /tmp/mkinitfs.XXXXXX)
else
	mkdir -p "$tmpdir"
fi

if [ -z "$keeptmp" ]; then
	[ -d "$tmpdir" ] && rm -rf "$tmpdir"/*
fi

if [ -z "$list_sources" ] && [ -z "$quiet" ]; then
	echo "==> initramfs: creating $outfile"
fi
initfs_base || exit 1
initfs_kmods || exit 1
initfs_apk_keys || exit 1
initfs_cpio || exit 1


# cleanup
if [ -z "$keeptmp" ]; then
	[ -d "$tmpdir" ] && rm -rf "$tmpdir"
fi