linux lvm 安全 删除 硬盘或分区

张映 发表于 2015-12-31

分类目录: linux

标签:, , , , ,

lvm的逻辑卷,可能由一个或者多个硬盘或者分区组成,如何才能安全的删除逻辑卷中的硬盘或者分区呢。适用场景,我有二块小硬盘,我想把其中一块小的硬盘换成大的。首先就要把逻辑卷中的小硬盘,安全的删除掉。下面是操作的详细过程。

1,查看分区情况

[root@iZ94zz3wqciZ data]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda1             20G   15G  4.6G  76% /
tmpfs                 498M     0  498M   0% /dev/shm
/dev/mapper/myfiles-fastdfs
                       27G  9.9G   16G  39% /mnt/fastdfs

这里的27G,是由一块22G的硬盘和一块5G的硬盘组成的

2,取消挂载,进行硬盘检测

[root@iZ94zz3wqciZ ~]# umount /mnt/fastdfs/    //取消挂

[root@iZ94zz3wqciZ ~]# e2fsck -f /dev/mapper/myfiles-fastdfs   //磁盘检测
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/myfiles-fastdfs: 103722/1769472 files (0.1% non-contiguous), 2686136/7072768 blocks

3,对逻辑卷重定义大小

[root@iZ94zz3wqciZ ~]# resize2fs -p /dev/mapper/myfiles-fastdfs  21G     //重定义大小
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/myfiles-fastdfs to 5505024 (4k) blocks.
Begin pass 2 (max = 529848)
Relocating blocks             XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 216)
Scanning inode table          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 4 (max = 66693)
Updating inode references     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/mapper/myfiles-fastdfs is now 5505024 blocks long.

[root@iZ94zz3wqciZ ~]# lvreduce -L 21G /dev/mapper/myfiles-fastdfs     //重定义大小
  WARNING: Reducing active logical volume to 21.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce fastdfs? [y/n]: y
  Size of logical volume myfiles/fastdfs changed from 26.98 GiB (6907 extents) to 21.00 GiB (5376 extents).
  Logical volume fastdfs successfully resized

由27G变成21G,大于要删除的5G盘,就算5G的盘子数据满了,也可以全部移走

4,查看物理卷情况

[root@iZ94zz3wqciZ ~]# pvdisplay -m
  --- Physical volume ---
  PV Name               /dev/xvdb1
  VG Name               myfiles
  PV Size               21.99 GiB / not usable 4.78 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              5629
  Free PE               253
  Allocated PE          5376
  PV UUID               N2kxpi-FjET-rQ8p-MaDF-z3tH-fK3L-oW1STl

  --- Physical Segments ---
  Physical extent 0 to 5375:
    Logical volume      /dev/myfiles/fastdfs
    Logical extents     0 to 5375
  Physical extent 5376 to 5628:
    FREE

  --- Physical volume ---
  PV Name               /dev/xvdc1
  VG Name               myfiles
  PV Size               4.99 GiB / not usable 2.41 MiB
  Allocatable           yes      //激活状态,可写入
  PE Size               4.00 MiB
  Total PE              1278     //总共1278PE
  Free PE               1278     //空闲1278PE,也就是说这块硬盘是空的
  Allocated PE          0
  PV UUID               4sNW6e-XTha-7lYy-L2k8-uvWV-xxaN-wqXf0i

  --- Physical Segments ---
  Physical extent 0 to 1277:
    FREE

5,注销要删除的硬盘xvdc1

[root@iZ94zz3wqciZ ~]# pvchange -xn /dev/xvdc1    //注销xvdc1
  Physical volume "/dev/xvdc1" changed
  1 physical volume changed / 0 physical volumes not changed

[root@iZ94zz3wqciZ ~]# pvdisplay -m
  --- Physical volume ---
  PV Name               /dev/xvdb1
  VG Name               myfiles
  PV Size               21.99 GiB / not usable 4.78 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              5629
  Free PE               253
  Allocated PE          5376
  PV UUID               N2kxpi-FjET-rQ8p-MaDF-z3tH-fK3L-oW1STl

  --- Physical Segments ---
  Physical extent 0 to 5375:
    Logical volume      /dev/myfiles/fastdfs
    Logical extents     0 to 5375
  Physical extent 5376 to 5628:
    FREE

  --- Physical volume ---
  PV Name               /dev/xvdc1
  VG Name               myfiles
  PV Size               4.99 GiB / not usable 2.41 MiB
  Allocatable           NO       //不是激活状态了
  PE Size               4.00 MiB
  Total PE              1278
  Free PE               1278
  Allocated PE          0
  PV UUID               4sNW6e-XTha-7lYy-L2k8-uvWV-xxaN-wqXf0i

  --- Physical Segments ---
  Physical extent 0 to 1277:
    FREE

6,移动数据,将要删除的盘的数据移动到别的盘

[root@iZ94zz3wqciZ ~]# pvmove -i 1 /dev/xvdc1
  No data to move for myfiles

这一步,非常重要,如果盘子里面有数据,不做这一步,会造成数据丢失

7,将硬盘或分区从逻辑卷组中删除,并删除物理卷

[root@iZ94zz3wqciZ ~]# vgreduce myfiles /dev/xvdc1        //从逻辑卷组中删除
  Removed "/dev/xvdc1" from volume group "myfiles"

[root@iZ94zz3wqciZ ~]# pvremove /dev/xvdc1
  Labels on physical volume "/dev/xvdc1" successfully wiped     //删除物理卷

8,挂载并查看结果

[root@iZ94zz3wqciZ ~]# mount -a

[root@iZ94zz3wqciZ ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda1             20G   15G  4.6G  76% /
tmpfs                 498M     0  498M   0% /dev/shm
/dev/mapper/myfiles-fastdfs
                       21G  9.9G  9.8G  51% /mnt/fastdfs

到这儿,就安全的删除一个硬盘或者分区了。



转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/linux/1779.html