Created
August 23, 2016 02:57
-
-
Save karlkfi/aa7ad81930527d80a29d5df9a1023ed6 to your computer and use it in GitHub Desktop.
Resizing a vagrant centos disk... the hard way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Resizing a vagrant centos disk... the hard way | |
set -o errexit | |
set -o nounset | |
set -o pipefail | |
set -o xtrace | |
vagrant up | |
vagrant halt | |
# disk size in MB (default 100GB) | |
DISK_SIZE=${1:-102400} | |
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE}")/.." && pwd)" | |
VM_NAME="dcos-docker" | |
VBOX_VM_DIR="${HOME}/VirtualBox VMs/${VM_NAME}" | |
cd "${VBOX_VM_DIR}" | |
OLD_DISK_NAME="$(VBoxManage showvminfo "${VM_NAME}" | grep '.vmdk' | sed "s:.*\\${VM_NAME}/\(.*.vmdk\).*:\1:")" | |
TEMP_DISK_NAME="clone-disk1.vdi" | |
NEW_DISK_NAME="new-disk1.vmdk" | |
# if the following clonehd fails, you may need to nuke your vbox disks: | |
# VBoxManage list hdds | grep "^UUID:" | sed 's/UUID:[ \t]*//' | xargs -n1 VBoxManage closemedium disk | |
# clone, resize, and reformat | |
VBoxManage clonehd "${OLD_DISK_NAME}" "${TEMP_DISK_NAME}" --format vdi | |
VBoxManage modifyhd "${TEMP_DISK_NAME}" --resize ${DISK_SIZE} | |
VBoxManage clonehd "${TEMP_DISK_NAME}" "${NEW_DISK_NAME}" --format vmdk | |
rm "${TEMP_DISK_NAME}" | |
CONTROLLER_NAME="$(VBoxManage showvminfo "${VM_NAME}" | grep 'Storage Controller Name' | sed 's/Storage Controller Name (0):[ \t]*//')" | |
# attach new disk as the primary storage device | |
VBoxManage storageattach "${VM_NAME}" --storagectl "${CONTROLLER_NAME}" --port 0 --device 0 --type hdd --medium "${NEW_DISK_NAME}" | |
cd "${PROJECT_ROOT}" | |
vagrant up | |
#vagrant ssh -c "sudo /vagrant/vagrant/resize-inner.sh" | |
vagrant ssh << EOFOUTER | |
set -o errexit | |
set -o nounset | |
set -o pipefail | |
set -o xtrace | |
sudo su - | |
# NOTE: Must be run as root! | |
# Programmatically script fdisk input. | |
# The sed script strips the comments, which are present for maintainability. | |
# Blank lines will use the default option. | |
# TODO: find a way to ignore 'ioctl' error without ignoring all errors | |
sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | fdisk /dev/sda || true | |
n # new partition | |
p # primary partition | |
3 # partition number 3 | |
# default - start at beginning of disk | |
# default - extend partition to end of disk | |
t # change partition type | |
3 # partition number 3 | |
8e # set to Linux LVM (hex code) | |
p # print the in-memory partition table | |
w # write the partition table and quit | |
EOF | |
# refresh disk partition without rebooting | |
partprobe | |
# create physical volume from partition | |
pvcreate /dev/sda3 | |
# add the new physical volume to the centos volume group | |
vgextend centos /dev/sda3 | |
# resize the root logical volume to include the new space | |
lvextend -l +100%FREE /dev/mapper/centos-root | |
# grow the root file-system to fit the logical volume | |
xfs_growfs /dev/centos/root | |
# print new disk sizes (root & boot are xfs on centos) | |
df -h -t xfs | |
EOFOUTER | |
# nuke the old disk, now that we know we suceeded | |
rm "${VBOX_VM_DIR}/${OLD_DISK_NAME}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment