comment appliance import script

This commit is contained in:
ronivay
2021-07-19 13:12:30 +03:00
parent a3d160b679
commit d1433e025e

View File

@@ -6,6 +6,7 @@
# Repository: https://github.com/ronivay/XenOrchestraInstallerUpdater #
#########################################################################
# image url is static and not configurable by user
IMAGE_URL="https://xo-appliance.yawn.fi/downloads/image.xva.gz"
function OSCheck {
@@ -30,6 +31,7 @@ function NetworkChoose {
set +e
# get network name/uuid of all available networks configured in the pool
# shellcheck disable=SC1117
IFS=$'\n' read -r -d '' -a networks <<< "$(xe network-list | grep "uuid\|name-label" | cut -d':' -f2 | sed 's/^ //' | paste - -)"
@@ -39,11 +41,14 @@ function NetworkChoose {
local PS3="Pick a number. CTRL+C to exit: "
select network in "${networks[@]}"
do
# get only the network uuid from array which we need later on when adding vif
read -r -a network_split <<< "$network"
networkuuid=${network_split[0]}
# print a menu where to choose network from
case $network in
*)
# save network uuid for later
vifuuid="$networkuuid"
break
;;
@@ -56,9 +61,11 @@ function StorageChoose {
set +e
# get storage name/uuid of all available storages with content-type=user which should match all usable storage repositories
# shellcheck disable=SC1117
IFS=$'\n' read -r -d '' -a storages <<< "$(xe sr-list content-type=user | grep "uuid\|name-description" | cut -d':' -f2 | sed 's/^ //' | paste - -)"
# bail out if no storage repositories are found
if [[ ${#storages[@]} -eq 0 ]]; then
echo "No storage repositories found, can't import VM"
echo "Create SR and try again. More information: https://xcp-ng.org/docs/storage.html"
@@ -71,15 +78,19 @@ function StorageChoose {
local PS3="Pick a number. CTRL+C to exit: "
select storage in "${storages[@]}" "default"
do
# get only the storage repository uuid which we need later on when importing image
read -r -a storage_split <<< "$storage"
storageuuid=${storage_split[0]}
# print a menu where to choose storage from
case $storage in
default)
# this value is handled during import if set to default
sruuid=default
break
;;
*)
# save storage uuid for later
sruuid=$storageuuid
break
;;
@@ -98,10 +109,13 @@ function NetworkSettings {
echo "Set network settings for VM. Leave IP-address as blank to use DHCP"
echo
# read ip address from user input. dhcp is default if left empty
read -r -p "IP address: " ipaddress
ipaddress=${ipaddress:-dhcp}
# if not using dhcp, we need more information
if [[ "$ipaddress" != "dhcp" ]]; then
# get network details from user and prompt again if input doesn't match ip address regex
while ! [[ $ipaddress =~ $ipregex ]]; do
echo "Check IP address format"
read -r -p "IP address: " ipaddress
@@ -139,12 +153,14 @@ function VMImport {
echo
# Import image. We pipe through zcat because xe vm-import should transparently decompress gzipped image, but doesn't seem to understand when stream ends when piped through curl/wget whatnot.
# if SR was not defined, we leave that parameter out
if [[ $sruuid == "default" ]]; then
uuid=$(curl "$IMAGE_URL" | zcat | xe vm-import filename=/dev/stdin)
else
uuid=$(curl "$IMAGE_URL" | zcat | xe vm-import filename=/dev/stdin sr-uuid="$sruuid")
fi
# exit if import failed for any reason
# shellcheck disable=SC2181
if [[ $? != "0" ]]; then
echo "Import failed"
@@ -153,12 +169,15 @@ function VMImport {
echo
echo "Import complete"
# no network interface included in the image, we need to create one based on network uuid set by user earlier
xe vif-create network-uuid="$vifuuid" vm-uuid="$uuid" device=0 >/dev/null
# VM startup script reads network details from xenstore and configures interface based on that so set values based on user input earlier
if [[ "$ipaddress" != "dhcp" ]]; then
xe vm-param-set uuid="$uuid" xenstore-data:vm-data/ip="$ipaddress" xenstore-data:vm-data/netmask="$netmask" xenstore-data:vm-data/gateway="$gateway" xenstore-data:vm-data/dns="$dns"
fi
# remove all other boot options except disk to speed startup
xe vm-param-remove uuid="$uuid" param-name=HVM-boot-params param-key=order
xe vm-param-set uuid="$uuid" HVM-boot-params:"order=c"
@@ -168,6 +187,7 @@ function VMImport {
set +e
# loop max 300 seconds for VM to startup and xen tools to announce ip-address value
count=0
limit=10
ip=$(xe vm-param-get uuid="$uuid" param-name=networks param-key=0/ip 2>/dev/null)
@@ -178,6 +198,7 @@ function VMImport {
(( count++ ))
done
# network details are needed in xenstore only during first startup so remove them at this point since VM should be running
if [[ "$ipaddress" != "dhcp" ]]; then
xe vm-param-remove param-name=xenstore-data param-key=vm-data/ip uuid="$uuid" 2>/dev/null
xe vm-param-remove param-name=xenstore-data param-key=vm-data/netmask uuid="$uuid" 2>/dev/null
@@ -185,6 +206,7 @@ function VMImport {
xe vm-param-remove param-name=xenstore-data param-key=vm-data/dns uuid="$uuid" 2>/dev/null
fi
# if we got ip-address value from VM, we print how to access it...
if [[ "$ip" != "" ]]; then
echo
echo "VM Started successfully"
@@ -194,6 +216,7 @@ function VMImport {
echo "Default credentials for SSH: xo/xopass"
echo
echo "Remember to change both passwords before putting VM to use!"
# ... and print the same without ip-address information if ip-address value was missing
else
echo
echo "VM started but we couldn't fetch it's ip-address from xentools"
@@ -207,6 +230,7 @@ function VMImport {
}
# run all functions in a specific order
OSCheck
StorageChoose
NetworkChoose