mtsc/matter-iso/main.sh

181 lines
4.1 KiB
Bash
Raw Normal View History

#!/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"
success() {
echo -e "$BOLD$GREEN>>>$RESET$BOLD $1$RESET"
}
info() {
echo -e "$BOLD$BLUE>>>$RESET$BOLD $1$RESET"
}
error() {
echo -e "$BOLD$RED>>>$RESET$BOLD $1$RESET"
exit 1
}
####################
## util functions ##
####################
check_ret() {
if [ $? -ne 0 ]; then
error "$1"
fi
}
setup_dns() {
info "Setting up DNS"
echo "nameserver 1.1.1.1" >> "$ROOTDIR/etc/resolv.conf"
}
clean_dns(){
rm "$ROOTDIR/etc/resolv.conf"
}
#################
## main script ##
#################
if [ $# -ne 2 ]; then
error "Please specify a release archive and an output directory"
fi
ARCHIVE="$(realpath $1)"
if [ ! -f $ARCHIVE ]; then
error "Archive file not found"
fi
PUBKEY="F9E70878C2FB389AEC2BA34CA3654DF5AD9F641D"
OUTDIR="$(realpath $2)"
ROOTDIR="$OUTDIR/root"
TMPDIR="$OUTDIR/tmp"
mkdir -p $TMPDIR
if [ -d $ROOTDIR ]; then
rm -rf $ROOTDIR
fi
mkdir -p $ROOTDIR
pushd $OUTDIR > /dev/null
cp $ARCHIVE release.tar.gz
check_ret "Failed to copy the archive!"
if [[ "$(file release.tar.gz)" != *"gzip compressed data"* ]]; then
error "Bad archive format"
fi
info "Extracting archive..."
tar xf release.tar.gz -C "$ROOTDIR"
check_ret "Extract failed!"
popd > /dev/nul
setup_dns
info "Adding public key"
matter-chroot "$ROOTDIR" gpg --receive-key $PUBKEY
info "Installing extra packages"
matter-chroot "$ROOTDIR" mp-sync
matter-chroot "$ROOTDIR" mp-install systemd dhcpcd
check_ret "Install failed!"
clean_dns
info "Configuring base system"
matter-chroot "$ROOTDIR" systemd-machine-id-setup
matter-chroot "$ROOTDIR" systemctl preset-all
matter-chroot "$ROOTDIR" cp /etc/skel/.* /root
matter-chroot "$ROOTDIR" cp /usr/sbin/init .
echo "matteriso" >> "$ROOTDIR/etc/hostname"
cat > "$ROOTDIR/etc/issue" << EOF
Welcome to MatterLinux ISO!
- Login with root:root
- Sync before installing packages: mp-sync
- For installation: https://matterlinux.xyz/wiki/install
- Join XMPP for questions: general@conf.matterlinux.xyz
Happy hacking!
EOF
info "Changing password"
cat > "$ROOTDIR/passwd.sh" << EOF
echo "root:root" | chpasswd
EOF
matter-chroot "$ROOTDIR" bash /passwd.sh
rm "$ROOTDIR/passwd.sh"
success "Setup completed, now creating the ISO"
pushd "$ROOTDIR" > /dev/null
info "Building initrd..."
find . | cpio --quiet -H newc -o | xz -T0 --check=crc32 > "$TMPDIR/initrd.img"
check_ret "Failed to build initrd"
popd > /dev/null
setup_dns
info "Installing and copying over the kernel"
matter-chroot "$ROOTDIR" mp-install linux
cp "$ROOTDIR/boot/vmlinuz-linux" "$TMPDIR"
clean_dns
info "Saving the grub configuration"
mkdir -p "$TMPDIR/boot/grub"
cat > "$TMPDIR/boot/grub/grub.cfg" << EOF
set default=0
set timeout=10
insmod efi_gop
insmod font
if loadfont /boot/grub/fonts/unicode.pf2
then
insmod gfxterm
set gfxmode-auto
set gfxpayload=keep
terminal_output gfxterm
fi
menuentry 'Matter GNU/Linux ISO' --class os {
insmod gzio
insmod part_msdos
linux /vmlinuz-linux
initrd /initrd.img
}
EOF
info "Building the ISO"
pushd "$OUTDIR" > /dev/null
grub-mkrescue -o "matter.iso" $TMPDIR
check_ret "grub-mkrescue failed!"
popd > /dev/null
success "ISO is ready for boot"
info "Cleaning up"
rm -rf $TMPDIR
rm "$OUTDIR/release.tar.gz"
success "Build completed"