244 lines
9 KiB
Bash
Executable file
244 lines
9 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
# 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.
|
|
|
|
# Print manual if no parameters provided or invalid amount of parameters is provided
|
|
if [[ ! -n $1 || -n $2 ]]; then
|
|
cat <<- END
|
|
Usage: arkdep-build [variant]
|
|
Variants:
|
|
archlinux Plain Arch Linux TTY image
|
|
arkanelinux Arkane Linux GNOME image
|
|
|
|
Variants are loaded based on their directory names, the ones listed here
|
|
are included by default.
|
|
|
|
Variables:
|
|
ARKDEP_NO_TAR Do not create a compressed tarball, only create the images
|
|
ARKDEP_CUSTOM_NAME Define a custom image name
|
|
ARKDEP_OUTPUT_TARGET Overwrite location to which images will be written
|
|
ARKDEP_CONFIGS Define directory in which build will search for configuration files
|
|
END
|
|
exit 0
|
|
fi
|
|
|
|
## Common functions
|
|
#
|
|
# Cleanup and quit if error
|
|
cleanup_and_quit () {
|
|
|
|
# If any paramters are passed we will assume it to be an error
|
|
[[ -n $1 ]] && printf "\e[1;31m<#>\e[0m $*\e[0m\n" >&2
|
|
|
|
umount $workdir
|
|
|
|
btrfs property set -ts $workdir ro false
|
|
btrfs property set -ts $workdir/etc ro false
|
|
btrfs property set -ts $workdir/var ro false
|
|
|
|
# Remove temporary btrfs volumes
|
|
rm -rf $workdir/etc \
|
|
$workdir/var \
|
|
$workdir
|
|
|
|
# Quit program if argument provided to function
|
|
[[ -n $1 ]] && exit 1
|
|
|
|
# Otherwise just quit, there is no error
|
|
exit 0
|
|
|
|
}
|
|
|
|
## Set common variables
|
|
#
|
|
declare -r workdir='/var/tmp/rootfs'
|
|
[[ -v ARKDEP_CONFIGS ]] &&
|
|
declare -r configs_dir="$ARKDEP_CONFIGS" ||
|
|
declare -r configs_dir="$(readlink -m ./arkdep-build.d/)"
|
|
declare -r variant="$1"
|
|
declare -r type="$(cat $(readlink -m $configs_dir/$variant/type) 2> /dev/null || cleanup_and_quit 'Failed to get build type')"
|
|
[[ -v ARKDEP_OUTPUT_TARGET ]] &&
|
|
declare -r output_target="$ARKDEP_OUTPUT_TARGET" ||
|
|
declare -r output_target="$(pwd)/target/"
|
|
|
|
# Generate a 42 character long random string, used for generating psuedo-random
|
|
# image names
|
|
gen_random_string () {
|
|
|
|
if [[ -v ARKDEP_CUSTOM_NAME ]]; then
|
|
random=$ARKDEP_CUSTOM_NAME
|
|
else
|
|
random=$(openssl rand -hex 100 | head -c 42)
|
|
fi
|
|
|
|
printf "${random}\n"
|
|
|
|
}
|
|
|
|
## Error checking
|
|
#
|
|
# Quit if not root
|
|
[[ ! $EUID -eq 0 ]] &&
|
|
printf '\e[1;31m<#>\e[0m\e[1m This program has to be run as root\n\e[0m' &&
|
|
exit 1
|
|
|
|
# Check if all dependencies are installed, quit if not
|
|
for prog in btrfs pacstrap; do
|
|
if ! command -v $prog > /dev/null; then
|
|
printf "\e[1;31m<#>\e[0m\e[1m Failed to locate $prog, ensure it is installed\e[0m\n"
|
|
exit 1
|
|
fi
|
|
done
|
|
|
|
# Check if requested variant exists
|
|
[[ ! -d $configs_dir/$variant ]] &&
|
|
printf '\e[1;31m<#>\e[0m\e[1m The requested variant does not exist\e[0m\n' &&
|
|
exit 1
|
|
|
|
## Variants
|
|
#
|
|
# Build archlinux type image
|
|
if [[ $type == 'archlinux' ]]; then
|
|
|
|
# Ensure base.list exists, if not error and quit
|
|
if [[ ! -e $configs_dir/$variant/base.list ]]; then
|
|
printf "\e[1;31m<#>\e[0m\e[1m The required file 'base.list' is not preset in $(readlink -m $configs_dir/$variant)\e[0m\n" &&
|
|
exit 1
|
|
fi
|
|
|
|
printf '\e[1;34m-->\e[0m\e[1m Started Arch linux image build\e[0m\n'
|
|
|
|
# Create temporary Btrfs subvolume
|
|
printf "\e[1;34m-->\e[0m\e[1m Creating temporary Btrfs subvolumes at $(readlink -m $workdir)\e[0m\n"
|
|
btrfs subvolume create $workdir || cleanup_and_quit "Failed to create btrfs subvolume $(readlink -m $workdir)"
|
|
btrfs subvolume create $workdir/etc || cleanup_and_quit "Failed to create btrfs subvolume $(readlink -m $workdir/etc)"
|
|
btrfs subvolume create $workdir/var || cleanup_and_quit "Failed to create btrfs subvolume $(readlink -m $workdir/var)"
|
|
|
|
printf "\e[1;34m-->\e[0m\e[1m Creating bind mount at $(readlink -m $workdir)\e[0m\n"
|
|
mount --bind $workdir $workdir
|
|
|
|
# Read base package list and install base system
|
|
readarray base_packages < $configs_dir/$variant/base.list
|
|
printf '\e[1;34m-->\e[0m\e[1m Installing base packages\e[0m\n'
|
|
pacstrap $workdir ${base_packages[*]} || cleanup_and_quit 'Failed to install secondary package list'
|
|
|
|
# If overlay directory exists in variant copy it's contents to the temporary subvolume
|
|
if [[ -d $configs_dir/$variant/overlay ]]; then
|
|
printf '\e[1;34m-->\e[0m\e[1m Copying overlay to root\e[0m\n'
|
|
cp -rv $configs_dir/$variant/overlay/* $workdir/
|
|
fi
|
|
|
|
# Run post-bootstrap script if exists
|
|
if [[ -f $configs_dir/$variant/scripts/post-bootstrap.sh ]]; then
|
|
printf '\e[1;34m-->\e[0m\e[1m Running post-bootstrap script\e[0m\n'
|
|
source $configs_dir/$variant/scripts/post-bootstrap.sh
|
|
fi
|
|
|
|
# Read package list and install secondary system components, skip if not used
|
|
if [[ -e $configs_dir/$variant/package.list ]]; then
|
|
printf '\e[1;34m-->\e[0m\e[1m Installing secondary packages\e[0m\n'
|
|
readarray packages < $configs_dir/$variant/package.list
|
|
arch-chroot $workdir pacman -S --noconfirm ${packages[*]} || cleanup_and_quit 'Failed to install base packages'
|
|
fi
|
|
|
|
# Run post-install script if exists
|
|
if [[ -f $configs_dir/$variant/scripts/post-install.sh ]]; then
|
|
printf '\e[1;34m-->\e[0m\e[1m Running post-install script\e[0m\n'
|
|
source $configs_dir/$variant/scripts/post-install.sh
|
|
fi
|
|
|
|
# Clear pacman cache
|
|
printf '\e[1;34m-->\e[0m\e[1m Clearing pacman cache\e[0m\n'
|
|
arch-chroot $workdir pacman -Scc <<< Y <<< Y
|
|
# Insert a new line to clean, the previous command fails to do so
|
|
printf '\n'
|
|
|
|
# Remove subvolumes created by systemd
|
|
[[ -d $workdir/var/lib/portables ]] &&
|
|
printf '\e[1;34m-->\e[0m\e[1m Removing systemd subvolume var/lib/portables\e[0m\n'
|
|
btrfs subvolume delete $workdir/var/lib/portables
|
|
[[ -d $workdir/var/lib/machines ]] &&
|
|
printf '\e[1;34m-->\e[0m\e[1m Removing systemd subvolume var/lib/machines\e[0m\n'
|
|
btrfs subvolume delete $workdir/var/lib/machines
|
|
|
|
# Make /usr/local symlink in var
|
|
printf '\e[1;34m-->\e[0m\e[1m Moving dirs to var and creating symlinks\e[0m\n'
|
|
mv $workdir/usr/local $workdir/var/usrlocal || cleanup_and_quit 'Failed to move usr/local to var/usrlocal'
|
|
ln -sv ../var/usrlocal $workdir/usr/local || cleanup_and_quit 'Failed to create usrlocal symlink'
|
|
|
|
# locale symlink
|
|
mv $workdir/usr/lib/locale $workdir/var/usrliblocale || cleanup_and_quit 'Failed to move usr/local to var/usrlocal'
|
|
ln -sv ../../../var/usrliblocale $workdir/usr/lib/locale || cleanup_and_quit 'Failed to create usrlocal symlink'
|
|
|
|
# roothome symlink
|
|
mv $workdir/root $workdir/var/roothome || cleanup_and_quit 'Failed to move root to var/roothome'
|
|
ln -sv var/roothome $workdir/root || cleanup_and_quit 'Failed to create roothome symlink'
|
|
|
|
# Opt symlink
|
|
mv $workdir/opt $workdir/var/opt || cleanup_and_quit 'Failed to move opt to var/opt'
|
|
ln -sv var/opt $workdir/opt || cleanup_and_quit 'Failed to create opt symlink'
|
|
|
|
# srv symlink
|
|
mv $workdir/srv $workdir/var/srv || cleanup_and_quit 'Failed to move srv to var/srv'
|
|
ln -sv var/srv $workdir/srv || cleanup_and_quit 'Failed to create srv symlink'
|
|
|
|
# mnt symlink
|
|
mv $workdir/mnt $workdir/var/mnt || cleanup_and_quit 'Failed to move mnt to var/mnt'
|
|
ln -sv var/mnt $workdir/mnt || cleanup_and_quit 'Failed to create mnt symlink'
|
|
|
|
printf '\e[1;34m-->\e[0m\e[1m Moving passwd, shadow and group files to lib\e[0m\n'
|
|
|
|
# Create second passwd, group and shadow file in usr/lib and configure
|
|
for file in passwd group shadow; do
|
|
grep -v "^root:" $workdir/etc/$file > $workdir/usr/lib/$file
|
|
done
|
|
|
|
# Remove all users except for root, is typically overwritten by user overlay but
|
|
# may be used during os installation as a template
|
|
for file in passwd group shadow; do
|
|
grep "^root:" $workdir/etc/$file > $workdir/etc/$file-tmp
|
|
mv $workdir/etc/$file-tmp $workdir/etc/$file
|
|
done
|
|
|
|
# Ensure passwd/group/shadow permissions are set properly
|
|
chmod 600 $workdir/etc/shadow $workdir/usr/lib/shadow
|
|
chmod 644 $workdir/etc/{passwd,group} $workdir/usr/lib/{passwd,group}
|
|
|
|
#
|
|
# nss-switch.conf is added using the overlay
|
|
#
|
|
|
|
# Remove passwd/group/shadow backup files
|
|
rm $workdir/etc/{passwd-,shadow-,group-}
|
|
|
|
# Make subvolume read-only
|
|
printf '\e[1;34m-->\e[0m\e[1m Adding read-only property to subvolumes\e[0m\n'
|
|
btrfs property set -ts $workdir ro true || cleanup_and_quit 'Failed to set root to read-only'
|
|
btrfs property set -ts $workdir/etc ro true || cleanup_and_quit 'Failed to set etc to read-only'
|
|
btrfs property set -ts $workdir/var ro true || cleanup_and_quit 'Failed to set var to read-only'
|
|
|
|
# Generate random name for new image
|
|
printf '\e[1;34m-->\e[0m\e[1m Generating psuedo-random image name\e[0m\n'
|
|
declare -r image_name=$(gen_random_string)
|
|
printf "$image_name\n"
|
|
|
|
# Create dir for storing the images
|
|
mkdir -vp $(readlink -m $output_target/$image_name)
|
|
|
|
# Write subvolume to image
|
|
printf '\e[1;34m-->\e[0m\e[1m Creating images\e[0m\n'
|
|
btrfs send -f $output_target/$image_name/$image_name-rootfs.img $workdir
|
|
btrfs send -f $output_target/$image_name/$image_name-etc.img $workdir/etc
|
|
btrfs send -f $output_target/$image_name/$image_name-var.img $workdir/var
|
|
|
|
if [[ ! -v ARKDEP_NO_TAR ]]; then
|
|
printf '\e[1;34m-->\e[0m\e[1m Compressing images\e[0m\n'
|
|
tar -cv -I 'zstd -12 -T0 ' -f $output_target/$image_name.tar.zst -C $output_target/$image_name .
|
|
fi
|
|
|
|
cleanup_and_quit
|
|
|
|
fi
|