5

记录一次磁盘镜像的LVM分区缩小调整过程

 3 years ago
source link: https://blog.ihipop.info/2019/07/5212.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

记录一次磁盘镜像的LVM分区缩小调整过程

拿到镜像是这样的。

$ fdisk -l OMP120.img
Disk OMP120.img: 29.8 GiB, 31937527808 bytes, 62377984 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x000d89e6
Device      Boot   Start      End  Sectors  Size Id Type
OMP120.img1 *       2048  1026047  1024000  500M 83 Linux
OMP120.img2      1026048 62377983 61351936 29.3G 8e Linux LVM

losetup伺候,其中,LVM内是如下情况

# pvs -v --segments
    Wiping internal VG cache
    Wiping cache of LVM-capable devices
  PV           VG        Fmt  Attr PSize  PFree Start SSize LV      Start Type   PE Ranges            
  /dev/loop0p2 vg_wellav lvm2 a--  29.25g    0      0  6728 lv_root     0 linear /dev/loop0p2:0-6727  
  /dev/loop0p2 vg_wellav lvm2 a--  29.25g    0   6728   761 lv_swap     0 linear /dev/loop0p2:6728-7488

但是这个pv卷轴已经没有空余空间,因为是离线镜像 所以不能使得分区属性有任何变化,包括uuid等,否则可能会导致机器出问题,而且root分区空余很多,考虑ext4有e2fsck,但是swap没有,而且root空间空余较多,考虑直接缩减root分区

先检查下文件系统

# e2fsck -fy /dev/vg_wellav/lv_root
e2fsck 1.43.4 (31-Jan-2017)
/dev/vg_wellav/lv_root:正在修复日志
应为孤立的inode 926115(uid=%lu,gid=%lg,mode=%lm,size=%ls)
应为孤立的inode 926114(uid=%lu,gid=%lg,mode=%lm,size=%ls)
应为孤立的inode 926113(uid=%lu,gid=%lg,mode=%lm,size=%ls)
应为孤立的inode 924330(uid=%lu,gid=%lg,mode=%lm,size=%ls)
应为孤立的inode 920981(uid=%lu,gid=%lg,mode=%lm,size=%ls)
第 1 步:检查inode、块和大小
第 2 步:检查目录结构
第 3 步:检查目录连接性
第 4 步:检查引用计数
第 5 步:检查组概要信息
可用块数错误(0,实际为0)
处理? yes
可用inode数错误(1575548,实际为1575524)
处理? yes
/dev/vg_wellav/lv_root:***** 文件系统已修改 *****
/dev/vg_wellav/lv_root:149612/1725136 文件(0.1% 为非连续的), 1315641/6889472 块

然后把根文件系统缩小为24G,为lvreduce留足安全余量

# resize2fs /dev/vg_wellav/lv_root 24G
resize2fs 1.43.4 (31-Jan-2017)
将 /dev/vg_wellav/lv_root 上的文件系统调整为 6291456 个块(每块 4k)。
/dev/vg_wellav/lv_root 上的文件系统现在为 6291456 个块(每块 4k)。

这里,6291456*4*/1024/1024=24G

然后,使用lvreduce缩小root分区为25G

# lvreduce -L 25G /dev/vg_wellav/lv_root
  WARNING: Reducing active logical volume to 25.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg_wellav/lv_root? [y/n]: y
  Size of logical volume vg_wellav/lv_root changed from 26.28 GiB (6728 extents) to 25.00 GiB (6400 extents).
  Logical volume vg_wellav/lv_root successfully resized.

然后,使用resize2fs扩大root分区为正好合适这个lv的尺寸

# resize2fs /dev/vg_wellav/lv_root
resize2fs 1.43.4 (31-Jan-2017)
将 /dev/vg_wellav/lv_root 上的文件系统调整为 6553600 个块(每块 4k)。
/dev/vg_wellav/lv_root 上的文件系统现在为 6553600 个块(每块 4k)。
# e2fsck  -fy /dev/vg_wellav/lv_root
e2fsck 1.43.4 (31-Jan-2017)
第 1 步:检查inode、块和大小
第 2 步:检查目录结构
第 3 步:检查目录连接性
第 4 步:检查引用计数
第 5 步:检查组概要信息
/dev/vg_wellav/lv_root:149612/1635200 文件(0.2% 为非连续的), 1310004/6553600 块

可以看到已经扩大到了正好合适的25G

6553600*4/1204/1024=25G

这时候看,我们的pv已经多出来空余的部分了 但是麻烦的地方在于,那个空闲的部分在中间

# pvs -v --segments
    Wiping internal VG cache
    Wiping cache of LVM-capable devices
  PV           VG        Fmt  Attr PSize  PFree Start SSize LV      Start Type   PE Ranges            
  /dev/loop0p2 vg_wellav lvm2 a--  29.25g 1.28g     0  6400 lv_root     0 linear /dev/loop0p2:0-6399  
  /dev/loop0p2 vg_wellav lvm2 a--  29.25g 1.28g  6400   328             0 free                        
  /dev/loop0p2 vg_wellav lvm2 a--  29.25g 1.28g  6728   761 lv_swap     0 linear /dev/loop0p2:6728-7488

尝试重新安排空间 ,报错,因为没足够空余空间进行空调调整。

# pvmove --alloc anywhere /dev/loop0p2:6728-7488
  Insufficient free space: 761 extents needed, but only 328 available
  Unable to allocate mirror extents for vg_wellav/pvmove0.
  Failed to convert pvmove LV to mirrored

所以我们需要给这个pv里面再添加一些空余的物理磁盘

# dd if=/dev/zero of=/tmp/zero.img bs=1G count=4
# fdisk /tmp/zero.img
Welcome to fdisk (util-linux 2.29.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xecbc9b42.
Command (m for help): p
Disk /tmp/zero.img: 4 GiB, 4294967296 bytes, 8388608 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xecbc9b42
Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-8388607, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-8388607, default 8388607):
Created a new partition 1 of type 'Linux' and of size 4 GiB.
Command (m for help): t
Selected partition 1
Partition type (type L to list all types): 8e
Changed type of partition 'Linux' to 'Linux LVM'.
Command (m for help): w
The partition table has been altered.
Syncing disks.
# pvcreate /tmp/zero.img
  Device /tmp/zero.img not found (or ignored by filtering).
# losetup -fP --show /tmp/zero.img
/dev/loop1
# pvcreate /dev/loop1p1
  Physical volume "/dev/loop1p1" successfully created.
  VG        #PV #LV #SN Attr   VSize  VFree
  vg_wellav   1   2   0 wz--n- 29.25g 1.28g
# vgextend vg_wellav /dev/loop1p1
  Volume group "vg_wellav" successfully extended
# pvs -v --segments
    Wiping internal VG cache
    Wiping cache of LVM-capable devices
  PV           VG        Fmt  Attr PSize  PFree Start SSize LV      Start Type   PE Ranges            
  /dev/loop0p2 vg_wellav lvm2 a--  29.25g 1.28g     0  6400 lv_root     0 linear /dev/loop0p2:0-6399  
  /dev/loop0p2 vg_wellav lvm2 a--  29.25g 1.28g  6400   328             0 free                        
  /dev/loop0p2 vg_wellav lvm2 a--  29.25g 1.28g  6728   761 lv_swap     0 linear /dev/loop0p2:6728-7488
  /dev/loop1p1 vg_wellav lvm2 a--   4.00g 4.00g     0  1023             0 free  

然后调用pvmove进行空间整理,注意这里如果不加目的地址 他会先全部移动到/dev/loop1p1上 你需要移动两次,其实也无所谓,他自己会处理

# pvmove --alloc anywhere /dev/loop0p2:6728-7488
  /dev/loop0p2: Moved: 0.13%
  /dev/loop0p2: Moved: 26.02%
  /dev/loop0p2: Moved: 50.07%
  /dev/loop0p2: Moved: 70.57%
  /dev/loop0p2: Moved: 91.20%
  /dev/loop0p2: Moved: 100.00%
# pvs -v --segments
    Wiping internal VG cache
    Wiping cache of LVM-capable devices
  PV           VG        Fmt  Attr PSize  PFree Start SSize LV      Start Type   PE Ranges          
  /dev/loop0p2 vg_wellav lvm2 a--  29.25g 4.25g     0  6400 lv_root     0 linear /dev/loop0p2:0-6399
  /dev/loop0p2 vg_wellav lvm2 a--  29.25g 4.25g  6400  1089             0 free                      
  /dev/loop1p1 vg_wellav lvm2 a--   4.00g 1.02g     0   761 lv_swap     0 linear /dev/loop1p1:0-760
  /dev/loop1p1 vg_wellav lvm2 a--   4.00g 1.02g   761   262             0 free  
# pvmove --alloc anywhere /dev/loop1p1:0-760
  /dev/loop1p1: Moved: 1.05%
  /dev/loop1p1: Moved: 100.00%
# pvs -v --segments
    Wiping internal VG cache
    Wiping cache of LVM-capable devices
  PV           VG        Fmt  Attr PSize  PFree Start SSize LV      Start Type   PE Ranges            
  /dev/loop0p2 vg_wellav lvm2 a--  29.25g 1.28g     0  6400 lv_root     0 linear /dev/loop0p2:0-6399  
  /dev/loop0p2 vg_wellav lvm2 a--  29.25g 1.28g  6400   761 lv_swap     0 linear /dev/loop0p2:6400-7160
  /dev/loop0p2 vg_wellav lvm2 a--  29.25g 1.28g  7161   328             0 free                        
  /dev/loop1p1 vg_wellav lvm2 a--   4.00g 4.00g     0  1023             0 free

然后安全的移除新添加的零时盘

# vgreduce vg_wellav  /dev/loop1p1
  Removed "/dev/loop1p1" from volume group "vg_wellav"
# pvs -v --segments
    Wiping internal VG cache
    Wiping cache of LVM-capable devices
  PV           VG        Fmt  Attr PSize  PFree Start SSize LV      Start Type   PE Ranges            
  /dev/loop0p2 vg_wellav lvm2 a--  29.25g 1.28g     0  6400 lv_root     0 linear /dev/loop0p2:0-6399  
  /dev/loop0p2 vg_wellav lvm2 a--  29.25g 1.28g  6400   761 lv_swap     0 linear /dev/loop0p2:6400-7160
  /dev/loop0p2 vg_wellav lvm2 a--  29.25g 1.28g  7161   328             0 free

然后用gparted把多余的vg空间的尾巴切掉就好了,然后,参考这个文章,裁剪img镜像

最后用losetup检查检查没问题即可收工。

Author Info : From:记录一次磁盘镜像的LVM分区缩小调整过程 URL:https://blog.ihipop.info/2019/07/5212.html Please Reserve This Link,Thanks!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK