I have several VMs set up within Hyper-V using Ubuntu 17.10 and have found that they are not configured correctly - that is, they have been set up using the default block size for the VHDX files and are using much more space than they require and have a higher maximum VHDHX size than I would like. In one example, I have a drive which is ~50GB but only contains 15GB of data.
I am looking to create new VHDHX files using more sensible settings and clone everything that is on the drives on to the newly created drives, but I'm running into some problems. The existing drives are showing up as having 2 partitions as shown here, and then there are 2 logical volumes on the larger partition - one for data and one for swap.
The drive I am looking to clone this to will be a 30GB VHDHX file, and I need some advice on the best way to go about doing this. I initially tried using dd and encountered errors due to the source being larger than the destination. I then tried using gparted to resize the larger drive, but could not due to gparted seeing the LVM partition as having no free space. I also tried reducing the size of the LVM partition and then using gparted, but also ran into errors with this. I've been able to successfully reduce the size of the LVM partition and then use dd to clone the LVM volume to another LVM volume I have created on the new drive, though I am concerned that when I use lvreduce to resize the original partition I may be cutting off data if the data is not contiguous and is spread across the drive.
I am quite new to this, so do not know what I may be overlooking and may need to account for in doing it, but essentially I'm looking for some guidance on the required steps to ensure that the new drive contains everything that is on the old one and I can boot from it. If for example, I use lvreduce to resize the large volume to the size I need, how do I verify that I have not lost anything in doing this? Would this even be the correct approach?
So far my plan is:
- Create a new Ubuntu VM to use to perform this process.
- Attach the original drive + a new drive to be used for copying the data.
- Shrink the logical volume on the original drive.
- Create the same partitions & volumes on the new drive.
- Use dd to copy the logical volumes to the equivalent logical volumes on the new drive.
Is there a better way to do this, or anything I have missed?/pitfalls to be aware of?