During working on KVM Guest Linux machine, i need to transfer some files from Guest to Host machine.

In Linux environment it could be quit easy with various services available in Linux machine like NFS, Samba, FTP or HTTP that we could configure on Host/Guest machine and export file over same. but what when we have un-shareable network like in Guest machine is connected to VPN and can’t share files on network interfaces.
In such cases we need to understand that we need something which is independent to network and data could share on non-network interfaces. like we have one setup below

Host Machine ubuntu : 16.04 (Normal network)
Guest Machine centOS : CentOS7 (connected to VPN)

I can access Guest machine through serial console or through virt-manager graphical console only not any network console like SSH etc. But with serial console, i think we can’t access files and directories to Host to guest and vice versa.

Disk Setup on Guest

So here we will learn how can we access Guest qcow2 disk image on Host machine and mount it on Host machine.

We have below Disk and LVM created on Guest machine

File attached on KVM Guest form Host

#virsh domblklist srv7
Target     Source
------------------------------------------------
vda        /data/vmware_images/CentOS7/system.img

#qemu-img info /data/vmware_images/CentOS7/system.img
image: /data/vmware_images/CentOS7/system.img
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 2.4G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         srv7-27-Apr-2017          0 2017-04-27 11:20:18   00:00:00.000
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

Disk on Guest Machine

#virsh  console srv7  #accessing Guest console
Connected to domain srv7
Escape character is ^]

[root@srv7 ~]# pvs
  PV         VG     Fmt  Attr PSize  PFree
/dev/vda2  centos lvm2 a--  19.51g    0 
[root@srv7 ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree
centos  1   2   0 wz--n- 19.51g    0 
[root@srv7 ~]# lvs
  LV   VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
root centos -wi-ao---- 17.51g                                        
 swap centos -wi-ao----  2.00g     

[root@srv7 ~]# fdisk -l /dev/vda  Disk partition

Disk /dev/vda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00093fdd

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    41943039    20458496   8e  Linux LVM

With above output it is clear that we have one disk vda that has two partitions. out of two partition one partition has one VG (centos) and that VG has two LVM (root and swap). So here root LVM is mounted for root file-system as below

[root@srv7 ~]# df -hTP /dev/centos/root 
Filesystem              Type  Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs    18G  1.7G   16G  10% /

Connect Guest Disk file on Host

let’s start trying mounting it on Host machine with Network block device method as mentioned below.

For mounting it first we need to detect this device in Host machine. for same we need to connect it with qemu-nbd command in below mentioned way.

qemu-nbd --connect=/dev/nbd0 /data/vmware_images/CentOS7/system.img

Sometime i saw nbd module is not added in kernel modules. This need to be added in kernel modules as mentioned below.

#modprobe nbd

#lsmod| grep nbd
nbd                    20480  1

#modinfo nbd
filename:       /lib/modules/4.4.0-79-generic/kernel/drivers/block/nbd.ko
license:        GPL
description:    Network Block Device
srcversion:     C67096AF2AE3C738DBE0B7E
depends:        
intree:         Y
vermagic:       4.4.0-79-generic SMP mod_unload modversions 
parm:           nbds_max:number of network block devices to initialize (default: 16) (int)
parm:           max_part:number of partitions per device (default: 0) (int)

After connecting it you would able to see this disk in your system with fdisk command like below

#fdisk -l /dev/nbd0
Disk /dev/nbd0: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00093fdd

Device      Boot   Start      End  Sectors  Size Id Type
/dev/nbd0p1 *       2048  1026047  1024000  500M 83 Linux
/dev/nbd0p2      1026048 41943039 40916992 19.5G 8e Linux LVM

Mount Guest Disk on Host

Now you just need to detect this PV in your system and mount LV as per standard procedure like below.
First we have to scan PV with pvscan command , but sometime i noticed pvscan command not show new PV devices like below for same , we have to use –cache with pvscan command

#pvscan 
  PV /dev/sda2   VG Data_vg         lvm2 [99.90 GiB / 0    free]
  Total: 1 [99.90 GiB] / in use: 1 [99.90 GiB] / in no VG: 0 [0   ]

#pvscan --cache

#pvscan 
  PV /dev/nbd0p2   VG centos          lvm2 [19.51 GiB / 0    free]
  PV /dev/sda2     VG Data_vg         lvm2 [99.90 GiB / 0    free]
  Total: 2 [119.41 GiB] / in use: 2 [119.41 GiB] / in no VG: 0 [0   ]

Like above we could re-fresh again for system PV list and locate our new PVs. Further we could scan system VGs and mount required LVs in Host directories like below.

#vgscan 
  Reading all physical volumes.  This may take a while...
  Found volume group "centos" using metadata type lvm2
  Found volume group "Data_vg" using metadata type lvm2

#vgchange -ay 
  2 logical volume(s) in volume group "centos" now active
  1 logical volume(s) in volume group "Data_vg" now active

#lvscan 
  ACTIVE            '/dev/centos/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/centos/root' [17.51 GiB] inherit
  ACTIVE            '/dev/Data_vg/Data_vg_lv' [99.90 GiB] inherit

#mount /dev/centos/root /mnt

#df -hTP /mnt
Filesystem              Type  Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs    18G  1.7G   16G  10% /mnt

So in above way, we are able to connect Qcow2 format file and mount it in local Host directory.