initial luks support

This commit is contained in:
illiliti 2020-01-25 14:27:02 +03:00
parent 5bc7f6e047
commit f340b76e5f
3 changed files with 71 additions and 38 deletions

32
config
View File

@ -5,14 +5,14 @@
# parse fstab # parse fstab
#use_fstab=0 #use_fstab=0
# root fs # root fs ( device,partuuid,uuid,label )
root="UUID=28305682-0dfe-40a9-bf39-5df42123b749" root="UUID=07729c48-25d8-4096-acaf-ce5322915680"
# root fs type # root type
#rootfstype="" #root_type=""
# root fs mount options # root mount options
#rootflags="" #root_args=""
# drivers # drivers
#drivers="" #drivers=""
@ -24,22 +24,28 @@ root="UUID=28305682-0dfe-40a9-bf39-5df42123b749"
use_lvm=1 use_lvm=1
# LVM include config # LVM include config
use_lvmconf=0 #use_lvmconf=0
# LVM issue_discards # LVM issue_discards
lvm_discard=0 lvm_discard=1
# LUKS support # LUKS support
#use_luks=0 use_luks=1
# parse crypttab # parse crypttab
#use_crypttab=0 #use_crypttab=0
# LUKS header # LUKS encrypted root ( device,partuuid,uuid,label )
#luks_header=/path/to/header luks_root="PARTUUID=b04395be-f467-458b-8630-9a429b487600"
# LUKS detached header
#luks_header="/path/to/header"
# LUKS keyfile # LUKS keyfile
#luks_keyfile=/path/to/keyfile #luks_keyfile="/path/to/keyfile"
# LUKS allow_discards # LUKS allow_discards
#luks_discard=0 luks_discard=1
# LUKS unlock options
#luks_args=""

View File

@ -28,7 +28,7 @@ fi
tmpdir="$(mktemp -d /tmp/initramfs.XXXXXXXX)" tmpdir="$(mktemp -d /tmp/initramfs.XXXXXXXX)"
kernel="$(uname -r)" kernel="$(uname -r)"
moddir="/lib/modules" moddir="/lib/modules"
binaries="./busybox udevd udevadm mount modprobe umount $binaries" binaries="./busybox findfs blkid udevd udevadm mount modprobe umount $binaries"
# structure # structure
for d in dev var run etc usr/lib usr/bin mnt/root proc root sys; do for d in dev var run etc usr/lib usr/bin mnt/root proc root sys; do
@ -49,6 +49,7 @@ done
# TODO parse fstab | crypttab # TODO parse fstab | crypttab
#while [ "$use_fstab" -eq 1 ] && read fs dir type opts; do thing; done < /etc/fstab #while [ "$use_fstab" -eq 1 ] && read fs dir type opts; do thing; done < /etc/fstab
# TODO implement busybox mdev/mdevd
# install mdev # install mdev
#mkdir "$tmpdir/usr/lib/mdev" #mkdir "$tmpdir/usr/lib/mdev"
#cp ./mdev/mdev.conf ./mdev/passwd ./mdev/group "$tmpdir/etc" #cp ./mdev/mdev.conf ./mdev/passwd ./mdev/group "$tmpdir/etc"
@ -79,21 +80,39 @@ find "/usr/lib/udev" -type f | grep -v "rc_keymaps\|hwdb.d" | cpio -pd "$tmpdir"
#nobody:x:99:99::/dev/null:/bin/false #nobody:x:99:99::/dev/null:/bin/false
#EOF #EOF
# TODO implement use_lvmconf
# handle lvm # handle lvm
if [ "$use_lvm" = 1 ] && [ -x "$(command -v lvm)" ]; then if [ "$use_lvm" = 1 ] && [ -x "$(command -v lvm)" ]; then
binaries="lvm dmsetup $binaries" binaries="lvm dmsetup $binaries"
mkdir "$tmpdir/etc/lvm" mkdir "$tmpdir/etc/lvm"
# avoid lvmetad warning message
echo "use_lvmetad = 0" >> "$tmpdir/etc/lvm/lvm.conf" echo "use_lvmetad = 0" >> "$tmpdir/etc/lvm/lvm.conf"
if [ "$lvm_discard" = 1 ]; then if [ "$lvm_discard" = 1 ]; then
echo "issue_discards = 1" >> "$tmpdir/etc/lvm/lvm.conf" echo "issue_discards = 1" >> "$tmpdir/etc/lvm/lvm.conf"
fi fi
# TODO implement use_lvmconf
fi fi
# TODO handle luks # handle luks
#if [ "$use_luks" = 1 ] && [ -x "$(command -v cryptsetup)" ]; then if [ "$use_luks" = 1 ] && [ -x "$(command -v cryptsetup)" ]; then
# binaries="cryptsetup $binaries" binaries="cryptsetup dmsetup $binaries"
#fi
# avoid locking directory missing warning message
mkdir "$tmpdir/run/cryptsetup"
# TODO get rid of this workaround
# workaround for luks2
cp "$(readlink -f libgcc_s.so.1)" "$tmpdir/usr/lib"
cp -a /usr/lib/libgcc_s.so.1 "$tmpdir/usr/lib"
if [ "$luks_discard" = 1 ]; then
luks_args="--allow-discards $luks_args"
fi
# TODO detached header
# TODO keyfile
fi
# TODO rewrite drivers installing | handle $drivers config var # TODO rewrite drivers installing | handle $drivers config var
# install drivers # install drivers
@ -162,17 +181,20 @@ chmod +x "$tmpdir/init"
# initialize config # initialize config
cat <<EOF > "$tmpdir/config" cat <<EOF > "$tmpdir/config"
root="$root" root="$root"
rootfstype="$rootfstype" root_type="$root_type"
rootflags="$rootflags" root_args="$root_args"
drivers="$drivers" #drivers="$drivers"
#use_lvm="$use_lvm" use_lvm="$use_lvm"
#lvm_discard="$lvm_discard" lvm_discard="$lvm_discard"
#use_luks="$use_luks" use_luks="$use_luks"
#luks_header="$luks_header" luks_root="$luks_root"
#luks_keyfile="$luks_keyfile" luks_header="$luks_header"
#luks_discard="$luks_discard" luks_keyfile="$luks_keyfile"
luks_discard="$luks_discard"
luks_args="$luks_args"
EOF EOF
# TODO add another compession tools
# packing # packing
if ! ( cd "$tmpdir" && find . | cpio --create --verbose --format=newc | gzip --best ) > "./initramfs-$kernel.img.gz" 2>/dev/null; then if ! ( cd "$tmpdir" && find . | cpio --create --verbose --format=newc | gzip --best ) > "./initramfs-$kernel.img.gz" 2>/dev/null; then
echo "failed" echo "failed"

25
init
View File

@ -4,7 +4,7 @@
set -x set -x
# install busybox # install busybox
/usr/bin/busybox --install -s /usr/bin /usr/bin/busybox --install
panic() { echo "bruh moment :(" && sh; } panic() { echo "bruh moment :(" && sh; }
@ -19,8 +19,9 @@ mount -t proc none /proc
mount -t sysfs none /sys mount -t sysfs none /sys
mount -t devtmpfs none /dev mount -t devtmpfs none /dev
# TODO implement busybox mdev/mdevd
# setup mdev # setup mdev
#echo "/sbin/mdev" >/proc/sys/kernel/hotplug #echo /sbin/mdev >/proc/sys/kernel/hotplug
#mdev -s #mdev -s
# setup udev # setup udev
@ -31,23 +32,27 @@ udevadm settle
# TODO parse /proc/cmdline # TODO parse /proc/cmdline
# unlock cryptsetup container
[ "$use_luks" = 1 ] && {
luks_root="$(findfs $luks_root)"
# TODO improve mapper name ( crypttab or config option )
cryptsetup $luks_args luksOpen "$luks_root" luks_root || panic
}
# load drivers # load drivers
#modprobe -a $drivers #modprobe -a $drivers
# merge mount flags # merge mount flags
[ -n "$rootflags" ] && mountargs="$rootflags" [ -n "$root_args" ] && mount_args="$root_args"
[ -n "$rootfstype" ] && mountargs="$mountargs -t $rootfstype" [ -n "$root_type" ] && mount_args="$mount_args -t $root_type"
# mount rootfs # mount rootfs
mount $mountargs "$root" "/mnt/root" || panic mount $mount_args "$root" /mnt/root || panic
# clean up # clean up
udevadm control --exit udevadm control --exit
umount /dev /sys /proc
umount "/dev"
umount "/sys"
umount "/proc"
# boot system # boot system
echo SUCCESS echo SUCCESS
exec switch_root "/mnt/root" "/sbin/init" exec switch_root /mnt/root /sbin/init