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>
7 # This Copyright note is generated by scripts/Create-CopyPatch,
8 # more information can be found in the files COPYING and README.
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 ---
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"
33 if [ $UID != 0 ]; then
34 echo "Non root - exiting ..."
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 ]"
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!"
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!"
87 echo "System.map: $sysmap"
90 for x
in cpio gzip; do
91 if ! type -p $x >/dev
/null
; then
102 # create basic structure
104 echo "Create dirtree ..."
106 mkdir
-p {dev
,bin
,sbin
,proc
,sys
,lib
/modules
,lib
/udev
,etc
/hotplug.d
/default
}
110 mknod dev
/console c
5 1
112 # copy the basic / rootfs kernel modules
115 if [ "$moddir" ]; then
116 echo "Copying kernel modules ..."
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
128 local module
=${x##*/}
134 # does it need firmware?
135 fw
="`$modinfo -F firmware $x`"
137 echo -e -n "\nWarning: $module needs firmware"
138 if [ "$firmware" -o "${vitalmods[$module]}" ]; then
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"
146 echo ", not found, skipped"
152 cp -af "$root$fn" "$dir/"
153 # TODO: copy source if symlink
154 [ -f "$tmpdir$fn" ] && zstd
-19 --rm -f --quiet "$tmpdir$fn"
164 if [ -z "$skipped" ]; then
165 mkdir
-p `dirname ./$xt` # TODO: use builtin?
167 zstd
-19 --rm -f --quiet $tmpdir$xt
170 for fn
in `$modinfo -F depends $x | sed 's/,/ /g'`; do
175 #echo "already there"
180 find $moddir/kernel
-type f
> $map
181 grep -v -e /wireless
/ -e netfilter
$map |
grep $filter |
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 ..."
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
/
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`"
219 extralibs
="`ls $root/{lib*/libnss_files,usr/lib*/libgcc_s}.so* 2>/dev/null || true`"
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
246 while local x
=`readlink $libdir$lib`; [ "$x" ]; do
247 echo " $xlibdir$lib SYMLINKS to $x"
248 local y
=$tmpdir$xlibdir$lib
250 ln -sfv $x $tmpdir$xlibdir$lib
251 if [ "${x#/}" == "$x" ]; then # relative?
252 # directory to prepend?
253 [ ${lib%/*} != "$lib" ] && x
="${lib%/*}/$x"
257 local y
=$tmpdir$xlibdir$lib
259 cp -af $libdir$lib $tmpdir$xlibdir$lib
261 copy_dyn_libs
$libdir$lib
270 for x
in $root/sbin
/{udevd
,udevadm
,kmod
,modprobe
,insmod
,blkid
} \
271 $root/usr
/sbin
/disktype
273 cp -av $x $tmpdir/sbin
/
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
}
284 echo "Warning: Skipped optional file ${x#$root}!"
286 cp -a $x $tmpdir/sbin
/
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
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
} \
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
319 find . |
cpio -o -H newc
320 ) | zstd
-19 -T0 > "${outfile:-$root/boot/initrd-$kernelver}"