From d91cd305634bfe0de200cb0b062a56349249d26e Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Sat, 27 Oct 2007 16:51:55 +0000 Subject: * Detect whether chroot / bind-mount support is available. --- configure.ac | 5 +++++ src/libstore/build.cc | 25 +++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index 350f8791fc1c..5d8a719133da 100644 --- a/configure.ac +++ b/configure.ac @@ -99,6 +99,11 @@ static char buf[1024];]], AC_LANG_POP(C++) +# Check for chroot support (requires chroot() and bind mounts). +AC_CHECK_FUNCS([chroot]) +AC_CHECK_HEADERS([sys/mount.h]) + + # Check for AC_LANG_PUSH(C++) AC_CHECK_HEADERS([locale]) diff --git a/src/libstore/build.cc b/src/libstore/build.cc index 6e2b748618b7..533883dc5298 100644 --- a/src/libstore/build.cc +++ b/src/libstore/build.cc @@ -24,6 +24,15 @@ #include #include +#include "config.h" + +#if HAVE_SYS_MOUNT_H +#include +#endif + + +#define CHROOT_ENABLED HAVE_CHROOT && HAVE_SYS_MOUNT_H && defined(MS_BIND) + namespace nix { @@ -583,9 +592,6 @@ void deletePathWrapped(const Path & path) ////////////////////////////////////////////////////////////////////// -#include - - /* Helper RAII class for automatically unmounting bind-mounts in chroots. */ struct BindMount @@ -613,6 +619,7 @@ struct BindMount void bind(const Path & source, const Path & target) { +#if CHROOT_ENABLED debug(format("bind mounting `%1%' to `%2%'") % source % target); this->source = source; @@ -622,10 +629,12 @@ struct BindMount if (mount(source.c_str(), target.c_str(), "", MS_BIND, 0) == -1) throw SysError(format("bind mount from `%1%' to `%2%' failed") % source % target); +#endif } void unbind() { +#if CHROOT_ENABLED if (source == "") return; debug(format("unmount bind-mount `%1%'") % target); @@ -658,6 +667,7 @@ struct BindMount } source = ""; +#endif } }; @@ -1644,8 +1654,9 @@ void DerivationGoal::startBuilder() /* Are we doing a chroot build? */ useChroot = queryBoolSetting("build-use-chroot", false); Path tmpRootDir; - + if (useChroot) { +#if CHROOT_ENABLED /* Create a temporary directory in which we set up the chroot environment using bind-mounts. @@ -1683,6 +1694,10 @@ void DerivationGoal::startBuilder() guarantee that list elements are destroyed in order?) */ for (Paths::iterator i = dirsInChroot.begin(); i != dirsInChroot.end(); ++i) bindMounts.push_front(boost::shared_ptr(new BindMount(*i, tmpRootDir + *i))); + +#else + throw Error("chroot builds are not supported on this platform"); +#endif } @@ -1710,6 +1725,7 @@ void DerivationGoal::startBuilder() try { /* child */ +#if CHROOT_ENABLED /* If building in a chroot, do the chroot right away. initChild() will do a chdir() to the temporary build directory to make sure the current directory is in the @@ -1718,6 +1734,7 @@ void DerivationGoal::startBuilder() same directories.) */ if (useChroot && chroot(tmpRootDir.c_str()) == -1) throw SysError(format("cannot change root directory to `%1%'") % tmpRootDir); +#endif initChild(); -- cgit 1.4.1