lost and found ( for me ? )

KVM : share files between KVM host and VM with virtio-9p

KVM host info

[ root@f18 ~]# qemu-kvm --version
QEMU emulator version 1.4.1, Copyright (c) 2003-2008 Fabrice Bellard
[root@f18 ~]# libvirtd --version
libvirtd (libvirt) 1.0.4

[root@f18 ~]# uname -ri
3.9.0-1.vanilla.mainline.knurd.1.fc18.x86_64 x86_64

[root@f18 ~]# cat /etc/fedora-release
Fedora release 18 (Spherical Cow)

I have disabled SElinux on both the KVM host and the VM.

on the KVM host , make a directory to share files between KVM host and VM.
[root@f18 ~]# mkdir /tmp/share

edit VM’s xml file.
add red lines
virsh # dumpxml f18-vm1
<domain type='kvm' id='16'>
 <memory unit='KiB'>1048576</memory>
 <currentMemory unit='KiB'>1048576</currentMemory>
 <vcpu placement='static'>1</vcpu>
   <type arch='x86_64' machine='pc-i440fx-1.4'>hvm</type>
   <boot dev='hd'/>
 <clock offset='utc'/>
   <disk type='file' device='disk'>
     <driver name='qemu' type='qcow2'/>
     <source file='/var/run/media/hattori/fedora-HDD/KVM_images/f18-vm1.qcow2'/>
     <target dev='vda' bus='virtio'/>
     <alias name='virtio-disk0'/>
     <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
   <disk type='block' device='cdrom'>
     <driver name='qemu' type='raw'/>
     <target dev='hdc' bus='ide'/>
     <alias name='ide0-1-0'/>
     <address type='drive' controller='0' bus='1' target='0' unit='0'/>
   <controller type='usb' index='0'>
     <alias name='usb0'/>
     <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
   <controller type='ide' index='0'>
     <alias name='ide0'/>
     <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
   <controller type='virtio-serial' index='0'>
     <alias name='virtio-serial0'/>
     <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
   <filesystem type='mount' accessmode='squash'>
     <source dir='/tmp/share'/>
     <target dir='/hostshare'/>
     <alias name='fs0'/>
     <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
   <interface type='bridge'>
     <mac address='52:54:00:22:d0:b9'/>
     <source bridge='br0'/>
     <target dev='vnet0'/>
     <model type='virtio'/>
     <alias name='net0'/>
     <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
   <serial type='pty'>
     <source path='/dev/pts/1'/>
     <target port='0'/>
     <alias name='serial0'/>
   <console type='pty' tty='/dev/pts/1'>
     <source path='/dev/pts/1'/>
     <target type='serial' port='0'/>
     <alias name='serial0'/>
   <channel type='spicevmc'>
     <target type='virtio' name='com.redhat.spice.0'/>
     <alias name='channel0'/>
     <address type='virtio-serial' controller='0' bus='0' port='1'/>
   <input type='tablet' bus='usb'>
     <alias name='input0'/>
   <input type='mouse' bus='ps2'/>
   <graphics type='spice' port='5900' autoport='yes' listen=''>
     <listen type='address' address=''/>
     <model type='qxl' ram='65536' vram='65536' heads='1'/>
     <alias name='video0'/>
     <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
   <memballoon model='virtio'>
     <alias name='balloon0'/>
     <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
 <seclabel type='none'/>

virsh #

You can add filesystem via virt-manager

start the VM

virsh # start f18-vm1

on the KVM host

[root@f18 ~]# ps aux | grep qemu | grep -v grep
qemu     25013  6.2  3.9 3713064 310872 ?      Sl   01:20   0:31 /usr/bin/qemu-system-x86_64 -machine accel=kvm -name f18-vm1 -S -M pc-i440fx-1.4 -m 1024 -smp 1,sockets=1,cores=1,threads=1 -uuid 533a5533-ec0c-e327-4ca6-8d25c1a1e610 -no-user-config -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/f18-vm1.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc -no-shutdown -device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 -device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x4 -drive file=/var/run/media/hattori/fedora-HDD/KVM_images/f18-vm1.qcow2,if=none,id=drive-virtio-disk0,format=qcow2 -device virtio-blk-pci,scsi=off,bus=pci.0,addr=0x5,drive=drive-virtio-disk0,id=virtio-disk0,bootindex=1 -drive if=none,id=drive-ide0-1-0,readonly=on,format=raw -device ide-cd,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0 -fsdev local,security_model=none,id=fsdev-fs0,path=/tmp/share -device virtio-9p-pci,id=fs0,fsdev=fsdev-fs0,mount_tag=/hostshare,bus=pci.0,addr=0x7 -netdev tap,fd=23,id=hostnet0,vhost=on,vhostfd=24 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:22:d0:b9,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -chardev spicevmc,id=charchannel0,name=vdagent -device virtserialport,bus=virtio-serial0.0,nr=1,chardev=charchannel0,id=channel0,name=com.redhat.spice.0 -device usb-tablet,id=input0 -spice port=5900,addr=,disable-ticketing,seamless-migration=on -vga qxl -global qxl-vga.ram_size=67108864 -global qxl-vga.vram_size=67108864 -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x6

in the VM , mount the 9p filesystem from the KVM host using the virtio transport
To use file share between KVM host and the VM ,9p module is required.

tag /hostshare points to KVM host’s /tmp/share directory and that has been mounted on VM’s /root/tmp

on the VM
virsh # console f18-vm1
Connected to domain f18-vm1
Escape character is ^]

[root@localhost ~]#

[root@localhost ~]# mkdir /root/tmp
[root@localhost ~]# mount -t 9p -o trans=virtio,version=9p2000.L /hostshare /root/tmp

[root@localhost ~]# mount | grep 9p
/hostshare on /root/tmp type 9p (rw,relatime,sync,dirsync,trans=virtio,version=9p2000.L)

[root@localhost ~]# lsmod | grep 9p
9p                     53725  1
fscache                60427  1 9p
9pnet_virtio           13466  1
9pnet                  73543  2 9p,9pnet_virtio

on the KVM host , make a file
[root@f18 ~]# cd /tmp/share/
[root@f18 share]# echo hello > hello.txt

on the VM
hello.txt which was created on the KVM host has been shared under the VM’s /root/tmp directory.
[root@localhost tmp]# pwd

[root@localhost tmp]# ls

[root@localhost tmp]# cat hello.txt

on the KVM host
[root@localhost ~]# umount /hostshare

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.