2024-01-14 20:52:21 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# matter-iso | MatterLinux ISO Build Script
|
|
|
|
# Copyright (C) 2023 Matterlinux
|
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
############################
|
|
|
|
## logging functions/vars ##
|
|
|
|
############################
|
|
|
|
BOLD="\e[1m"
|
|
|
|
RESET="\e[0m"
|
|
|
|
GREEN="\e[32m"
|
|
|
|
BLUE="\e[34m"
|
|
|
|
GRAY="\e[37m"
|
|
|
|
RED="\e[31m"
|
2024-01-16 21:05:00 +00:00
|
|
|
LOG_PREFIX=">>>"
|
|
|
|
|
|
|
|
echo_color() {
|
|
|
|
echo -e "${1}"
|
|
|
|
}
|
2024-01-14 20:52:21 +00:00
|
|
|
|
|
|
|
success() {
|
2024-01-16 21:05:00 +00:00
|
|
|
echo_color "${BOLD}${GREEN}${LOG_PREFIX}${RESET}${BOLD} $1 ${RESET}"
|
2024-01-14 20:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info() {
|
2024-01-16 21:05:00 +00:00
|
|
|
echo_color "${BOLD}${BLUE}${LOG_PREFIX}${RESET}${BOLD} $1 ${RESET}"
|
2024-01-14 20:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
error() {
|
2024-01-16 21:05:00 +00:00
|
|
|
echo_color "${BOLD}${RED}${LOG_PREFIX}${RESET}${BOLD} $1 ${RESET}"
|
2024-01-14 20:52:21 +00:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:05:00 +00:00
|
|
|
errorne() {
|
|
|
|
echo_color "${BOLD}${RED}${LOG_PREFIX}${RESET}${BOLD} $1 ${RESET}"
|
|
|
|
}
|
|
|
|
|
|
|
|
print() {
|
|
|
|
echo_color "${BOLD}${GRAY}$1${RESET}"
|
|
|
|
}
|
|
|
|
|
|
|
|
set_indent() {
|
|
|
|
LOG_PREFIX="|>"
|
|
|
|
}
|
|
|
|
|
|
|
|
unset_indent() {
|
|
|
|
LOG_PREFIX=">>>"
|
|
|
|
}
|
|
|
|
|
2024-01-14 20:52:21 +00:00
|
|
|
####################
|
|
|
|
## util functions ##
|
|
|
|
####################
|
|
|
|
check_ret() {
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
error "$1"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:05:00 +00:00
|
|
|
check_retc() {
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
errorne "$1"
|
|
|
|
clean_tmpdir
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-01-14 20:52:21 +00:00
|
|
|
setup_dns() {
|
|
|
|
info "Setting up DNS"
|
2024-01-16 21:05:00 +00:00
|
|
|
echo "nameserver 1.1.1.1" >> "$TMPDIR/etc/resolv.conf"
|
2024-01-14 20:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
clean_dns(){
|
2024-01-16 21:05:00 +00:00
|
|
|
rm "$TMPDIR/etc/resolv.conf"
|
|
|
|
}
|
|
|
|
|
|
|
|
clean_tmpdir(){
|
|
|
|
if [ -d $TMPDIR ]; then
|
|
|
|
info "Cleaning up temp directory"
|
|
|
|
rm -rf $TMPDIR
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
check_iso_vars() {
|
|
|
|
if [ ! -n "$NAME" ]; then
|
|
|
|
return 1
|
|
|
|
elif [ ! -n "$VERSION" ]; then
|
|
|
|
return 1
|
|
|
|
elif ! type build &>/dev/null; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 0
|
2024-01-14 20:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#################
|
|
|
|
## main script ##
|
|
|
|
#################
|
|
|
|
if [ $# -ne 2 ]; then
|
2024-01-16 21:05:00 +00:00
|
|
|
error "Please specify a release archive and a config directory"
|
2024-01-14 20:52:21 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
ARCHIVE="$(realpath $1)"
|
|
|
|
if [ ! -f $ARCHIVE ]; then
|
|
|
|
error "Archive file not found"
|
|
|
|
fi
|
|
|
|
|
2024-01-16 21:05:00 +00:00
|
|
|
if [[ "$(file $ARCHIVE)" != *"gzip compressed data"* ]]; then
|
|
|
|
error "Bad archive format"
|
|
|
|
fi
|
2024-01-14 20:52:21 +00:00
|
|
|
|
2024-01-16 21:05:00 +00:00
|
|
|
CONFDIR="$(realpath $2)"
|
|
|
|
if [ ! -d $CONFDIR ]; then
|
|
|
|
error "Config directory not found"
|
2024-01-14 20:52:21 +00:00
|
|
|
fi
|
2024-01-16 21:05:00 +00:00
|
|
|
|
|
|
|
TMPDIR="${CONFDIR}_tmp"
|
|
|
|
DISTDIR="${CONFDIR}/dist"
|
|
|
|
ROOTDIR="${DISTDIR}/root"
|
|
|
|
ISOSH="${TMPDIR}/iso.sh"
|
|
|
|
|
2024-01-14 20:52:21 +00:00
|
|
|
mkdir -p $ROOTDIR
|
2024-01-16 21:05:00 +00:00
|
|
|
success "Created root and dist directory"
|
2024-01-14 20:52:21 +00:00
|
|
|
|
2024-01-16 21:05:00 +00:00
|
|
|
info "Copying over the config directory"
|
|
|
|
cp -r $CONFDIR $TMPDIR
|
|
|
|
check_ret "Copy failed"
|
2024-01-14 20:52:21 +00:00
|
|
|
|
2024-01-16 21:05:00 +00:00
|
|
|
if [ ! -f $ISOSH ]; then
|
|
|
|
error "ISO script not found"
|
|
|
|
fi
|
|
|
|
|
|
|
|
source $ISOSH
|
|
|
|
check_retc "Cannot source the ISO script"
|
|
|
|
check_iso_vars
|
|
|
|
check_retc "ISO script is not valid"
|
|
|
|
success "Sourced the ISO script"
|
|
|
|
|
|
|
|
info "Removing excluded files"
|
|
|
|
rm -rf "$TMPDIR/dist"
|
|
|
|
set_indent
|
|
|
|
for e in "${EXCLUDE[@]}"; do
|
|
|
|
info "Removing $e"
|
|
|
|
rm -rf "$TMPDIR/$e"
|
|
|
|
done
|
|
|
|
unset_indent
|
|
|
|
|
|
|
|
info "Extracting release archive"
|
|
|
|
SECONDS=0
|
|
|
|
tar --skip-old-files -xf "$ARCHIVE" -C "$TMPDIR"
|
|
|
|
check_retc "Extract failed"
|
|
|
|
success "Extracted in ${SECONDS}s"
|
|
|
|
|
|
|
|
for k in "${KEYS[@]}"; do
|
|
|
|
if [ -z ${keys_str+x} ]; then
|
|
|
|
keys_str="$k"
|
|
|
|
else
|
|
|
|
keys_str="$keys_str $k"
|
2024-01-14 20:52:21 +00:00
|
|
|
fi
|
2024-01-16 21:05:00 +00:00
|
|
|
done
|
2024-01-14 20:52:21 +00:00
|
|
|
|
2024-01-16 21:05:00 +00:00
|
|
|
for p1 in "${PKGS1[@]}"; do
|
|
|
|
if [ -z ${pkgs1_str+x} ]; then
|
|
|
|
pkgs1_str="$p1"
|
|
|
|
else
|
|
|
|
pkgs1_str="$pkgs1_str $p1"
|
|
|
|
fi
|
|
|
|
done
|
2024-01-14 20:52:21 +00:00
|
|
|
|
2024-01-16 21:05:00 +00:00
|
|
|
for p2 in "${PKGS2[@]}"; do
|
|
|
|
if [ -z ${pkgs2_str+x} ]; then
|
|
|
|
pkgs2_str="$p2"
|
|
|
|
else
|
|
|
|
pkgs2_str="$pkgs2_str $p2"
|
|
|
|
fi
|
|
|
|
done
|
2024-01-14 20:52:21 +00:00
|
|
|
|
2024-01-16 21:05:00 +00:00
|
|
|
setup_dns
|
|
|
|
info "Adding public keys"
|
|
|
|
matter-chroot "$TMPDIR" gpg --receive-keys $keys_str
|
|
|
|
check_retc "Failed to add public keys"
|
|
|
|
|
|
|
|
if [ ! -z "${PKGS1}" ]; then
|
|
|
|
info "Installing extra packages (1)"
|
|
|
|
matter-chroot "$TMPDIR" mp-sync
|
|
|
|
check_retc "Sync failed"
|
|
|
|
|
|
|
|
matter-chroot "$TMPDIR" MP_YES=1 mp-install $pkgs1_str
|
|
|
|
check_retc "Install failed"
|
|
|
|
fi
|
2024-01-14 20:52:21 +00:00
|
|
|
clean_dns
|
|
|
|
|
2024-01-16 21:05:00 +00:00
|
|
|
info "Running build script"
|
|
|
|
echo "source /iso.sh && build" > "$TMPDIR/stager.sh"
|
|
|
|
matter-chroot "$TMPDIR" chmod +x /stager.sh
|
|
|
|
matter-chroot "$TMPDIR" /stager.sh
|
|
|
|
check_ret "Build script failed"
|
|
|
|
rm "$TMPDIR/stager.sh"
|
|
|
|
|
|
|
|
info "Cleaning up and building initrd"
|
|
|
|
rm "$ISOSH"
|
|
|
|
pushd "$TMPDIR" > /dev/null
|
|
|
|
mkdir -p "$ROOTDIR/boot"
|
|
|
|
find . | cpio --quiet -H newc -o | xz -T0 --check=crc32 > "$ROOTDIR/boot/initrd.img"
|
2024-01-14 20:52:21 +00:00
|
|
|
check_ret "Failed to build initrd"
|
|
|
|
popd > /dev/null
|
|
|
|
|
2024-01-16 21:05:00 +00:00
|
|
|
if [ ! -z "${PKGS2}" ]; then
|
|
|
|
info "Installing extra packages (2)"
|
|
|
|
setup_dns
|
|
|
|
matter-chroot "$TMPDIR" MP_YES=1 mp-install $pkgs2_str
|
|
|
|
check_ret "Install failed"
|
|
|
|
clean_dns
|
2024-01-14 20:52:21 +00:00
|
|
|
fi
|
|
|
|
|
2024-01-16 21:05:00 +00:00
|
|
|
info "Copying over the bootdir"
|
|
|
|
cp -r "$TMPDIR/boot"/* "$ROOTDIR/boot"
|
2024-01-14 20:52:21 +00:00
|
|
|
|
2024-01-16 21:05:00 +00:00
|
|
|
info "Building the ISO..."
|
|
|
|
pushd "$DISTDIR" > /dev/null
|
|
|
|
grub-mkrescue -o "$DISTDIR/${NAME}_${VERSION}.iso" root
|
|
|
|
check_retc "grub-mkrescue failed"
|
|
|
|
popd > /dev/null
|
2024-01-14 20:52:21 +00:00
|
|
|
|
2024-01-16 21:05:00 +00:00
|
|
|
success "ISO is ready, cleaning up"
|
|
|
|
clean_tmpdir
|