Skip to content

Commit

Permalink
General fixes to support busybox in mkinitcpio images
Browse files Browse the repository at this point in the history
Co-authored-by: Zach Dykstra <dykstra.zachary@gmail.com>
Co-authored-by: Andrew J. Hesford <ajh@sideband.org>
  • Loading branch information
ahesford and zdykstra committed Jan 27, 2022
1 parent 1fe8c4c commit 50a2da4
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 42 deletions.
108 changes: 69 additions & 39 deletions 90zfsbootmenu/bin/zfs-chroot
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
#!/bin/bash
# vim: softtabstop=2 shiftwidth=2 expandtab

_mnt=()

cleanup() {
for _fs in "${_mnt[@]}"; do
umount "${_fs}" || zerror "unable to unmount ${_fs}"
done

_mnt=()
trap - HUP INT QUIT ABRT EXIT
}

# shellcheck disable=SC1091
source /lib/profiling-lib.sh >/dev/null 2>&1
# shellcheck disable=SC1091
source /etc/zfsbootmenu.conf 2>&1 || exit 1
# shellcheck disable=SC1091
source /lib/kmsg-log-lib.sh >/dev/null 2>&1 || exit 1
# shellcheck disable=SC1091
source /lib/zfsbootmenu-core.sh >/dev/null 2>&1 || exit 1

selected="${1}"
Expand All @@ -13,45 +27,61 @@ zdebug "started with ${selected}"

[ -n "${selected}" ] || exit 0

if mountpoint="$( allow_rw=yes mount_zfs "${selected}" )"; then
zdebug "mounted ${selected} to ${mountpoint}"
mount -t proc proc "${mountpoint}/proc"
mount -t sysfs sys "${mountpoint}/sys"
mount -B /dev "${mountpoint}/dev"
mount -B /tmp "${mountpoint}/tmp"
mount -t devpts pts "${mountpoint}/dev/pts"

pool="${selected%%/*}"

# Snapshots and read-only pools always produce read-only mounts
if is_snapshot "${selected}" || ! is_writable "${pool}"; then
writemode="$( colorize green "read-only")"
else
writemode="$( colorize red "read/write")"
fi

_SHELL=
if [ -x "${mountpoint}/bin/bash" ] \
if ! mountpoint="$( allow_rw=yes mount_zfs "${selected}" )"; then
zerror "failed to mount ${selected}"
exit 1
fi

# Track submounts so we know how to clean up on exit
trap cleanup HUP INT QUIT ABRT EXIT
_mnt=( "${mountpoint}" )

zdebug "mounted ${selected} to ${mountpoint}"

mount -B /tmp "${mountpoint}/tmp" \
&& _mnt=( "${mountpoint}/tmp" "${_mnt[@]}" )

mount -t proc proc "${mountpoint}/proc" \
&& _mnt=( "${mountpoint}/proc" "${_mnt[@]}" )

mount -t sysfs sys "${mountpoint}/sys" \
&& _mnt=( "${mountpoint}/sys" "${_mnt[@]}" )

mount -B /dev "${mountpoint}/dev" \
&& _mnt=( "${mountpoint}/dev" "${_mnt[@]}" )

# Not all /dev filesystems have /dev/pts
[ -d "${mountpoint}/dev/pts" ] \
&& mount -t devpts pts "${mountpoint}/dev/pts" \
&& _mnt=( "${mountpoint}/dev/pts" "${_mnt[@]}" )

pool="${selected%%/*}"

# Snapshots and read-only pools always produce read-only mounts
if is_snapshot "${selected}" || ! is_writable "${pool}"; then
writemode="$( colorize green "read-only")"
else
writemode="$( colorize red "read/write")"
fi

_SHELL=
if [ -x "${mountpoint}/bin/bash" ] \
&& chroot "${mountpoint}" /bin/bash -c "exit 0" >/dev/null 2>&1 ; then
_SHELL="/bin/bash"
chroot_extra="--norc"
elif [ -x "${mountpoint}/bin/sh" ] \
_SHELL="/bin/bash"
chroot_extra="--norc"
elif [ -x "${mountpoint}/bin/sh" ] \
&& chroot "${mountpoint}" /bin/sh -c "exit 0" >/dev/null 2>&1 ; then
_SHELL="/bin/sh"
else
zerror "unable to test execute a shell in ${selected}"
fi

if [ -n "${_SHELL}" ]; then
echo -e "$( colorize orange "${selected}") is mounted ${writemode}, /tmp is shared and read/write\n"

# regardless of shell, set PS1
if ! env "PS1=$( colorize orange "${selected}") \w > " chroot "${mountpoint}" "${_SHELL}" "${chroot_extra}" ; then
zdebug "chroot ${selected}:${_SHELL} returned code $?"
fi
fi

if ! umount -R "${mountpoint}"; then
zerror "unable to unmount ${mountpoint}"
fi
_SHELL="/bin/sh"
fi

if [ -z "${_SHELL}" ]; then
zerror "unable to test execute a shell in ${selected}"
exit 1
fi

echo -e "$( colorize orange "${selected}") is mounted ${writemode}, /tmp is shared and read/write\n"

# regardless of shell, set PS1
if ! env "PS1=$( colorize orange "${selected}") \w > " chroot "${mountpoint}" "${_SHELL}" "${chroot_extra}" ; then
zdebug "chroot ${selected}:${_SHELL} returned code $?"
fi
9 changes: 7 additions & 2 deletions 90zfsbootmenu/bin/zlogtail
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ fuzzy_default_options+=(

export FZF_DEFAULT_OPTS="${fuzzy_default_options[*]}"

# shellcheck disable=SC2086
dmesg --notime -f user,daemon -l err,warn | ${FUZZYSEL}
# Try to use feature flags found on dmesg from util-linux
if output="$( dmesg --notime -f user -l err,warn 2>/dev/null )" ; then
echo "${output}" | ${FUZZYSEL}
else
# fall back to manually parsing dmesg output; will show all ZBM generated logs up to zinfo level
dmesg | awk '/ZFSBootMenu:/{ for (i=3; i<=NF; i++){ printf("%s ", $i)}; printf "\n" }' | ${FUZZYSEL}
fi
6 changes: 5 additions & 1 deletion 90zfsbootmenu/bin/ztrace
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/bin/bash
#shellcheck disable=SC2086

# Unusable for now with busybox-provided dmesg/less
if ! dmesg -T --time-format reltime -f user -l 7 >/dev/null 2>&1 ; then
exit 0
fi

r="\033[0;31m"
g="\033[0;32m"
Expand Down

0 comments on commit 50a2da4

Please sign in to comment.