Random Bits

Random Bits

  • Home
  • Contact

How to Shrink an LVM Volume Safely

Logical Volume Management is a vast improvement over standard partitioning schemes. Among many other things, it allows you to decrease the size of a volume without recreating it completely. Here’s how.

First, as is always the case when you’re modifying disk volumes, partitions, or file systems, you should really have a recent backup. A typo in one the following commands could easily destroy data. You have been warned!

All of the required steps must be performed on an unmounted volume. If want to reduce the size of a non-root volume, simply unmount it. For a root volume, you’ll have to boot from a CD. Any modern live or rescue CD should work fine. I prefer SystemRescueCD. It includes almost any disk management programs you might need. After booting from a CD, you may have to issue:

# vgchange -a y

This makes any logical volumes available to the Linux. Most boot CD’s will do it automatically some time during the boot process, but repeating the command won’t hurt. Next, force a file system check on the volume in question:

# e2fsck -f /dev/polar/root

Device names for LVM volumes follow the convention: /dev/<volume group>/<logical volume>. In this case, my volume group is named polar and the volume I’m going to shrink is named root. This is a critical step; resizing a file system in an inconsistent state could have disastrous consequences. Next, resize the actual file system:

# resize2fs /dev/polar/root 180G

Replace 180G with about 90% of the size you want the final volume to be. For example, in this case, I want the final volume to be 200 gigabytes, so I’ll reduce the file system to 180 gigabytes. Why is this necessary? When we reduce the size of the actual volume in the next step, it’s critical that the new size is greater than or equal to the size of the file system. After reading the documentation for both resizefs and lvreduce, I still haven’t been able to find out whether they’re using standard computer gigabytes (1024^3 bytes) or drive manufacturer gigabytes (1000^3 bytes). In this case, the difference is very important. To be on the safe side, we’ll just shrink the file system a bit more than necessary and expand it to use the full space available later. Next, reduce the size of the logical volume:

# lvreduce -L 200G /dev/polar/root

In this case, use the actual size you want to the volume to be. Finally, grow the file system so that it uses all available space on the logical volume:

# resize2fs /dev/polar/root

That’s it. Enjoy your newly acquired free space.

Related Posts:

  • Safely Removing External Drives in Linux

    Simply unmounting a filesystem is not the ideal way to...

  • Monitoring Hard Drive Health on Linux with smartmontools

    S.M.A.R.T. is a system in modern hard drives designed to...

  • Log iptables Messages to a Separate File with rsyslog

    Learn how to filter iptables log messages to a separate...

Sponsored Links:

This entry was posted on Saturday, August 30th, 2008 at 6:38 AM and is filed under Linux. You can leave a response, or trackback from your own site.

16 Comments on “How to Shrink an LVM Volume Safely”

  1. Col says:
    February 18, 2010 at 6:48 PM

    Excellent tutorial, exactly what I was after, thanks for sharing!

    Reply
  2. Anonymous says:
    July 22, 2010 at 12:48 PM

    [...] [...]

    Reply
  3. How can I minimise the size of an ext3 partition (and its LVM logical volume)? Drija says:
    November 17, 2010 at 10:31 PM

    [...] affect the logical volume. Most of the recipes on the web for shrinking ext3 inside an LV (e.g. this one), follow this basic pattern to alleviate problems with inaccurate calculations of filesystem [...]

    Reply
  4. m47730 says:
    January 19, 2011 at 5:24 AM

    Great tutorial!
    Be aware to unmount volume and be certain that the command doesn’t give you errors, otherwise you destroy the filesystem! By expirience :.(

    Reply
  5. 秋天的博客 » How to Shrink an LVM Volume Safely says:
    March 4, 2011 at 9:43 PM

    [...] http://blog.shadypixel.com/how-to-shrink-an-lvm-volume-safely/ [...]

    Reply
  6. Vincent's Memo » Blog Archive » lvm – shrink lvm volume says:
    June 4, 2011 at 9:18 AM

    [...] Reference URL: http://blog.shadypixel.com/how-to-shrink-an-lvm-volume-safely/ [...]

    Reply
  7. Natuerliche Arzneimittel says:
    September 16, 2011 at 5:40 AM

    If you are not careful, you can shrink the LV to a size less than what is required by the file system. If the LV is resized smaller than what the file system has been resized to, things will go very badly for you. Did I mention you should backup your data before hand?

    Reply
  8. Vkram says:
    January 3, 2012 at 11:10 AM

    # lvreduce -L 200G /dev/polar/root
    Under RHEL5.6 this command is like wrong turn. Beside reducing the size its make the LV size 200M, Dangreous for real experiment.
    This is a right comman which reduce the size.

    # lvreduce -L -200G /dev/polar/root

    Reply
    • Brian says:
      April 20, 2012 at 2:58 AM

      @Vkram the requirement is to change the size to 200G and not to remove 200G from the Logical Volume. So the initial command is correct.

      Reply
    • forgo says:
      February 11, 2013 at 2:01 AM

      What is the difference between:

      # lvreduce -L 200G /dev/polar/root and
      # lvreduce -L 200G /dev/polar/root ??

      Reply
  9. sadirux says:
    October 2, 2012 at 4:23 AM

    hello,
    lvreduce or lvextend can automaticaly resize fs with -r flag

    lvreduce -r -L 200G /dev/polar/root

    have fun

    Reply
  10. buy sell software says:
    January 8, 2013 at 11:39 PM

    I wanted to post you the very small word just to give many thanks as before regarding the unique concepts you have shown here. It was extremely generous of people like you to make extensively all that many people could have made available for an ebook to end up making some dough for their own end, especially given that you could possibly have tried it if you ever considered necessary. These basics additionally served to provide a great way to understand that the rest have a similar dream like my very own to grasp somewhat more regarding this issue. I’m certain there are millions of more enjoyable periods in the future for individuals who look over your blog post.

    Reply
  11. random says:
    January 14, 2013 at 3:35 AM

    1 GB = 1 gigabyte = 1000 migabytes, while 1 GiB = 1 gibibyte = 1024 mibibytes, see http://en.wikipedia.org/wiki/Gigabyte

    Reply
  12. Guido Kroon says:
    January 25, 2013 at 1:46 PM

    Thank you so much for clarifying this.

    Reply
  13. treating toenail fungus says:
    April 23, 2013 at 9:20 PM

    Today, I went to the beachfront with my kids. I found a sea shell and gave it to
    my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the shell to her ear and screamed.
    There was a hermit crab inside and it pinched her ear. She never wants to
    go back! LoL I know this is totally off topic but I had to tell someone!

    Reply
  14. Harshal Kshatriya says:
    May 14, 2013 at 2:39 AM

    Thanks for sharing.. Amazing post. Really helpful..

    Reply

Leave a Reply

Click here to cancel reply.

  • Topics

    • Personal (2)
    • Technology (13)
      • Linux (11)
      • Web (4)
  • Meta

    • Register
    • Log in
    • Valid XHTML

Feed | Privacy Policy
Copyright © 2008-2009 Random Bits
Powered by Wordpress