mirror of
https://github.com/vdsm/virtual-dsm.git
synced 2025-11-07 02:23:42 +08:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ef5b650991 | ||
|
|
fd19c7b4f3 | ||
|
|
727297642c | ||
|
|
cd457801e7 | ||
|
|
392e7afdfe | ||
|
|
b425e34907 |
57
src/disk.sh
57
src/disk.sh
@@ -82,30 +82,32 @@ createDisk() {
|
|||||||
local DISK_SPACE=$2
|
local DISK_SPACE=$2
|
||||||
local DISK_DESC=$3
|
local DISK_DESC=$3
|
||||||
local DISK_FMT=$4
|
local DISK_FMT=$4
|
||||||
local DIR SPACE DATA_SIZE
|
local DATA_SIZE DIR SPACE
|
||||||
|
|
||||||
|
DATA_SIZE=$(numfmt --from=iec "$DISK_SPACE")
|
||||||
|
|
||||||
if [[ "$ALLOCATE" != [Nn]* ]]; then
|
if [[ "$ALLOCATE" != [Nn]* ]]; then
|
||||||
|
|
||||||
# Check free diskspace
|
# Check free diskspace
|
||||||
DIR=$(dirname "$DISK_FILE")
|
DIR=$(dirname "$DISK_FILE")
|
||||||
SPACE=$(df --output=avail -B 1 "$DIR" | tail -n 1)
|
SPACE=$(df --output=avail -B 1 "$DIR" | tail -n 1)
|
||||||
local SPACE_GB=$(( (SPACE + 1073741823)/1073741824 ))
|
|
||||||
DATA_SIZE=$(numfmt --from=iec "$DISK_SPACE")
|
|
||||||
|
|
||||||
if (( DATA_SIZE > SPACE )); then
|
if (( DATA_SIZE > SPACE )); then
|
||||||
|
local SPACE_GB=$(( (SPACE + 1073741823)/1073741824 ))
|
||||||
error "Not enough free space to create a $DISK_DESC of $DISK_SPACE in $DIR, it has only $SPACE_GB GB available..."
|
error "Not enough free space to create a $DISK_DESC of $DISK_SPACE in $DIR, it has only $SPACE_GB GB available..."
|
||||||
error "Please specify a smaller ${DISK_DESC^^}_SIZE or disable preallocation by setting ALLOCATE=N." && exit 76
|
error "Please specify a smaller ${DISK_DESC^^}_SIZE or disable preallocation by setting ALLOCATE=N." && exit 76
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local FAIL="Could not create a $DISK_SPACE $DISK_FMT file for $DISK_DESC ($DISK_FILE)"
|
info "Creating a $DISK_TYPE $DISK_DESC image in $DISK_FMT format with a size of $DISK_SPACE..."
|
||||||
|
local FAIL="Could not create a $DISK_TYPE $DISK_FMT $DISK_DESC image of $DISK_SPACE ($DISK_FILE)"
|
||||||
|
|
||||||
case "${DISK_FMT,,}" in
|
case "${DISK_FMT,,}" in
|
||||||
raw)
|
raw)
|
||||||
if [[ "$ALLOCATE" == [Nn]* ]]; then
|
if [[ "$ALLOCATE" == [Nn]* ]]; then
|
||||||
|
|
||||||
# Create an empty file
|
# Create an empty file
|
||||||
if ! truncate -s "$DISK_SPACE" "$DISK_FILE"; then
|
if ! truncate -s "$DATA_SIZE" "$DISK_FILE"; then
|
||||||
rm -f "$DISK_FILE"
|
rm -f "$DISK_FILE"
|
||||||
error "$FAIL" && exit 77
|
error "$FAIL" && exit 77
|
||||||
fi
|
fi
|
||||||
@@ -113,8 +115,8 @@ createDisk() {
|
|||||||
else
|
else
|
||||||
|
|
||||||
# Create an empty file
|
# Create an empty file
|
||||||
if ! fallocate -l "$DISK_SPACE" "$DISK_FILE"; then
|
if ! fallocate -l "$DATA_SIZE" "$DISK_FILE"; then
|
||||||
if ! truncate -s "$DISK_SPACE" "$DISK_FILE"; then
|
if ! truncate -s "$DATA_SIZE" "$DISK_FILE"; then
|
||||||
rm -f "$DISK_FILE"
|
rm -f "$DISK_FILE"
|
||||||
error "$FAIL" && exit 77
|
error "$FAIL" && exit 77
|
||||||
fi
|
fi
|
||||||
@@ -125,7 +127,7 @@ createDisk() {
|
|||||||
qcow2)
|
qcow2)
|
||||||
local DISK_OPTS="$DISK_ALLOC"
|
local DISK_OPTS="$DISK_ALLOC"
|
||||||
[ -n "$DISK_FLAGS" ] && DISK_OPTS="$DISK_OPTS,$DISK_FLAGS"
|
[ -n "$DISK_FLAGS" ] && DISK_OPTS="$DISK_OPTS,$DISK_FLAGS"
|
||||||
if ! qemu-img create -f "$DISK_FMT" -o "$DISK_OPTS" -- "$DISK_FILE" "$DISK_SPACE" ; then
|
if ! qemu-img create -f "$DISK_FMT" -o "$DISK_OPTS" -- "$DISK_FILE" "$DATA_SIZE" ; then
|
||||||
rm -f "$DISK_FILE"
|
rm -f "$DISK_FILE"
|
||||||
error "$FAIL" && exit 70
|
error "$FAIL" && exit 70
|
||||||
fi
|
fi
|
||||||
@@ -152,9 +154,9 @@ resizeDisk() {
|
|||||||
# Check free diskspace
|
# Check free diskspace
|
||||||
DIR=$(dirname "$DISK_FILE")
|
DIR=$(dirname "$DISK_FILE")
|
||||||
SPACE=$(df --output=avail -B 1 "$DIR" | tail -n 1)
|
SPACE=$(df --output=avail -B 1 "$DIR" | tail -n 1)
|
||||||
local SPACE_GB=$(( (SPACE + 1073741823)/1073741824 ))
|
|
||||||
|
|
||||||
if (( REQ > SPACE )); then
|
if (( REQ > SPACE )); then
|
||||||
|
local SPACE_GB=$(( (SPACE + 1073741823)/1073741824 ))
|
||||||
error "Not enough free space to resize $DISK_DESC to $DISK_SPACE in $DIR, it has only $SPACE_GB GB available.."
|
error "Not enough free space to resize $DISK_DESC to $DISK_SPACE in $DIR, it has only $SPACE_GB GB available.."
|
||||||
error "Please specify a smaller ${DISK_DESC^^}_SIZE or disable preallocation by setting ALLOCATE=N." && exit 74
|
error "Please specify a smaller ${DISK_DESC^^}_SIZE or disable preallocation by setting ALLOCATE=N." && exit 74
|
||||||
fi
|
fi
|
||||||
@@ -162,22 +164,22 @@ resizeDisk() {
|
|||||||
|
|
||||||
local GB=$(( (CUR_SIZE + 1073741823)/1073741824 ))
|
local GB=$(( (CUR_SIZE + 1073741823)/1073741824 ))
|
||||||
info "Resizing $DISK_DESC from ${GB}G to $DISK_SPACE..."
|
info "Resizing $DISK_DESC from ${GB}G to $DISK_SPACE..."
|
||||||
local FAIL="Could not resize $DISK_FMT file of $DISK_DESC ($DISK_FILE) from ${GB}G to $DISK_SPACE .."
|
local FAIL="Could not resize the $DISK_TYPE $DISK_FMT $DISK_DESC image from ${GB}G to $DISK_SPACE ($DISK_FILE)"
|
||||||
|
|
||||||
case "${DISK_FMT,,}" in
|
case "${DISK_FMT,,}" in
|
||||||
raw)
|
raw)
|
||||||
if [[ "$ALLOCATE" == [Nn]* ]]; then
|
if [[ "$ALLOCATE" == [Nn]* ]]; then
|
||||||
|
|
||||||
# Resize file by changing its length
|
# Resize file by changing its length
|
||||||
if ! truncate -s "$DISK_SPACE" "$DISK_FILE"; then
|
if ! truncate -s "$DATA_SIZE" "$DISK_FILE"; then
|
||||||
error "$FAIL" && exit 75
|
error "$FAIL" && exit 75
|
||||||
fi
|
fi
|
||||||
|
|
||||||
else
|
else
|
||||||
|
|
||||||
# Resize file by allocating more space
|
# Resize file by allocating more space
|
||||||
if ! fallocate -l "$DISK_SPACE" "$DISK_FILE"; then
|
if ! fallocate -l "$DATA_SIZE" "$DISK_FILE"; then
|
||||||
if ! truncate -s "$DISK_SPACE" "$DISK_FILE"; then
|
if ! truncate -s "$DATA_SIZE" "$DISK_FILE"; then
|
||||||
error "$FAIL" && exit 75
|
error "$FAIL" && exit 75
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
@@ -185,7 +187,7 @@ resizeDisk() {
|
|||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
qcow2)
|
qcow2)
|
||||||
if ! qemu-img resize -f "$DISK_FMT" "--$DISK_ALLOC" "$DISK_FILE" "$DISK_SPACE" ; then
|
if ! qemu-img resize -f "$DISK_FMT" "--$DISK_ALLOC" "$DISK_FILE" "$DATA_SIZE" ; then
|
||||||
error "$FAIL" && exit 72
|
error "$FAIL" && exit 72
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
@@ -215,9 +217,9 @@ convertDisk() {
|
|||||||
DIR=$(dirname "$TMP_FILE")
|
DIR=$(dirname "$TMP_FILE")
|
||||||
CUR_SIZE=$(getSize "$SOURCE_FILE")
|
CUR_SIZE=$(getSize "$SOURCE_FILE")
|
||||||
SPACE=$(df --output=avail -B 1 "$DIR" | tail -n 1)
|
SPACE=$(df --output=avail -B 1 "$DIR" | tail -n 1)
|
||||||
local SPACE_GB=$(( (SPACE + 1073741823)/1073741824 ))
|
|
||||||
|
|
||||||
if (( CUR_SIZE > SPACE )); then
|
if (( CUR_SIZE > SPACE )); then
|
||||||
|
local SPACE_GB=$(( (SPACE + 1073741823)/1073741824 ))
|
||||||
error "Not enough free space to convert $DISK_DESC to $DST_FMT in $DIR, it has only $SPACE_GB GB available..."
|
error "Not enough free space to convert $DISK_DESC to $DST_FMT in $DIR, it has only $SPACE_GB GB available..."
|
||||||
error "Please free up some disk space or disable preallocation by setting ALLOCATE=N." && exit 76
|
error "Please free up some disk space or disable preallocation by setting ALLOCATE=N." && exit 76
|
||||||
fi
|
fi
|
||||||
@@ -225,21 +227,28 @@ convertDisk() {
|
|||||||
|
|
||||||
info "Converting $DISK_DESC to $DST_FMT, please wait until completed..."
|
info "Converting $DISK_DESC to $DST_FMT, please wait until completed..."
|
||||||
|
|
||||||
case "$DST_FMT" in
|
if [[ "$DST_FMT" != "raw" ]]; then
|
||||||
qcow2)
|
|
||||||
if [[ "$ALLOCATE" == [Nn]* ]]; then
|
if [[ "$ALLOCATE" == [Nn]* ]]; then
|
||||||
CONV_FLAGS="$CONV_FLAGS -c"
|
CONV_FLAGS="$CONV_FLAGS -c"
|
||||||
fi
|
fi
|
||||||
[ -n "$DISK_FLAGS" ] && DISK_OPTS="$DISK_OPTS,$DISK_FLAGS"
|
[ -n "$DISK_FLAGS" ] && DISK_OPTS="$DISK_OPTS,$DISK_FLAGS"
|
||||||
;;
|
fi
|
||||||
esac
|
|
||||||
|
|
||||||
rm -f "$TMP_FILE"
|
rm -f "$TMP_FILE"
|
||||||
|
|
||||||
# shellcheck disable=SC2086
|
# shellcheck disable=SC2086
|
||||||
if ! qemu-img convert -f "$SOURCE_FMT" $CONV_FLAGS -o "$DISK_OPTS" -O "$DST_FMT" -- "$SOURCE_FILE" "$TMP_FILE"; then
|
if ! qemu-img convert -f "$SOURCE_FMT" $CONV_FLAGS -o "$DISK_OPTS" -O "$DST_FMT" -- "$SOURCE_FILE" "$TMP_FILE"; then
|
||||||
rm -f "$TMP_FILE"
|
rm -f "$TMP_FILE"
|
||||||
error "Failed to convert $DISK_DESC to $DST_FMT format in $DIR, is there enough space available?" && exit 79
|
error "Failed to convert $DISK_TYPE $DISK_DESC image to $DST_FMT format in $DIR, is there enough space available?" && exit 79
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ "$DST_FMT" == "raw" ]]; then
|
||||||
|
if [[ "$ALLOCATE" != [Nn]* ]]; then
|
||||||
|
CUR_SIZE=$(stat -c%s "$TMP_FILE")
|
||||||
|
if ! fallocate -l "$CUR_SIZE" "$TMP_FILE"; then
|
||||||
|
info "Failed to allocate $CUR_SIZE bytes for $TMP_FILE"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
rm -f "$SOURCE_FILE"
|
rm -f "$SOURCE_FILE"
|
||||||
@@ -287,7 +296,11 @@ addDisk () {
|
|||||||
DATA_SIZE=$(numfmt --from=iec "$DISK_SPACE")
|
DATA_SIZE=$(numfmt --from=iec "$DISK_SPACE")
|
||||||
|
|
||||||
if (( DATA_SIZE < 6442450944 )); then
|
if (( DATA_SIZE < 6442450944 )); then
|
||||||
error "Please increase ${DISK_DESC^^}_SIZE to at least 6 GB." && exit 73
|
if (( DATA_SIZE < 1 )); then
|
||||||
|
error "Invalid value for ${DISK_DESC^^}_SIZE: $DISK_SPACE" && exit 73
|
||||||
|
else
|
||||||
|
error "Please increase ${DISK_DESC^^}_SIZE to at least 6 GB." && exit 73
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! [ -f "$DISK_FILE" ] ; then
|
if ! [ -f "$DISK_FILE" ] ; then
|
||||||
@@ -329,8 +342,10 @@ addDisk () {
|
|||||||
DISK_EXT="$(fmt2ext "$DISK_FMT")" || exit $?
|
DISK_EXT="$(fmt2ext "$DISK_FMT")" || exit $?
|
||||||
|
|
||||||
if [[ "$ALLOCATE" == [Nn]* ]]; then
|
if [[ "$ALLOCATE" == [Nn]* ]]; then
|
||||||
|
DISK_TYPE="growable"
|
||||||
DISK_ALLOC="preallocation=off"
|
DISK_ALLOC="preallocation=off"
|
||||||
else
|
else
|
||||||
|
DISK_TYPE="preallocated"
|
||||||
DISK_ALLOC="preallocation=falloc"
|
DISK_ALLOC="preallocation=falloc"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
18
src/power.sh
18
src/power.sh
@@ -52,19 +52,17 @@ _graceful_shutdown() {
|
|||||||
|
|
||||||
while [ "$(cat $QEMU_COUNT)" -lt "$QEMU_TIMEOUT" ]; do
|
while [ "$(cat $QEMU_COUNT)" -lt "$QEMU_TIMEOUT" ]; do
|
||||||
|
|
||||||
# Increase the counter
|
|
||||||
echo $(($(cat $QEMU_COUNT)+1)) > "$QEMU_COUNT"
|
|
||||||
|
|
||||||
# Try to connect to qemu
|
# Try to connect to qemu
|
||||||
if echo 'info version'| nc -q 1 -w 1 localhost "$QEMU_PORT" >/dev/null 2>&1 ; then
|
if ! echo 'info version'| nc -q 1 -w 1 localhost "$QEMU_PORT" >/dev/null 2>&1 ; then
|
||||||
|
break
|
||||||
sleep 1
|
|
||||||
|
|
||||||
cnt="$(cat $QEMU_COUNT)/$QEMU_TIMEOUT"
|
|
||||||
[[ "$DEBUG" == [Yy1]* ]] && info "Shutting down, waiting... ($cnt)"
|
|
||||||
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Increase the counter
|
||||||
|
cnt=$(($(cat $QEMU_COUNT)+1))
|
||||||
|
echo $cnt > "$QEMU_COUNT"
|
||||||
|
|
||||||
|
[[ "$DEBUG" == [Yy1]* ]] && info "Shutting down, waiting... ($cnt/$QEMU_TIMEOUT)"
|
||||||
|
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ "$(cat $QEMU_COUNT)" -ge "$QEMU_TIMEOUT" ]; then
|
if [ "$(cat $QEMU_COUNT)" -ge "$QEMU_TIMEOUT" ]; then
|
||||||
|
|||||||
10
src/print.sh
10
src/print.sh
@@ -1,6 +1,8 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -Eeuo pipefail
|
set -Eeuo pipefail
|
||||||
|
|
||||||
|
: ${DHCP:='N'}
|
||||||
|
|
||||||
info () { printf "%b%s%b" "\E[1;34m❯ \E[1;36m" "$1" "\E[0m\n" >&2; }
|
info () { printf "%b%s%b" "\E[1;34m❯ \E[1;36m" "$1" "\E[0m\n" >&2; }
|
||||||
error () { printf "%b%s%b" "\E[1;31m❯ " "ERROR: $1" "\E[0m\n" >&2; }
|
error () { printf "%b%s%b" "\E[1;31m❯ " "ERROR: $1" "\E[0m\n" >&2; }
|
||||||
|
|
||||||
@@ -22,7 +24,7 @@ do
|
|||||||
[ -f "$shutdown" ] && exit 1
|
[ -f "$shutdown" ] && exit 1
|
||||||
[ -f "$file" ] && break
|
[ -f "$file" ] && break
|
||||||
|
|
||||||
# Retrieve IP from guest VM
|
# Retrieve network info from guest VM
|
||||||
{ json=$(curl -m 20 -sk "$url"); rc=$?; } || :
|
{ json=$(curl -m 20 -sk "$url"); rc=$?; } || :
|
||||||
|
|
||||||
[ -f "$shutdown" ] && exit 1
|
[ -f "$shutdown" ] && exit 1
|
||||||
@@ -45,7 +47,11 @@ do
|
|||||||
{ ip=$(echo "$json" | jq -r '.data.data.ip.data[] | select((.name=="eth0") and has("ip")).ip'); rc=$?; } || :
|
{ 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
|
(( rc != 0 )) && error "$jq_err $rc ( $json )" && continue
|
||||||
[[ "$ip" == "null" ]] && error "$resp_err $json" && continue
|
[[ "$ip" == "null" ]] && error "$resp_err $json" && continue
|
||||||
[ -z "$ip" ] && continue
|
|
||||||
|
if [ -z "$ip" ]; then
|
||||||
|
[[ "$DHCP" == [Yy1]* ]] && continue
|
||||||
|
ip="20.20.20.21"
|
||||||
|
fi
|
||||||
|
|
||||||
echo "$ip:$port" > $file
|
echo "$ip:$port" > $file
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user