-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcplay.sh
executable file
·142 lines (131 loc) · 3.53 KB
/
tcplay.sh
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
#!/usr/bin/env bash
######################### Make the Virtual Volume available #########################
make_available(){
if [[ $# -lt 1 ]]; then
return 1
fi
if [[ ! -e "$1" || ! -r "$1" || ! -f "$1" ]]; then
return 2
fi
echo "Issuing losetup command:"
if ! sudo losetup /dev/loop0 "$1"; then
return 3
fi
cmd_status=1
echo "losetup done!"
echo "Issuing tcplay command:"
if ! sudo tcplay -m "$1" -d /dev/loop0; then
return 4
fi
cmd_status=2
echo "tcplay done!"
echo "Issuing mount command:"
if ! sudo mount /dev/mapper/"$1" /media/ISOimgs; then
return 5;
fi
cmd_status=3
echo "mount done!"
return 0
}
######################### Make the Virtual Volume unavailable #########################
make_unavailable(){
case $cmd_status in
3)
echo "Undoing mount command!"
if ! sudo umount /media/ISOimgs; then
echo "Error undoing mount command!" 1>&2
return $cmd_status
fi
;&
2)
echo "Undoing tcplay command with dmsetup!"
if ! sudo dmsetup remove "$1"; then
echo "Error undoing tcplay with dmsetup!" 1>&2
return $cmd_status
fi
;&
1)
echo "Undoing losetup command!"
if ! sudo losetup -d /dev/loop0; then
echo "Error undoing losetup command!" 1>&2
return $cmd_status
fi
;;
0)
echo "No commans done so no commands undone!" 1>&2
return $cmd_status
;;
*) echo "No commands undone or unknown error!" 1>&2
exit 1
;;
esac
echo "Undone!"
}
show_params(){
echo "Number of parameters is $#."
echo "List of parameters is $*."
}
######################### Main Script #########################
cmd_status=0
###### Error hanling ######
err_no_err=0
err_status=0
err_param=1
err_no_file=2
err_losetup=3
err_tcplay=4
err_mount=5
err_umount=6
err_dmsetup=7
err_r_losetup=8
#if [[ $# -lt 1 ]]; then
# echo "Usage: $0 /path/to/container.tc"
# exit 1
#fi
while true; do
echo "Enter an option:"
echo "[A] or [a] to make Available"
echo "[U] or [u] to make Unavailable"
echo "[Q] or [q] to quit"
while true; do
read -r -n1 -p "> " opt
echo
case $opt in
"a"|"A")
make_available "$@" cm_status
case "$?" in
1)
echo "Not enough parameters. Usage: $0 /path/to/container.tc"
;;
2)
echo "$1 oesn't exist or not ready!"
;;
3)
echo "Error with $1! Please fix manually!"
;;
4)
echo "Error with $1! Please fix manually!"
;;
5)
echo "Error with $1! Please fix manually!"
;;
0)
echo
;;
*)
echo "Unknown error! Exiting..."
exit 1
;;
esac
"u"|"U")
make_unavailable "$@" cmd_status
;;
"q"|"Q")
exit 1
;;
*)
echo "Unknown error!" 1>&2
;;
esac
done
done