Tuesday, March 9, 2010

Expanding Linux Partitions with LVM

What is LVM

The Logical Volume Manager (LVM) enables you to resize your partitions without having to modify the partition tables on your hard disk. LVM becomes useful when you are running out of disk space and instead of reinstalling your system on a larger driver, you can simpy expand your existing partiton scheme, without loosing any data on it.


Why this article

I'll try to show you, plain and simple, as less pain as possible howto setup and use LVM on your system. Since the official manuals and HOWTOs are *very* long, I'll make it quick and simple. Well, at leas I'll try to ;-)


LVM Terms

Once you understand the terms, setting up and managing LVM will be a "peace of cake" for you. So it's important for you that you know what this terms means. If you won't understand the terms, than you might brake your system badly. This is the first-hand info, It happend to me on the RHCE exam. I broke the system in a way that was completely unusefull :) Lucky for me this was the only thing that i didn't manage to work, so i passed anyways ;-)


  • physical volume: A physical volume (PV) is another name for a regular physical disk partition that is used or will be used by LVM.
  • logical volume: The equivalent of a disk partition in a non-LVM system. The LV is visible as a standard block device; as such the LV can contain a file system (eg. /home).
  • volume group: The Volume Group is the highest level abstraction used within the LVM. It gathers together a collection of Logical Volumes and Physical Volumes into one administrative unit.
  • physical extent: Each physical volume is divided chunks of data, known as physical extents, these extents have the same size as the logical extents for thevolume group.
  • logical extent: Each logical volume is split into chunks of data, known as logicalextents. The extent size is the same for all logical volumes in the volume group.


The Scenario

  • In the first scenario we will be creating LVM's on already installed system. We will expand our /home folder with brand new 4GB hard disk added.
  • in the second scenario will setup LVM partitions at install time and expand it later, with 4GB hard disk, that we will add.


The quick list

After understanding the LVM concept, keep this in mind and you'll be fine.


Adding a Logical Volume

  • backup
  • fdisk, and change the partition system id to 8e (Linux LVM)
  • pvcreate (Create Physical Volume)
  • vgcreate (Create Volume group)
  • lvcreate (to assign LV size)
  • mkfs -j (reformat the filesystem)
  • fstab (edit to suite your needs)
  • mount (remount the new volume)


Extending a Logical volume

  • backup
  • umount
  • fdisk, and change the partition system id to 8e (Linux LVM)
  • pvcreate (Create Physical Volume)
  • vgextend (Extend the Volume Group)
  • lvextend (Extend the LV)
  • mount
  • ext2online (Expand volume to the actual size)
  • umount
  • e2fsck -f (check the filesystem)
  • mount (remount the new volume)


Removing Logical Volumes

  • backup (do you need data on this volume?)
  • umount (umount volume)
  • lvremove (remove Logical Volume)


Creating and adding LVM

Let's get to work. First, we will setup LVM on already installed system, than we will setup LVM at install time and expend it later on.


On already installed and running system

In this scenario we have /home partition on a seperate partition and ext3 filesystem on it. We backup the partition, convert the parttion to LVM partition type and expand it with brand new 4GB hard disk added to our system.


First thing that you need to think of when expanding is backup of your partition/volumethat you will expand, because you will loose all your data on it when finished. We're going to backup our home folder with tar, since we want the permissions to be kept as they are. This is done with the following command

[root@localhost ~]# tar cvfz backup-home.tar.gz /home
tar: Removing leading `/' from member names
/home/
/home/lost+found/
/home/anze/
/home/anze/.bashrc
/home/anze/.bash_logout
/home/anze/.gtkrc
/home/anze/.bash_profile
[root@localhost ~]#

Ok, just to make you feel better, check the content of the tar file, just tu make sure...

[root@localhost ~]# tar -tvf backup-home.tar.gz
drwxr-xr-x root/root 0 2005-11-07 10:25:33 home/
drwx------ root/root 0 2005-11-07 10:43:49 home/lost+found/
drwxr-xr-x anze/anze 0 2005-11-07 10:25:33 home/anze/
-rw-r--r-- anze/anze 124 2005-11-07 10:25:33 home/anze/.bashrc
-rw-r--r-- anze/anze 24 2005-11-07 10:25:33 home/anze/.bash_logout
-rw-r--r-- anze/anze 120 2005-11-07 10:25:33 home/anze/.gtkrc
-rw-r--r-- anze/anze 191 2005-11-07 10:25:33 home/anze/.bash_profile
[root@localhost ~]#

Next, change the partition system type with fdisk. This is done as follows:

[root@localhost ~]# fdisk /dev/hda


Command (m for help): p

Disk /dev/hda: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 1 13 104391 83 Linux
/dev/hda2 14 268 2048287+ 83 Linux
/dev/hda3 269 333 522112+ 82 Linux swap / Solaris
/dev/hda4 334 522 1518142+ 5 Extended
/dev/hda5 334 522 1518111 83 Linux

Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 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.
Syncing disks.
[root@localhost ~]#


Now that we have everything set up for the merge, let's create the physical volume from home partition

[root@localhost ~]# pvcreate /dev/hda5
Physical volume "/dev/hda5" successfully created
[root@localhost ~]#

...and physical volume on the new hard disk:

[root@localhost ~]# pvcreate /dev/hdb
Physical volume "/dev/hdb" successfully created
[root@localhost ~]#

Now, create the Volume group named VolGroup00 and include the home partition and the new disk partition into this group:

[root@localhost ~]# vgcreate VolGroup00 /dev/hda5 /dev/hdb
Volume group "VolGroup00" successfully created

Now that this is done, let's check the volume groups:

[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 5.44 GB
PE Size 4.00 MB
Total PE 1393
Alloc PE / Size 0 / 0
Free PE / Size 1393 / 5.44 GB
VG UUID qPNDwe-Ma0K-PNJC-6zKS-gvCi-4H3P-1GYykh

So, everything is fine, as you can see we have 5.44GB (1393 PE's) free space to create a logical volume

[root@localhost ~]# lvcreate -l 1393 VolGroup00
Logical volume "lvol0" created
[root@localhost ~]#

And to see the results:

[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Name /dev/VolGroup00/lvol0
VG Name VolGroup00
LV UUID 0rGNBf-dcaW-bS1G-JEw8-QvlU-KIGT-AfusDb
LV Write Access read/write
LV Status available
# open 0
LV Size 5.44 GB
Current LE 1393
Segments 2
Allocation inherit
Read ahead sectors 0
Block device 253:0

[root@localhost ~]#

Done. Next, let's edit our /etc/fstab file so it will be able to mount /home from a newlogical volume:

This is my case:

[root@localhost ~]# cat /etc/fstab
# This file is edited by fstab-sync - see 'man fstab-sync' for details
LABEL=/ / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
/dev/devpts /dev/pts devpts gid=5,mode=620 0 0
/dev/shm /dev/shm tmpfs defaults 0 0
/dev/VolGroup00/lvol0 /home ext3 defaults 0 0
/dev/proc /proc proc defaults 0 0
/dev/sys /sys sysfs defaults 0 0
LABEL=SWAP-hda3 swap swap defaults 0 0
/dev/fd0 /media/floppy auto pamconsole,exec,noauto,managed 0 0
/dev/hdc /media/cdrom auto pamconsole,exec,noauto,managed 0 0
[root@localhost ~]#

Create the file system on the logical volume:

[root@localhost ~]# mkfs -j /dev/VolGroup00/lvol0
mke2fs 1.37 (21-Mar-2005)

Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
713856 inodes, 1426432 blocks
71321 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1463812096

44 block groups
32768 blocks per group, 32768 fragments per group
16224 inodes per group

Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736

Writing inode tables: done
Creating journal (8192 blocks): done

Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]#

And we are done. Mount it, just to make sure, you have the correct entries in the /etc/fstab file

[root@localhost ~]# mount /home

If everything configured correctly, the /home is mounted and you can check the capacity with:

[root@localhost ~]# df -h

Filesystem Size Used Avail Use% Mounted on
/dev/hda2 1.9G 603M 1.3G 33% /
/dev/hda1 99M 9.3M 85M 10% /boot
/dev/shm 163M 0 163M 0% /dev/shm
/dev/mapper/VolGroup00-lvol0
5.4G 43M 5.1G 1% /home
[root@localhost ~]#

At this point you are ready to restore your home folder files and you're all set.


At install time

First, let's create basic partitions. I have a 4Gb disk, that I will configure as follows:

  • 100Mb ext3 partition for /boot
  • 512Mb swap partition
  • 2GB ext3 partition for /
  • the rest of free space will be used for /home

Since I'm sure you already know how to do the "basic" part of partitioning I'll show you the LVM part, It's really easy. All you have to do is select "New" partition and under "File System Type" choose "physical volume (LVM). Make sure you select to fill the whole rest of free space for size.

The setup part looks like this:

Image:lvm1a.JPG


And your partition setup end up looking like this

Image:lvm2a.JPG

Now that you've define the physical volume it's time to define what will this partition hold. Select the "LVM" button. The new windows opens and you can choose from a various things.

  • Volume Group Name: We can leave this as is (VolGroup00). If you were up to define more logical volume names, you would use VolGroup01 for the second group, VolGroup02 for the third group etc...
  • Physical Extend: If you forgot what this is, scrool up. It's important that you know what you are doing.
  • Logical Volumes: here we will define a new logical volume for /homefolder/partition. Select "Add" button and configure it as shown below:


Image:lvm3a.JPG


As you can see the mount point is /home, file system type is ext3, Logical volume name is LogVol00, and the size of this volume is 1440Mb.

Your partitions will end up looking like this:

Image:lvm4a.JPG


This is the end of LVM setup part. Next, install the packages that you like and finish the installation. After the reboot, you can check your partitions to see that your /home is actually made of LVM.


Extending LVM partition

Fedora Core 4 comes with a LVM tool called system-config-lvm. It's a GUI for managing LVM's. You can start it up by either selecting it from the menu bar or from the console with the command

[root@localhost ~]# system-config-lvm

The GUI looks really nice and it provides us with all the information that you need. The only bad thing here is that the tool is not complete yet, so we won't be using it. Instead, we will be expanding our partitions in console.


Ok, first we need to unmount home folders, we can't be working while volume is mounted.

[root@localhost ~]# umount /home/

Now, let's check the existing physical volumes with the following command

[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/hda5
VG Name VolGroup00
PV Size 1.44 GB / not usable 0
Allocatable yes
PE Size (KByte) 32768
Total PE 46
Free PE 1
Allocated PE 45
PV UUID auQ2kD-GuHQ-6feE-nSr9-cA0A-dGbD-CrHZhg

[root@localhost ~]#

Ok, as you can see, we have one Physical volume that is made from the /dev/hda5 partition, and we have a Volume Group name for it, it's VolGroup00. You can also see the size of the physical volume etc...


Let's prepare our new disk to be able to join our existing VG. First step is to create one (or more if you like) partition(s) on the new disk and we have to make sure it's labeled correct. For our example we'll create one partition, type LVM and we will use the whole disk size. Let's assume the second disk is secondary master (/dev/hdb) so start fdisk with:

[root@localhost ~]# fdisk /dev/hdb

Create new, primary partition that will contain all the disk space:

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-8322, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-8322, default 8322):
Using default value 8322


Command (m for help): p


Disk /dev/hdb: 4294 MB, 4294967296 bytes
16 heads, 63 sectors/track, 8322 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes


Device Boot Start End Blocks Id System
/dev/hdb1 1 8322 4194256+ 83 Linux

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Note that we changed the partition system ID to Linux LVM. I've chosen the longer way, just to make you see how It's partitioning part done. Because if you plan to have the whole disk space for LVM, you can skip this part of creating partitions, instead, you can just make the whole disk physical volume by typing

[root@localhost ~]# pvcreate /dev/hdb
Physical volume "/dev/hdb" successfully created


Let's check again our PV's with the pvdisplay command again, and you should see something like this:

[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/hda5
VG Name VolGroup00
PV Size 1.44 GB / not usable 0
Allocatable yes
PE Size (KByte) 32768
Total PE 46
Free PE 1
Allocated PE 45
PV UUID auQ2kD-GuHQ-6feE-nSr9-cA0A-dGbD-CrHZhg

--- Physical volume ---
PV Name /dev/hdb
VG Name VolGroup00
PV Size 3.97 GB / not usable 0
Allocatable yes
PE Size (KByte) 32768
Total PE 127
Free PE 127
Allocated PE 0
PV UUID 6TvkYu-sY2K-tIsj-aXZh-KLPb-81Ox-oPIFyO

As you can see we now have two Physical volumes that we will merge into one VG in the next step.

Extend the VG to include the new hard drive or partitions that you've created. In our case, we will add /dev/hdb to the /home VG. You would run the following command:

[root@localhost ~]# vgextend VolGroup00 /dev/hdb
Volume group "VolGroup00" successfully extended

Now let's see the VG stats again:

[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 2
Act PV 2
VG Size 5.41 GB
PE Size 32.00 MB
Total PE 173
Alloc PE / Size 45 / 1.41 GB
Free PE / Size 128 / 4.00 GB
VG UUID P9fvht-mGVG-ksd7-vyhf-yDfi-lhC9-zrNSEw

[root@localhost ~]#

No surprises here, we see that the total size of VG, that we can use for our /home partition can be 5.41GB (128 PE's free).

At this point we have only extend the VG and not the actual size of LV! The lvextend command will configure LVs in KB, MB, GB, or even TB. For example, you could get the same result with the following command:

[root@localhost ~]# lvextend -L5410M /dev/VolGroup00/LogVol00
Rounding up size to full physical extent 5.31 GB
Extending logical volume LogVol00 to 5.31 GB
Logical volume LogVol00 successfully resized

Now, mount the extended volume back

[root@localhost ~]# mount /home

Use the ext2online tool to resize the volume to the actual size

[root@localhost ~]# ext2online /dev/VolGroup00/LogVol00

Let's make sure our extended filesystem is healthy, so we need to unmount it and check it with fsck tool:

[root@localhost ~]# umount /home
[root@localhost ~]# e2fsck -f /dev/VolGroup00/LogVol00
e2fsck 1.38 (30-Jun-2005)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/VolGroup00/LogVol00: 20/334464 files (5.0% non-contiguous), 14806/393216 blocks

Mount /home again and check the size

[root@localhost ~]# mount /home
[root@localhost ~]# df -h

Filesystem Size Used Avail Use% Mounted on
/dev/hda2 1.9G 600M 1.3G 33% /
/dev/hda1 99M 9.3M 85M 10% /boot
/dev/shm 163M 0 163M 0% /dev/shm
/dev/mapper/VolGroup00-LogVol00
5.4G 43M 5.1G 1% /home


Removing Logical Volumes

Removing an existing LV is simple. The command to accomplish that is lvremove.

Make sure you save any data in directories that are mounted on the LV.

Unmount any directories associated with the LV, otherwise you won't be able to delete them.

[root@localhost ~]# umount /dev/Volume01/LogVol01


Next, apply the lvremove command to the LV:

[root@localhost ~]# lvremove /dev/Volume01/LogVol01

Done. You should now have the PEs from this LV free for use in other LVs.

No comments:

Post a Comment