-
Notifications
You must be signed in to change notification settings - Fork 8
/
formatlto
executable file
·149 lines (135 loc) · 5.2 KB
/
formatlto
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
#!/usr/bin/env bash
# formatlto
# Formats an LTO tape with LTFS.
SCRIPTDIR=$(dirname "${0}")
DEPENDENCIES=(mkltfs)
TAPE_SERIAL_REGEX="^[A-Z0-9]{6}$"
BLUE="$(tput setaf 4)" # Blue - For Questions
NC="$(tput sgr0)" # No Color
_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
}
_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}"
}
_usage(){
cat <<EOF
$(basename "${0}")
This script formats an LTO tape with LTFS.
Dependencies: ${DEPENDENCIES[@]}
Usage: $(basename "${0}") [-f] [-c] | -h
-f force formatting
-c use compression
-x use barcode label as tape serial (caveat: this is not standard)
-h display this help
EOF
}
unset MIDDLE_OPTIONS
while getopts ":fcxh" opt ; do
case "${opt}" in
f) FORCE=1 ;;
c) COMPRESS=1 ;;
x) TAPE_SERIAL_REGEX="^[A-Z0-9]{6}(L[5-8]|M8)$" ;;
h) _usage ; exit 0 ;;
*) _report -w "Error: Bad option -${OPTARG}" ; _usage ; exit 1 ;;
esac
done
_check_dependencies "${DEPENDENCIES[@]}"
if [ "${FORCE}" = "1" ] ; then
MIDDLE_OPTIONS+=(-f)
_report -d "Will force formatting."
fi
if [ ! "${COMPRESS}" = "1" ] ; then
MIDDLE_OPTIONS+=(-c)
else
_report -d "Will use compression."
fi
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
if [[ "$(ltfs -h 2>&1 | grep -c device_list)" -eq 0 ]] ; then
_report -w "The installed version of ltfs at $(which ltfs) doesn't support the '-o device_list' option. Please upgrade ltfs to 2.4.4.0 or newer."
exit 1
else
_report -w "No available LTO decks were found or they're all busy."
df -Ph | grep "^Filesystem\|^ltfs" | sort
exit 1
fi
fi
_report -d "Checking for ready decks 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 [[ "${LTO_COUNT}" -eq 1 ]] ; then
DEVICE_ID="$(echo "${LTO_LIST}" | grep -o "Device Name = [^ ,]*" | cut -d = -f2 | sed 's/ //g')"
PRODUCT_ID="$(echo "${LTO_LIST}" | grep -o "Product ID = [^ ,]*" | cut -d = -f2 | sed 's/ //g')"
SERIAL="$(echo "${LTO_LIST}" | grep -o "Serial Number = [^ ,]*" | cut -d = -f2 | sed 's/ //g')"
if [[ "$(uname -s)" = "Linux" ]] ; then
DECK="/dev/sg3"
_report -d "Will use ${DECK} to format the tape."
else
DECK="${DEVICE_ID}"
_report -d "Will use Deck ${DECK} (${SERIAL}) to format the tape."
fi
else
#### TO DO: select the correct path to the drive on Linux and Windows
if [[ "$(uname -s)" = "Linux" ]] ; then
DECK='/dev/sg3'
else
_report -q -n "Enter the Device ID for the deck to use: "
read DECK
fi
fi
_report -qn "Enter the tape identifier: "
read TAPE_SERIAL
if [[ ! $(echo "${TAPE_SERIAL}" | grep -E "${TAPE_SERIAL_REGEX}") ]] ; then
_report -w "Error: The tape serial is not valid."
exit 1
fi
mkltfs ${MIDDLE_OPTIONS[@]} --device=${DECK} --tape-serial="${TAPE_SERIAL}" --volume-name="${TAPE_SERIAL}"