Skip to content

Instantly share code, notes, and snippets.

@elreydetoda
Created November 27, 2024 12:28
Show Gist options
  • Save elreydetoda/aeade9ac99d212919f3d88b57ab206d2 to your computer and use it in GitHub Desktop.
Save elreydetoda/aeade9ac99d212919f3d88b57ab206d2 to your computer and use it in GitHub Desktop.
TruNAS Scale Disk Mgmt
#!/usr/bin/env bash
# https://elrey.casa/bash/scripting/harden
set -${-//[sc]/}eu${DEBUG:+xv}o pipefail
function list_disks(){
lsblk -O --json |
jq -r '.blockdevices[] | select(.tran == "usb" or .tran == "scsi") | .children[] | "\(.label)\t\(.path)\t\(.size)\t\(.fstype)"'
}
function mount_disk(){
media_path="${TOP_FOLDER}/${d_name}"
sudo mkdir -p "${media_path}"
sudo mount "${path}" "${media_path}"
}
function prompt_user(){
until [ "${action:-}" == 'done' ]; do
PS3='Which disk do you want to mount? '
printf '\n%s\n' "Select which disk to mount:" >&2
# headers for disk output
printf 'Name,\tPath,\t\tSize,\tPartition Format\n' >&2
# https://stackoverflow.com/questions/46081687/how-to-make-select-display-the-options-in-a-single-column#46081970
COLUMNS=1
select action in "${DISK_OUTPUT[@]}"; do
d_name=$(cut -f 1 <<< "${action}")
path=$(cut -f 2 <<< "${action}")
d_type=$(cut -f 4 <<< "${action}")
printf 'You chose number %s, mounting %s\n' "${REPLY}" "${d_name}"
[[ "${action}" != 'done' ]] && mount_disk
break
done
done
unset PS3
}
function unmount_disks(){
echo; echo
sudo umount -v "${TOP_FOLDER}"/*
sudo rmdir -v "${TOP_FOLDER}"/*
exit 0
}
function main(){
[[ -z "${TMUX:-}" ]] && tmux && exit 0
TOP_FOLDER="/tmp/mounted_drives"
[[ "$(ls -1 "${TOP_FOLDER}" | wc -l)" -gt 0 ]] && read -n1 -r -p 'Unmount Disks? (y/N) ' UMOUNT_DISKS && [[ "$( tr '[:upper:]' '[:lower:]' <<< "${UMOUNT_DISKS}" )" == 'y' ]] && unmount_disks
mapfile -t DISK_OUTPUT < <(list_disks)
#printf '%s\n' "${DISK_OUTPUT[@]}"
DISK_OUTPUT+=('done')
#printf '%s\n' "${DISK_OUTPUT[@]}"
prompt_user
}
# https://elrey.casa/bash/scripting/main
if [[ "${0}" = "${BASH_SOURCE[0]:-bash}" ]] ; then
main "${@}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment