Files
hartman-server/script-drives-add
2021-10-21 14:57:49 -04:00

58 lines
1.4 KiB
Bash

#!/usr/bin/env bash
# Add a drive to the system
# TODO generalize
# Copyright 2021 Bryan C. Roessler
[[ -f functions ]] && . functions || exit 1
is_root
unset drive mountpoint
[[ $# -eq 0 ]] && lsblk -f && echo "Must provide a drive identifier" && exit 1
[[ $# -ge 1 ]] && drive="$1"
[[ $# -eq 2 ]] && mountpoint="$2"
[[ $# -gt 2 ]] && echo "Too many arguments passed" && exit 1
! info=$(lsblk -n -o NAME,FSTYPE,LABEL,UUID,MOUNTPOINT "$drive" | tr -s ' ') && echo "Drive $drive does not exist" && exit 1
name=$(cut -d' ' -f1 <<< "$info")
fstype=$(cut -d' ' -f2 <<< "$info")
label=$(cut -d' ' -f3 <<< "$info")
uuid=$(cut -d' ' -f4 <<< "$info")
mountedpoint=$(cut -d' ' -f5 <<< "$info")
if [[ -d "$mountedpoint" ]]; then
echo "Drive $drive is already mounted at $mountedpoint"
ask_ok "OK to unmount?" || exit $?
umount "$mountedpoint" || exit $?
fi
if [[ ! -v mountpoint ]]; then
label=$(cut -d' ' -f3 <<< "$info")
if [[ "$label" == "" ]]; then
echo "No mount point provided and could not autodetect FS label"
read -r -p "Please enter a mountpoint: " mountpoint
else
mountpoint="/mnt/$label"
fi
fi
if [[ -d "$mountpoint" ]] && [[ "$(ls -A /path/to/dir)" ]]; then
echo "Warning: $mountpoint is not empty!"
echo "Here are the contents:"
ls -al "$mountpoint"
read -r -p "Please enter an empty mountpoint: " mountpoint
fi
[[ ! -d "$mountpoint" ]] && mkdir -p "$mountpoint"