CentOS lvm虚拟磁盘管理(vg_centos-lv_root扩充和缩小)

查看磁盘使用情况

[root@centos6 ~]# df -h
Filesystem                     Size  Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root  148G   49G   92G  35% /
tmpfs                          3.9G     0  3.9G   0% /dev/shm
/dev/sda1                      477M   88M  360M  20% /boot
/dev/mapper/vg_centos-lv_home   69G   11G   55G  16% /home

查看分区情况

[root@centos6 ~]# fdisk -l

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0xcad4ebea

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64       60802   487873536   8e  Linux LVM

Disk /dev/mapper/vg_centos-lv_root: 161.1 GB, 161061273600 bytes
255 heads, 63 sectors/track, 19581 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/vg_centos-lv_swap: 6257 MB, 6257901568 bytes
255 heads, 63 sectors/track, 760 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/vg_centos-lv_home: 75.2 GB, 75161927680 bytes
255 heads, 63 sectors/track, 9137 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x00000000

查看已经存在的PV

[root@centos6 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               vg_centos
  PV Size               465.27 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              119109
  Free PE               61297
  Allocated PE          57812
  PV UUID               MBViEk-fqNP-FUlc-PB3X-gOfi-x0pI-z7Bzbc

查看VG

[root@centos6 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  8
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               465.27 GiB
  PE Size               4.00 MiB
  Total PE              119109
  Alloc PE / Size       57812 / 225.83 GiB
  Free  PE / Size       61297 / 239.44 GiB
  VG UUID               Xbfben-cWfV-XN6s-3SqM-SQOJ-tXPY-nCCFqC

查看LV

[root@centos6 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg_centos/lv_root
  LV Name                lv_root
  VG Name                vg_centos
  LV UUID                WmZIWz-aiAY-CUPs-bYnH-4iyy-yKeG-b5onvL
  LV Write Access        read/write
  LV Creation host, time centos, 2019-02-24 04:08:00 +0800
  LV Status              available
  # open                 1
  LV Size                150.00 GiB
  Current LE             38400
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/vg_centos/lv_home
  LV Name                lv_home
  VG Name                vg_centos
  LV UUID                sA7I2d-44Fm-HUBi-BAbK-F1ST-fSTW-mmBzx0
  LV Write Access        read/write
  LV Creation host, time centos, 2019-02-24 04:08:18 +0800
  LV Status              available
  # open                 1
  LV Size                70.00 GiB
  Current LE             17920
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/vg_centos/lv_swap
  LV Name                lv_swap
  VG Name                vg_centos
  LV UUID                OBXMGc-pGZK-b66R-i072-Jnfi-t07d-NHMiW0
  LV Write Access        read/write
  LV Creation host, time centos, 2019-02-24 04:12:30 +0800
  LV Status              available
  # open                 2
  LV Size                5.83 GiB
  Current LE             1492
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

缩小LV(非root分区)

lvreduce -L 50G -f -r /dev/vg/lv   # 减少到50G
lvreduce -L -10G -f -r /dev/vg/lv  # 减10G

#相当于以下3条命令
e2fsck -f /dev/testvg/testlv
resize2fs /dev/testvg/testlv 50G #仅限ext4
lvreduce -L 50G /dev/testvg/testlv

 

xfs缩容,只能重新创建lv逻辑分区来实现缩容,可以参考文章后面xfs的链接

 

拓展LV

lvextend -L +10G -f -r /dev/testvg/testlv  #增加10G
lvextend -L 100G -f -r /dev/testvg/testlv   #空间扩大到100G

# 相当于
lvextend -L +10G /dev/testvg/testlv
resize2fs -f /dev/testvg/testlv 30G   #仅限ext4

 

# 或者用
lvresize -L 10G /dev/testvg/testlv

  

root分区拓展和缩小操作,不能在本机一次性完成,操作后不能进行resize

我们可以进入救援模式或者将硬盘拆下来到其他的centos或者archlinux系统下resize

 

e2fsck -f /dev/mapper/vg_host01-lv_root
resize2fs -p /dev/mapper/vg_host01-lv_root 50G #仅限ext4

 

查看空闲的空间

[root@centos6 ~]# vgdisplay|grep "Free  PE"
  Free  PE / Size       61297 / 239.44 GiB

 

PV拓展和缩小

# 在resize 之前需要调整lv的空间,把lvresize缩小的空间移动到最后
pvs -v --segments /dev/sdb1
pvmove --alloc anywhere /dev/sdb1:1000-1999  /dev/sdb10-999   #https://blog.csdn.net/allway2/article/details/102660278
#重设物理卷大小 https://www.jianshu.com/p/9fdafdb198a9
pvresize --setphysicalvolumesize 20G /dev/sdb1

  

  

更多lvm命令参考 https://www.systutorials.com/docs/linux/man/8-lvm/

XFS文件系统

参考 https://www.cnblogs.com/Alwayslearn/p/16799747.html 

 

虚拟磁盘

扩大缩小qcow2虚拟机镜像大小 

[root@localhost p2v-pool]# virt-df -h centos7-sda 
Filesystem                                Size       Used  Available  Use%
centos7-sda:/dev/sda1                     192M        11M       181M    6%
centos7-sda:/dev/sda2                     1.0G       134M       912M   13%
centos7-sda:/dev/centos00/home             15G       240K        15G    1%
centos7-sda:/dev/centos00/root             15G       1.2G        14G    9%
[root@localhost p2v-pool]# qemu-img create -f qcow2 centos7.qcow2 37G
Formatting 'centos7.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=39728447488 lazy_refcounts=off refcount_bits=16
[root@localhost p2v-pool]# virt-resize --expand /dev/sda3 centos7-sda centos7.qcow2 
[   0.0] Examining centos7-sda
virt-resize: error: You cannot use --expand when there is no surplus space 
to expand into.  You need to make the target disk larger by at least                                                                                                                                                 
624.3M.                                                                                                                                                                                                              
                                                                                                                                                                                                                     
If reporting bugs, run virt-resize with debugging enabled and include the                                                                                                                                            
complete output:                                                                                                                                                                                                     
                                                                                                                                                                                                                     
  virt-resize -v -x [...]                                                                                                                                                                                            
[root@localhost p2v-pool]# rm -f centos7.qcow2 
[root@localhost p2v-pool]# qemu-img create -f qcow2 centos7.qcow2 38G
Formatting 'centos7.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=40802189312 lazy_refcounts=off refcount_bits=16
[root@localhost p2v-pool]# virt-resize --expand /dev/sda3 centos7-sda centos7.qcow2 
[   0.0] Examining centos7-sda
**********

Summary of changes:

/dev/sda1: This partition will be left alone.

/dev/sda2: This partition will be left alone.

/dev/sda3: This partition will be resized from 36.4G to 36.7G.  The LVM PV 
on /dev/sda3 will be expanded using the ‘pvresize’ method.

**********
[   3.7] Setting up initial partition table on centos7.qcow2
[  14.9] Copying /dev/sda1
[  15.2] Copying /dev/sda2
[  16.7] Copying /dev/sda3
 100% [######################################################################################################################################################################################################] 00:00
[ 102.3] Expanding /dev/sda3 using the ‘pvresize’ method

Resize operation completed with no errors.  Before deleting the old disk, 
carefully check that the resized disk boots and works correctly.

  

https://blog.csdn.net/tutucute0000/article/details/38414449

https://zhuanlan.zhihu.com/p/158123147

https://www.cnblogs.com/wang_yb/p/3980599.html

posted @ 2022-03-26 20:21  风吹过的绿洲  阅读(665)  评论(0编辑  收藏  举报