module Propellor.Property.Mount where
import Propellor.Base
import Utility.Path
import Data.List
type FsType = String
type Source = String
type MountPoint = FilePath
newtype MountOpts = MountOpts [String]
deriving Monoid
class ToMountOpts a where
toMountOpts :: a -> MountOpts
instance ToMountOpts MountOpts where
toMountOpts = id
instance ToMountOpts String where
toMountOpts s = MountOpts [s]
formatMountOpts :: MountOpts -> String
formatMountOpts (MountOpts []) = "defaults"
formatMountOpts (MountOpts l) = intercalate "," l
mounted :: FsType -> Source -> MountPoint -> MountOpts -> Property UnixLike
mounted fs src mnt opts = property (mnt ++ " mounted") $
toResult <$> liftIO (mount fs src mnt opts)
bindMount :: FilePath -> FilePath -> Property Linux
bindMount src dest = tightenTargets $
cmdProperty "mount" ["--bind", src, dest]
`assume` MadeChange
`describe` ("bind mounted " ++ src ++ " to " ++ dest)
swapOn :: Source -> RevertableProperty Linux Linux
swapOn mnt = tightenTargets doswapon <!> tightenTargets doswapoff
where
swaps = lines <$> readProcess "swapon" ["--show=NAME"]
doswapon = check (notElem mnt <$> swaps) $
cmdProperty "swapon" [mnt]
doswapoff = check (elem mnt <$> swaps) $
cmdProperty "swapoff" [mnt]
mount :: FsType -> Source -> MountPoint -> MountOpts -> IO Bool
mount fs src mnt opts = boolSystem "mount" $
[ Param "-t", Param fs
, Param "-o", Param (formatMountOpts opts)
, Param src
, Param mnt
]
mountPoints :: IO [MountPoint]
mountPoints = lines <$> readProcess "findmnt" ["-rn", "--output", "target"]
isMounted :: MountPoint -> IO Bool
isMounted mnt = isJust <$> getFsType mnt
mountPointsBelow :: FilePath -> IO [MountPoint]
mountPointsBelow target = filter (\p -> simplifyPath p /= simplifyPath target)
. filter (dirContains target)
<$> mountPoints
getFsType :: MountPoint -> IO (Maybe FsType)
getFsType = findmntField "fstype"
getFsMountOpts :: MountPoint -> IO MountOpts
getFsMountOpts p = maybe mempty toMountOpts
<$> findmntField "fs-options" p
type UUID = String
getMountUUID :: MountPoint -> IO (Maybe UUID)
getMountUUID = findmntField "uuid"
getSourceUUID :: Source -> IO (Maybe UUID)
getSourceUUID = blkidTag "UUID"
type Label = String
getMountLabel :: MountPoint -> IO (Maybe Label)
getMountLabel = findmntField "label"
getSourceLabel :: Source -> IO (Maybe UUID)
getSourceLabel = blkidTag "LABEL"
getMountSource :: MountPoint -> IO (Maybe Source)
getMountSource = findmntField "source"
findmntField :: String -> FilePath -> IO (Maybe String)
findmntField field mnt = catchDefaultIO Nothing $
headMaybe . filter (not . null) . lines
<$> readProcess "findmnt" ["-n", mnt, "--output", field]
blkidTag :: String -> Source -> IO (Maybe String)
blkidTag tag dev = catchDefaultIO Nothing $
headMaybe . filter (not . null) . lines
<$> readProcess "blkid" [dev, "-s", tag, "-o", "value"]
umountLazy :: FilePath -> IO ()
umountLazy mnt =
unlessM (boolSystem "umount" [ Param "-l", Param mnt ]) $
stopPropellorMessage $ "failed unmounting " ++ mnt
unmountBelow :: FilePath -> IO ()
unmountBelow d = do
submnts <- mountPointsBelow d
forM_ submnts umountLazy