Current section
Files
Jump to
Current section
Files
scripts/merge-squashfs
#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2016 Frank Hunleth
# SPDX-FileCopyrightText: 2017 Justin Schneck
# SPDX-FileCopyrightText: 2017 rradu
# SPDX-FileCopyrightText: 2024 Ben Youngblood
# SPDX-FileCopyrightText: 2024 Rockwell Schrock
#
# SPDX-License-Identifier: Apache-2.0
#
# Merge an overlay directory into a squashfs
#
# Usage: merge-squashfs input.squashfs output.squashfs overlay-dir squashfs-priorities
#
set -e
LC_ALL=C
LANG=C
export LC_ALL LANG
# Cache the location of sed due to very slow path searches on OSX if anything
# under /home is in the path.
SED="$(command -v sed)"
# Account for Mac differences
if [[ "$(uname)" = "Darwin" ]]; then
# Use gstat on the Mac, but make sure that it exists first.
STAT=$(command -v gstat || echo "/usr/local/bin/gstat")
if [[ ! -e "$STAT" ]]; then
echo "merge-squashfs: ERROR: Please install gstat first"
echo
echo "For example:"
echo " brew install coreutils"
exit 1
fi
else
STAT="$(command -v stat)"
fi
# This implementation might be slow
perm_to_num() {
string=$1
let perms=0
[[ "${string}" = ?r???????? ]] && perms=$(( perms + 0400 ))
[[ "${string}" = ??w??????? ]] && perms=$(( perms + 0200 ))
[[ "${string}" = ???x?????? ]] && perms=$(( perms + 0100 ))
[[ "${string}" = ???s?????? ]] && perms=$(( perms + 04100 ))
[[ "${string}" = ???S?????? ]] && perms=$(( perms + 04000 ))
[[ "${string}" = ????r????? ]] && perms=$(( perms + 040 ))
[[ "${string}" = ?????w???? ]] && perms=$(( perms + 020 ))
[[ "${string}" = ??????x??? ]] && perms=$(( perms + 010 ))
[[ "${string}" = ??????s??? ]] && perms=$(( perms + 02010 ))
[[ "${string}" = ??????S??? ]] && perms=$(( perms + 02000 ))
[[ "${string}" = ???????r?? ]] && perms=$(( perms + 04 ))
[[ "${string}" = ????????w? ]] && perms=$(( perms + 02 ))
[[ "${string}" = ?????????x ]] && perms=$(( perms + 01 ))
[[ "${string}" = ?????????t ]] && perms=$(( perms + 01001 ))
[[ "${string}" = ?????????T ]] && perms=$(( perms + 01000 ))
printf '%o' $perms
}
owner_to_uid_gid() {
echo "$1" | "$SED" -e "s/\// /g"
}
get_path() {
# trim the squashfs-root and escape any spaces
echo "$*" | "$SED" -e 's/squashfs-root//' -e 's/ /\\ /g'
}
device_file() {
perm=$1
owner=$2
# Depending on the major and minor numbers, you either get
# major,minor or major, minor. Remove the comma and account
# for the optional space.
if [[ $3 =~ ,.+ ]]; then
major_minor=$(echo "$3" | "$SED" -e 's/,/ /')
filename=$6
else
major_minor="$(echo "$3" | "$SED" -e 's/,/ /') $4"
filename=$7
fi
echo "$(get_path "$filename") ${perm:0:1} $(perm_to_num "$perm") $(owner_to_uid_gid "$owner") $major_minor"
}
symlink_file() {
# symlinks are the handled the same as files currently.
echo "$(get_path "$6") m $(perm_to_num "$1") $(owner_to_uid_gid "$2")"
}
directory_file() {
perm="$1"
owner="$2"
shift 5
pathname="$(get_path "$@")"
# mksquashfs doesn't support setting permissions and ownership
# on the root directory
if [[ ! -z "$pathname" ]]; then
echo "$pathname m $(perm_to_num "$perm") $(owner_to_uid_gid "$owner")"
fi
}
regular_file() {
perm="$1"
owner="$2"
shift 5
pathname="$(get_path "$@")"
echo "$pathname m $(perm_to_num "$perm") $(owner_to_uid_gid "$owner")"
}
unsquash_to_pseudofile() {
input=$1
while read -r perms owner rest; do
case $perms in
c*)
device_file "$perms" "$owner" $rest
;;
b*)
device_file "$perms" "$owner" $rest
;;
l*)
symlink_file "$perms" "$owner" $rest
;;
d*)
directory_file "$perms" "$owner" $rest
;;
-*)
regular_file "$perms" "$owner" $rest
;;
*)
# Ignore
;;
esac
done < <(unsquashfs -n -ll "$input")
}
find_to_pseudofile() {
inputdir=$1
while read -r mode path; do
path=$(echo "$path" | "$SED" -e "s^$inputdir^^" -e "s/ /\\\\ /g")
if [[ ! -z $path ]]; then
echo "$path m $mode 0 0"
fi
done < <(find "$inputdir" -exec "$STAT" -c "%a %n" "{}" ";")
}
# "readlink -f" implementation for BSD
# This code was extracted from the Elixir shell scripts
readlink_f () {
cd "$(dirname "$1")" > /dev/null
filename="$(basename "$1")"
if [[ -h "$filename" ]]; then
readlink_f "$(readlink "$filename")"
else
echo "$(pwd -P)/$filename"
fi
}
if [[ $# -lt 4 ]]; then
echo "Usage: merge-squashfs input.squashfs output.squashfs overlay-dir squashfs_priorities"
exit 1
fi
input_squashfs=$(readlink_f "$1")
output_squashfs=$(readlink_f "$2")
overlay_dir=$(readlink_f "$3")
squashfs_priorities=$(readlink_f "$4")
if [[ ! -f $input_squashfs ]]; then
echo "Can't open $input_squashfs"
exit 1
fi
if [[ ! -d $overlay_dir ]]; then
echo "Can't find $overlay_dir"
exit 1
fi
# Use $TMPDIR if it's set
if [[ -e "$TMPDIR" ]]; then
workdir=$(mktemp -d "$TMPDIR/merge-squashfs-tmp.XXXXXXXXXX")
else
workdir=$(mktemp -d)
fi
pushd "$workdir" >/dev/null
unsquash_to_pseudofile "$input_squashfs" >pseudofile.in
find_to_pseudofile "$overlay_dir" >>pseudofile.in
# remove repeated entries to avoid warnings from mksquashfs
awk '!x[$1]++' pseudofile.in > pseudofile
unsquashfs -f "$input_squashfs" >/dev/null
cp -Rf "$overlay_dir/." "$workdir/squashfs-root"
mksquashfs squashfs-root "$output_squashfs" -pf pseudofile -sort "$squashfs_priorities" -noappend -no-recovery -no-progress $NERVES_MKSQUASHFS_FLAGS
# cleanup
popd >/dev/null
rm -fr "$workdir"