Note: XFS, ResierFS等其他文件系统的resize步骤各有不同。做resize之前,请阅读相关文件系统文档 。如果操作有误可能导致文件系统损坏,请在进行resize之前备份数据!!

LV可以根据需求增大或减小,但是LV改变大小以后,在LV中的文件系统也需要相应的改变大小。这个概念非常重要,如果没有相应的调整LV中文件系统的大小,那么可能导致数据误用!

在下面内容中,我们将指导你增大和缩小LV,并相应处理相关的文件系统,Let's get to it!

增大文件系统

增大LVM中的文件系统可以在线操作(文件系统处于mount状态),为了增大LV 和文件系统:

  • 检查包含文件系统的LV中是否有剩余空间
  • 如果LV中没有剩余空间,则需要扩展LV(可能需要扩展VG,如果没有剩余空间)
  • 使用LV中的所有可用空间增大文件系统

Lets get started!

  1. 首先检查文件系统大小,确认是否需要扩展

    [root@Linux01 ~]# pwd
    /TCPDumpLV

    [root@Linux01 TCPDumpLV]# df -kh .

    Filesystem                                             Size  Used  Avail  Use%  Mounted on
    /dev/mapper/TCPDumpVolGRP-TCPDumpLV  3.1G  2.9G  69M   98%    /TCPDumpLV

    Note: df命令显示系统可用磁盘空间69M,使用率98%,如果不增加空间很快该目录就会被占满。

  2. 让我们确认哪个VG包含LV-> /dev/mapper/TCPDumpVolGRP-TCPDumpLV

    [root@Linux01 ~]# lvdisplay /dev/TCPDumpVolGRP/TCPDumpLV

    --- Logical volume ---

    LV Name                   /dev/TCPDumpVolGRP/TCPDumpLV
    VG Name                  TCPDumpVolGRP
    LV UUID                    hYQs4t-YtY7-51hl-c4ps-4N6d-2W7h-IidcxF
    LV Write Access         read/write
    LV Status                  available
    # open                      1
    LV Size                      3.12 GB
    Current LE                  100
    Segments                   1
    Allocation                    inherit
    Read ahead sectors       auto
    - currently set to          256
    Block device                 253:5

    Note: 从上边输出可以看到对应的VG是TCPDumpVolGRP

  3. 我们检查一下VG->TCPDumpVolGRP是否有足够的可用空间分配给LV

    [root@Linux01 ~]# vgdisplay TCPDumpVolGRP

    --- Volume group ---

    VG Name                           TCPDumpVolGRP
    System ID
    Format                               lvm2
    Metadata Areas  3
    Metadata Sequence No         5
    VG Access                          read/write
    VG Status                           resizable
    MAX LV                              0
    Cur LV                               2
    Open LV                             2
    Max PV                              0
    Cur PV                               3
    Act PV                               3
    VG Size                              11.91 GB
    PE Size                               32.00 MB
    Total PE                             381
    Alloc PE / Size                     228 / 7.12 GB
    Free PE / Size                     153 / 4.78 GB
    VG UUID                             9fWFIS-vDlg-xOW6-Xmb8-Tkrg-GPZw-ZnUZwh

    Note: 由输出可以看出此VG有4.78G可用空间。如果这个可用空间还是不够使用,那么我们就需要先向VG中增加PV才能进行下面步骤。

  4. 此处,我们假设给LV->TCPDumpLV增加3GB

    [root@Linux01 TCPDumpLV]# lvresize -L +3GB /dev/TCPDumpVolGRP/TCPDumpLV
    Extending logical volume TCPDumpLV to 6.12 GB
    Logical volume TCPDumpLV successfully resized
    [root@Linux01 TCPDumpLV]#

  5. 确认LV的新大小

    [root@Linux01 ~]# lvdisplay /dev/TCPDumpVolGRP/TCPDumpLV

    --- Logical volume ---

    LV Name                      /dev/TCPDumpVolGRP/TCPDumpLV
    VG Name                     TCPDumpVolGRP
    LV UUID                       hYQs4t-YtY7-51hl-c4ps-4N6d-2W7h-IidcxF
    LV Write Access            read/write
    LV Status                    available
    # open                        1
    LV Size                        6.12 GB
    Current LE                   196
    Segments                     2
    Allocation                     inherit
    Read ahead sectors        auto
    - currently set to           256
    Block device                  253:5

     

    [root@Linux01 ~]# pwd

    /TCPDumpLV

    [root@Linux01 TCPDumpLV]# df -kh .

    Filesystem                                                 Size     Used       Avail       Use%       Mounted on
    /dev/mapper/TCPDumpVolGRP-TCPDumpLV      3.1G     2.9G        69M        98%        /TCPDumpLV

    Note: 可以看到 LV已经增大到 6.12GB,但是文件系统仍然是 3.1G没有改变。

     

  6. 现在需要resize相应的ext3文件系统来利用LV中的可用空间。

    [root@Linux01 TCPDumpLV]# resize2fs -p /dev/mapper/TCPDumpVolGRP-TCPDumpLV
    resize2fs 1.39 (29-May-2006)
    Filesystem at /dev/mapper/TCPDumpVolGRP-TCPDumpLV is mounted on /TCPDumpLV; on-line resizing required
    Performing an on-line resize of /dev/mapper/TCPDumpVolGRP-TCPDumpLV to 1605632 (4k) blocks.
    The filesystem on /dev/mapper/TCPDumpVolGRP-TCPDumpLV is now 1605632 blocks long.

    [root@Linux01 TCPDumpLV]# df -kh .

    Filesystem                                              Size  Used  Avail Use% Mounted on
    /dev/mapper/TCPDumpVolGRP-TCPDumpLV   6.1G  2.9G  2.9G  50%  /TCPDumpLV

     

     

     

     

     

     

     

     

     

OK,我们在线增加了你的文件系统大小!接下来讲解如何减小文件系统大小。

减小文件系统

减小LVM中的文件系统必须离线操作(处于umount装态),要减小文件系统和LV:

  • Unmount相应的文件系统
  • 运行磁盘检查确保卷的完整
  • 减小文件系统
  • 减小LV

Note: You cannot shrink the file system beyond the amount of free space that is available on it.  So if the file system you want to shrink has 1GB of free space, you will only be able to shrink the volume by 1GB.  However, logical volumes are not as forgiving.  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 we mention you should backup your data before hand?

Let's get started!

  1. First, check to see how much space is available

    [root@Linux01 ~]# pwd
    /TCPDumpLV

    [root@Linux01 TCPDumpLV]# df -kh .
    Filesystem                                                 Size     Used      Avail       Use%         Mounted on
    /dev/mapper/TCPDumpVolGRP-TCPDumpLV       6.1G    922M     4.9G        16%          /TCPDumpLV
    Note: The disk free command shows that we are using 922MB and have 4.9G available on our file system.  Therefore, we can safely shrink the volume to 1.5G (leaving a little bit for overhead) without any issue.

  2. 卸载文件系统

    [root@Linux01 TCPDumpLV]# cd /
    [root@Linux01 /]# umount /TCPDumpLV

  3. 检查文件系统是否有错误

    [root@Linux01 /]# e2fsck -f /dev/mapper/TCPDumpVolGRP-TCPDumpLV
    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
    /dev/mapper/TCPDumpVolGRP-TCPDumpLV: 13/802816 files (7.7% non-contiguous), 261017/1605632 blocks
    [root@Linux01 /]#

  4. 减小文件系统到1.5GB

    [root@Linux01 /]# resize2fs /dev/mapper/TCPDumpVolGRP-TCPDumpLV 1500M
    resize2fs 1.39 (29-May-2006)
    Resizing the filesystem on /dev/mapper/TCPDumpVolGRP-TCPDumpLV to 384000 (4k) blocks.
    The filesystem on /dev/mapper/TCPDumpVolGRP-TCPDumpLV is now 384000 blocks long.
    [root@Linux01 /]#

  5. 减小LV到1.5GB

    [root@Linux01 /]# lvresize -L 1.5G /dev/TCPDumpVolGRP/TCPDumpLV
    WARNING: Reducing active logical volume to 1.50 GB
    THIS MAY DESTROY YOUR DATA (filesystem etc.)
    Do you really want to reduce TCPDumpLV? [y/n]: y
    Reducing logical volume TCPDumpLV to 1.50 GB
    Logical volume TCPDumpLV successfully resized
    [root@Linux01 /]#

    Note: Special precaution should be taken with this step.  It's possible to reduce the logical volume size by more than the size of the file system.  If you do reduce the LV size by more than what you resized the file system to (from step #4), this will almost certainly end very badly for you.  Ensure the LV is large enough for the file system and that you make a backup before hand!

  6. 校验LV的新大小

    [root@Linux01 ~]# lvdisplay /dev/TCPDumpVolGRP/TCPDumpLV

    --- Logical volume ---

    LV Name                       /dev/TCPDumpVolGRP/TCPDumpLV
    VG Name                      TCPDumpVolGRP
    LV UUID                        hYQs4t-YtY7-51hl-c4ps-4N6d-2W7h-IidcxF
    LV Write Access             read/write
    LV Status                     available
    # open                          0
    LV Size                         1.50 GB
    Current LE                     48
    Segments                      1
    Allocation                       inherit
    Read ahead sectors          auto
    - currently set to             256
    Block device                    253:5

     

  7. 重新挂载文件系统,并校验新大小

    [root@Linux01 /]# mount /dev/TCPDumpVolGRP/TCPDumpLV
    [root@Linux01 /]# cd /TCPDumpLV/

    [root@Linux01 TCPDumpLV]# df -kh .

    Filesystem                                               Size    Used      Avail      Use%      Mounted on
    /dev/mapper/TCPDumpVolGRP-TCPDumpLV    1.5G    920M     497M      65%       /TCPDumpLV