2023-12-21 01:05:17 -05:00
|
|
|
#!/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 01:48:32 -05:00
|
|
|
#this script is currently incomplete
|
2023-12-21 01:05:17 -05:00
|
|
|
|
2023-12-21 01:48:32 -05:00
|
|
|
set -e
|
|
|
|
if [ "$DEBUG" ]; then
|
|
|
|
set -x
|
|
|
|
fi
|
2023-12-21 01:05:17 -05:00
|
|
|
|
2023-12-21 01:48:32 -05: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 01:51:47 -05:00
|
|
|
local core_count="$(nproc --all)"
|
2023-12-21 01:48:32 -05:00
|
|
|
|
|
|
|
rm -rf $working_path
|
|
|
|
git clone $repo_url -b master --depth=1 $working_path
|
|
|
|
cd $working_path
|
|
|
|
|
2023-12-22 01:51:47 -05:00
|
|
|
env LDFLAGS="-static" make -j$core_count
|
2023-12-21 01:48:32 -05:00
|
|
|
local binary_path="$working_path/src/unionfs"
|
|
|
|
cp $binary_path $out_path
|
2023-12-22 01:51:47 -05:00
|
|
|
cd $original_dir
|
2023-12-21 01:48:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
rootfs_dir=$(realpath $1)
|
2023-12-21 19:58:08 -05:00
|
|
|
old_dir=$(realpath $2)
|
2023-12-21 01:48:32 -05:00
|
|
|
shim_path=$(realpath $3)
|
|
|
|
|
|
|
|
shim_rootfs="/tmp/shim_rootfs"
|
2023-12-21 19:58:08 -05:00
|
|
|
root_squashfs="$rootfs_dir/root.squashfs"
|
|
|
|
modules_squashfs="$rootfs_dir/modules.squashfs"
|
2023-12-21 01:48:32 -05:00
|
|
|
kernel_dir=/tmp/shim_kernel
|
2023-12-21 19:58:08 -05:00
|
|
|
unionfs_dir="/tmp/unionfs-fuse"
|
2023-12-21 01:48:32 -05:00
|
|
|
|
|
|
|
echo "compiling unionfs-fuse"
|
2023-12-21 19:58:08 -05:00
|
|
|
compile_unionfs $unionfs_dir/unionfs $unionfs_dir
|
2023-12-21 01:48:32 -05:00
|
|
|
|
2023-12-21 19:58:08 -05:00
|
|
|
echo "creating loop device for shim"
|
2023-12-21 01:48:32 -05:00
|
|
|
shim_loop=$(create_loop "${shim_path}")
|
2023-12-21 19:58:08 -05:00
|
|
|
kernel_loop="${shim_loop}p2" #KERN-A should always be p2
|
2023-12-21 01:48:32 -05:00
|
|
|
|
|
|
|
echo "copying shim kernel"
|
2023-12-21 19:58:08 -05:00
|
|
|
rm -rf $kernel_dir
|
2023-12-21 01:48:32 -05:00
|
|
|
mkdir $kernel_dir -p
|
2023-12-21 19:58:08 -05:00
|
|
|
dd if=$kernel_loop of=$kernel_dir/kernel.bin bs=1M status=progress
|
2023-12-21 01:48:32 -05:00
|
|
|
|
2023-12-22 01:51:47 -05:00
|
|
|
echo "extracting initramfs from kernel (this may take a while)"
|
2023-12-21 01:48:32 -05:00
|
|
|
extract_initramfs $kernel_dir/kernel.bin $kernel_dir $rootfs_dir
|
2023-12-21 19:58:08 -05:00
|
|
|
rm -rf $rootfs_dir/init
|
|
|
|
|
|
|
|
echo "mounting shim"
|
|
|
|
make_mountable "${shim_loop}p3"
|
|
|
|
safe_mount "${shim_loop}p3" $shim_rootfs
|
|
|
|
|
|
|
|
echo "compressing old rootfs"
|
|
|
|
mksquashfs $old_dir $root_squashfs -noappend -comp gzip
|
2023-12-21 01:48:32 -05:00
|
|
|
|
2023-12-21 19:58:08 -05:00
|
|
|
echo "patching new rootfs"
|
|
|
|
mv $unionfs_dir/unionfs $rootfs_dir/bin/unionfs
|
2023-12-22 01:51:47 -05:00
|
|
|
cp -ar squashfs/* $rootfs_dir/
|
|
|
|
chmod +x $rootfs_dir/bin/*
|
2023-12-21 01:48:32 -05:00
|
|
|
|
2023-12-22 01:51:47 -05:00
|
|
|
echo "done"
|