linux lvm 安全 删除 硬盘或分区

张映 发表于 2015-12-31

分类目录: linux

标签:, , , , ,

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

1,查看分区情况

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

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

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

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

3,对逻辑卷重定义大小

  1. [root@iZ94zz3wqciZ ~]# resize2fs -p /dev/mapper/myfiles-fastdfs  21G     //重定义大小  
  2. resize2fs 1.41.12 (17-May-2010)  
  3. Resizing the filesystem on /dev/mapper/myfiles-fastdfs to 5505024 (4k) blocks.  
  4. Begin pass 2 (max = 529848)  
  5. Relocating blocks             XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  
  6. Begin pass 3 (max = 216)  
  7. Scanning inode table          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  
  8. Begin pass 4 (max = 66693)  
  9. Updating inode references     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  
  10. The filesystem on /dev/mapper/myfiles-fastdfs is now 5505024 blocks long.  
  11.   
  12. [root@iZ94zz3wqciZ ~]# lvreduce -L 21G /dev/mapper/myfiles-fastdfs     //重定义大小  
  13.   WARNING: Reducing active logical volume to 21.00 GiB  
  14.   THIS MAY DESTROY YOUR DATA (filesystem etc.)  
  15. Do you really want to reduce fastdfs? [y/n]: y  
  16.   Size of logical volume myfiles/fastdfs changed from 26.98 GiB (6907 extents) to 21.00 GiB (5376 extents).  
  17.   Logical volume fastdfs successfully resized  

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

4,查看物理卷情况

  1. [root@iZ94zz3wqciZ ~]# pvdisplay -m  
  2.   --- Physical volume ---  
  3.   PV Name               /dev/xvdb1  
  4.   VG Name               myfiles  
  5.   PV Size               21.99 GiB / not usable 4.78 MiB  
  6.   Allocatable           yes  
  7.   PE Size               4.00 MiB  
  8.   Total PE              5629  
  9.   Free PE               253  
  10.   Allocated PE          5376  
  11.   PV UUID               N2kxpi-FjET-rQ8p-MaDF-z3tH-fK3L-oW1STl  
  12.   
  13.   --- Physical Segments ---  
  14.   Physical extent 0 to 5375:  
  15.     Logical volume      /dev/myfiles/fastdfs  
  16.     Logical extents     0 to 5375  
  17.   Physical extent 5376 to 5628:  
  18.     FREE  
  19.   
  20.   --- Physical volume ---  
  21.   PV Name               /dev/xvdc1  
  22.   VG Name               myfiles  
  23.   PV Size               4.99 GiB / not usable 2.41 MiB  
  24.   Allocatable           yes      //激活状态,可写入  
  25.   PE Size               4.00 MiB  
  26.   Total PE              1278     //总共1278PE  
  27.   Free PE               1278     //空闲1278PE,也就是说这块硬盘是空的  
  28.   Allocated PE          0  
  29.   PV UUID               4sNW6e-XTha-7lYy-L2k8-uvWV-xxaN-wqXf0i  
  30.   
  31.   --- Physical Segments ---  
  32.   Physical extent 0 to 1277:  
  33.     FREE  

5,注销要删除的硬盘xvdc1

  1. [root@iZ94zz3wqciZ ~]# pvchange -xn /dev/xvdc1    //注销xvdc1  
  2.   Physical volume "/dev/xvdc1" changed  
  3.   1 physical volume changed / 0 physical volumes not changed  
  4.   
  5. [root@iZ94zz3wqciZ ~]# pvdisplay -m  
  6.   --- Physical volume ---  
  7.   PV Name               /dev/xvdb1  
  8.   VG Name               myfiles  
  9.   PV Size               21.99 GiB / not usable 4.78 MiB  
  10.   Allocatable           yes  
  11.   PE Size               4.00 MiB  
  12.   Total PE              5629  
  13.   Free PE               253  
  14.   Allocated PE          5376  
  15.   PV UUID               N2kxpi-FjET-rQ8p-MaDF-z3tH-fK3L-oW1STl  
  16.   
  17.   --- Physical Segments ---  
  18.   Physical extent 0 to 5375:  
  19.     Logical volume      /dev/myfiles/fastdfs  
  20.     Logical extents     0 to 5375  
  21.   Physical extent 5376 to 5628:  
  22.     FREE  
  23.   
  24.   --- Physical volume ---  
  25.   PV Name               /dev/xvdc1  
  26.   VG Name               myfiles  
  27.   PV Size               4.99 GiB / not usable 2.41 MiB  
  28.   Allocatable           NO       //不是激活状态了  
  29.   PE Size               4.00 MiB  
  30.   Total PE              1278  
  31.   Free PE               1278  
  32.   Allocated PE          0  
  33.   PV UUID               4sNW6e-XTha-7lYy-L2k8-uvWV-xxaN-wqXf0i  
  34.   
  35.   --- Physical Segments ---  
  36.   Physical extent 0 to 1277:  
  37.     FREE  

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

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

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

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

  1. [root@iZ94zz3wqciZ ~]# vgreduce myfiles /dev/xvdc1        //从逻辑卷组中删除  
  2.   Removed "/dev/xvdc1" from volume group "myfiles"  
  3.   
  4. [root@iZ94zz3wqciZ ~]# pvremove /dev/xvdc1  
  5.   Labels on physical volume "/dev/xvdc1" successfully wiped     //删除物理卷  

8,挂载并查看结果

  1. [root@iZ94zz3wqciZ ~]# mount -a  
  2.   
  3. [root@iZ94zz3wqciZ ~]# df -h  
  4. Filesystem            Size  Used Avail Use% Mounted on  
  5. /dev/xvda1             20G   15G  4.6G  76% /  
  6. tmpfs                 498M     0  498M   0% /dev/shm  
  7. /dev/mapper/myfiles-fastdfs  
  8.                        21G  9.9G  9.8G  51% /mnt/fastdfs  

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



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