服务器系统集成及数据服务中心

 

一级栏目
一级栏目
一级栏目
一级栏目
一级栏目
一级栏目
一级栏目
一级栏目
锟斤拷锟斤拷锟斤拷锟斤拷…………………………………………………………………………………………>>>

linux磁盘管理系列之LVM的使用2

扩展LVM

上面我们的测试是vg是4G,我们的创建的lv是2G的,我们想给创建的lv扩展1G空间。

复制代码
[root@centos7 ~]$ lvextend -L +1G /dev/vg_test_01/lv_test_01                 # 我们扩大1G, +1G表示在原有基础上扩大1G,我们这里是有vg是有空间的,没有的话就需要创建pv,扩大vg在做 
  Size of logical volume vg_test_01/lv_test_01 changed from 2.00 GiB (512 extents) to 3.00 GiB (768 extents). 
  Logical volume vg_test_01/lv_test_01 successfully resized. 
[root@centos7 ~]$ df -h                                                      # 上面的命令提示lv已经到3G了, 但是使用df-h 查看还是2g 
Filesystem                         Size  Used Avail Use% Mounted on 
/dev/sda3                           48G  4.9G   41G  11% / 
devtmpfs                           979M  4.0K  979M   1% /dev 
tmpfs                              993M     0  993M   0% /dev/shm 
tmpfs                              993M   22M  971M   3% /run 
tmpfs                              993M     0  993M   0% /sys/fs/cgroup 
/dev/sda1                          976M  130M  780M  15% /boot 
/dev/sda5                           20G  307M   18G   2% /app 
tmpfs                              199M   32K  199M   1% /run/user/0 
/dev/sr0                           8.1G  8.1G     0 100% /run/media/root/CentOS 7 x86_64 
/dev/mapper/vg_test_01-lv_test_01  2.0G   33M  2.0G   2% /mnt/test_01 
[root@centos7 ~]$ resize2fs /dev/mapper/vg_test_01-lv_test_01              # 需要增长下文件系统的,让文件系统和lv大小匹配下 
meta-data=/dev/mapper/vg_test_01-lv_test_01 isize=512    agcount=4, agsize=131072 blks 
         =                       sectsz=512   attr=2, projid32bit=1 
         =                       crc=1        finobt=0 spinodes=0 
data     =                       bsize=4096   blocks=524288, imaxpct=25 
         =                       sunit=0      swidth=0 blks 
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1 
log      =internal               bsize=4096   blocks=2560, version=2 
         =                       sectsz=512   sunit=0 blks, lazy-count=1 
realtime =none                   extsz=4096   blocks=0, rtextents=0 
data blocks changed from 524288 to 786432 
[root@centos7 ~]$ df -h                                                    # 再次查看就是一致的了 
Filesystem                         Size  Used Avail Use% Mounted on 
/dev/sda3                           48G  4.9G   41G  11% / 
devtmpfs                           979M  4.0K  979M   1% /dev 
tmpfs                              993M     0  993M   0% /dev/shm 
tmpfs                              993M   22M  971M   3% /run 
tmpfs                              993M     0  993M   0% /sys/fs/cgroup 
/dev/sda1                          976M  130M  780M  15% /boot 
/dev/sda5                           20G  307M   18G   2% /app 
tmpfs                              199M   32K  199M   1% /run/user/0 
/dev/sr0                           8.1G  8.1G     0 100% /run/media/root/CentOS 7 x86_64 
/dev/mapper/vg_test_01-lv_test_01  3.0G   33M  3.0G   2% /mnt/test_01
复制代码

注意:

  1.    ext系列可以使用resieze2fs去调整。其他的文件系统可以参考http://tldp.org/HOWTO/LVM-HOWTO/extendlv.html
  2.    lvextentd 有个-r选项,自动去调整大小,匹配lv和文件系统大小的。

缩减LVM

逻辑卷的大小可以减小,也可以增加。 但是,在缩小卷本身之前,请记住缩小文件系统的大小或存在于卷中的任何文件是非常重要的,否则可能会丢失数据。

注意: xfs,jsf文件系统是没法缩减lvm的,因为这2个文件系统没法缩减文件系统大小的。参考http://tldp.org/HOWTO/LVM-HOWTO/reducelv.html

主要步骤:

  1.   检查空间使用情况和分析
  2.   umount设备
  3.   缩减文件系统大小
  4.   缩减lv大小
  5.   恢复挂载设备

上面我们的逻辑卷是3G的大小,在缩减之前我们需要确保缩减的空间不能影响数据的存放的。

检查工作

复制代码
[root@centos7 ~]$ cp -a /etc/ /mnt/test_01/                              # 我这里模拟下,只能目录有文件的情况  
[root@centos7 ~]$ du -sh /mnt/test_01/                                   # 查看下大小 
42M    /mnt/test_01/ 
[root@centos7 ~]$ df -h                                                   # 查看逻辑卷分区利用情况 ,数据才存放76M,我们的逻辑卷都3G呢, 如果我们想缩减1G,完全没有问题的。 
Filesystem                         Size  Used Avail Use% Mounted on 
/dev/sda3                           48G  4.9G   41G  11% / 
devtmpfs                           979M  4.0K  979M   1% /dev 
tmpfs                              993M     0  993M   0% /dev/shm 
tmpfs                              993M   22M  971M   3% /run 
tmpfs                              993M     0  993M   0% /sys/fs/cgroup 
/dev/sda1                          976M  130M  780M  15% /boot 
/dev/sda5                           20G  307M   18G   2% /app 
tmpfs                              199M   32K  199M   1% /run/user/0 
/dev/sr0                           8.1G  8.1G     0 100% /run/media/root/CentOS 7 x86_64 
/dev/mapper/vg_test_01-lv_test_01  3.0G   76M  3.0G   3% /mnt/test_01
复制代码

缩减空间

复制代码
[root@centos7 ~]$ umount /dev/vg_test_01/lv_test_01                           # 取消挂载 
[root@centos7 ~]$ resize2fs  /dev/vg_test_01/lv_test_01  1G                   # resieze2fs定义下文件系统大小 
resize2fs 1.42.9 (28-Dec-2013) 
Please run e2fsck -f /dev/vg_test_01/lv_test_01 first. 

[root@centos7 ~]$ e2fsck -f /dev/vg_test_01/lv_test_01                        #上面的命令要检查,那就检查下 
e2fsck 1.42.9 (28-Dec-2013) 
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/vg_test_01/lv_test_01: 3614/196608 files (0.1% non-contiguous), 42361/786432 blocks 
[root@centos7 ~]$ resize2fs  /dev/vg_test_01/lv_test_01  1G                   # 检查完,那就执行缩减文件系统大小 
resize2fs 1.42.9 (28-Dec-2013) 
Resizing the filesystem on /dev/vg_test_01/lv_test_01 to 262144 (4k) blocks. 
The filesystem on /dev/vg_test_01/lv_test_01 is now 262144 blocks long. 
[root@centos7 ~]$ lvreduce  -L 1G /dev/vg_test_01/lv_test_01                  # 缩减下逻辑卷到1G,这里有提示,缩减可能会丢失数据,输入y即可 
  WARNING: Reducing active logical volume to 1.00 GiB. 
  THIS MAY DESTROY YOUR DATA (filesystem etc.) 
Do you really want to reduce vg_test_01/lv_test_01? [y/n]: yG                  
  Size of logical volume vg_test_01/lv_test_01 changed from 3.00 GiB (768 extents) to 1.00 GiB (256 extents). 
  Logical volume vg_test_01/lv_test_01 successfully resized. 
[root@centos7 ~]$ mount /dev/vg_test_01/lv_test_01  /mnt/test_01/            # 恢复挂载 
[root@centos7 ~]$ ll /mnt/test_01/ 
total 28 
drwxr-xr-x. 140 root root 12288 Dec  7 13:54 etc 
drwx.   2 root root 16384 Dec  7 15:47 lost+found 
[root@centos7 ~]$ df -h                                                     # 检查下 
Filesystem                         Size  Used Avail Use% Mounted on 
/dev/sda3                           48G  4.9G   41G  11% / 
devtmpfs                           979M  4.0K  979M   1% /dev 
tmpfs                              993M     0  993M   0% /dev/shm 
tmpfs                              993M   22M  971M   3% /run 
tmpfs                              993M     0  993M   0% /sys/fs/cgroup 
/dev/sda1                          976M  130M  780M  15% /boot 
/dev/sda5                           20G  307M   18G   2% /app 
tmpfs                              199M   32K  199M   1% /run/user/0 
/dev/sr0                           8.1G  8.1G     0 100% /run/media/root/CentOS 7 x86_64 
/dev/mapper/vg_test_01-lv_test_01  944M   52M  825M   6% /mnt/test_01
复制代码

迁移LVM的vg

移动vg到另外一个机器上

主要步骤:

  1.   检查和分析
  2. 一些预处理工作
  3. umount文件系统
  4.   设置卷组非活动
  5.   导出卷组
  6.   取出设备,插入到新机器
  7.   导入卷组
  8.   激活卷组
  9.   挂载文件系统

检查分析

复制代码
[root@centos7 ~]$ vgs 
  VG         #PV #LV #SN Attr   VSize VFree 
  vg_test_01   4   1   0 wzn- 3.98g 2.98g 
[root@centos7 ~]$ pvs 
l  PV         VG         Fmt  Attr PSize    PFree    
  /dev/sdb1  vg_test_01 lvm2 a  1020.00m       0  
  /dev/sdb2             lvm2 -     1.00g    1.00g 
  /dev/sdc1  vg_test_01 lvm2 a  1020.00m 1016.00m 
  /dev/sdc2             lvm2 -     1.00g    1.00g 
  /dev/sdd1  vg_test_01 lvm2 a  1020.00m 1020.00m 
  /dev/sdd2             lvm2 -     5.00g    5.00g 
  /dev/sde1  vg_test_01 lvm2 a  1020.00m 1020.00m 
  /dev/sde2             lvm2 -     2.00g    2.00g 
[root@centos7 ~]$ lvs 
  LV         VG         Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert 
  lv_test_01 vg_test_01 -wi-ao 1.00g                                                    
复制代码

我们使用lvs,vgs,pvs分别查看信息。发现我们vg是3G的大小。lv是1G的大小。但是vg的pv来自不同的磁盘的。我们要迁移vg。不能把包含pv都迁移过去把。 那这一下子需要4个盘的。

我们把pv数据迁移下,到时候摘盘的时候只那一个就可以多好。 发现/dev/sdd2是一个5G的盘。 打算使用/dev/sdd这个盘作为迁移盘来迁移数据。

一些预处理工作

复制代码
[root@centos7 ~]$ umount /mnt/test_01/                                    # 去除挂载
[root@centos7 ~]$ vgchange  -an vg_test_01                                # 设置vg不激活 
  0 logical volume(s) in volume group "vg_test_01" now active
[root@centos7 ~]$ pvmove /dev/sd{b1,c1,e1}                                 # 迁移pv数据 
  /dev/sdb1: Moved: 0.00% 
  /dev/sdb1: Moved: 100.00% 
[root@centos7 ~]$ pvs                                                      # 确保pv都是空的,没有数据存放了。 
  PV         VG         Fmt  Attr PSize    PFree    
  /dev/sdb1  vg_test_01 lvm2 a  1020.00m 1020.00m 
  /dev/sdb2             lvm2 -     1.00g    1.00g 
  /dev/sdc1  vg_test_01 lvm2 a  1020.00m 1016.00m 
  /dev/sdc2             lvm2 -     1.00g    1.00g 
  /dev/sdd1  vg_test_01 lvm2 a  1020.00m       0  
  /dev/sdd2  vg_test_01 lvm2 a    <5.00g   <5.00g 
  /dev/sde1  vg_test_01 lvm2 a  1020.00m 1020.00m 
  /dev/sde2             lvm2 -     2.00g    2.00g 
 
[root@centos7 ~]$ vgreduce vg_test_01  /dev/sd{b1,c1,e1}                   # 缩减vg 去除b1,c1,e1 
  Removed "/dev/sdb1" from volume group "vg_test_01" 
  Removed "/dev/sdc1" from volume group "vg_test_01" 
  Removed "/dev/sde1" from volume group "vg_test_01" 
[root@centos7 ~]$ pvremove /dev/sd{b1,c1,e1}                               # 移除pv,这里提示, 
  Lables on physical volume "/dev/sdb1" sucdessfully wiped. 
  Labels on physical volume "/dev/sdc1" successfully wiped. 
  Labels on physical volume "/dev/sde1" successfully wiped.
复制代码

导出vg

复制代码
[root@centos7 ~]$ vgdisplay                                       # 导出前的确认 
  - Volume group - 
  VG Name               vg_test_01 
  System ID              
  Format                lvm2 
  Metadata Areas        2 
  Metadata Sequence No  24 
  VG Access             read/write 
  VG Status             resizable 
  MAX LV                0 
  Cur LV                1 
  Open LV               0 
  Max PV                0 
  Cur PV                2 
  Act PV                2 
  VG Size               5.99 GiB 
  PE Size               4.00 MiB 
  Total PE              1534 
  Alloc PE / Size       256 / 1.00 GiB 
  Free  PE / Size       1278 / 4.99 GiB 
  VG UUID               sZwHea-LabA-SJaV-Wz40-qFp3-Um2w-pvmJAY 
    
[root@centos7 ~]$ vgexport vg_test_01                                    # 导出vg发现有激活的设备,需要关闭 
  Volume group "vg_test_01" has active logical volumes 
[root@centos7 ~]$ vgchange -an vg_test_01                                # 设置为非激活 
  0 logical volume(s) in volume group "vg_test_01" now active 
[root@centos7 ~]$ vgexport vg_test_01                                    # 导出, 要看到成功导出才可以,这里注意了, 如果这个vg名字和目标机器有重名的话, 这里使用rename改下在导出 
  Volume group "vg_test_01" successfully exported 

[root@centos7 ~]$ sync                                                   # 多执行几次sync。 防止内存和磁盘没有同步的问题。 
[root@centos7 ~]$ sync 
[root@centos7 ~]$ sync 
[root@centos7 ~]$ sync
复制代码

我这里把这个磁盘取出来安装到另外一个机器上面。下面的一部分操作在新机器上执行。注意啦。

识别硬件

复制代码
[root@centos6 ~]$ /git/Bash/other/scan_scsi_device.sh                             # 执行下扫描硬件脚本,我自己写的,后面把这个bash脚本贴出来 
start scan 
/sys/devices/pci0000:00/0000:00:07.1/host0/scsi_host/host0/scan scan finished. 
/sys/devices/pci0000:00/0000:00:07.1/host1/scsi_host/host1/scan scan finished. 
/sys/devices/pci0000:00/0000:00:10.0/host2/scsi_host/host2/scan scan finished. 
end  scan 
please use lsblk check,  
[root@centos6 ~]$ lsblk                                                           # 使用lsblk查看,可以发现,识别出来就是那个/dev/sdc 
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT 
sr0     11:0    1  3.7G  0 rom  /media/CentOS_6.9_Final 
sr1     11:1    1  8.1G  0 rom  /media/CentOS 7 x86_64 
sda      8:0    0  200G  0 disk  
├─sda1   8:1    0    1G  0 part /boot 
├─sda2   8:2    0 48.8G  0 part / 
├─sda3   8:3    0 19.5G  0 part /app 
├─sda5   8:5    0    2G  0 part [SWAP] 
├─sda4   8:4    0    2M  0 part  
└─sda6   8:6    0    1G  0 part  
sdb      8:16   0  100G  0 disk  
├─sdb1   8:17   0    1G  0 part  
├─sdb2   8:18   0   20G  0 part  
├─sdb3   8:19   0    1G  0 part  
├─sdb4   8:20   0    1K  0 part  
├─sdb5   8:21   0   20G  0 part  
├─sdb6   8:22   0    1G  0 part  
└─sdb7   8:23   0    1G  0 part  
sr2     11:2    1  3.7G  0 rom  /media/CentOS_6.9_Final_ 
sr3     11:3    1    2G  0 rom  /media/CentOS_6.9_Final__ 
sdc      8:32   0   20G  0 disk  
├─sdc1   8:33   0    1G  0 part  
└─sdc2   8:34   0    5G  0 part 
复制代码

脚本内容如下

复制代码
[root@centos6 ~]$ cat /git/Bash/other/scan_scsi_device.sh  
#!/bin/bash 
#================================================ 
#FileName   :scan_scsi_device 
#Author     :zhaojiedi 
#Description:如果你添加了一个新的硬盘设备, 但是没有被系统识别,使用如下脚本可以识别出来,  
#DateTime   :2017-11-30 18:03:27 
#Version    :V1.0 
#Other      :最新版本建议从这里获取https://github.com/zhaojiedi1992/Bash 
#================================================ 
#!/bin/bash 
echo "start scan" 
for i in `find /sys/devices/ -name scan` do  
    echo "- - -" > $i 
    echo  $i scan finished. 
done 
echo "end  scan" 
echo -e "\033[32mplease use lsblk check\033[0m"
复制代码

导入并挂载

复制代码
[root@centos6 ~]$ vgscan                                               # vg扫描 
  Reading all physical volumes.  This may take a while... 
  Found exported volume group ", vg_test_01" using metadata type lvm2 
[root@centos6 ~]$ pvscan                                               # pv扫描 
  PV /dev/sdc1    is in exported VG vg_test_01 [1020.00 MiB / 0    free] 
  PV /dev/sdc2    is in exported VG vg_test_01 [5.00 GiB / 4.99 GiB free] 
  Total: 2 [5.99 GiB] / in use: 2 [5.99 GiB] / in no VG: 0 [0   ] 
[root@centos6 ~]$ vgimport vg_test_01                                  # 确认无错误的就导入。 
  Volume group "vg_test_01" successfully imported 
[root@centos6 ~]$ vgs                                                  # 查看下vg信息 
  VG         #PV #LV #SN Attr   VSize VFree 
  vg_test_01   2   1   0 wzn- 5.99g 4.99g 
[root@centos6 ~]$ vgchange -ay vg_test_01                              # 激活vg 
  1 logical volume(s) in volume group "vg_test_01" now active 
[root@centos6 ~]$ mkdir /mnt/new_test                                  # 创建挂载点 
[root@centos6 ~]$ mount /dev/vg_test_01/lv_test_01  /mnt/new_test/     # 挂载 
[root@centos6 ~]$ du -sh /mnt/new_test/                                # 确认文件没有丢失 
45M    /mnt/new_test/
复制代码

快照LVM

分析下当前环境,我把vg移动到centos6上了。一下操作在centos6上执行的。

复制代码
[root@centos6 ~]$ vgs                                        # 查看下vg信息, 一共6G,还有5G呢。 
  VG         #PV #LV #SN Attr   VSize VFree 
  vg_test_01   2   1   0 wzn- 5.99g 4.99g 
[root@centos6 ~]$ pvs                                       # 有2个pv 
  PV         VG         Fmt  Attr PSize    PFree 
  /dev/sdc1  vg_test_01 lvm2 au 1020.00m    0  
  /dev/sdc2  vg_test_01 lvm2 au    5.00g 4.99g
复制代码

我们刚才给/mnt/new_test目录挂载逻辑卷vg_test_01。假如我们的/mnt/new_test的内容一直在变化。想去备份数据就需要快照卷了。

创建快照

[root@centos6 etc]$ lvcreate  -L 500M -s -p r -n vg_test_01_snapshot /dev/vg_test_01/lv_test_01       # -L指定大小, -s 表示创建快照而不是普通逻辑卷,-p r 指定权限为只读的, -n指定名字 
  Logical volume "vg_test_01_snapshot" created.

注意: 如果使用的xfs文件系统, 挂载的时候需要加入nouuid,ro选项,且创建快照的时候不能指定-p r选项。具体参考: http://tldp.org/HOWTO/LVM-HOWTO/snapshots_backup.html

模拟一些数据修改操作

[root@centos6 etc]$ echo "zhaojiedi1992" >>/mnt/new_test/zhaojiedi1992.txt                            # 制作一些修改情况 
[root@centos6 etc]$ vim /mnt/new_test/etc/motd

监控快照情况

复制代码
[root@centos6 ~]$ lvdisplay  /dev/vg_test_01/vg_test_01_snapshot  
  - Logical volume - 
  LV Path                /dev/vg_test_01/vg_test_01_snapshot 
  LV Name                vg_test_01_snapshot 
  VG Name                vg_test_01 
  LV UUID                pZ8x2u-lqPA-uLef-P816-dS8V-wZ0y-PKWWwL 
  LV Write Access        read only                                         #只读的 
  LV Creation host, time centos6.magedu.com, 2017-12-02 06:11:56 +0800 
  LV snapshot status     active destination for lv_test_01                 # 这是lv_test_01的快照 
  LV Status              available 
  # open                 0 
  LV Size                1.00 GiB                                          # 快照的总大小 
  Current LE             256 
  COW-table size         500.00 MiB 
  COW-table LE           125 
  Allocated to snapshot  0.01%                                             # 这个地方可以看当前快照占用情况。  
  Snapshot chunk size    4.00 KiB 
  Segments               1 
  Allocation             inherit 
  Read ahead sectors     auto 
  - currently set to     256 
  Block device           253:3
复制代码

挂载快照并备份

复制代码
[root@centos6 etc]$ mkdir /mnt/test_bak                                            # 创建一个快照挂载目录 
[root@centos6 etc]$ mount /dev/vg_test_01/vg_test_01_snapshot  /mnt/test_bak/      # 挂载,因为我们上面创建快照的时候指定-pr只读的 
mount: block device /dev/mapper/vg_test_01-vg_test_01_snapshot is write-protected, mounting read-only 
[root@centos6 etc]$ cat /mnt/test_bak/                                             # 快照后的zhaojiedi1992.txt没有的。 
etc/        lost+found/  
[root@centos6 etc]$ cat /mnt/test_bak/etc/motd                                     # 这个地方的文件也是快照前的样子。没有问题 
Welcom you this system 
[root@centos6 etc]$ tar -jcvf /root/test.tar.bz acl selinux  xattrs /mnt/test_bak/   # 备份数据赶紧的
复制代码

移除快照

[root@centos6 etc]$ umount /mnt/test_bak/                                    # 取消挂载 
[root@centos6 etc]$ lvremove /dev/vg_test_01/vg_test_01_snapshot             # 移除了快照卷 
Do you really want to remove active logical volume vg_test_01_snapshot? [y/n]: y 
  Logical volume "vg_test_01_snapshot" successfully removed

参考资料

redhat : https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/installation_guide/create_lvm-x86

suse : https://www.suse.com/documentation/sles11/stor_admin/data/lvm.html

lvm how to : http://tldp.org/HOWTO/LVM-HOWTO/index.html

发布时间:2019/10/28 阅读:6061次 来源:
 


 
锟斤拷锟斤拷锟斤拷锟斤拷分类
   
  行业新闻
 
  公司动态
 
  技术资料
 
  电脑维修
 
  恢复案例
 
  SQL数据库
 
  磁盘阵列
 
  服务器
 
  财务软件
 
  网络问题
 
  linux-XFS
 
  苹果电脑&系统
 
  安卓系统相关
 
  办公文件
 
  操作系统
 
  日常生活(烟台)
 
  网站相关
 
   
 
友情链接
 
 
 
 
 
   
公司地址:烟台电脑市场#308
电话:0535-6688830 E-Mail:163@163.com
Copyright©2011-2012 烟台瑞驰科贸有限公司 All Rights Reserved.
鲁ICP备11014811号-1
您是本站第 位访问者