LVM:通过从已删除的逻辑卷中释放可用空间来减少/缩小物理卷的大小

LVM:通过从已删除的逻辑卷中释放可用空间来减少/缩小物理卷的大小

我有以下 LVM 分区:

# df -h

  Filesystem                Size  Used Avail Use% Mounted on
  /dev/mapper/data-root     5.3G  4.2G  864M  84% /
  ...
  /dev/mapper/data-install  6.8G  5.7G  783M  89% /install

# vgs
  VG   #PV #LV #SN Attr   VSize   VFree
  data   1   2   0 wz--n- <12.50g    0

# vgdisplay
  --- Volume group ---
  VG Name               data
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <12.50 GiB
  PE Size               4.00 MiB
  Total PE              3199
  Alloc PE / Size       3199 / <12.50 GiB
  Free  PE / Size       0 / 0
  VG UUID               jSACsC-wGwy-ki0w-nSCn-kxMy-bLOV-dVN68E


# pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sda2  data lvm2 a--  <12.50g    0

# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               data
  PV Size               <12.50 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              3199
  Free PE               0
  Allocated PE          3199
  PV UUID               CPggVR-sUXw-4gfw-lb2v-cq0j-M5ey-Ct7td3

# lvs
  LV      VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  install data -wi-ao----  7.00g
  root    data -wi-ao---- <5.50g


# lvdisplay
  --- Logical volume ---
  LV Path                /dev/data/install
  LV Name                install
  VG Name                data
  LV UUID                ZXQmp5-L17b-eQyS-g3An-msk7-Jqso-MTaTIw
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-09-28 14:42:43 +0000
  LV Status              available
  open                 1
  LV Size                7.00 GiB
  Current LE             1792
  Segments               1
  Allocation             inherit
  Read ahead sectors     aut
  - currently set to     8192
  Block device           253:1



  --- Logical volume ---
  LV Path                /dev/data/root
  LV Name                root
  VG Name                data
  LV UUID                i3eMbC-rcm1-H2Gm-JJeF-jRxO-nL9h-toHSHQ
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-09-28 14:42:55 +0000
  LV Status              available
  # open                 1
  LV Size                <5.50 GiB
  Current LE             1407
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

我正在删除逻辑卷之一:

# umount /install
# lvremove /dev/data/install
# sed -i 'N;/\/dev\/mapper\/data-install/{s#/dev/mapper/data-install.*\n##}' /etc/fstab

移除后:

# df -h
  Filesystem             Size  Used Avail Use% Mounted on
  /dev/mapper/data-root  5.3G  4.2G  867M  84% /
  ...


# vgs
  VG   #PV #LV #SN Attr   VSize   VFre
  data   1   1   0 wz--n- <12.50g 7.00g


# vgdisplay
  --- Volume group ---
  VG Name               data
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <12.50 GiB
  PE Size               4.00 MiB
  Total PE              3199
  Alloc PE / Size       1407 / <5.50 GiB
  Free  PE / Size       1792 / 7.00 GiB
  VG UUID               jSACsC-wGwy-ki0w-nSCn-kxMy-bLOV-dVN68E


# pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sda2  data lvm2 a--  <12.50g 7.00g


# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               data
  PV Size               <12.50 GiB / not usable 3.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              3199
  Free PE               1792
  Allocated PE          1407
  PV UUID               CPggVR-sUXw-4gfw-lb2v-cq0j-M5ey-Ct7td3


# lvs
  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root data -wi-ao---- <5.50g


# lvdisplay
  --- Logical volume ---
  LV Path                /dev/data/root
  LV Name                root
  VG Name                data
  LV UUID                i3eMbC-rcm1-H2Gm-JJeF-jRxO-nL9h-toHSHQ
  LV Write Access        read/write
  LV Creation host, time localhost, 2018-09-28 14:42:55 +0000
  LV Status              available
  # open                 1
  LV Size                <5.50 GiB
  Current LE             1407
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

如何将物理卷、卷组以及最后的分区一起缩小到删除逻辑卷后的可用空间大小?最好应该指定相对值(到pvresize?)或类似的东西lvextend -l 100%FREE <lvolume>。我还想单独使用命令行(没有 GUI)来完成此任务。还值得注意的是,根/系统逻辑卷始终处于活动状态并在使用中。

答案1

嗯,pvresize没有像lvextend那样的相对值选项,所以你需要指定你想要的确切大小。您可以执行以下操作:

  1. 缩小物理体积:pvresize --setphysicalvolumesize 5.5G /dev/sda2
  2. 使用您选择的工具缩小分区。如果你想编写脚本,可以使用parted。将分区设置为比 5.5G 稍大一点,以考虑计算字节大小的差异,或者确保在两个工具中使用相同的单位。
  3. 如果工具之间的单位不同,请扩展物理卷以占用整个分区:pvresize /dev/sda2

相关内容