shimboot/build_squashfs.sh

87 lines
2.0 KiB
Bash
Raw Normal View History

#!/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
2023-12-21 00:48:32 -06:00
#this script is currently incomplete
2023-12-21 00:48:32 -06:00
set -e
if [ "$DEBUG" ]; then
set -x
fi
2023-12-21 00:48:32 -06:00
. ./image_utils.sh
. ./shim_utils.sh
print_help() {
echo "Usage: ./build_squashfs.sh rootfs_dir uncompressed_rootfs_dir path_to_shim"
}
if [ "$EUID" -ne 0 ]; then
echo "this needs to be run as root."
exit 1
fi
if [ -z "$3" ]; then
print_help
exit 1
fi
compile_unionfs() {
local out_path="$1"
local working_path="$2"
local repo_url="https://github.com/rpodgorny/unionfs-fuse"
local original_dir="$(pwd)"
2023-12-22 00:51:47 -06:00
local core_count="$(nproc --all)"
2023-12-21 00:48:32 -06:00
rm -rf $working_path
git clone $repo_url -b master --depth=1 $working_path
cd $working_path
2023-12-22 00:51:47 -06:00
env LDFLAGS="-static" make -j$core_count
2023-12-21 00:48:32 -06:00
local binary_path="$working_path/src/unionfs"
cp $binary_path $out_path
2023-12-22 00:51:47 -06:00
cd $original_dir
2023-12-21 00:48:32 -06:00
}
2024-01-25 15:19:25 -06:00
rootfs_dir=$(realpath -m $1)
old_dir=$(realpath -m $2)
shim_path=$(realpath -m $3)
2023-12-21 00:48:32 -06:00
shim_rootfs="/tmp/shim_rootfs"
2023-12-21 18:58:08 -06:00
root_squashfs="$rootfs_dir/root.squashfs"
modules_squashfs="$rootfs_dir/modules.squashfs"
2023-12-21 00:48:32 -06:00
kernel_dir=/tmp/shim_kernel
2023-12-21 18:58:08 -06:00
unionfs_dir="/tmp/unionfs-fuse"
2023-12-21 00:48:32 -06:00
echo "compiling unionfs-fuse"
2023-12-21 18:58:08 -06:00
compile_unionfs $unionfs_dir/unionfs $unionfs_dir
2023-12-21 00:48:32 -06:00
2023-12-21 18:58:08 -06:00
echo "creating loop device for shim"
2023-12-21 00:48:32 -06:00
shim_loop=$(create_loop "${shim_path}")
2023-12-21 18:58:08 -06:00
kernel_loop="${shim_loop}p2" #KERN-A should always be p2
2023-12-21 00:48:32 -06:00
echo "copying shim kernel"
2023-12-21 18:58:08 -06:00
rm -rf $kernel_dir
2023-12-21 00:48:32 -06:00
mkdir $kernel_dir -p
2023-12-21 18:58:08 -06:00
dd if=$kernel_loop of=$kernel_dir/kernel.bin bs=1M status=progress
2023-12-21 00:48:32 -06:00
2023-12-22 00:51:47 -06:00
echo "extracting initramfs from kernel (this may take a while)"
2023-12-21 00:48:32 -06:00
extract_initramfs $kernel_dir/kernel.bin $kernel_dir $rootfs_dir
2023-12-21 18:58:08 -06:00
rm -rf $rootfs_dir/init
2023-12-22 21:15:24 -06:00
echo "removeing shim loop device"
losetup -d $shim_loop
2023-12-21 18:58:08 -06:00
echo "compressing old rootfs"
mksquashfs $old_dir $root_squashfs -noappend -comp gzip
2023-12-21 00:48:32 -06:00
2023-12-21 18:58:08 -06:00
echo "patching new rootfs"
mv $unionfs_dir/unionfs $rootfs_dir/bin/unionfs
2023-12-22 00:51:47 -06:00
cp -ar squashfs/* $rootfs_dir/
chmod +x $rootfs_dir/bin/*
2023-12-21 00:48:32 -06:00
2023-12-22 00:51:47 -06:00
echo "done"