Skip to content

Instantly share code, notes, and snippets.

@webevt
Last active November 15, 2017 23:30
Show Gist options
  • Save webevt/3f7c8990fd80f6f43052ed5a28769cbd to your computer and use it in GitHub Desktop.
Save webevt/3f7c8990fd80f6f43052ed5a28769cbd to your computer and use it in GitHub Desktop.
Resize Virtualbox Disk
# Find your VM name
VBoxManage list vms
VMNAME="YOUR_VM_NAME"
# Get VM disk path.
VMDISK1=$(VBoxManage showvminfo "${VMNAME}" --machinereadable | grep -E '^\"SATA' | head -n1 | cut -d'=' -f2 | sed -e 's/^"//' -e 's/"$//')
# Convert disk to VDI
if [ "${VMDISK##*.}" != "vdi" ]; then
VMDISK2="${VMDISK1%%.*}.$(cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1).vdi"
VBoxManage clonemedium "${VMDISK1}" "${VMDISK2}" --format VDI
# Attach new disk
VMDISK_INFO=$(VBoxManage showvminfo "${VMNAME}" --machinereadable | grep -E '^\"SATA' | head -n1 | cut -d'=' -f1 | sed -e 's/^"//' -e 's/"$//')
VMDISK_CONTROLLER=$(echo "${VMDISK_INFO}" | cut -d'-' -f1)
VMDISK_PORT=$(echo "${VMDISK_INFO}" | cut -d'-' -f2)
VMDISK_DEVICE=$(echo "${VMDISK_INFO}" | cut -d'-' -f3)
VBoxManage storageattach "${VMNAME}" \
--storagectl "${VMDISK_CONTROLLER}" \
--port "${VMDISK_PORT}" \
--device "${VMDISK_DEVICE}" \
--type hdd \
--medium "${VMDISK2}"
else
VMDISK2="${VMDISK1}"
fi
# Resize disk
VBoxManage modifymedium "${VMDISK2}" --resize NEW_SIZE
# Resize primary partition with GParted Live CD.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment