Copyright | (c) Nickolay Kudasov 2014 |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | nickolay.kudasov@gmail.com |
Safe Haskell | None |
Language | Haskell2010 |
Internal module to provide a set of functions to create,
control and manage LXC containers.
Normally you should import System.LXC
module only.
- type ContainerCreateFn = Ptr C'lxc_container -> CString -> CString -> Ptr C'bdev_specs -> CInt -> Ptr CString -> IO CBool
- mkCreateFn :: FunPtr ContainerCreateFn -> ContainerCreateFn
- type ContainerCloneFn = Ptr C'lxc_container -> CString -> CString -> CInt -> CString -> CString -> C'uint64_t -> Ptr CString -> IO (Ptr C'lxc_container)
- mkCloneFn :: FunPtr ContainerCloneFn -> ContainerCloneFn
- type ContainerBoolFn = Ptr C'lxc_container -> IO CBool
- mkBoolFn :: FunPtr ContainerBoolFn -> ContainerBoolFn
- type ContainerStringFn = Ptr C'lxc_container -> IO CString
- mkStringFn :: FunPtr ContainerStringFn -> ContainerStringFn
- type ContainerProcessIDFn = Ptr C'lxc_container -> IO C'pid_t
- mkProcessIDFn :: FunPtr ContainerProcessIDFn -> ContainerProcessIDFn
- type ContainerStringBoolFn = Ptr C'lxc_container -> CString -> IO CBool
- mkStringBoolFn :: FunPtr ContainerStringBoolFn -> ContainerStringBoolFn
- type ContainerBoolBoolFn = Ptr C'lxc_container -> CBool -> IO CBool
- mkBoolBoolFn :: FunPtr ContainerBoolBoolFn -> ContainerBoolBoolFn
- type ContainerStartFn = Ptr C'lxc_container -> CInt -> Ptr CString -> IO CBool
- mkStartFn :: FunPtr ContainerStartFn -> ContainerStartFn
- type ContainerShutdownFn = Ptr C'lxc_container -> CInt -> IO CBool
- mkShutdownFn :: FunPtr ContainerShutdownFn -> ContainerShutdownFn
- type ContainerClearConfigFn = Ptr C'lxc_container -> IO ()
- mkClearConfigFn :: FunPtr ContainerClearConfigFn -> ContainerClearConfigFn
- type ContainerGetRunningConfigItemFn = Ptr C'lxc_container -> CString -> IO CString
- mkGetRunningConfigItemFn :: FunPtr ContainerGetRunningConfigItemFn -> ContainerGetRunningConfigItemFn
- type ContainerGetItemFn = Ptr C'lxc_container -> CString -> CString -> CInt -> IO CInt
- mkGetItemFn :: FunPtr ContainerGetItemFn -> ContainerGetItemFn
- type ContainerSetItemFn = Ptr C'lxc_container -> CString -> CString -> IO CBool
- mkSetItemFn :: FunPtr ContainerSetItemFn -> ContainerSetItemFn
- type ContainerGetInterfacesFn = Ptr C'lxc_container -> IO (Ptr CString)
- mkGetInterfacesFn :: FunPtr ContainerGetInterfacesFn -> ContainerGetInterfacesFn
- type ContainerGetIPsFn = Ptr C'lxc_container -> CString -> CString -> CInt -> IO (Ptr CString)
- mkGetIPsFn :: FunPtr ContainerGetIPsFn -> ContainerGetIPsFn
- type ContainerWaitFn = Ptr C'lxc_container -> CString -> CInt -> IO CBool
- mkWaitFn :: FunPtr ContainerWaitFn -> ContainerWaitFn
- type ContainerSnapshotFn = Ptr C'lxc_container -> CString -> IO CInt
- mkSnapshotFn :: FunPtr ContainerSnapshotFn -> ContainerSnapshotFn
- type ContainerSnapshotListFn = Ptr C'lxc_container -> Ptr (Ptr C'lxc_snapshot) -> IO CInt
- mkSnapshotListFn :: FunPtr ContainerSnapshotListFn -> ContainerSnapshotListFn
- type ContainerConsoleGetFDFn = Ptr C'lxc_container -> Ptr CInt -> Ptr CInt -> IO CInt
- mkConsoleGetFDFn :: FunPtr ContainerConsoleGetFDFn -> ContainerConsoleGetFDFn
- type ContainerConsoleFn = Ptr C'lxc_container -> CInt -> CInt -> CInt -> CInt -> CInt -> IO CInt
- mkConsoleFn :: FunPtr ContainerConsoleFn -> ContainerConsoleFn
- type ContainerAttachFn = Ptr C'lxc_container -> C_lxc_attach_exec_t -> Ptr () -> Ptr C'lxc_attach_options_t -> Ptr C'pid_t -> IO CInt
- mkAttachFn :: FunPtr ContainerAttachFn -> ContainerAttachFn
- type ContainerAttachRunWaitFn = Ptr C'lxc_container -> Ptr C'lxc_attach_options_t -> CString -> Ptr CString -> IO CInt
- mkAttachRunWaitFn :: FunPtr ContainerAttachRunWaitFn -> ContainerAttachRunWaitFn
- type SnapshotFreeFn = Ptr C'lxc_snapshot -> IO ()
- mkFreeFn :: FunPtr SnapshotFreeFn -> SnapshotFreeFn
- newtype LXC a = LXC {}
- lxc :: (Ptr C'lxc_container -> IO a) -> LXC a
- withContainer :: MonadIO m => Container -> LXC a -> m a
- data LXCError = LXCError {}
- prettyLXCError :: LXCError -> String
- data CloneOption
- data CreateOption
- cloneFlag :: Num a => CloneOption -> a
- createFlag :: Num a => CreateOption -> a
- data Snapshot = Snapshot {}
- data Container = Container {}
- newC'lxc_container :: Container -> IO (Ptr C'lxc_container)
- peekC'lxc_container :: Ptr C'lxc_container -> IO (String -> Container)
- withC'lxc_container :: Container -> (Ptr C'lxc_container -> IO a) -> IO a
- data ContainerState
- parseState :: String -> ContainerState
- printState :: ContainerState -> String
- data BDevSpecs = BDevSpecs {}
- withC'bdev_specs :: BDevSpecs -> (Ptr C'bdev_specs -> IO a) -> IO a
- type Field s a = Ptr s -> Ptr a
- mkFn :: (FunPtr (Ptr s -> a) -> Ptr s -> a) -> Field s (FunPtr (Ptr s -> a)) -> Ptr s -> IO a
- boolFn :: Field C'lxc_container (FunPtr ContainerBoolFn) -> LXC Bool
- stringBoolFn :: Field C'lxc_container (FunPtr ContainerStringBoolFn) -> Maybe String -> LXC Bool
- boolBoolFn :: Field C'lxc_container (FunPtr ContainerBoolBoolFn) -> Bool -> LXC Bool
- getItemFn :: Field C'lxc_container (FunPtr ContainerGetItemFn) -> String -> LXC (Maybe String)
- setItemFn :: Field C'lxc_container (FunPtr ContainerSetItemFn) -> String -> Maybe String -> LXC Bool
- setItemFn' :: Field C'lxc_container (FunPtr ContainerSetItemFn) -> String -> String -> LXC Bool
- getDaemonize :: LXC Bool
- getLastError :: LXC (Maybe LXCError)
- isDefined :: LXC Bool
- isRunning :: LXC Bool
- state :: LXC ContainerState
- freeze :: LXC Bool
- unfreeze :: LXC Bool
- initPID :: LXC (Maybe ProcessID)
- loadConfig :: Maybe FilePath -> LXC Bool
- start :: Bool -> [String] -> LXC Bool
- stop :: LXC Bool
- wantDaemonize :: Bool -> LXC Bool
- wantCloseAllFDs :: Bool -> LXC Bool
- configFileName :: LXC (Maybe FilePath)
- wait :: ContainerState -> Int -> LXC Bool
- setConfigItem :: String -> String -> LXC Bool
- destroy :: LXC Bool
- saveConfig :: FilePath -> LXC Bool
- rename :: String -> LXC Bool
- reboot :: LXC Bool
- shutdown :: Int -> LXC Bool
- clearConfig :: LXC ()
- getConfigItem :: String -> LXC (Maybe String)
- getRunningConfigItem :: String -> LXC (Maybe String)
- getKeys :: String -> LXC [String]
- getInterfaces :: LXC [String]
- getIPs :: String -> String -> Word32 -> LXC [String]
- getCGroupItem :: String -> LXC (Maybe String)
- setCGroupItem :: String -> String -> LXC Bool
- clearConfigItem :: String -> LXC Bool
- getConfigPath :: LXC FilePath
- setConfigPath :: FilePath -> LXC Bool
- clone :: Maybe String -> Maybe FilePath -> [CloneOption] -> Maybe String -> Maybe String -> Maybe Word64 -> [String] -> LXC (Maybe Container)
- consoleGetFD :: Maybe Int -> LXC (Maybe (Int, Int, Int))
- console :: Maybe Int -> Fd -> Fd -> Fd -> Int -> LXC Bool
- attach :: AttachExecFn -> AttachCommand -> AttachOptions -> LXC (Maybe ProcessID)
- attachRunWait :: AttachOptions -> String -> [String] -> LXC (Maybe ExitCode)
- snapshot :: Maybe FilePath -> LXC (Maybe Int)
- peekC'lxc_snapshot :: Ptr C'lxc_snapshot -> IO Snapshot
- snapshotList :: LXC [Snapshot]
- snapshotRestore :: String -> String -> LXC Bool
- snapshotDestroy :: String -> LXC Bool
- mayControl :: LXC Bool
- addDeviceNode :: FilePath -> Maybe FilePath -> LXC Bool
- removeDeviceNode :: FilePath -> Maybe FilePath -> LXC Bool
- create :: String -> Maybe String -> Maybe BDevSpecs -> [CreateOption] -> [String] -> LXC Bool
- getRef :: Ptr C'lxc_container -> IO Bool
- dropRef :: Ptr C'lxc_container -> IO (Maybe Bool)
- getWaitStates :: IO [ContainerState]
- getGlobalConfigItem :: String -> IO (Maybe String)
- getVersion :: IO String
- listContainersFn :: (CString -> Ptr (Ptr CString) -> Ptr (Ptr (Ptr C'lxc_container)) -> IO CInt) -> Maybe String -> IO [Container]
- listDefinedContainers :: Maybe String -> IO [Container]
- listActiveContainers :: Maybe String -> IO [Container]
- listAllContainers :: Maybe String -> IO [Container]
- logClose :: IO ()
Documentation
type ContainerCreateFn = Ptr C'lxc_container -> CString -> CString -> Ptr C'bdev_specs -> CInt -> Ptr CString -> IO CBool Source
type ContainerCloneFn = Ptr C'lxc_container -> CString -> CString -> CInt -> CString -> CString -> C'uint64_t -> Ptr CString -> IO (Ptr C'lxc_container) Source
type ContainerBoolFn = Ptr C'lxc_container -> IO CBool Source
type ContainerStringFn = Ptr C'lxc_container -> IO CString Source
type ContainerProcessIDFn = Ptr C'lxc_container -> IO C'pid_t Source
type ContainerStringBoolFn = Ptr C'lxc_container -> CString -> IO CBool Source
type ContainerBoolBoolFn = Ptr C'lxc_container -> CBool -> IO CBool Source
type ContainerStartFn = Ptr C'lxc_container -> CInt -> Ptr CString -> IO CBool Source
type ContainerShutdownFn = Ptr C'lxc_container -> CInt -> IO CBool Source
type ContainerClearConfigFn = Ptr C'lxc_container -> IO () Source
type ContainerGetRunningConfigItemFn = Ptr C'lxc_container -> CString -> IO CString Source
mkGetRunningConfigItemFn :: FunPtr ContainerGetRunningConfigItemFn -> ContainerGetRunningConfigItemFn Source
type ContainerGetItemFn = Ptr C'lxc_container -> CString -> CString -> CInt -> IO CInt Source
type ContainerSetItemFn = Ptr C'lxc_container -> CString -> CString -> IO CBool Source
type ContainerGetInterfacesFn = Ptr C'lxc_container -> IO (Ptr CString) Source
type ContainerGetIPsFn = Ptr C'lxc_container -> CString -> CString -> CInt -> IO (Ptr CString) Source
type ContainerWaitFn = Ptr C'lxc_container -> CString -> CInt -> IO CBool Source
type ContainerSnapshotFn = Ptr C'lxc_container -> CString -> IO CInt Source
type ContainerSnapshotListFn = Ptr C'lxc_container -> Ptr (Ptr C'lxc_snapshot) -> IO CInt Source
type ContainerConsoleGetFDFn = Ptr C'lxc_container -> Ptr CInt -> Ptr CInt -> IO CInt Source
type ContainerConsoleFn = Ptr C'lxc_container -> CInt -> CInt -> CInt -> CInt -> CInt -> IO CInt Source
type ContainerAttachFn = Ptr C'lxc_container -> C_lxc_attach_exec_t -> Ptr () -> Ptr C'lxc_attach_options_t -> Ptr C'pid_t -> IO CInt Source
type ContainerAttachRunWaitFn = Ptr C'lxc_container -> Ptr C'lxc_attach_options_t -> CString -> Ptr CString -> IO CInt Source
type SnapshotFreeFn = Ptr C'lxc_snapshot -> IO () Source
LXC container-related computations.
.LXC
~ ReaderT
(String
, Ptr
C'lxc_container
) IO
Run
computations using LXC
awithContainer
.
withContainer :: MonadIO m => Container -> LXC a -> m a Source
LXC error structure.
LXCError | |
|
prettyLXCError :: LXCError -> String Source
Pretty print LXC error message.
data CloneOption Source
Options for clone
operation.
CloneKeepName | Do not edit the rootfs to change the hostname. |
CloneKeepMacAddr | Do not change the MAC address on network interfaces. |
CloneSnapshot | Snapshot the original filesystem(s). |
CloneKeepBDevType | Use the same bdev type. |
CloneMaybeSnapshot | Snapshot only if bdev supports it, else copy. |
CloneMaxFlags | Number of |
data CreateOption Source
Options for create
operation.
CreateQuiet | Redirect |
CreateMaxFlags | Number of |
cloneFlag :: Num a => CloneOption -> a Source
Turn CloneOption
into a bit flag.
createFlag :: Num a => CreateOption -> a Source
Turn CreateOption
into a bit flag.
An LXC container snapshot.
Snapshot | |
|
Container object.
Container | |
|
newC'lxc_container :: Container -> IO (Ptr C'lxc_container) Source
Allocate a new lxc_container
.
peekC'lxc_container :: Ptr C'lxc_container -> IO (String -> Container) Source
withC'lxc_container :: Container -> (Ptr C'lxc_container -> IO a) -> IO a Source
Marshal Container
to lxc_container
using temporary storage.
data ContainerState Source
Container state.
ContainerStopped | Container is stopped. |
ContainerStarting | Container is starting. |
ContainerRunning | Container is running. |
ContainerStopping | Container is stopping. |
ContainerAborting | Container is aborting. |
ContainerFreezing | Container is freezing. |
ContainerFrozen | Container is frozen. |
ContainerThawed | Container is thawed. |
ContainerOtherState String | Container is in some other state. |
parseState :: String -> ContainerState Source
Parse state as string representation.
printState :: ContainerState -> String Source
Get string representation of a state.
Specifications for how to create a new backing store.
BDevSpecs | |
|
withC'bdev_specs :: BDevSpecs -> (Ptr C'bdev_specs -> IO a) -> IO a Source
Marshal Haskell BDevSpecs
into C structure using temporary storage.
- the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.
mkFn :: (FunPtr (Ptr s -> a) -> Ptr s -> a) -> Field s (FunPtr (Ptr s -> a)) -> Ptr s -> IO a Source
boolFn :: Field C'lxc_container (FunPtr ContainerBoolFn) -> LXC Bool Source
stringBoolFn :: Field C'lxc_container (FunPtr ContainerStringBoolFn) -> Maybe String -> LXC Bool Source
boolBoolFn :: Field C'lxc_container (FunPtr ContainerBoolBoolFn) -> Bool -> LXC Bool Source
getItemFn :: Field C'lxc_container (FunPtr ContainerGetItemFn) -> String -> LXC (Maybe String) Source
setItemFn :: Field C'lxc_container (FunPtr ContainerSetItemFn) -> String -> Maybe String -> LXC Bool Source
setItemFn' :: Field C'lxc_container (FunPtr ContainerSetItemFn) -> String -> String -> LXC Bool Source
getDaemonize :: LXC Bool Source
Whether container wishes to be daemonized.
getLastError :: LXC (Maybe LXCError) Source
Get last container's error.
Determine if /var/lib/lxc/$name/config
exists.
True
if container is defined, else False
.
state :: LXC ContainerState Source
Determine state of container.
:: Maybe FilePath | Full path to alternate configuration file, or |
-> LXC Bool |
|
Load the specified configuration for the container.
:: Bool | Use |
-> [String] | Array of arguments to pass to init. |
-> LXC Bool |
|
Start the container.
Determine if the container wants to run disconnected from the terminal.
:: Bool | Value for the |
-> LXC Bool |
|
Determine whether container wishes all file descriptors to be closed on startup.
configFileName :: LXC (Maybe FilePath) Source
Return current config file name.
:: ContainerState | State to wait for. |
-> Int | Timeout in seconds. |
-> LXC Bool |
|
Wait for container to reach a particular state.
- A timeout of
-1
means wait forever. A timeout0
means do not wait.
Set a key/value configuration option.
Delete the container.
True
on success, else False
.
- NOTE: Container must be stopped and have no dependent snapshots.
Save configuaration to a file.
Rename a container.
Request the container reboot by sending it SIGINT
.
True
if reboot request successful, else False
.
:: Int | Seconds to wait before returning false. ( |
-> LXC Bool |
|
Request the container shutdown by sending it SIGPWR
.
clearConfig :: LXC () Source
Completely clear the containers in-memory configuration.
Retrieve the value of a config item.
Retrieve the value of a config item from running container.
Retrieve a list of config item keys given a key prefix.
getInterfaces :: LXC [String] Source
Obtain a list of network interfaces.
:: String | Network interface name to consider. |
-> String | Network family (for example |
-> Word32 | IPv6 scope id (ignored if family is not "inet6"). |
-> LXC [String] | A list of network interfaces. |
Determine the list of container IP addresses.
:: String |
|
-> LXC (Maybe String) |
|
Retrieve the specified cgroup subsystem value for the container.
Set the specified cgroup subsystem value for the container.
Clear a configuration item.
Analog of setConfigItem
.
getConfigPath :: LXC FilePath Source
Determine full path to the containers configuration file.
Each container can have a custom configuration path. However
by default it will be set to either the LXCPATH
configure
variable, or the lxcpath value in the LXC_GLOBAL_CONF
configuration
file (i.e. /etc/lxc/lxc.conf
).
The value for a specific container can be changed using
setConfigPath
.
Set the full path to the containers configuration file.
:: Maybe String | New name for the container. If |
-> Maybe FilePath | lxcpath in which to create the new container. If |
-> [CloneOption] | Additional |
-> Maybe String | Optionally force the cloned bdevtype to a specified plugin. By default the original is used (subject to snapshot requirements). |
-> Maybe String | Information about how to create the new storage (i.e. fstype and fsdata). |
-> Maybe Word64 | In case of a block device backing store, an optional size. If |
-> [String] | Additional arguments to pass to the clone hook script. |
-> LXC (Maybe Container) | Newly-allocated copy of container $c$, or |
Copy a stopped container.
:: Maybe Int | Terminal number to attempt to allocate, or |
-> LXC (Maybe (Int, Int, Int)) | Tuple |
Allocate a console tty for the container.
- The returned file descriptor is used to keep the tty allocated. The caller should call close(2) on the returned file descriptor when no longer required so that it may be allocated by another caller.
:: Maybe Int | Terminal number to attempt to allocate, |
-> Fd | File descriptor to read input from. |
-> Fd | File descriptor to write output to. |
-> Fd | File descriptor to write error output to. |
-> Int | The escape character ( |
-> LXC Bool |
|
Allocate and run a console tty.
:: AttachExecFn | Function to run. |
-> AttachCommand | Data to pass to |
-> AttachOptions | Attach options. |
-> LXC (Maybe ProcessID) | Process ID of process running inside container |
Create a sub-process attached to a container and run a function inside it.
:: AttachOptions | Attach options. |
-> String | Full path inside container of program to run. |
-> [String] | Array of arguments to pass to program. |
-> LXC (Maybe ExitCode) |
|
Run a program inside a container and wait for it to exit.
:: Maybe FilePath | Full path to file containing a description of the snapshot. |
-> LXC (Maybe Int) |
|
Create a container snapshot.
Assuming default paths, snapshots will be created as
/var/lib/lxc/<c>/snaps/snap<n>
where <c>
represents the container name and <n>
represents the zero-based snapshot number.
snapshotList :: LXC [Snapshot] Source
Obtain a list of container snapshots.
:: String | Name of snapshot. |
-> String | Name to be used for the restored snapshot. |
-> LXC Bool |
|
Create a new container based on a snapshot.
The restored container will be a copy (not snapshot) of the snapshot, and restored in the lxcpath of the original container.
- WARNING: If new name is the same as the current container name, the container will be destroyed. However, this will fail if the snapshot is overlay-based, since the snapshots will pin the original container.
- NOTE: As an example, if the container exists as
/var/lib/lxc/c1
, snapname might be"snap0"
(representing/var/lib/lxc/c1/snaps/snap0
). If new name isc2
, thensnap0
will be copied to/var/lib/lxc/c2
.
Destroy the specified snapshot.
mayControl :: LXC Bool Source
Determine if the caller may control the container.
False
if there is a control socket for the container monitor
and the caller may not access it, otherwise returns True
.
:: FilePath | Full path of the device. |
-> Maybe FilePath | Alternate path in the container (or |
-> LXC Bool |
|
Add specified device to the container.
:: FilePath | Full path of the device. |
-> Maybe FilePath | Alternate path in the container (or |
-> LXC Bool |
|
Remove specified device from the container.
:: String | Template to execute to instantiate the root filesystem and adjust the configuration. |
-> Maybe String | Backing store type to use (if |
-> Maybe BDevSpecs | Additional parameters for the backing store (for example LVM volume group to use). |
-> [CreateOption] |
|
-> [String] | Arguments to pass to the template. |
-> LXC Bool |
|
Create a container.
dropRef :: Ptr C'lxc_container -> IO (Maybe Bool) Source
Drop a reference to the specified container.
Just False
on success, Just True
if reference was successfully dropped
and container has been freed, and Nothing
on error.
getWaitStates :: IO [ContainerState] Source
Obtain a list of all container states.
:: String | The name of the config key. |
-> IO (Maybe String) | String representing the current value for the key. |
Get the value for a global config key.
getVersion :: IO String Source
Determine version of LXC.
listContainersFn :: (CString -> Ptr (Ptr CString) -> Ptr (Ptr (Ptr C'lxc_container)) -> IO CInt) -> Maybe String -> IO [Container] Source
Get a list of defined containers in a lxcpath.
Get a list of active containers for a given lxcpath.
Get a complete list of all containers for a given lxcpath.