Skip to content

Instantly share code, notes, and snippets.

@thonixx
Last active May 6, 2020 13:01
Show Gist options
  • Save thonixx/b71b97fdefb505a43bd775dc321deefa to your computer and use it in GitHub Desktop.
Save thonixx/b71b97fdefb505a43bd775dc321deefa to your computer and use it in GitHub Desktop.
Resize a VM disk online and on-the-fly
##
## Prerequisites:
## - already resized disk on the VM host
##
## Notes:
## - I assume that we have three partitions (primary (root fs), extended, logical (swap))
## - I assume that we move swap out of the extended partition and convert to a normal primary one
## - I also assume that we place swap at the end of the disk
##
ssh _VM_
sudo swapoff -a
sudo parted /dev/_DISK_
parted> p
# now check if the new size is displayed already, otherwise halt the server and start it again
parted> rm 5
parted> rm 2
parted> p
# check if swap and logical partition is gone
parted> resizepart 1
# be sure to get the right End for the new partition minus the size of the new swap (2 GB i.e.)
End? [xyGB]? _NEWSIZE_GB
parted> p
# check if the root part has the right size and create swap with the right START and END
parted> mkpart primary linux-swap _START_ _END_
parted> p
# check if there are 2 primary partitions now
parted> quit
cat /proc/partitions
# check if the kernel updated the partition sizes correctly, otherwise issue "sudo partprobe" or cold-reboot again, then continue
sudo resize2fs /dev/_PARTITION_
df -h /dev/_PARTITION_
mkswap /dev/_PARTITION_
# write out the UID and edit it in /etc/fstab for the correct swap
vi /etc/fstab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment