Share memory between a virtual machine and the host

12,219

Solution 1

It doesn't work that way: the virtual machine and the host don't share the same memory. That's why it's called a virtual machine. Shared memory, as you've been using it, is an OS-level concept; you can't use it to share memory with something that's outside the control of the (guest) OS.

In principle, the virtual machine technology could offer some way to share memory between the guest and the host. But that would defeat the purpose of virtualization: it would allow guest programs to escape the virtual machine.

If you want to share data between a virtual machine and its host, use a file on the host, on a directory that's mounted in the virtual machine (e.g. through vboxsf on VirtualBox); or more generally use a file somewhere that's accessible on both sides.

Solution 2

Actually you can do it, but you need some experience in embedded software development, you can use the concept of inter-VM communication, which is acheived by making a virtual-PCI device available to both host and guest. You can also look at some of the details in: [Updated] https://github.com/qemu/qemu/blob/master/docs/specs/ivshmem-spec.txt

Hope this will help! Thanks

Solution 3

Next to the others, here is an idea.

Shared memory (and the other sysvipc features) are mapped in linux into an internal filesystem. Effectively, if you create/attach/detach/delete a shared memory segment, the related system calls are using the filesystem api to make the correspoinding operations similarly, as if you would work on tmpfs files.

By, analogy, my idea would be: what if this "filesystem" would be a shared filesystem, for example NFS? Of course, you couldn't use the standard "shmat" and similar syscalls, but you could very easily implement trivial wrappers, which create, open, mmap, close and delete files on an nfs volume.

I don't think, that the required wrapper library would be longer as 10-20 kB.

And, of course, you can share these nfs volumes between your hosts and guests as you will.

This nfs volume can be backed up by a ramfs, a tmpfs or even by an orderly, disk-backed filesystem on the host (or even on any of the guests).

As a side-effect, you could do this even between guests running on different hosts.

Solution 4

You might be interested in LXD for this. It enables one to share both disk, memory and more between the host and the guests. Controlling and limiting resources is still possible by the way.

Solution 5

You can't; that is kind of the point of a virtual machine: it thinks it is the only OS running.

Share:
12,219

Related videos on Youtube

Chris M
Author by

Chris M

Updated on September 18, 2022

Comments

  • Chris M
    Chris M over 1 year

    Through shmget and shmat, I am able to access the data stored in one program from another program. Here's the gist of the code:

    key=ftok("shared.c",'c');
    shmid=shmget(key,1024,0644|IPC_CREAT);
    data=shmat(shmid,(void *)0,0);
    printf("Enter the data");
    gets(data);
    

    Similarly I can write another program and use shmat there to access the data.

    Now I wanted to know how can I access it from the host operating system. Since the shared memory id will be different in the host memory, shmat won't work. How can I access the shared memory from the host machine?

    can we do it in this way: we know that there exist a page table with respect to each operating system in a hypervisor which will maps the logical address to physical address,there is a pmap table which maps the physical address of the hypervisor with the physical address of the host machine,and also there exist shadow page tables in hypervisor which maps the logical guest address to host physical address. Is there any way of accessing the shadow page tables or pagetable corresponding to the OS

    • Nils
      Nils about 12 years
      Is this related to this question? If so, you should close the other question and tag this one with VirtualBox.
    • Nils
      Nils about 12 years
      Why do you want to do that?
  • Nils
    Nils about 12 years
    Other way around: He wants to access from the Host machine.
  • Nils
    Nils about 12 years
    Host - not Guest. This should be possible - e.g. VirtualBox runs in user space on the host.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 12 years
    @Nils Sure, it's technically possible. It requires some cooperation between the VM machinery and the guest OS. As far as I know, the major VM engines have no such feature because it's too much work for something that runs against the usual uses of VMs.
  • Chris M
    Chris M about 12 years
    can we do like this :we know that there exist a page table with respect to each operating system in a hypervisor which will maps the logical address to physical address,there is a pmap table which maps the physical address of the hypervisor with the physical address of the host machine,and also there exist shadow page tables in hypervisor which maps the logical guest address to host physical address. Is there any way of accessing the shadow page tables or pagetable corresponding to the OS
  • peterh
    peterh about 8 years
    Right, good answer. A single remark: "This alignment may change without notice after an upgrade of the VM technology." <- Yes, and this is why it should be part of the virtualization API, both on the host and on the guest.
  • Rui F Ribeiro
    Rui F Ribeiro almost 8 years
    Have you heard about redis and memcached?
  • peterh
    peterh almost 8 years
    @RuiFRibeiro Redis not, memcached yes. But I think memcached is a purely application-level thing, essentially a fast, memory-backed hashing database.
  • Rui F Ribeiro
    Rui F Ribeiro almost 8 years
    Granted. The point is that in the past I also incurred in the mistake of setting up memory sharing with shm, in scenarios where redis or memcached would have been sufficient.