A Beginner's Guide To LVM - Page 4

Want to support HowtoForge? Become a subscriber!
 
Submitted by falko (Contact Author) (Forums) on Sun, 2007-01-14 19:06. ::

4 Resize Logical Volumes And Their Filesystems

In this chapter we will learn how to resize our logical volume share which has an ext3 filesystem. (I will show how to resize logical volumes with xfs and reiserfs filesystems further down this tutorial.)

First we must unmount it:

umount /var/share

share should not be listed anymore in the

df -h

output:

server1:~# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              19G  665M   17G   4% /
tmpfs                  78M     0   78M   0% /lib/init/rw
udev                   10M   88K   10M   1% /dev
tmpfs                  78M     0   78M   0% /dev/shm
/dev/sda1             137M   17M  114M  13% /boot
/dev/mapper/fileserver-backup
                      5.0G  144K  5.0G   1% /var/backup
/dev/mapper/fileserver-media
                      1.0G   33M  992M   4% /var/media

Now let's enlarge share from 40GB to 50GB:

lvextend -L50G /dev/fileserver/share

server1:~# lvextend -L50G /dev/fileserver/share
  Extending logical volume share to 50.00 GB
  Logical volume share successfully resized

Until now we have enlarged only share, but not the ext3 filesystem on share. This is what we do now:

e2fsck -f /dev/fileserver/share

server1:~# e2fsck -f /dev/fileserver/share
e2fsck 1.40-WIP (14-Nov-2006)
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/fileserver/share: 11/5242880 files (9.1% non-contiguous), 209588/10485760 blocks

Make a note of the total amount of blocks (10485760) because we need it when we shrink share later on.

resize2fs /dev/fileserver/share

server1:~# resize2fs /dev/fileserver/share
resize2fs 1.40-WIP (14-Nov-2006)
Resizing the filesystem on /dev/fileserver/share to 13107200 (4k) blocks.
The filesystem on /dev/fileserver/share is now 13107200 blocks long.

Let's mount share:

mount /dev/fileserver/share /var/share

and in the

df -h

output share should now have 50GB instead of 40:

server1:~# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              19G  665M   17G   4% /
tmpfs                  78M     0   78M   0% /lib/init/rw
udev                   10M   88K   10M   1% /dev
tmpfs                  78M     0   78M   0% /dev/shm
/dev/sda1             137M   17M  114M  13% /boot
/dev/mapper/fileserver-backup
                      5.0G  144K  5.0G   1% /var/backup
/dev/mapper/fileserver-media
                      1.0G   33M  992M   4% /var/media
/dev/mapper/fileserver-share
                       50G  180M   47G   1% /var/share

Shrinking a logical volume is the other way round: first we must shrink the filesystem before we reduce the logical volume's size. Let's shrink share to 40GB again:

umount /var/share

df -h

server1:~# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              19G  665M   17G   4% /
tmpfs                  78M     0   78M   0% /lib/init/rw
udev                   10M   88K   10M   1% /dev
tmpfs                  78M     0   78M   0% /dev/shm
/dev/sda1             137M   17M  114M  13% /boot
/dev/mapper/fileserver-backup
                      5.0G  144K  5.0G   1% /var/backup
/dev/mapper/fileserver-media
                      1.0G   33M  992M   4% /var/media

e2fsck -f /dev/fileserver/share

server1:~# e2fsck -f /dev/fileserver/share
e2fsck 1.40-WIP (14-Nov-2006)
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/fileserver/share: 11/6553600 files (9.1% non-contiguous), 251733/13107200 blocks

When resizing an ext3 filesystem to a certain size (instead of all available space), resize2fs takes the number of blocks as argument (you can as well specify the new size in MB, etc. See

man resize2fs

for more details). From our previous operation we know the 40GB equals 10485760 blocks so we run

resize2fs /dev/fileserver/share 10485760

server1:~# resize2fs /dev/fileserver/share 10485760
resize2fs 1.40-WIP (14-Nov-2006)
Resizing the filesystem on /dev/fileserver/share to 10485760 (4k) blocks.
The filesystem on /dev/fileserver/share is now 10485760 blocks long.

We've shrinked the filesystem, now we must shrink the logical volume, too:

lvreduce -L40G /dev/fileserver/share

server1:~# lvreduce -L40G /dev/fileserver/share
  WARNING: Reducing active logical volume to 40.00 GB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce share? [y/n]:
 <-- y
  Reducing logical volume share to 40.00 GB
  Logical volume share successfully resized

We can ignore the warning that data might be destroyed because we have shrinked the filesystem before.

Let's mount share again:

mount /dev/fileserver/share /var/share

The output of

df -h

should now look like this:

server1:~# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              19G  665M   17G   4% /
tmpfs                  78M     0   78M   0% /lib/init/rw
udev                   10M   88K   10M   1% /dev
tmpfs                  78M     0   78M   0% /dev/shm
/dev/sda1             137M   17M  114M  13% /boot
/dev/mapper/fileserver-backup
                      5.0G  144K  5.0G   1% /var/backup
/dev/mapper/fileserver-media
                      1.0G   33M  992M   4% /var/media
/dev/mapper/fileserver-share
                       40G  177M   38G   1% /var/share


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Mon, 2014-05-05 01:16.
need to not use if fdisk if drive is over 2 TB though.
Submitted by acname (not registered) on Sun, 2012-12-09 10:26.
perfect manual. thanx a lot
Submitted by jonathan young (not registered) on Sun, 2012-01-29 06:39.

This guide is so idiot-proofed and full of explanations.  Thank you so much, you saved my bacon. I am a beginning linux administrator (as a sideline to being a web architect) and LVM is so brand new to me, I was scared to resize lv's and now i'm like "wow, this is easy" 

 thank you so much!