fix: Allocate after conversion
This commit is contained in:
parent
0f550a8657
commit
40a944ec39
1 changed files with 28 additions and 16 deletions
44
src/disk.sh
44
src/disk.sh
|
@ -79,15 +79,16 @@ createDisk() {
|
||||||
local DISK_FMT=$4
|
local DISK_FMT=$4
|
||||||
local DIR SPACE DATA_SIZE
|
local DIR SPACE DATA_SIZE
|
||||||
|
|
||||||
|
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
|
||||||
|
@ -100,7 +101,7 @@ createDisk() {
|
||||||
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
|
||||||
|
@ -108,8 +109,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
|
||||||
|
@ -120,7 +121,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
|
||||||
|
@ -147,9 +148,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
|
||||||
|
@ -164,15 +165,15 @@ resizeDisk() {
|
||||||
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
|
||||||
|
@ -180,7 +181,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
|
||||||
;;
|
;;
|
||||||
|
@ -210,9 +211,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
|
||||||
|
@ -220,14 +221,12 @@ 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"
|
||||||
|
|
||||||
|
@ -237,6 +236,15 @@ convertDisk() {
|
||||||
error "Failed to convert $DISK_DESC to $DST_FMT format in $DIR, is there enough space available?" && exit 79
|
error "Failed to convert $DISK_DESC to $DST_FMT format in $DIR, is there enough space available?" && exit 79
|
||||||
fi
|
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
|
||||||
|
|
||||||
rm -f "$SOURCE_FILE"
|
rm -f "$SOURCE_FILE"
|
||||||
mv "$TMP_FILE" "$DST_FILE"
|
mv "$TMP_FILE" "$DST_FILE"
|
||||||
|
|
||||||
|
@ -281,6 +289,10 @@ addDisk () {
|
||||||
DISK_SPACE=$(echo "$DISK_SPACE" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g')
|
DISK_SPACE=$(echo "$DISK_SPACE" | sed 's/MB/M/g;s/GB/G/g;s/TB/T/g')
|
||||||
DATA_SIZE=$(numfmt --from=iec "$DISK_SPACE")
|
DATA_SIZE=$(numfmt --from=iec "$DISK_SPACE")
|
||||||
|
|
||||||
|
if (( DATA_SIZE < 1 )); then
|
||||||
|
error "Invalid value for ${DISK_DESC^^}_SIZE: $DISK_SPACE" && exit 73
|
||||||
|
fi
|
||||||
|
|
||||||
if ! [ -f "$DISK_FILE" ] ; then
|
if ! [ -f "$DISK_FILE" ] ; then
|
||||||
|
|
||||||
if [[ "${DISK_FMT,,}" != "raw" ]]; then
|
if [[ "${DISK_FMT,,}" != "raw" ]]; then
|
||||||
|
|
Loading…
Reference in a new issue