Wednesday, July 10, 2013

Resizing the disk space on Linux VMs running on VMware ESXi 5

Step1:

Connect to your ESXi Server and switch off the machine running on ESXiEdit the disk and increased the desired size and reboot the machine.




Step 2:


Now this machine is up and running and having the unallocated disk space, we need to use this

Login to machine via ssh


List the disk partition


# fdisk -l /dev/sda


Disk /dev/sda: 171.8 GB, 171798691840 bytes

255 heads, 63 sectors/track, 20886 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b5bc9

   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.

/dev/sda2              64       13055   104344576   8e  Linux LVM

so the disk have two partition /dev/sda1 and /dev/sda2

Create new partition

#fdisk /dev/sda

type n - to create a new partition
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
type p - for primary partition
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
p
We already have /dev/sda1 and /dev/sda2 Press ’3′ for this new partition which will be created as /dev/sda3
Partition number (1-4): 3
Just press enter twice above as by default the first and last cylinders of the unallocated space should be correct. After this the partition is then ready.
First cylinder (3211-4456, default 2611): "enter"
Using default value 4456
Last cylinder, +cylinders or +size{K,M,G} (3211-4456, default 4456): "enter"
Using default value 4456
Command (m for help): t
Partition number (1-5): 3
type 8e - for the "Linux LVM" type
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)
type w - to write changes and exit
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 or after you run partprobe(8) or kpartx(8)
Syncing disks.

Now Reboot the machine
Increasing the logical volume

We use the pvcreate command which creates a physical volume for later use by the logical volume manager (LVM). In this case the physical volume will be our new /dev/sda3 partition.
# pvcreate /dev/sda3
let's now create the physical volume in that partition:

# pvcreate /dev/sda3
Now let's extend the server's Volume Group to that physical volume.

# vgdisplay

Note down the entry next to "VG Name". That's your Volume Group name.


# vgextend EnterVolumeGroupNameHere /dev/sda3


#####

If you get a message saying /dev/sda3 could not be added to your Volume Group, you need to remove the physical volume and recreate it. Metadata might have gotten corrupt and thus the volume cannot be added to your Volume Group. So just do:

# pvremove /dev/sda3

And then again:

# pvcreate /dev/sda3

#####


Since we're (essentially) extending the main logical volume, let's get the name of that:

# lvdisplay

and note down the entry next to "LV Name". This is your logical volume's name (e.g. /dev/srv/root), which you'll now extend to the newly added partition/physical volume.


- Extend the logical volume by X GBs:

# lvextend -L +XG yourLogicalVolumeName
Make sure you replace X above with the actual number of GBs you've added in your VM's settings. So if you increased your VM by 20GBs, the command becomes:

# lvextend -L +20G yourLogicalVolumeName


Finally, let's resize the file system to the new allocated space:

$ resize2fs yourLogicalVolumeName

(this may take some time depending on number of GBs added to the file system.


- Check the new file system sizes:

# df -hT


This is all done.

No comments:

Post a Comment