已索引

How do I move a Volume Group from one system to another?

SOLUTION VERIFIED - Updated December 7 2014 at 7:32 AM - English

Environment

Red Hat Enterprise Linux & LVM

Issue

How do I move a Volume Group from one system to another?

Resolution

To move a whole volume group from one system to a new system, the vgexport and vgimport commands can be used.

Note: These steps are only for the local disk, not for the Directl Lun mapped to the system.

When vgexport or vgimport is used, it is not necessary to move drives from one system to another. It is an administrative policy tool to prevent access to volumes while moving them to another system.

In this example we will run through the steps to move a volume group named design from one system to another. A logical volume from this volume group is initially mounted at /mnt/design/users:

源主机上操作

Umount the file system ( If required make use of "rescue" mode ). Make sure that no users are accessing files on the active volume.

# umount /mnt/design/users

Mark the volume group inactive. Marking the volume group inactive removes it from the kernel and prevents any further activity on it.

# vgchange -an design
vgchange -- volume group "design" successfully deactivated

Export the volume group. This prevents it from being accessed on the "old" host system and prepares it to be removed.

# vgexport design
vgexport -- volume group "design" successfully exported

When the machine is shut down, the disk can be unplugged and then connected to it's new machine.

目标主机上操作

When plugged into the new system it becomes /dev/sdb so an initial pvscan shows:

# pvscan

pvscan -- reading all physical volumes (this may take a while...)
pvscan -- inactive PV "/dev/sdb1"  is in EXPORTED VG "design" [996 MB / 996 MB free]
pvscan -- inactive PV "/dev/sdb2"  is in EXPORTED VG "design" [996 MB / 244 MB free]
pvscan -- total: 2 [1.95 GB] / in use: 2 [1.95 GB] / in no VG: 0 [0]

Import the volume group.

  • If importing on an LVM 2 system, run:
# vgimport design
Volume group "design" successfully imported
  • If importing on an LVM 1 system, add the PVs that need to be imported:
# vgimport design /dev/sdb1 /dev/sdb2

vgimport -- doing automatic backup of volume group "design"
vgimport -- volume group "design" successfully imported and activated

Activate the volume group.

# vgchange -ay design

Mount the file system.

# mkdir -p /mnt/design/users
# mount /dev/design/users /mnt/design/users

To make the change persistent after a system reboot put the entry in /etc/fstab file.

案例记录

一台主机因为开发人员不懂Linux系统,按照网上的文章强制安装了 gcc 相关的一堆软件包,严重破坏了系统的 GLIBC 库,ssh 服务和 yum/rpm 等命令都不可用,报错信息类似如下:
rpm: /lib64/libc.so.6: version 'GLIBC_2.14' not found (required by /usr/lib64/libz.so.1)

计划给开发人员重新部署一台测试用机,但是要保留原主机下的应用程序文件。

于是将原主机的磁盘挂载到新主机上后,lsblk可以看到新磁盘有两个分区:

[root@newhost ~]# lsblk
NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sr0                        11:0    1  1024M  0 rom  
sda                         8:0    0   200G  0 disk 
├─sda1                      8:1    0   512M  0 part /boot
├─sda2                      8:2    0    32G  0 part [SWAP]
└─sda3                      8:3    0 167.5G  0 part 
  ├─VG-LogVol_root (dm-0) 253:0    0    99G  0 lvm  /
  └─VG-LogVol_tmp (dm-1)  253:1    0    20G  0 lvm  /tmp
sdb                         8:16   0   100G  0 disk 
├─sdb1                      8:17   0   500M  0 part 
└─sdb2                      8:18   0  79.5G  0 part 

尝试挂载故障主机磁盘时提示是 LVM 磁盘:

[root@newhost ~]# mkdir /mnt/tmp
[root@newhost ~]# mount /dev/sdb2 /mnt/tmp
mount: unknown filesystem type 'LVM2_member'

检查当时 LVM 的状态,未发现故障主机磁盘相关的LVM信息:

[root@newhost ~]# vgs
  VG   #PV #LV #SN Attr   VSize   VFree 
  VG     1   2   0 wz--n- 167.50g 48.50g
[root@newhost ~]# 
[root@newhost ~]# pvs
  PV         VG   Fmt  Attr PSize   PFree 
  /dev/sda3  VG   lvm2 a--  167.50g 48.50g

执行 pvscan 进行发现操作:

[root@newhost ~]# pvscan 
  PV /dev/sdb2   VG rootvg   lvm2 [79.50 GiB / 0    free]
  PV /dev/sda3   VG VG       lvm2 [167.50 GiB / 48.50 GiB free]
  Total: 2 [247.00 GiB] / in use: 2 [247.00 GiB] / in no VG: 0 [0   ]

[root@newhost ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda3  VG     lvm2 a--  167.50g 48.50g
  /dev/sdb2  rootvg lvm2 a--   79.50g     0 


[root@newhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree 
  VG       1   2   0 wz--n- 167.50g 48.50g
  rootvg   1   2   0 wz--n-  79.50g     0 


[root@newhost ~]# lvs
  LV          VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  LogVol_root VG     -wi-ao---- 99.00g                                                    
  LogVol_tmp  VG     -wi-ao---- 20.00g                                                    
  lv_root     rootvg -wi------- 63.50g                                                    
  lv_swap     rootvg -wi------- 16.00g                       

现在已经发现了 LVM,但是还不能使用:

[root@newhost ~]# mount /dev/rootvg/lv_root /mnt/tmp/
mount: you must specify the filesystem type

[root@newhost ~]# ls /dev/rootvg/lv_root
ls: cannot access /dev/rootvg/lv_root: No such file or directory

把 VG 激活后就可以使用了:

[root@newhost ~]# vgchange -ay rootvg
  2 logical volume(s) in volume group "rootvg" now active


[root@newhost ~]# ls /dev/rootvg/lv_root
/dev/rootvg/lv_root


[root@newhost ~]# mount /dev/rootvg/lv_root /mnt/tmp/


[root@newhost ~]# df -hP
Filesystem                  Size  Used Avail Use% Mounted on
/dev/mapper/VG-LogVol_root   98G   12G   81G  13% /
tmpfs                       7.8G     0  7.8G   0% /dev/shm
/dev/sda1                   488M   32M  432M   7% /boot
/dev/mapper/VG-LogVol_tmp    20G  433M   19G   3% /tmp
/dev/mapper/rootvg-lv_root   63G   26G   34G  43% /mnt/tmp

总结起来就是两条命令:先 pvscan 发现,再 vgchange -ay 激活就可以使用了。

-- By 许望(RHCA、OCM、VCP)
最后修改:2022 年 01 月 29 日 03 : 00 PM
如果觉得我的文章对你有用,请随意赞赏