-
Notifications
You must be signed in to change notification settings - Fork 8
/
mountlto
executable file
·207 lines (187 loc) · 7.55 KB
/
mountlto
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env bash
# mountlto
SCRIPTDIR=$(dirname "${0}")
DEPENDENCIES=(ltfs)
CONFIG_FILE="${SCRIPTDIR}/ltopers.conf"
MINIMUM_TAPE_SERIAL_LENGTH=2
DEFAULT_LTO_INDEX_DIR="${HOME}/Documents/lto_indexes"
if [[ -f "${CONFIG_FILE}" ]] ; then
. "${CONFIG_FILE}"
fi
_usage(){
cat <<EOF
$(basename "${0}")
This mounts an LTFS-formatted LTO tape from an attached drive. By default the first drive
ready to mount a tape is used. Or run `$(basename "${0}") X` where X is the device id to
mount from a specific drive.
Dependencies: ${DEPENDENCIES[@]}
Usage: $(basename "${0}") [-n] | -h
-n don't mount any tape, just list the available decks and their device id
-h display this help
EOF
}
_check_dependencies(){
DEPS_OK=YES
while [ "${*}" != "" ] ; do
DEPENDENCY="${1}"
if [ ! "$(which "${DEPENDENCY}")" ] ; then
_report -wt "This script requires ${DEPENDENCY} to run but it is not installed"
_report -wt "If you are running ubuntu or debian you might be able to install ${DEPENDENCY} with the following command"
_report -wt "sudo apt-get install ${DEPENDENCY}"
_report -wt "If you are running mac you might be able to install ${DEPENDENCY} with the following command"
_report -wt "brew install ${DEPENDENCY}"
DEPS_OK=NO
fi
shift
done
if [[ "${DEPS_OK}" = "NO" ]]; then
_report -wt "Unmet dependencies"
_report -wt "Aborting!"
exit 1
else
return 0
fi
}
_check_for_lto_index_dir(){
# if LTO_INDEX_DIR is set in ltopers.conf then that will be used, otherwise a default
if [ -z "${LTO_INDEX_DIR}" ] ; then
LTO_INDEX_DIR="${DEFAULT_LTO_INDEX_DIR}"
fi
}
_cleanup_mount_dir(){
if [[ -d "${MOUNT_DIR}" ]] ; then
echo "calling umount"
umount "${MOUNT_DIR}"
rmdir "${MOUNT_DIR}"
fi
}
_report(){
local RED="$(tput setaf 1)" # Red - For Warnings
local GREEN="$(tput setaf 2)" # Green - For Declarations
local BLUE="$(tput setaf 4)" # Blue - For Questions
local NC="$(tput sgr0)" # No Color
local COLOR=""
local STARTMESSAGE=""
local ECHOOPT=""
local LOG_MESSAGE=""
OPTIND=1
while getopts ":qdwstn" OPT; do
case "${OPT}" in
q) COLOR="${BLUE}" ;; # question mode, use color blue
d) COLOR="${GREEN}" ;; # declaration mode, use color green
w) COLOR="${RED}" ;; # warning mode, use color red
s) STARTMESSAGE+=([$(basename "${0}")] ) ;; # prepend scriptname to the message
t) STARTMESSAGE+=($(_get_iso8601) '- ' ) ;; # prepend timestamp to the message
n) ECHOOPT="-n" ;; # to avoid line breaks after echo
esac
done
shift $(( OPTIND - 1 ))
MESSAGE="${1}"
echo "${ECHOOPT}" "${COLOR}${STARTMESSAGE[*]}${MESSAGE}${NC}"
}
_maketemp(){
if [[ -n "${1}" ]] ; then
PREFIX="$(basename "${0}").${1}"
else
PREFIX="$(basename "${0}")"
fi
mktemp -q -t "${PREFIX}"
if [ "${?}" -ne 0 ]; then
echo "${0}: Can't create temp file, exiting..."
_report -w "_maketemp was unable to create the temp file, so the script had to exit."
exit 1
fi
}
OPTIND=1
while getopts ":nh" opt ; do
case "${opt}" in
n) LISTONLY="Y" ;;
h) _usage ; exit 0 ;;
:) echo "Option -${OPTARG} requires an argument" ; exit 1 ;;
*) echo "Bad option -${OPTARG}" ; _usage ; exit 1 ;;
esac
done
shift "$((OPTIND-1))"
unset LTFS_OPTIONS
_check_for_lto_index_dir
_check_dependencies "${DEPENDENCIES[@]}"
LTO_LOGS="${LTO_INDEX_DIR}"
[[ ! -d "${LTO_LOGS}" ]] && mkdir -p "${LTO_LOGS}"
LTO_LIST="$(ltfs -o device_list 2>&1 | grep "Device Name = [0-9]*")"
# try to figure out how many lto decks are attached
LTO_COUNT=$(echo -n "${LTO_LIST}" | grep -c '^')
if [[ "${LTO_COUNT}" -eq 0 ]] ; then
_report -w "No available LTO decks were found (or perhaps 'ltfs' doesn't support the '-o device_list' option. Check connections or upgrade ltfs to 2.4.4.0 or newer)."
exit 1
fi
_report -d "Checking for ready tapes amongst..."
printf "%-12s %20s %20s \n" "Device ID" "Product ID" "Serial Number"
echo "${LTO_LIST}" | while read LTO_RECORD ; do
DEVICE_ID="$(echo "${LTO_RECORD}" | grep -o "Device Name = [^ ,]*" | cut -d = -f2 | sed 's/ //g')"
PRODUCT_ID="$(echo "${LTO_RECORD}" | grep -o "Product ID = [^ ,]*" | cut -d = -f2 | sed 's/ //g')"
SERIAL="$(echo "${LTO_RECORD}" | grep -o "Serial Number = [^ ,]*" | cut -d = -f2 | sed 's/ //g')"
printf "%-12s %20s %20s \n" "${DEVICE_ID}" "${PRODUCT_ID}" "${SERIAL}"
done
echo
if [[ "$LISTONLY" = "Y" ]] ; then
exit
fi
if [[ -n "${1}" ]] ; then
SELECTED_DEVICE_ID="${1}"
_report -d "Will try to mount device ${SELECTED_DEVICE_ID} (as requested)."
else
SELECTED_DEVICE_ID="$(echo "${LTO_LIST}" | grep -o "Device Name = [^ ,]*" | cut -d = -f2 | sed 's/ //g' | head -n 1)"
_report -d "Will try to mount device ${SELECTED_DEVICE_ID} (first available)."
fi
SELECTED_DEVICE_RECORD="$(echo "${LTO_LIST}" | grep "Device Name = ${SELECTED_DEVICE_ID},")"
DEVICE_ID="$(echo "${SELECTED_DEVICE_RECORD}" | grep -o "Device Name = [^ ,]*" | cut -d = -f2 | sed 's/ //g')"
PRODUCT_ID="$(echo "${SELECTED_DEVICE_RECORD}" | grep -o "Product ID = [^ ,]*" | cut -d = -f2 | sed 's/ //g')"
SERIAL="$(echo "${SELECTED_DEVICE_RECORD}" | grep -o "Serial Number = [^ ,]*" | cut -d = -f2 | sed 's/ //g')"
if [[ -z "${DEVICE_ID}" ]] ; then
_report -w "Could not determine the device id. Error."
exit
fi
if [ "$(command -v ltfs_ldun)" ] ; then
ltfs_ldun load "${DEVICE_ID}" 1>/dev/null
fi
LOADTMP="$(_maketemp "${SERIAL}")_loadtmp.txt"
unset TAPE_SERIAL
COUNTER=0
_report -dn "Checking for tape barcode with deck ${DEVICE_ID} (${SERIAL})"
while [[ "${#TAPE_SERIAL}" -lt "${MINIMUM_TAPE_SERIAL_LENGTH}" ]] && [[ "${COUNTER}" -lt 2 ]]; do
((COUNTER++))
ltfs -f -o devname="${DEVICE_ID}" 2> "${LOADTMP}"
LTFS_ERR="${?}"
# find barcode by either "Volser(Barcode)" (Quantum style) or "Tape attribute: Barcode" (IBM style)
TAPE_SERIAL=$(grep "Volser(Barcode).*\|Tape attribute: Medium Label*\|Tape attribute: Barcode.*\|Volume mounted successfully*" "${LOADTMP}" | grep -Eo "\b[A-Z0-9]{6}\b|\b[A-Z0-9]{8}\b" | head -n 1)
echo -n "."
done
echo
if [[ "${#TAPE_SERIAL}" -lt "${MINIMUM_TAPE_SERIAL_LENGTH}" ]] ; then
if [[ $(cat "${LOADTMP}" | grep -a "Mounting the volume") && ! $(cat "${LOADTMP}" | grep -a "failed to load the tape") ]] ; then
DECKSERIAL=$(grep -a -o "serial number is [A-Z0-9]*" "${LOADTMP}" | awk '{print $4}')
TAPE_SERIAL="${DECKSERIAL}"
else
unset TAPE_SERIAL
fi
else
_report -d "Deck ${DEVICE_ID} (${SERIAL}) found with tape ${TAPE_SERIAL}."
echo -n -e "\033]0;Deck:${SERIAL} LTO:${TAPE_SERIAL}\007"
if [[ $(uname -s) = "Darwin" ]] ; then
MOUNT_DIR="/Volumes/${TAPE_SERIAL}"
else
MOUNT_DIR="/mnt/${TAPE_SERIAL}"
fi
_report -d "MOUNTING TAPE : THE TAPE WILL MOUNT TO THE ${MOUNT_DIR} FOLDER"
_report -d "MOUNTING TAPE : AFTER MOUNT PRESS CONTROL-C$ TO EJECT"
trap _cleanup_mount_dir SIGHUP SIGINT SIGTERM
LTFS_OPTIONS+=(-o eject)
LTFS_OPTIONS+=(-o noatime)
LTFS_OPTIONS+=(-o capture_index)
LTFS_OPTIONS+=(-o devname="${DEVICE_ID}")
LTFS_OPTIONS+=(-o volname="${TAPE_SERIAL}")
ltfs -f -o work_directory="${LTO_LOGS}" ${LTFS_OPTIONS[@]} "${MOUNT_DIR}"
echo -n -e "\033]0;\007"
[[ -d "${MOUNT_DIR}" ]] && rmdir "${MOUNT_DIR}"
renameschemas -u
fi