Share host filsystem to guest using virtfs 9p filesystem passthrough

Virtfs is a virtualization aware filesystem passthrough..Sharing host files on the guest through generic network file systems like NFS and CIFS suffer from major performance and feature deficiencies as these protocols are not designed or optimized for virtualization. To address the needs of
the virtualization paradigm, a new paravirtualized file system called VirtFS has been intorduced. Virtfs takes features of 9p2000.L protocol.

Using virtfs, you can share filesystem of host to the guest system. It works via para virtual transport mechanism called ‘virtio’. Virtfs has been implemented with ‘zero copy’ mechanism. This is a key advantage of VirtFS compared to using a network file system which would always require at least one (but usually more) data copies.

There are mainly 2 components in this implementation.

1)  virtio-9p-pci device ::

This will be used to transport protocol messages and data between the host
and the guest.

2) ‘fsdev’ device :

This is used to define the export file system characteristics like file
system type, security model .

For more about the implementation details,please  look at reference section..

Below is demo of this feature in “qemu” , “kvm”, “libvirt” environment.

Host system details : Fedora 15 :

[root@humbles-lap Desktop]$ cat /etc/redhat-release
Fedora release 15 (Lovelock)
[root@humbles-lap Desktop]$ uname -r
2.6.43.5-2.fc15.x86_64

Guest System details:  Fedora 16:

[root@localhost mnt]# cat /etc/redhat-release
Fedora release 16 (Verne)
[root@localhost mnt]# uname -r
3.1.0-7.fc16.x86_64
[root@localhost mnt]#

First of all, check support for above mentioned components ( virtio-9p-pci & fsdev) in host system qemu-kvm binary..

Host:

[root@humbles-lap Desktop]$ qemu-kvm –help|grep virtfs
-virtfs local,path=path,mount_tag=tag,security_model=[mapped|passthrough|none]

[root@humbles-lap Desktop]$ qemu-kvm device ?

name “virtio-9p-pci”, bus PCI

[root@humbles-lap Desktop]# lsmod |grep virt
9pnet_virtio           13416  0
9pnet                  73543  1 9pnet_virtio
virtio_net             23133  0
[root@humbles-lap Desktop]#

Guest Kernel has to support 9p:

[root@humbles-lap bin]#  grep 9P /boot/config-$(uname -r)
CONFIG_NET_9P=m
CONFIG_NET_9P_VIRTIO=m
CONFIG_NET_9P_RDMA=m
# CONFIG_NET_9P_DEBUG is not set
CONFIG_9P_FS=m
CONFIG_9P_FSCACHE=y
CONFIG_9P_FS_POSIX_ACL=y

IN HOST SYSTEM:
—————————————————-

The below string has to be specified with ‘qemu-kvm’ process to export a ‘virtfs’ filesystem for guest.

-virtfs local,path=[path to share],security_model=[mapped|passthrough|
none],mount_tag=[mount tag]

Where : Path = Sharing filesystem path
security_model: This can be either “mapped” or “passthrough”..
mount_tag: This is the idenitifier which can be used in guest when moutning.

The easiest way for defining this is, placing it in the guest configuration file..


< source dir='/misc/shared'/>

< address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>

After placing this in “guest configuration” file:

Start the guest :

ex: virsh create Fedora-16.xml

Check for “qemu-kvm” process to know whether “virtfs” options has been picked properly..

[root@humbles-lap Desktop]# ps aux |grep qemu-kvm
qemu      7473  0.8  8.0 1356940 314552 ?      Sl   17:00   0:27 /usr/bin/qemu-kvm -S -M pc-0.14 -enable-kvm -m 1024 -smp 1,sockets=1,cores=1,threads=1 -name Fedora-16 -uuid 75d12600-d3c5-9bb1-7d0a-5ed0e550f5ee -nodefconfig -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/Fedora-16.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc -boot c -drive file=/var/lib/libvirt/images/Fedora-16.img,if=none,id=drive-virtio-disk0,boot=on,format=raw -device virtio-blk-pci,bus=pci.0,addr=0x4,drive=drive-virtio-disk0,id=virtio-disk0 -drive if=none,media=cdrom,id=drive-ide0-1-0,readonly=on,format=raw -device ide-drive,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0 -fsdev local,security_model=passthrough,id=fsdev-fs0,path=/misc/shared -device virtio-9p-pci,id=fs0,fsdev=fsdev-fs0,mount_tag=test_mount,bus=pci.0,addr=0x6 -netdev tap,fd=24,id=hostnet0,vhost=on,vhostfd=25 -device virtio-net-pci,netdev=hostnet0,id=net0,mac=52:54:00:80:db:8d,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -usb -device usb-tablet,id=input0 -vnc 127.0.0.1:0 -vga cirrus -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x5

IN GUEST SYSTEM:

You have to mount the shared filesystem using “9p” . Use below command for the same:

#mount -t 9p -o trans=virtio test_mount /mnt

Where,

Filesyste type: 9p
Transport mechanism: virtio
Mount tag: This is the tag ‘test_mount’ which used when sharing from host system.
Mount Point: /mnt

Check for filesystem :

HOST

[root@humbles-lap shared]# pwd
/misc/shared
[root@humbles-lap shared]# echo “Hi Guest” –> virtfs_file
[root@humbles-lap shared]#

GUEST:

[root@localhost mnt]# mount |grep test_mount
test_mount on /mnt type 9p (rw,relatime,sync,dirsync,trans=virtio)
[root@localhost mnt]# pwd
/mnt
[root@localhost mnt]# cat virtfs_file
Hi Guest

Reference: www.kernel.org/doc/ols/2010/ols2010-pages-109-120.pdf

Nested virtualization with KVM/VMX OR Guest inside guest in kvm in fedora linux

I would like to explain “nested Virtualization” in my own terms . How-ever it is already documented , so I am just quoting some bits from the same.

On Intel processors, KVM uses Intel’s VMX (Virtual-Machine eXtensions)
to easily and efficiently run guest operating systems. Normally, these guests
*cannot* themselves be hypervisors running their own guests, because in VMX,
guests cannot use VMX instructions.

The “Nested VMX” feature adds this missing capability – of running guest
hypervisors (which use VMX) with their own nested guests. It does so by
allowing a guest to use VMX instructions, and correctly and efficiently
emulating them using the single level of VMX available in the hardware.

Terminology
———–

Single-level virtualization has two levels – the host (KVM) and the guests.
In nested virtualization, we have three levels: The host (KVM), which we call
L0, the guest hypervisor, which we call L1, and its nested guest, which we
call L2.

Running nested VMX
——————

The nested VMX feature is disabled by default. It can be enabled by giving
the “nested=1” option to the kvm-intel module.

No modifications are required to user space (qemu). However, qemu’s default
emulated CPU type (qemu64) does not list the “VMX” CPU feature, so it must be
explicitly enabled, by giving qemu one of the following options:

     -cpu host              (emulated CPU has all features of the real CPU)

     -cpu qemu64,+vmx       (add just the vmx feature to a named CPU type)

Instead of explaining a lot of theory about it, I will demo its use:

[cc]

//Checking whether “kvm_intel” will support this parameter?

[root@humbles-lap Humble]# modinfo kvm_intel

filename:       /lib/modules/2.6.43.5-2.fc15.x86_64/kernel/arch/x86/kvm/kvm-intel.ko

license:        GPL

author:         Qumranet

depends:        kvm

intree:         Y

vermagic:       2.6.43.5-2.fc15.x86_64 SMP mod_unload 

parm:           vpid:bool

parm:           flexpriority:bool

parm:           ept:bool

parm:           unrestricted_guest:bool

parm:           emulate_invalid_guest_state:bool

parm:           vmm_exclusive:bool

parm:           yield_on_hlt:bool

parm:           fasteoi:bool

parm:           nested:bool

parm:           ple_gap:int

parm:           ple_window:int

// This feature is "disabled" by default..

[root@humbles-lap Humble]# cat /sys/module/kvm_intel/parameters/nested 

N

// Remove the module

[root@humbles-lap Humble]# rmmod kvm_intel

[root@humbles-lap Humble]# lsmod |grep kvm_intel

[root@humbles-lap Humble]#

// Make the 'nested' boolean "ON" and reload:

[root@humbles-lap Humble]# insmod /lib/modules/2.6.43.5-2.fc15.x86_64/kernel/arch/x86/kvm/kvm-intel.ko nested=Y

[root@humbles-lap Humble]# lsmod |grep intel

kvm_intel             132406  0 

snd_hda_intel          33276  3 

snd_hda_codec         115767  3 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel

snd_pcm                97170  4 snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec

snd                    78908  14 snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_seq,snd_seq_device,snd_pcm,snd_timer

kvm                   407039  1 kvm_intel

snd_page_alloc         18101  2 snd_hda_intel,snd_pcm

intel_ips              18304  0

[root@humbles-lap Humble]# cat /sys/module/kvm_intel/parameters/nested 

Y

[root@humbles-lap Humble]#

 

[/cc]

Now, if you are configuring a virtual machine or guest as another hypervisor, you should have virtualization support in its processor. For that you have to export your CPU as "vmx" enabled one. You can achieve this by editing guest configuration file and then starting the vm.

Below is my guest configuration file wrt to the CPU session . The important field here is ""

 

[cc]

[root@humbles-lap Humble]# cat /etc/libvirt/qemu/rhel5.4-x86_64-kvm.xml |grep -w cpu -A 13

  <cpu match='exact'>

    <model>Westmere</model>

    <vendor>Intel</vendor>

    

    <feature policy='require' name='est'/>

    <feature policy='require' name='monitor'/>

    <feature policy='require' name='ss'/>

    <feature policy='require' name='vme'/>

    

    <feature policy='require' name='ht'/>

    <feature policy='require' name='ds'/>

    

    <feature policy='require' name='tm'/>

    

--

  </cpu>

 

[/cc]

//Then I started this VM via "virsh" command..

[root@humbles-lap Humble]# virsh create /etc/libvirt/qemu/rhel5.4-x86_64-kvm.xml
Domain rhel5.4-x86_64-kvm created from /etc/libvirt/qemu/rhel5.4-x86_64-kvm.xml

[root@humbles-lap Humble]#

// Check the 'qemu-kvm' process for "+vmx" flag

[root@humbles-lap Humble]# ps aux |grep -i qemu-kvm |grep vmx
qemu      8389 50.6  1.3 872040 53908 ?        Sl   23:26   0:06 /usr/bin/qemu-kvm -S -M pc-0.13 -cpu core2duo,+lahf_lm,+rdtscp,+aes,+popcnt,+sse4.2,+sse4.1,+xtpr,+cx16,+tm2,+est,+vmx,+ds_cpl,+pbe,+tm,+ht,+ss,+acpi,+ds -enable-kvm -m 512 -mem-prealloc -mem-path /dev/hugepages/libvirt/qemu -smp 2,sockets=2,cores=1,threads=1 -name rhel5.4-x86_64-kvm -uuid b2d13137-ff17-d505-0a6b-fb79c867584a -nodefconfig -nodefaults -chardev socket,id=charmonitor,path=/var/lib/libvirt/qemu/rhel5.4-x86_64-kvm.monitor,server,nowait -mon chardev=charmonitor,id=monitor,mode=control -rtc base=utc -boot c -drive file=/misc/guest-images/rhel5.4-x86_64-kvm.img,if=none,id=drive-ide0-0-0,format=raw -device ide-drive,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0 -netdev tap,fd=21,id=hostnet0 -device rtl8139,netdev=hostnet0,id=net0,mac=54:52:00:6c:6e:70,bus=pci.0,addr=0x3 -chardev pty,id=charserial0 -device isa-serial,chardev=charserial0,id=serial0 -usb -vnc 127.0.0.1:0 -k en-us -vga cirrus -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x4
[root@humbles-lap Humble]#

Once "VM" started, try to login to that VM and see whether its processor support virtualization..

 

[cc]

[Humble@humbles-lap ~]$ ssh root@192.168.122.125
root@192.168.122.125's password:
Last login: Thu Jun 14 23:28:20 2012
[root@dhcp208-238 ~]# cat /proc/cpuinfo |grep -i vmx
flags        : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc pni vmx ssse3 cx16 sse4_1 sse4_2 popcnt lahf_lm
flags        : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc pni vmx ssse3 cx16 sse4_1 sse4_2 popcnt lahf_lm

[/cc]

Now you have to believe me that this VM can act as a host/hypervisor system for another VM.

 

As you do in a host system, try to create VM inside this guest and play.

 

Reference# nested-vmx.txt in kernel source

Explore “Qemu Tracing ” ..

Qemu provides a new “tracing” mechanism which can be used in conjuction with several backends.. Especially useful to make use of it via “system Tap” which shipped with most of the distros..   The trace points are really useful when debugging or when doing performance analysis. The trace events can be enabled/disabled at run time.. …

Read more

Difference between KVM and RHEV/OVIRT

Does the subject make any sense? hmmmm.. I don’t know. It is up to your decision because this is one of the questions which I get normally from people when they hear about Ovirt/RHEV ( RHEV is downstream/product offering from Red Hat based on upstream or community project called ovirt) or if you explain about it.

I will try to put these components in simple terms, if I fail, poke me with your comments.

KVM is a hypervisor, well yeah.. strictly speaking, I have to tell that, KVM makes/turns a Linux system as a hypervisor. Thus allows you to run virtual machines inside it. Sure, maybe you have heard that .. but then what is ‘libvirt’?

Libvirt is an API that allows you to talk with your hypervisor. The applications like ‘virt-manager can take help from Libvirt to talk to the hypervisor ( ex: KVM). Thus manage virtual machine life cycle ie nothing but creation, managing and additional features like migration..etc.

From the above explanation, you may understand that, if you have a Linux kernel running in your system/machine KVM can host VMs ( virtual machines), and applications like ‘virt-manager’ can be used to administer it with the help of a library or API called libvirt.

Then, how Ovirt/RHEV is related to KVM? well, if you take a picture of an Ovirt/RHEV environment in my mind, it would look like this.

[Hypervisor 1] <——> [ Ovirt Manager/RHEV-Manager] <——-> [Hypervisor 2]

How these hypervisors are made? again KVM makes this hypervisor .. These hypervisors contact/talk to RHEV/Ovirt-Manager server. What is the communication channel between these hypervisors and Ovirt/RHEV-manager? It is a service called “vdsm” Ok.. sounds good. In that case, no “libvirt”? hmmm.. RHEV3 or the latest version of Ovirt makes use of Libvirt service. In RHEV3 context “vdsm”, trigger libvirt for VM life cycle operations. but still, vdsm is the communication agent between ovirt/rhev hypervisor and RHEV-Manager/Ovirt-Manager

RHEV hypervisor can be a RHEL system with KVM modules loaded. Otherwise a ‘live CD’ image called RHEV-H can act as a hypervisor. RHEV-H does not differ from a RHEL hypervisor in terms of KVM or vdsm. Just think RHEV-H as a live CD image with minimal size. It is a dedicated appliance and not supposed to run other applications like RHEL hypervisor.

We talked about Ovirt-Manager/RHEV-Manager Or Ovirt-M/RHEV-M, but what is it?

In simple terms, it is the managing server. Think of it in terms of an ‘administration’ server. In fact a GUI installed server 🙂

Finally, Do you want a ‘windows server’ in a RHEV environment? hmmm.. RHEV-Manager in RHEV 2 required it., but the latest version of RHEV-3/Ovirt does not require the ‘windows’ server. You can install RHEV3/ovirt Manager in a Linux system with a simple command,

#rhev-m setup

I am stopping here.. but please feel free to ask your queries, if any. I would try to explain it in detail. This is a simple wrap up on KVM and RHEV/Ovirt…

ps# RHEV2 setup was not using ‘libvirt’ in RHEV hypervisor,

rhevm-manage-domains command to configure Active Directory or IPA in RHEV3

Unlike early rhev3 beta releases, the authentication server configuration ( Active Directory or IPA)  has been moved to seperate utility called ‘rhevm-manage-domains’. Installing rhev3 is that easy now.. It is as simple as that, firing ‘rhevm-setup’ command in a linux system.. Refer: rhevm-setup Once you ran above command, you have to configure authentication setup ( …

Read more

error: Requested operation is not valid: Disk .. does not support snapshotting

When I first tried virsh snapshot, I too got the same error. But it was easy to understand why I got that. It is nothing but the image format was “raw’. The snapshot feature depends on qemu to perform its operation.. For the qemu to work , the image format should be “qcow2“.. Now, a …

Read more

How to access/retrieve guest data from host system in kvm?

Earlier it was bit difficult to access data from guest image as it required serious attention on the process. But today it is much much easier using ‘guestmount’ binary. Thanks to libguestfs & fuse. Below snip from my Fedora system will prove my words.. [root@humbles-lap /]# which guestmount /usr/bin/guestmount [root@humbles-lap /]# rpm -qf /usr/bin/guestmount libguestfs-mount-1.8.13-1.fc14.x86_64 …

Read more