summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2018-07-16 14:26:16 -0400
committerAndrew Cady <d@jerkface.net>2018-07-16 14:26:16 -0400
commitedac3168ff43f8eef8e4e62ae8b1d65020a27335 (patch)
treefa50aad7dc38d6cb6a09ae406ef928028a9e4821
parentd66f1b658186356df2d3c40994853da596c2b1e5 (diff)
use cgroup container for chroot command
-rw-r--r--fsmgr.hs28
1 files changed, 26 insertions, 2 deletions
diff --git a/fsmgr.hs b/fsmgr.hs
index e715b3b..c1813d7 100644
--- a/fsmgr.hs
+++ b/fsmgr.hs
@@ -23,6 +23,7 @@ import qualified Options.Applicative as Opt
23 ;import Options.Applicative hiding (action) 23 ;import Options.Applicative hiding (action)
24 24
25import ConfigFile 25import ConfigFile
26import System.Directory (createDirectoryIfMissing)
26import System.Posix.Process (getProcessID) 27import System.Posix.Process (getProcessID)
27 28
28noParent :: BaseImageSpecification -> Bool 29noParent :: BaseImageSpecification -> Bool
@@ -139,6 +140,12 @@ run :: Options -> IO ()
139run (Options (Build (BuildOpts target))) = shakeBuildOneImage target 140run (Options (Build (BuildOpts target))) = shakeBuildOneImage target
140run (Options (Chroot (ChrootOpts target args))) = chrootImage target args 141run (Options (Chroot (ChrootOpts target args))) = chrootImage target args
141 142
143useCGroups :: Bool
144useCGroups = True -- TODO: make command-line option
145
146handle' :: IO b -> Action a -> Action a
147handle' = flip actionOnException
148
142chrootImage :: FilePath -> [String] -> IO () 149chrootImage :: FilePath -> [String] -> IO ()
143chrootImage target args = 150chrootImage target args =
144 shake shakeOptions {shakeFiles = "_build"} $ do 151 shake shakeOptions {shakeFiles = "_build"} $ do
@@ -153,8 +160,15 @@ chrootImage target args =
153 cmd_ "btrfstune -S0 -f" tmp 160 cmd_ "btrfstune -S0 -f" tmp
154 cmd_ "mkdir" [mnt] 161 cmd_ "mkdir" [mnt]
155 cmd_ "mount -t btrfs" [tmp, mnt] 162 cmd_ "mount -t btrfs" [tmp, mnt]
156 cmd_ (WithStderr False) (WithStdout False) "chroot" (mnt:args) -- TODO: cgroup 163 let (umount :: IO ()) = do
157 cmd_ "umount" [mnt] -- TODO: recursive umount 164 cmd_ "umount" [mnt]
165 cmd_ "rm -f" [tmp]
166 handle' umount $
167 if useCGroups
168 then liftIO $ cgroupChroot ("fsmgr" <.> takeFileName target) mnt args
169 else cmd_ (WithStderr False) "chroot" (mnt : args) -- TODO: cgroup
170 cmd_ "umount" [mnt]
171 cmd_ "sync"
158 cmd_ "btrfstune -S1" tmp 172 cmd_ "btrfstune -S1" tmp
159 cmd_ "mv" [tmp, inp] 173 cmd_ "mv" [tmp, inp]
160 174
@@ -186,6 +200,16 @@ filepat ~%> act = phonys f
186 f file | (filepat ?== file) = Just (act file) 200 f file | (filepat ?== file) = Just (act file)
187 f _ = Nothing 201 f _ = Nothing
188 202
203cgroupChroot :: String -> FilePath -> [String] -> IO ()
204cgroupChroot groupName mnt [] = cgroupChroot groupName mnt ["/bin/bash"]
205cgroupChroot groupName mnt args = do
206 let cgdir = "/sys/fs/cgroup/pids" </> groupName
207 createDirectoryIfMissing False cgdir
208 cmd_ (Cwd mnt) (WithStderr False)
209 "unshare --ipc --uts --cgroup --mount --pid --fork chroot ."
210 "sh -exc" ["mount -t proc proc /proc; mount -t devpts devpts /dev/pts; exec \"$@\""]
211 "sh" args
212
189shakeRules :: Rules () 213shakeRules :: Rules ()
190shakeRules = do 214shakeRules = do
191 "_build/*.yaml.canon" %> \out -> do 215 "_build/*.yaml.canon" %> \out -> do