archboot/usr/bin/archboot-update-installer.sh

176 lines
8.4 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# created by Tobias Powalowski <tpowa@archlinux.org>
source /usr/lib/archboot/functions
2022-01-13 17:21:39 +01:00
_D_SCRIPTS=""
_L_COMPLETE=""
_L_INSTALL_COMPLETE=""
_G_RELEASE=""
_CONFIG="/etc/archboot/${_RUNNING_ARCH}-update_installer.conf"
2022-01-13 17:21:39 +01:00
_W_DIR="/archboot"
2022-03-07 22:47:42 +01:00
_INSTALLER_SOURCE="https://gitlab.archlinux.org/tpowa/archboot/-/raw/master"
2021-09-22 09:02:56 +02:00
2022-02-03 07:14:49 +01:00
kver() {
# get kernel version from installed kernel
2022-03-18 12:32:00 +01:00
[[ "${_RUNNING_ARCH}" == "x86_64" ]] && VMLINUZ="/vmlinuz-linux"
[[ "${_RUNNING_ARCH}" == "aarch64" ]] && VMLINUZ="/Image"
2022-02-03 07:14:49 +01:00
if [[ -f "${VMLINUZ}" ]]; then
offset=$(hexdump -s 526 -n 2 -e '"%0d"' "${VMLINUZ}")
read -r _HWKVER _ < <(dd if="${VMLINUZ}" bs=1 count=127 skip=$(( offset + 0x200 )) 2>/dev/null)
fi
# fallback if no detectable kernel is installed
[[ "${_HWKVER}" == "" ]] && _HWKVER="$(uname -r)"
}
2021-09-22 09:02:56 +02:00
usage () {
2022-03-18 16:09:33 +01:00
echo "Update installer, launch latest environment or create latest image files:"
2022-03-18 14:25:36 +01:00
echo "-------------------------------------------------------------------------"
echo "PARAMETERS:"
echo " -u Update scripts: setup, quickinst, tz, km and helpers."
echo ""
echo "On fast internet connection (100Mbit) (approx. 5 minutes):"
echo " -latest Launch latest archboot environment (using kexec)."
echo " This operation needs at least 2.4 GB RAM."
echo ""
echo " -latest-install Launch latest archboot environment with downloaded"
echo " package cache (using kexec)."
echo " This operation needs at least 2.9 GB RAM."
echo ""
echo " -latest-image Generate latest image files in /archboot-release directory"
echo " This operation needs at least 3.3 GB RAM."
echo ""
echo " -h This message."
exit 0
2021-09-22 09:02:56 +02:00
}
[[ -z "${1}" ]] && usage
while [ $# -gt 0 ]; do
case ${1} in
2022-01-13 17:21:39 +01:00
-u|--u) _D_SCRIPTS="1" ;;
-latest|--latest) _L_COMPLETE="1" ;;
-latest-install|--latest-install) _L_INSTALL_COMPLETE="1";;
-latest-image|--latest-image) _G_RELEASE="1" ;;
2021-09-22 09:02:56 +02:00
-h|--h|?) usage ;;
*) usage ;;
esac
shift
done
if [[ ! "$(cat /etc/hostname)" == "archboot" ]]; then
echo "This script should only be run in booted archboot environment. Aborting..."
exit 1
fi
# Download latest setup and quickinst script from git repository
2022-01-13 17:21:39 +01:00
if [[ "${_D_SCRIPTS}" == "1" ]]; then
2021-10-06 17:49:16 +02:00
echo "Downloading latest km, tz, quickinst, setup and helpers..."
2022-03-07 22:47:42 +01:00
wget -q "$_INSTALLER_SOURCE/usr/bin/archboot-quickinst.sh?inline=false" -O /usr/bin/quickinst >/dev/null 2>&1
wget -q "$_INSTALLER_SOURCE/usr/bin/archboot-setup.sh?inline=false" -O /usr/bin/setup >/dev/null 2>&1
wget -q "$_INSTALLER_SOURCE/usr/bin/archboot-km.sh?inline=false" -O /usr/bin/km >/dev/null 2>&1
wget -q "$_INSTALLER_SOURCE/usr/bin/archboot-tz.sh?inline=false" -O /usr/bin/tz >/dev/null 2>&1
wget -q "$_INSTALLER_SOURCE/usr/bin/archboot-${_RUNNING_ARCH}-create-container.sh?inline=false" -O "/usr/bin/archboot-${_RUNNING_ARCH}-create-container.sh" >/dev/null 2>&1
wget -q "$_INSTALLER_SOURCE/usr/bin/archboot-${_RUNNING_ARCH}-release.sh?inline=false" -O "/usr/bin/archboot-${_RUNNING_ARCH}-release.sh" >/dev/null 2>&1
wget -q "$_INSTALLER_SOURCE/usr/bin/archboot-binary-check.sh?inline=false" -O /usr/bin/archboot-binary-check.sh >/dev/null 2>&1
wget -q "$_INSTALLER_SOURCE/usr/bin/archboot-update-installer.sh?inline=false" -O /usr/bin/update-installer.sh >/dev/null 2>&1
wget -q "$_INSTALLER_SOURCE/usr/lib/archboot/functions?inline=false" -O "/usr/lib/archboot/functions" >/dev/null 2>&1
wget -q "$_INSTALLER_SOURCE/usr/lib/archboot/container_functions?inline=false" -O "/usr/lib/archboot/container_functions" >/dev/null 2>&1
wget -q "$_INSTALLER_SOURCE/usr/lib/archboot/release_functions?inline=false" -O "/usr/lib/archboot/release_functions" >/dev/null 2>&1
wget -q "$_INSTALLER_SOURCE/usr/lib/archboot/iso_functions?inline=false" -O "/usr/lib/archboot/iso_functions" >/dev/null 2>&1
2021-10-07 07:48:14 +02:00
echo "Finished: Downloading scripts done."
2021-10-08 10:04:39 +02:00
exit 0
2021-09-22 09:02:56 +02:00
fi
2021-10-08 10:04:39 +02:00
echo "Information: Logging is done on /dev/tty7 ..."
2021-09-22 09:02:56 +02:00
# Generate new environment and launch it with kexec
2022-01-13 17:21:39 +01:00
if [[ "${_L_COMPLETE}" == "1" || "${_L_INSTALL_COMPLETE}" == "1" ]]; then
if [[ -f /.update-installer ]]; then
2022-03-07 22:39:50 +01:00
echo "Aborting: update-installer.sh is already running on other tty ..."
echo "If you are absolutly sure it's not running, you need to remove /.update-installer"
exit 1
fi
touch /.update-installer
# remove everything not necessary
echo "Step 1/9: Removing not necessary files from / ..."
2022-02-02 22:33:01 +01:00
[[ -d "/usr/lib/firmware" ]] && rm -r "/usr/lib/firmware"
[[ -d "/usr/lib/modules" ]] && rm -r "/usr/lib/modules"
2022-02-02 22:06:37 +01:00
_SHARE_DIRS="efitools file grub hwdata kbd licenses makepkg nmap openvpn pacman refind tc usb_modeswitch vim zoneinfo zsh"
2022-02-08 17:17:17 +01:00
for i in ${_SHARE_DIRS}; do
#shellcheck disable=SC2115
2022-02-02 22:06:37 +01:00
[[ -d "/usr/share/${i}" ]] && rm -r "/usr/share/${i}"
done
2022-03-21 10:57:49 +01:00
echo "Step 2/9: Waiting for gpg pacman keyring import to finish ..."
2022-03-21 11:19:03 +01:00
while pgrep -x gpg > /dev/null 2>&1; do
2022-03-18 14:02:45 +01:00
sleep 1
done
echo "Step 3/9: Generating archboot container in ${_W_DIR} ..."
echo " This will need some time ..."
2021-09-26 08:52:27 +02:00
# create container without package cache
2022-02-02 22:24:47 +01:00
[[ "${_L_COMPLETE}" == "1" ]] && ("archboot-${_RUNNING_ARCH}-create-container.sh" "${_W_DIR}" -cc -cp >/dev/tty7 2>&1 || exit 1)
2021-09-26 08:52:27 +02:00
# create container with package cache
2022-03-18 12:32:00 +01:00
if [[ -e /var/cache/pacman/pkg/archboot.db ]]; then
# offline mode
2022-03-18 14:14:10 +01:00
# add the db too on reboot
2022-03-18 14:25:36 +01:00
install -D -m644 /var/cache/pacman/pkg/archboot.db /archboot/var/cache/pacman/pkg/archboot.db
[[ "${_L_INSTALL_COMPLETE}" == "1" ]] && ("archboot-${_RUNNING_ARCH}-create-container.sh" "${_W_DIR}" -cc --install-source=file:///var/cache/pacman/pkg >/dev/tty7 2>&1 || exit 1)
2022-03-18 12:32:00 +01:00
else
#online mode
2022-03-18 12:32:00 +01:00
[[ "${_L_INSTALL_COMPLETE}" == "1" ]] && ("archboot-${_RUNNING_ARCH}-create-container.sh" "${_W_DIR}" -cc >/dev/tty7 2>&1 || exit 1)
fi
2021-09-25 20:10:26 +02:00
# generate initrd in container, remove archboot packages from cache, not needed in normal install, umount tmp before generating initrd
echo "Step 4/9: Moving kernel from ${_W_DIR} to / ..."
2022-01-18 22:41:45 +01:00
if [[ "${_RUNNING_ARCH}" == "x86_64" ]]; then
2022-01-13 17:21:39 +01:00
mv "${_W_DIR}"/boot/vmlinuz-linux / || exit 1
2022-02-02 22:06:37 +01:00
### not supported
#mv "${_W_DIR}"/boot/intel-ucode.img / || exit 1
2022-01-08 09:39:59 +01:00
fi
2022-01-18 22:41:45 +01:00
if [[ "${_RUNNING_ARCH}" == "aarch64" ]]; then
2022-01-13 17:21:39 +01:00
mv "${_W_DIR}"/boot/Image / || exit 1
2022-01-08 09:39:59 +01:00
fi
echo "Step 5/9: Collect initramfs files in ${_W_DIR} ..."
echo " This will need some time ..."
# add fix for mkinitcpio 31, remove when 32 is released
cp "${_W_DIR}"/usr/share/archboot/patches/31-mkinitcpio.fixed "${_W_DIR}"/usr/bin/mkinitcpio
cp "${_W_DIR}"/usr/share/archboot/patches/31-initcpio.functions.fixed "${_W_DIR}"/usr/lib/initcpio/functions
kver
# write initramfs to /tmp
systemd-nspawn -q -D "${_W_DIR}" /bin/bash -c "umount /tmp; mkinitcpio -k ${_HWKVER} -c ${_CONFIG} -d /tmp/" >/dev/tty7 2>&1 || exit 1
2022-02-03 10:01:03 +01:00
# move initramfs to /
2022-02-03 09:13:09 +01:00
mv "${_W_DIR}/tmp" /initrd || exit 1
echo "Step 6/9: Remove ${_W_DIR} ..."
rm -r "${_W_DIR}" || exit 1
echo "Step 7/9: Create initramfs /initrd.img ..."
2022-02-03 09:25:48 +01:00
echo " This will need some time ..."
2022-02-03 09:42:15 +01:00
cd initrd || exit 1
2022-02-03 10:36:40 +01:00
#from /usr/bin/mkinitpcio.conf
# compress image with zstd
find . -mindepth 1 -printf '%P\0' | sort -z |
LANG=C bsdtar --uid 0 --gid 0 --null -cnf - -T - |
LANG=C bsdtar --null -cf - --format=newc @- | zstd -T0 -10> /initrd.img || exit 1
2022-02-06 09:36:41 +01:00
cd ..
echo "Step 8/9: Remove /initrd ..."
2022-02-06 09:36:41 +01:00
rm -r "/initrd" || exit 1
2022-02-02 22:06:37 +01:00
### not supported
#mv "${_W_DIR}"/boot/amd-ucode.img / || exit 1
2022-01-13 17:21:39 +01:00
# remove "${_W_DIR}"
echo "Step 9/9: Loading files to kexec now, reboot in a few seconds ..."
# load kernel and initrds into running kernel
2022-01-18 22:41:45 +01:00
if [[ "${_RUNNING_ARCH}" == "x86_64" ]]; then
2022-02-06 09:36:41 +01:00
kexec -l /vmlinuz-linux --initrd=/initrd.img --reuse-cmdline
2022-01-08 09:39:59 +01:00
fi
2022-01-18 22:41:45 +01:00
if [[ "${_RUNNING_ARCH}" == "aarch64" ]]; then
2022-02-02 22:06:37 +01:00
kexec -l /Image --initrd=/initrd.img --reuse-cmdline
2022-01-08 09:39:59 +01:00
fi
2021-10-07 07:22:05 +02:00
echo "Finished: Rebooting ..."
# restart environment
2021-09-22 09:02:56 +02:00
systemctl kexec
fi
2021-09-22 09:02:56 +02:00
# Generate new images
2022-01-13 17:21:39 +01:00
if [[ "${_G_RELEASE}" == "1" ]]; then
2022-01-21 11:54:13 +01:00
echo "Step 1/1: Generating new iso files now in ${_W_DIR} ..."
2021-10-07 07:22:05 +02:00
echo " This will need some time ..."
2022-01-21 11:54:13 +01:00
"archboot-${_RUNNING_ARCH}-release.sh" "${_W_DIR}" >/dev/tty7 2>&1 || exit 1
echo "Finished: New isofiles are located in ${_W_DIR}"
2021-09-22 09:02:56 +02:00
fi