Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions 90zfsbootmenu/zfs-chroot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ if mountpoint="$( allow_rw=yes mount_zfs "${selected}" )"; then

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

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

if [ -x "${mountpoint}/bin/bash" ]; then
_SHELL="/bin/bash"
Expand Down
43 changes: 8 additions & 35 deletions 90zfsbootmenu/zfsbootmenu-help.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/bin/bash
# vim: softtabstop=2 shiftwidth=2 expandtab

# shellcheck disable=SC1091
[ -r /lib/zfsbootmenu-lib.sh ] && source /lib/zfsbootmenu-lib.sh

# zfsbootmenu-help invokes itself, so the value of $WIDTH depends
# on if $0 is launching fzf/sk (-L) or is being launched inside
# fzf/sk (-s).
Expand All @@ -10,36 +13,6 @@ PREVIEW_SIZE="$(( WIDTH - 26 ))"

[ -z "${FUZZYSEL}" ] && FUZZYSEL="fzf"

center() {
printf "%*s" $(( (${#1} + WIDTH ) / 2)) "${1}"
}

colorize() {
color="${1}"
shift
case "${color}" in
black) echo -e -n '\033[0;30m' ;;
red) echo -e -n '\033[0;31m' ;;
green) echo -e -n '\033[0;32m' ;;
orange) echo -e -n '\033[0;33m' ;;
blue) echo -e -n '\033[0;34m' ;;
magenta) echo -e -n '\033[0;35m' ;;
cyan) echo -e -n '\033[0;36m' ;;
lightgray) echo -e -n '\033[0;37m' ;;
darkgray) echo -e -n '\033[1;30m' ;;
lightred) echo -e -n '\033[1;31m' ;;
lightgreen) echo -e -n '\033[1;32m' ;;
yellow) echo -e -n '\033[1;33m' ;;
lightblue) echo -e -n '\033[1;34m' ;;
lightmagenta) echo -e -n '\033[1;35m' ;;
lightcyan) echo -e -n '\033[1;36m' ;;
white) echo -e -n '\033[1;37m' ;;
*) echo -e -n '\033[0m' ;;
esac
echo -e -n "$@"
echo -e -n '\033[0m'
}

mod_header() {
local key="$1"
local subject="$2"
Expand Down Expand Up @@ -74,7 +47,7 @@ help_pager() {

# shellcheck disable=SC2034
read -r -d '' MAIN <<EOF
$( colorize magenta "$( center "Main Menu")" )
$( colorize magenta "$( center_string "Main Menu")" )
$( colorize lightblue "boot" )
$( colorize green "[ENTER]" )

Expand Down Expand Up @@ -145,7 +118,7 @@ SECTIONS+=("MAIN Main Menu")

# shellcheck disable=SC2034
read -r -d '' SNAPSHOT <<EOF
$( colorize magenta "$( center "Snapshot Management")" )
$( colorize magenta "$( center_string "Snapshot Management")" )
$( colorize lightblue "duplicate" )
$( colorize green "[ENTER]" )

Expand Down Expand Up @@ -205,7 +178,7 @@ SECTIONS+=("SNAPSHOT Snapshot Management")

# shellcheck disable=SC2034
read -r -d '' KERNEL <<EOF
$( colorize magenta "$( center "Kernel Management")" )
$( colorize magenta "$( center_string "Kernel Management")" )
$( colorize lightblue "boot" )
$( colorize green "[ENTER]" )

Expand All @@ -225,7 +198,7 @@ SECTIONS+=("KERNEL Kernel Management")

# shellcheck disable=SC2034
read -r -d '' DIFF <<EOF
$( colorize magenta "$( center "Diff Viewer")" )
$( colorize magenta "$( center_string "Diff Viewer")" )
$( colorize lightblue "Column 1 descriptions" )
$( colorize orange "-") The path has been removed
$( colorize orange "+") The path has been created
Expand All @@ -248,7 +221,7 @@ SECTIONS+=("DIFF Diff Viewer")

# shellcheck disable=SC2034
read -r -d '' POOL <<EOF
$( colorize magenta "$( center "ZPOOL Health")" )
$( colorize magenta "$( center_string "ZPOOL Health")" )
$( mod_header R "rewind checkpoint" )

If a pool checkpoint is available, the selected pool is exported and then imported with the $( colorize red "--rewind-to-checkpoint" ) flag set.
Expand Down
67 changes: 63 additions & 4 deletions 90zfsbootmenu/zfsbootmenu-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,59 @@ zerror() {
zlog 3 "$@"
}

# arg1: color name
# arg2...argN: text to color
# prints: text with color escape codes
# returns: nothing

colorize() {
local color
color="${1}"
shift
case "${color}" in
black) echo -e -n '\033[0;30m' ;;
red) echo -e -n '\033[0;31m' ;;
green) echo -e -n '\033[0;32m' ;;
orange) echo -e -n '\033[0;33m' ;;
blue) echo -e -n '\033[0;34m' ;;
magenta) echo -e -n '\033[0;35m' ;;
cyan) echo -e -n '\033[0;36m' ;;
lightgray) echo -e -n '\033[0;37m' ;;
darkgray) echo -e -n '\033[1;30m' ;;
lightred) echo -e -n '\033[1;31m' ;;
lightgreen) echo -e -n '\033[1;32m' ;;
yellow) echo -e -n '\033[1;33m' ;;
lightblue) echo -e -n '\033[1;34m' ;;
lightmagenta) echo -e -n '\033[1;35m' ;;
lightcyan) echo -e -n '\033[1;36m' ;;
white) echo -e -n '\033[1;37m' ;;
*) echo -e -n '\033[0m' ;;
esac
echo -e -n "$@"
echo -e -n '\033[0m'
}

# arg1: text to center
# prints: left-padded text
# returns: nothing

# Accepted environment variables:
# WIDTH: pre-calculated screen width

center_string() {
local _WIDTH
if [ -z "${WIDTH}" ]; then
if [ -z "${FZF_PREVIEW_COLUMNS}" ]; then
_WIDTH="$( tput cols )"
else
_WIDTH="${FZF_PREVIEW_COLUMNS}"
fi
else
_WIDTH="${WIDTH}"
fi
printf "%*s" $(( (${#1} + _WIDTH ) / 2)) "${1}"
}

# arg1: ZFS filesystem name
# prints: mountpoint
# returns: 0 on success
Expand Down Expand Up @@ -211,7 +264,7 @@ draw_be() {
if ! selected="$( ${FUZZYSEL} -0 --prompt "BE > " \
${expects} ${expects//alt-/ctrl-} ${expects//alt-/ctrl-alt-} \
--header="${header}" --preview-window="up:${PREVIEW_HEIGHT}" \
--preview="/libexec/zfsbootmenu-preview ${BASE} {} ${BOOTFS}" < "${env}" )"; then
--preview="/libexec/zfsbootmenu-preview {} ${BOOTFS}" < "${env}" )"; then
return 1
fi

Expand Down Expand Up @@ -245,7 +298,7 @@ draw_kernel() {
if ! selected="$( HELP_SECTION=KERNEL ${FUZZYSEL} \
--prompt "${benv} > " --tac --with-nth=2 --header="${header}" \
${expects} ${expects//alt-/ctrl-} ${expects//alt-/ctrl-alt-} \
--preview="/libexec/zfsbootmenu-preview ${BASE} ${benv} ${BOOTFS}" \
--preview="/libexec/zfsbootmenu-preview ${benv} ${BOOTFS}" \
--preview-window="up:${PREVIEW_HEIGHT}" < "${_kernels}" )"; then
return 1
fi
Expand Down Expand Up @@ -282,7 +335,7 @@ draw_snapshots() {
HELP_SECTION=SNAPSHOT ${FUZZYSEL} \
--prompt "Snapshot > " --header="${header}" --tac \
${expects} ${expects//alt-/ctrl-} ${expects//alt-/ctrl-alt-} \
--preview="/libexec/zfsbootmenu-preview ${BASE} ${benv} ${BOOTFS}" \
--preview="/libexec/zfsbootmenu-preview ${benv} ${BOOTFS}" \
--preview-window="up:${PREVIEW_HEIGHT}" )"; then
return 1
fi
Expand Down Expand Up @@ -328,7 +381,7 @@ draw_diff() {
( zfs diff -F -H "${snapshot}" "${diff_target}" & echo $! >&3 ) 3>/tmp/diff.pid | \
sed "s,${mnt},," | \
HELP_SECTION=DIFF ${FUZZYSEL} --prompt "${snapshot} > " \
--preview="/libexec/zfsbootmenu-preview ${BASE} ${diff_target} ${BOOTFS}" \
--preview="/libexec/zfsbootmenu-preview ${diff_target} ${BOOTFS}" \
--preview-window="up:${PREVIEW_HEIGHT}" \
--bind 'esc:execute-silent( kill $( cat /tmp/diff.pid ) )+abort'

Expand Down Expand Up @@ -880,6 +933,12 @@ load_be_cmdline() {
# prints: nothing
# returns: 0 on success, 1 on failure

# Accepted environment variables
# force_import=1: enable force importing of a pool
# read_write=1: import read-write, defaults to read-only
# rewind_to_checkpoint=1: enable --rewind-to-checkpoint
# all_pools=1: import all pools instead of a single specific pool

import_pool() {
local pool import_args

Expand Down
56 changes: 18 additions & 38 deletions 90zfsbootmenu/zfsbootmenu-preview.sh
Original file line number Diff line number Diff line change
@@ -1,56 +1,36 @@
#!/bin/bash
# vim: softtabstop=2 shiftwidth=2 expandtab

BASE="${1}"
ENV="${2}"
BOOTFS="${3}"

# shellcheck disable=SC2034
BLUE='\033[0;34m'
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# shellcheck disable=SC1091
[ -r /lib/zfsbootmenu-lib.sh ] && source /lib/zfsbootmenu-lib.sh

while IFS= read -r line
do
selected_kernel="${line}"
done < "${BASE}/${ENV}/default_kernel"
ENV="${1}"
BOOTFS="${2}"

BE_ARGS="$( load_be_cmdline "${ENV}" )"

while IFS= read -r line
do
selected_arguments="${line}"
done <<< "${BE_ARGS}"
# shellcheck disable=SC2034
IFS=' ' read -r _fs selected_kernel _initramfs <<<"$( select_kernel "${ENV}")"
selected_kernel="${selected_kernel##*/}"

pool="${ENV%%/*}"
readonly_prop="$( zpool get -H -o value readonly "${pool}" )"
[[ ${readonly_prop} = "on" ]] && _readonly="r/o" || _readonly="r/w"

if [ "${BOOTFS}" = "${ENV}" ]; then
selected_env_str="${ENV} (default, ${_readonly}) - ${selected_kernel}"
if is_writable "${pool}" ; then
_readonly="r/w"
_COLOR="red"
else
selected_env_str="${ENV} (${_readonly}) - ${selected_kernel}"
_readonly="r/o"
_COLOR="green"
fi

if [ -z "${FZF_PREVIEW_COLUMNS}" ]
then
WIDTH="$( tput cols )"
if [ "${BOOTFS}" = "${ENV}" ]; then
_DEFAULT="default, "
else
WIDTH="${FZF_PREVIEW_COLUMNS}"
_DEFAULT=""
fi

# Left pad the strings to center them based on the preview width
selected_env_str="$( printf "%*s\n" $(( (${#selected_env_str} + WIDTH ) / 2)) "${selected_env_str}" )"
selected_arguments="$( printf "%*s\n" $(( (${#selected_arguments} + WIDTH ) / 2)) "${selected_arguments}" )"
selected_env_str="$( center_string "${ENV} (${_DEFAULT}${_readonly}) - ${selected_kernel}" )"

if [ "${_readonly}" = "r/o" ]; then
echo -e "${GREEN}${selected_env_str}${NC}"
else
echo -e "${RED}${selected_env_str}${NC}"
fi
selected_arguments="$( load_be_cmdline "${ENV}" )"
selected_arguments="$( center_string "$( load_be_cmdline "${ENV}" )" )"

# colorize doesn't automatically add a newline
colorize "${_COLOR}" "${selected_env_str}\n"
echo "${selected_arguments}"