Skip to content

Instantly share code, notes, and snippets.

@platu
Last active June 14, 2024 13:07
Show Gist options
  • Save platu/80abe91df20868cc8e49e1de0f23a8ed to your computer and use it in GitHub Desktop.
Save platu/80abe91df20868cc8e49e1de0f23a8ed to your computer and use it in GitHub Desktop.
IaC Lab01 vm image file resize script
#!/bin/bash
resize_cmd="virt-resize"
if ! command -v "${resize_cmd}" &>/dev/null; then
echo "Command '${resize_cmd}' not found. Please install 'libguestfs-tools' package."
exit 1
fi
# This script resizes $1 VM disk image adding $2 GB to the original size
vm="$1"
shift
size="$1"
if [[ -z ${vm} ]] || [[ -z ${size} ]]; then
echo "Usage: $0 <vm> <size>"
exit 1
fi
qemu-img resize "${vm}" "+${size}"
cp "${vm}" "${vm}.orig"
${resize_cmd} --expand /dev/sda1 "${vm}.orig" "${vm}"
rm "${vm}.orig"
info=$(qemu-img info "${vm}") || true
vm_name=$(basename "${vm}")
grep "virtual size:" <<<"${info}" >"${vm_name}"_resized.txt
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment