Copyright | (c) Nickolay Kudasov 2014 |
---|---|
License | BSD-style (see the file LICENSE) |
Maintainer | nickolay.kudasov@gmail.com |
Safe Haskell | None |
Language | Haskell2010 |
This module provides a set of functions to create, control and manage LXC containers. You can get more info about LXC at https://help.ubuntu.com/lts/serverguide/lxc.html and https://linuxcontainers.org.
Normally you should import System.LXC
module only.
- data LXC a
- withContainer :: MonadIO m => Container -> LXC a -> m a
- data Container = Container {}
- data Snapshot = Snapshot {}
- data BDevSpecs = BDevSpecs {}
- data ContainerState
- parseState :: String -> ContainerState
- printState :: ContainerState -> String
- data CloneOption
- data CreateOption
- cloneFlag :: Num a => CloneOption -> a
- createFlag :: Num a => CreateOption -> a
- data LXCError = LXCError {}
- prettyLXCError :: LXCError -> String
- isDefined :: LXC Bool
- isRunning :: LXC Bool
- state :: LXC ContainerState
- initPID :: LXC (Maybe ProcessID)
- getInterfaces :: LXC [String]
- getIPs :: String -> String -> Word32 -> LXC [String]
- getDaemonize :: LXC Bool
- getLastError :: LXC (Maybe LXCError)
- configFileName :: LXC (Maybe FilePath)
- getConfigPath :: LXC FilePath
- setConfigPath :: FilePath -> LXC Bool
- loadConfig :: Maybe FilePath -> LXC Bool
- saveConfig :: FilePath -> LXC Bool
- getKeys :: String -> LXC [String]
- setConfigItem :: String -> String -> LXC Bool
- getConfigItem :: String -> LXC (Maybe String)
- getRunningConfigItem :: String -> LXC (Maybe String)
- clearConfig :: LXC ()
- clearConfigItem :: String -> LXC Bool
- start :: Bool -> [String] -> LXC Bool
- stop :: LXC Bool
- reboot :: LXC Bool
- shutdown :: Int -> LXC Bool
- freeze :: LXC Bool
- unfreeze :: LXC Bool
- wait :: ContainerState -> Int -> LXC Bool
- create :: String -> Maybe String -> Maybe BDevSpecs -> [CreateOption] -> [String] -> LXC Bool
- clone :: Maybe String -> Maybe FilePath -> [CloneOption] -> Maybe String -> Maybe String -> Maybe Word64 -> [String] -> LXC (Maybe Container)
- rename :: String -> LXC Bool
- destroy :: LXC Bool
- 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)
- snapshotList :: LXC [Snapshot]
- snapshotRestore :: String -> String -> LXC Bool
- snapshotDestroy :: String -> LXC Bool
- wantDaemonize :: Bool -> LXC Bool
- wantCloseAllFDs :: Bool -> LXC Bool
- getCGroupItem :: String -> LXC (Maybe String)
- setCGroupItem :: String -> String -> LXC Bool
- mayControl :: LXC Bool
- addDeviceNode :: FilePath -> Maybe FilePath -> LXC Bool
- removeDeviceNode :: FilePath -> Maybe FilePath -> LXC Bool
- listDefinedContainers :: Maybe String -> IO [Container]
- listActiveContainers :: Maybe String -> IO [Container]
- listAllContainers :: Maybe String -> IO [Container]
- getWaitStates :: IO [ContainerState]
- getGlobalConfigItem :: String -> IO (Maybe String)
- getVersion :: IO String
- logClose :: IO ()
LXC Monad
Custom comment for LXC.
data LXC a
withContainer :: MonadIO m => Container -> LXC a -> m a
Data types
data Container
Container object.
Container | |
|
data Snapshot
An LXC container snapshot.
Snapshot | |
|
data BDevSpecs
Specifications for how to create a new backing store.
BDevSpecs | |
|
data ContainerState
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
Parse state as string representation.
printState :: ContainerState -> String
Get string representation of a state.
Flags
data CloneOption
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
Options for create
operation.
CreateQuiet | Redirect |
CreateMaxFlags | Number of |
cloneFlag :: Num a => CloneOption -> a
Turn CloneOption
into a bit flag.
createFlag :: Num a => CreateOption -> a
Turn CreateOption
into a bit flag.
LXC errors
data LXCError
LXC error structure.
LXCError | |
|
prettyLXCError :: LXCError -> String
Pretty print LXC error message.
Container methods
Query container state.
Determine if /var/lib/lxc/$name/config
exists.
True
if container is defined, else False
.
Determine state of container.
getInterfaces :: LXC [String]
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.
getDaemonize :: LXC Bool
Whether container wishes to be daemonized.
getLastError :: LXC (Maybe LXCError)
Get last container's error.
Container config
configFileName :: LXC (Maybe FilePath)
Return current config file name.
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 FilePath | Full path to alternate configuration file, or |
-> LXC Bool |
|
Load the specified configuration for the container.
Save configuaration to a file.
Retrieve a list of config item keys given a key prefix.
Set a key/value configuration option.
Retrieve the value of a config item.
Retrieve the value of a config item from running container.
clearConfig :: LXC ()
Completely clear the containers in-memory configuration.
Clear a configuration item.
Analog of setConfigItem
.
Control container state
:: Bool | Use |
-> [String] | Array of arguments to pass to init. |
-> LXC Bool |
|
Start the 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
.
:: 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.
Manage containers
:: 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.
:: 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 |
Copy a stopped container.
Rename a container.
Delete the container.
True
on success, else False
.
- NOTE: Container must be stopped and have no dependent snapshots.
Console
:: 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.
Attach to container
:: 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.
Snapshots
:: 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]
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.
Misc
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.
:: String |
|
-> LXC (Maybe String) |
|
Retrieve the specified cgroup subsystem value for the container.
Set the specified cgroup subsystem value for the container.
mayControl :: LXC Bool
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.
Global LXC functions
List containers
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.
Misc
getWaitStates :: IO [ContainerState]
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
Determine version of LXC.