diff --git a/src/print.sh b/src/print.sh index f63f145..92c0f46 100644 --- a/src/print.sh +++ b/src/print.sh @@ -26,67 +26,122 @@ resp_err="Guest returned an invalid response:" curl_err="Failed to connect to guest: curl error" jq_err="Failed to parse response from guest: jq error" -while [ ! -s "$file" ]; do - - # Check if not shutting down +exitIfShuttingDown() { [ -f "$shutdown" ] && exit 1 +} - sleep 3 +queryGuest() { - [ -f "$shutdown" ] && exit 1 - [ -s "$file" ] && break - - # Retrieve network info from guest VM { json=$(curl -m 20 -sk "$url"); rc=$?; } || : - [ -f "$shutdown" ] && exit 1 - (( rc != 0 )) && error "$curl_err $rc" && continue + exitIfShuttingDown - { result=$(echo "$json" | jq -r '.status'); rc=$?; } || : - (( rc != 0 )) && error "$jq_err $rc ( $json )" && continue - [[ "$result" == "null" ]] && error "$resp_err $json" && continue + if (( rc != 0 )); then + error "$curl_err $rc" + return 1 + fi + + return 0 +} + +readJsonField() { + + local query="$1" + local result + + { result=$(echo "$json" | jq -r "$query"); rc=$?; } || : + + if (( rc != 0 )); then + error "$jq_err $rc ( $json )" + return 1 + fi + + if [[ "$result" == "null" ]]; then + error "$resp_err $json" + return 1 + fi + + printf '%s\n' "$result" +} + +readGuestStatus() { + + result=$(readJsonField '.status') || return 1 if [[ "$result" != "success" ]]; then { msg=$(echo "$json" | jq -r '.message'); rc=$?; } || : - error "Guest replied $result: $msg" && continue + error "Guest replied $result: $msg" + return 1 fi - { port=$(echo "$json" | jq -r '.data.data.dsm_setting.data.http_port'); rc=$?; } || : - (( rc != 0 )) && error "$jq_err $rc ( $json )" && continue - [[ "$port" == "null" ]] && error "$resp_err $json" && continue - [ -z "$port" ] && continue + return 0 +} - { ip=$(echo "$json" | jq -r '.data.data.ip.data[] | select((.name=="eth0") and has("ip")).ip'); rc=$?; } || : - (( rc != 0 )) && error "$jq_err $rc ( $json )" && continue - [[ "$ip" == "null" ]] && error "$resp_err $json" && continue +readGuestPort() { - [ -z "$ip" ] && continue + port=$(readJsonField '.data.data.dsm_setting.data.http_port') || return 1 + [ -z "$port" ] && return 1 + + return 0 +} + +readGuestIp() { + + ip=$(readJsonField '.data.data.ip.data[] | select((.name=="eth0") and has("ip")).ip') || return 1 + [ -z "$ip" ] && return 1 + + return 0 +} + +writeDsmLocation() { echo "$ip:$port" > "$file" +} -done +pollGuestLocation() { -[ -f "$shutdown" ] && exit 1 + while [ ! -s "$file" ]; do -location=$(<"$file") + # Check if not shutting down + exitIfShuttingDown -if enabled "$DHCP"; then + sleep 3 + + exitIfShuttingDown + [ -s "$file" ] && break + + # Retrieve network info from guest VM + queryGuest || continue + readGuestStatus || continue + readGuestPort || continue + readGuestIp || continue + writeDsmLocation + + done +} + +writeDhcpPage() { + + local title body script html msg="http://$location" title="Virtual DSM" body="The location of DSM is http://$location" script="" - HTML=$(<"$template") - HTML="${HTML/\[1\]/$title}" - HTML="${HTML/\[2\]/$script}" - HTML="${HTML/\[3\]/$body}" - HTML="${HTML/\[4\]/}" - HTML="${HTML/\[5\]/}" + html=$(<"$template") + html="${html/\[1\]/$title}" + html="${html/\[2\]/$script}" + html="${html/\[3\]/$body}" + html="${html/\[4\]/}" + html="${html/\[5\]/}" - echo "$HTML" > "$page" + echo "$html" > "$page" echo "$body" > "$msgs" +} -else +buildStaticMessage() { + + local nic ip port nic=$(<"$driver") ip=$(<"$address") @@ -97,13 +152,27 @@ else else msg="http://$ip:$port" fi +} +printLoginMessage() { + echo "" >&2 + info "-----------------------------------------------------------" + info " You can now login to DSM at $msg" + info "-----------------------------------------------------------" + echo "" >&2 +} + +pollGuestLocation +exitIfShuttingDown + +location=$(<"$file") + +if enabled "$DHCP"; then + writeDhcpPage +else + buildStaticMessage fi -echo "" >&2 -info "-----------------------------------------------------------" -info " You can now login to DSM at $msg" -info "-----------------------------------------------------------" -echo "" >&2 +printLoginMessage exit 0 diff --git a/src/proc.sh b/src/proc.sh index 3898262..7c58501 100644 --- a/src/proc.sh +++ b/src/proc.sh @@ -56,7 +56,6 @@ configureKvmCpuModel() { } appendKvmInvtscFeature() { - if grep -qw "svm" <<< "$flags"; then # AMD processor @@ -67,6 +66,7 @@ appendKvmInvtscFeature() { else # Intel processor + local vmx vmx=$(sed -ne '/^vmx flags/s/^.*: //p' /proc/cpuinfo) if grep -qw "tsc_scaling" <<< "$vmx"; then