Posted on Wednesday, 10th February 2010 by Balazs

Problem

An OpenVZ host can share any part of its filesystem with any guest operating system running on it. This post describes how to manually mount a filesystem tree from the host OpenVZ box to a guest, and how to automate the process. We will also address an error that can happen with a correctly configured file preventing the automated mount process.

Solution

Manual mount

To mount a filesystem tree to any guest from the host, run the following:
mount --bind /path/to/tree/on/host ${VE_ROOT}/path/to/tree/on/guest

You need to replace ${VE_ROOT} with /var/lib/vz/root/<guest id>

Automate the mount

There is two options to automate the mount: automation for each guest and automation for a specific guest. The file looks the same either way. For setting up a mount on each guest, use vps.mount and vps.umount as the file names. To use automated mount for a specific guest, use the the filename that is <guest id>.mount and <guest id>.umount. For example, if your container id for your virtual private server (guest) is 123, then the specific files to set the mount and unmount code for are 123.mount and 123.umount. This is how the .mount file needs to look:

#!/bin/bash
source /etc/vz/vz.conf
source ${VE_CONFFILE}
echo "Starting ${VEID}.mount..."
mount --bind /path/to/tree/on/host ${VE_ROOT}/path/to/tree/on/guest
echo "...${VEID}.mount complete"

And this is how the .umount file needs to look:

#!/bin/bash
source /etc/vz/vz.conf
source ${VE_CONFFILE}
echo "Starting ${VEID}.umount..."
umount ${VE_ROOT}/path/to/tree/on/guest
echo "...${VEID}.umount done"

Note that the echo is necessairy, otherwise, the filesystem will most likely not mount correctly. This is due to the fact that on start and restart the vzctl program will call umount first, and if that script fails (returns a non zero exit code) the .mount script isn’t called. Thus, if the directory isn’t mounted and there isn’t an echo at the end of the script, the script returns the exit code of the last command which in this case is the exit code from the failed umount command.

References

Share and Enjoy:
  • Print
  • LinkedIn
  • Facebook
  • FriendFeed
  • Twitter
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google Bookmarks
  • Slashdot
  • Yahoo! Buzz
  • Yahoo! Bookmarks
  • RSS
  • Ping.fm
  • email
  • PDF

Tags: , , ,
Posted in Linux | Comments (Comments)

blog comments powered by Disqus