-
Notifications
You must be signed in to change notification settings - Fork 1
/
isomount
executable file
·222 lines (198 loc) · 7.04 KB
/
isomount
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/bin/bash
#
# $Id: isomount 113 2022-07-21 18:43:42Z HMBJOrth $
# (c) 2022 Juergen Orth, Fujitsu
#
# Script for booting server with remote media ISO
# Server should be shut down before!
#
# Be careful: This is a PROTOTYPE!!!
#
# read default params from CFG-File
[ -r ${0}.cfg ] && . ${0}.cfg || { echo "No Configfile!"; exit 1; }
##############################################################################
# Don't change lines below!
##############################################################################
Proto=https
RedfishBase=redfish/v1
CurlOptions="-k -i --silent --noproxy $iRMC"
LOGFILE=/tmp/${0##*/}-${iRMC}.log
debug() {
if [ "$DEBUG" ]
then
[ "$1" ] && { debugtimestamp $* >> $LOGFILE; } || tee -a $LOGFILE
else
[ "$1" ] || cat
fi
}
timestamp() {
date '+%Y-%m-%d %H:%M:%S ---'
}
debugtimestamp() {
echo
echo "##########################################"
echo $(timestamp) $*
}
# Create JSON params from URI
MountParams() { # URI
IsoUri="$1"
ShareType=${IsoUri%%:*}
ShareType=${ShareType^^}
IsoUri=${IsoUri#*://}
if [[ $IsoUri =~ .*@.* ]]
then
Cred=${IsoUri%@*}
IsoUri=${IsoUri#*@}
if [[ $Cred =~ .*\;.* ]]
then
UserDomain=${Cred%;*}
Cred=${Cred#*;}
fi
UserName=${Cred%:*}
Password=${Cred#*:}
fi
Server=${IsoUri%%/*}
IsoUri=${IsoUri#*/}
[[ $IsoUri =~ .*/.* ]] && ShareName=${IsoUri%/*}
ImageName=${IsoUri##*/}
echo "{"
echo ' "FTSVirtualMediaAction": "ConnectCD",'
for p in Server UserName Password UserDomain ShareType ShareName ImageName
do
if [ "$(eval echo \${$p})" ]
then
echo -ne " "
eval echo -n \\\"$p\\\": \\\"\${$p}\\\"
[ "$p" = ImageName ] && echo || echo ","
fi
done
echo "}"
}
# Check PowerState of Server
ServerPoweredOn() {
cmd="curl $Proto://$iRMC/$RedfishBase/Chassis/0 \
-X GET -u $User:$Pw $CurlOptions"
debug ServerPoweredOn: $cmd
State=$($cmd |\
debug |\
awk '/^{/ { flag=1 } { if (flag) print }' | jq -r '.PowerState')
[ "$State" = "Off" ] && return 1 || return 0
}
# Check if remote media can be mounted
CheckRmSlots() {
cmd="curl $Proto://$iRMC/$RedfishBase/Systems/0/Oem/ts_fujitsu/VirtualMedia \
-X GET -u $User:$Pw $CurlOptions"
debug CheckRmSlots: $cmd
$cmd |\
debug |\
awk '/^{/ { flag=1 } { if (flag) print }' | jq '.CDImage.NumberOfFreeDevices'
}
# Mount given ISO as remote media
MountISO() { # ISOURI
Uri=$1
cmd="curl $Proto://$iRMC/$RedfishBase/Systems/0/Actions/Oem/FTSComputerSystem.VirtualMedia \
-H 'Accept: application/json' \
-H 'Content-type: application/json' \
-X POST -u $User:$Pw $CurlOptions -d "$(MountParams $Uri)""
debug MountIso: $cmd
curl $Proto://$iRMC/$RedfishBase/Systems/0/Actions/Oem/FTSComputerSystem.VirtualMedia \
-H 'Accept: application/json' \
-H 'Content-type: application/json' \
-X POST -u $User:$Pw $CurlOptions -d "$(MountParams $Uri)" | debug
}
# Unmount remote media
UnMountISO() {
cmd="curl $Proto://$iRMC/$RedfishBase/Systems/0/Actions/Oem/FTSComputerSystem.VirtualMedia \
-H 'Accept: application/json' \
-H 'Content-type: application/json' \
-X POST -u $User:$Pw $CurlOptions -d '{"FTSVirtualMediaAction": "DisconnectCD"}'"
debug UnMountISO: $cmd
curl $Proto://$iRMC/$RedfishBase/Systems/0/Actions/Oem/FTSComputerSystem.VirtualMedia \
-H 'Accept: application/json' \
-H 'Content-type: application/json' \
-X POST -u $User:$Pw $CurlOptions -d '{"FTSVirtualMediaAction": "DisconnectCD"}' | debug
}
# Read current boot options
GetBoot() {
cmd="curl $Proto://$iRMC/$RedfishBase/Systems/0/Oem/ts_fujitsu/BootConfig \
-H 'Accept: application/json' \
-H 'Content-type: application/json' \
-X GET -u $User:$Pw $CurlOptions "
debug GetBoot: $cmd
curl $Proto://$iRMC/$RedfishBase/Systems/0/Oem/ts_fujitsu/BootConfig \
-H 'Accept: application/json' \
-H 'Content-type: application/json' \
-X GET -u $User:$Pw $CurlOptions | debug
}
# Set boot options to CD for one shot
SetBoot() {
cmd="curl $Proto://$iRMC/$RedfishBase/Systems/0/Oem/ts_fujitsu/BootConfig \
-H 'Accept: application/json' \
-H 'Content-type: application/json' \
-H "If-Match":$(GetBoot|awk -F\" '/@odata.etag/ {print $(NF-1)}') \
-X PATCH -u $User:$Pw $CurlOptions \
-d '{ "BootDevice": "Cd", "NextBootOnlyEnabled": true }'"
debug SetBoot: $cmd
curl $Proto://$iRMC/$RedfishBase/Systems/0/Oem/ts_fujitsu/BootConfig \
-H 'Accept: application/json' \
-H 'Content-type: application/json' \
-H "If-Match":$(GetBoot|awk -F\" '/@odata.etag/ {print $(NF-1)}') \
-X PATCH -u $User:$Pw $CurlOptions \
-d '{ "BootDevice": "Cd", "NextBootOnlyEnabled": true }' | debug
}
# Power server on
PowerOnServer() {
cmd="curl $Proto://$iRMC/$RedfishBase/Systems/0/Actions/Oem/FTSComputerSystem.Reset \
-H 'Accept: application/json' \
-H 'Content-type: application/json' \
-X POST -u $User:$Pw $CurlOptions -d '{"FTSResetType": "PowerOn" }'"
debug ResetServer: $cmd
curl $Proto://$iRMC/$RedfishBase/Systems/0/Actions/Oem/FTSComputerSystem.Reset \
-H 'Accept: application/json' \
-H 'Content-type: application/json' \
-X POST -u $User:$Pw $CurlOptions -d '{"FTSResetType": "PowerOn" }' | debug
}
# For debugging
# echo $Uri; MountParams $Uri; exit
##############################################################################
# Main program
##############################################################################
if [ "$DEBUG" ]
then
echo "$(timestamp) Info: Debug mode enabled. "
printf " %s\n" Logfile=$LOGFILE
printf " %s\n" iRMC=$iRMC User=$User
printf " %s\n" URI=$Uri
fi
type jq >/dev/null 2>&1 || { echo "Tool 'jq' is needed for this script!"; exit 2; }
echo -n "$(timestamp) Checking Powerstate ... "
if ServerPoweredOn
then
echo Server is powered on or not reachable.
echo Please ensure to power off server before running this script.
echo Exiting
exit 3
else
echo "OK (off)"
fi
# Mount ISO as remote media
if [ "0$(CheckRmSlots)" -gt 0 ]
then
echo -n "$(timestamp) Trying to mount $Uri ... "
MountISO $Uri | grep -q '^HTTP.*204' && echo successful || \
{ echo "failed! Exiting"; exit 4; }
else
echo "$(timestamp) Remote media already mounted"
exit 5
fi
# Set CD as next boot device (once)
echo -n "$(timestamp) Set boot device to ISO ... "
SetBoot | grep -q '^HTTP.*200' && echo "successful" || echo failed
# Power on server and let the update run
echo "$(timestamp) Powering on server ..."
PowerOnServer
# When finished dismount ISO
echo -n "Wait until update has finished and OS is up and running to unmount ISO image. Then press ENTER ... "
read taste
echo -n "$(timestamp) Unmount ISO ... "
UnMountISO | grep -q '^HTTP.*204' && echo "successful" || echo failed