2023-10-02 01:47:30 -04:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
#build the bootloader image
|
|
|
|
|
|
|
|
set -e
|
|
|
|
if [ "$DEBUG" ]; then
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
|
2023-12-21 01:09:41 -05:00
|
|
|
. ./image_utils.sh
|
2023-12-21 01:05:17 -05:00
|
|
|
. ./shim_utils.sh
|
2023-10-02 15:17:59 -04:00
|
|
|
|
2023-10-02 01:47:30 -04:00
|
|
|
print_help() {
|
2023-10-04 02:56:57 -04:00
|
|
|
echo "Usage: ./build.sh output_path shim_path rootfs_dir"
|
2023-10-02 01:47:30 -04:00
|
|
|
}
|
|
|
|
|
2023-10-02 15:06:06 -04:00
|
|
|
check_deps() {
|
2023-10-20 16:51:56 -04:00
|
|
|
local needed_commands="cpio binwalk pcregrep realpath cgpt mkfs.ext4 mkfs.ext2 fdisk rsync"
|
2023-10-02 15:06:06 -04:00
|
|
|
for command in $needed_commands; do
|
|
|
|
if ! command -v $command &> /dev/null; then
|
|
|
|
echo $command
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2023-10-02 01:47:30 -04:00
|
|
|
if [ "$EUID" -ne 0 ]; then
|
|
|
|
echo "this needs to be run as root."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-10-07 00:28:40 -04:00
|
|
|
if [ -z "$3" ]; then
|
2023-10-02 01:47:30 -04:00
|
|
|
print_help
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-10-02 15:06:06 -04:00
|
|
|
missing_commands=$(check_deps)
|
|
|
|
if [ "${missing_commands}" ]; then
|
|
|
|
echo "You are missing dependencies needed for this script."
|
|
|
|
echo "Commands needed:"
|
|
|
|
echo "${missing_commands}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-10-02 18:57:49 -04:00
|
|
|
output_path=$(realpath "${1}")
|
|
|
|
shim_path=$(realpath "${2}")
|
2023-10-04 02:56:57 -04:00
|
|
|
rootfs_dir=$(realpath "${3}")
|
2023-10-02 01:47:30 -04:00
|
|
|
|
|
|
|
echo "created loop device for shim"
|
2023-10-02 15:06:06 -04:00
|
|
|
shim_loop=$(create_loop "${shim_path}")
|
2023-10-02 01:47:30 -04:00
|
|
|
kernel_loop="${shim_loop}p2" #KERN-A should always be p2
|
|
|
|
|
|
|
|
echo "copying shim kernel to new file in /tmp"
|
|
|
|
kernel_dir=/tmp/shim_kernel
|
|
|
|
mkdir $kernel_dir -p
|
|
|
|
dd if=$kernel_loop of=$kernel_dir/kernel.bin bs=1M status=none
|
|
|
|
|
|
|
|
echo "extracting data from kernel"
|
|
|
|
initramfs_dir=/tmp/shim_initramfs
|
|
|
|
rm -rf $initramfs_dir
|
2023-12-21 01:05:17 -05:00
|
|
|
extract_initramfs $kernel_dir/kernel.bin $kernel_dir $initramfs_dir
|
2023-10-02 01:47:30 -04:00
|
|
|
|
2023-10-02 02:08:31 -04:00
|
|
|
echo "patching initramfs"
|
2023-10-02 15:17:59 -04:00
|
|
|
patch_initramfs $initramfs_dir
|
2023-10-02 02:08:31 -04:00
|
|
|
|
2023-10-02 18:57:49 -04:00
|
|
|
echo "creating disk image"
|
2023-10-06 13:29:10 -04:00
|
|
|
rootfs_size=$(du -sm $rootfs_dir | cut -f 1)
|
2023-10-20 16:51:56 -04:00
|
|
|
rootfs_part_size=$(($rootfs_size * 11 / 10))
|
2023-10-06 13:29:10 -04:00
|
|
|
#create a 20mb bootloader partition
|
2023-10-20 16:51:56 -04:00
|
|
|
#rootfs partition is 20% larger than its contents
|
2023-10-06 13:29:10 -04:00
|
|
|
create_image $output_path 20 $rootfs_part_size
|
2023-10-02 18:57:49 -04:00
|
|
|
|
|
|
|
echo "creating loop device for the image"
|
|
|
|
image_loop=$(create_loop ${output_path})
|
|
|
|
|
|
|
|
echo "creating partitions on the disk image"
|
|
|
|
create_partitions $image_loop "${kernel_dir}/kernel.bin"
|
|
|
|
|
2023-10-07 00:28:40 -04:00
|
|
|
echo "mounting the original shim rootfs"
|
|
|
|
shim_rootfs="/tmp/shim_rootfs"
|
|
|
|
make_mountable "${shim_loop}p3"
|
|
|
|
safe_mount "${shim_loop}p3" $shim_rootfs
|
|
|
|
|
2023-10-02 18:57:49 -04:00
|
|
|
echo "copying data into the image"
|
2023-10-20 08:04:19 -04:00
|
|
|
populate_partitions $image_loop $initramfs_dir $rootfs_dir
|
2023-10-02 18:57:49 -04:00
|
|
|
|
2023-10-02 01:47:30 -04:00
|
|
|
echo "cleaning up loop devices"
|
2023-10-02 18:57:49 -04:00
|
|
|
losetup -d $shim_loop
|
2023-10-20 16:51:56 -04:00
|
|
|
losetup -d $image_loop
|
|
|
|
echo "done"
|