mtsc/matter-iso/main.sh

226 lines
5.1 KiB
Bash
Raw Normal View History

#!/bin/bash
2024-08-11 14:45:54 +00:00
# matter-iso | MatterLinux ISO build script
2024-05-01 20:56:27 +00:00
# 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 <https://www.gnu.org/licenses/>.
2024-08-11 14:45:54 +00:00
#############################
## import common functions ##
#############################
location="$(dirname "${0}")"
location="$(realpath "${location}")"
commonsh="$(echo "${location}" | sed 's/\/bin/\/lib/g')/mtsc-common.sh"
2024-08-11 14:45:54 +00:00
source "${commonsh}"
2024-08-11 14:45:54 +00:00
if [ "${?}" != "0" ]; then
echo "Failed to import mtsc-common"
exit 1
2024-08-11 14:45:54 +00:00
fi
####################
## util functions ##
####################
check_retc() {
if [ $? -ne 0 ]; then
2024-08-11 14:45:54 +00:00
error "${1}"
clean_tmpdir
exit 1
fi
}
clean_tmpdir(){
2024-08-11 14:45:54 +00:00
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
2024-08-11 14:45:54 +00:00
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
2024-08-18 22:10:22 +00:00
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"
2024-08-11 14:45:54 +00:00
exit 1
fi
if [ $# -ne 2 ]; then
error "Please specify a release archive and a config directory"
2024-08-11 14:45:54 +00:00
exit 1
fi
2024-08-12 00:07:34 +00:00
2024-08-11 14:45:54 +00:00
archive="$(realpath "${1}")"
2024-08-12 00:07:34 +00:00
if [ ! -f "${archive}" ]; then
error "Archive file not found"
2024-08-11 14:45:54 +00:00
exit 1
fi
2024-08-11 14:45:54 +00:00
if [[ "$(file "${archive}")" != *"gzip compressed data"* ]]; then
error "Bad archive format"
2024-08-11 14:45:54 +00:00
exit 1
fi
2024-08-11 14:45:54 +00:00
confdir="$(realpath "${2}")"
if [ ! -d "${confdir}" ]; then
error "Config directory not found"
2024-08-11 14:45:54 +00:00
exit 1
fi
2024-08-11 14:45:54 +00:00
tmpdir="${confdir}_tmp"
distdir="${confdir}/dist"
rootdir="${distdir}/root"
isoh="${tmpdir}/iso.sh"
2024-08-11 14:45:54 +00:00
mkdir -p "${rootdir}"
success "Created root and dist directory"
info "Copying over the config directory"
2024-08-11 14:45:54 +00:00
cp -r "${confdir}" "${tmpdir}"
check_ret "Copy failed"
2024-08-11 14:45:54 +00:00
if [ ! -f "${isoh}" ]; then
error "ISO script not found"
2024-08-11 14:45:54 +00:00
exit 1
fi
2024-08-11 14:45:54 +00:00
source "${isoh}"
check_retc "Cannot source the ISO script"
2024-08-11 14:45:54 +00:00
check_iso_vars
check_retc "ISO script is not valid"
success "Sourced the ISO script"
info "Removing excluded files"
2024-08-11 14:45:54 +00:00
rm -rf "${tmpdir}/dist"
set_indent
for e in "${EXCLUDE[@]}"; do
2024-08-11 14:45:54 +00:00
info "Removing ${e}"
rm -rf "${tmpdir}/${e}"
done
2024-08-11 14:45:54 +00:00
rm -rf "${tmpdir}/.git"
unset_indent
info "Extracting release archive"
SECONDS=0
2024-08-11 14:45:54 +00:00
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"
2024-08-12 00:07:34 +00:00
matter-chroot "${tmpdir}" gpg --receive-keys $keys_str
check_retc "Failed to add public keys"
if [ ! -z "${PKGS1}" ]; then
info "Installing extra packages (1)"
2024-08-11 14:45:54 +00:00
matter-chroot "${tmpdir}" matt sync
check_retc "Sync failed"
2024-08-12 00:07:34 +00:00
matter-chroot "${tmpdir}" matt install --yes $pkgs1_str
check_retc "Install failed"
fi
info "Running build script"
2024-08-18 22:10:22 +00:00
echo "source /iso.sh && BUILD" > "${tmpdir}/stager.sh"
2024-08-12 00:07:34 +00:00
matter-chroot "${tmpdir}" chmod +x /stager.sh
matter-chroot "${tmpdir}" /stager.sh
check_ret "Build script failed"
2024-08-11 14:45:54 +00:00
rm "${tmpdir}/stager.sh"
info "Cleaning up and building initrd"
2024-08-11 14:45:54 +00:00
rm "${isoh}"
pushd "${tmpdir}" > /dev/null
2024-08-11 14:45:54 +00:00
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)"
2024-08-12 00:07:34 +00:00
matter-chroot "${tmpdir}" matt install --yes $pkgs2_str
check_ret "Install failed"
fi
info "Copying over the bootdir"
2024-08-11 14:45:54 +00:00
cp -r "${tmpdir}/boot"/* "${rootdir}/boot"
info "Building the ISO..."
2024-08-11 14:45:54 +00:00
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