HostOnNet Blog

lvresize

Looking for Linux Server Admin or WordPress Expert? We can help.

lvresize can be used to resize LVM partition.

Lets create a LVM partition to test

[root@kvm /]# lvcreate --size 100G --name vm1 vg_kvm
  Logical volume "vm1" created.
[root@kvm /]#

Format the LVM partition as ext4

[root@kvm /]# mkfs.ext4 /dev/mapper/vg_kvm-vm1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
6553600 inodes, 26214400 blocks
1310720 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
800 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000, 7962624, 11239424, 20480000, 23887872

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

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

Verify the file system by mounting it and copy a file to it, so we can verify it works after resize.

[root@kvm /]# mount /dev/mapper/vg_kvm-vm1 /mnt
[root@kvm /]# cd /mnt
[root@kvm mnt]# cp /boot/grub/grub.conf .
[root@kvm mnt]# ll
total 20
-rw-------. 1 root root  1177 Sep 25 17:00 grub.conf
drwx------. 2 root root 16384 Sep 25 16:58 lost+found
[root@kvm mnt]#

Unmount the disk with umount

[root@kvm ~]# umount /mnt
[root@kvm ~]#

Before reisze, we should check the hard disk for errors

[root@kvm ~]# fsck -fy /dev/mapper/vg_kvm-vm1
fsck from util-linux-ng 2.17.2
e2fsck 1.41.12 (17-May-2010)
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/mapper/vg_kvm-vm1: 12/6553600 files (0.0% non-contiguous), 459350/26214400 blocks
[root@kvm ~]#

Resize the filesystem with resize2fs

[root@kvm ~]# resize2fs /dev/mapper/vg_kvm-vm1 10G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/vg_kvm-vm1 to 2621440 (4k) blocks.
The filesystem on /dev/mapper/vg_kvm-vm1 is now 2621440 blocks long.

[root@kvm ~]#

We have resized filesystem inside LVM parttion. LVM partition size did not changed yet. Lets mount the partition and verify filesystem size

[root@kvm ~]# mount /dev/mapper/vg_kvm-vm1 /mnt
[root@kvm ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_kvm-lv_root
                       50G  1.6G   46G   4% /
tmpfs                  16G     0   16G   0% /dev/shm
/dev/sda1             477M   52M  400M  12% /boot
/dev/mapper/vg_kvm-lv_home
                      163G  162M  154G   1% /home
/dev/mapper/vg_kvm-vm1
                      9.8G   36M  9.2G   1% /mnt
[root@kvm ~]#

File system size is 9.8 GB. Looks good, so we unmount the partition

[root@kvm ~]# umount /mnt
[root@kvm ~]#

Now we can resize lvm partition with lvresize command. Make sure the size we give is bigger than actual file system size or file system get corrupted. Alwasy take backup before you do as there is a chance of data lose when you do.

[root@kvm ~]# lvresize --size 12G  /dev/mapper/vg_kvm-vm1
  WARNING: Reducing active logical volume to 12.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg_kvm/vm1? [y/n]: y
  Size of logical volume vg_kvm/vm1 changed from 100.00 GiB (25600 extents) to 12.00 GiB (3072 extents).
  Logical volume vm1 successfully resized.
[root@kvm ~]#

Verify it by mounting the partition

[root@kvm ~]# mount /dev/mapper/vg_kvm-vm1 /mnt
[root@kvm ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_kvm-lv_root
                       50G  1.6G   46G   4% /
tmpfs                  16G     0   16G   0% /dev/shm
/dev/sda1             477M   52M  400M  12% /boot
/dev/mapper/vg_kvm-lv_home
                      163G  162M  154G   1% /home
/dev/mapper/vg_kvm-vm1
                      9.8G   36M  9.2G   1% /mnt
[root@kvm ~]# lvs
  LV      VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_home vg_kvm -wi-ao---- 164.77g                                                    
  lv_root vg_kvm -wi-ao----  50.00g                                                    
  lv_swap vg_kvm -wi-ao----  15.73g                                                    
  vm1     vg_kvm -wi-ao----  12.00g                                                    
[root@kvm ~]# 

lvs also show new size of 12 GB.

At this stage, we can run resize2fs again to grow the filesystem to use full LVM partition, so there is no space wasted.

See lvm

Posted in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.