From 60283c7e300c00904ef5548bb6b8945c7e84767b Mon Sep 17 00:00:00 2001 From: Hal Emmerich Date: Fri, 20 Sep 2019 23:50:08 -0500 Subject: [PATCH] Initramfs booting funcitonal, built in using kernel config --- .gitignore | 1 + makefile | 7 +- resources/BuildResources/config | 5 +- resources/BuildResources/initramfs-init | 10 ++- resources/BuildResources/kernel.its | 12 +++ scripts/buildFilesystem.sh | 28 +++++-- scripts/buildInitramFs.sh | 102 ++++++++++++++++++++++++ scripts/buildKernel.sh | 8 ++ scripts/injectKernelIntoFS.sh | 19 ++++- 9 files changed, 177 insertions(+), 15 deletions(-) create mode 100755 scripts/buildInitramFs.sh diff --git a/.gitignore b/.gitignore index d4ed1bf..d3f17d8 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ PrawnOS-Alpha-c201-libre-2GB* PrawnOS-*-Alpha-c201-libre-2GB* tmp.* PrawnOS-Alpha-c201-libre-2GB-git*.img +PrawnOS-initramfs.cpio.gz diff --git a/makefile b/makefile index f06d00b..8d246f8 100644 --- a/makefile +++ b/makefile @@ -70,6 +70,10 @@ clean_all: kernel: scripts/buildKernel.sh $(KVER) +.PHONY: initramfs +initramfs: + scripts/buildInitramFs.sh + #makes the base filesystem image, no kernel only if the base image isnt present .PHONY: filesystem filesystem: @@ -88,8 +92,9 @@ injected_image: #makes a copy of the base image with a new injected kernel .PHONY: image image: make clean_img - make kernel make filesystem + make initramfs + make kernel #Make a new copy of the filesystem image cp $(BASE) $(OUTNAME) make kernel_inject diff --git a/resources/BuildResources/config b/resources/BuildResources/config index f24cc3f..223c4a9 100644 --- a/resources/BuildResources/config +++ b/resources/BuildResources/config @@ -151,13 +151,16 @@ CONFIG_NET_NS=y # CONFIG_SYSFS_DEPRECATED is not set CONFIG_RELAY=y CONFIG_BLK_DEV_INITRD=y -CONFIG_INITRAMFS_SOURCE="" +CONFIG_INITRAMFS_SOURCE="PrawnOS-initramfs.cpio.gz" +CONFIG_INITRAMFS_ROOT_UID=0 +CONFIG_INITRAMFS_ROOT_GID=0 CONFIG_RD_GZIP=y # CONFIG_RD_BZIP2 is not set # CONFIG_RD_LZMA is not set CONFIG_RD_XZ=y # CONFIG_RD_LZO is not set # CONFIG_RD_LZ4 is not set +CONFIG_INITRAMFS_COMPRESSION=".gz" # CONFIG_CC_OPTIMIZE_FOR_PERFORMANCE is not set CONFIG_CC_OPTIMIZE_FOR_SIZE=y CONFIG_SYSCTL=y diff --git a/resources/BuildResources/initramfs-init b/resources/BuildResources/initramfs-init index 3576425..0317b4e 100644 --- a/resources/BuildResources/initramfs-init +++ b/resources/BuildResources/initramfs-init @@ -1,6 +1,8 @@ #!/bin/busybox sh +echo In PrawnOS Init + cmdline() { local value value=" $(cat /proc/cmdline) " @@ -21,7 +23,6 @@ rootpartuuid() { mount -n -t proc proc /proc mount -n -t sysfs sysfs /sys mount -n -t devtmpfs devtmpfs /dev -mount -n -t tmpfs tmpfs /run # get the root device, so we can find the boot partiton UNPARSED=$(cmdline root) @@ -35,6 +36,8 @@ ROOT_DEV="${BLKID%1:*}" echo ${ROOT_DEV} +#uncomment for debugging +# exec setsid /bin/sh -c 'exec /bin/sh /dev/tty1 2>&1' # we use this to change what cmdline options get passed into # the next boot stage, aka to enable root encryption @@ -46,6 +49,9 @@ mount ${ROOT_DEV}3 /newroot umount /sys umount /proc +umount /dev + +# exec setsid /bin/sh -c 'exec /bin/sh /dev/tty1 2>&1' #swith to the new rootfs -exec swith_root /newroot "/sbin/init" ${CMDLINE} \ No newline at end of file +exec switch_root /newroot /sbin/init ${CMDLINE} \ No newline at end of file diff --git a/resources/BuildResources/kernel.its b/resources/BuildResources/kernel.its index 549ef9c..ac2967f 100644 --- a/resources/BuildResources/kernel.its +++ b/resources/BuildResources/kernel.its @@ -27,12 +27,24 @@ algo = "sha1"; }; }; + ramdisk@1{ + description = "initrd.img"; + data = /incbin/("PrawnOS-initramfs.cpio.gz"); + type = "ramdisk"; + arch = "arm"; + os = "linux"; + compression = "none"; + hash@1{ + algo = "sha1"; + }; + }; }; configurations { default = "conf"; conf{ kernel = "kernel"; fdt = "fdt"; + ramdisk = "ramdisk@1"; }; }; }; diff --git a/scripts/buildFilesystem.sh b/scripts/buildFilesystem.sh index 8e5aa55..0cb4996 100755 --- a/scripts/buildFilesystem.sh +++ b/scripts/buildFilesystem.sh @@ -163,17 +163,32 @@ chroot $outmnt locale-gen #Install the base packages chroot $outmnt apt update -chroot $outmnt apt install -y initscripts udev kmod net-tools inetutils-ping traceroute iproute2 isc-dhcp-client wpasupplicant iw alsa-utils cgpt vim-tiny less psmisc netcat-openbsd ca-certificates bzip2 xz-utils ifupdown nano apt-utils git kpartx gdisk parted rsync busybox-static +chroot $outmnt apt install -y initscripts udev kmod net-tools inetutils-ping traceroute iproute2 isc-dhcp-client wpasupplicant iw alsa-utils cgpt vim-tiny less psmisc netcat-openbsd ca-certificates bzip2 xz-utils ifupdown nano apt-utils git kpartx gdisk parted rsync busybox-static cryptsetup #make the initramfs image that gets copied to partiton 2 +#this is not yet fully funtional, needs the kernel parts which are +#added in "injectKernelIntoFS.sh" + #make a skeleton filesystem -initramfs_src=$outmnt/usr/src/initramfs -mkdir -p $initramfs_src/ -mkdir ${initramfs_src}/{bin,dev,etc,newroot,proc,sys,sbin,run,lib} +initramfs_src=$outmnt/InstallResources/initramfs_src +mkdir -p $initramfs_src +mkdir $initramfs_src/bin +mkdir $initramfs_src/dev +mkdir $initramfs_src/etc +mkdir $initramfs_src/newroot +mkdir $initramfs_src/proc +mkdir $initramfs_src/sys +mkdir $initramfs_src/sbin +mkdir $initramfs_src/run +mkdir $initramfs_src/lib mkdir $initramfs_src/lib/arm-linux-gnueabihf -#install the few tools we need + +#install the few tools we need, and the supporting libs cp $outmnt/bin/busybox $outmnt/sbin/cryptsetup $initramfs_src/bin/ -cp ${outmnt}/lib/arm-linux-gnueabihf/{libblkid.so.1,libc.so.6,libuuid.so.1} ${initramfs_src}/lib/arm-linux-gnueabihf/ +cp $outmnt/lib/arm-linux-gnueabihf/libblkid.so.1 $initramfs_src/lib/arm-linux-gnueabihf/ +cp $outmnt/lib/arm-linux-gnueabihf/libuuid.so.1 $initramfs_src/lib/arm-linux-gnueabihf/ +cp $outmnt/lib/arm-linux-gnueabihf/libc.so.6 $initramfs_src/lib/arm-linux-gnueabihf/ + cp $outmnt/lib/ld-linux-armhf.so.3 $initramfs_src/lib/ cp $outmnt/sbin/blkid $initramfs_src/bin/ @@ -182,7 +197,6 @@ cp $build_resources/initramfs-init $initramfs_src/init chmod +x $initramfs_src/init #compress and install -#TODO, make this correct find $initramfs_src -print0 | cpio --null --create --verbose --format=newc | gzip --best > $outmnt/boot/PrawnOS-initramfs.cpio.gz diff --git a/scripts/buildInitramFs.sh b/scripts/buildInitramFs.sh new file mode 100755 index 0000000..5f5efcc --- /dev/null +++ b/scripts/buildInitramFs.sh @@ -0,0 +1,102 @@ + +#!/bin/sh -xe + +#Build initramfs image + + +# This file is part of PrawnOS (http://www.prawnos.com) +# Copyright (c) 2018 Hal Emmerich + +# PrawnOS is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 +# as published by the Free Software Foundation. + +# PrawnOS is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with PrawnOS. If not, see . + + +outmnt=$(mktemp -d -p `pwd`) +outdev=/dev/loop7 +KVER=$1 +ROOT_DIR=`pwd` +build_resources=$ROOT_DIR/resources/BuildResources + +if [ ! -f $ROOT_DIR/PrawnOS-*-c201-libre-2GB.img-BASE ] +then + echo "No base filesystem, run 'make filesystem' first" + exit 1 +fi + +#A hacky way to ensure the loops are properly unmounted and the temp files are properly deleted. +#Without this, a reboot is sometimes required to properly clean the loop devices and ensure a clean build +cleanup() { + set +e + + umount -l $outmnt > /dev/null 2>&1 + rmdir $outmnt > /dev/null 2>&1 + losetup -d $outdev > /dev/null 2>&1 + + set +e + + umount -l $outmnt > /dev/null 2>&1 + rmdir $outmnt > /dev/null 2>&1 + losetup -d $outdev > /dev/null 2>&1 +} + +trap cleanup INT TERM EXIT + +losetup -P $outdev $ROOT_DIR/PrawnOS-*-c201-libre-2GB.img-BASE +#mount the root filesystem +mount -o noatime ${outdev}p3 $outmnt +#mount the initramfs partition +mount -o noatime ${outdev}p2 $outmnt/boot + +#make a skeleton filesystem +initramfs_src=$outmnt/InstallResources/initramfs_src +rm -rf $initramfs_src* +mkdir -p $initramfs_src +mkdir $initramfs_src/bin +mkdir $initramfs_src/dev +mkdir $initramfs_src/etc +mkdir $initramfs_src/newroot +mkdir $initramfs_src/proc +mkdir $initramfs_src/sys +mkdir $initramfs_src/sbin +mkdir $initramfs_src/run +mkdir $initramfs_src/lib +mkdir $initramfs_src/lib/arm-linux-gnueabihf + +#install the few tools we need, and the supporting libs +cp $outmnt/bin/busybox $outmnt/sbin/cryptsetup $initramfs_src/bin/ +cp $outmnt/lib/arm-linux-gnueabihf/libblkid.so.1 $initramfs_src/lib/arm-linux-gnueabihf/ +cp $outmnt/lib/arm-linux-gnueabihf/libuuid.so.1 $initramfs_src/lib/arm-linux-gnueabihf/ +cp $outmnt/lib/arm-linux-gnueabihf/libc.so.6 $initramfs_src/lib/arm-linux-gnueabihf/ + +cp $outmnt/lib/ld-linux-armhf.so.3 $initramfs_src/lib/ +cp $outmnt/sbin/blkid $initramfs_src/bin/ + +#add the init script +cp $build_resources/initramfs-init $initramfs_src/init +chmod +x $initramfs_src/init + +#compress and install +rm -rf $outmnt/boot/PrawnOS-initramfs.cpio.gz +cd $initramfs_src +ln -s busybox bin/cat +ln -s busybox bin/mount +ln -s busybox bin/sh +ln -s busybox bin/switch_root +ln -s busybox bin/umount +find . -print0 | cpio --null --create --verbose --format=newc | gzip --best > $outmnt/boot/PrawnOS-initramfs.cpio.gz + +cd $ROOT_DIR + +[ ! -d build ] && mkdir build +cd build +# store for kernel building +cp $outmnt/boot/PrawnOS-initramfs.cpio.gz . diff --git a/scripts/buildKernel.sh b/scripts/buildKernel.sh index 1da323c..93f6259 100755 --- a/scripts/buildKernel.sh +++ b/scripts/buildKernel.sh @@ -29,6 +29,12 @@ ROOT_DIR=`pwd` RESOURCES=$ROOT_DIR/resources/BuildResources [ ! -d build ] && mkdir build cd build +if [ ! -f PrawnOS-initramfs.cpio.gz ] +then + echo "No initramfs image, run 'make initramfs' first" + cd $ROOT_DIR + exit 1 +fi # build AR9271 firmware [ ! -d open-ath9k-htc-firmware ] && git clone --depth 1 https://github.com/qca/open-ath9k-htc-firmware.git cd open-ath9k-htc-firmware @@ -51,6 +57,8 @@ make mrproper [ "$TEST_PATCHES" = true ] && for i in $RESOURCES/patches-untested/kernel/*.patch; do patch -p1 < $i; done [ "$TEST_PATCHES" = true ] && for i in $RESOURCES/patches-untested/DTS/*.patch; do patch -p1 < $i; done +#copy in the initramfs and kernel config +cp $ROOT_DIR/build/PrawnOS-initramfs.cpio.gz . cp $RESOURCES/config .config make -j `grep ^processor /proc/cpuinfo | wc -l` CROSS_COMPILE=arm-none-eabi- ARCH=arm zImage modules dtbs [ ! -h kernel.its ] && ln -s $RESOURCES/kernel.its . diff --git a/scripts/injectKernelIntoFS.sh b/scripts/injectKernelIntoFS.sh index 3198c47..158b2bd 100755 --- a/scripts/injectKernelIntoFS.sh +++ b/scripts/injectKernelIntoFS.sh @@ -53,16 +53,27 @@ trap cleanup INT TERM EXIT #Mount the build filesystem image losetup -P $outdev $2 -mount -o noatime ${outdev}p2 $outmnt +#mount the root filesystem +mount -o noatime ${outdev}p3 $outmnt +#mount the initramfs partition +# mount -o noatime ${outdev}p2 $outmnt/boot # put the kernel in the kernel partition, modules in /lib/modules and AR9271 # firmware in /lib/firmware dd if=$build_resources/blank_kernel of=${outdev}p1 conv=notrunc dd if=build/linux-$KVER/vmlinux.kpart of=${outdev}p1 conv=notrunc make -C build/linux-$KVER ARCH=arm INSTALL_MOD_PATH=$outmnt modules_install -#Dont put ath firmware in filesystem, it is now built into the kernel image -# install -D -m 644 build/open-ath9k-htc-firmware/target_firmware/htc_9271.fw $outmnt/lib/firmware/ath9k_htc/htc_9271-1.4.0.fw -# install -D -m 644 build/open-ath9k-htc-firmware/target_firmware/htc_7010.fw $outmnt/lib/firmware/ath9k_htc/htc_7010-1.4.0.fw + +# put the required kernel items into the initramfs aka the device tree and the kernel image +# TODO: the in-place kernel upgrade script must be changed to copy in new versions of these files +# cp build/linux-$KVER/arch/arm/boot/dts/rk3288-veyron-speedy.dtb $outmnt/boot/rk3288-veyron-speedy.dtb +# cp build/linux-$KVER/vmlinux.kpart $outmnt/boot + +#TODO: do we actually need the kernel in /boot?? I think not. lets test and find out + +# the initramfs is build into the kernel image + +# the ath9k firmware is built into the kernel image, so nothing else must be done umount -l $outmnt > /dev/null 2>&1 rmdir $outmnt > /dev/null 2>&1