Skip to content

Commit

Permalink
Fix with squashfuse-zig, other small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mgord9518 authored Jun 13, 2024
1 parent 62c6add commit 439a7b8
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions make_runtime.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
[ -z $ARCH ] && ARCH='x86_64-aarch64'
[ -z $COMP ] && COMP='lz4'
[ -z $img_type ] && img_type='squashfs'
[ -z $TMPDIR ] && TMPDIR='/tmp'

[ $STATIC_SQUASHFUSE ] && static_prefix='.static'

Expand All @@ -24,19 +25,23 @@ else
compress_flags="-9"
fi

for arch in 'x86_64' 'aarch64' 'i386' 'armhf'; do
for arch in 'x86' 'x86_64' 'arm' 'aarch64'; 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 [ $(grep "$arch" <<< "-$ARCH-") ]; then
file="squashfuse/squashfuse.$arch"
if [ ! -f "$file" ] && [ ! -f "$file.gz" ]; then
echo "Downloading $arch"

wget "$squashfuse_source/squashfuse-linux-$arch.tar.xz" -O - \
| tar -xJ -C "squashfuse/"

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

mv "squashfuse/squashfuse" "squashfuse/squashfuse.$arch"
fi

if [ $COMPRESS_SQUASHFUSE ]; then
Expand All @@ -49,54 +54,60 @@ for arch in 'x86_64' 'aarch64' 'i386' 'armhf'; do
fi
done

temp_runtime="$TMPDIR/shImg.temp.runtime"

# Collapse the script to make it smaller, not really sure whether I should keep
# it or not as it also obfuscates the code and the size difference makes little
# difference as the squashfuse binaries make up an overwhelming majority of the
# size of the runtime
echo '#!/bin/sh
#.shImg.#
#see <github.com/mgord9518/shappimage> for src' > runtime
#see <github.com/mgord9518/shappimage> for src' > "$temp_runtime"

cat runtime.sh | tr -d '\t' | sed 's/#.*//' | grep . >> runtime

cat runtime.sh | tr -d '\t' | sed 's/#.*//' | grep . >> "$temp_runtime"

arch=$(echo "$ARCH" | tr '-' ';')

# Honestly, I can't think of any reason NOT to compress the squashfuse binaries
# but leaving it as optional anyway
[ $COMPRESS_SQUASHFUSE ] && sed -i 's/head -c $length >/head -c $length | gzip -d >/' runtime
sed -i "s/=_IMAGE_COMPRESSION_/=$COMP/" runtime
sed -i "s/=_IMAGE_TYPE_/=$img_type/" runtime
sed -i "s/=_ARCH_/='$arch'/" runtime
[ $COMPRESS_SQUASHFUSE ] && sed -i 's/head -c $length >/head -c $length | gzip -d >/' "$temp_runtime"
sed -i "s/=_IMAGE_COMPRESSION_/=$COMP/" "$temp_runtime"
sed -i "s/=_IMAGE_TYPE_/=$img_type/" "$temp_runtime"
sed -i "s/=_ARCH_/='$arch'/" "$temp_runtime"


# Add one because this number is used by tail, which reads offsets off by 1
offset=$(($(cat runtime | wc -c) + 1))
offset=$(($(cat "$temp_runtime" | wc -c) + 1))
length=0

# 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))
length=$(printf "%07d" $(wc -c ${bin} | cut -d ' ' -f 1))

arch=$(cut -d'.' -f2 <<< "$bin")

sed -i "s/${arch}_offset=0000000/${arch}_offset=$offset/" runtime
sed -i "s/${arch}_length=0000000/${arch}_length=$length/" runtime
[ "$arch" = 'x86' ] && arch='i386'
[ "$arch" = 'arm' ] && arch='armhf'

sed -i "s/${arch}_offset=0000000/${arch}_offset=$offset/" "$temp_runtime"
sed -i "s/${arch}_length=0000000/${arch}_length=$length/" "$temp_runtime"
done

runtime_size=$(cat runtime $bin_list | wc -c | tr -dc '0-9')
runtime_size=$(cat "$temp_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
sed -i "s/=_IMAGE_OFFSET_/=$image_offset/" "$temp_runtime"

cat runtime $bin_list > runtime2
cat "$temp_runtime" $bin_list > "$temp_runtime.2"

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

rm runtime
rm "$temp_runtime"

0 comments on commit 439a7b8

Please sign in to comment.