Skip to content

Instantly share code, notes, and snippets.

@jgato
Created January 24, 2022 08:42
Show Gist options
  • Save jgato/a3025c90b7012e9eb9ef4a3c35b90c49 to your computer and use it in GitHub Desktop.
Save jgato/a3025c90b7012e9eb9ef4a3c35b90c49 to your computer and use it in GitHub Desktop.
It (HTTP) expose a local iso image and boots a BMC server with that iso using Redfish protocol
# !/bin/bash
#################################
# Very simple script that exposes a local iso image
# and boots a BMC virtual media pointing to it.
# It uses a Redfish Interface
# It uses a python simple http server to expose the image
# The server runs on port 7800
################################
# The script is very simple and lacks of making
# many tests, so it just runs all the commands
# it would need some user extra checks
################################
# set these variables
REDFISH_PROTOCOL="http"
REDFISH_HOST="127.0.0.1"
REDFISH_PORT="8000"
REDFISH_USER="admin"
REDFISH_PASSWORD="admin"
# point to a local existing file (full path+file)
ISO_PATH="/FULL_PATH/file.iso"
# when using sushy emulations
REDFISH_SYSTEM="UUID"
REDFISH_MANAGER="UUID"
# when using idrac
#REDFISH_SYSTEM="System.Embedded.1"
#REDFISH_MANAGER="iDRAC.Embedded.1"
# NO CHANGE FOLLOWING VARIABLES
REDFISH_URL_SYSTEMS=${REDFISH_PROTOCOL}://${REDFISH_USER}:${REDFISH_PASSWORD}@${REDFISH_HOST}:${REDFISH_PORT}/redfish/v1/Systems/${REDFISH_SYSTEM}
REDFISH_URL_MANAGERS=${REDFISH_PROTOCOL}://${REDFISH_USER}:${REDFISH_PASSWORD}@${REDFISH_HOST}:${REDFISH_PORT}/redfish/v1/Managers/${REDFISH_MANAGER}
set -e
function cleanup {
echo -e "Cleaning..."
if [ -n SERVER_PID ]; then
kill -9 $SERVER_PID
fi
}
trap cleanup EXIT
if ! test -f "${ISO_PATH}"; then
echo "$ISO_PATH not found."
exit 1
fi
FILE_NAME=`basename "${ISO_PATH}"`
FILE_PATH=`dirname "${ISO_PATH}"`
python3 -m http.server 7800 -d ${FILE_PATH} &
sleep 2
if ! test $? == 0; then
echo "HTTP server not executed correctly"
exit 1
fi
SERVER_PID=$!
ISO_URL=http://127.0.0.1:7800/${FILE_NAME}
echo -e "Getting some info from HOST to check if available"
echo -e "Host status: "
curl -s ${REDFISH_URL_SYSTEMS} | jq .PowerState
echo -e " - Here you should see something similar to a PowerSate of the host, if not, DONT CONTINUE".
echo -e " - Also, be aware that now we will connect a virtual media to an iso, and this will power off and power on the server with a new boot sequence"
echo -e " - If you are using Sushy tools and libvirt backend, the ISO will be 'uplodaed' in your /tmp/. It is up to you how/when to clean it"
echo "Do you wanto to abort or continue? (a)bort or (c)ontinue, default abort"
read ABORT
if [ "${ABORT}" != "c" ]; then
exit 1
fi
echo -e "Setting boot sequence to CD"
curl -s -X PATCH -H 'Content-Type: application/json' -d '{
"Boot": {
"BootSourceOverrideTarget": "Cd",
"BootSourceOverrideEnabled": "Continuous"
}
}' ${REDFISH_URL_SYSTEMS}
curl -s ${REDFISH_URL_SYSTEMS} | jq .Boot
echo -e "Connecting the ISO"
curl -s -X POST ${REDFISH_URL_MANAGERS}/VirtualMedia/Cd/Actions/VirtualMedia.InsertMedia -H "Content-Type: application/json" -d '{
"Image":"'"${ISO_URL}"'",
"Inserted": true
}'
curl -s ${REDFISH_URL_MANAGERS}/VirtualMedia/Cd/ | jq '[{iso_connected: .Inserted}]'
curl -s ${bmc}/redfish/v1/Managers/${bmc_server}/VirtualMedia/Cd/ | jq '[{iso_connected: .Inserted}]'
echo -e "Powering off"
curl -H "Content-Type: application/json" -X POST ${REDFISH_URL_SYSTEMS}/Actions/ComputerSystem.Reset -d '{"ResetType": "ForceOff"}'
echo -e "Powering on"
curl -H "Content-Type: application/json" -X POST ${REDFISH_URL_SYSTEMS}/Actions/ComputerSystem.Reset -d '{"ResetType": "ForceOn"}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment