本文最后更新于 395 天前,其中的信息可能已经过时,如有错误请发送邮件到 wuxianglongblog@163.com
LVM 是 Linux 操作系统的逻辑卷管理器。 现在有两个 Linux 版本的 LVM, 分别是 LVM1,LVM2。LVM1 是一种已经被认为稳定了几年的成熟产品,LVM2 是最新最好的 LVM 版本。 LVM2 几乎完全向后兼容使用 LVM1 创建的卷。此例外是快照(在升级到 LVM 2 之前,必须删除快照卷)。
逻辑卷管理提供了比传统的磁盘和分区视图更高级别的计算机系统上磁盘存储的视图。 这使得系统管理员可以更灵活地将存储分配给应用程序和用户。
在逻辑卷管理器的控制下创建的存储卷可以随意调整大小和移动,尽管这可能需要对文件系统工具进行一些升级。
逻辑卷管理器还允许管理用户定义组中的存储卷,允许系统管理员处理明显命名的卷组(如 “development” 和 “sales”),而不是物理磁盘名称,例如 “sda” 和 “sdb”
| volume group (VG) |
| |
| |
| physical volume (PV) |
| |
| |
| logical volume (LV) |
| |
| |
| physical extent (PE) |
| |
| logical extent (LE) |
| |
一个具体的例子将有助于:
假设我们有一个名为 VG1 的卷组,这个卷组的物理盘区大小为 4MB。 在这个卷组中,我们引入了 2 个硬盘分区 /dev/hda1 和 /dev/hdb1。
这些分区将成为物理卷 PV1 和 PV2(管理员可以自行决定是否提供更有意义的名称)。
PV 被分成 4MB 块,因为这是卷组的大小。磁盘大小不同,我们在 PV1 中获得 99 个扩展盘区,在 PV2 中获得 248 个扩展盘区。
我们现在可以创建自己的逻辑卷,它可以是 1 到 347(248 + 99)范围之间的任何大小。
当创建逻辑卷时,在逻辑盘区和物理盘区之间定义映射,例如, 逻辑盘区 1 可以映射到 PV1 的物理盘区 51 上,写入逻辑盘的第一个 4MB 的数据实际上被写入到 PV1 的第 51 盘区。
依次为:disk -> partition -> PV -> VG -> LV -> fs,也即磁盘 -> 分区 -> 物理卷 -> 卷组 -> 逻辑卷 -> 文件系统。
线性映射:将分配一定范围的 PE 到 LV 的一个区域,例如 LE 1 - 99 映射到 PV1,LE 100 - 347 映射到 PV2。
分条映射:将跨多个物理卷交织逻辑盘区的块,这个方式一定程度上提高了性能,具体如下
1st chunk of LE [1] -> PV1 [1], #第一个逻辑块在 PV1 设备上, 第二个在 PV2 设备上,等等。
2nd chunk of LE[1] -> PV2[1],
3rd chunk of LE[1] -> PV3[1],
4th chunk of LE[1] -> PV1[2],
这是 LVM 提供的一个非常棒的功能就是快照了,他允许管理员创建一个新的块设备来呈现一个逻辑卷的的精确副本,在某个时间冻结,管理员可以及时备份指定一致状态下的数据,备份完毕就可以删除设备了。
快照最初包含有关快照的一些元数据,但没有来自源逻辑卷的实际数据。快照使用写时复制(copy on write)技术来检测数据在原始数据块中何时更改。它将拍摄快照时保存的值复制到快照卷中的块中,然后允许将新数据存储在源块中。
随着更多块从源逻辑卷上的原始值发生更改,快照大小也会增加。如果快照卷变满,它将被丢弃,所以分配足够的空间是非常重要的,具体取决于你数据的变换情况,如果快照大小和原始数据一样大,它将永远不会溢出的。
| [root@centos7 ~]$ rpm -q lvm2 |
| lvm2-2.02.171-8.el7.x86_64 |
| [root@centos7 ~]$ |
| [root@centos7 ~]$ pv |
| pvchange pvck pvcreate pvdisplay pvmove pvremove pvresize pvs pvscan |
| [root@centos7 ~]$ vg |
| vgcfgbackup vgck vgdisplay vgimport vgmknodes vgrename vgsplit |
| vgcfgrestore vgconvert vgexport vgimportclone vgreduce vgs |
| vgchange vgcreate vgextend vgmerge vgremove vgscan |
| [root@centos7 ~]$ lv |
| lvchange lvdisplay lvmconf lvmdump lvmsadc lvremove lvs |
| lvconvert lvextend lvmconfig lvmetad lvmsar lvrename lvscan |
| lvcreate lvm lvmdiskscan lvmpolld lvreduce lvresize |
| 上面的命令比较多,简单介绍几个常用的命令。 |
| • pv打头的:代表pv相关的命令 |
| • vg带头的:代表vg相关的命令 |
| • lv带头的: 代表lv相关的命令 |
| • create:创建相关 |
| • remove:移除相关 |
| • display:显示相关 |
| • import:导入 相关 |
| • export:导出相关 |
| • rename:重命名 |
| • vgchange:改变状态相关 |
| • extend:扩展相关 |
| reduce:缩进相关 |
pv 作用:初始化磁盘和磁盘分区
在将磁盘或者磁盘分区用作物理卷(PV)之前,你需要对其进行初始化工作。
| [root@centos7 ~]$ pvcreate /dev/sdb1 |
注意:
- 不建议对整个磁盘做 PV 初始化工作。因为其他操作系统不能识别 LVM 元数据,导致我们认为磁盘是空闲的,重新设置分区覆盖原有数据
- 如果是分区需要调整分区的类型为 LVM (代码为 8e)
分区并设置分区类型
| [root@centos7 ~]$ fdisk -l |
| Disk /dev/sda: 214.7GB, 214748364800bytes, 419430400 sectors |
| Units = sectors of 1* 512= 512 bytes |
| Sector size (logical/physical): 512bytes / 512 bytes |
| I/O size (minimum/optimal): 512bytes / 512 bytes |
| Disk label type: dos |
| Disk identifier: 0x000b0b8a |
| Device Boot Start End Blocks Id System |
| /dev/sda1 * 20482099199104857683 Linux |
| /dev/sda2 209920027053465513421772883 Linux |
| /dev/sda3 2705346563729346555120000083 Linux |
| /dev/sda4 372934656419430399232478725 Extended |
| /dev/sda5 3729387524138987512048000083 Linux |
| Disk /dev/sdb: 107.4GB, 107374182400bytes, 209715200 sectors |
| Units = sectors of 1* 512= 512 bytes |
| Sector size (logical/physical): 512bytes / 512 bytes |
| I/O size (minimum/optimal): 512bytes / 512 bytes |
| Disk label type: dos |
| Disk identifier: 0x93d380cf |
| Device Boot Start End Blocks Id System |
| /dev/sdb1 204820991991048576 8e Linux LVM |
| /dev/sdb2 209920041963511048576 8e Linux LVM |
| Disk /dev/sde: 21.5GB, 21474836480bytes, 41943040 sectors |
| Units = sectors of 1* 512= 512 bytes |
| Sector size (logical/physical): 512bytes / 512 bytes |
| I/O size (minimum/optimal): 512bytes / 512 bytes |
| Disk label type: dos |
| Disk identifier: 0x73afb36f |
| Device Boot Start End Blocks Id System |
| /dev/sde1 204820991991048576 8e Linux LVM |
| /dev/sde2 209920062935032097152 8e Linux LVM |
| Disk /dev/sdd: 21.5GB, 21474836480bytes, 41943040 sectors |
| Units = sectors of 1* 512= 512 bytes |
| Sector size (logical/physical): 512bytes / 512 bytes |
| I/O size (minimum/optimal): 512bytes / 512 bytes |
| Disk label type: dos |
| Disk identifier: 0x7e0900d8 |
| Device Boot Start End Blocks Id System |
| /dev/sdd1 204820991991048576 8e Linux LVM |
| /dev/sdd2 2099200125849595242880 8e Linux LVM |
| Disk /dev/sdc: 21.5GB, 21474836480bytes, 41943040 sectors |
| Units = sectors of 1* 512= 512 bytes |
| Sector size (logical/physical): 512bytes / 512 bytes |
| I/O size (minimum/optimal): 512bytes / 512 bytes |
| Disk label type: dos |
| Disk identifier: 0xc56b90d8 |
| Device Boot Start End Blocks Id System |
| /dev/sdc1 204820991991048576 8e Linux LVM |
| /dev/sdc2 209920041963511048576 8e Linux LVM |
| Disk /dev/sdf: 21.5GB, 21474836480bytes, 41943040 sectors |
| Units = sectors of 1* 512= 512 bytes |
| Sector size (logical/physical): 512bytes / 512 bytes |
| I/O size (minimum/optimal): 512bytes / 512 bytes |
| [root@centos7 ~]$ partprobe |
| Warning: Unable to open /dev/sr0 read-write(Read-only filesystem). /dev/sr0 has been opened read-only. |
| [root@centos7 ~]$ lsblk |
| NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT |
| sda 8:00200G 0 disk |
| ├─sda1 8:101G 0part /boot |
| ├─sda2 8:20128G 0 part |
| ├─sda3 8:3048.8G 0part / |
| ├─sda4 8:40512B 0 part |
| └─sda5 8:5019.5G 0part /app |
| sdb 8:160100G 0 disk |
| ├─sdb1 8:1701G 0 part |
| └─sdb2 8:1801G 0 part |
| sdc 8:32020G 0 disk |
| ├─sdc1 8:3301G 0 part |
| └─sdc2 8:3401G 0 part |
| sdd 8:48020G 0 disk |
| ├─sdd1 8:4901G 0 part |
| └─sdd2 8:5005G 0 part |
| sde 8:64020G 0 disk |
| ├─sde1 8:6501G 0 part |
| └─sde2 8:6602G 0 part |
| sdf 8:80020G 0 disk |
| sr0 11:018.1G 0rom /run/media/root/CentOS 7x86_64 |
上面我们在 /dev/sd {b,c,d,e} 这 4 个盘每个都有 2 个类型为 lvm 的盘。接下来使用 pvcreat 初始化为物理卷
| [root@centos7 ~]$ pvcreate /dev/sd{b1,b2,c1,c2,d1,d2,e1,e2} -y |
| Wiping xfs_external_log signature on /dev/sdb2. |
| Wiping xfs_external_log signature on /dev/sdb2. |
| Wiping xfs_external_log signature on /dev/sdb2. |
| Wiping xfs_external_log signature on /dev/sdb2. |
| Wiping xfs_external_log signature on /dev/sdb2. |
| Wiping xfs_external_log signature on /dev/sdb2. |
| Wiping xfs_external_log signature on /dev/sdb2. |
| Wiping xfs_external_log signature on /dev/sdb2. |
| Physical volume "/dev/sdb1" successfully created. |
| Physical volume "/dev/sdb2" successfully created. |
| Physical volume "/dev/sdc1" successfully created. |
| Physical volume "/dev/sdc2" successfully created. |
| Physical volume "/dev/sdd1" successfully created. |
| Physical volume "/dev/sdd2" successfully created. |
| Physical volume "/dev/sde1" successfully created. |
| Physical volume "/dev/sde2"successfully created. |
| [root@centos7 ~]$ pvs |
| PV VG Fmt Attr PSize PFree |
| /dev/sdb1 vg_test_01 lvm2 a-- 1020.00m1020.00m/dev/sdb2 lvm2 --- 1.00g 1.00g |
| /dev/sdc1 vg_test_01 lvm2 a-- 1020.00m1020.00m/dev/sdc2 lvm2 --- 1.00g 1.00g |
| /dev/sdd1 vg_test_01 lvm2 a-- 1020.00m1020.00m/dev/sdd2 lvm2 --- 5.00g 5.00g |
| /dev/sde1 vg_test_01 lvm2 a-- 1020.00m1020.00m/dev/sde2 lvm2 --- 2.00g 2.00g |
vg 的创建是比较简单的。
| [root@centos7 ~]$ vgcreate vg_test_01 /dev/sd{b1,c1,d1,e1} |
| Volume group "vg_test_01"successfully created |
| [root@centos7 ~]$ vgs |
| VG |
| vg_test_01 400wz--n- 3.98g 3.98g |
| [root@centos7 ~]$ vgdisplay |
| --- Volume group --- |
| VG Name vg_test_01 |
| System ID |
| Format lvm2 |
| Metadata Areas 4 |
| Metadata Sequence No 1 |
| VG Access read/write |
| VG Status resizable |
| MAX LV 0 |
| Cur LV 0 |
| Open LV 0 |
| Max PV 0 |
| Cur PV 4 |
| Act PV 4 |
| VG Size 3.98 GiB |
| PE Size 4.00 MiB |
| Total PE 1020 |
| Alloc PE / Size 0/ 0 |
| Free PE / Size 1020/ 3.98 GiB |
| VG UUID Y5NjjP-e3Aq-3u6E-gw6P-fLyy-Y5pN-ryyN9R |
| [root@centos7 ~]$ vgchange -a y vg_test_01 |
| 0logical volume(s) involume group "vg_test_01"now active |
| [root@centos7 ~]$ vgchange -a n vg_test_01 |
| 0logical volume(s) involume group "vg_test_01" now active |
| [root@centos7 ~]$ vgremove vg_test_01 |
| [root@centos7 ~]$ vgcreate vg_test_01 /dev/sd{b1,c1,d1,e1} |
| Volume group "vg_test_01" successfully created |
| [root@centos7 ~]$ vgextend vg_test_01 /dev/sdb2 |
| Volume group "vg_test_01"successfully extended |
| 这个操作需要使用vgdisplay查看vg的使用情况和删除的pv上没有数据。如果有使用pvmove迁移完毕数据才能移除。 |
| [root@centos7 ~]$ vgreduce vg_test_01 /dev/sdb2 |
| Removed "/dev/sdb2"from volume group "vg_test_01" |
| [root@centos7 ~]$ vgdisplay |
| --- Volume group --- |
| VG Name vg_test_01 |
| System ID |
| Format lvm2 |
| Metadata Areas 4 |
| Metadata Sequence No 3 |
| VG Access read/write |
| VG Status resizable |
| MAX LV 0 |
| Cur LV 0 |
| Open LV 0 |
| Max PV 0 |
| Cur PV 4 |
| Act PV 4 |
| VG Size 3.98 GiB |
| PE Size 4.00 MiB |
| Total PE 1020 |
| Alloc PE / Size 0/ 0 |
| Free PE / Size 1020/ 3.98 GiB |
| VG UUID sZwHea-LabA-SJaV-Wz40-qFp3-Um2w-pvmJAY |
| |
| [root@centos7 ~]$ lvcreate -L 2G -n lv_test_01 vg_test_01 |
| WARNING: linux_raid_member signature detected on /dev/vg_test_01/lv_test_01 at offset 4096. Wipe it? [y/n]: y |
| Wiping linux_raid_member signature on /dev/vg_test_01/lv_test_01. |
| WARNING: ext4 signature detected on /dev/vg_test_01/lv_test_01 at offset 1080. Wipe it? [y/n]: y |
| Wiping ext4 signature on /dev/vg_test_01/lv_test_01. |
| Logical volume "lv_test_01"created. |
| [root@centos7 ~]$ lvremove /dev/vg_test_01/lv_test_01 |
| |
| Do you really want to remove active logical volume vg_test_01/lv_test_01? [y/n]: y |
| Logical volume "lv_test_01"successfully removed |
注意: 如果这个 lv 被挂载了。 请先 umount
| [root@centos7 ~]$ lvcreate -L 2G -n lv_test_01 vg_test_01 |
| Logical volume "lv_test_01" created. |
| [root@centos7 ~]$ mkfs.ext4 /dev/vg_test_01/lv_test_01 |
| [root@centos7 ~]$ mkdir/mnt/test_01 |
| [root@centos7 ~]$ mount/dev/vg_test_01/lv_test_01 /mnt/test_01/ |
上面我们的测试是 vg 是 4G,我们的创建的 lv 是 2G 的,我们想给创建的 lv 扩展 1G 空间。
| [root@centos7 ~]$ lvextend -L +1G /dev/vg_test_01/lv_test_01 |
| Size of logical volume vg_test_01/lv_test_01 changed from 2.00GiB (512extents) to 3.00GiB (768 extents). |
| Logical volume vg_test_01/lv_test_01 successfully resized. |
| [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 0993M 0% /dev/shm |
| tmpfs 993M 22M 971M 3% /run |
| tmpfs 993M 0993M 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 0100% /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 |
| meta-data=/dev/mapper/vg_test_01-lv_test_01 isize=512agcount=4, agsize=131072 blks |
| = sectsz=512attr=2, projid32bit=1= crc=1finobt=0spinodes=0 |
| data = bsize=4096blocks=524288, imaxpct=25= sunit=0swidth=0 blks |
| naming =version 2bsize=4096ascii-ci=0ftype=1 |
| log =internal bsize=4096blocks=2560, version=2= sectsz=512sunit=0blks, lazy-count=1 |
| realtime =none extsz=4096blocks=0, rtextents=0 |
| data blocks changed from 524288to 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 0993M 0% /dev/shm |
| tmpfs 993M 22M 971M 3% /run |
| tmpfs 993M 0993M 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 0100% /run/media/root/CentOS 7 x86_64 |
| /dev/mapper/vg_test_01-lv_test_01 3.0G 33M 3.0G 2% /mnt/test_01 |
注意:
- ext 系列可以使用 resieze2fs 去调整。其他的文件系统可以参考 http://tldp.org/HOWTO/LVM-HOWTO/extendlv.html
lvextentd 有个 - r 选项,自动去调整大小,匹配 lv 和文件系统大小的。
逻辑卷的大小可以减小,也可以增加。 但是,在缩小卷本身之前,请记住缩小文件系统的大小或存在于卷中的任何文件是非常重要的,否则可能会丢失数据。
注意: xfs,jsf 文件系统是没法缩减 lvm 的,因为这 2 个文件系统没法缩减文件系统大小的。参考 http://tldp.org/HOWTO/LVM-HOWTO/reducelv.html
主要步骤:
- 检查空间使用情况和分析
- umount 设备
- 缩减文件系统大小
- 缩减 lv 大小
- 恢复挂载设备
上面我们的逻辑卷是 3G 的大小,在缩减之前我们需要确保缩减的空间不能影响数据的存放的。
| [root@centos7 ~]$ cp-a /etc/ /mnt/test_01/ |
| [root@centos7 ~]$ du-sh/mnt/test_01/ |
| 42M /mnt/test_01/ |
| [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 0993M 0% /dev/shm |
| tmpfs 993M 22M 971M 3% /run |
| tmpfs 993M 0993M 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 0100% /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 |
| 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/196608files (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 262144blocks long. |
| [root@centos7 ~]$ lvreduce -L 1G /dev/vg_test_01/lv_test_01 |
| 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.00GiB (768extents) to 1.00GiB (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. 140root root 12288Dec 713:54 etc |
| drwx------. 2root root 16384Dec 715:47lost+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 0993M 0% /dev/shm |
| tmpfs 993M 22M 971M 3% /run |
| tmpfs 993M 0993M 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 0100% /run/media/root/CentOS 7 x86_64 |
| /dev/mapper/vg_test_01-lv_test_01 944M 52M 825M 6% /mnt/test_01 |
移动 vg 到另外一个机器上
主要步骤:
- 检查和分析
- 一些预处理工作
- umount 文件系统
- 设置卷组非活动
- 导出卷组
- 取出设备,插入到新机器
- 导入卷组
- 激活卷组
- 挂载文件系统
| [root@centos7 ~]$ vgs |
| VG |
| vg_test_01 410wz--n- 3.98g 2.98g |
| [root@centos7 ~]$ pvs |
| l PV VG Fmt Attr PSize PFree |
| /dev/sdb1 vg_test_01 lvm2 a-- 1020.00m0/dev/sdb2 lvm2 --- 1.00g 1.00g |
| /dev/sdc1 vg_test_01 lvm2 a-- 1020.00m1016.00m/dev/sdc2 lvm2 --- 1.00g 1.00g |
| /dev/sdd1 vg_test_01 lvm2 a-- 1020.00m1020.00m/dev/sdd2 lvm2 --- 5.00g 5.00g |
| /dev/sde1 vg_test_01 lvm2 a-- 1020.00m1020.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.00m1020.00m/dev/sdb2 lvm2 --- 1.00g 1.00g |
| /dev/sdc1 vg_test_01 lvm2 a-- 1020.00m1016.00m/dev/sdc2 lvm2 --- 1.00g 1.00g |
| /dev/sdd1 vg_test_01 lvm2 a-- 1020.00m0/dev/sdd2 vg_test_01 lvm2 a-- <5.00g <5.00g |
| /dev/sde1 vg_test_01 lvm2 a-- 1020.00m1020.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. |
| [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 |
| Volume group "vg_test_01" has active logical volumes |
| [root@centos7 ~]$ vgchange -an vg_test_01 |
| 0logical volume(s) involume group "vg_test_01" now active |
| [root@centos7 ~]$ vgexport vg_test_01 |
| Volume group "vg_test_01" successfully exported |
| [root@centos7 ~]$ sync |
| [root@centos7 ~]$ sync |
| [root@centos7 ~]$ sync |
| [root@centos7 ~]$ sync |
我这里把这个磁盘取出来安装到另外一个机器上面。下面的一部分操作在新机器上执行。注意啦。
| [root@centos6 ~]$ /git/Bash/other/scan_scsi_device.sh |
| 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 |
| NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT |
| sr0 11:013.7G 0rom /media/CentOS_6.9_Final |
| sr1 11:118.1G 0rom /media/CentOS 7 x86_64 |
| sda 8:00200G 0 disk |
| ├─sda1 8:101G 0part /boot |
| ├─sda2 8:2048.8G 0part / |
| ├─sda3 8:3019.5G 0part /app |
| ├─sda5 8:502G 0 part [SWAP] |
| ├─sda4 8:402M 0 part |
| └─sda6 8:601G 0 part |
| sdb 8:160100G 0 disk |
| ├─sdb1 8:1701G 0 part |
| ├─sdb2 8:18020G 0 part |
| ├─sdb3 8:1901G 0 part |
| ├─sdb4 8:2001K 0 part |
| ├─sdb5 8:21020G 0 part |
| ├─sdb6 8:2201G 0 part |
| └─sdb7 8:2301G 0 part |
| sr2 11:213.7G 0rom /media/CentOS_6.9_Final_ |
| sr3 11:312G 0rom /media/CentOS_6.9_Final__ |
| sdc 8:32020G 0 disk |
| ├─sdc1 8:3301G 0 part |
| └─sdc2 8:3405G 0part |
脚本内容如下
| [root@centos6 ~]$ cat /git/Bash/other/scan_scsi_device.sh |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| echo"start scan"fori in`find/sys/devices/ -name scan`; doecho"- - -"> $i |
| echo $i scan finished. |
| doneecho"end scan"echo-e "\033[32mplease use lsblk check\033[0m" |
| [root@centos6 ~]$ vgscan |
| Reading all physical volumes. This may take a while... |
| Found exported volume group "vg_test_01" using metadata type lvm2 |
| [root@centos6 ~]$ pvscan |
| PV /dev/sdc1 is inexported VG vg_test_01 [1020.00MiB / 0free] |
| PV /dev/sdc2 is inexported VG vg_test_01 [5.00GiB / 4.99GiB free] |
| Total: 2[5.99GiB] / inuse: 2[5.99GiB] / inno VG: 0[0 ] |
| [root@centos6 ~]$ vgimport vg_test_01 |
| Volume group "vg_test_01" successfully imported |
| [root@centos6 ~]$ vgs |
| VG |
| vg_test_01 210wz--n- 5.99g 4.99g[root@centos6 ~]$ vgchange -ay vg_test_01 |
| 1logical volume(s) involume 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/ |
分析下当前环境,我把 vg 移动到 centos6 上了。一下操作在 centos6 上执行的。
| [root@centos6 ~]$ vgs |
| VG |
| vg_test_01 210wz--n- 5.99g 4.99g |
| [root@centos6 ~]$ pvs |
| PV VG Fmt Attr PSize PFree |
| /dev/sdc1 vg_test_01 lvm2 a--u 1020.00m0/dev/sdc2 vg_test_01 lvm2 a--u 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 |
| 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-0206:11:56+0800 |
| LV snapshot status active destination for lv_test_01 |
| LV Status available |
| |
| 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 setto 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/ |
| 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/ |
| 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 |