逻辑卷管理器-缩减

 
0、背景:两块物理硬盘组成逻辑卷,由于硬盘故障或硬盘迁移等原因,需从卷组中移除硬盘,要先针对逻辑卷(testlv)进行缩减。
 
[root@localhost testdir]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
devtmpfs                   475M     0  475M   0% /dev
tmpfs                      487M     0  487M   0% /dev/shm
tmpfs                      487M  7.7M  479M   2% /run
tmpfs                      487M     0  487M   0% /sys/fs/cgroup
/dev/mapper/centos-root     25G  1.9G   24G   8% /
/dev/sda1                  497M  130M  367M  27% /boot
/dev/mapper/bsoftvg-data    21G   33M   21G   1% /data
tmpfs                       98M     0   98M   0% /run/user/0
/dev/mapper/testvg-testlv   40G   17G   24G  43% /testdir
[root@localhost testdir]# blkid
/dev/sda1: UUID="dbe36a10-9cce-47c8-a5f1-a6a5245665e5" TYPE="xfs"
/dev/sda2: UUID="G4T07B-KCn3-oGuc-diPV-nQks-MyfT-0E8Rfp" TYPE="LVM2_member"
/dev/sda3: UUID="kL2FKX-f0QQ-iKnX-GKsW-U9Pg-7z3e-ExoWqK" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="e493b776-83fe-43d6-aaed-2c4ccc6af3fc" TYPE="xfs"
/dev/mapper/centos-swap: UUID="2153499d-e627-4380-9beb-14a8c0abf6c6" TYPE="swap"
/dev/mapper/bsoftvg-data: UUID="34a18630-78ea-4deb-88b7-9bf600a8cc54" TYPE="xfs"
/dev/sdb: UUID="IjBHLs-oojg-y4v3-E50L-vgUO-lSBb-eed6zr" TYPE="LVM2_member"
/dev/mapper/testvg-testlv: UUID="57f1b978-585b-40a7-a08f-0a7b43863add" TYPE="xfs"
/dev/sdc: UUID="sOhp6w-fchC-ZMRt-s188-esE0-tPc4-i37gtb" TYPE="LVM2_member"

  

 

 1、取消挂载

umount /testdir/
遇到取消挂载不成功的情况,退出bash,重新登录。
[root@localhost testdir]# umount /testdir/
umount: /testdir: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
[root@localhost testdir]#
[root@localhost testdir]#
[root@localhost testdir]#
[root@localhost testdir]# lsof | grep /test
bash      1321         root  cwd       DIR              253,3       198         64 /testdir
lsof      1569         root  cwd       DIR              253,3       198         64 /testdir
grep      1570         root  cwd       DIR              253,3       198         64 /testdir
lsof      1571         root  cwd       DIR              253,3       198         64 /testdir
[root@localhost testdir]# exit
logout

  

[root@localhost ~]# umount  /testdir/
[root@localhost ~]#
[root@localhost ~]# df -h
Filesystem                Size  Used Avail Use% Mounted on
devtmpfs                  475M     0  475M   0% /dev
tmpfs                     487M     0  487M   0% /dev/shm
tmpfs                     487M  7.7M  479M   2% /run
tmpfs                     487M     0  487M   0% /sys/fs/cgroup
/dev/mapper/centos-root    25G  1.9G   24G   8% /
/dev/sda1                 497M  130M  367M  27% /boot
/dev/mapper/bsoftvg-data   21G   33M   21G   1% /data
tmpfs                      98M     0   98M   0% /run/user/0

  

2、缩减逻辑卷到18G

lvreduce -L 18G /dev/testvg/testlv

[root@localhost ~]# lvreduce -L 18G /dev/testvg/testlv
  WARNING: Reducing active logical volume to 18.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce testvg/testlv? [y/n]: y
  Size of logical volume testvg/testlv changed from 39.99 GiB (10238 extents) to 18.00 GiB (4608 extents).
  Logical volume testvg/testlv successfully resized. 
提示:缩减逻辑卷会破坏数据、文件系统等,需要提前进行数据备份。
缩减lv后,pv、gv均多了空闲空间
[root@localhost ~]# pvs
  PV         VG      Fmt  Attr PSize   PFree
  /dev/sda2  centos  lvm2 a--   29.00g   4.00m
  /dev/sda3  bsoftvg lvm2 a--   20.50g      0
  /dev/sdb   testvg  lvm2 a--  <20.00g  <2.00g
  /dev/sdc   testvg  lvm2 a--  <20.00g <20.00g
[root@localhost ~]# vgs
  VG      #PV #LV #SN Attr   VSize  VFree
  bsoftvg   1   1   0 wz--n- 20.50g     0
  centos    1   2   0 wz--n- 29.00g  4.00m
  testvg    2   1   0 wz--n- 39.99g 21.99g
[root@localhost ~]# lvs
  LV     VG      Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  data   bsoftvg -wi-ao---- 20.50g
  root   centos  -wi-ao---- 25.00g
  swap   centos  -wi-ao----  4.00g
  testlv testvg  -wi-a----- 18.00g

  

3、取消激活要移除的硬盘

pvchange -xn /dev/sdc

[root@localhost ~]# pvchange -xn /dev/sdc
  Physical volume "/dev/sdc" changed
  1 physical volume changed / 0 physical volumes not changed
[root@localhost ~]#pvdisplay

  

 

4、从vg中移除硬盘
vgreduce testvg /dev/sdc
[root@localhost ~]# vgreduce testvg /dev/sdc
  Removed "/dev/sdc" from volume group "testvg"
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# vgs
  VG      #PV #LV #SN Attr   VSize   VFree
  bsoftvg   1   1   0 wz--n-  20.50g     0
  centos    1   2   0 wz--n-  29.00g  4.00m
  testvg    1   1   0 wz--n- <20.00g <2.00g
[root@localhost ~]#
[root@localhost ~]# pvs
  PV         VG      Fmt  Attr PSize   PFree
  /dev/sda2  centos  lvm2 a--   29.00g  4.00m
  /dev/sda3  bsoftvg lvm2 a--   20.50g     0
  /dev/sdb   testvg  lvm2 a--  <20.00g <2.00g
  /dev/sdc           lvm2 ---   20.00g 20.00g

  

5、移除物理卷,此时可以拔出硬盘

pvremove /dev/sdc
[root@localhost ~]# pvremove /dev/sdc
  Labels on physical volume "/dev/sdc" successfully wiped.
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# pvs
  PV         VG      Fmt  Attr PSize   PFree
  /dev/sda2  centos  lvm2 a--   29.00g  4.00m
  /dev/sda3  bsoftvg lvm2 a--   20.50g     0
  /dev/sdb   testvg  lvm2 a--  <20.00g <2.00g

  

6、对缩减的卷组进行格式化

mkfs.xfs /dev/testvg/testlv
[root@localhost ~]# mkfs.xfs  -f /dev/testvg/testlv
meta-data=/dev/testvg/testlv     isize=512    agcount=4, agsize=1179648 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=4718592, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

  

7、格式化lv会生成新的UUID,需要更新fstab文件,并重新加载:mount -a

[root@localhost ~]# blkid
/dev/sda1: UUID="dbe36a10-9cce-47c8-a5f1-a6a5245665e5" TYPE="xfs"
/dev/sda2: UUID="G4T07B-KCn3-oGuc-diPV-nQks-MyfT-0E8Rfp" TYPE="LVM2_member"
/dev/sda3: UUID="kL2FKX-f0QQ-iKnX-GKsW-U9Pg-7z3e-ExoWqK" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="e493b776-83fe-43d6-aaed-2c4ccc6af3fc" TYPE="xfs"
/dev/mapper/centos-swap: UUID="2153499d-e627-4380-9beb-14a8c0abf6c6" TYPE="swap"
/dev/mapper/bsoftvg-data: UUID="34a18630-78ea-4deb-88b7-9bf600a8cc54" TYPE="xfs"
/dev/sdb: UUID="IjBHLs-oojg-y4v3-E50L-vgUO-lSBb-eed6zr" TYPE="LVM2_member"
/dev/mapper/testvg-testlv: UUID="f4546708-5e38-45a7-8894-d8f6057182dc" TYPE="xfs"
/dev/sdc: UUID="sOhp6w-fchC-ZMRt-s188-esE0-tPc4-i37gtb" TYPE="LVM2_member"

  vim /etc/fstab

# /etc/fstab
# Created by anaconda on Mon Feb 21 10:59:48 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=dbe36a10-9cce-47c8-a5f1-a6a5245665e5 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
UUID=34a18630-78ea-4deb-88b7-9bf600a8cc54 /data xfs     defaults        0 0
UUID=f4546708-5e38-45a7-8894-d8f6057182dc /testdir xfs  defaults        0 0

 

[root@localhost ~]# mount -a
[root@localhost ~]#
[root@localhost ~]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
devtmpfs                   475M     0  475M   0% /dev
tmpfs                      487M     0  487M   0% /dev/shm
tmpfs                      487M  7.7M  479M   2% /run
tmpfs                      487M     0  487M   0% /sys/fs/cgroup
/dev/mapper/centos-root     25G  1.9G   24G   8% /
/dev/sda1                  497M  130M  367M  27% /boot
/dev/mapper/bsoftvg-data    21G   33M   21G   1% /data
/dev/mapper/testvg-testlv   18G   33M   18G   1% /testdir
tmpfs                       98M     0   98M   0% /run/user/0

  

 

posted @ 2022-03-23 00:38  idongqing  阅读(130)  评论(0编辑  收藏  举报