Skip to content

Commit

Permalink
Fix external image/disk export scripts to work with Debian 12. (#2420)
Browse files Browse the repository at this point in the history
  • Loading branch information
zmarano authored Dec 11, 2024
1 parent f78741a commit 46b15e8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
3 changes: 2 additions & 1 deletion daisy_workflows/export/disk_export_ext.wf.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
"block-project-ssh-keys": "true",
"gcs-path": "${OUTSPATH}/${NAME}",
"format": "${format}",
"buffer-disk": "disk-${NAME}-buffer-${ID}",
"buffer-disk-name": "disk-${NAME}-buffer-${ID}",
"source-disk-name": "${source_disk}",
"resizing-script-name": "${NAME}_disk_resizing_mon.sh",
"startup-script": "${SOURCE:${NAME}_export_disk_ext.sh}"
},
Expand Down
13 changes: 7 additions & 6 deletions daisy_workflows/export/disk_resizing_mon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,30 @@ INTERVAL=10

# Prepare parameters for resizing
ZONE=$(curl "${METADATA_URL}/zone" -H "Metadata-Flavor: Google"| cut -d'/' -f4)
BUFFER_DISK=$(curl "${METADATA_URL}/attributes/buffer-disk" -H "Metadata-Flavor: Google")
BUFFER_DISK_NAME=$(curl "${METADATA_URL}/attributes/buffer-disk-name" -H "Metadata-Flavor: Google")
BUFFER_DISK=$(readlink -f /dev/disk/by-id/google-${BUFFER_DISK_NAME})

echo "Max disk size ${MAX_SIZE}GB, min buffer size ${BUFFER_SIZE}GB, starting monitoring available disk buffer every ${INTERVAL}s..."
while sleep ${INTERVAL}; do
# Check whether available buffer space is lower than threshold.
AVAILABLE_BUFFER=$(df -BG /dev/sdc --output=avail | sed -n 2p)
AVAILABLE_BUFFER=$(df -BG ${BUFFER_DISK} --output=avail | sed -n 2p)
AVAILABLE_BUFFER=${AVAILABLE_BUFFER%?}
if [[ ${AVAILABLE_BUFFER} -ge ${BUFFER_MIN_SIZE} ]]; then
continue
fi

# Decide the new size of the device.
CURRENT_DEVICE_SIZE_BYTES=$(lsblk /dev/sdc --output=size -b | sed -n 2p)
CURRENT_DEVICE_SIZE_BYTES=$(lsblk ${BUFFER_DISK} --output=size -b | sed -n 2p)
CURRENT_DEVICE_SIZE=$(awk "BEGIN {print int(((${CURRENT_DEVICE_SIZE_BYTES}-1)/${BYTES_1GB}) + 1)}")
NEXT_SIZE=$(awk "BEGIN {print int(${CURRENT_DEVICE_SIZE} + ${BUFFER_SIZE})}")

echo "GCEExport: Resizing buffer disk from ${CURRENT_DEVICE_SIZE}GB to ${NEXT_SIZE}GB..."
if ! out=$(gcloud compute disks resize ${BUFFER_DISK} --size=${NEXT_SIZE}GB --quiet --zone "${ZONE}" 2>&1); then
if ! out=$(gcloud compute disks resize ${BUFFER_DISK_NAME} --size=${NEXT_SIZE}GB --quiet --zone "${ZONE}" 2>&1); then
echo "ExportFailed: Failed to resize buffer disk. [Privacy-> Error: ${out} <-Privacy]"
continue
fi
echo ${out}
if ! out=$(sudo resize2fs /dev/sdc 2>&1); then
if ! out=$(sudo resize2fs ${BUFFER_DISK} 2>&1); then
echo "ExportFailed: Failed to resize partition of buffer disk. [Privacy-> Error: ${out} <-Privacy]"
continue
fi
Expand All @@ -41,7 +42,7 @@ while sleep ${INTERVAL}; do
# If current file system has reached or exceeded max size, then stop resizing.
# We need to know the size of the available file system other than the size of
# the partition, so "df" is used instead of "lsblk" here.
CURRENT_FILESYSTEM_SIZE=$(df -BG /dev/sdc --output=size | sed -n 2p)
CURRENT_FILESYSTEM_SIZE=$(df -BG ${BUFFER_DISK} --output=size | sed -n 2p)
CURRENT_FILESYSTEM_SIZE=${CURRENT_FILESYSTEM_SIZE%?}
if [[ ${CURRENT_FILESYSTEM_SIZE} -ge ${MAX_SIZE} ]]; then
echo "Buffer disk reaches max size."
Expand Down
12 changes: 8 additions & 4 deletions daisy_workflows/export/export_disk_ext.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ URL="http://metadata/computeMetadata/v1/instance/attributes"
GS_PATH=$(curl -f -H Metadata-Flavor:Google ${URL}/gcs-path)
FORMAT=$(curl -f -H Metadata-Flavor:Google ${URL}/format)
DISK_RESIZING_MON=$(curl -f -H Metadata-Flavor:Google ${URL}/resizing-script-name)
SOURCE_DISK_NAME=$(curl -f -H Metadata-Flavor:Google ${URL}/source-disk-name)
BUFFER_DISK_NAME=$(curl -f -H Metadata-Flavor:Google ${URL}/buffer-disk-name)
SOURCE_DISK=$(readlink -f /dev/disk/by-id/google-${SOURCE_DISK_NAME})
BUFFER_DISK=$(readlink -f /dev/disk/by-id/google-${BUFFER_DISK_NAME})

# Strip gs://
IMAGE_OUTPUT_PATH=${GS_PATH##*//}
Expand All @@ -39,7 +43,7 @@ mkdir -p "/gs/${OUTS_PATH}"

# Prepare disk size info.
# 1. Disk image size info.
SIZE_BYTES=$(lsblk /dev/sdb --output=size -b | sed -n 2p)
SIZE_BYTES=$(lsblk ${SOURCE_DISK} --output=size -b | sed -n 2p)
# 2. Round up to the next GB.
SIZE_OUTPUT_GB=$(awk "BEGIN {print int(((${SIZE_BYTES}-1)/${BYTES_1GB}) + 1)}")
# 3. Add 5GB of additional space to max size to prevent the corner case that output
Expand All @@ -52,8 +56,8 @@ set -x

# Prepare buffer disk.
echo "GCEExport: Initializing buffer disk for qemu-img output..."
mkfs.ext4 /dev/sdc
mount /dev/sdc "/gs/${OUTS_PATH}"
mkfs.ext4 ${BUFFER_DISK}
mount ${BUFFER_DISK} "/gs/${OUTS_PATH}"
if [[ $? -ne 0 ]]; then
echo "ExportFailed: Failed to prepare buffer disk by mkfs + mount."
fi
Expand All @@ -73,7 +77,7 @@ chmod +x ${DISK_RESIZING_MON_LOCAL_PATH}
${DISK_RESIZING_MON_LOCAL_PATH} ${MAX_BUFFER_DISK_SIZE_GB} &

echo "GCEExport: Exporting disk of size ${SIZE_OUTPUT_GB}GB and format ${FORMAT}."
if ! out=$(qemu-img convert /dev/sdb "/gs/${IMAGE_OUTPUT_PATH}" -p -O $FORMAT 2>&1); then
if ! out=$(qemu-img convert -p -O $FORMAT ${SOURCE_DISK} "/gs/${IMAGE_OUTPUT_PATH}" 2>&1); then
echo "ExportFailed: Failed to export disk source to GCS [Privacy-> ${GS_PATH} <-Privacy] due to qemu-img error: [Privacy-> ${out} <-Privacy]"
exit
fi
Expand Down

0 comments on commit 46b15e8

Please sign in to comment.