Dash:
[t2-trunk.git] / package / base / mkinitrd / mkinitrd.sh
blobe1ae6780af93f077163a839de2ed0d474326a3af
1 #!/bin/bash
2 # --- T2-COPYRIGHT-NOTE-BEGIN ---
3 # T2 SDE: package/*/mkinitrd/mkinitrd.sh
4 # Copyright (C) 2005 - 2021 The T2 SDE Project
5 # Copyright (C) 2005 - 2021 René Rebe <rene@exactcode.de>
6 #
7 # This Copyright note is generated by scripts/Create-CopyPatch,
8 # more information can be found in the files COPYING and README.
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License version 2.
12 # --- T2-COPYRIGHT-NOTE-END ---
14 set -e
16 map=`mktemp`
17 firmware=
18 minimal=
19 network=1
20 archprefix=
21 outfile=
23 declare -A vitalmods
24 vitalmods[qla1280.ko]=1 # Sgi Octane
25 vitalmods[qla2xxx.ko]=1 # Sun Blade
26 vitalmods[tg3.ko]=1 # Sun Fire
27 vitalmods[xhci-pci.ko]=1 # probably every modern machine
29 filter="-e ext4 -e isofs -e pata_legacy -e sym53c8xx -e s[rd]_mod"
31 declare -A added
33 if [ $UID != 0 ]; then
34 echo "Non root - exiting ..."
35 exit 1
38 while [ "$1" ]; do
39 case $1 in
40 [0-9]*) kernelver="$1" ;;
41 -R) root="$2" ; shift ;;
42 -a) archprefix="$2" ; shift ;;
43 --firmware) firmware=1 ;;
44 --minimal) minimal=1 ;;
45 --network) network=0 ;;
46 -e) filter="$filter $2" ; shift ;;
47 -o) outfile="$2" ; shift ;;
48 *) echo "Usage: mkinitrd [ --firmware ] [ --minimal ] [ --network ] [ -R root ] [ -e filter ] [ -o filename ] [ kernelver ]"
49 exit 1 ;;
50 esac
51 shift
52 done
54 [ "$minimal" != 1 ] && filter="$filter -e reiserfs -e btrfs -e /jfs -e /xfs
55 -e /udf -e /unionfs -e ntfs -e /fat -e /hfs -e floppy
56 -e /ata/ -e /scsi/ -e /fusion/ -e /sdhci/ -e nvme/host -e /mmc/ -e ps3fb -e ps3disk
57 -e dm-mod -e dm-raid -e md/raid -e dm/mirror -e dm/linear -e dm-crypt -e dm-cache
58 -e /aes -e /sha -e /blake -e /cbc
59 -e cciss -e ips -e virtio -e nls_cp437 -e nls_iso8859-1 -e nls_utf8
60 -e /.hci -e usb-common -e usb-storage -e sbp2 -e uas
61 -e usbhid -e i2c-hid -e hid-generic -e hid-multitouch
62 -e hid-apple -e hid-microsoft -e hyperv-keyboard"
64 [ "$network" = 1 ] && filter="$filter -e /ipv4/ -e '/ipv6\.' -e ethernet -e nfsv4"
66 [ "$kernelver" ] || kernelver=`uname -r`
67 [ "$moddir" ] || moddir="$root/lib/modules/$kernelver"
69 modinfo="${archprefix}modinfo -b $moddir -k $kernelver"
70 depmod=${archprefix}depmod
72 echo "Kernel: $kernelver, module dir: $moddir"
74 if [ ! -d $moddir ]; then
75 echo "Warning: $moddir does not exist!"
76 moddir=""
79 sysmap=""
80 [ -f "$root/boot/System.map-$kernelver" ] && sysmap="$root/boot/System.map-$kernelver"
82 if [ -z "$sysmap" ]; then
83 echo "System.map-$kernelver not found!"
84 exit 2
87 echo "System.map: $sysmap"
89 # check needed tools
90 for x in cpio gzip; do
91 if ! type -p $x >/dev/null; then
92 echo "$x not found!"
93 exit 2
95 done
97 tmpdir="$map.d"
98 cd ${tmpdir%/*}
99 mkdir ${tmpdir##*/}
100 cd $tmpdir
102 # create basic structure
104 echo "Create dirtree ..."
106 mkdir -p {dev,bin,sbin,proc,sys,lib/modules,lib/udev,etc/hotplug.d/default}
107 mknod dev/null c 1 3
108 mknod dev/zero c 1 5
109 mknod dev/tty c 5 0
110 mknod dev/console c 5 1
112 # copy the basic / rootfs kernel modules
115 if [ "$moddir" ]; then
116 echo "Copying kernel modules ..."
118 add_depend() {
119 local skipped=
120 local x="$1"
122 # expand to full name if it was a depend
123 [ $x = ${x##*/} ] && x=`sed -n "/\/$x\.ko.*/{p; q}" $map`
125 if [ "${added["$x"]}" != 1 ]; then
126 added["$x"]=1
128 local module=${x##*/}
129 echo -n "$module "
131 # strip $root prefix
132 xt=${x##$root}
134 # does it need firmware?
135 fw="`$modinfo -F firmware $x`"
136 if [ "$fw" ]; then
137 echo -e -n "\nWarning: $module needs firmware"
138 if [ "$firmware" -o "${vitalmods[$module]}" ]; then
139 for fn in $fw; do
140 local fn="/lib/firmware/$fn"
141 local dir="./${fn%/*}"
142 if [ ! -e "$root$fn" ]; then
143 if [ "${vitalmods[$module]}" ]; then
144 echo ", not found, vital, including anyway"
145 else
146 echo ", not found, skipped"
147 skipped=1
149 else
150 mkdir -p "$dir"
151 echo -n ", $fn"
152 cp -af "$root$fn" "$dir/"
153 # TODO: copy source if symlink
154 [ -f "$tmpdir$fn" ] && zstd -19 --rm -f --quiet "$tmpdir$fn"
156 done
157 echo
158 else
159 echo ", skipped"
160 skipped=1
164 if [ -z "$skipped" ]; then
165 mkdir -p `dirname ./$xt` # TODO: use builtin?
166 cp -af $x $tmpdir$xt
167 zstd -19 --rm -f --quiet $tmpdir$xt
169 # add it's deps, too
170 for fn in `$modinfo -F depends $x | sed 's/,/ /g'`; do
171 add_depend "$fn"
172 done
174 else
175 #echo "already there"
180 find $moddir/kernel -type f > $map
181 grep -v -e /wireless/ -e netfilter $map | grep $filter |
182 while read fn; do
183 add_depend "$fn"
184 done
185 ) | fold -s; echo
187 # generate map files
189 mkdir -p lib/modules/$kernelver
190 cp -avf $moddir/modules.{order*,builtin*} lib/modules/$kernelver/
191 $depmod -ae -b $tmpdir -F $sysmap $kernelver
192 # only keep the .bin-ary files
193 rm $tmpdir/lib/modules/$kernelver/modules.{alias,dep,symbols,builtin,order}
196 echo "Injecting programs and configuration ..."
198 # copying config
200 cp -ar $root/etc/{group,udev} $tmpdir/etc/
202 [ -e $root/lib/udev/rules.d ] && cp -ar $root/lib/udev/rules.d $tmpdir/lib/udev/
203 [ -e $root/etc/mdadm.conf ] && cp -ar $root/etc/mdadm.conf $tmpdir/etc/
204 cp -ar $root/etc/modprobe.* $root/etc/ld-* $tmpdir/etc/ 2>/dev/null || true
206 # in theory all, but fat and not all always needed ...
207 cp -a $root/lib/udev/{ata,scsi,cdrom}_id $tmpdir/lib/udev/
209 elf_magic () {
210 readelf -h "$1" | grep 'Machine\|Class'
213 # copy dynamic libraries, and optional plugins, if any.
215 if [ "$minimal" = 1 ]; then
216 extralibs="`ls $root/lib*/{libdl,libncurses.so}* 2>/dev/null || true`"
217 else
218 # glibc only
219 extralibs="`ls $root/{lib*/libnss_files,usr/lib*/libgcc_s}.so* 2>/dev/null || true`"
222 copy_dyn_libs () {
223 local magic
224 # we can not use ldd(1) as it loads the object, which does not work on cross builds
225 for lib in $extralibs `readelf -de $1 |
226 sed -n -e 's/.*Shared library.*\[\([^]\]*\)\]/\1/p' \
227 -e 's/.*Requesting program interpreter: \([^]]*\)\]/\1/p'`
229 # remove $root prefix from extra libs
230 [ "$lib" != "${lib#$root/}" ] && lib="${lib##*/}"
232 if [ -z "$magic" ]; then
233 magic="$(elf_magic $1)"
234 [[ $1 = *bin/* ]] && echo "Warning: $1 is dynamically linked!"
236 for libdir in $root/lib*/ $root/usr/lib*/ "$root"; do
237 if [ -e $libdir$lib ]; then
238 [ ! -L $libdir$lib -a "$magic" != "$(elf_magic $libdir$lib)" ] && continue
239 xlibdir=${libdir#$root}
240 echo " ${1#$root} NEEDS $xlibdir$lib"
242 if [ "${added["$xlibdir$lib"]}" != 1 ]; then
243 added["$xlibdir$lib"]=1
245 mkdir -p ./$xlibdir
246 while local x=`readlink $libdir$lib`; [ "$x" ]; do
247 echo " $xlibdir$lib SYMLINKS to $x"
248 local y=$tmpdir$xlibdir$lib
249 mkdir -p ${y%/*}
250 ln -sfv $x $tmpdir$xlibdir$lib
251 if [ "${x#/}" == "$x" ]; then # relative?
252 # directory to prepend?
253 [ ${lib%/*} != "$lib" ] && x="${lib%/*}/$x"
255 lib="$x"
256 done
257 local y=$tmpdir$xlibdir$lib
258 mkdir -p ${y%/*}
259 cp -af $libdir$lib $tmpdir$xlibdir$lib
261 copy_dyn_libs $libdir$lib
264 done
265 done
268 # setup programs
270 for x in $root/sbin/{udevd,udevadm,kmod,modprobe,insmod,blkid} \
271 $root/usr/sbin/disktype
273 cp -av $x $tmpdir/sbin/
274 copy_dyn_libs $x
275 done
277 # setup optional programs
279 [ "$minimal" != 1 ] &&
280 for x in $root/sbin/{vgchange,lvchange,lvm,mdadm} \
281 $root/usr/sbin/{cryptsetup,ipconfig} $root/usr/embutils/{dmesg,swapon}
283 if [ ! -e $x ]; then
284 echo "Warning: Skipped optional file ${x#$root}!"
285 else
286 cp -a $x $tmpdir/sbin/
287 copy_dyn_libs $x
289 done
291 # copy a small shell
292 for sh in $root/bin/{pdksh,bash}; do
293 if [ -e "$sh" ]; then
294 cp $sh $tmpdir/bin/${sh##*/}
295 ln -sf ${sh##*/} $tmpdir/bin/sh
296 break
298 done
300 # static, tiny embutils and friends
302 cp $root/usr/embutils/{mount,umount,rm,mv,mkdir,ln,ls,switch_root,chroot,sleep,losetup,chmod,cat,sed,mknod} \
303 $tmpdir/bin/
304 ln -s mv $tmpdir/bin/cp
306 cp $root/sbin/initrdinit $tmpdir/init
307 chmod +x $tmpdir/init
309 # Custom ACPI DSDT table
310 if test -f "$root/boot/DSDT.aml"; then
311 echo "Adding local DSDT file: $dsdt"
312 cp $root/boot/DSDT.aml $tmpdir/DSDT.aml
315 # create the cpio image
317 echo "Archiving ..."
318 ( cd $tmpdir
319 find . | cpio -o -H newc
320 ) | zstd -19 -T0 > "${outfile:-$root/boot/initrd-$kernelver}"
321 rm -rf $tmpdir $map