FreeOS.com logo

FreeOS Most Popular
* Most Read stories
* Commented Stories
* Active Categories
* Non Linux Section
* User Submitters
* Top Polls
* Top Authors
* Top Reviews
* Top Rated
* Top Search Terms

Top Articles
* Writing a Linux device driver
* The Linux filesystem explained
* Samba NT Domain Controller
* Setting up Squid as your caching HTTP/FTP proxy
* Web server tutorial - Part 1

FreeOS Highlights
* Howtos (72)
* Reviews (20)
* Opinions (18)
* Interviews (8)
* News (3)

My FreeOS

Nick:
Pass:
Register

Forgot your password?

Contact Us
Contact Us

       

Project: Linux triangle Howtos triangle

LVM: The logical way to manage space

By Mayank Sarup <Mayank@freeos.com>
Posted: ( 2001-04-13 03:26:46 EST by gatha )

Running out of disk space on partitions is a problem commonly and universally experienced by all of us. And although there are various solutions, almost all of them are temporary. In this article we take a detailed look at LVM, which tackles the problem in the most effective manner, making space management easy.

There are tools available that can resize your partition, but what do you do if there is no free space left on your hard drive? One option is that you delete and re-create your partitions. Add another hard drive, create a bigger partition there and move our data there. Although possible, such methods are temporary and in all probability, you will run out of space again. The need of the hour therefore, is not a static and temporary solution, but a more dynamic one that’s also easy to administer and maintain. And LVM fulfills that need.

LVM is very close to the magic 1.0 release (it’s at 0.9), but you don’t have to wait for that version. It’s now part of the 2.4 kernel and patches for 2.2.17+ are available. Some distributions like SuSE have patched the 2.2 kernel for LVM support and also provide the required tools.

LVM frees you from dilemmas like `Should I have a /home that is 5GB or should I make it 4GB and make a slightly larger /var partition?’ For whatever decision you take, most of the time, you either end up allocating more space than required or you find that future requirements have nullified your allocations. But with LVM, you don’t need to spend time thinking about how much space you should allocate to a partition. You can make a partition as big or as small as you like. You can always dynamically add/reduce the space allocated to the partition. Even better, you can do this while your partition is online i.e. it is still mounted.


Get it!

Head over to http://www.sistina.com/lvm. Download the tarball for your LVM version. We grabbed the 0.9.1 beta 2 release. Untar the file in a suitable location and change into the LVM/0.9 directory.

Compile it!

If you’re working with a 2.2 kernel, get your hands on kernel 2.2.17 because that’s the kernel version for which patches are available in the `Patches’ sub-directory. Applying the patches is a simple affair. Read the `Install’ file for more information on the patches suitable for your system. Change to /usr/src/linux or to the directory containing the kernel sources and run the following commands to patch your kernel. Patch in the order given below.

cat /src/LVM/0.9/PATCHES/linux-2.2.17-rawio.patch | patch –p1
cat /src/LVM/0.9/PATCHES/lvm-0.9-2.2.17-stock.patch | patch -p1

Patches for beta 2.4 kernels are also available, but since it has already made its way into the kernel, you will be better off using a stable kernel release. Here, we’re using kernel 2.4.3.

Do a ‘make menuconfig’ to configure your patched kernel. Go into “Multi-device support” and choose `Compile support’ for LVM into your kernel or make it into a module. We recommend making it into a Module. In that case, put the following two lines in your /etc/modules.conf

alias block-major-58 lvm-mod
alias char-major-109 lvm-mod

You will also require the userspace tools for LVM administration. Go to LVM/0.9 directory and compile them.

./configure
make
make install

Now that you have the required LVM tools installed, add “vgscan” and “vgchange –-a y” to your startup files so that, the available volume groups are initialized and available at startup before the LVM-based filesystems are mounted. This would be /sbin/init.d/boot in SuSE, and /etc/rc.d/rc.sysinit in Red Hat. Similarly add “vgchange -a n” to the shutdown script after your filesystems are unmounted. Look for the script /sbin/init.d/halt in SuSE.

Understand it!

Now, before you start using LVM, you need to look at some of the terms that you will encounter. To start with, there are Physical Volumes (PVs). These are the basic building blocks and would be the partitions that you have available. You can also use RAID volumes but we’re just working with plain old partitions. So /dev/hda6, hdb1 etc. are the PV’s.

These PV’s together form a Volume Group (VG). Basically, the effective disk space that is available in a VG is the sum total of the various PV’s that you add to the VG.

Once you have allocated enough disk space to your VG (Don’t worry about allocating space. Remember, you can add any amount of space later), you can set about creating Logical Volumes (LVs). LVs are the final containers of your data. We’'ve actually come full circle here. We started out with distinct partitions that were collated into a single storage area and now we’re creating distinct, logical partitions from the space we added to the VG.

Use it!

Now that you know a few of the buzzwords of LVM, lets go on to the command line and start using LVM. Look for some unused partitions/space that can be used for LVM. You need at least one. Create a new partition or use an existing partition. Existing partitions will lose all data so make sure there’s nothing useful on those partitions. Fdisk is a good tool to create the partitions. You will also have to set the partition type to 8e (Linux LVM).

#fdisk -l /dev/hda
...
/dev/hda7 2362 2616 2048256 8e Linux LVM
/dev/hda8 2617 2871 2048256 8e Linux LVM

Now initialize the partition.

#pvcreate /dev/hda7
pvcreate -- —physical volume “/dev/hda7” successfully created

# pvcreate /dev/hda8
pvcreate— -- physical volume “/dev/hda8” successfully created

The above command creates a volume group descriptor at the start of the partition. The allocation of the physical extents is stored here.

Create a new volume group and add the two physical volumes to it.

# vgcreate freeos /dev/hda7 /dev/hda8
vgcreate— -- INFO: using default physical extent size 4 MB
vgcreate— -- INFO: maximum logical volume size is 255.99 Gigabyte
vgcreate— -- doing automatic backup of volume group “freeos”
vgcreate— -- volume group “freeos” successfully created and activated

This will create a volume group named freeos containing the physical volumes /dev/hda7 and /dev/hda8.

Now you can go on to create the logical volumes within the volume group.

# lvcreate -L 2G -n lv1 freeos
lvcreate— -- doing automatic backup of “freeos”
lvcreate -- —logical volume “/dev/freeos/lv1” successfully created

This will create a logical volume of size 2GB named lv1 in the volume group freeos.

Use the command “vgdisplay” to see the details regarding the volume groups present in your system.

# vgdisplay
--- Volume group ---
VG Name freeos
VG Access read/write
VG Status available/resizable
VG # 0
MAX LV 256
Cur LV 1
Open LV 0
MAX LV Size 255.99 GB
Max PV 256
Cur PV 2
Act PV 2
VG Size 3.91 GB
PE Size 4 MB
Total PE 1000
Alloc PE / Size 256 / 1 GB
Free PE / Size 744 / 2.91 GB
VG UUID TB8zIU-MGPs-uo6r-cB5T-Uj3q-SzPB-fDfFEE

Now you need to build a filesystem on this logical volume. Here, we’re using reiserfs.

#mkreiserfs /dev/freeos/lv1

Mount the newly created filesystem. We found that we had to specify the filesystem type.

#mount -t reiserfs /dev/freeos/lv1 /mnt/lv1

Now you can copy all your data here or move some partition running short of space here. If there are no problems and the working is perfect, you can add an entry to /etc/fstab so that the filesystem is mounted at boot.

/dev/freeos/lv1 /mnt/lv1 reiserfs defaults 1 2

Resizing!

Well, this is cited as the most important reason for using LVM— -- the fact that you can just add space to your volume group and also dynamically resize your logical volumes.

Adding more space to your volume group is a simple affair. Let’s say you have /dev/hda9 of type Linux LVM and you would like to add that to the volume group freeos. The tool to use is vgextend.

# vgextend freeos /dev/hda9

vgextend— -- INFO: maximum logical volume size is 255.99 Gigabyte
vgextend -- —doing automatic backup of volume group “freeos”
vgextend -- —volume group “freeos” successfully extended

Ok, so now you have added more space to your volume group. Let’s use some of the space to extend our lv1 logical volume.

# lvextend -L+1G /dev/freeos/lv1

lvextend— -- extending logical volume “/dev/freeos/lv1” to 2 GB
lvextend— -- doing automatic backup of volume group “freeos”
lvextend— -- logical volume “/dev/freeos/lv1” successfully extended

Lvextend takes a variety of parameters for size. You use the plus “+” to say that that partition size should be extended by 1 GB. You can, alternatively, use a “-“ here to reduce the partition size by 1 GB. You can give an absolute value too.

Now check the free space on your partition.

# df -h
/dev/freeos/lv1 1.0G 32M 992M 3% /mnt/test

You still need to resize the filesystem to take over the new space.

# resize_reiserfs -s+1024M -f /dev/freeos/lv1
# df -h
/dev/freeos/lv1 2.0G 32M 2.0G 2% /mnt/test

Although you can do the resize while the partition is online, we recommend that you first unmount the partition and then do such operations on the filesystem. When resizing downwards, it is definitely a must that you unmount the filesystem.

To reduce the filesystem size, first reduce the size of your logical volume.

# resize_reiserfs -s-1024M -f /dev/freeos/lv1
You are running ALPHA version of reiserfs shrinker.
This version is only for testing or very careful use. Backup of your data is essential.
Do you want to continue? [y/N]:y
Processing the tree: 0%....20%....40%....60%....80%....100%
nodes processed (moved):
int 0 (0),
leaves 1 (0),
unfm 0 (0),
total 1 (0).

ReiserFS report:
blocksize 4096
block count 262144 (524288)
free blocks 253925 (516061)
bitmap block count 8 (16)

Syncing..done

Now reduce the size of the logical volume.

# lvreduce -L-1G /dev/freeos/lv1
lvreduce -- —Warning: reducing active logical volume to 1 GB
lvreduce— -- This may destroy your data (filesystem etc.)
lvreduce -- —do you really want to reduce “/dev/freeos/lv1”? [y/n]: y
lvreduce— -- doing automatic backup of volume group “freeos”
lvreduce— -- logical volume “/dev/freeos/lv1” successfully reduced

As you can see, barring the initial creation of the volume groups, LVM is far more extensible and easy to use than what you have been used to so far. Now you don’t need to worry about running out of disk space, thanks to LVM, and carry on working.

LVM home

Other articles by Mayank Sarup

Current Rating: [ 7.76 / 10 ] Number of Times Rated: [ 122 ]

More Howtos
* Kernel Compilation & Avoiding ‘Unresolved Symbol’
* Configuring CVS and CVSUP on Linux
* Knoppix installation tips
* Maximum Mount
* A WebServer Guide -- Help Using Apache

Contents
Articles
  Howtos
  Interviews
  News
  Opinions
  Reviews
Comparison
Links
  Articles
  Howtos
  Interviews
  Opinions
  Reviews
  Websites
News

Linux
About Linux

Print It!
Printer Friendly Version