Plan 9 can be used to mount a host folder inside a virtual machine using libvirt, however there are very few solutions to mount the entire guest filesystem on the host machine. Scenarios could potentially include batch-editing configuration files.
In order to accomplish the former plain old NFSv3 can be used alongside autofs. The result will be that all guest virtual machines will be mapped by hostname under a subdirectory on the host machine that can be then used conventionally on the command line in order to access the guest filesystem.
The setup requires both the server and client component to be configured - the guests will act as NFS servers and the client will be the host.
Under Debian, the required packages for the host are the following:
nfs-common
autofs
that can be installed by issuing:
aptitude install autofs nfs-common
The client machine requires the nfs-server
package to be installed:
aptitude install nfs-server
The rest of the tutorial assumes that each guest virtual machine has at least a locally accessible hostname (or IP address).
In order to configure the NFS server, edit /etc/exports
and append the line:
/ HOST(rw,sync,no_subtree_check,no_root_squash)
where:
HOST
is the hostname of the machine hosting the virtual machines,rw
means that the root filesystem will be exported with read-write permissions,no_subtree_check
prevents extensive subdirectory checks,no_root_squash
allows the host to write to the exported root filesystem.and then issue:
exportfs -arv
which should indicate that the entire root filesystem has been exported to the specified hostname.
Under Debian, with the autofs
package installed, create a file at /etc/auto.master.d/nfs.autofs
with the following contents:
/media/nfs /etc/auto.net --timeout=300 --ghost
where:
/media/nfs
is the directory under which guest machine filesystems could be accessed by hostname,/etc/auto.net
is a script that will use the showmount
command to check which folders can be mounted when the hostname is accessed via its hostname,–timeout=300
implies that mounting the guest filesystem will timeout after 300
seconds (5 minutes) and,–ghost
means that AutoFS will create empty folders when guest filesystems cannot be mounted.
With the configuration in place, restart autofs
with:
/etc/init.d/autofs restart
With the guest NFS export and AutoFS configured on the host, the guest filesystem can be mounted just by accessing the path:
/media/nfs/HOST
where:
/media/nfs
is the path specified on the host in the file /etc/auto.master.d/nfs.autofs
,HOST
is the hostname of the guest machine.For instance, issuing:
vi /media/nfs/machine.home/etc/issue
will open the file /etc/issue
on the guest machine.
Well, with no_root_squash
, the entire guest filesystem will be exported under the designated mount point (in this case, /media/nfs/HOST
) such that any user on the host will have read-write access to the entire guest filesystem. Perhaps the best options is to use NFSv4 and OpenLDAP to map users instead of using the no_root_squash
option.
NFS can be configured to use UID and GID to map users, respectively groups such that exported filesystems will be modifiable by the local users in case they are the same. This allows for a secure and more fluent administration where additional and superfluous users do not have to be created on every machine to be configured.
To use OpenLDAP, the host machine will have to be configured to host an LDAP server which can be done by following the OpenLDAP tutorial.
Once the OpenLDAP server is configured on the host and the guest is configured to allow authentication of LDAP users (make sure to check that LDAP users are able to log in), NFS can be configured to use LDAP to map users.
Edit /etc/idmapd.conf
and instruct NFS to use nsswitch to map users by adding the following lines:
[Translation] Method = nsswitch
The configuration will make NFS query the methods detailed in /etc/nsswitch.conf
to authenticate and map users - which was one of the points of the OpenLDAP tutorial.