move initramfs extraction to separate script

This commit is contained in:
ading2210 2023-12-20 22:05:17 -08:00
parent 6c98649c9f
commit 86269dfa05
4 changed files with 38 additions and 27 deletions

2
.gitignore vendored
View File

@ -1,2 +1,2 @@
/data
/test.bin
/*.bin

View File

@ -9,6 +9,7 @@ fi
. ./patch_initramfs.sh
. ./build_image.sh
. ./shim_utils.sh
print_help() {
echo "Usage: ./build.sh output_path shim_path rootfs_dir"
@ -55,34 +56,9 @@ mkdir $kernel_dir -p
dd if=$kernel_loop of=$kernel_dir/kernel.bin bs=1M status=none
echo "extracting data from kernel"
previous_dir=$(pwd)
cd $kernel_dir
if [ -e "${kernel_dir}/binwalk.out" ]; then
#don't run binwalk again if we don't need to
binwalk_out=$(cat $kernel_dir/binwalk.out)
else
binwalk_out=$(binwalk --extract kernel.bin --run-as=root)
echo $binwalk_out > $kernel_dir/binwalk.out
fi
#i can't be bothered to learn how to use sed
extracted_file=$(echo $binwalk_out | pcregrep -o1 "\d+\s+0x([0-9A-F]+)\s+gzip compressed data")
echo "extracting initramfs archive from kernel (this may take a while)"
cd _kernel.bin.extracted/
if [ ! -e "_${extracted_file}.extracted/" ]; then
binwalk --extract $extracted_file --run-as=root > /dev/null
fi
cd "_${extracted_file}.extracted/"
cpio_file=$(file ./* | pcregrep -o1 "([0-9A-F]+):\s+ASCII cpio archive")
echo "extracting initramfs cpio archive"
initramfs_dir=/tmp/shim_initramfs
rm -rf $initramfs_dir
cat $cpio_file | cpio -D $initramfs_dir -imd --quiet
echo "shim initramfs extracted to ${initramfs_dir}"
#leave /tmp
cd $previous_dir
extract_initramfs $kernel_dir/kernel.bin $kernel_dir $initramfs_dir
echo "patching initramfs"
patch_initramfs $initramfs_dir

10
build_squashfs.sh Executable file
View File

@ -0,0 +1,10 @@
#!/bin/bash
#build a rootfs that uses a squashfs + unionfs
#consists of a minimal busybox system containing:
# - FUSE kernel modules from the shim
# - unionfs-fuse statically compiled
# - the main squashfs, compressed with gzip
#todo - gotta refactor the other build scripts first

25
shim_utils.sh Executable file
View File

@ -0,0 +1,25 @@
#!/bin/bash
#utilties for reading shim disk images
extract_initramfs() {
local kernel_bin="$1"
local working_dir="$2"
local output_dir="$3"
#first stage
local kernel_file="$(basename $kernel_bin)"
local binwalk_out=$(binwalk --extract $kernel_bin --directory=$working_dir --run-as=root)
local stage1_file=$(echo $binwalk_out | pcregrep -o1 "\d+\s+0x([0-9A-F]+)\s+gzip compressed data")
local stage1_dir="$working_dir/_$kernel_file.extracted"
local stage1_path="$stage1_dir/$stage1_file"
#second stage
binwalk --extract $stage1_path --directory=$stage1_dir --run-as=root
local stage2_dir="$stage1_dir/_$stage1_file.extracted/"
local cpio_file=$(file $stage2_dir/* | pcregrep -o1 "([0-9A-F]+):\s+ASCII cpio archive")
local cpio_path="$stage2_dir/$cpio_file"
rm -rf $output_dir
cat $cpio_path | cpio -D $output_dir -imd --quiet
}