Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
runtime.sh:
 * Replace tabs with spaces
 * Now fetch squashfuse binaries from shImg header

make_runtime.sh
 * No longer throws a bunch of errors for unused archs
 * Reduce some duplicated code and make it more readable
  • Loading branch information
mgord9518 committed Jan 30, 2024
1 parent 3dbfb62 commit 7e49058
Show file tree
Hide file tree
Showing 2 changed files with 330 additions and 296 deletions.
54 changes: 25 additions & 29 deletions make_runtime.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# ARCH variable sets the arch, COMP sets the compression algo
# Default to all supported architectures and LZ4 compression if unset
# TODO: Allow multiple compression algorithms in building
[ -z $ARCH ] && ARCH='x86_64-aarch64-armhf'
[ -z $ARCH ] && ARCH='x86_64-aarch64'
[ -z $COMP ] && COMP='lz4'
[ -z $img_type ] && img_type='squashfs'

Expand All @@ -24,21 +24,27 @@ else
compress_flags="-9"
fi

for arch in 'x86_64' 'aarch64' 'x86' 'armhf'; do
for arch in 'x86_64' 'aarch64' 'i386' 'armhf'; do
# Download required squashfuse squashfusearies per architecture if they don't already
# exist
if [ $(grep "$arch" <<< "$ARCH") ]; then
if [ ! -f "squashfuse/squashfuse.$arch"* ]; then
wget "$squashfuse_source/squashfuse_${COMP}${static_prefix}.$arch" \
-O "squashfuse/squashfuse.$arch"

if [ $? -ne 0 ]; then
rm "squashfuse/squashfuse.$arch"
exit $?
fi

fi

if [ $COMPRESS_SQUASHFUSE ]; then
"$compress_command" $compress_flags "squashfuse/squashfuse.$arch"
rm "squashfuse/squashfuse.$arch"
binList="$binList squashfuse/squashfuse.$arch.gz"
bin_list="$bin_list squashfuse/squashfuse.$arch.gz"
else
binList="$binList squashfuse/squashfuse.$arch"
bin_list="$bin_list squashfuse/squashfuse.$arch"
fi
fi
done
Expand All @@ -62,45 +68,35 @@ sed -i "s/=_IMAGE_COMPRESSION_/=$COMP/" runtime
sed -i "s/=_IMAGE_TYPE_/=$img_type/" runtime
sed -i "s/=_ARCH_/='$arch'/" runtime

# Sizes of all files being packed into the runtime
_x64_l=$(printf "%07d" `wc -c squashfuse/squashfuse.x86_64* | cut -d ' ' -f 1`)
i386_l=$(printf "%07d" `wc -c squashfuse/squashfuse.i386* | cut -d ' ' -f 1`)
ar64_l=$(printf "%07d" `wc -c squashfuse/squashfuse.aarch64* | cut -d ' ' -f 1`)
ar32_l=$(printf "%07d" `wc -c squashfuse/squashfuse.armhf* | cut -d ' ' -f 1`)

# Offsets of squashfuse binaries by arch
# These are used when the runtime is executed to know where in the file to
# extract the appropriate binary
_x64_o=$(cat runtime | wc -c | tr -dc '0-9')
_x64_o=$(printf "%07d" $((10#$_x64_o + 1)))
i386_o=$(printf "%07d" $((10#$_x64_o + 10#$_x64_l)))
ar64_o=$(printf "%07d" $((10#$i386_o + 10#$i386_l)))
ar32_o=$(printf "%07d" $((10#$ar64_o + 10#$ar64_l)))

# Add in all the sizes and offsets
sed -i "s/=_x64_o_/=$_x64_o/" runtime
sed -i "s/=_x64_l_/=$_x64_l/" runtime
# Add one because this number is used by tail, which reads offsets off by 1
offset=$(($(cat runtime | wc -c) + 1))
length=0

sed -i "s/=i386_o_/=$i386_o/" runtime
sed -i "s/=i386_l_/=$i386_l/" runtime
# TODO: remove need for zero-padding
for bin in $bin_list; do
offset=$(printf "%07d" $((10#$offset + 10#$length)))
length=$(printf "%07d" $(wc -c ${bin}* | cut -d ' ' -f 1))

sed -i "s/=ar64_o_/=$ar64_o/" runtime
sed -i "s/=ar64_l_/=$ar64_l/" runtime
arch=$(cut -d'.' -f2 <<< "$bin")

sed -i "s/=ar32_o_/=$ar32_o/" runtime
sed -i "s/=ar32_l_/=$ar32_l/" runtime
sed -i "s/${arch}_offset=0000000/${arch}_offset=$offset/" runtime
sed -i "s/${arch}_length=0000000/${arch}_length=$length/" runtime
done

runtime_size=$(cat runtime $binList | wc -c | tr -dc '0-9')
runtime_size=$(cat runtime $bin_list | wc -c | tr -dc '0-9')

# Had to expand to 7 digits because of DwarFS's large size
image_offset=$(printf "%014d" "$runtime_size")
sed -i "s/=_IMAGE_OFFSET_/=$image_offset/" runtime

cat runtime $binList > runtime2
cat runtime $bin_list > runtime2

if [ ! $img_type = dwarfs ]; then
mv runtime2 "runtime-$COMP$STATIC-$ARCH"
else
mv runtime2 "runtime_dwarfs-static-$ARCH"
rm squashfuse/squashfuse.x86_64.gz
fi

rm runtime
Loading

0 comments on commit 7e49058

Please sign in to comment.