📜 ⬆️ ⬇️

Reducing the size of a thin disk in ESXi

When I started working with ESXi, I ran into the problem of insufficient disk space due to heavily expanded thin disks. In general, it is better to create smaller disks, because it is much easier to enlarge a disk in ESXi than to reduce it. But what to do if you still need to reduce the disk? Moreover, my position was aggravated by LVM sections on the server that Acronis did not recognize, and, therefore, could not change the size of the section.
I will describe the way I used:

Stage 1) Reducing the disk inside the virtual machine
First you need to reduce the disk inside the virtual machine itself, for this there are many universal programs: Acronis disc director, paragon partition manager, built-in disk management utility in windows, etc. None of these programs helped me, so I had to use the Linux and LiveCD tools.

1) First you need to boot from livecd linux, I used centos 7 for this. When booting the virtual machine, the ESC key for the boot menu appears.

2) Activate LVM sections
vgchange -ay 

3) Verification of activated logical volumes
 lvscan 

4) Check file systems
 fsck -fy /dev/test/root fsck -fy /dev/test/home 

5) Reduce the size of the file system
 resize2fs /dev/test/home 20G 

')
6) Reduce the size of the volume to the size of the file system
 lvreduce -L 20G /dev/test/home 


We can also add this place to another one.
For this:

Increase the root volume:
 lvextend -L +10G /dev/test/root 

Increasing the size of the file system on the root volume to the size of the volume:
 resize2fs /dev/test/root 

7) Again we check the integrity of the file system
 fsck -fy /dev/test/root fsck -fy /dev/test/home 

8) Next, you can start the virtual machine and make sure that everything works correctly
 df -h 


Stage 2) reduction of provisioning space.

10) Turn off the virtual machine
11) Connect via SSH to the physical server on which ESXi is installed
12) Go to the directory in which the vmdk VM file is stored (the path can be found in the properties of the virtual disk in the vSphere graphical client). I have it
 cd /vmfs/volumes/datastore1*/ cd VM_name/ 

The catalog has a similar structure:
Virtual machine directory structure
/ vmfs / volumes / 53114b32-24d88d5a-2cbe-0025b500004f / MTS-SCPortal-TEST3 # ls -l
total 19810320
-rw-r - r-- 1 root root 27 Dec 20 20:46 VM_name-0b1f4705.hlog
-rw ------- 1 root 6442450944 Feb 2 08:24 VM_name-0b1f4705.vswp
-rw ------- 1 root 147102629888 Feb 2 08:28 VM_name-flat.vmdk
-rw ------- 1 root root 8684 Feb 2 08:25 VM_name.nvram
-rw ------- 1 root 507 Feb 2 08:25 VM_name.vmdk
-rw-rrr-- 1 root root 0 Dec 20 20:14 VM_name.vmsd
-rwxr-xr-x 1 root root 3584 Feb 2 08:25 VM_name.vmx
-rw ------- 1 root root 0 Feb 2 08:24 VM_name.vmx.lck
-rw-rrr-- 1 root root 273 Dec 20 20:14 VM_name.vmxf
-rwxr-xr-x 1 root root 3584 Feb 2 08:25 VM_name.vmx ~
-rw ------- 1 root 3424256 Feb 2 08:24 vmmcores-1.gz
-rw-rrr-- 1 root root 240722 Dec 20 20:46 vmware-1.log
-rw-rrr-- 1 root root 1252287 Feb 2 08:24 vmware-2.log
-rw-rrr-- 1 root root 43555 Feb 2 08:24 vmware-3.log
-rw-r - r-- 1 root root 151873 Feb 2 08:25 vmware.log
-rw ------- 1 root root 122683392 Feb 2 08:24 vmx-VM_name-186599173-1.vswp
/ vmfs / volumes / 53114b32-24d88d5a-2cbe-0025b500004f / VM_name #

13) View the contents of the configuration file with the extension * .vmdk using the cat or vi command:
 cat VM_name.vmdk 

Sample contents of the virtual machine configuration file
# Disk DescriptorFile
version = 1
encoding = "UTF-8"
CID = 829544ab
parentCID = ffffffff
isNativeSnapshot = "no"
createType = "vmfs"

#Extent description
RW 44000000 VMFS "VM_name-flat.vmdk"

# The Disk Data Base
#DDB

ddb.adapterType = "lsilogic"
ddb.geometry.cylinders = "17884"
ddb.geometry.heads = "255"
ddb.geometry.sectors = "63"
ddb.longContentID = "c78bccbfd4724f0ee20a1ef2829544ab"
ddb.thinProvisioned = "0"
ddb.uuid = "60 00 C2 98 20 ac 05 4a-e5 39 e4 40 e8 a2 d8 d0"
ddb.virtualHWVersion = "8"

14) The size of the vmdk disk is specified in the #Extent description section (after the RW symbols). Change this value, for example, to set the disk size to 20GB, set 41943040 (20 GB * 1024 * 1024 * 1024/512)
 vi VM_name.vmdk 

I put the value a little larger than the size calculated by the method above (41943040 (20 GB * 1024 * 1024 * 1024/512)), just in case.
15) It remains to clone or emigrate the virtual machine, after which the new disk size will be displayed.

Physical thin disk reduction without changing provisioning
It is also possible to only reduce the size of the virtual machine file, by removing zero blocks.
For this:
1) It is necessary to fill all unused space, inside the virtual machine, with zeros. Create a file the size of the unused disk area, and then delete it.
 cat /dev/zero > /file cd / rm file 

2) Connect via SSH to the ESXi console as described above and go to the directory with the virtual machine.
3) In the console, the hypervisor has a special utility that allows you to wipe the zero blocks, thereby reducing the physical size of the thin disk.
This is done using the -K key (you can also use the --punchzero key) in the console of the ESXi server
 vmkfstools -K VM_name.vmdk 

It should be noted that the vmkfstools utility, launched with the -K key, can also convert a regular disk (zeroedthick or eagerzeroedthick) into a thin disk with cleaning out zero blocks and, accordingly, reducing the size of vmdk.

PS I hope this article will be useful to someone. Anyway, I spent a lot of time before I managed to correctly reduce the size of the thin disk.

Source: https://habr.com/ru/post/276377/


All Articles