#!/bin/bash # matter-iso | MatterLinux ISO build script # MatterLinux 2023-2024 (https://matterlinux.xyz) # 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 . ############################# ## import common functions ## ############################# location="$(dirname "${0}")" location="$(realpath "${location}")" commonsh="$(echo "${location}" | sed 's/\/bin/\/lib/g')/mtsc-common.sh" source "${commonsh}" if [ "${?}" != "0" ]; then echo "Failed to import mtsc-common" exit 1 fi #################### ## util functions ## #################### check_retc() { if [ $? -ne 0 ]; then error "${1}" clean_tmpdir exit 1 fi } clean_tmpdir(){ if [ -d "${tmpdir}" ]; then unset_indent info "Cleaning up temp directory" set_indent for d in "${tmpdir}/"*; do if mountpoint -q -- "${d}"; then error "Failed: ${d} is still mounted" unset_indent return 1 fi done rm -rf "${tmpdir}" success "Completed" fi unset_indent return 0 } 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 ! type "grub-mkrescue" > /dev/null; then error "You need to install GRUB to create ISO files with grub-mkrescue" set_indent warn "If you want to create UEFI ISO files make sure you install x64 GRUB or grub-efi" warn "If you want to create BIOS ISO files make sure you install x86 GRUB or grub-bios" warn "After installing GRUB, don't forget to install libisoburn as well" exit 1 fi if [ "$EUID" -ne 0 ]; then error "You should run this script as root" exit 1 fi if [ $# -ne 2 ]; then error "Please specify a release archive and a config directory" exit 1 fi archive="$(realpath "${1}")" if [ ! -f "${archive}" ]; then error "Archive file not found" exit 1 fi if [[ "$(file "${archive}")" != *"gzip compressed data"* ]]; then error "Bad archive format" exit 1 fi confdir="$(realpath "${2}")" if [ ! -d "${confdir}" ]; then error "Config directory not found" exit 1 fi tmpdir="${confdir}_tmp" distdir="${confdir}/dist" rootdir="${distdir}/root" isoh="${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 "${isoh}" ]; then error "ISO script not found" exit 1 fi source "${isoh}" 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 rm -rf "${tmpdir}/.git" 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}" matt sync check_retc "Sync failed" matter-chroot "${tmpdir}" matt install --yes $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 "${isoh}" 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}" matt install --yes $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