Sunday, July 7, 2013

Linux LVM extend

HOW TO EXTEND A LINUX LVM VOLUME

This example is a Debian-based virtual machine managed by ESXi 4.1.
Let’s see what the disk space looks like before we make any changes:
$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vmorfels01-root
                       19G  1.5G   17G   9% /
none                  495M  172K  495M   1% /dev
none                  500M     0  500M   0% /dev/shm
none                  500M   40K  500M   1% /var/run
none                  500M     0  500M   0% /var/lock
none                  500M     0  500M   0% /lib/init/rw
none                   19G  1.5G   17G   9% /var/lib/ureadahead/debugfs
/dev/sda1             228M   33M  184M  15% /boot
This is a typical LVM config where the total disk size is about 20GB.
Give it more disk
Firstly I will change the provisioned size of the disk. In my example I am using vSphere client to change the provisioned size of the virtual server’s disk from 20GB to 30GB to be able to have some more space on the server.
Add a partition to the disk
Power up the machine and run fdisk to configure the disk.
orfels@vmorfels01:~$ sudo /sbin/fdisk /dev/sda
Print the partition table
Command (m for help): p
 
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x0009b807
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          32      248832   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              32        2611    20719617    5  Extended
/dev/sda5              32        2611    20719616   8e  Linux LVM
 
Command (m for help):
Add a primary partition
fdisk commands used:
  • n – New partition
  • p – Print partition table
  • t – Change system type
  • w – Write changes to partition table
Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (2611-3916, default 2611):
Using default value 2611
Last cylinder, +cylinders or +size{K,M,G} (2611-3916, default 3916):
Using default value 3916
 
Command (m for help): p
 
Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 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: 0x0009b807
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          32      248832   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              32        2611    20719617    5  Extended
/dev/sda3            2611        3916    10484774   83  Linux
/dev/sda5              32        2611    20719616   8e  Linux LVM
Change the system type of partition to 8e, which is type LVM:
Command (m for help): t
Partition number (1-5): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)
 
Command (m for help): p
 
Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 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: 0x0009b807
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          32      248832   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              32        2611    20719617    5  Extended
/dev/sda3            2611        3916    10484774   8e  Linux LVM
/dev/sda5              32        2611    20719616   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 or after you run partprobe(8) or kpartx(8)
Syncing disks.
Now it is time to reboot the machine.
$ sudo shutdown -r now
Set up a new physical volume
Once the machine has been restarted, let’s use pvdisplay to see what the physical volume looks like now.
orfels@vmorfels01:~$ sudo /sbin/pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               vmorfels01
  PV Size               19.76 GiB / not usable 2.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              5058
  Free PE               4
  Allocated PE          5054
  PV UUID               pt2ToJ-Bb55-Q82r-j4Tm-UZA9-KfJ4-H33YEA
Now run pvcreate to create a new physical volume.
orfels@vmorfels01:~$ sudo /sbin/pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created
Consume the new physical volume
Extend the volume group to consume the new physical volume, allowing us to use some or all of the new physical volume.
orfels@vmorfels01:~$ sudo /sbin/vgextend vmorfels01 /dev/sda3
  Volume group "vmorfels01" successfully extended
Considering that for this example I have added 10GB to the virtual machine’s disk, let’s check how much free there is:
orfels@vmorfels01:~$ sudo /sbin/vgdisplay | grep Free
  Free  PE / Size       2563 / 10.01 GiB
Grow the logical volume across the new partition, extending the logical volume with all the amount of free space found.
orfels@vmorfels01:~$ sudo /sbin/lvextend -L+10GB /dev/vmorfels01/root
  Extending logical volume root to 28.88 GiB
  Logical volume root successfully resized
Expand/resize the file system to use the space. The following command will grow to the maximum available size.
orfels@vmorfels01:~$ sudo resize2fs /dev/vmorfels01/root
resize2fs 1.41.11 (14-Mar-2010)
Filesystem at /dev/vmorfels01/root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 2
Performing an on-line resize of /dev/vmorfels01/root to 7570432 (4k) blocks.
The filesystem on /dev/vmorfels01/root is now 7570432 blocks long.
We have more space!
Now check the space to see the change:
orfels@vmorfels01:~$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vmorfels01-root
                       29G  846M   27G   4% /
none                  496M  176K  495M   1% /dev
none                  500M     0  500M   0% /dev/shm
none                  500M   36K  500M   1% /var/run
none                  500M     0  500M   0% /var/lock
none                  500M     0  500M   0% /lib/init/rw
/dev/sda1             228M   17M  199M   8% /boot

No comments:

Post a Comment