2023-07-05 01:44:37 +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.
2023-08-15 09:54:08 +02:00
set -o pipefail
2023-07-05 01:44:37 +02:00
# Print manual if no parameters provided or invalid amount of parameters is provided
2023-08-15 09:54:08 +02:00
if [[ ! -n $1 ]]; then
2023-07-05 01:44:37 +02:00
cat <<- END
2023-12-31 16:47:53 +01:00
Usage: arkdep <action> [variant] [version]
2023-07-05 01:44:37 +02:00
Actions:
2023-07-22 18:15:44 +02:00
deploy Deploy a new or update an existing deployment
2023-09-21 21:51:57 +02:00
init Initialize arkdep on a new system
teardown Remove all arkdep-deploy related files and folders
2023-12-31 16:47:53 +01:00
healthcheck Check for and report any config issues or untracked deployments
get-available List available packages in repo
2023-10-20 08:22:28 +02:00
remove Remove a specified deployment
2023-11-18 06:23:57 +00:00
Examples:
arkdep deploy
Check for updates, if available download and deploy
2023-12-10 20:14:42 +01:00
arkdep deploy test-arkanelinux-kde
2023-11-18 06:23:57 +00:00
Download and deploy the latest release of the defined image variant
arkdep deploy - stable_v1
Download the defined image release from the default variant
2023-12-10 20:14:42 +01:00
arkdep deploy test-arkanelinux-kde stable_v1
2023-11-18 06:23:57 +00:00
Download the defined image release from the specified variant
2024-01-04 18:31:33 +01:00
arkdep deploy cache stable_v1
Deploy an image from the cache directly skipping database download and checksum check
2023-11-18 06:23:57 +00:00
arkdep remove stable_v1
Remove the defined deployment
2023-07-05 01:44:37 +02:00
END
exit 0
fi
## Set common variables
#
2023-09-21 21:51:57 +02:00
declare -r arkdep_dir='/arkdep/'
2023-07-05 01:44:37 +02:00
2024-02-12 02:25:02 +01:00
if [[ ! -d $arkdep_dir ]] && [[ ! $1 == 'init' ]]; then
printf "\e[1;31m<#>\e[0m\e[1m Arkep does not seem to be managing this system for $arkdep_dir was not found\e[0m\n"
exit 1
fi
2023-09-11 19:12:40 +02:00
## Load config file
#
2023-10-15 08:30:44 +02:00
source $(readlink -m $arkdep_dir/config)
2023-09-11 19:12:40 +02:00
2023-12-10 20:14:42 +01:00
# Set default variables if config variables are undefined
[[ -z ${enable_overlay+x} ]] && enable_overlay=1 && printf '\e[1;33m<!>\e[0m\e[1m enable_overlay not defined in config, using default\e[0m\n'
[[ -z ${repo_url+x} ]] && repo_url='https://arkanelinux.org/arkdep' && printf '\e[1;33m<!>\e[0m\e[1m repo_url not defined in config, using default\e[0m\n'
[[ -z ${repo_default_image+x} ]] && repo_default_image='arkanelinux' && printf '\e[1;33m<!>\e[0m\e[1m repo_default_image not defined in config, using default\e[0m\n'
[[ -z ${deploy_keep+x} ]] && deploy_keep=3 && printf '\e[1;33m<!>\e[0m\e[1m deploy_config not defined in config, using default\e[0m\n'
[[ -z ${clean_cache_on_remove+x} ]] && clean_cache_on_remove=1 && printf '\e[1;33m<!>\e[0m\e[1m clean_cache_on_remove not defined in config, using default\e[0m\n'
2023-12-31 16:47:53 +01:00
[[ -z ${always_healthcheck+x} ]] && always_healthcheck=1 && printf '\e[1;33m<!>\e[0m\e[1m always_healthcheck not defined in config, using default\e[0m\n'
2024-01-19 13:23:04 +01:00
[[ -z ${gpg_signature_check+x} ]] && gpg_signature_check=1 && printf '\e[1;33m<!>\e[0m\e[1m gpg_signature_check not defined in config, using default\e[0m\n'
2024-02-08 21:35:35 +01:00
[[ -z ${minimum_available_boot_storage+x} ]] && minimum_available_boot_storage=153600 && printf '\e[1;33m<!>\e[0m\e[1m minimum_available_boot_storage not defined in config, using default\e[0m\n'
[[ -z ${minimum_available_root_storage+x} ]] && minimum_available_root_storage=12582912 && printf '\e[1;33m<!>\e[0m\e[1m minimum_available_root_storage not defined in config, using default\e[0m\n'
2023-12-10 20:14:42 +01:00
2023-07-05 01:44:37 +02:00
## Common functions
#
# Cleanup and quit if error
cleanup_and_quit () {
2023-10-21 22:06:19 +02:00
# If any paramters are passed we will assume it to be an error message
2023-07-05 01:44:37 +02:00
[[ -n $1 ]] && printf "\e[1;31m<#>\e[0m $*\e[0m\n" >&2
2023-11-12 22:40:17 +00:00
# Ensure we do not try to remove our current deployment
if grep "$(readlink -m $arkdep_dir/deployments/$deployment)" /proc/cmdline > /dev/null; then
2023-12-10 20:14:42 +01:00
printf '\e[1;33m<!>\e[0m\e[1m Cleanup target is current active deployment, skipping\e[0m\n'
2023-11-12 22:40:17 +00:00
exit 1
fi
2023-09-14 19:10:20 +02:00
# Remove the subvolume we were working on
# TODO: Make this a generic function and share with the removal of old images?
if [[ -n ${data[0]} ]]; then
2023-11-12 22:40:17 +00:00
btrfs property set -f -ts $(readlink -m $arkdep_dir/deployments/${data[0]}/rootfs) ro false
2023-09-21 21:51:57 +02:00
rm -rf $arkdep_dir/deployments/${data[0]}
2024-02-04 02:30:39 +01:00
rm $arkdep_dir/cache/${data[0]}-*.img
2023-09-21 21:51:57 +02:00
rm -rf /boot/arkdep/${data[0]}
2023-09-14 19:10:20 +02:00
rm /boot/loader/entries/${data[0]}.conf
fi
2023-07-05 01:44:37 +02:00
2023-11-12 22:40:17 +00:00
exit 1
2023-07-05 01:44:37 +02:00
}
2023-12-31 16:47:53 +01:00
## Healthcheck
#
# Check for and report on any issues such as untracked deployments or hanging files in cache
healthcheck () {
# Gather tracked deployments
declare -r tracker=($(cat $arkdep_dir/tracker))
declare -r deployed=($(ls $arkdep_dir/deployments/))
declare untracked=${deployed[@]}
# Check for hanging cache files
declare -r cached=($(ls $arkdep_dir/cache/))
declare hanging_cache=()
# Generate grep regex for cache check
declare cache_regex=$(printf "|%s" "${tracker[@]}")
cache_regex=${cache_regex:1}
# Compare items in tracker to actual deployed
for tracked in ${tracker[@]}; do
untracked=("${untracked[@]/$tracked}")
done
for cached_item in ${cached[@]}; do
hanging_cache+=($(echo $cached_item | grep -v -E "$cache_regex"))
done
# Clean whitespaces
untracked=$(echo $untracked | xargs)
if [[ ! -z $untracked ]]; then
printf '\e[1;33m<!>\e[0m\e[1m The following deployments were found but are untracked\n\e[0m'
for t in ${untracked[@]}; do
printf "$t\n"
done
fi
if [[ ! -z $hanging_cache ]]; then
printf '\e[1;33m<!>\e[0m\e[1m The following hanging images were found in cache\n\e[0m'
for t in ${hanging_cache[@]}; do
printf "$t\n"
done
fi
2024-01-19 13:23:04 +01:00
# Warn if gpg check is enabled but no keys are installed
if [[ ! $gpg_signature_check -eq 0 ]] && [[ ! -s $arkdep_dir/keys/trusted-keys ]]; then
2024-02-04 02:31:57 +01:00
printf "\e[1;33m<!>\e[0m\e[1m gpg_signature_check is enabled but $(readlink -m $arkdep_dir/keys/trusted-keys) does not exist or is empty\n\e[0m"
2024-01-19 13:23:04 +01:00
fi
2023-12-31 16:47:53 +01:00
# If $1 is healthcheck it was manually called by the user
[[ $1 == 'healthcheck' ]] && exit 0
}
# Always healthcheck on run if requested in config, unless the user explicitely called it
[[ $always_healthcheck -eq 1 ]] && [[ ! $1 == 'healthcheck' ]] && healthcheck
2023-07-05 01:44:37 +02:00
## Error checking
#
2024-02-20 07:43:25 +01:00
# Quit if not root, only run if required
if [[ ! $1 =~ ^(get-available|healthcheck) ]]; then
if [[ ! $EUID -eq 0 ]]; then
printf '\e[1;31m<#>\e[0m\e[1m This program has to be run as root\n\e[0m' &&
exit 1
fi
fi
2023-07-05 01:44:37 +02:00
# Check if all dependencies are installed, quit if not
2024-01-19 13:23:04 +01:00
for prog in btrfs wget dracut bootctl curl gpg gpgv; do
2023-07-05 01:44:37 +02:00
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"
2024-02-08 21:35:35 +01:00
# Do not immediately exit to log all missing programs
2023-10-15 08:30:44 +02:00
err=1
2023-07-05 01:44:37 +02:00
fi
2023-10-15 08:30:44 +02:00
[[ $err ]] && exit 1
2023-07-05 01:44:37 +02:00
done
2024-02-08 21:35:35 +01:00
# Ensure minimum required storage is available, only run if new deployment will be made
if [[ $1 == 'deploy' ]]; then
declare boot_storage_available=($(df --output=avail /boot))
boot_storage_available=${boot_storage_available[1]}
declare root_storage_available=($(df --output=avail /))
root_storage_available=${root_storage_available[1]}
2024-02-09 17:02:45 +01:00
# Check amount of available boot storage, do not run if set to 0
if [[ $boot_storage_available -lt $minimum_available_boot_storage ]] && [[ $minimum_available_boot_storage -ne 0 ]]; then
2024-02-08 21:35:35 +01:00
printf "\e[1;31m<#>\e[0m\e[1m Less than ${minimum_available_boot_storage}Kib available on boot partition\e[0m\n"
exit 1
fi
2024-02-09 17:02:45 +01:00
# Check amount of available root storage, do not run if set to 0
if [[ $root_storage_available -lt $minimum_available_root_storage ]] && [[ $minimum_available_root_storage -ne 0 ]] ; then
2024-02-08 21:35:35 +01:00
printf "\e[1;31m<#>\e[0m\e[1m Less than ${minimum_available_root_storage}Kib available on root partition\e[0m\n"
exit 1
fi
fi
2023-07-05 01:44:37 +02:00
## Core functions
#
2023-09-21 21:51:57 +02:00
# Initialize the system for arkdep
2023-07-05 01:44:37 +02:00
init () {
2023-07-31 03:07:34 +02:00
# Ensure systemd-boot is installed before continuing, for it is the only thing we support
bootctl is-installed || cleanup_and_quit 'systemd-boot seems to not be installed'
2023-09-21 21:51:57 +02:00
printf '\e[1;34m-->\e[0m\e[1m Initializing arkdep\e[0m\n'
2023-07-05 01:44:37 +02:00
2023-09-21 21:51:57 +02:00
[[ -d $arkdep_dir ]] && cleanup_and_quit "$arkdep_dir already exists"
2023-07-05 01:44:37 +02:00
2023-09-21 21:51:57 +02:00
# Create the /arkdep subvolume
printf "\e[1;34m-->\e[0m\e[1m Creating $(readlink -m $arkdep_dir) subvolume\e[0m\n"
btrfs subvolume create $arkdep_dir || cleanup_and_quit "Failed to create btrfs subvolume"
2023-07-05 01:44:37 +02:00
# Create directory structure
2023-12-11 22:11:25 +01:00
printf '\e[1;34m-->\e[0m\e[1m Creating directory structure\e[0m\n'
2023-09-21 21:51:57 +02:00
mkdir -pv $(readlink -m $arkdep_dir/deployments) \
$(readlink -m $arkdep_dir/deployments) \
$(readlink -m $arkdep_dir/cache) \
$(readlink -m $arkdep_dir/templates) \
$(readlink -m $arkdep_dir/overlay) \
2024-01-19 13:23:04 +01:00
$(readlink -m $arkdep_dir/keys) \
2023-09-21 21:51:57 +02:00
$(readlink -m $arkdep_dir/shared) ||
2023-10-26 18:37:07 +00:00
cleanup_and_quit "Failed to create $arkdep_dir and related directories"
2023-07-05 01:44:37 +02:00
2023-08-25 00:15:23 +02:00
# Create empty database files
2023-09-21 21:51:57 +02:00
touch $(readlink -m $arkdep_dir/tracker)
2024-01-19 13:23:04 +01:00
touch $(readlink -m $arkdep_dir/keys/trusted-keys)
2023-08-25 00:15:23 +02:00
2023-07-31 03:07:34 +02:00
# Add home shared subvolume and make writable
2023-09-21 21:51:57 +02:00
btrfs subvolume create $(readlink -m $arkdep_dir/shared/home) || cleanup_and_quit "Failed to create home subvolume"
btrfs property set -f -ts $(readlink -m $arkdep_dir/shared/home) ro false
2023-07-31 03:07:34 +02:00
2023-07-05 01:44:37 +02:00
# Write default config file
2023-12-11 22:11:25 +01:00
printf '\e[1;34m-->\e[0m\e[1m Adding default config file\e[0m\n'
2023-09-21 21:51:57 +02:00
cat <<- END > $arkdep_dir/config
2024-01-19 13:23:04 +01:00
# Write /arkdep/overlay overlay to new deployments
2023-09-16 11:53:45 +02:00
enable_overlay=1
2023-07-05 01:44:37 +02:00
2023-08-14 18:01:33 +02:00
# URL to image repository, do not add trailing slash
2023-09-21 21:51:57 +02:00
repo_url='https://repo.arkanelinux.org/arkdep'
2023-08-14 18:01:33 +02:00
# Default image pulled from repo if nothing defined
repo_default_image='arkanelinux'
2023-08-25 00:15:23 +02:00
2023-11-02 20:58:34 +00:00
# Keep the latest n-1 deployments, remove anything older
2023-11-14 17:36:29 +00:00
deploy_keep=4
2023-12-09 04:03:33 +01:00
# Remove images from the cache when their deployments are removed
clean_cache_on_remove=1
2023-12-31 16:47:53 +01:00
# Check for untracked deployments and other issues on run
always_healthcheck=1
2024-01-19 13:23:04 +01:00
# Perform a GPG signature check on remote sources
2024-02-08 21:35:35 +01:00
# 1 = enabled but optional, 2 = required
2024-01-19 13:23:04 +01:00
gpg_signature_check=1
2024-02-08 21:35:35 +01:00
2024-02-08 21:41:15 +01:00
# Minimum amount of storage which needs to be available on /boot in Kib
2024-02-08 21:35:35 +01:00
minimum_available_boot_storage=153600
2024-02-08 21:41:15 +01:00
# Minimum amount of storage which needs to be available on / in Kib
2024-02-08 21:35:35 +01:00
minimum_available_root_storage=12582912
2023-07-05 01:44:37 +02:00
END
2023-08-10 06:44:20 +02:00
# Add default bootloader config file
2023-09-21 21:51:57 +02:00
cat <<- END > $arkdep_dir/templates/systemd-boot
2024-02-09 21:09:20 +01:00
title Arkane GNU/Linux - Arkdep
2023-09-21 21:51:57 +02:00
linux /arkdep/%target%/vmlinuz
2023-07-31 03:07:34 +02:00
initrd /amd-ucode.img
initrd /intel-ucode.img
2023-09-21 21:51:57 +02:00
initrd /arkdep/%target%/initramfs-linux.img
options root="LABEL=arkane_root" rootflags=subvol=/arkdep/deployments/%target%/rootfs rw
2023-07-31 03:07:34 +02:00
END
2023-07-05 01:44:37 +02:00
exit 0
2023-07-31 03:07:34 +02:00
2023-07-05 01:44:37 +02:00
}
teardown () {
cat <<- END
2023-09-21 21:51:57 +02:00
WARNING: Removing arkdep may leave your system in an unbootable state and you
2023-07-05 01:44:37 +02:00
may have to manually reconfigure your bootloader etc.. Only proceed if you know
what you are doing!
The following changes will be made to your system;
2023-09-21 21:51:57 +02:00
- All subvolumes under $arkdep_dir will be deleted
2023-12-14 05:36:14 +01:00
- All systemd-boot bootloader entries containing the word "arkdep" will be removed
- Kernel and initramfs storage location /boot/arkdep will be removed
2023-07-05 01:44:37 +02:00
END
2023-12-14 05:36:14 +01:00
# Ensure user knows what they are doing
2023-11-27 08:25:01 +00:00
read -p 'Type "I KNOW WHAT I AM DOING" in uppercase to confirm that you know what you are doing: ' input_confirm
2023-07-05 01:44:37 +02:00
if [[ $input_confirm == 'I KNOW WHAT I AM DOING' ]]; then
2023-09-21 21:51:57 +02:00
printf '\e[1;34m-->\e[0m\e[1m Tearing down arkdep\e[0m\n'
2023-07-05 01:44:37 +02:00
2023-09-21 21:51:57 +02:00
# Quit with error if $arkdep_dir does not exist
if [[ ! -d $arkdep_dir ]]; then
printf "\e[1;31m<#>\e[0m $(readlink -m $arkdep_dir) does not exist, there is nothing to tear down"
2023-07-05 01:44:37 +02:00
exit 1
fi
2023-10-20 07:19:44 +02:00
# Remove all bootloader entries
rm -v $(grep -ril arkdep /boot/loader/entries)
2023-07-05 01:44:37 +02:00
2023-12-14 05:36:14 +01:00
# Remove kernels and initramfs deployed by Arkdep
rm -rfv /boot/arkdep
2023-10-20 07:19:44 +02:00
# Ensure all nested volumes in arkdep are writable and remove
for volume in $(btrfs subvolume list / | grep -oE '[^ ]+$' | grep "^$arkdep_dir" | tac); do
2023-11-12 23:25:42 +00:00
btrfs property set -f -ts $(readlink -m /$volume) ro false
2023-10-20 07:19:44 +02:00
btrfs subvolume delete $volume
done
2023-07-05 01:44:37 +02:00
else
2023-12-14 05:36:14 +01:00
printf '\e[1;33m<!>\e[0m\e[1m Teardown canceled, no changes made to system\e[0m\n'
2023-07-05 01:44:37 +02:00
fi
exit 0
}
2023-10-20 08:22:28 +02:00
remove_deployment () {
# Ensure required vars are set
2023-11-18 08:43:35 +00:00
[[ -z $1 ]] && \
2023-10-20 08:22:28 +02:00
printf 'No deployment defined\n' && exit 1
2023-11-09 22:36:50 +00:00
# Ensure deployment is not currently active
2023-11-18 08:43:35 +00:00
if grep "$(readlink -m $arkdep_dir/deployments/$1)" /proc/cmdline > /dev/null; then
2023-11-09 22:36:50 +00:00
printf 'Target is currently active deployment\n'
exit 1
fi
2023-10-21 22:06:19 +02:00
# Ensure user only provided a single target
2023-11-18 08:43:35 +00:00
[[ ! -z $2 ]] && \
2023-10-21 22:06:19 +02:00
printf 'Multiple targets provided, remove only accepts a single target at a time\n' && exit 1
2023-12-15 02:24:05 +01:00
# Ensure requested deployment is tracked
2023-11-18 08:43:35 +00:00
declare -r hits=($(grep $1 $arkdep_dir/tracker))
2023-10-20 08:22:28 +02:00
if [[ ${#hits[@]} -gt 1 ]]; then
printf 'Multiple deployments match target, be more specific\n'
exit 1
elif [[ ${#hits[@]} -lt 1 ]]; then
printf 'No deployments match target\n'
exit 1
fi
declare -r target="${hits[0]}"
# Remove bootloader entry
rm -rfv /boot/loader/entries/$target.conf
rm -rfv /boot/arkdep/$target
# Ensure the deployment and all sub-volumes are writable
for volume in $(btrfs subvolume list / | grep -oE '[^ ]+$' | grep $target); do
2023-11-12 23:25:42 +00:00
btrfs property set -f -ts $(readlink -m /$volume) ro false || printf "failed to make subvol $volume writable\n"
2023-10-20 08:22:28 +02:00
done
# Remove the deployment
rm -rf $(readlink -m $arkdep_dir/deployments/$target)
# Remove from tracker
2023-12-15 02:24:05 +01:00
grep -v $1 $arkdep_dir/tracker > $arkdep_dir/tracker_tmp || cleanup_and_quit 'Failed to update tracker file'
mv $arkdep_dir/tracker_tmp $arkdep_dir/tracker || cleanup_and_quit 'Failed to move tracker_tmp file to tracker'
2023-10-20 08:22:28 +02:00
2023-12-09 04:03:33 +01:00
# Remove images from cache if requested
if [[ $clean_cache_on_remove -eq 1 ]]; then
# Only attempt remove if file exists
if ls $arkdep_dir/cache/ | grep $1; then
2023-12-31 19:28:22 +01:00
rm -v $(readlink -m $arkdep_dir/cache/$1.tar.*)
2023-12-09 04:03:33 +01:00
fi
fi
2023-10-20 08:22:28 +02:00
exit 0
}
2023-07-22 18:15:44 +02:00
# List all available packages defined in the repo's list file
get_available () {
printf "\e[1;34m-->\e[0m\e[1m Downloading list file from $repo_url\e[0m\n"
2023-12-11 22:11:25 +01:00
curl -sf "${repo_url}/list" || printf '\e[1;33m<!>\e[0m\e[1m Failed to download list file\e[0m\n'
2023-11-15 21:21:16 +00:00
exit 0
2023-07-05 01:44:37 +02:00
}
2023-07-22 18:15:44 +02:00
# Deploy a new or update an existing deployment
deploy () {
2023-08-14 20:08:38 +02:00
# target and version are optional, if not defined default to primary as defined in
2023-09-21 21:51:57 +02:00
# /arkdep/config and latest
2024-01-04 18:07:36 +01:00
if [[ -n $1 ]] && [[ $1 != '-' ]]; then
2023-11-18 08:43:35 +00:00
declare -r deploy_target=$1
2023-08-15 10:12:02 +02:00
else
2024-01-04 18:07:36 +01:00
declare -r deploy_target=$repo_default_image
2023-08-15 10:12:02 +02:00
fi
2023-11-18 08:43:35 +00:00
if [[ -n $2 ]]; then
declare -r deploy_version=$2
2023-08-15 10:12:02 +02:00
else
declare -r deploy_version='latest'
fi
2023-07-22 18:15:44 +02:00
2024-01-04 18:07:36 +01:00
# If cache requested version may not be latest
2024-01-04 22:10:23 +01:00
if [[ $1 == 'cache' ]] && [[ $deploy_version == 'latest' ]]; then
2024-01-04 18:07:36 +01:00
cleanup_and_quit '"latest" and undefined are not a valid version definitions for a cache source'
fi
2023-07-22 18:15:44 +02:00
printf "\e[1;34m-->\e[0m\e[1m Deploying $deploy_target $deploy_version\e[0m\n"
2024-01-04 18:07:36 +01:00
# Split latest_version at the delimiter, creating an array with data.0=package ver, data.1=compression method, data.2=sha1 hash
# only run if request target is not cache
if [[ $1 != 'cache' ]]; then
# If latest is requested grab database and get first line
2024-01-31 00:25:03 +01:00
printf '\e[1;34m-->\e[0m\e[1m Downloading database\e[0m\n'
2024-01-04 18:07:36 +01:00
if [[ $deploy_version == 'latest' ]]; then
declare curl_data=$(curl -sf "${repo_url}/${deploy_target}/database" | head -n 1)
elif [[ $deploy_target != 'cache' ]]; then
# Only return first hit
declare curl_data=$(curl -sf "${repo_url}/${deploy_target}/database" | grep -E "^$2" | head -1)
else
declare curl_data='cache'
fi
readarray -d : -t data <<< "$curl_data"
# If target is cache
2023-08-15 09:54:08 +02:00
else
2024-01-20 06:35:34 +01:00
# Find full name in cache, exclude sig files, if no hit quit with error
declare cache_hits=($(ls $arkdep_dir/cache | grep -E "^$deploy_version" | grep -v '.sig$'))
2024-01-04 18:07:36 +01:00
# Temporary var to store the delimited file found in cache
declare data_inter=()
# Check if none or more than a single hit, we only expect a single item to match
[[ ${#cache_hits[@]} -gt 1 ]] && cleanup_and_quit 'More than a single item in cache matched requested version'
[[ ${#cache_hits[@]} -lt 1 ]] && cleanup_and_quit 'No item in cache matched requested version'
# Split filename at delimiter
readarray -d . -t data_inter <<< "$cache_hits"
# Set expected vars for remainder of script
data[0]=${data_inter[0]}
data[1]=${data_inter[2]}
data[2]='-'
fi
2023-11-18 02:44:25 +00:00
# Ensure none of the vars contain whitespaces
data[0]=${data[0]//[$'\t\r\n']}
data[1]=${data[1]//[$'\t\r\n']}
2023-08-15 09:54:08 +02:00
data[2]=${data[2]//[$'\t\r\n']}
2023-11-18 02:44:25 +00:00
# Lets do a bunch of checks to ensure the data is all present
if [[ -z ${data[0]+x} ]] || [[ ! -n ${data[0]} ]]; then
printf '\e[1;31m<#>\e[0m\e[1m No target found\n\e[0m'
exit 1
fi
if [[ -z ${data[1]+x} ]] || [[ ! -n ${data[1]} ]]; then
printf '\e[1;31m<#>\e[0m\e[1m No compression method found\n\e[0m'
exit 1
fi
if [[ -z ${data[2]+x} ]] || [[ ! -n ${data[2]} ]]; then
2024-01-04 18:07:36 +01:00
# Do not trigger if hash is -, is used for cache deployments
if [[ $deploy_target != '-' ]]; then
printf '\e[1;31m<#>\e[0m\e[1m No checksum found\n\e[0m'
exit 1
fi
2023-11-18 02:44:25 +00:00
fi
2023-09-14 19:42:54 +02:00
# Lets ensure the requested image is not already deployed
2023-09-21 21:51:57 +02:00
if [[ -e $arkdep_dir/deployments/${data[0]} ]]; then
2023-09-14 19:42:54 +02:00
printf "\e[1;33m<!>\e[0m\e[1m ${data[0]} is already deployed, canceling deployment\e[0m\n"
2023-11-12 23:03:29 +00:00
exit 1
2023-09-14 19:42:54 +02:00
fi
2023-07-22 18:15:44 +02:00
# Check if requested version is already downloaded
2023-09-21 21:51:57 +02:00
if [[ -e $arkdep_dir/cache/${data[0]}.tar.${data[1]} ]]; then
2023-07-22 18:15:44 +02:00
printf "\e[1;34m-->\e[0m\e[1m ${data[0]} already in cache, skipping download\e[0m\n"
else
2024-01-31 00:25:03 +01:00
printf "\e[1;34m-->\e[0m\e[1m Downloading disk image\e[0m\n"
2023-08-14 20:08:38 +02:00
# Download the tarball if not yet downloaded
2023-09-21 21:51:57 +02:00
if [[ ! -e $arkdep_dir/cache/${data[0]}.tar.${data[1]} ]]; then
2023-12-23 07:36:59 +01:00
wget -q --show-progress -P $(readlink -m $arkdep_dir/cache/) "$repo_url/$deploy_target/${data[0]}.tar.${data[1]}" ||
2023-08-14 20:08:38 +02:00
cleanup_and_quit 'Failed to download tarball'
2024-01-19 13:23:04 +01:00
2024-01-20 06:35:34 +01:00
# Download GPG signature, only perform check if not disabled by user and keychain exists
2024-01-19 13:23:04 +01:00
if [[ ! $gpg_signature_check -eq 0 ]] && [[ -s $arkdep_dir/keys/trusted-keys ]]; then
2024-01-20 06:35:34 +01:00
# Download gpg signature if not yet in cache
if [[ ! -s $arkdep_dir/cache/${data[0]}.tar.${data[1]}.sig ]]; then
wget -q --show-progress -P $(readlink -m $arkdep_dir/cache/) "$repo_url/$deploy_target/${data[0]}.tar.${data[1]}.sig"
sig_exitcode=$?
fi
2024-01-19 13:23:04 +01:00
if [[ ! $sig_exitcode -eq 0 ]] && [[ $gpg_signature_check -eq 1 ]]; then
# Sig download is allowed to fail
printf "\e[1;33m<!>\e[0m\e[1m Failed to download GPG signature, signature check will be skipped\e[0m\n"
elif [[ ! $sig_exitcode -eq 0 ]] && [[ $gpg_signature_check -eq 2 ]]; then
2024-01-20 06:35:34 +01:00
# gpg_signature_check = 2, error and quit the program on fail
2024-01-19 13:23:04 +01:00
cleanup_and_quit 'GPG signature check configured to quit on download failure'
fi
fi
2023-08-14 20:08:38 +02:00
fi
2024-02-21 02:06:45 +01:00
fi
2024-01-25 17:50:43 +01:00
2024-02-21 02:06:45 +01:00
if [[ $gpg_signature_check -eq 2 ]] && [[ ! -s $arkdep_dir/cache/${data[0]}.tar.${data[1]}.sig ]]; then
# if GPG check required but file not present error and quit
cleanup_and_quit 'GPG signature expected but none were provided'
elif [[ ! -s $arkdep_dir/cache/${data[0]}.tar.${data[1]}.sig ]]; then
skip_gpg_check=1
2023-07-22 18:15:44 +02:00
fi
2024-01-20 06:35:34 +01:00
# If not configured to skip by previous error handeling check the signature to the downloaded image
2024-01-25 17:50:43 +01:00
if [[ ! $skip_gpg_check -eq 1 ]]; then
2024-01-20 06:35:34 +01:00
printf '\e[1;34m-->\e[0m\e[1m Checking GPG signature\e[0m\n'
# Perform GPG signature check
gpgv --keyring $arkdep_dir/keys/trusted-keys $arkdep_dir/cache/${data[0]}.tar.${data[1]}.sig $arkdep_dir/cache/${data[0]}.tar.${data[1]} ||
cleanup_and_quit 'gpg check failed'
elif [[ ${data[2]} != '-' ]]; then
# If GPG check not triggered instead check hash, unless defined as -
2024-01-04 18:07:36 +01:00
printf '\e[1;34m-->\e[0m\e[1m Validating integrity\e[0m\n'
sha1sum "$(readlink -m $arkdep_dir/cache/${data[0]}.tar.${data[1]})" |
grep "${data[2]}" ||
cleanup_and_quit "Checksum does not match the one defined in database\e[0m\n"
fi
2023-07-22 18:15:44 +02:00
2023-09-16 18:31:55 +02:00
# Extract the root image if not yet extracted
2023-12-11 22:11:25 +01:00
printf '\e[1;34m-->\e[0m\e[1m Writing root\e[0m\n'
2023-09-16 18:31:55 +02:00
2023-08-14 20:08:38 +02:00
# Create directory using unique deployment name
2023-09-21 21:51:57 +02:00
mkdir -pv $(readlink -m $arkdep_dir/deployments/${data[0]}) || cleanup_and_quit 'Failed to create deployment directory'
2023-08-14 20:08:38 +02:00
2023-09-21 21:51:57 +02:00
if [[ ! -e $arkdep_dir/cache/${data[0]}-rootfs.img ]]; then
tar -xf $(readlink -m $arkdep_dir/cache/${data[0]}.tar.${data[1]}) -C $(readlink -m $arkdep_dir/cache/) "./${data[0]}-rootfs.img" ||
2023-08-14 20:08:38 +02:00
cleanup_and_quit 'Failed to extract root'
fi
2023-07-22 18:15:44 +02:00
# Write the root image
2023-09-21 21:51:57 +02:00
btrfs receive -f $(readlink -m $arkdep_dir/cache/${data[0]}-rootfs.img) $(readlink -m $arkdep_dir/deployments/${data[0]}) ||
2023-07-31 03:07:34 +02:00
cleanup_and_quit 'Failed to receive root'
# Cleanup root image
2023-09-21 21:51:57 +02:00
rm $(readlink -m $arkdep_dir/cache/${data[0]}-rootfs.img)
2023-07-31 03:07:34 +02:00
# Extract the etc image if not yet extracted
2023-12-11 22:11:25 +01:00
printf '\e[1;34m-->\e[0m\e[1m Writing etc\e[0m\n'
2023-09-21 21:51:57 +02:00
if [[ ! -e $arkdep_dir/cache/${data[0]}-etc.img ]]; then
tar -xf $(readlink -m $arkdep_dir/cache/${data[0]}.tar.${data[1]}) -C $(readlink -m $arkdep_dir/cache/) "./${data[0]}-etc.img" ||
2023-08-14 20:08:38 +02:00
cleanup_and_quit 'failed to extract etc'
fi
2023-07-31 03:07:34 +02:00
2023-08-14 20:08:38 +02:00
# Write the etc image and create var directory, we have to unlock rootfs temporarily to do this
2023-09-21 21:51:57 +02:00
btrfs property set -f -ts $(readlink -m $arkdep_dir/deployments/${data[0]}/rootfs) ro false ||
2023-07-31 03:07:34 +02:00
cleanup_and_quit 'Failed to unlock root to write etc'
2023-08-14 20:08:38 +02:00
2023-09-21 21:51:57 +02:00
btrfs receive -f $(readlink -m $arkdep_dir/cache/${data[0]}-etc.img) $(readlink -m $arkdep_dir/deployments/${data[0]}/rootfs/) ||
2023-07-31 03:07:34 +02:00
cleanup_and_quit 'Failed to receive etc'
2023-08-14 20:08:38 +02:00
2023-12-11 22:11:25 +01:00
printf '\e[1;34m-->\e[0m\e[1m Ensure var, root and arkdep mountpoints exist\e[0m\n'
2023-09-21 21:51:57 +02:00
mkdir -pv $(readlink -m $arkdep_dir/deployments/${data[0]}/rootfs/var)
2023-09-21 22:01:17 +02:00
mkdir -pv $(readlink -m $arkdep_dir/deployments/${data[0]}/rootfs/arkdep)
2023-08-14 20:08:38 +02:00
2023-09-16 18:31:55 +02:00
# Lock the root volume again
2023-09-21 21:51:57 +02:00
btrfs property set -f -ts $(readlink -m $arkdep_dir/deployments/${data[0]}/rootfs) ro true ||
2023-07-31 03:07:34 +02:00
cleanup_and_quit 'Failed to lock root'
2023-08-14 20:08:38 +02:00
# Unlock the etc deployment
2023-09-21 21:51:57 +02:00
btrfs property set -f -ts $(readlink -m $arkdep_dir/deployments/${data[0]}/rootfs/etc) ro false ||
2023-08-14 20:08:38 +02:00
cleanup_and_quit 'Failed to unlock root to write etc'
2023-08-25 00:15:23 +02:00
# Cleanup etc image
2023-09-21 21:51:57 +02:00
rm $(readlink -m $arkdep_dir/cache/${data[0]}-etc.img)
2023-08-25 00:15:23 +02:00
2023-07-31 03:07:34 +02:00
# Write the var image
2023-09-21 21:51:57 +02:00
if [[ ! -e $arkdep_dir/shared/var ]]; then
2023-12-11 22:11:25 +01:00
printf '\e[1;34m-->\e[0m\e[1m Writing var\e[0m\n'
2023-09-18 23:23:24 +02:00
# Extract the var image if not yet extracted
2023-09-21 21:51:57 +02:00
if [[ ! -e $arkdep_dir/cache/${data[0]}-var.img ]]; then
tar -xf $(readlink -m $arkdep_dir/cache/${data[0]}.tar.${data[1]}) -C $(readlink -m $arkdep_dir/cache/) "./${data[0]}-var.img" ||
2023-09-18 23:23:24 +02:00
cleanup_and_quit 'failed to extract var'
fi
2023-09-21 21:51:57 +02:00
btrfs receive -f $(readlink -m $arkdep_dir/cache/${data[0]}-var.img) $(readlink -m $arkdep_dir/shared/) ||
2023-08-25 00:15:23 +02:00
cleanup_and_quit 'Failed to receive var'
2023-09-18 23:39:29 +02:00
else
# Notify if var is not deployed
2023-12-11 22:26:46 +01:00
printf "\e[1;33m<!>\e[0m\e[1m var is already preset, skipping var deployment\e[0m\n"
2023-08-25 00:15:23 +02:00
fi
2023-07-31 03:07:34 +02:00
# Make var writable
2023-09-21 21:51:57 +02:00
btrfs property set -f -ts $(readlink -m $arkdep_dir/shared/var) ro false ||
2023-07-31 03:07:34 +02:00
cleanup_and_quit 'Failed to unlock var'
2023-07-22 18:15:44 +02:00
2023-08-14 20:08:38 +02:00
# Cleanup var image
2023-09-21 21:51:57 +02:00
rm $(readlink -m $arkdep_dir/cache/${data[0]}-var.img)
2023-08-14 20:08:38 +02:00
2023-09-16 11:53:45 +02:00
# Add overlay if enabled
if [[ $enable_overlay -eq 1 ]]; then
2023-12-11 22:11:25 +01:00
printf '\e[1;34m-->\e[0m\e[1m Copying overlay to deployment\e[0m\n'
2023-10-23 20:02:44 +00:00
declare -r overlay_files=($(ls $arkdep_dir/overlay/))
2023-09-16 11:53:45 +02:00
# Check if only /etc is present, if it is we do not have to unlock the root volume
for file in ${overlay_files[*]}; do
2023-10-23 19:12:24 +00:00
if [[ ! $file == 'etc' ]]; then
2023-09-16 11:53:45 +02:00
printf "\e[1;33m<!>\e[0m\e[1m ${data[0]} Non /etc file or directory detected, root will be temporarily unlocked\e[0m\n"
overlay_unlock_root=1
fi
done
# Unlock root if required
if [[ $overlay_unlock_root -eq 1 ]]; then
2023-09-21 21:51:57 +02:00
btrfs property set -f -ts $(readlink -m $arkdep_dir/deployments/${data[0]}) ro false
2023-09-16 11:53:45 +02:00
fi
2023-09-21 21:51:57 +02:00
cp -rv $(readlink -m $arkdep_dir/overlay/*) $(readlink -m /$arkdep_dir/deployments/${data[0]}/rootfs/)
2023-09-16 11:53:45 +02:00
# Lock root again if required
if [[ $overlay_unlock_root -eq 1 ]]; then
2023-09-21 21:51:57 +02:00
btrfs property set -f -ts $(readlink -m $arkdep_dir/deployments/${data[0]}) ro true
2023-09-16 11:53:45 +02:00
fi
fi
2023-12-11 22:11:25 +01:00
printf '\e[1;34m-->\e[0m\e[1m Copying kernel image\e[0m\n'
2023-12-11 21:49:41 +01:00
# Get list of all available kernels
kernels_installed=($(ls $arkdep_dir/deployments/${data[0]}/rootfs/usr/lib/modules/))
2024-01-19 13:38:17 +01:00
mkdir -pv /boot/arkdep/${data[0]}
2023-12-11 21:49:41 +01:00
# Deploy kernel to /boot, deploy first hit of kernels_installed
2024-01-19 13:38:17 +01:00
cp -v $(readlink -m $arkdep_dir/deployments/${data[0]}/rootfs/usr/lib/modules/${kernels_installed[0]}/vmlinuz) /boot/arkdep/${data[0]}/ ||
2023-09-14 19:31:19 +02:00
cleanup_and_quit 'Failed to copy kernel image'
2023-09-11 18:57:30 +02:00
# Install kernel and generate initramfs
2023-12-11 22:11:25 +01:00
printf '\e[1;34m-->\e[0m\e[1m Generating initramfs\e[0m\n'
2024-01-19 13:38:17 +01:00
dracut -q -k $(readlink -m $arkdep_dir/deployments/${data[0]}/rootfs/usr/lib/modules/${kernels_installed[0]}) \
2023-09-21 21:51:57 +02:00
--kernel-image /boot/arkdep/${data[0]}/vmlinuz \
2023-12-11 21:49:41 +01:00
--kver ${kernels_installed[0]} \
2023-09-14 19:10:20 +02:00
--force \
2023-09-21 21:51:57 +02:00
/boot/arkdep/${data[0]}/initramfs-linux.img || cleanup_and_quit 'Failed to generate initramfs'
2023-09-11 18:57:30 +02:00
2023-08-25 00:15:23 +02:00
# Add to database
2023-12-11 22:11:25 +01:00
printf '\e[1;34m-->\e[0m\e[1m Updating database\e[0m\n'
2023-11-25 02:27:04 +00:00
printf "${data[0]}\n$(cat $(readlink -m $arkdep_dir/tracker))" |
tee $arkdep_dir/tracker.tmp
mv $arkdep_dir/tracker.tmp $arkdep_dir/tracker
2023-08-25 00:15:23 +02:00
2023-08-27 02:07:37 +02:00
# Deploy bootloader configuration
2023-12-09 18:53:22 +01:00
# also insert newline
2023-12-11 22:11:25 +01:00
printf '\n\e[1;34m-->\e[0m\e[1m Adding bootloader entry\e[0m\n'
2023-09-21 21:51:57 +02:00
sed "s/%target%/${data[0]}/" $arkdep_dir/templates/systemd-boot > /boot/loader/entries/${data[0]}.conf
2023-08-27 02:07:37 +02:00
2023-12-31 19:10:19 +01:00
# Set new deployment as default bootloader entry
2023-12-11 22:11:25 +01:00
printf '\e[1;34m-->\e[0m\e[1m Setting new bootloader entry as default\e[0m\n'
2023-11-12 14:02:22 +00:00
bootctl set-default ${data[0]}.conf || cleanup_and_quit "Failed to set default bootloader entry"
2023-08-27 02:07:37 +02:00
2023-08-25 00:15:23 +02:00
# Remove entries outside of keep
2023-11-02 20:24:23 +00:00
declare -r remove_deployments=($(tail -n +$deploy_keep $arkdep_dir/tracker))
2023-08-25 00:15:23 +02:00
# Remove old deployments
2023-11-02 20:24:23 +00:00
for deployment in ${remove_deployments[@]}; do
2023-11-25 04:23:38 +00:00
printf "\e[1;34m-->\e[0m\e[1m Cleaning up old deployment $deployment\e[0m\n"
2023-11-18 08:43:35 +00:00
remove_deployment $deployment
2023-11-25 02:27:04 +00:00
grep -v $deployment $arkdep_dir/tracker |
tee $arkdep_dir/tracker.tmp
mv $arkdep_dir/tracker.tmp $arkdep_dir/tracker
2023-08-25 00:15:23 +02:00
done
2023-08-27 02:07:37 +02:00
2023-11-15 21:21:16 +00:00
exit 0
2023-07-05 01:44:37 +02:00
}
[[ $1 == 'init' ]] && init
[[ $1 == 'teardown' ]] && teardown
2023-07-22 18:15:44 +02:00
[[ $1 == 'get-available' ]] && get_available
2023-11-18 08:43:35 +00:00
[[ $1 == 'deploy' ]] && deploy $2 $3
[[ $1 == 'remove' ]] && remove_deployment $2 $3
2023-12-31 16:47:53 +01:00
[[ $1 == 'healthcheck' ]] && healthcheck $1
# No valid params were provided
exit 3