shrinking a lvm partition is straight forward if it doesn’t contain a partition table. Simply do a:
umount lvm_partition resize2fs /dev/vg/lv newSize lvresize -L disksize /dev/vg/lv resize2fs /dev/vg/lv
execute the last command if nessary. Assuming you have a partition table in a 10G domU which uses the default partition table,
Disk /dev/xenvg/XenWeb: 10.5 GB, 10502537216 bytes 255 heads, 63 sectors/track, 1276 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/xenvg/XenWeb1 * 1 13 104391 83 Linux /dev/xenvg/XenWeb2 47 1276 9879975 83 Linux /dev/xenvg/XenWeb3 14 46 265072+ 82 Linux swap / Solaris [root@bpehhome2 mapper]# lvscan ACTIVE '/dev/xenvg/root' [4.50 GB] inherit ACTIVE '/dev/xenvg/XenAuth' [8.00 GB] inherit ACTIVE '/dev/xenvg/XenDebianDefault' [2.00 GB] inherit ACTIVE '/dev/xenvg/swap' [1.00 GB] inherit ACTIVE '/dev/xenvg/XenCentOSInstall' [3.00 GB] inherit ACTIVE '/dev/xenvg/XenWeb1' [5.03 GB] inherit ACTIVE '/dev/xenvg/XenWeb' [9.78 GB] inherit
I want to reduce the size of the XenWeb logical volume to 9G. First of all, let me shut it down.
[root@bpehhome2 mapper]# xm shutdown web [root@bpehhome2 mapper]# pwd /dev/mapper [root@bpehhome2 mapper]# kpartx -a /dev/xenvg/XenWeb [root@bpehhome2 mapper]# ls control xenvg-XenAuth xenvg-XenWeb xenvg-XenWebp2 XenWeb2 xenvg-root xenvg-XenCentOSInstall xenvg-XenWeb1 xenvg-XenWebp3 XenWeb3 xenvg-swap xenvg-XenDebianDefault xenvg-XenWebp1 XenWeb1
OK, let’s reduce the partition XenWeb2 size. I want XenWeb2 to be 9000M, but when I do the resize, I need to bring it abit lower, say 8500M. You will see why later.
[root@bpehhome2 mapper]# resize2fs XenWeb2 8500M resize2fs 1.39 (29-May-2006) Please run 'e2fsck -f XenWeb2' first. [root@bpehhome2 mapper]# e2fsck -f XenWeb2 e2fsck 1.39 (29-May-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 /: 246385/2395520 files (1.0% non-contiguous), 1044750/2469993 blocks [root@bpehhome2 mapper]# resize2fs XenWeb2 8500M resize2fs 1.39 (29-May-2006) Resizing the filesystem on XenWeb2 to 2176000 (4k) blocks. The filesystem on XenWeb2 is now 2176000 blocks long.
Its time to resize the logical volumn
[root@bpehhome2 mapper]# lvresize -L 9000M /dev/xenvg/XenWeb Rounding up size to full physical extent 8.81 GB WARNING: Reducing active and open logical volume to 8.81 GB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce XenWeb? [y/n]: y Reducing logical volume XenWeb to 8.81 GB Logical volume XenWeb successfully resized [root@bpehhome2 mapper]# fdisk /dev/xenvg/XenWeb The number of cylinders for this disk is set to 1150. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Command (m for help): p Disk /dev/xenvg/XenWeb: 9462 MB, 9462349824 bytes 255 heads, 63 sectors/track, 1150 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/xenvg/XenWeb1 * 1 13 104391 83 Linux /dev/xenvg/XenWeb2 47 1276 9879975 83 Linux /dev/xenvg/XenWeb3 14 46 265072+ 82 Linux swap / Solaris Partition table entries are not in disk order Command (m for help): d Partition number (1-4): 2 Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (47-1150, default 47): Using default value 47 Last cylinder or +size or +sizeM or +sizeK (47-1150, default 1150): Using default value 1150 Command (m for help): p Disk /dev/xenvg/XenWeb: 9462 MB, 9462349824 bytes 255 heads, 63 sectors/track, 1150 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/xenvg/XenWeb1 * 1 13 104391 83 Linux /dev/xenvg/XenWeb2 47 1150 8867880 83 Linux /dev/xenvg/XenWeb3 14 46 265072+ 82 Linux swap / Solaris Partition table entries are not in disk order 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 22: Invalid argument. The kernel still uses the old table. The new table will be used at the next reboot. Syncing disks.
Noticed that XenWeb2 is now 8.8G which is close to what we want. But we still need to do another resize2fs to get XenWeb2 working.
[root@bpehhome2 mapper]# kpartx -a /dev/xenvg/XenWeb [root@bpehhome2 mapper]# resize2fs XenWeb2 resize2fs 1.39 (29-May-2006) Resizing the filesystem on XenWeb2 to 2216970 (4k) blocks. The filesystem on XenWeb2 is now 2216970 blocks long. [root@bpehhome2 mapper]# kpartx -d /dev/xenvg/XenWeb
Everything looks good. Its time now to reboot my domU and test if it is working.
[root@bpehhome2 mapper]# xm create /etc/xen/web.cfg [root@bpehhome2 mapper]# xm console web CentOS release 5 (Final) Kernel 2.6.18-53.1.14.el5xen on an i686 web login: root Password: Last login: Sat Feb 7 10:32:43 on xvc0 [root@web ~]# mount /dev/xvda2 on / type ext3 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) /dev/xvda1 on /boot type ext3 (rw) tmpfs on /dev/shm type tmpfs (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) [root@web ~]# fdisk -l Disk /dev/xvda: 9462 MB, 9462349824 bytes 255 heads, 63 sectors/track, 1150 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/xvda1 * 1 13 104391 83 Linux /dev/xvda2 47 1150 8867880 83 Linux /dev/xvda3 14 46 265072+ 82 Linux swap / Solaris Partition table entries are not in disk order
Everything looks ok. It is a good idea to do a backup of the logical volume before shrinking because shrinking disk space is always more involved than extending it.
I described the theory more in depth in my other article about extending lvm partition.