• How to Shrink A LVM Physical Volume


    1. What is LVM?

    LVM is a Logical Volume Manager for the Linux operating system. It provides a higher-level of the disk storage than the traditional view of disks and partitions. This gives the system administrator much more flexibility in allocating storage to applications and users. Storage volumes created under the control of LVM can be resized and moved around almost at will, although this may need some upgrading of file system tools.

    There are two version of LVM for Linux:

    1) LVM 2 – The latest and greatest version of LVM for Linux

    LVM 2 is almost completely backward compatible with volumes created with LVM 1. The exception to this is snapshots;

    LVM 2 uses the device mapper kernel driver. Device mapper support is in the 2.6 kernel tree and there are patches available for current 2.4 kernels.

    2) LVM 1 – The version that is in the 2.4 series kernel

    LVM 1 is a mature product that has been considered stable for a couple of years. The kernel driver for LVM 1 is included in the 2.4 series kernels, but this does not mean that your 2.4.x kernel is up to date with the latest version of LVM.

    2. Anatomy of LVM

    Fig 1 shows what is in LVM.

    1) Our hard drives /dev/sda, /dev/sdb … may include several partitions name /dev/sda1, /dev/sda2, /dev/sdb1 …;

    2) Each physical partitions is a physical volume (PV) in LVM, PV is divided chunks of data, known as physical extents (PE);

    3) Each volume group (VG) is composed of several PVs in LVM physical side;

    4) Each volume group (VG) is composed of several logical volume (LV) in file system side, LV is split into chunks of data, known as logical extents (LE);

    5) Each LV is mounting as a file system such as /usr, /local.

    Fig 1 anatomy of LVM

    3. LVM Common Task

    1) Display

    Pvdisplay, vgdisplay, lvdisplay, pvs, vgs, lvs

    2) Initializing disks or disk partitions

    # pvcreate /dev/sda1

    3) Creating a volume group

    # vgcreate my_volume_group /dev/sda1 /dev/sda2

    4) Activating a volume group

    # vgchange –a y my_volume_group

    5) Removing a volume group

    # vgchange –a n my_volume_group

    # vgremove my_volume_group

    6) Adding physical volumes to a volume group

    # vgextend my_volume_group /dev/sda1

    7) Removing physical volumes from a volume group

    # vgreduce my_volume_group

    8) Creating a logical volume

    # lvcreate –L 1500 –ntestlv testvg

    9) Removing a logical volume

    # umount /dev/myvg/homevol

    # lvremove /dev/myvg/homevol

    10) Extending a logical volume

    # lvextend –L 12G /dev/myvg/homevol

    After you have extended logical volume it is necessary to increase the file system size to match, but default, most file system resizing tools will increase the size of the file system to be the size of the underlying logical volume, so you don’t need to worry about specifying the same size for each of the two commands.

    # umount /dev/myvg/homevol

    # resize2fs /dev/myvg/homevol

    # mount /dev/myvg/homevol /home

    11) Others

    # lvresize 60G /dev/vg/lv -> # resize2fs 60G /dev/vg/lv

    # pvmove /dev/sda1 /dev/sda2

    4. All LVM commands

    [PV]

    pvchange   pvcreate   pvmove     pvresize   pvscan 

    pvck       pvdisplay pvremove   pvs        pv.sh

    [VG]

    vgcfgbackup   vgconvert     vgextend      vgreduce      vgscan

    vgcfgrestore vgcreate      vgimport      vgremove      vgsplit

    vgchange      vgdisplay     vgmerge       vgrename  

    vgck          vgexport      vgmknodes     vgs

    [LV]

    lvdisplay    lvmdiskscan lvm.static   lvresize 

    lvchange     lvextend     lvmdump      lvreduce     lvs 

    lvconvert    lvm          lvmsadc      lvremove     lvscan

    lvcreate     lvmchange    lvmsar       lvrename  

    5. Step-by-step to shrink a LVM physical volume

    1) Boot into rescue mode with installation CD, skip mounting of the system partitions;

    2) Activating volume group

    # lvm vgchange –a y

    3) Fsck your single, large root file system on LogVol00

    #e2fsck –f /dev/VolGroup00/LogVol00

    4) Shrink your(ext3) file system inside the LogVol00 container BEFORE shrinking its container

    # resize2fs –p /dev/VolGroup00/LogVol00 50G

    5) Shrink the LogVol container to match

    # lvm lvresize /dev/VolGroup/LogVol00 –size 50G

    6) There is now enough free physical space available (lvm pvs) to shrink the physical volume by that much in order to make room for a new partition of that size or smaller. Unfortunately, the current implementation of “pvresize” is lacking – from the man page: “pvresize will refuse to shrink physical volume if it has allocated extents after where its new end would be. In the future, it should relocate these elsewhere in the volume group if there is sufficient free space, like pvmove does.”

    Because of this limitation, or we can say “this bug”, it’s necessary to temporarily remove your LoVol01 (swap LV), which is still physical allocated at the very end of your PV, then add it back such that its newly located immediately after the smaller LogVol00 nearer to the start of the PV (and partition).

    Assuming your swap is “2G”, for example, do:

    # lvm pvdisplay -m

    # lvm lvremove /dev/VolGroup00/LogVol01

    # lvm lvcreate –size 2G –name LogVol01 VolGroup00

    # mkswap /dev/VolGroup00/LogVol01

    7) Shrink the physical volume

    First determine how much you can shrink it, by running # lvm pvs to see “PFree”, then

    # lvm pvresize /dev/sda1 –setphysicalvolumesize 50G

    If this step fails, it's probably because you're trying to shrink too small, and/or there are still allocated extents -- other than swap, which you moved in the step above -- after where the new tail end would be. A future version of pvresize will automatically move those to earlier free space as needed. (As an alternative, if you have lots of free space on another drive, you could pvcreate there, then pvmove temporarily, then pvresize, and pvmove back.)

    8) Finally – Shrink the partition containing the shrunk PV to match its new size.

    Display the PV sizes and take note of the smaller PSize:

    # Lvm pvs for example it’s 65.00G

    Display the partition and take note of the start sector and partition number of the partition with “lvm” in the “Flags” column:

    #parted /dev/sda print

    For example, it prints

    Minor Start End Type Filesystem Flags

    1 0.031 101.975 primary ext3 boot

    2 101.975 76785.127 primary lvm

    So we have free space 76.785127-65.00=11G(we need a safety margin of >=64MB)

    9) Recreate our partitions

    WARNING: Point of no return – have backups of your partitions point such as 101.975

    So now we need to nuke the old, large partition from the partition table, and immediately recreate it with the exact same start sector, but with a newer, shorter end sector.

    Nuke it # parted /dev/sda rm 2 (remove sda2)

    Recreate it smaller + safety margin.

    # parted /dev/sda mkpart primary 101.975 65785.127

    # parted /dev/sda set 2 lvm on

    # parted /dev/sda print

    #fdisk –l /dev/sda

    10) Optional step

    You can use pvresize, lvresize and resize2fs onece more if you would not like to waste some space produced by above steps.

    11) Verify you didn’t make a major oops truncating something by running fsck again

    # fsck –fC /dev/VolGroup00/LogVol00

    12) All done. You now have some unallocated space ourside of LVM to play with, enjoy! J

  • 相关阅读:
    BootStrap Table前台和后台分页对JSON格式的要求
    神奇的外部嵌套(使用ROW_NUMBER()查询带条件的时候提示列名无效)
    要想获取select的值,使用ng-modle,否则无法获取select 的值
    Angular使用操作事件指令ng-click传多个参数示例
    Jenins 邮件通知
    Jenkins 流水线(Pipeline)
    Jenkins Master-Slave 架构
    Jenins 参数化构建
    Jenkins 用户权限管理
    Jenkins 安装
  • 原文地址:https://www.cnblogs.com/jjkv3/p/3117063.html
Copyright © 2020-2023  润新知