Friday, May 1, 2015

VMware ESXI 5.x Shrink LVM without dataloss

In Vmware ESXI host

 # ls -ltr
total 91829760
-rw-r--r--    1 root     root           265 Apr  4 13:04 copy73 (2).vmxf
-rw-------    1 root     root           516 Apr 22 09:07 copy73.vmdk
-rw-------    1 root     root     25769803776 Apr 22 11:37 copy73-flat.vmdk
-rw-r--r--    1 root     root            43 Apr 23 17:42 copy73 (2).vmsd
-rw-------    1 root     root     25769803776 Apr 23 18:15 copy73_2-flat.vmdk
-rw-r--r--    1 root     root        167490 Apr 23 18:25 vmware-1.log
-rw-------    1 root     root           492 Apr 23 18:28 copy73_2.vmdk
-rw-------    1 root     root           496 Apr 23 18:28 copy73 (2)_2.vmdk
-rw-------    1 root     root           496 Apr 23 18:28 copy73 (2)_1.vmdk
-rw-------    1 root     root     21474836480 Apr 23 18:28 copy73 (2)_2-flat.vmdk
-rw-------    1 root     root     21474836480 Apr 23 18:28 copy73 (2)_1-flat.vmdk
-rw-------    1 root     root          8684 Apr 23 18:36 copy73 (2).nvram
-rw-r--r--    1 root     root        134657 Apr 23 18:36 vmware.log
-rwxr-xr-x    1 root     root          3142 May  1 10:27 copy73 (2).vmx

1. Clone copy disk to backup, before convert
/vmfs/volumes/4a2ea43a-dcf1b3df-3141-00215e74a364/copy73 (2) # vmkfstools -i copy73_2.vmdk copy73_3.vmdk
Destination disk format: VMFS zeroedthick
Cloning disk 'copy73_2.vmdk'...
Clone: 10% done.

2. Edit vitual machine add disk from clone step 1


3. Reboot and check disk
[root@TT4-Web00-S ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/VolGroup00/LogVol00
                       14G  6.5G  6.5G  50% /
/dev/sda1              99M   13M   82M  14% /boot
tmpfs                 3.0G     0  3.0G   0% /dev/shm
/dev/sdb1              11G  9.0G  593M  94% /u02
/dev/sdc1              20G  8.0G   11G  43% /u03
[root@TT4-Web00-S ~]#

4. Boot Troubleshooting => Rescue a CentOS system
Use: CentOS-7-x86_64-DVD-1503-01.iso

5. Check volume group name
bash-3.2# lvm vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "VolGroup00" using metadata type lvm2
 
bash-3.2# lvm vgchange -a y 
  2 logical volume(s) in volume group "VolGroup00" now active
 
bash-3.2# ls /dev/VolGroup00/
LogVol00  LogVol01

6. You can display information about the logical volumes using lvm lvs
bash-3.2# lvm lvs
  LV       VG         Attr   LSize  Origin Snap%  Move Log Copy%  Convert
  LogVol00 VolGroup00 -wi-ao 14.12G                                     
  LogVol01 VolGroup00 -wi-ao  9.75G                    

 7. Display Logical Volumes

bash-3.2# lvdisplay
  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                zG2Jef-Jn3f-gqgj-VsCX-OCBd-jcpM-n0spR8
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                14.12 GB
  Current LE             452
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
  
  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol01
  VG Name                VolGroup00
  LV UUID                ceUxVm-GypP-PNRR-WPFP-sUqv-MhPB-CWRgeu
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                9.75 GB
  Current LE             312
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
 

bash-3.2# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "VolGroup00" using metadata type lvm2
bash-3.2# lvscan
  ACTIVE            '/dev/VolGroup00/LogVol00' [14.12 GB] inherit
  ACTIVE            '/dev/VolGroup00/LogVol01' [9.75 GB] inherit

bash-3.2# vgdisplay
  --- Volume group ---
  VG Name               VolGroup00
  System ID            
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               23.88 GB
  PE Size               32.00 MB
  Total PE              764
  Alloc PE / Size       764 / 23.88 GB
  Free  PE / Size       0 / 0  
  VG UUID               zeIZpI-rZkO-MKFA-NgAC-OuU5-rt7f-jwRHQB

 
7. Resize to from 14G to 10G data
[root@livecd ~]# e2fsck -f /dev/VolGroup00/LogVol00
e2fsck 2.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/VolGroup00/LogVol00: 92069/4704768 files (1.3% non-contiguous), 1285660/4702208 blocks

[root@livecd ~]# resize2fs /dev/VolGroup00/LogVol00 10G
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/VolGroup00/LogVol00 to 2097152 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 2097152 blocks long.


[root@livecd ~]# lvreduce -L 10G /dev/VolGroup00/LogVol00
  WARNING: Reducing active logical volume to 10.00 GB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce LogVol00? [y/n]: y
  Reducing logocal volume LogVol00 to 10.00 GB
  Logical volume LogVol00 successfully resized

bash-3.2# lvdisplay /dev/VolGroup00/LogVol00
  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                zG2Jef-Jn3f-gqgj-VsCX-OCBd-jcpM-n0spR8
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                10 GB
  Current LE             320
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
 
[root@livecd ~]# pvscan
    PV /dev/sda22   VG VolGroup00   lvm2 [23.88 GB / 4.12 GB free]
  Total: 1 [23.88 GB] / in use: 1 [23.88 GB] / in no VG: 0 [0   ]
 
[root@livecd ~]# lvscan
  ACTIVE    '/dev/VolGroup00/LogVol00' [10.00 GB] inherit
  ACTIVE    '/dev/VolGroup00/LogVol01' [9.75 GB] inherit
 






8. Remove Volume Swap
[root@livecd ~]# lvremove /dev/VolGroup00/LogVol01
  Can't remove open logical volume "LogVol01"

[root@livecd ~]# swapoff /dev/VolGroup00/LogVol01

[root@livecd ~]# lvremove /dev/VolGroup00/LogVol01
Do you really want to remove active logical volume LogVol01? [y/n]:y
  Logical volume "LogVol01" successfully removed

 
[root@livecd ~]# lvcreate -L 2G -n LogVol01 VolGroup00 
  Logical volume "LogVol01" created

[root@livecd ~]# mkswap /dev/VolGroup00/LogVol01
Setting up swapspace vrsion 1, size = 2147479 kB

[root@livecd ~]# swapon /dev/VolGroup00/LogVol01

[root@livecd ~]# pvscan
  PV /dev/sda2   VG VolGroup00   lvm2 [23.88GB / 11.88 GB free]
  Total: 1 [23.88 GB] / in use: 1 [23.88 GB] / in on FV: 0 [0   ]

 [root@livecd ~]# pvresize --setphysicalvolumesize 12G /dev/sda2
  /dev/sda2: cannot resize to 383 extents as 384 are allocated.
  0 physical volume(s) resized / 1 physical volumes(s) not resized
 
 [root@livecd ~]# pvresize --setphysicalvolumesize 12500M /dev/sda2
  Physical volume "/dev/sda2" changed
  1 physical volume(s) resized / 0 physical volumes(s) not resized
 
  [root@livecd ~]# pvscan
  PV /dev/sda2   VG VolGroup00   lvm2 [12.19 GB / 192.00 MB free]
  Total: 1 [12.19 GB] / in use: 1 [12.19 GB] / in on FV: 0 [0   ]
 
  12500M-192M = 12308M
 
  [root@livecd ~]# pvresize --setphysicalvolumesize 12308M /dev/sda2
  Physical volume "/dev/sda2" changed
  1 physical volume(s) resized / 0 physical volumes(s) not resized

 [root@chuo221 ~]# pvscan
  PV /dev/sda2   VG VolGroup00   lvm2 [12.00 GB / 0    free]
  Total: 1 [12.00 GB] / in use: 1 [12.00 GB] / in no VG: 0 [0   ]
 
 
 
 

9. Shrinking VMWare disk

Calculate the desired size
vmdk_shrunken = [x * (1024*1024*1024)] / 512
where x is the size in Gigabytes

=> [12 * (1024*1024*1024)] / 512 = 25165824

Change to the desired size

Locate the lines that look like:
# Extent description
RW 25165824 VMFS “foo-flat.vmdk”


/vmfs/volumes/4a2ea43a-dcf1b3df-3141-00215e74a364/copy73 (2) # vi copy73_3.vmdk

# Disk DescriptorFile
version=1
encoding="UTF-8"
CID=c0e13e2a
parentCID=ffffffff
isNativeSnapshot="no"
createType="vmfs"

# Extent description
RW 25165824 VMFS "copy73_3-flat.vmdk"

# The Disk Data Base
#DDB

ddb.adapterType = "lsilogic"
ddb.deletable = "true"
ddb.geometry.cylinders = "3133"
ddb.geometry.heads = "255"
ddb.geometry.sectors = "63"
ddb.longContentID = "67a55ef19ceef3110ed453dec0e13e2a"
ddb.uuid = "60 00 C2 9c a1 49 6f 68-bb 51 38 e1 15 63 5c 5d"
ddb.virtualHWVersion = "7"



10 Export to a new VMDK

/vmfs/volumes/4a2ea43a-dcf1b3df-3141-00215e74a364/copy73 (2) # vmkfstools -i copy73_3.vmdk copy73_4.vmdk
Destination disk format: VMFS zeroedthick
Cloning disk 'copy73_3.vmdk'...
Clone: 49% done.

Remove old disk from VM, add new disk to VM
Remove VM from inventory, Re-add VM to inventory


  http://technogrip.blogspot.com/2012/04/shrinking-linux-centos-lvm-disk-on.html
  http://www.vmwarearena.com/2013/03/shrink-virtual-machine-vmdk.html
 


 11. Fixing LVM I/O Errors

[root@TT4-Web00-S ~]# pvscan
  /dev/sda2: read failed after 0 of 4096 at 25662783488: Input/output error
  /dev/sda2: read failed after 0 of 4096 at 25662865408: Input/output error

  PV /dev/sda2   VG VolGroup00   lvm2 [12.97 GB / 992.00 MB free]
  Total: 1 [12.97 GB] / in use: 1 [12.97 GB] / in no VG: 0 [0   ]
 
 
 
  [root@TT4-Web00-S ~]# fdisk /dev/sda

The number of cylinders for this disk is set to 1958.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/sda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        3133    25061400   83  Linux

Command (m for help): d
Partition number (1-4): 2

Command (m for help): p

Disk /dev/sda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (14-1958, default 14): 14
Last cylinder or +size or +sizeM or +sizeK (14-1958, default 1958): 1958

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        1958    15623212+  8e  Linux LVM


Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
reboot ...done

No comments: