Wednesday, July 18, 2012

Increase A VMware Disk Size (VMDK) Formatted As Linux LVM

Copy from : http://mattiasgeniar.be/2010/08/27/increase-a-vmware-disk-size-vmdk-formatted-as-linux-lvm/

 To increase the size of your VMware Virtual Machine, you need to do 2 major steps. First, you need toincrease the disk's size in your vSphere Client or through the CLI. This will increase the "hardware" disk that your Virtual Machine can see. Then, you need to utilize that extra space by partitioning it. If you're interested in just resizing your Linux LVM, please proceed to step 2.

In this example, I'm increasing a 3GB disk to a 10GB disk (so you can follow using the examples).
I would advise you to read the excellent documention on Logical Volume Management on tldp.org.
Just a small note beforehand; if your server supports hot adding new disks, you can just as easily add a new Hard Disk to your Virtual Machine. Doing so, would mean you can increase your LVM's size without having to reboot. If you increase the size of your currently attached disk (like the example below), you'll need to reboot your server at least once to re-read your partition table.
In Linux, you can re-scan the hard disks without rebooting the guest Virtual Machine. Check the following blogpost if you do not want to reboot your VM after adding a new hard disk or increasing the current hard disk size: Rescan disk devices on a Linux VMware virtual machine without a reboot

1) Checking if you can extend the current disk or need to add a new one

This is rather important step, because a disk that has been partitioned in 4 primary partitions already can not be extended any more. To check this, log into your server and run fdisk -l at the command line.
# fdisk -l

Disk /dev/sda: 187.9 GB, 187904819200 bytes
255 heads, 63 sectors/track, 22844 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          25      200781   83  Linux
/dev/sda2              26        2636    20972857+  8e  Linux LVM
If it looks like that, with only 2 partitions, you can safely extend the current hard disk in the Virtual Machine.
However, if it looks like this:
# fdisk -l

Disk /dev/sda: 187.9 GB, 187904819200 bytes
255 heads, 63 sectors/track, 22844 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          25      200781   83  Linux
/dev/sda2              26        2636    20972857+  8e  Linux LVM
/dev/sda3            2637       19581   136110712+  8e  Linux LVM
/dev/sda4           19582       22844    26210047+  8e  Linux LVM
It will show you that there are already 4 primary partitions on the system, and you need to add a new Virtual Disk to your Virtual Machine. You can still use that extra Virtual Disk to increase your LVM size, so don't worry.

2) The "hardware" part, "physically" adding diskspace to your VM

Increasing the disk size can be done via the vSphere Client, by editing the settings of the VM (right click > Settings).
Edit settings
Now, depending on the first step, if there aren't four primary partitions yet, you can increasing the privisioned disk space.
Increase disk size
If the "Provisioned Size" area (top right corner) is greyed out, consider turning off the VM first (if it does not allow "hot adding" of disks/sizes), and check if you have any snapshots made of that VM. You can not increase the disk size, as long as there are available snapshots.
Alternatively, if you already have 4 primary paritions, you can also choose "Add..." to add new Hardware "Virtual Disk" to your VM, with the desired extra space.

3) Partitioning the unalloced space

Once you've changed the disk's size, either boot up your VM again, or restart if it was still running. Linux needs to boot with the new disk, so it can see you've added (unallocated) disk space.
Once you've booted again, you can check if the extra space can be seen on the disk.
~$  fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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         391     3036285   8e  Linux LVM
So the server can now see the 10GB hard disk. Let's create a partition, by start fdisk for the /dev/sda device.
~$  fdisk /dev/sda

The number of cylinders for this disk is set to 1305.
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): n
Now enter 'n', to create a new partition.
Command action
e   extended
p   primary partition (1-4)
 p
Now choose "p" to create a new primary partition. Please note, your system can only have 4 primary partitions on this disk! If you've already reached this limit, create an extended partition.
Partition number (1-4): 3
Choose your partition number. Since I already had /dev/sda1 and /dev/sda2, the logical number would be3.
First cylinder (392-1305, default 392): 
Using default value 392
Last cylinder or +size or +sizeM or +sizeK (392-1305, default 1305): 
Using default value 1305
Note; the cylinder values will vary on your system. It should be safe to just hint enter, as fdisk will give you a default value for the first and last cylinder (and for this, it will use the newly added diskspace).
Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)
Now type t to change the partition type. When prompted, enter the number of the partition you've just created in the previous steps. When you're asked to enter the "Hex code", enter 8e, and confirm by hitting enter.
Command (m for help): w
Once you get back to the main command within fdisk, type w to write your partitions to the disk. You'll get a message about the kernel still using the old partition table, and to reboot to use the new table. Please obey kindly, and reboot the virtual machine.
After you've rebooted, you can see the newly created partition with fdisk.
~$  fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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         391     3036285   8e  Linux LVM
/dev/sda3             392        1305     7341705   8e  Linux LVM

3) Extend your Logical Volume with the new partition

Now, create the physical volume as a basis for your LVM. Please replace /dev/sda3 with the newly created partition.
~$  pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created
Now find out how your Volume Group is called.
~$  vgdisplay
--- Volume group ---
VG Name               VolGroup00
...
Let's extend that Volume Group by adding the newly created physical volume to it.
~$  vgextend VolGroup00 /dev/sda3
Volume group "VolGroup00" successfully extended
With pvscan, we can see our newly added physical volume, and the usable space (7GB in this case).
~$  pvscan
PV /dev/sda2   VG VolGroup00   lvm2 [2.88 GB / 0    free]
PV /dev/sda3   VG VolGroup00   lvm2 [7.00 GB / 7.00 GB free]
Total: 2 [9.88 GB] / in use: 2 [9.88 GB] / in no VG: 0 [0   ]
Now we can extend Logical Volume (as opposed to the Physical Volume we added to the group earlier). The command is "lvextend /dev/VolGroupxx /dev/sdXX".
~$  lvextend /dev/VolGroup00/LogVol00 /dev/sda3
Extending logical volume LogVol00 to 9.38 GB
Logical volume LogVol00 successfully resized
If you're running this on Ubuntu, use the following.
~$  lvextend /dev/mapper/vg-name /dev/sda3
All that remains now, it to resize the file system to the volume group, so we can use the space. Replace the path to the correct /dev device if you're on ubuntu/debian like systems.
~$  resize2fs /dev/VolGroup00/LogVol00
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 2457600 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 2457600 blocks long.
And we're good to go!
~$  df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00 9.1G 1.8G  6.9G  21% /
/dev/sda1              99M   18M   77M  19% /boot
tmpfs                 125M     0  125M   0% /dev/shm