LVM to extend a partition

LVM to extend a partition From FreekiWiki Jump to: navigation, search Contents [hide]

[edit] How to Extend a Partition (step by step)

SUMMARY To grow a filesystem, you must install a new hard disk (unless you have free space available), format it as a Physical Volume (PV), add that PV to your Volume Group (VG), then add the space to your Logical Volume (LV), and finally use the filesystem tools to grow it.

You will need to be on a wired connection for this to work All these commands will need to be run as root (via sudo). Run the following commands in a terminal window.

1. Format Physical Volumes (PVs)

Initialize a disk or disk partition as a physical volume by running the "pvcreate" command on the whole disk. For example:

sudo pvcreate /dev/sdb

This creates a volume group descriptor at the start of the second IDE disk. You can initialize several disks and/or partitions at once. Just list all the disks and partitions on the command line you wish to format as PVs.

2. Create Volume Groups (VGs)

Use the "vgcreate" program to group selected PVs into VGs, and to optionally set the extent size (the default is 4MB). The following command creates a volume group named "VG1" from two disk partitions from different disks:

vgcreate VG1 /dev/hda1 /dev/hdb1

3. Create Logical Volumes (LVs)

Creating a logical volume in some VG is the most complex part of LVM setup, due to the many options available. The basic command syntax is:

lvcreate options size VG_name

Where size is either "-l num_extents" or "-L num_bytes", where num_bytes is a number followed by one of k, m, g, or t. If this second form is used you may not get an LV of that exact size, as LVs are always a whole number of extents. You can also use "--extents" for "-l" or "--size" for "-L".

One of the most common options is "-n name" (you can use "--name" for "-n") to specify a name for the logical volume. If you don't use this option than the LVs are named automatically "lvol1", "lvol2", "lvol3", etc.

If you want to create an LV that uses the entire VG, use the "vgdisplay" command to find the "Total PE" size, then use that when running lvcreate.

Once the LVs have been created you can format them with filesystems (or as swap space) using standard tools such as "mkfs". If the new filesystem can be successfully mounted, a final step is to edit the /etc/fstab file and possibly the rc.sysinit file, so that the volumes are mounted automatically at boot time. It may also be necessary to setup an initial ramdisk for booting (if the "root" filesystem is built on a logical volume).

4. Grow VGs, LVs, and Filesystems

To grow a filesystem, you must install a new hard disk (unless you have free space available), format it as a PV, add that PV to your VG, then add the space to your LV, and finally use the filesystem tools to grow it. (Not all filesystem allow or come with tools to grow and shrink them!)

VGs are resizable (spelled in Linux as "resizeable") by adding or removing PVs from them. However by default they are created as fixed in size. To mark a VG as resizable use the command:

root# vgchange -x y #or --resizeable y

Once this is done add a PV (say "hdb2") to some VG (say "VG1") with the command:

root# vgextend VG1 /dev/hdb2

Next, extend an LV with the "lvextend" command. This command works almost the same as the "lvcreate" command, but with a few different options. When specifying how much to increase the size of the LV, you can either specify how much to grow the LV with "+size" or you can specify the new (absolute) size (by omitting the plus sign). So to extend the LV "LV1" on VG "VG1" by 2GB, use:

root# lvextend -L +2G /dev/VG1/LV1

You could also use:

root# lvresize -L +2G /dev/VG1/LV1

It would be a good idea to use the same mapping as the original LV, or you will have strange performance issues! Also note this command can be used to extend a snapshot volume if necessary.

After you have extended the logical volume the last step is to increase the file system size. How you do this depends on the file system you are using. Most filesystem types come with their own utilities to grow/shrink filesystems, if they allow that. These utilities usually grow to fill the entire partition or LV, so there is no need to specify the filesystem size.

Some common filesystem utilities are (assume we are expanding the /home filesystem in LV1 on VG1):

* EXT2 and EXT3: EXT2/3 filesystems must be unmounted before they can be resized. The commands to use are:

root# umount /home # /home is the mount point for /dev/VG1/LV1 root# fsck -f /home # required! root# resize2fs /dev/VG1/LV1 # grow FS to fill LV1. root# mount /home

Examining LVM Information

To see information about some VG use:

vgdisplay some_volume_group vgs some_volume_group

To see information about some PV use the command:

pvdisplay some_disk_or_partition # e.g., /dev/hda1 pvs some_disk_or_partition

To see information about some LV use:

lvdisplay some-logical-volume lvs some-logical-volume

The man pages for these commands provides further details.

How to resize LVM logical volumes with ext4 as filesystem

Ever been in the situation where you needed to save some important files to a server and your greeted with “Not enough space left on device”-kind messages? No? Well, as that happened too often too me for my liking, I decided to do it right this time when I set up my home server and use logical volume manager (LVM) straight from the start. So basically, all I had to do was to shrink a filesystem that had free space in it and its partition (logical volume (LV) to be precisely) afterwards and then to resize the logical volume/filesystem where I needed the space.

As all the necessary tools are available normally on a system with LVM support, I could dive right in: view source

3. Create Logical Volumes (LVs)

Creating a logical volume in some VG is the most complex part of LVM setup, due to the many options available. The basic command syntax is:

lvcreate options size VG_name

Where size is either "-l num_extents" or "-L num_bytes", where num_bytes is a number followed by one of k, m, g, or t. If this second form is used you may not get an LV of that exact size, as LVs are always a whole number of extents. You can also use "--extents" for "-l" or "--size" for "-L".

One of the most common options is "-n name" (you can use "--name" for "-n") to specify a name for the logical volume. If you don't use this option than the LVs are named automatically "lvol1", "lvol2", "lvol3", etc.

If you want to create an LV that uses the entire VG, use the "vgdisplay" command to find the "Total PE" size, then use that when running lvcreate.

Once the LVs have been created you can format them with filesystems (or as swap space) using standard tools such as "mkfs". If the new filesystem can be successfully mounted, a final step is to edit the /etc/fstab file and possibly the rc.sysinit file, so that the volumes are mounted automatically at boot time. It may also be necessary to setup an initial ramdisk for booting (if the "root" filesystem is built on a logical volume).

4. Grow VGs, LVs, and Filesystems

To grow a filesystem, you must install a new hard disk (unless you have free space available), format it as a PV,add that PV to your VG, then add the space to your LV, and finally use the filesystem tools to grow it. (Not all filesystem allow or come with tools to grow and shrink them!)

VGs are resizable (spelled in Linux as "resizeable") by adding or removing PVs from them. However by default they are created as fixed in size. To mark a VG as resizable use the command:

root# vgchange -x y #or --resizeable y

Once this is done add a PV (say "hdb2") to some VG (say "VG1") with the command:

root# vgextend VG1 /dev/hdb2

Next, extend an LV with the "lvextend" command. This command works almost the same as the "lvcreate" command, but with a few different options. When specifying how much to increase the size of the LV, you can either specify how much to grow the LV with "+size" or you can specify the new (absolute) size (by omitting the plus sign). So to extend the LV "LV1" on VG "VG1" by 2GB, use:

root# lvextend -L +2G /dev/VG1/LV1

You could also use:

root# lvresize -L +2G /dev/VG1/LV1

It would be a good idea to use the same mapping as the original LV, or you will have strange performance issues! Also note this command can be used to extend a snapshot volume if necessary.

After you have extended the logical volume the last step is to increase the file system size. How you do this depends on the file system you are using. Most filesystem types come with their own utilities to grow/shrink filesystems, if they allow that. These utilities usually grow to fill the entire partition or LV, so there is no need to specify the filesystem size.

Some common filesystem utilities are (assume we are expanding the /home filesystem in LV1 on VG1):

* EXT2 and EXT3: EXT2/3 filesystems must be unmounted before they can be resized. The commands to use are:

root# umount /home # /home is the mount point for /dev/VG1/LV1 root# fsck -f /home # required! root# resize2fs /dev/VG1/LV1 # grow FS to fill LV1. root# mount /home

Examining LVM Information

To see information about some VG use:

vgdisplay some_volume_group vgs some_volume_group

To see information about some PV use the command:

pvdisplay some_disk_or_partition # e.g., /dev/hda1 pvs some_disk_or_partition

To see information about some LV use:

lvdisplay some-logical-volume lvs some-logical-volume

The man pages for these commands provides further details.

How to resize LVM logical volumes with ext4 as filesystem

Ever been in the situation where you needed to save some important files to a server and your greeted with “Not enough space left on device”-kind messages? No? Well, as that happened too often too me for my liking, I decided to do it right this time when I set up my home server and use logical volume manager (LVM) straight from the start. So basically, all I had to do was to shrink a filesystem that had free space in it and its partition (logical volume (LV) to be precisely) afterwards and then to resize the logical volume/filesystem where I needed the space.

As all the necessary tools are available normally on a system with LVM support, I could dive right in: view source print? 01	# Unmount the filesystem and check its' LV 02	umount /mnt/foo 03	e2fsck -f /dev/mapper/vg0-foo 04	 05	# Shrink ext4 and then the LV to the desired size 06	resize2fs -p /dev/mapper/vg0-foo 40G 07	lvreduce -L 40G /dev/mapper/vg0-foo 08	 09	# Before continuing, run e2fsck. If it bails because the partition 10	# is too small, don't panic! The LV can still be extended with 11	# lvextend until e2fsck succeeds, e.g.: 12	# lvextend -L +1G /dev/mapper/vg0-foo 13	e2fsck -f /dev/mapper/vg0-foo 14	 15	# Resize the filesystem to match the LVs size, check and mount it 16	resize2fs -p /dev/mapper/vg0-foo 17	e2fsck -f /dev/mapper/vg0-foo 18	mount /mnt/foo

That was the tricky part. The rest is pretty straight forward:

* unmount the filesystem, * extend the logical volume and * expand the filesystem afterwards.

view source print? 01	umount /mnt/bar 02	 03	# Extend the LV to use all free space 04	lvextend -l +100%FREE /dev/mapper/vg0-bar 05	e2fsck -f /dev/mapper/vg0-bar 06	 07	# Resize the partition to fill the LV 08	resize2fs -p /dev/mapper/vg0-bar 09	e2fsck -f /dev/mapper/vg0-bar 10	mount /mnt/bar print? 01	# Unmount the filesystem and check its' LV 02	umount /mnt/foo 03	e2fsck -f /dev/mapper/vg0-foo 04	 05	# Shrink ext4 and then the LV to the desired size 06	resize2fs -p /dev/mapper/vg0-foo 40G 07	lvreduce -L 40G /dev/mapper/vg0-foo 08	 09	# Before continuing, run e2fsck. If it bails because the partition 10	# is too small, don't panic! The LV can still be extended with 11	# lvextend until e2fsck succeeds, e.g.: 12	# lvextend -L +1G /dev/mapper/vg0-foo 13	e2fsck -f /dev/mapper/vg0-foo 14	 15	# Resize the filesystem to match the LVs size, check and mount it 16	resize2fs -p /dev/mapper/vg0-foo 17	e2fsck -f /dev/mapper/vg0-foo 18	mount /mnt/foo

That was the tricky part. The rest is pretty straight forward:

* unmount the filesystem, * extend the logical volume and * expand the filesystem afterwards.

view source print? 01	umount /mnt/bar 02	 03	# Extend the LV to use all free space 04	lvextend -l +100%FREE /dev/mapper/vg0-bar 05	e2fsck -f /dev/mapper/vg0-bar 06	 07	# Resize the partition to fill the LV 08	resize2fs -p /dev/mapper/vg0-bar 09	e2fsck -f /dev/mapper/vg0-bar 10	mount /mnt/bar

Retrieved from "http://wiki.freegeek.org/index.php/Network_backup"

Category: Tech support