-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement clean_chroot, #267
base: master
Are you sure you want to change the base?
Changes from 4 commits
221a31b
61db3a0
5f10d52
0b5fb7b
45ad22d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,14 @@ | |
# shellcheck disable=SC2317 # shellcheck has trouble understanding the code flow in this file | ||
|
||
# error_handler {{{ | ||
error_handler() { | ||
last_exit_code="$?" | ||
last_bash_command="$BASH_COMMAND" | ||
echo "Unexpected non-zero exit code $last_exit_code in ${BASH_SOURCE[*]} at line ${BASH_LINENO[*]} detected! | ||
last bash command: $last_bash_command" | ||
exit 1 | ||
} | ||
|
||
set -e | ||
set -E | ||
set -o pipefail | ||
|
@@ -260,7 +268,7 @@ | |
debconf-set-selections < /etc/debootstrap/debconf-selections | ||
} | ||
|
||
if [ "$PACKAGES" = 'yes' ] ; then | ||
Check warning on line 271 in chroot-script GitHub Actions / shellcheck grml-debootstrap
|
||
PACKAGES_FILE="/etc/debootstrap/packages" | ||
|
||
if [ "$ARCH" = 'arm64' ]; then | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,6 @@ | |
set -E | ||
set -o pipefail | ||
trap "error_handler" ERR | ||
export -f "error_handler" | ||
# }}} | ||
|
||
# variables {{{ | ||
|
@@ -81,11 +80,6 @@ | |
[ -n "$VMSIZE" ] || VMSIZE="2G" | ||
[ -n "$GRUB_INSTALL" ] || GRUB_INSTALL='yes' | ||
|
||
# inside the chroot system locales might not be available, so use minimum: | ||
export LANG=C | ||
export LC_ALL=C | ||
export LANGUAGE=C | ||
|
||
# make sure interactive mode is only executed when | ||
# using an empty configuration file or option --interactive | ||
INTERACTIVE='' | ||
|
@@ -298,16 +292,16 @@ | |
# make sure nothing is left inside chroot so we can unmount it | ||
for service in ssh mdadm ; do | ||
if [ -x "${MNTPOINT}/etc/init.d/${service}" ] ; then | ||
chroot "$MNTPOINT" "/etc/init.d/${service}" stop | ||
clean_chroot "$MNTPOINT" "/etc/init.d/${service}" stop | ||
fi | ||
done | ||
|
||
[ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount -a >/dev/null 2>&1 || true | ||
[ -x "$MNTPOINT"/bin/umount ] && clean_chroot "$MNTPOINT" umount -a >/dev/null 2>&1 || true | ||
Check warning on line 299 in grml-debootstrap GitHub Actions / shellcheck grml-debootstrap
|
||
|
||
# ugly, but make sure we really don't leave anything (/proc /proc and | ||
# /dev /dev are intended, trying to work around timing issues, see #657023) | ||
for ARG in /run/udev /sys /proc /proc /dev/pts /dev/pts /dev /dev ; do | ||
[ -x "$MNTPOINT"/bin/umount ] && chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1 || true | ||
[ -x "$MNTPOINT"/bin/umount ] && clean_chroot "$MNTPOINT" umount $ARG >/dev/null 2>&1 || true | ||
Check warning on line 304 in grml-debootstrap GitHub Actions / shellcheck grml-debootstrap
|
||
umount "$MNTPOINT"/$ARG >/dev/null 2>&1 || true | ||
done | ||
|
||
|
@@ -821,7 +815,7 @@ | |
prompt_for_release() | ||
{ | ||
[ -n "$RELEASE" ] && DEFAULT_RELEASE="$RELEASE" || DEFAULT_RELEASE='bullseye' | ||
RELEASE="$(dialog --stdout --title "${PN}" --default-item $DEFAULT_RELEASE --menu \ | ||
Check warning on line 818 in grml-debootstrap GitHub Actions / shellcheck grml-debootstrap
Check failure on line 818 in grml-debootstrap GitHub Actions / shellcheck grml-debootstrap
|
||
"Please enter the Debian release you would like to use for installation:" \ | ||
0 50 8 \ | ||
buster Debian/10 \ | ||
|
@@ -873,7 +867,7 @@ | |
{ | ||
[ -n "$ISO" ] && DEFAULT_MIRROR='local' || DEFAULT_MIRROR='net' | ||
|
||
CHOOSE_MIRROR=$(dialog --stdout --title "$PN" --default-item $DEFAULT_MIRROR \ | ||
Check warning on line 870 in grml-debootstrap GitHub Actions / shellcheck grml-debootstrap
Check failure on line 870 in grml-debootstrap GitHub Actions / shellcheck grml-debootstrap
|
||
--menu "Where do you want to install from?" 0 0 0 \ | ||
net "install via network (downloading from mirror)" \ | ||
local "install from local directory/mirror" | ||
|
@@ -883,13 +877,13 @@ | |
[ -n "$MIRROR" ] || MIRROR='http://deb.debian.org/debian' | ||
MIRROR="$(dialog --stdout --title "${PN}" --inputbox \ | ||
"Please enter Debian mirror you would like to use for installing packages." \ | ||
0 0 $MIRROR)" || bailout | ||
Check warning on line 880 in grml-debootstrap GitHub Actions / shellcheck grml-debootstrap
|
||
|
||
else # CHOOSE_MIRROR == local | ||
[ -n "$ISO" ] || ISO='/mnt/mirror' | ||
ISO="$(dialog --stdout --title "${PN}" --inputbox \ | ||
"Please enter directory name you would like to use for installing packages." \ | ||
0 0 $ISO)" || bailout | ||
Check warning on line 886 in grml-debootstrap GitHub Actions / shellcheck grml-debootstrap
|
||
fi | ||
} | ||
# }}} | ||
|
@@ -1592,16 +1586,16 @@ | |
if [ -n "$ARM_EFI_TARGET" ]; then | ||
einfo "Installing Grub as bootloader into EFI." | ||
|
||
chroot "${MNTPOINT}" grub-install --target=arm64-efi --efi-directory=/boot/efi --bootloader-id=debian --recheck --no-nvram --removable | ||
clean_chroot "${MNTPOINT}" grub-install --target=arm64-efi --efi-directory=/boot/efi --bootloader-id=debian --recheck --no-nvram --removable | ||
# Has chroot-script installed GRUB to MBR using grub-install (successfully), already? | ||
# chroot-script skips installation for unset ${GRUB} | ||
elif [[ -z "${GRUB}" ]] || ! dd if="${GRUB}" bs=512 count=1 2>/dev/null | cat -v | grep -Fq GRUB; then | ||
einfo "Installing Grub as bootloader." | ||
|
||
if ! chroot "${MNTPOINT}" dpkg --list grub-pc 2>/dev/null | grep -q '^ii' ; then | ||
if ! clean_chroot "${MNTPOINT}" dpkg --list grub-pc 2>/dev/null | grep -q '^ii' ; then | ||
echo "Notice: grub-pc package not present yet, installing it therefore." | ||
# shellcheck disable=SC2086 | ||
DEBIAN_FRONTEND=$DEBIAN_FRONTEND chroot "$MNTPOINT" apt-get -y --no-install-recommends install $DPKG_OPTIONS grub-pc | ||
clean_chroot "$MNTPOINT" DEBIAN_FRONTEND=$DEBIAN_FRONTEND apt-get -y --no-install-recommends install $DPKG_OPTIONS grub-pc | ||
fi | ||
|
||
mkdir -p "${MNTPOINT}/boot/grub" | ||
|
@@ -1616,48 +1610,48 @@ | |
mkdir -p "${MNTPOINT}"/boot/efi | ||
mount -t vfat "${EFI_TARGET}" "${MNTPOINT}"/boot/efi | ||
|
||
if ! chroot "${MNTPOINT}" dpkg --list shim-signed 2>/dev/null | grep -q '^ii' ; then | ||
if ! clean_chroot "${MNTPOINT}" dpkg --list shim-signed 2>/dev/null | grep -q '^ii' ; then | ||
echo "Notice: shim-signed package not present yet, installing it therefore." | ||
# shellcheck disable=SC2086 | ||
DEBIAN_FRONTEND=$DEBIAN_FRONTEND chroot "$MNTPOINT" apt-get -y --no-install-recommends install $DPKG_OPTIONS shim-signed | ||
clean_chroot "$MNTPOINT" DEBIAN_FRONTEND=$DEBIAN_FRONTEND apt-get -y --no-install-recommends install $DPKG_OPTIONS shim-signed | ||
fi | ||
|
||
if [ "$(dpkg --print-architecture)" = "arm64" ]; then | ||
if ! chroot "${MNTPOINT}" dpkg --list grub-efi-arm64-signed 2>/dev/null | grep -q '^ii' ; then | ||
if ! clean_chroot "${MNTPOINT}" dpkg --list grub-efi-arm64-signed 2>/dev/null | grep -q '^ii' ; then | ||
echo "Notice: grub-efi-arm64-signed package not present yet, installing it therefore." | ||
# shellcheck disable=SC2086 | ||
DEBIAN_FRONTEND=$DEBIAN_FRONTEND chroot "$MNTPOINT" apt-get -y --no-install-recommends install $DPKG_OPTIONS grub-efi-arm64-bin grub-efi-arm64-signed | ||
clean_chroot "$MNTPOINT" DEBIAN_FRONTEND=$DEBIAN_FRONTEND apt-get -y --no-install-recommends install $DPKG_OPTIONS grub-efi-arm64-bin grub-efi-arm64-signed | ||
fi | ||
chroot "$MNTPOINT" grub-install --target=arm64-efi --efi-directory=/boot/efi --uefi-secure-boot --removable "/dev/$LOOP_DISK" | ||
clean_chroot "$MNTPOINT" grub-install --target=arm64-efi --efi-directory=/boot/efi --uefi-secure-boot --removable "/dev/$LOOP_DISK" | ||
elif [ "$(dpkg --print-architecture)" = "i386" ]; then | ||
if ! chroot "${MNTPOINT}" dpkg --list grub-efi-ia32-signed 2>/dev/null | grep -q '^ii' ; then | ||
if ! clean_chroot "${MNTPOINT}" dpkg --list grub-efi-ia32-signed 2>/dev/null | grep -q '^ii' ; then | ||
echo "Notice: grub-efi-ia32-signed package not present yet, installing it therefore." | ||
# shellcheck disable=SC2086 | ||
DEBIAN_FRONTEND=$DEBIAN_FRONTEND chroot "$MNTPOINT" apt-get -y --no-install-recommends install $DPKG_OPTIONS grub-efi-ia32-bin grub-efi-ia32-signed | ||
clean_chroot "$MNTPOINT" DEBIAN_FRONTEND=$DEBIAN_FRONTEND apt-get -y --no-install-recommends install $DPKG_OPTIONS grub-efi-ia32-bin grub-efi-ia32-signed | ||
fi | ||
chroot "$MNTPOINT" grub-install --target=i386-efi --efi-directory=/boot/efi --uefi-secure-boot --removable "/dev/$LOOP_DISK" | ||
chroot "$MNTPOINT" grub-install --target=i386-pc "/dev/$LOOP_DISK" | ||
clean_chroot "$MNTPOINT" grub-install --target=i386-efi --efi-directory=/boot/efi --uefi-secure-boot --removable "/dev/$LOOP_DISK" | ||
clean_chroot "$MNTPOINT" grub-install --target=i386-pc "/dev/$LOOP_DISK" | ||
else | ||
if ! chroot "${MNTPOINT}" dpkg --list grub-efi-amd64-signed 2>/dev/null | grep -q '^ii' ; then | ||
if ! clean_chroot "${MNTPOINT}" dpkg --list grub-efi-amd64-signed 2>/dev/null | grep -q '^ii' ; then | ||
echo "Notice: grub-efi-amd64-signed package not present yet, installing it therefore." | ||
# shellcheck disable=SC2086 | ||
DEBIAN_FRONTEND=$DEBIAN_FRONTEND chroot "$MNTPOINT" apt-get -y --no-install-recommends install $DPKG_OPTIONS grub-efi-amd64-bin grub-efi-amd64-signed | ||
clean_chroot "$MNTPOINT" DEBIAN_FRONTEND=$DEBIAN_FRONTEND apt-get -y --no-install-recommends install $DPKG_OPTIONS grub-efi-amd64-bin grub-efi-amd64-signed | ||
fi | ||
chroot "$MNTPOINT" grub-install --target=x86_64-efi --efi-directory=/boot/efi --uefi-secure-boot --removable "/dev/$LOOP_DISK" | ||
chroot "$MNTPOINT" grub-install --target=i386-pc "/dev/$LOOP_DISK" | ||
clean_chroot "$MNTPOINT" grub-install --target=x86_64-efi --efi-directory=/boot/efi --uefi-secure-boot --removable "/dev/$LOOP_DISK" | ||
clean_chroot "$MNTPOINT" grub-install --target=i386-pc "/dev/$LOOP_DISK" | ||
fi | ||
else | ||
dd if="${MNTPOINT}/usr/lib/grub/i386-pc/boot.img" of="${ORIG_TARGET}" conv=notrunc bs=440 count=1 | ||
case "${_opt_filesystem}" in | ||
f2fs) | ||
chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos f2fs | ||
clean_chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos f2fs | ||
;; | ||
xfs) | ||
chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos xfs | ||
clean_chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos xfs | ||
;; | ||
# NOTE - we might need to distinguish between further filesystems | ||
*) | ||
chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2 | ||
clean_chroot "${MNTPOINT}" grub-mkimage -O i386-pc -p "(hd0,msdos1)/boot/grub" -o /tmp/core.img biosdisk part_msdos ext2 | ||
;; | ||
esac | ||
|
||
|
@@ -1680,8 +1674,8 @@ | |
fi | ||
|
||
einfo "Updating grub configuration file." | ||
chroot "${MNTPOINT}" update-grub | ||
chroot "${MNTPOINT}" sync | ||
clean_chroot "${MNTPOINT}" update-grub | ||
clean_chroot "${MNTPOINT}" sync | ||
|
||
case "$RELEASE" in | ||
jessie) | ||
|
@@ -1759,6 +1753,46 @@ | |
} | ||
# }}} | ||
|
||
# clean_chroot {{{ | ||
clean_chroot() { | ||
# inside the chroot system locales might not be available, so use minimum: | ||
local -a env_vars=( | ||
"LANG=C" | ||
"LC_ALL=C" | ||
"LANGUAGE=C" | ||
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" | ||
) | ||
|
||
# List of additional environment variables to include if set | ||
local -a additional_vars=("http_proxy" "https_proxy" "ALL_PROXY") | ||
|
||
# Loop through the additional environment variables and add them if set | ||
for var in "${additional_vars[@]}"; do | ||
if [[ -n "${!var}" ]]; then | ||
env_vars+=("$var=${!var}") | ||
fi | ||
done | ||
|
||
# Capture additional environment variables passed as arguments. | ||
for var in "$@"; do | ||
if [[ "$var" == *=* ]]; then | ||
env_vars+=("$var") | ||
else | ||
break | ||
fi | ||
done | ||
|
||
# First argument is the chroot directory. | ||
local chroot_dir="$1" | ||
|
||
# Remaining arguments are for the command to be executed in the chroot environment. | ||
local chroot_command=("${@:2}") | ||
|
||
# Run chroot, then env -i with the specified environment variables inside the chroot | ||
chroot "$chroot_dir" /usr/bin/env -i "${env_vars[@]}" "${chroot_command[@]}" | ||
adrelanos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
# }}} | ||
|
||
# prepare chroot via chroot-script {{{ | ||
preparechroot() { | ||
einfo "Preparing chroot system" | ||
|
@@ -1816,12 +1850,12 @@ | |
[ -n "$TUNE2FS" ] && echo "TUNE2FS='${TUNE2FS//\'/\'\\\'\'}'" >> "$CHROOT_VARIABLES" | ||
[ -n "$VMSIZE" ] && echo "VMSIZE='${VMSIZE//\'/\'\\\'\'}'" >> "$CHROOT_VARIABLES" | ||
|
||
cp $VERBOSE "${CONFFILES}"/chroot-script "${MNTPOINT}"/bin/chroot-script | ||
Check warning on line 1853 in grml-debootstrap GitHub Actions / shellcheck grml-debootstrap
Check failure on line 1853 in grml-debootstrap GitHub Actions / shellcheck grml-debootstrap
|
||
chmod 755 "${MNTPOINT}"/bin/chroot-script | ||
[ -d "$MNTPOINT"/etc/debootstrap/ ] || mkdir "$MNTPOINT"/etc/debootstrap/ | ||
|
||
# make sure we have our files for later use via chroot-script | ||
cp $VERBOSE "${CONFFILES}/config" "${MNTPOINT}"/etc/debootstrap/ | ||
Check warning on line 1858 in grml-debootstrap GitHub Actions / shellcheck grml-debootstrap
Check failure on line 1858 in grml-debootstrap GitHub Actions / shellcheck grml-debootstrap
|
||
# make sure we adjust the configuration variables accordingly: | ||
sed -i "s#RELEASE=.*#RELEASE=\"$RELEASE\"#" "${MNTPOINT}"/etc/debootstrap/config | ||
sed -i "s#TARGET=.*#TARGET=\"$TARGET\"#" "${MNTPOINT}"/etc/debootstrap/config | ||
|
@@ -1840,20 +1874,20 @@ | |
PACKAGES_FILE="packages-arm64" | ||
fi | ||
|
||
cp $VERBOSE "${_opt_packages:-$CONFFILES/$PACKAGES_FILE}" \ | ||
Check warning on line 1877 in grml-debootstrap GitHub Actions / shellcheck grml-debootstrap
Check failure on line 1877 in grml-debootstrap GitHub Actions / shellcheck grml-debootstrap
|
||
"${MNTPOINT}/etc/debootstrap/${PACKAGES_FILE}" | ||
fi | ||
|
||
# debconf preseeding: | ||
_opt_debconf=${_opt_debconf:-$CONFFILES/debconf-selections} | ||
[ -f "${_opt_debconf}" ] && [ "$DEBCONF" = 'yes' ] && \ | ||
cp $VERBOSE "${_opt_debconf}" "${MNTPOINT}"/etc/debootstrap/debconf-selections | ||
Check failure on line 1884 in grml-debootstrap GitHub Actions / shellcheck grml-debootstrap
|
||
|
||
# copy scripts that should be executed inside the chroot: | ||
_opt_chroot_scripts=${_opt_chroot_scripts:-$CONFFILES/chroot-scripts/} | ||
[ -d "$_opt_chroot_scripts" ] && [ "$CHROOT_SCRIPTS" = 'yes' ] && { | ||
mkdir -p "${MNTPOINT}"/etc/debootstrap/chroot-scripts | ||
cp -a $VERBOSE "${_opt_chroot_scripts}"/* "${MNTPOINT}"/etc/debootstrap/chroot-scripts/ | ||
Check failure on line 1890 in grml-debootstrap GitHub Actions / shellcheck grml-debootstrap
|
||
} | ||
|
||
# notice: do NOT use $CHROOT_VARIABLES inside chroot but statically file instead! | ||
|
@@ -2052,9 +2086,9 @@ | |
mount -t devtmpfs udev "${MNTPOINT}"/dev | ||
mount -t devpts devpts "${MNTPOINT}"/dev/pts | ||
if [ "$DEBUG" = "true" ] ; then | ||
chroot "$MNTPOINT" /bin/bash -x /bin/chroot-script | ||
clean_chroot "$MNTPOINT" /bin/bash -x /bin/chroot-script | ||
else | ||
chroot "$MNTPOINT" /bin/chroot-script | ||
clean_chroot "$MNTPOINT" /bin/chroot-script | ||
fi | ||
try_umount 3 "$MNTPOINT"/dev/pts | ||
try_umount 3 "$MNTPOINT"/dev | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is removing the export of the error_handler from
grml-debootstrap
and addition of it to chroot script related to the environment cleanup, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you mentioned this in a comment as I just noticed:
We should squash all the relevant commits into one single commit and provide that useful information you collected into the commit message :)
Do you want me to give this a try or would you prefer to try on your own?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, please.