One physical drive of my ESXi host is starting to act strange and after a couple of years I think it is a good idea to start migrating to a new drive. Unfortunately, I do not do this often enough to remember the process. Therefor, I intend to document it here and it could hopefully be of help to someone else.
- ESXi 5.1
- Ubuntu Server 12.04 virtual machine on “Datastore A”
- VM hard disk is thin provisioned
- Move the VM to “Datastore B”
- Reduce the used space of the VM on the new datastore
The VM was set up with a 1TB thin provisioned drive and the initial usage was up to 400GB. Later on I moved the majority of the used storage to a separate VM and the usage now is around 35GB. However, the previously used storage is not freed up and I intend to accomplish this by cloning the VM to a new virtual disk. As far as I know, there are other methods to free up space but I have not tried any of those yet. To be investigated…
- Add new thin virtual hard disk to the VM of the same size on the new datastore
- Boot the VM to a cloning tool (I have used Acronis, but there are other free competent alternatives)
- Clone the old drive to the new one keeping the same partition setup
- Shut down the VM and copy the .vmx-file to the same folder as the .vmdk on the new datastore (created in step 1)
- Remove the VM from the inventory. Do not delete the files from the datastore
- Browse the new datastore, right click on the copied .vmx-file and select Add to inventory
- Edit the settings of the VM to remove the old virtual drive.
- Select a Ubuntu Live CD image (preferable the same version as the VM) for the virtual CD drive.
- Start the VM. vSphere will pop up a dialogue asking if the VM was moved or copied, select moved.
- Boot the VM to a Ubuntu Live CD to fix the mounting and grub
- Boot into the new VM
Let’s explain some steps in greater detail.
4. Copy the VMX file
If this is the initial state:
After adding a second drive, VM_B.vmdk, on the other datastore (step 1), cloning VM.vmdk to VM_B.vmdk (step 3) and copying the VM.vmx to the VM-folder on datastoreB (step 4), the layout would be the following:
DatastoreA/VM/VM.vmdk DatastoreA/VM/VM.vmx DatastoreB/VM/VM_B.vmdk DatastoreA/VM/VM.vmx
10. Boot the VM to a Ubuntu Live CD to fix mounts and grub
This section is heavily dependent on the guest OS. Ubuntu 12.04 uses UUID’s to mount drives and to decide which drive to boot from. The new virtual drive will have a different UUID than the original drive and will therefor not be able to boot the OS. This is where the Live CD comes in.
Once inside the Live CD, launch a terminal and orientate yourself. To identify the UUIDs of the partition use:
$ ll /dev/disk/by-uuid/ 5de1...9831 -> ../../sda1 f038...185d -> ../../sda2
Next, let’s mount the drive:
$ sudo mount /dev/sdXY /mnt
where X is the drive letter and Y is the root partition.(If you have some exclusive partitioning setup you might need to mount the other partitions to be able to follow these steps. But then again, you probably know what you are doing anyway)
Change the UUID in fstab:
$ sudo nano /mnt/etc/fstab
Look for the following lines and change the old UUIDs to the new ones.
# / was on /dev/sda2 during installation UUID=f038...158d / ext4 discard,errors=remount-ro 0 1 # swap was on /dev/sda1 during installation UUID=5de1...5831 none swap sw 0 0
Next up is changing the grub device mapping. The following steps have I borrowed from HowToUbuntu.org (How to repair, restore of reinstall Grub).
$ sudo mount --bind /dev /mnt/dev $ sudo mount --bind /dev/pts /mnt/dev/pts $ sudo mount --bind /proc /mnt/proc $ sudo mount --bind /sys /mnt/sys $ sudo chroot /mnt
With the help of chroot, the grub tools will operate on the virtual drive rather than the live OS. Run the following commands to re-/install the boot loader again.
# grub-install /dev/sda # grub-install --recheck /dev/sda # update-grub
Lets exit the chroot and unmount the directories:
# exit $ sudo umount /mnt/dev/pts $ sudo umount /mnt/dev $ sudo umount /mnt/proc $ sudo umount /mnt/sys $ sudo umount /mnt
Now we should be all set. Shut down the VM, remove the virtual Live CD and boot up the new VM.