Skip to content

Commit

Permalink
- command-working: support --sudo
Browse files Browse the repository at this point in the history
- fs-speed: dd SHOULD NOT be used for filesystem performance, using `fio` instead
- get-devices: register btrfs devices if any btrfs device is found
- gocryptfs-helper: remove `fs-speed` in migration, remove outdated `sharebox` reference
- is-missing: return 1 if no paths were missing
- mount-helper: if there are btrfs error and we are unmounting, continue, this worksaround rpi5 usb 3 flakiness, as `usbreset` has no effect: raspberrypi/linux#5753 (comment)
- setup-util: check for apt package installation before uninstalling, support --sudo, properly support --no-xdg
- add `setup-util-rpi-update`

Signed-off-by: Benjamin Lupton <[email protected]>
  • Loading branch information
balupton committed Feb 14, 2024
1 parent 7e96e9b commit 253ec21
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 102 deletions.
14 changes: 11 additions & 3 deletions commands/command-working
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ function command_working() (
Working check is done via [<command> --help] and [<command> --version] checks.
USAGE:
command-working [--] ...<command>
command-working [...options] [--] ...<command>
OPTIONS:
--sudo
Test the command with sudo.
RETURNS:
[0] if all commands are working
Expand All @@ -26,12 +30,15 @@ function command_working() (
}

# process
local item commands=()
local item commands=() option_sudo='no'
while test "$#" -ne 0; do
item="$1"
shift
case "$item" in
'--help' | '-h') help ;;
'--no-sudo'* | '--sudo'*)
option_sudo="$(get-flag-value --affirmative --fallback="$option_sudo" -- "$item")"
;;
'--')
commands+=("$@")
shift "$#"
Expand All @@ -57,11 +64,12 @@ function command_working() (
sshd
teip
trunk
rpi-update
)
function check_status {
local cmd=() command_exit_status
# ensure sbin commands work
if [[ $* == *sbin* ]]; then
if [[ $* == *sbin* ]] || test "$option_sudo" = 'yes'; then
cmd+=(
'sudo-helper'
'--reason=Your sudo/root/login password is required to verify this command is available and working:'
Expand Down
158 changes: 107 additions & 51 deletions commands/fs-speed
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ function fs_speed() (
Calculate the speed at a directory path.
USAGE:
fs-speed [--] ...<path>
fs-speed [...options] [--] ...<path>
OPTIONS:
--user=<user>
--group=<group>
If specified run the removal commands as this <user> and <group>.
EOF
if test "$#" -ne 0; then
echo-error "$@"
Expand All @@ -25,13 +30,17 @@ function fs_speed() (
}

# process
local item option_paths=()
local item option_quiet option_paths=() option_user='' option_group=''
option_quiet="$(echo-quiet-enabled -- "$@")"
while test "$#" -ne 0; do
item="$1"
shift
case "$item" in
'--help' | '-h') help ;;
'--no-quiet'* | '--quiet'* | '--no-verbose'* | '--verbose'*) ;; # handled by echo-quiet-enabled
'--path='*) option_paths+=("${item#*=}") ;;
'--user='*) option_user="${item#*=}" ;;
'--group='*) option_group="${item#*=}" ;;
'--')
option_paths+=("$@")
shift $#
Expand All @@ -47,63 +56,33 @@ function fs_speed() (
help 'No <path>s provided.'
fi

# =====================================
# Dependencies

source "$DOROTHY/sources/ripgrep.bash"

# =====================================
# Action

# prepare
local bs megabytes_in_gigabyte=1024
if is-mac; then
bs="1m" # mac only
else
bs="1M" # linux only
fi
# is broken: https://github.com/axboe/fio/blob/master/examples/fio-seq-read.fio
# is broken: https://github.com/axboe/fio/blob/master/examples/fio-seq-write.fio

function eval_wrapper {
while test "$1" = '--'; do
shift
done
if test -n "$option_user" -o -n "$option_group"; then
sudo-helper --inherit --user="$option_user" --group="$option_group" -- "$@"
else
eval-helper --no-quiet -- "$@"
fi
}

local scratchpad log bytes path
local scratchpad log bytes megabytes speed path
for path in "${option_paths[@]}"; do
# start
echo-segment --h1="Speed Test: $path"

# prepare a temporary file in the path we wish to speed test
scratchpad="$(fs-temp --root="$path" --file)"

# prepare a temporary file in our temp directory, to store the output
# which we use for the speed analysis
log="$(fs-temp --directory='fs-speed' --file="$(basename "$scratchpad").log")"

# ensure they are empty
fs-rm --quiet --no-confirm -- "$scratchpad" "$log"

# @todo add some type of DEBUG=yes flag to avoid helpers, so we can still see block and time per block calculations

# write performance
eval-helper --quiet \
--pending="$(echo-style --bold="Calculating write performance...")" \
--success="$(echo-style --success="Calculated write performance.")" \
--failure="$(echo-style --error="Failed calculate write.")" \
-- dd if=/dev/random of="$scratchpad" bs="$bs" count="$megabytes_in_gigabyte" |& tee "$log"
bytes="$(rg --only-matching --regexp='(\d+) bytes/sec' --replace='$1' <"$log" || :)"
if test -n "$bytes"; then
echo-style "Writes at " --bold="$((bytes / 1024 / 1024)) megabytes" " a second."
fi

# read performance
eval-helper --quiet \
--pending="$(echo-style --bold="Calculating read performance...")" \
--success="$(echo-style --success="Calculated read performance.")" \
--failure="$(echo-style --error="Failed calculate read.")" \
-- dd if="$scratchpad" of=/dev/null bs="$bs" |& tee "$log"
bytes="$(rg --only-matching --regexp='(\d+) bytes/sec' --replace='$1' <"$log" || :)"
if test -n "$bytes"; then
echo-style "Reads at " --bold="$((bytes / 1024 / 1024)) megabytes" " a second."
fi

# cleanup
fs-rm --quiet --no-confirm -- "$scratchpad" "$log"
# https://raw.githubusercontent.com/axboe/fio/master/examples/fio-seq-RW.fio
cd "$path"
fs-rm --quiet --no-confirm --user="$option_user" --group="$option_group" -- ./fio-seq-RW
eval_wrapper -- fio --name=fio-seq-RW --filename=fio-seq-RW --rw=rw --rwmixread=60 --rwmixwrite=40 --bs=256K --direct=0 --numjobs=2 --time_based=1 --runtime=90 --size=1G --ioengine=libaio --iodepth=16
fs-rm --quiet --no-confirm --user="$option_user" --group="$option_group" -- ./fio-seq-RW

# done
echo-segment --g1="Speed Test: $path"
Expand All @@ -114,3 +93,80 @@ function fs_speed() (
if test "$0" = "${BASH_SOURCE[0]}"; then
fs_speed "$@"
fi


# # prepare
# # https://unix.stackexchange.com/a/121888/50703 <-- DO NOT USE DD
# # https://stackoverflow.com/a/50882704/130638
# # https://unix.stackexchange.com/a/324210/50703
# # https://stackoverflow.com/a/29935167/130638 <--- DO NOT USE dd
# local bs size=2 count=5
# if is-mac; then
# bs="${size}g" # mac only
# else
# bs="${size}G" # linux only
# fi


# local scratchpad log bytes megabytes speed path
# for path in "${option_paths[@]}"; do
# # start
# echo-segment --h1="Speed Test: $path"

# # is broken: https://github.com/axboe/fio/blob/master/examples/fio-seq-read.fio
# # is broken: https://github.com/axboe/fio/blob/master/examples/fio-seq-write.fio
# # works but doesn't allow over-rides: https://raw.githubusercontent.com/axboe/fio/master/examples/fio-seq-RW.fio
# # cd "$path"
# # fio --name=fio-seq-RW --filename=fio-seq-RW --rw=rw --rwmixread=60 --rwmixwrite=40 --bs=256K --direct=0 --numjobs=4 --time_based=1 --runtime=90 --size=10G --ioengine=libaio --iodepth=16
# # fs-rm --quiet --no-confirm -- './fio-seq-RW'

# # prepare a temporary file in the path we wish to speed test
# scratchpad="$(fs-temp --root="$path" --file)"

# # prepare a temporary file in our temp directory, to store the output
# # which we use for the speed analysis
# log="$(fs-temp --directory='fs-speed' --file="$(basename "$scratchpad").log")"

# # ensure they are empty
# fs-rm --quiet --no-confirm -- "$scratchpad" "$log"

# # write performance
# eval-helper --quiet="$option_quiet" \
# --pending="$(echo-style --bold='Calculating write performance...')" \
# --success="$(echo-style --success='Calculated write performance.')" \
# --failure="$(echo-style --error='Failed calculate write.')" \
# -- dd if=/dev/urandom of="$scratchpad" bs="$bs" count="$count" |& tee "$log"
# bytes="$(rg --only-matching --regexp='(\d+) bytes/sec' --replace='$1' <"$log" || :)"
# if test -n "$bytes"; then
# megabytes="$(echo-math --precision=0 -- "$bytes / 1024 / 1024")"
# echo-style 'Writes at ' --bold="$megabytes megabytes" ' a second.'
# else
# speed="$(rg --only-matching --regexp='(\d+ [a-zA-Z]+)/s' --replace='$1' <"$log" || :)"
# if test -n "$speed"; then
# echo-style 'Writes at ' --bold="$speed" ' a second.'
# fi
# fi

# # read performance
# eval-helper --quiet="$option_quiet" \
# --pending="$(echo-style --bold='Calculating read performance...')" \
# --success="$(echo-style --success='Calculated read performance.')" \
# --failure="$(echo-style --error='Failed calculate read.')" \
# -- dd if="$scratchpad" of=/dev/zero bs="$bs" |& tee "$log"
# bytes="$(rg --only-matching --regexp='(\d+) bytes/sec' --replace='$1' <"$log" || :)"
# if test -n "$bytes"; then
# megabytes="$(echo-math --precision=0 -- "$bytes / 1024 / 1024")"
# echo-style 'Reads at ' --bold="$megabytes megabytes" ' a second.'
# else
# speed="$(rg --only-matching --regexp='(\d+ [a-zA-Z]+)/s' --replace='$1' <"$log" || :)"
# if test -n "$speed"; then
# echo-style 'Reads at ' --bold="$speed" ' a second.'
# fi
# fi

# # cleanup
# fs-rm --quiet --no-confirm -- "$scratchpad" "$log"

# # done
# echo-segment --g1="Speed Test: $path"
# done
3 changes: 2 additions & 1 deletion commands/get-devices
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,12 @@ function get_devices() (
local MOUNT_HAYSTACK='' MOUNT_RAID=''
function get_all {
MOUNT_HAYSTACK="$(mount)"
if test "$option_filesystem" = 'btrfs'; then
if test "$option_filesystem" = 'btrfs' || grep --quiet --fixed-strings --regexp='btrfs' <<< "$MOUNT_HAYSTACK"; then
# if you attach a btrfs cluster to a new machine
# it may not be completely discovered until the btrfs agent scans for the devices
# and btrfs supports mounting partial filesystems, which cause inumerable errors
eval-helper --quiet -- btrfs-helper discover >"$tty_target"
MOUNT_HAYSTACK="$(mount)"
fi
if is-mac; then
MOUNT_RAID="$(sudo-helper -- diskutil appleRAID list)"
Expand Down
7 changes: 0 additions & 7 deletions commands/gocryptfs-helper
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,6 @@ function gocryptfs_helper() (
act_mount "$old_vault" "$old_plain"
act_mount "$new_vault" "$new_plain"

# speed
if confirm --positive --ppid=$$ -- "Run a speed comparison?"; then
fs-speed -- "$old_plain" "$new_plain"
fi

# migrate
cpr --remove --tool=rsync -- \
"${old_plain}/" \
Expand Down Expand Up @@ -419,8 +414,6 @@ function gocryptfs_helper() (
# done
cat <<-EOF
$(echo-style --success="Migration complete. ✅")
When you are ready to start the sharebox with the migrated vault, run:
$(echo-style --code="sharebox start")
EOF
}

Expand Down
12 changes: 6 additions & 6 deletions commands/is-missing
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ function is_missing() (
function help {
cat <<-EOF >/dev/stderr
ABOUT:
Check if any <path> is missing (not a file/directory/symlink).
Check if all <path>s are missing (not a file/directory/symlink).
Opposite of [is-present].
USAGE:
is-missing [--] ...<path>
RETURNS:
[0] if ANY <path>s were missing
[1] if no <path>s were missing
[0] if all <path>s were missing
[1] if any <path>s were missing
EOF
if test "$#" -ne 0; then
echo-error "$@"
Expand Down Expand Up @@ -53,11 +53,11 @@ function is_missing() (
local input
for input in "${option_inputs[@]}"; do
# just -e is faulty, as -e fails on broken symlinks
if test ! -e "$input" -a ! -L "$input"; then
return 0
if test -e "$input" -o -L "$input"; then
return 1
fi
done
return 1
return 0
)

# fire if invoked standalone
Expand Down
6 changes: 5 additions & 1 deletion commands/mount-helper
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,11 @@ function mount_helper() (
echo-style --success='Validated' ' ' --invert="$option_share" ' ' --invert="$mount_type" ' ' --invert="$option_label" ' ' --invert="$option_count"
else
echo-style --error='Failed to validate' ' ' --invert="$option_share" ' ' --invert="$mount_type" ' ' --invert="$option_label" ' ' --invert="$option_count"
return 5 # EIO 5 Input/output error
if [[ $option_actions == *'[unmount]'* ]]; then
echo-style --notice='As the goal is to unmount, we are continuing...'
else
return 5 # EIO 5 Input/output error
fi
fi

# coerce source
Expand Down
Loading

0 comments on commit 253ec21

Please sign in to comment.