comment appliance import script
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
# Repository: https://github.com/ronivay/XenOrchestraInstallerUpdater #
|
# 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"
|
IMAGE_URL="https://xo-appliance.yawn.fi/downloads/image.xva.gz"
|
||||||
|
|
||||||
function OSCheck {
|
function OSCheck {
|
||||||
@@ -30,6 +31,7 @@ function NetworkChoose {
|
|||||||
|
|
||||||
set +e
|
set +e
|
||||||
|
|
||||||
|
# get network name/uuid of all available networks configured in the pool
|
||||||
# shellcheck disable=SC1117
|
# shellcheck disable=SC1117
|
||||||
IFS=$'\n' read -r -d '' -a networks <<< "$(xe network-list | grep "uuid\|name-label" | cut -d':' -f2 | sed 's/^ //' | paste - -)"
|
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: "
|
local PS3="Pick a number. CTRL+C to exit: "
|
||||||
select network in "${networks[@]}"
|
select network in "${networks[@]}"
|
||||||
do
|
do
|
||||||
|
# get only the network uuid from array which we need later on when adding vif
|
||||||
read -r -a network_split <<< "$network"
|
read -r -a network_split <<< "$network"
|
||||||
networkuuid=${network_split[0]}
|
networkuuid=${network_split[0]}
|
||||||
|
|
||||||
|
# print a menu where to choose network from
|
||||||
case $network in
|
case $network in
|
||||||
*)
|
*)
|
||||||
|
# save network uuid for later
|
||||||
vifuuid="$networkuuid"
|
vifuuid="$networkuuid"
|
||||||
break
|
break
|
||||||
;;
|
;;
|
||||||
@@ -56,9 +61,11 @@ function StorageChoose {
|
|||||||
|
|
||||||
set +e
|
set +e
|
||||||
|
|
||||||
|
# get storage name/uuid of all available storages with content-type=user which should match all usable storage repositories
|
||||||
# shellcheck disable=SC1117
|
# 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 - -)"
|
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
|
if [[ ${#storages[@]} -eq 0 ]]; then
|
||||||
echo "No storage repositories found, can't import VM"
|
echo "No storage repositories found, can't import VM"
|
||||||
echo "Create SR and try again. More information: https://xcp-ng.org/docs/storage.html"
|
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: "
|
local PS3="Pick a number. CTRL+C to exit: "
|
||||||
select storage in "${storages[@]}" "default"
|
select storage in "${storages[@]}" "default"
|
||||||
do
|
do
|
||||||
|
# get only the storage repository uuid which we need later on when importing image
|
||||||
read -r -a storage_split <<< "$storage"
|
read -r -a storage_split <<< "$storage"
|
||||||
storageuuid=${storage_split[0]}
|
storageuuid=${storage_split[0]}
|
||||||
|
|
||||||
|
# print a menu where to choose storage from
|
||||||
case $storage in
|
case $storage in
|
||||||
default)
|
default)
|
||||||
|
# this value is handled during import if set to default
|
||||||
sruuid=default
|
sruuid=default
|
||||||
break
|
break
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
|
# save storage uuid for later
|
||||||
sruuid=$storageuuid
|
sruuid=$storageuuid
|
||||||
break
|
break
|
||||||
;;
|
;;
|
||||||
@@ -98,10 +109,13 @@ function NetworkSettings {
|
|||||||
echo "Set network settings for VM. Leave IP-address as blank to use DHCP"
|
echo "Set network settings for VM. Leave IP-address as blank to use DHCP"
|
||||||
echo
|
echo
|
||||||
|
|
||||||
|
# read ip address from user input. dhcp is default if left empty
|
||||||
read -r -p "IP address: " ipaddress
|
read -r -p "IP address: " ipaddress
|
||||||
ipaddress=${ipaddress:-dhcp}
|
ipaddress=${ipaddress:-dhcp}
|
||||||
|
|
||||||
|
# if not using dhcp, we need more information
|
||||||
if [[ "$ipaddress" != "dhcp" ]]; then
|
if [[ "$ipaddress" != "dhcp" ]]; then
|
||||||
|
# get network details from user and prompt again if input doesn't match ip address regex
|
||||||
while ! [[ $ipaddress =~ $ipregex ]]; do
|
while ! [[ $ipaddress =~ $ipregex ]]; do
|
||||||
echo "Check IP address format"
|
echo "Check IP address format"
|
||||||
read -r -p "IP address: " ipaddress
|
read -r -p "IP address: " ipaddress
|
||||||
@@ -139,12 +153,14 @@ function VMImport {
|
|||||||
echo
|
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.
|
# 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
|
if [[ $sruuid == "default" ]]; then
|
||||||
uuid=$(curl "$IMAGE_URL" | zcat | xe vm-import filename=/dev/stdin)
|
uuid=$(curl "$IMAGE_URL" | zcat | xe vm-import filename=/dev/stdin)
|
||||||
else
|
else
|
||||||
uuid=$(curl "$IMAGE_URL" | zcat | xe vm-import filename=/dev/stdin sr-uuid="$sruuid")
|
uuid=$(curl "$IMAGE_URL" | zcat | xe vm-import filename=/dev/stdin sr-uuid="$sruuid")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# exit if import failed for any reason
|
||||||
# shellcheck disable=SC2181
|
# shellcheck disable=SC2181
|
||||||
if [[ $? != "0" ]]; then
|
if [[ $? != "0" ]]; then
|
||||||
echo "Import failed"
|
echo "Import failed"
|
||||||
@@ -153,12 +169,15 @@ function VMImport {
|
|||||||
echo
|
echo
|
||||||
echo "Import complete"
|
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
|
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
|
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"
|
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
|
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-remove uuid="$uuid" param-name=HVM-boot-params param-key=order
|
||||||
xe vm-param-set uuid="$uuid" HVM-boot-params:"order=c"
|
xe vm-param-set uuid="$uuid" HVM-boot-params:"order=c"
|
||||||
|
|
||||||
@@ -168,6 +187,7 @@ function VMImport {
|
|||||||
|
|
||||||
set +e
|
set +e
|
||||||
|
|
||||||
|
# loop max 300 seconds for VM to startup and xen tools to announce ip-address value
|
||||||
count=0
|
count=0
|
||||||
limit=10
|
limit=10
|
||||||
ip=$(xe vm-param-get uuid="$uuid" param-name=networks param-key=0/ip 2>/dev/null)
|
ip=$(xe vm-param-get uuid="$uuid" param-name=networks param-key=0/ip 2>/dev/null)
|
||||||
@@ -178,6 +198,7 @@ function VMImport {
|
|||||||
(( count++ ))
|
(( count++ ))
|
||||||
done
|
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
|
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/ip uuid="$uuid" 2>/dev/null
|
||||||
xe vm-param-remove param-name=xenstore-data param-key=vm-data/netmask 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
|
xe vm-param-remove param-name=xenstore-data param-key=vm-data/dns uuid="$uuid" 2>/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# if we got ip-address value from VM, we print how to access it...
|
||||||
if [[ "$ip" != "" ]]; then
|
if [[ "$ip" != "" ]]; then
|
||||||
echo
|
echo
|
||||||
echo "VM Started successfully"
|
echo "VM Started successfully"
|
||||||
@@ -194,6 +216,7 @@ function VMImport {
|
|||||||
echo "Default credentials for SSH: xo/xopass"
|
echo "Default credentials for SSH: xo/xopass"
|
||||||
echo
|
echo
|
||||||
echo "Remember to change both passwords before putting VM to use!"
|
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
|
else
|
||||||
echo
|
echo
|
||||||
echo "VM started but we couldn't fetch it's ip-address from xentools"
|
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
|
OSCheck
|
||||||
StorageChoose
|
StorageChoose
|
||||||
NetworkChoose
|
NetworkChoose
|
||||||
|
Reference in New Issue
Block a user