Thursday, May 8, 2014

Resizing a disk on a Linux VM (LVM Based)

We needed to resize a VM that had initially a 25Gb Virtual Hard Drive, to a 40Gb HD.  These are the steps below of how to do it, and what it looks like.

Step 1: Resize the disk in VMware, either through vSphere, or PowerCLI if you'd like.  I happened to have vSphere open so I did it that way:

(note, you will have to turn off the VM in order to do this)

After the VM has come up, you now do the below, here is a shortened version of things, and below it the actual output you get:

echo 1 > /sys/class/scsi_device/1\:0\:0\:0/device/rescan
echo 1 > /sys/class/scsi_device/2\:0\:0\:0/device/rescan

fdisk /dev/sda


2.       After the server reboots, run these three command:

partx -a /dev/sda ;
pvresize /dev/sda2 ;
pvcreate /dev/sda3 ;

vgextend vg_root /dev/sda3 ;
lvextend -l +100%FREE /dev/mapper/vg_root-lv_root
resize2fs /dev/mapper/vg_root-lv_root

(to see what your VG is called run  #vgdisplay  | grep "VG Name" )


Now here it is in detail:

[root@node01 ~]# echo 1 > /sys/class/scsi_device/2\:0\:0\:0/device/rescan
[root@node01 ~]# echo 1 > /sys/class/scsi_device/1\:0\:0\:0/device/rescan

[root@node01 ~]# fdisk /dev/sda

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): p

Disk /dev/sda: 26.8 GB, 26843545600 bytes
64 heads, 32 sectors/track, 25600 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000a646c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           2         501      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2             502       16384    16264192   8e  Linux LVM
Partition 2 does not end on cylinder boundary.

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

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
Partition number (1-4): 2
First cylinder (1-25600, default 1): 502
Last cylinder, +cylinders or +size{K,M,G} (502-25600, default 25600):
Using default value 25600

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.
[root@node01 ~]#
[root@node01 ~]# reboot

Broadcast message from root@node01
        (/dev/pts/1) at 21:12 ...

The system is going down for reboot NOW!
[root@node01 ~]# Red Hat Enterprise Linux Server release 6.2 (Santiago)
Kernel 2.6.32-220.el6.x86_64 on an x86_64

[root@node01 ~]# pvresize /dev/sda2
  Physical volume "/dev/sda2" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized
[root@node01 ~]# lvextend -l +100%FREE /dev/mapper/vg_root-lv_root
  Extending logical volume lv_root to 20.50 GiB
  Logical volume lv_root successfully resized
[root@node01 ~]# resize2fs /dev/mapper/vg_root-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_root-lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/mapper/vg_root-lv_root to 5373952 (4k) blocks.
The filesystem on /dev/mapper/vg_root-lv_root is now 5373952 blocks long.

[root@node01 ~]#
[root@node01 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
                       21G  5.0G   15G  27% /
tmpfs                  20G     0   20G   0% /dev/shm
/dev/sda1             485M   36M  424M   8% /boot

And that's all there is to it.  you now have a 40Gb drive instead of a 25Gb one.