Created
September 28, 2022 02:54
-
-
Save troian/5c6562d88568c42982032a4f789a8cdc to your computer and use it in GitHub Desktop.
unraid vdisk mount
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -x | |
if [[ $# -ne 2 ]]; then | |
echo "invalid amount of args" | |
fi | |
if [[ ! -f "$2" ]]; then | |
/usr/local/emhttp/webGui/scripts/notify -e "Unraid Server Notice" -s "Vdisk mount script" -d "config file does not exists $config_file" -i "alert" | |
exit 1 | |
fi | |
config_file=$(cat "$2") | |
partitions=$(echo "$config_file" | jq -Mr '.disks[].partitions | length') | |
echo "set nbd max_part=$partitions" | |
modprobe nbd max_part=$partitions | |
function notification_mount { | |
/usr/local/emhttp/webGui/scripts/notify -e "Unraid Server Notice" -s "Vdisk mount script" -d "$1 vdisk mounted" -i "normal" | |
} | |
function notification_vdisk_notfound { | |
/usr/local/emhttp/webGui/scripts/notify -e "Unraid Server Notice" -s "Vdisk mount script" -d "$1 not found" -i "alert" | |
} | |
function notification_mount_exists { | |
/usr/local/emhttp/webGui/scripts/notify -e "Unraid Server Notice" -s "Vdisk mount script" -d "mount $1 exists" -i "alert" | |
} | |
function do_mount { | |
local i=-1 | |
while read name vdisk; do | |
i=$((i + 1)) | |
if [[ ! -f $vdisk ]]; then | |
echo "vdisk file $vdisk not found" | |
continue | |
fi | |
dev=/dev/nbd$i | |
# connect vdisk to /dev/nbdX (x equaling a number) | |
qemu-nbd --format=$(qemu-img info $vdisk --output=json | jq -Mr '.format') --connect=$dev "$vdisk" | |
while IFS="," read pname pmount; do | |
# check mount point exists | |
if [[ -d $pmount ]]; then | |
if mount | awk -v pmount=$pmount '{if ($3 == pmount) { exit 0}} ENDFILE{exit -1}'; then | |
echo "mount $pmount is already mounted" | |
notification_mount_exists $pmount | |
continue | |
fi | |
else | |
echo "creating mount point $pmount" | |
mkdir -p $pmount | |
fi | |
mount $dev$pname "$pmount" | |
done <<< $(echo "$config_file" | jq --arg idx 0 -Mr '.disks[$idx|tonumber].partitions[] | [.name, .mount] | @csv' | tr -d '"') | |
done <<< $(echo "$config_file" | jq -Mr '.disks[] | [.name, .image] | @tsv') | |
} | |
function do_umount { | |
local i=-1 | |
while read name vdisk; do | |
i=$((i + 1)) | |
dev=/dev/nbd$i | |
while IFS="," read pname pmount; do | |
# check mount point exists | |
if [[ -d $pmount ]]; then | |
if mount | awk -v pmount=$pmount '{if ($3 == pmount) { exit 0}} ENDFILE{exit -1}'; then | |
echo "mount $pmount is mounted. unmounting" | |
umount $pmount | |
rm -r $pmount | |
fi | |
fi | |
done <<< $(echo "$config_file" | jq --arg idx 0 -Mr '.disks[$idx|tonumber].partitions[] | [.name, .mount] | @csv' | tr -d '"') | |
qemu-nbd --disconnect $dev | |
done <<< $(echo "$config_file" | jq -Mr '.disks[] | [.name, .image] | @tsv') | |
} | |
case "$1" in | |
mount) | |
do_mount | |
;; | |
umount) | |
do_umount | |
;; | |
*) | |
echo "unknown command $1" | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment