#!/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 . ############################ ## logging functions/vars ## ############################ BOLD="\e[1m" RESET="\e[0m" GREEN="\e[32m" BLUE="\e[34m" GRAY="\e[37m" RED="\e[31m" LOG_PREFIX=">>>" echo_color() { echo -e "${1}" } success() { echo_color "${BOLD}${GREEN}${LOG_PREFIX}${RESET}${BOLD} $1 ${RESET}" } info() { echo_color "${BOLD}${BLUE}${LOG_PREFIX}${RESET}${BOLD} $1 ${RESET}" } error() { echo_color "${BOLD}${RED}${LOG_PREFIX}${RESET}${BOLD} $1 ${RESET}" exit 1 } 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=">>>" } #################### ## util functions ## #################### check_ret() { if [ $? -ne 0 ]; then error "$1" fi } check_retc() { if [ $? -ne 0 ]; then errorne "$1" clean_tmpdir exit 1 fi } 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 } ################# ## main script ## ################# if [ $# -ne 2 ]; then error "Please specify a release archive and a config directory" fi ARCHIVE="$(realpath $1)" if [ ! -f $ARCHIVE ]; then error "Archive file not found" fi if [[ "$(file $ARCHIVE)" != *"gzip compressed data"* ]]; then error "Bad archive format" fi CONFDIR="$(realpath $2)" if [ ! -d $CONFDIR ]; then error "Config directory not found" fi TMPDIR="${CONFDIR}_tmp" DISTDIR="${CONFDIR}/dist" ROOTDIR="${DISTDIR}/root" ISOSH="${TMPDIR}/iso.sh" mkdir -p $ROOTDIR success "Created root and dist directory" info "Copying over the config directory" cp -r $CONFDIR $TMPDIR check_ret "Copy failed" 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" fi done for p1 in "${PKGS1[@]}"; do if [ -z ${pkgs1_str+x} ]; then pkgs1_str="$p1" else pkgs1_str="$pkgs1_str $p1" fi done for p2 in "${PKGS2[@]}"; do if [ -z ${pkgs2_str+x} ]; then pkgs2_str="$p2" else pkgs2_str="$pkgs2_str $p2" fi done 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 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" check_ret "Failed to build initrd" popd > /dev/null if [ ! -z "${PKGS2}" ]; then info "Installing extra packages (2)" matter-chroot "$TMPDIR" MP_YES=1 mp-install $pkgs2_str check_ret "Install failed" fi info "Copying over the bootdir" cp -r "$TMPDIR/boot"/* "$ROOTDIR/boot" info "Building the ISO..." pushd "$DISTDIR" > /dev/null grub-mkrescue -o "$DISTDIR/${NAME}_${VERSION}.iso" root check_retc "grub-mkrescue failed" popd > /dev/null success "ISO is ready, cleaning up" clean_tmpdir