2023-06-21 18:36:52 +02:00
|
|
|
#!/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: bttrfs-build [variant]
|
|
|
|
Variants:
|
|
|
|
archlinux Plain Arch Linux TTY image
|
|
|
|
arkanelinux Arkane Linux GNOME image
|
|
|
|
debian Plain Debian TTY image
|
|
|
|
|
|
|
|
Variants are loaded based on their directory names, the ones listed here
|
|
|
|
are included by default. For information on the exact configuration of
|
|
|
|
these variants or to create your own see /etc/bttrfs/variants.
|
|
|
|
END
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
## Set common variables
|
|
|
|
#
|
2023-06-26 00:32:59 +02:00
|
|
|
declare -r workdir='/var/tmp/rootfs'
|
2023-06-21 18:36:52 +02:00
|
|
|
declare -r configs_dir='./bttrfs/'
|
|
|
|
declare -r variant="$configs_dir/variants/$1"
|
|
|
|
declare -r type=$(cat $variant/type)
|
2023-06-26 00:32:59 +02:00
|
|
|
declare -r output_target="$(pwd)/"
|
2023-06-21 18:36:52 +02:00
|
|
|
|
|
|
|
## Common functions
|
|
|
|
#
|
|
|
|
# Cleanup and quit if error
|
|
|
|
cleanup_and_quit () {
|
|
|
|
|
|
|
|
# If any paramters are passed we will assume it to be an error
|
2023-06-22 02:27:51 +02:00
|
|
|
[[ -n $1 ]] && printf "\e[1;31m<#>\e[0m $*\e[0m\n" >&2
|
2023-06-21 18:36:52 +02:00
|
|
|
|
2023-06-26 00:32:59 +02:00
|
|
|
btrfs property set -ts $workdir ro false
|
|
|
|
btrfs property set -ts $workdir/etc ro false
|
|
|
|
btrfs property set -ts $workdir/var ro false
|
|
|
|
|
2023-06-21 18:36:52 +02:00
|
|
|
# Remove temporary btrfs volumes
|
2023-06-26 00:32:59 +02:00
|
|
|
[[ ! -n $BTTRFS_NO_CLEANUP ]] && btrfs subvolume delete $workdir/etc \
|
|
|
|
$workdir/var \
|
|
|
|
$workdir 2> /dev/null
|
2023-06-21 18:36:52 +02:00
|
|
|
|
|
|
|
# Quit program if argument provided to function
|
|
|
|
[[ -n $1 ]] && exit 1
|
|
|
|
|
|
|
|
# Otherwise just quit, there is no error
|
|
|
|
exit 0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
## Error checking
|
|
|
|
#
|
|
|
|
# Quit if not root
|
|
|
|
[[ ! $EUID -eq 0 ]] &&
|
2023-06-22 02:27:51 +02:00
|
|
|
printf '\e[1;31m<#>\e[0m\e[1m This program has to be run as root\n\e[0m' &&
|
2023-06-21 18:36:52 +02:00
|
|
|
exit 1
|
|
|
|
|
|
|
|
# Check if all dependencies are installed, quit if not
|
|
|
|
for prog in btrfs pacstrap; do
|
|
|
|
if ! command -v $prog > /dev/null; then
|
2023-06-22 02:27:51 +02:00
|
|
|
printf "\e[1;31m<#>\e[0m\e[1m Failed to locate $prog, ensure it is installed\e[0m\n"
|
2023-06-21 18:36:52 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# Check if requested variant exists
|
|
|
|
[[ ! -d $variant ]] &&
|
2023-06-22 02:27:51 +02:00
|
|
|
printf '\e[1;31m<#>\e[0m\e[1m The requested variant does not exist\e[0m\n' &&
|
2023-06-21 18:36:52 +02:00
|
|
|
exit 1
|
|
|
|
|
2023-06-22 01:53:19 +02:00
|
|
|
## Variants
|
|
|
|
#
|
2023-06-21 18:36:52 +02:00
|
|
|
# Build archlinux type image
|
2023-06-25 20:27:47 +02:00
|
|
|
if [[ $type == 'archlinux' ]]; then
|
2023-06-21 18:36:52 +02:00
|
|
|
|
|
|
|
# Ensure base.list exists, if not error and quit
|
|
|
|
[[ ! -e $variant/base.list ]] &&
|
2023-06-22 02:27:51 +02:00
|
|
|
printf "\e[1;31m<#>\e[0m\e[1m The required file 'base.list' is not preset in $(readlink -m $variant)\e[0m\n" &&
|
2023-06-21 18:36:52 +02:00
|
|
|
exit 1
|
|
|
|
|
|
|
|
# Check if optional packages.list file exists, if not notify and continue
|
|
|
|
[[ ! -e $variant/packages.list ]] &&
|
2023-06-22 02:27:51 +02:00
|
|
|
printf "\e[1;34m-->\e[0m\e[1m The optional file 'packages.list' is not preset in $(readlink -m $variant), running without it\e[0m\n" &&
|
2023-06-21 18:36:52 +02:00
|
|
|
skip_secondary_package_installation=1
|
|
|
|
|
2023-06-22 02:27:51 +02:00
|
|
|
printf '\e[1;34m-->\e[0m\e[1m Started Arch linux image build\e[0m\n'
|
2023-06-21 18:36:52 +02:00
|
|
|
|
|
|
|
# Create temporary Btrfs subvolume
|
2023-06-26 00:32:59 +02:00
|
|
|
printf "\e[1;34m-->\e[0m\e[1m Creating temporary Btrfs subvolumes at $(readlink -m $workdir)\e[0m\n"
|
2023-06-22 02:33:11 +02:00
|
|
|
btrfs subvolume create $workdir || cleanup_and_quit "Failed to create btrfs subvolume $(readlink -m $workdir)"
|
2023-06-26 00:32:59 +02:00
|
|
|
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)"
|
2023-06-21 18:36:52 +02:00
|
|
|
|
|
|
|
# Read base package list and install base system
|
|
|
|
readarray base_packages < $variant/base.list
|
2023-06-22 02:27:51 +02:00
|
|
|
printf "\e[1;34m-->\e[0m\e[1m Installing base packages\e[0m\n"
|
2023-06-21 18:36:52 +02:00
|
|
|
pacstrap $workdir ${base_packages[*]} || cleanup_and_quit 'Failed to install secondary package list'
|
|
|
|
|
|
|
|
# Read package list and install secondary system components, skip if not used
|
|
|
|
if [[ ! -n skip_secondary_package_installation ]]; then
|
2023-06-22 02:27:51 +02:00
|
|
|
printf '\e[1;34m-->\e[0m\e[1m Installing secondary packages...\e[0m\n'
|
2023-06-21 18:36:52 +02:00
|
|
|
readarray packages < $variant/packages.list
|
|
|
|
arch-chroot $workdir pacman -S --noconfirm ${packages[*]} || cleanup_and_quit 'Failed to install base packages'
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If rootfs directory exists in variant copy it to the temporary subvolume
|
|
|
|
if [[ -d $variant/rootfs ]]; then
|
|
|
|
cp -rv $variant/rootfs/* $workdir/
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Remove subvolumes created by systemd
|
|
|
|
[[ -d $workdir/var/lib/portables ]] &&
|
2023-06-22 02:27:51 +02:00
|
|
|
printf "\e[1;34m-->\e[0m\e[1m Removing systemd subvolume var/lib/portables\e[0m\n" &&
|
2023-06-21 18:36:52 +02:00
|
|
|
btrfs subvolume delete $workdir/var/lib/portables
|
|
|
|
[[ -d $workdir/var/lib/machines ]] &&
|
2023-06-22 02:27:51 +02:00
|
|
|
printf "\e[1;34m-->\e[0m\e[1m Removing systemd subvolume var/lib/machines\e[0m\n" &&
|
2023-06-21 18:36:52 +02:00
|
|
|
btrfs subvolume delete $workdir/var/lib/machines
|
|
|
|
|
2023-06-25 17:53:30 +02:00
|
|
|
# Clear pacman cache
|
|
|
|
rm -rf $workdir/var/cache/pacman/pkg/*
|
|
|
|
rm -rf $workdir/var/lib/pacman/local/*
|
|
|
|
|
2023-06-21 18:36:52 +02:00
|
|
|
# Make subvolume read-only
|
2023-06-26 00:32:59 +02:00
|
|
|
printf "\e[1;34m-->\e[0m\e[1m Adding read-only property to subvolumes\e[0m\n"
|
2023-06-21 18:36:52 +02:00
|
|
|
btrfs property set -ts $workdir ro true
|
2023-06-26 00:32:59 +02:00
|
|
|
btrfs property set -ts $workdir/etc ro true
|
|
|
|
btrfs property set -ts $workdir/var ro true
|
2023-06-21 18:36:52 +02:00
|
|
|
|
|
|
|
# Write subvolume to image
|
2023-06-26 00:32:59 +02:00
|
|
|
printf "\e[1;34m-->\e[0m\e[1m Creating images...\e[0m\n"
|
|
|
|
btrfs send -f $output_target/rootfs.img $workdir
|
|
|
|
btrfs send -f $output_target/etc.img $workdir/etc
|
|
|
|
btrfs send -f $output_target/var.img $workdir/var
|
2023-06-21 18:36:52 +02:00
|
|
|
|
|
|
|
cleanup_and_quit
|
|
|
|
fi
|