Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ading2210
GitHub Repository: ading2210/shimboot
Path: blob/main/build_squashfs.sh
402 views
1
#!/bin/bash
2
3
#build a rootfs that uses a squashfs + unionfs
4
#consists of a minimal busybox system containing:
5
# - FUSE kernel modules from the shim
6
# - unionfs-fuse statically compiled
7
# - the main squashfs, compressed with gzip
8
9
set -e
10
if [ "$DEBUG" ]; then
11
set -x
12
fi
13
14
. ./common.sh
15
. ./image_utils.sh
16
. ./shim_utils.sh
17
18
print_help() {
19
echo "Usage: ./build_squashfs.sh rootfs_dir uncompressed_rootfs_dir path_to_shim"
20
}
21
22
assert_root
23
assert_deps "git make gcc binwalk pcregrep"
24
assert_args "$3"
25
26
compile_unionfs() {
27
local out_path="$1"
28
local working_path="$2"
29
30
local repo_url="https://github.com/rpodgorny/unionfs-fuse"
31
local original_dir="$(pwd)"
32
local core_count="$(nproc --all)"
33
34
rm -rf $working_path
35
git clone $repo_url -b master --depth=1 $working_path
36
cd $working_path
37
38
env LDFLAGS="-static" CFLAGS="-O3" make -j$core_count
39
local binary_path="$working_path/src/unionfs"
40
cp $binary_path $out_path
41
cd $original_dir
42
}
43
44
rootfs_dir=$(realpath -m $1)
45
old_dir=$(realpath -m $2)
46
shim_path=$(realpath -m $3)
47
48
shim_rootfs="/tmp/shim_rootfs"
49
root_squashfs="$rootfs_dir/root.squashfs"
50
modules_squashfs="$rootfs_dir/modules.squashfs"
51
unionfs_dir="/tmp/unionfs-fuse"
52
53
print_info "compiling unionfs-fuse"
54
compile_unionfs $unionfs_dir/unionfs $unionfs_dir
55
56
print_info "reading the shim image"
57
extract_initramfs_full $shim_path $rootfs_dir
58
rm -rf $rootfs_dir/init
59
60
print_info "compressing old rootfs"
61
mksquashfs $old_dir $root_squashfs -noappend -comp gzip
62
63
print_info "patching new rootfs"
64
mv $unionfs_dir/unionfs $rootfs_dir/bin/unionfs
65
cp -ar squashfs/* $rootfs_dir/
66
chmod +x $rootfs_dir/bin/*
67
68
print_info "done"
69