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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中的string类提供了shrink_to_fit()函数,可以使string的容量(capacity)随着size的缩小而缩小。\[2\]当我们使用resize()函数来改变string的大小时,如果size缩小后,字符超出的部分会被裁剪掉,但是capacity不会减小。而shrink_to_fit()函数可以在size缩小后,将capacity也相应地缩小,以节省内存空间。\[2\]这个函数在C++11中被引入,可以通过调用string对象的shrink_to_fit()方法来使用。 #### 引用[.reference_title] - *1* [C++ string清空并释放内存空间的两种方法(shrink_to_fit()、swap())](https://blog.csdn.net/qq_41687938/article/details/117731243)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [c++ string的详细用法(15)reserve()与resize()与shrink_to_fit()](https://blog.csdn.net/qq_40630246/article/details/103655230)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [【C++】如何释放vector的内存空间及std::vector::shrink_to_fit用法简介](https://blog.csdn.net/weixin_43753894/article/details/126970068)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值