2

I'm currently working on an embedded system design and my test development machine is currently a VM running in KVM. Is it possible to have KVM mount a directory (like a chroot) and use that as the root file system for the VM?

I currently have the root filesystem in a single sparse file, and I can mount it via loop to a directory, but when making and testing changes it would be much easier to have the root fs in a directory structure on my local machine.

tMC
  • 1,175
  • 1
  • 9
  • 12

1 Answers1

2

If your virtual machine has IP connectivity, mount its root filesystem over NFS. (You'll need to have the NFS client driver and its dependencies in the kernel or initrd/initramfs.)

On the host, install an NFS server and export the directory by declaring it in /etc/exports.

/path/to/root 10.0.9.0/24(ro,async,no_subtree_check)

On the guest, read nfsroot.txt in the kernel documentation; in a nutshell, the kernel command line should contain something like

root=/dev/nfs nfsroot=10.0.9.1:/path/to/root

If sharing the directory tree during the VM's run time isn't an absolute requirement, and all you're after is conveniently regenerating your root filesystem before booting the VM, then it would be simple enough to write a small script or makefile that rebuilds the root filesystem image before booting. This is pretty common in embedded development. A convenient choice of root filesystem is initramfs, a variant of initrd. See also How to generate initramfs image with busybox links?.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • I thought about that, but my embedded kernel doesn't support networking- yet. – tMC May 16 '11 at 21:08
  • @tMC: Well then, what filesystem drivers are included in your embedded kernel? Whatever you end up using will have to have a kernel driver in the guest. – Gilles 'SO- stop being evil' May 16 '11 at 21:11
  • currently just the ext filesystems currently. I didn't know if there was a way to abstract the disk with some KVM fs driver or the like. The current design doesn't call for NFS in the VM, so if i add it, it would have to be a specific 'development build' – tMC May 16 '11 at 21:16
  • 1
    @tMC: The ext* filesystem drivers expect to have exclusive control over the underlying image. So it seems that you will have to do a specific development build if you want to use the same mounted hierarchy on both machines simultaneously. – Gilles 'SO- stop being evil' May 16 '11 at 21:30
  • @tMC: It occurs to me that the best solution for you is probably an initramfs. (And if you'd considered it and were looking for something more convenient, well, I don't think there is, given your requirements.) – Gilles 'SO- stop being evil' May 16 '11 at 23:15