2020-01-06 00:20:12 +05:30
|
|
|
#!/bin/sh
|
2020-01-05 23:31:39 +05:30
|
|
|
#
|
|
|
|
# tiny initramfs generation tool
|
|
|
|
|
2020-01-06 00:20:12 +05:30
|
|
|
# debugging
|
|
|
|
set -x
|
|
|
|
|
2020-01-09 21:16:50 +05:30
|
|
|
# check root and files
|
2020-01-05 23:31:39 +05:30
|
|
|
if [ "$(id -u)" != 0 ]; then
|
2020-01-09 21:16:50 +05:30
|
|
|
echo "must be run as root"
|
|
|
|
exit 1
|
|
|
|
elif [ ! -e ./config ]; then
|
|
|
|
echo "config doesn't exists"
|
|
|
|
exit 1
|
|
|
|
elif [ ! -e ./init ]; then
|
|
|
|
echo "init doesn't exists"
|
|
|
|
exit 1
|
2020-01-13 07:52:45 +05:30
|
|
|
# TODO remove static busybox dependency | handle busybox requirements ( busybox --list | grep ash )
|
2020-01-09 21:16:50 +05:30
|
|
|
elif [ ! -n "$(ldd ./busybox | grep "not a dynamic executable")" ]; then
|
|
|
|
echo "busybox doesn't exists or dynamically linked. please download or/and build static busybox"
|
2020-01-05 23:31:39 +05:30
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-01-09 21:16:50 +05:30
|
|
|
# source config
|
|
|
|
. ./config
|
2020-01-05 23:31:39 +05:30
|
|
|
|
|
|
|
# variables
|
|
|
|
tmpdir="$(mktemp -d /tmp/initramfs.XXXXXXXX)"
|
|
|
|
kernel="$(uname -r)"
|
|
|
|
moddir="/lib/modules"
|
2020-01-28 20:43:42 +05:30
|
|
|
binaries="./busybox findfs blkid mount modprobe umount $binaries"
|
2020-01-05 23:31:39 +05:30
|
|
|
|
|
|
|
# structure
|
2020-01-28 20:43:42 +05:30
|
|
|
for d in dev tmp var run etc usr/lib usr/bin mnt/root proc root sys; do
|
2020-01-07 03:09:15 +05:30
|
|
|
mkdir -p "$tmpdir/$d"
|
2020-01-05 23:31:39 +05:30
|
|
|
done
|
|
|
|
|
2020-01-11 07:28:18 +05:30
|
|
|
# symlinks
|
2020-01-19 02:31:21 +05:30
|
|
|
( cd "$tmpdir" && {
|
2020-01-28 20:43:42 +05:30
|
|
|
ln -s usr/lib lib
|
|
|
|
ln -s usr/lib lib64
|
|
|
|
ln -s usr/bin bin
|
|
|
|
ln -s usr/bin sbin
|
|
|
|
ln -s ../run var/run
|
2020-01-19 02:31:21 +05:30
|
|
|
cd "$tmpdir/usr"
|
2020-01-28 20:43:42 +05:30
|
|
|
ln -s bin sbin
|
|
|
|
ln -s lib lib64
|
2020-01-19 02:31:21 +05:30
|
|
|
} )
|
2020-01-07 03:09:15 +05:30
|
|
|
|
|
|
|
# TODO parse fstab | crypttab
|
2020-01-05 23:31:39 +05:30
|
|
|
#while [ "$use_fstab" -eq 1 ] && read fs dir type opts; do thing; done < /etc/fstab
|
|
|
|
|
2020-01-28 20:43:42 +05:30
|
|
|
# handle device managers
|
|
|
|
if [ "$use_mdevd" = 1 ]; then
|
|
|
|
# install mdevd
|
|
|
|
binaries="mdevd mdevd-coldplug $binaries"
|
|
|
|
mkdir "$tmpdir/lib/mdev"
|
|
|
|
cp mdev.conf "$tmpdir/etc"
|
|
|
|
cp storage-device "$tmpdir/lib/mdev"
|
|
|
|
chmod +x "$tmpdir/lib/mdev/storage-device"
|
|
|
|
elif [ "$use_mdev" = 1 ]; then
|
|
|
|
# install mdev
|
|
|
|
mkdir "$tmpdir/lib/mdev"
|
|
|
|
cp mdev.conf "$tmpdir/etc"
|
|
|
|
cp storage-device "$tmpdir/lib/mdev"
|
|
|
|
chmod +x "$tmpdir/lib/mdev/storage-device"
|
|
|
|
elif [ "$use_udev" = 1 ]; then
|
|
|
|
# install udev
|
|
|
|
binaries="udevd udevadm dmsetup $binaries"
|
|
|
|
find /usr/lib/udev -type f | grep -v "rc_keymaps\|hwdb.d" | cpio -pd "$tmpdir"
|
|
|
|
fi
|
2020-01-19 02:31:21 +05:30
|
|
|
|
2020-01-28 20:43:42 +05:30
|
|
|
cat <<EOF > "$tmpdir/etc/group"
|
|
|
|
root:x:0:
|
|
|
|
tty:x:5:
|
|
|
|
dialout:x:11:
|
|
|
|
uucp:x:14:
|
|
|
|
kmem:x:3:
|
|
|
|
input:x:25:
|
|
|
|
video:x:13:
|
|
|
|
audio:x:12:
|
|
|
|
lp:x:10:
|
|
|
|
disk:x:9:
|
|
|
|
cdrom:x:16:
|
|
|
|
tape:x:6:
|
|
|
|
kvm:x:24:
|
|
|
|
floppy:x:8:
|
|
|
|
EOF
|
2020-01-25 16:57:02 +05:30
|
|
|
|
2020-01-28 20:43:42 +05:30
|
|
|
cat <<EOF > "$tmpdir/etc/passwd"
|
|
|
|
root:x:0:0::/root:/bin/sh
|
|
|
|
nobody:x:99:99::/:/bin/false
|
|
|
|
EOF
|
2020-01-25 16:57:02 +05:30
|
|
|
|
2020-01-28 20:43:42 +05:30
|
|
|
# handle lvm
|
|
|
|
if [ "$use_lvm" = 1 ] && [ -x "$(command -v lvm)" ]; then
|
|
|
|
binaries="lvm $binaries"
|
|
|
|
#mkdir "$tmpdir/etc/lvm"
|
|
|
|
|
|
|
|
# use_lvmetad = 0 - avoid lvmetad missing warning message
|
|
|
|
#cat <<EOF > "$tmpdir/etc/lvm/lvmlocal.conf"
|
|
|
|
#local {
|
|
|
|
# issue_discards = ${lvm_discard:-0}
|
|
|
|
# use_lvmetad = 0
|
|
|
|
#}
|
|
|
|
#EOF
|
2020-01-25 16:57:02 +05:30
|
|
|
# TODO implement use_lvmconf
|
2020-01-19 02:31:21 +05:30
|
|
|
fi
|
|
|
|
|
2020-01-25 16:57:02 +05:30
|
|
|
# handle luks
|
|
|
|
if [ "$use_luks" = 1 ] && [ -x "$(command -v cryptsetup)" ]; then
|
2020-01-28 20:43:42 +05:30
|
|
|
binaries="cryptsetup $binaries"
|
2020-01-25 16:57:02 +05:30
|
|
|
|
|
|
|
# avoid locking directory missing warning message
|
|
|
|
mkdir "$tmpdir/run/cryptsetup"
|
|
|
|
|
|
|
|
# TODO get rid of this workaround
|
|
|
|
# workaround for luks2
|
2020-01-28 20:43:42 +05:30
|
|
|
cp /usr/lib/libgcc_s.so.1 "$tmpdir/usr/lib"
|
2020-01-25 16:57:02 +05:30
|
|
|
|
2020-01-28 20:43:42 +05:30
|
|
|
[ "$luks_discard" = 1 ] && luks_args="--allow-discards $luks_args"
|
2020-01-25 16:57:02 +05:30
|
|
|
|
|
|
|
# TODO detached header
|
|
|
|
# TODO keyfile
|
|
|
|
fi
|
2020-01-19 02:31:21 +05:30
|
|
|
|
2020-01-06 00:20:12 +05:30
|
|
|
# TODO rewrite drivers installing | handle $drivers config var
|
2020-01-05 23:31:39 +05:30
|
|
|
# install drivers
|
2020-01-12 02:42:24 +05:30
|
|
|
find \
|
|
|
|
"$moddir/$kernel/kernel/drivers/virtio" \
|
|
|
|
"$moddir/$kernel/kernel/arch" \
|
|
|
|
"$moddir/$kernel/kernel/crypto" \
|
|
|
|
"$moddir/$kernel/kernel/fs" \
|
|
|
|
"$moddir/$kernel/kernel/lib" \
|
|
|
|
"$moddir/$kernel/kernel/drivers/block" \
|
|
|
|
"$moddir/$kernel/kernel/drivers/ata" \
|
|
|
|
"$moddir/$kernel/kernel/drivers/md" \
|
|
|
|
"$moddir/$kernel/kernel/drivers/scsi" \
|
|
|
|
"$moddir/$kernel/kernel/drivers/usb/storage" \
|
|
|
|
"$moddir/$kernel/kernel/drivers/usb/host" \
|
|
|
|
-type f | cpio -pd "$tmpdir" 2>/dev/null
|
|
|
|
|
|
|
|
# install list of drivers
|
2020-01-07 03:09:15 +05:30
|
|
|
cp "$moddir/$kernel/modules.softdep" "$moddir/$kernel/modules.builtin" "$moddir/$kernel/modules.order" "$tmpdir/$moddir/$kernel"
|
2020-01-05 23:31:39 +05:30
|
|
|
|
2020-01-12 02:42:24 +05:30
|
|
|
# generate dependencies list of drivers
|
2020-01-07 03:09:15 +05:30
|
|
|
depmod -b "$tmpdir" "$kernel"
|
2020-01-05 23:31:39 +05:30
|
|
|
|
2020-01-13 07:52:45 +05:30
|
|
|
# TODO make strip optional
|
2020-01-12 02:42:24 +05:30
|
|
|
# install and strip binaries and libraries
|
2020-01-07 03:09:15 +05:30
|
|
|
for b in $(echo $binaries); do
|
2020-01-13 07:52:45 +05:30
|
|
|
# check binary existence
|
|
|
|
if [ ! "$(command -v $b)" ]; then
|
|
|
|
echo "$b doesn't exists or permission denied"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# copy and strip binary
|
2020-01-12 02:42:24 +05:30
|
|
|
cp "$(command -v $b)" "$tmpdir/usr/bin"
|
|
|
|
chmod +x "$tmpdir/usr/bin/$b"
|
|
|
|
strip -s "$tmpdir/usr/bin/$b"
|
|
|
|
|
2020-01-07 03:09:15 +05:30
|
|
|
# check statically linking
|
2020-01-12 02:42:24 +05:30
|
|
|
ldd "$(command -v $b)" >/dev/null || continue
|
2020-01-07 03:09:15 +05:30
|
|
|
|
2020-01-12 02:42:24 +05:30
|
|
|
# handle libraries symlinks for dymanically linked binaries
|
|
|
|
for l in $(ldd "$(command -v $b)" | sed -nre 's,.* (/.*lib.*/.*.so.*) .*,\1,p' -e 's,.*(/lib.*/ld.*.so.*) .*,\1,p'); do
|
|
|
|
# check symlink
|
2020-01-07 03:09:15 +05:30
|
|
|
if [ -h "$l" ]; then
|
2020-01-13 07:52:45 +05:30
|
|
|
# check lib already existence
|
2020-01-28 20:43:42 +05:30
|
|
|
if [ ! -e "$tmpdir/usr/lib/${l##*/}" ] && [ ! -e "$tmpdir$(readlink -f $l)" ]; then
|
2020-01-12 02:42:24 +05:30
|
|
|
# regular
|
|
|
|
cp "$(readlink -f $l)" "$tmpdir/usr/lib"
|
2020-01-28 20:43:42 +05:30
|
|
|
strip -s "$tmpdir$(readlink -f $l)"
|
|
|
|
# TODO handle all symlinks
|
|
|
|
# symlink may link to symlink
|
|
|
|
[ -h "/usr/lib/$(readlink $l)" ] && cp -a "/usr/lib/$(readlink $l)" "$tmpdir/usr/lib"
|
2020-01-12 02:42:24 +05:30
|
|
|
# symlink
|
|
|
|
cp -a "$l" "$tmpdir/usr/lib"
|
|
|
|
fi
|
2020-01-07 03:09:15 +05:30
|
|
|
else
|
2020-01-14 05:43:34 +05:30
|
|
|
if [ ! -e "$tmpdir/usr/lib/${l##*/}" ]; then
|
|
|
|
cp "$l" "$tmpdir/usr/lib"
|
|
|
|
strip -s "$tmpdir/usr/lib/${l##*/}"
|
2020-01-12 02:42:24 +05:30
|
|
|
fi
|
2020-01-07 03:09:15 +05:30
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
2020-01-05 23:31:39 +05:30
|
|
|
|
2020-01-12 22:13:16 +05:30
|
|
|
# install init
|
2020-01-13 07:52:45 +05:30
|
|
|
cp ./init "$tmpdir/init"
|
|
|
|
chmod +x "$tmpdir/init"
|
2020-01-05 23:31:39 +05:30
|
|
|
|
2020-01-07 03:09:15 +05:30
|
|
|
# initialize config
|
2020-01-05 23:31:39 +05:30
|
|
|
cat <<EOF > "$tmpdir/config"
|
|
|
|
root="$root"
|
2020-01-25 16:57:02 +05:30
|
|
|
root_type="$root_type"
|
|
|
|
root_args="$root_args"
|
2020-01-28 20:43:42 +05:30
|
|
|
use_mdevd="$use_mdevd"
|
|
|
|
use_mdev="$use_mdev"
|
|
|
|
use_udev="$use_udev"
|
2020-01-25 16:57:02 +05:30
|
|
|
#drivers="$drivers"
|
|
|
|
use_lvm="$use_lvm"
|
|
|
|
use_luks="$use_luks"
|
|
|
|
luks_root="$luks_root"
|
|
|
|
luks_header="$luks_header"
|
|
|
|
luks_keyfile="$luks_keyfile"
|
|
|
|
luks_args="$luks_args"
|
2020-01-05 23:31:39 +05:30
|
|
|
EOF
|
|
|
|
|
2020-01-25 16:57:02 +05:30
|
|
|
# TODO add another compession tools
|
2020-01-05 23:31:39 +05:30
|
|
|
# packing
|
2020-01-12 02:42:24 +05:30
|
|
|
if ! ( cd "$tmpdir" && find . | cpio --create --verbose --format=newc | gzip --best ) > "./initramfs-$kernel.img.gz" 2>/dev/null; then
|
2020-01-07 03:09:15 +05:30
|
|
|
echo "failed"
|
|
|
|
exit 1
|
2020-01-05 23:31:39 +05:30
|
|
|
fi
|
|
|
|
|
2020-01-11 07:28:18 +05:30
|
|
|
# remove tmpdir
|
2020-01-05 23:31:39 +05:30
|
|
|
rm -rf "$tmpdir"
|
|
|
|
|
|
|
|
echo "done! check out initramfs-$kernel.img.gz"
|