diff --git a/README.md b/README.md index ef5674a96f3cfea8abbe636f1282f3caa9f70bd9..302fe9a5bee390c7e3da7bb471aa279c6440138a 100644 --- a/README.md +++ b/README.md @@ -37,22 +37,25 @@ Below is the uci for swmodd ```bash config globals 'globals' - option enabled '1' - option lan_bridge 'br-lan' - option oci_bundle_root '/data/crun' - option lxc_bundle_root '/data/lxc' + option enabled '1' + option debug '1' + option log_level '3' + option lan_bridge 'br-lan' + option root '/3rdParty' + +config execenv 'execenv_1' + option name 'lxc' ``` -- For installation of oci based application containers, bundle path need to defined in above uci in 'oci_bundle_root' option. -- For installation of lxc based containers, bundle path need to defined in above uci in 'lxc_bundle_root' option. -- It is suggested to configure a non-volatile secondary memory location in this options. Also 'lan_bridge' need to point to a valid bridge where application containers network namespace shall be attached. +- root is the base directory for containers, it is advised to configure an external non-volatile file-system path in this option +- execenv.name contains the name of sub-directory inside root, which contains the containers base filesystem +- Also 'lan_bridge' need to point to a valid bridge where application containers network namespace shall be attached. > Note: For more details about UCI schema click [here](https://dev.iopsys.eu/iopsys/swmodd/-/tree/devel/schema/uci/swmodd.json) ## Important topics -* [LXC template](./docs/guide/lxc_template.md) +* [Tools](./docs/guide/tools.md) * [Deployment of application containers](./docs/guide/application_container.md) -* [UBUS methods](./docs/guide/ubus_methods.md) * [UCI parameters](./docs/api/uci/uci.md) ## Dependencies diff --git a/docs/api/uci/swmodd.md b/docs/api/uci/swmodd.md index 5006030ac6d89ffbcb3d9d1c8ad606c8acf52cc2..2345118c89968275136e70d507bc491c028d4175 100644 --- a/docs/api/uci/swmodd.md +++ b/docs/api/uci/swmodd.md @@ -1,129 +1 @@ -# SWMODD UCI - -<tbody> - <tr> - <td colspan="2"> - <div style="font-weight: bold;">swmodd</div> - <table style="width: 100%;"> - <tbody> - <tr> - <td><div style="font-weight: bold; font-size: 14px;">section</div></td> - <td><div style="font-weight: bold; font-size: 14px;">description</div></td> - <td><div style="font-weight: bold; font-size: 14px;">multi</div></td> - <td><div style="font-weight: bold; font-size: 14px;">options</div></td> - </tr> - <tr> - <td class="td_row_even"><div class="td_row_even">globals</div></td> - <td class="td_row_even"><div class="td_row_even">Configuration of SWMODD</div></td> - <td class="td_row_even"><div class="td_row_even">false</div></td> - <td class="td_row_even"> - <table style="width: 100%;"> - <tbody> - <tr> - <td><div style="font-weight: bold; font-size: 14px;">name</div></td> - <td><div style="font-weight: bold; font-size: 14px;">type</div></td> - <td><div style="font-weight: bold; font-size: 14px;">required</div></td> - <td><div style="font-weight: bold; font-size: 14px;">default</div></td> - <td><div style="font-weight: bold; font-size: 14px;">description</div></td> - </tr> - <tr> - <td class="td_row_even"><div class="td_row_even">enabled</div></td> - <td class="td_row_even"><div class="td_row_even">boolean</div></td> - <td class="td_row_even"><div class="td_row_even">yes</div></td> - <td class="td_row_even"><div class="td_row_even">0</div></td> - <td class="td_row_even"><div class="td_row_even">If set to <b>1</b>, SWMODD will be enabled</div></td> - </tr> - <tr> - <td class="td_row_odd"><div class="td_row_odd">lan_bridge</div></td> - <td class="td_row_odd"><div class="td_row_odd">string</div></td> - <td class="td_row_odd"><div class="td_row_odd">yes</div></td> - <td class="td_row_odd"><div class="td_row_odd">br-lan</div></td> - <td class="td_row_odd"><div class="td_row_odd">This option is applicable for OCI based application containers only. It must be a valid bridge name where application containers network namespace shall be attached</div></td> - </tr> - <tr> - <td class="td_row_even"><div class="td_row_even">debug</div></td> - <td class="td_row_even"><div class="td_row_even">boolean</div></td> - <td class="td_row_even"><div class="td_row_even">no</div></td> - <td class="td_row_even"><div class="td_row_even">1</div></td> - <td class="td_row_even"><div class="td_row_even">If set to <b>1</b> debugging is enabled</div></td> - </tr> - <tr> - <td class="td_row_odd"><div class="td_row_odd">lxc_bundle_root</div></td> - <td class="td_row_odd"><div class="td_row_odd">string</div></td> - <td class="td_row_odd"><div class="td_row_odd">yes</div></td> - <td class="td_row_odd"><div class="td_row_odd"></div></td> - <td class="td_row_odd"><div class="td_row_odd">This option is valid for LXC based containers. It must be a non-volatile secondary memory location where LXC based containers are installed</div></td> - </tr> - <tr> - <td class="td_row_even"><div class="td_row_even">oci_bundle_root</div></td> - <td class="td_row_even"><div class="td_row_even">string</div></td> - <td class="td_row_even"><div class="td_row_even">yes</div></td> - <td class="td_row_even"><div class="td_row_even"></div></td> - <td class="td_row_even"><div class="td_row_even">This option is valid for OCI based application containers. This must be a non-volatile secondary memory location for installation of OCI based application containers</div></td> - </tr> - <tr> - <td class="td_row_even"><div class="td_row_even">oci_pull_timeout</div></td> - <td class="td_row_even"><div class="td_row_even">string</div></td> - <td class="td_row_even"><div class="td_row_even">no</div></td> - <td class="td_row_even"><div class="td_row_even">10</div></td> - <td class="td_row_even"><div class="td_row_even">This option is valid for OCI based application containers. This specifies the time to wait for pulling the image from container registry. It's value is accepted in minutes unit</div></td> - </tr> - <tr> - <td class="td_row_odd"><div class="td_row_odd">log_level</div></td> - <td class="td_row_odd"><div class="td_row_odd">integer</div></td> - <td class="td_row_odd"><div class="td_row_odd">no</div></td> - <td class="td_row_odd"><div class="td_row_odd">3</div></td> - <td class="td_row_odd"> - <div class="td_row_odd"> - Specifies the log type to use. Supported values are: - <table> - <tr> - <td> value </td> - <td> type </td> - <tr> - <tr> - <td> 0 </td> - <td> EMERGENCY </td> - </tr> - <tr> - <td> 1 </td> - <td> ALERT </td> - </tr> - <tr> - <td> 2 </td> - <td> CRITICAL </td> - </tr> - <tr> - <td> 3 </td> - <td> ERROR </td> - </tr> - <tr> - <td> 4 </td> - <td> WARNING </td> - </tr> - <tr> - <td> 5 </td> - <td> NOTICE </td> - </tr> - <tr> - <td> 6 </td> - <td> INFO </td> - </tr> - <tr> - <td> 7 </td> - <td> DEBUG </td> - </tr> - </table> - </div> - </td> - </tr> - </tbody> - </table> - </td> - </tr> - </tbody> - </table> - </td> - </tr> -</tbody> - +<tbody><tr><td colspan="2"><div style="font-weight: bold">swmodd</div><table style="width:100%"><tbody><tr><td><div style="font-weight: bold; font-size: 14px">section</div></td><td><div style="font-weight: bold; font-size: 14px">description</div></td><td><div style="font-weight: bold; font-size: 14px">multi</div></td><td><div style="font-weight: bold; font-size: 14px">options</div></td></tr><tr><td class="td_row_even"><div class="td_row_even">globals</div></td><td class="td_row_even"><div class="td_row_even">Configuration of SWMODD</div></td><td class="td_row_even"><div class="td_row_even">false</div></td><td class="td_row_even"><table style="width:100%"><tbody><tr><td><div style="font-weight: bold; font-size: 14px">name</div></td><td><div style="font-weight: bold; font-size: 14px">type</div></td><td><div style="font-weight: bold; font-size: 14px">required</div></td><td><div style="font-weight: bold; font-size: 14px">default</div></td><td><div style="font-weight: bold; font-size: 14px">description</div></td></tr><tr><td class="td_row_even"><div class="td_row_even">enabled</div></td><td class="td_row_even"><div class="td_row_even">boolean</div></td><td class="td_row_even"><div class="td_row_even">yes</div></td><td class="td_row_even"><div class="td_row_even">0</div></td><td class="td_row_even"><div class="td_row_even">If set to 1, SWMODD will be enabled</div></td></tr><tr><td class="td_row_odd"><div class="td_row_odd">lan_bridge</div></td><td class="td_row_odd"><div class="td_row_odd">string</div></td><td class="td_row_odd"><div class="td_row_odd">yes</div></td><td class="td_row_odd"><div class="td_row_odd">br-lan</div></td><td class="td_row_odd"><div class="td_row_odd">This option is applicable for OCI based application containers only. It must be a valid bridge name where application containers network namespace shall be attached</div></td></tr><tr><td class="td_row_even"><div class="td_row_even">debug</div></td><td class="td_row_even"><div class="td_row_even">boolean</div></td><td class="td_row_even"><div class="td_row_even">no</div></td><td class="td_row_even"><div class="td_row_even">1</div></td><td class="td_row_even"><div class="td_row_even">If set to 1, debugging is enabled</div></td></tr><tr><td class="td_row_odd"><div class="td_row_odd">root</div></td><td class="td_row_odd"><div class="td_row_odd">string</div></td><td class="td_row_odd"><div class="td_row_odd">yes</div></td><td class="td_row_odd"><div class="td_row_odd"></div></td><td class="td_row_odd"><div class="td_row_odd">Base directory for all containers</div></td></tr><tr><td class="td_row_even"><div class="td_row_even">log_level</div></td><td class="td_row_even"><div class="td_row_even">integer</div></td><td class="td_row_even"><div class="td_row_even">no</div></td><td class="td_row_even"><div class="td_row_even">3</div></td><td class="td_row_even"><div class="td_row_even">Specifies the log type to use. Supported values are: 0(EMERGENCY), 1(ALERT), 2(CRITICAL), 3(ERROR), 4(WARNING), 5(NOTICE), 6(INFO), 7(DEBUG)</div></td></tr></tbody></table></td></tr><tr><td class="td_row_odd"><div class="td_row_odd">execenv</div></td><td class="td_row_odd"><div class="td_row_odd">Execution Environments Configuration</div></td><td class="td_row_odd"><div class="td_row_odd">false</div></td><td class="td_row_odd"><table style="width:100%"><tbody><tr><td><div style="font-weight: bold; font-size: 14px">name</div></td><td><div style="font-weight: bold; font-size: 14px">type</div></td><td><div style="font-weight: bold; font-size: 14px">required</div></td><td><div style="font-weight: bold; font-size: 14px">default</div></td><td><div style="font-weight: bold; font-size: 14px">description</div></td></tr><tr><td class="td_row_even"><div class="td_row_even">name</div></td><td class="td_row_even"><div class="td_row_even">string</div></td><td class="td_row_even"><div class="td_row_even">yes</div></td><td class="td_row_even"><div class="td_row_even"></div></td><td class="td_row_even"><div class="td_row_even">Name of the execution environment, which together with swmodd.globals.root creates the workdir for the containers</div></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody> \ No newline at end of file diff --git a/docs/guide/lxc_migration.md b/docs/guide/lxc_migration.md index 240e7a99859398933b0c2e454abe36a3420ccf1d..aa53a142905d4caa1767970628bca207087353d0 100644 --- a/docs/guide/lxc_migration.md +++ b/docs/guide/lxc_migration.md @@ -1,67 +1,71 @@ # LXC Based container migration -SWMODD in release-6.5 branch based releases only has support for LXC based containers, but in devel now it has support support for OCI based containers and LXC based containers both. -OCI based containers being preferred option for LCM in devel. This document aims to provide guidance for migrating LXC containers from release-6.5 to devel. +Initially `swmodd` only has support for LXC based containers, which partially managed with lxc-auto for auto-start of containers. +Configuration of bundle root was limited to 'lxc.conf', since lxc containers in initial design mapped to an 'ExecEnv' in datamodel, so there were no function to add/remove containers from datamodel and hence all the configuration/deployments are handled with firmware defaults, so there was no need to store additional metadata regarding containers. -## Bit inside on lxc container handling -1. LXC build root defined in '/etc/lxc/lxc.conf' with "lxc.lxcpath" option -2. Each LXC container mapped to 'Device.SoftwareModules.ExecEnv.' datamodel parameter -3. Additional Installed DU's installed in a container stored in `/etc/swmod/map_du_<container_name>` file +Latter support of OCI based application containers being added, which adds the possibility of adding/removing an application container as they are mapped with 'DeploymentUnit', so they can be managed using InstallDU() operation. -```bash -config deployment - option name 'bbk_cli' - option version '7b810a69' - option uuid '477a210f-9ed1-401a-b028-717a726e5296' - option duid '9zz0000' - option environment 'test' - option eeid '1' - list service 'bbk_cli' - option description 'To measure connection speed in an environment that is missing a web browser, ' -``` +This also needs datamodel mapping extensions to store all these metadata, so that they are persistent across firmware upgrades and restarts and factory resets. -4. Auto-start of LXC based containers managed with the help of `lxc-auto` package and is stored in `/etc/config/lxcauto` file -5. LXC based container configuration stored in a `config` file inside build root path + +## Bit more inside on lxc container handling in older releases +1. LXC bundle root defined in '/etc/lxc/lxc.conf' with "lxc.lxcpath" option +2. Each LXC container mapped to 'Device.SoftwareModules.ExecEnv.' datamodel parameter +3. Auto-start of LXC based containers managed with the help of `lxc-auto` package and is stored in `/etc/config/lxcauto` file +4. LXC based container configuration stored in a `config` file inside build root path So, the information is bit scattered and had too many dependencies on external modules, also some information does not persists across factory reset. -## Bit inside on devel changes -1. Defining the bundle root now moved to swmodd uci (In deployment, its a best practice use a non-root persistent storage path for this option) +## After alignment with TR181-2.16 +1. Defining the bundle root now moved to swmodd uci (Note: In deployment, its a best practice use a non-root persistent storage path for this option) ```bash config globals 'globals' - option lxc_bundle_root '/mnt/container/' - option oci_bundle_root '/mnt/container/' + option root '/3rdParty' + +config execenv 'execenv_1' + option name 'container' ``` -- But it still has preference on `lxc.conf` configuration, it works like - - If lxcpath is set in /etc/config/swmodd then swmodd configures the same in lxc.conf through uci-default script - - If not defined in /etc/config/swmodd but lxcpath is present in lxc.conf then uci-default script writes the lxc_bundle_root with the same in swmodd uci - - If not defined in /etc/config/swmodd and also in lxc.conf then uci-default set it with "/srv/" in both lxc.conf and /etc/config/swmodd +2. All containers (LXC/OCI) are treated application/service container and its mapped to DU (`Device.SoftwareModules.DeploymentUnits.`). +3. `Device.SoftwareModules.ExecEnv.` mapped to `execenv` section in swmodd UCI, which represents the sub directory inside the `root` path where all DUs are installed. +4. Datamodel metadata now stored in "ocicontainer" UCI file at `<root>/<name>/` for e.g `/3rdParty/container/`, to make it persistent across firmwareUpgrades/factoryResets/Reboots. +5. auto-boot dependency now removed, its now handled by swmodd itself based on "ocicontainer" UCI file. -2. LXC containers still mapped to `Device.SoftwareModules.ExecEnv.`, but it start with instance id 2 -3. `Device.SoftwareModules.ExecEnv.1` mapped to system and used for OCI based containers -4. OCI containers mapped to `Device.SoftwareModules.DeploymentUnits.` -5. To make the LXC and OCI containers information persistent across factory reset, now its stored in lxc_bundle_root path in "lxccontainer" (for lxc info) file. -6. auto-boot dependency now moved to swmodd it self based on mappings present in "lxccontainer" uci file. +Example of ocicontainer uci file: ```bash -config container +config du_eu_assoc '<section_name>' option name '<container_name>' option type 'lxc' option autostart '1' + option requested_state 'Active' + option du_status 'Installed' + option uuid '<uuid>' + option duid '<duid'> + option ee_name '<execenv name>' + option eu_name '<container_name>' ``` -7. All DU and EU details are stored in lxccontainer uci file as well for LXC based containers - ## Migration notes -1. lxc-auto must not be present as now lxc containers handled by swmodd itself. -2. uci-default script '02-migrate-lxc' added to migrate the lxcauto to new uci file +1. lxc-auto shall not be used for managing lxc containers auto-start +2. uci-default script '01-fix-bundle-path' added to migrate the bundle-root to execenv configuration, its works as + - If oci_bundle_root is set in /etc/config/swmodd then uci-default script writes the same in lxc.conf and configures the `root` and execenv section in swmodd UCI. + - If oci_bundle_root is not defined but lxc_bundle_root is present then uci-default script writes the same in lxc.conf and configures the `root` and execenv section in swmodd UCI. + - If both are not defined but lxc path is present in lxc.conf then uci-default script configures the `root` and execenv section in swmodd UCI with this value. + - If none of the above three are valid then uci-default set `root` with "/srv" and execenv name with `lxc` in /etc/config/swmodd and set lxc path with `/srv/lxc' in lxc.conf. +3. uci-default script '02-migrate-lxc' added to migrate the lxc containers as DU in ocicontainer UCI file. It finds all available LXC containers inside `execenv` and add `du_eu_assoc` section in the ocicontainer file for the corresponding lxc. +4. uci-default script '03-execenv-ref' has been added to migrate the execution environment reference of each DUs in ocicontainer file, since earlier `system` i.e openwrt was treated as environment now every bundle_root would be treated as an environment. + +> Note: Apart from the above described configuration if there is any customer specific configuration then that needs to be migrated additionaly. ## Summary -Moving forward lxc based container will be deprecated, so its good to update the lxc/config to oci/config.json, so that the lxc container can be run as oci containers. +There has been a big shift in how containers mapped earlier and now with datamodel, but functionaly there should not be any impact. We have added couple to migration script to easily migrated from previous release/design to new release/design. + + +## References - [OCI runtime specs](https://github.com/opencontainers/runtime-spec/blob/main/config.md) - [LXC runtime specs](https://linuxcontainers.org/lxc/manpages/man5/lxc.container.conf.5.html) - [LXC config manpages](https://manpages.ubuntu.com/manpages/trusty/man5/lxc.container.conf.5.html) diff --git a/docs/guide/lxc_template.md b/docs/guide/lxc_template.md deleted file mode 100644 index b71b61e39d1c903c2865ed4f7b917ea5627c7801..0000000000000000000000000000000000000000 --- a/docs/guide/lxc_template.md +++ /dev/null @@ -1,11 +0,0 @@ -# swmodd lxc templates -swmodd also includes LXC templates to easily create LXC based containers. This template shall used only for development and test purpose. - -## To create a new container using iopsys template: - -`lxc-create -n <container_name> -t iopsys` -example: -```bash -root@iopsys:~# lxc-create -n test -t iopsys -root@iopsys:~# -``` diff --git a/docs/guide/tools.md b/docs/guide/tools.md new file mode 100644 index 0000000000000000000000000000000000000000..4fdcf5440aa4cf3173385740f60d0ea2421f249e --- /dev/null +++ b/docs/guide/tools.md @@ -0,0 +1,45 @@ +# Debug and development tools + +It's often required to get a handy lxc/oci container to validate something quickly. +`swmodd` provides a LXC templates to easily create LXC based containers and a script to create oci based container. + +> Note: These template/script shall used only for development and test purposes, it shall not be used in deployments. + +## To create a new container using iopsys template: + +`lxc-create -n <container_name> -t iopsys` + +example: +```bash +root@iopsys:~# lxc-create -n test -t iopsys +root@iopsys:~# +``` + +## To create a new oci container + +```bash +# crun_create -h +CRUN iopsys image builder + +Special arguments: + + [ -h | --help ]: Print this help message and exit. + [ -c | --config ]: Generate config in swmodd for specified container + +Arguments: + + [ -n | --name <name> ]: The container name + [ -r | --root <path> ]: The root dir path where the environment exists + [ -e | --env <name> ]: Name of the environment + [ --no-reload ]: Do not reload crun + +IOPSYS template specific arguments: + + [ --busybox-path <path> ]: busybox pathname (default: /bin/busybox) +``` + +example: + +```bash +crun_create -n test +``` diff --git a/docs/guide/ubus_methods.md b/docs/guide/ubus_methods.md deleted file mode 100644 index dc4d42ef515901cb667e9ef9125066983e31eb2c..0000000000000000000000000000000000000000 --- a/docs/guide/ubus_methods.md +++ /dev/null @@ -1,107 +0,0 @@ -# swmodd uBus - -It exposes the SoftwareModules datamodel objectes over ubus. The `swmodd` registers `swmodules` namespaces with ubus, that has the shown below functionalities: - -```bash -root@iopsys:~# ubus -v list swmodules -'swmodules' @19113d4c - "ee_list":{} - "reload":{} - "du_list":{"eeid":"Integer","ee_name":"String"} - "eu_list":{"eeid":"Integer","ee_name":"String"} - "du_install":{"eeid":"Integer","ee_name":"String","uuid":"String","url":"String","username":"String","password":"String"} - "du_update":{"eeid":"Integer","ee_name":"String","uuid":"String","url":"String","username":"String","password":"String"} - "du_uninstall":{"eeid":"Integer","ee_name":"String","du_name":"String"} - "eu_set_state":{"eeid":"Integer","ee_name":"String","eu_name":"String","state":"Boolean"} - "ee_set_state":{"eeid":"Integer","ee_name":"String","state":"String"} -root@iopsys:~# -``` - -For more info on the `swmodules` ubus schema see [link](../api/ubus/swmodules.md) or [raw schema](../../schemas/ubus/swmodules.json) - -## swmodd ubus examples -The outputs shown below are just an example, it can vary on each system. Long outputs are truncated to beautify the document. - -### List down the available execution environments -```bash -root@iopsys:~# ubus call swmodules ee_list -{ - "environment": [ - { - "ee_name": "OpenWRT_Linux", - "eeid": 1, - "status": "Up", - "pause": 0, - "autoboot": 1, - "type": "Virtual_EE", - "vendor": "iopsys-eca940fff680", - "version": "4.19.235", - "allocated_disk_space": 27632, - "available_disk_space": 26716, - "allocated_memory": 1014988, - "available_memory": 476840, - "parent_ee_ref": 0 - } - ] -} -root@iopsys:~# -``` -### Install new application container as deployment unit -This requires support to pull images from container registry. - -```bash -root@iopsys:~# ubus call swmodules du_install '{"url":"docker://httpd:latest"}' -{ - "status": true -} -root@iopsys:~# -``` -### List available deployment units/installed services -```bash -root@iopsys:~# ubus call swmodules du_list -{ - "deployment_unit": [ - { - "du_name": "httpd", - "ee_name": "OpenWRT_Linux", - "eeid": 1, - "uuid": "347dd4bf-6b51-4d62-be61-77c8ded98698", - "duid": "w0t0000", - "url": "docker://httpd:latest", - "version": "", - "config": "", - "description": "", - "du_status": "Installed", - "eu_name": "httpd", - "vendor": "" - } - ] -} -root@iopsys:~# -``` - -### List execution units/running services -```bash -root@iopsys:~# ubus call swmodules eu_list -{ - "execution_unit": [ - { - "eu_name": "httpd", - "command": "", - "state": "Active", - "config": "", - "version": "4.19.235", - "description": "This is a CRUN container", - "ee_name": "OpenWRT_Linux", - "eeid": 1, - "euid": "22728", - "disk_space": 91840, - "memory_space": 1014988, - "vendor": "umoci-default", - "req_state": "Active", - "autostart": true, - "du_name": "httpd" - } - ] -} -``` diff --git a/schemas/uci/swmodd.json b/schemas/uci/swmodd.json index ef5a03c9e052435c92de9b78f7152a3c44f2726f..8a7054d598cf5a3e9a6e9d255a990d53dc8220e8 100644 --- a/schemas/uci/swmodd.json +++ b/schemas/uci/swmodd.json @@ -27,18 +27,11 @@ "description": "If set to 1, debugging is enabled" }, { - "name": "lxc_bundle_root", + "name": "root", "type": "string", "required": "yes", "default": "", - "description": "This option is valid for LXC based containers. It must be a non-volatile secondary memory location where LXC based containers are installed" - }, - { - "name": "oci_bundle_root", - "type": "string", - "required": "yes", - "default": "", - "description": "This option is valid for OCI based application containers. This must be a non-volatile secondary memory location for installation of OCI based application containers" + "description": "Base directory for all containers" }, { "name": "log_level", @@ -48,6 +41,20 @@ "description": "Specifies the log type to use. Supported values are: 0(EMERGENCY), 1(ALERT), 2(CRITICAL), 3(ERROR), 4(WARNING), 5(NOTICE), 6(INFO), 7(DEBUG)" } ] + }, + { + "section": "execenv", + "description": "Execution Environments Configuration", + "multi": "false", + "options": [ + { + "name": "name", + "type": "string", + "required": "yes", + "default": "", + "description": "Name of the execution environment, which together with swmodd.globals.root creates the workdir for the containers" + } + ] } ] } diff --git a/scripts/crun_create b/scripts/crun_create index edb6f8b5a7e92fad90440d22a12cf1ee138abdf3..a442d1c265d7814d363669f805643fc0472c6b71 100755 --- a/scripts/crun_create +++ b/scripts/crun_create @@ -1,8 +1,15 @@ #!/bin/sh +. /lib/functions.sh + BUSYBOX_EXE=$(command -v busybox) BUSYBOX_DEV_LIST="" BUSYBOX_LIB_MOUNT_LIST="" +BUNDLE_PATH="" + +ENVNAME="" +DU_URL="" +RELOAD=1 # Make sure busybox is present if [ ! -x "${BUSYBOX_EXE}" ]; then @@ -10,12 +17,35 @@ if [ ! -x "${BUSYBOX_EXE}" ]; then exit 1 fi -BUNDLE_PATH=$(uci -q get swmodd.globals.oci_bundle_root) -if [ -z "${BUNDLE_PATH}" ]; then - BUNDLE_PATH="/srv/crun" # default location -fi +set_bundle_path() { + local root + + root=$(uci_get swmodd.globals.root) + if [ ! -d "${root}" ]; then + echo "Base path [$root] not configured/present" + exit 1 + fi -mkdir -p "${BUNDLE_PATH}" + if [ -z "${ENVNAME}" ]; then + config_load swmodd + ENVNAME=$(uci_get swmodd.@execenv[0].name) + + echo "Use default env [$ENVNAME]" + fi + + if [ -n "${root}" ] && [ -n "${ENVNAME}" ]; then + BUNDLE_PATH="${root}" + if [ "${root: -1}" != '/' ]; then + BUNDLE_PATH="${root}/" + fi + + BUNDLE_PATH="${BUNDLE_PATH}${ENVNAME}" + mkdir -p "${BUNDLE_PATH}" + else + echo "ERROR: Execution environment [$ENVNAME] not defined" + exit 1 + fi +} copy_configuration() { @@ -620,7 +650,7 @@ create_container() generate_config() { - local name oci_bundle_root res cfg + local name res cfg name="${1}" if [ -z "${name}" ]; then @@ -628,36 +658,39 @@ generate_config() exit 1 fi - oci_bundle_root=$(uci -q get swmodd.globals.oci_bundle_root) - if [ -z "${oci_bundle_root}" ]; then - echo "ERROR: Failed to get CRUN UCI path" - exit 1 + oci_bundle_root=${BUNDLE_PATH} + touch ${oci_bundle_root}/ocicontainer + + # Now lets check if already present in config file + exist=$(uci -q -c "${oci_bundle_root}" show ocicontainer | grep ".name='$name'") + if [ -n "${exist}" ]; then + echo "INFO: ${name} already exist in config file ${oci_bundle_root}/ocicontainer" + exit 1 fi # Configure in crun uci for new container # uci -q -c "${oci_bundle_root}" set ocicontainer."${name}"=du_eu_assoc # Enable the container - uuid=$(uuidgen) uci -q -c "${oci_bundle_root}" set ocicontainer."${name}".name="${name}" uci -q -c "${oci_bundle_root}" set ocicontainer."${name}".autostart=1 uci -q -c "${oci_bundle_root}" set ocicontainer."${name}".requested_state='Active' uci -q -c "${oci_bundle_root}" set ocicontainer."${name}".du_status='Installed' if [ -f "${oci_bundle_root}/$name/config" ]; then uci -q -c "${oci_bundle_root}" set ocicontainer."${name}".type='lxc' - uci -q -c "${oci_bundle_root}" set ocicontainer."${name}".url='local://lxc_template' + uci -q -c "${oci_bundle_root}" set ocicontainer."${name}".url="${DU_URL}" else uci -q -c "${oci_bundle_root}" set ocicontainer."${name}".type='crun' uci -q -c "${oci_bundle_root}" set ocicontainer."${name}".url='local://crun_template' fi - uci -q -c "${oci_bundle_root}" set ocicontainer."${name}".uuid="${uuid}" - uci -q -c "${oci_bundle_root}" set ocicontainer."${name}".ee_name='OpenWRT_Linux' - uci -q -c "${oci_bundle_root}" set ocicontainer."${name}".duid="${uuid}" + uci -q -c "${oci_bundle_root}" set ocicontainer."${name}".ee_name="${ENVNAME}" uci -q -c "${oci_bundle_root}" set ocicontainer."${name}".eu_name="${name}" uci -c "${oci_bundle_root}" commit ocicontainer # Reload crun service - ubus call uci commit '{"config":"crun"}' + if [ "$RELOAD" -eq "1" ]; then + ubus call uci commit '{"config":"crun"}' + fi } usage() @@ -672,17 +705,15 @@ Special arguments: Arguments: - [ --name <name> ]: The container name - -IOPSYS template specific arguments: - - [ --busybox-path <path> ]: busybox pathname (default: ${BUSYBOX_EXE}) + [ -n | --name <name> ]: The container name + [ -e | --env <name> ]: Name of the Execution Environment to create the container + [ --no-reload ]: Do not reload crun EOF return 0 } -if ! options=$(getopt -o hcn: -l help,config,name:,busybox-path: -- "$@"); then +if ! options=$(getopt -o hcn:e:u: -l help,config,name:,env:,url:,busybox-path:,no-reload -- "$@"); then usage exit 1 fi @@ -695,7 +726,10 @@ do -h|--help) usage && exit 0;; -n|--name) name=$2; shift 2;; -c|--config) config=1 && shift 1;; + -e|--env) ENVNAME=$2; shift 2;; + -u|--url) DU_URL=$2; shift 2;; --busybox-path) BUSYBOX_EXE=$2; shift 2;; + --no-reload) RELOAD=0 && shift 1;; --) shift 1; break ;; *) break ;; esac @@ -707,6 +741,7 @@ if [ -z "${name}" ]; then exit 1 fi +set_bundle_path if [ "$config" -eq "1" ]; then generate_config "$name" else diff --git a/src/swmod.c b/src/swmod.c index 7904ce98ef4671c30b5b779d4a739078068c9338..49489a780a9dea86386b00d3e43638b5d6ed5691 100644 --- a/src/swmod.c +++ b/src/swmod.c @@ -32,6 +32,7 @@ struct ubus_context *ubus_ctx; ExecEnv_t g_environments[MAX_ENV] = {0}; ConfigParams swmod_config; +static int incr = 0; enum { DU_INSTALL_ENV_ID, @@ -161,7 +162,7 @@ populate_environments(void) INIT_LIST_HEAD(&ee_head); /* Host system */ - populate_host_system_environment(&ee_head); + populate_host_system_environment(&ee_head, swmod_config.oci_bundle_root); /* Linux containers */ #ifdef SWMOD_LXC @@ -426,7 +427,6 @@ static void swmod_perform_package_install(PkgInfo *pkg) swmod_uci_set_value_by_section(s, "ee_name", pkg->env_name); swmod_uci_set_value_by_section(s, "du_status", "Installing"); - static int incr = 0; char *duid = generate_duid(true, incr++); swmod_uci_set_value_by_section(s, "duid", duid); FREE(duid); @@ -867,6 +867,20 @@ static void prepare_du_list_result(ExecEnv_t *env, struct blob_buf *bb) if (strcmp(ee_name, env->name) != 0) continue; + char *uuid = swmod_uci_get_value_by_section(ss, "uuid"); + if (strlen(uuid) == 0) { + uuid = generate_uuid(); + swmod_uci_set_value_by_section(ss, "uuid", uuid ? uuid : ""); + FREE(uuid); + } + + char *duid = swmod_uci_get_value_by_section(ss, "duid"); + if (strlen(duid) == 0) { + duid = generate_duid(true, incr++); + swmod_uci_set_value_by_section(ss, "duid", duid ? duid : ""); + FREE(duid); + } + void *t = blobmsg_open_table(bb, ""); blobmsg_add_string(bb, "du_name", swmod_uci_get_value_by_section(ss, "name")); diff --git a/src/swmod.h b/src/swmod.h index ed93e5e622acd16c428d761b9fd7e8907eec9ed7..81da30a01217ee62d93010ddf319d6f76a9aabfb 100644 --- a/src/swmod.h +++ b/src/swmod.h @@ -91,8 +91,8 @@ typedef struct ExecEnvNode { } ExecEnvNode; typedef struct swmodConfig { - char oci_bundle_root[MAX_LEN_32]; - char lxc_bundle_root[MAX_LEN_32]; + char oci_bundle_root[MAX_LEN_64]; + char lxc_bundle_root[MAX_LEN_64]; } ConfigParams; struct du_info { diff --git a/src/swmod_api.c b/src/swmod_api.c index ab51e6f83b3e924ddffefc50f6b513033e49e268..b1b652d7a25da9d991bbebb9c8a0df3590b0fe1f 100644 --- a/src/swmod_api.c +++ b/src/swmod_api.c @@ -330,7 +330,7 @@ void swmod_get_env_info(ExecEnv_t *env) #endif break; case EE_TYPE_HOST: - get_host_system_info(env); + get_host_system_info(env, swmod_config.oci_bundle_root); break; default: break; diff --git a/src/swmod_host.c b/src/swmod_host.c index 74cd87a10ae93b57db629d46fd02bcf44c024cfb..658b2fc089ddee71df60a7c947a1462d4e0359ce 100644 --- a/src/swmod_host.c +++ b/src/swmod_host.c @@ -19,18 +19,12 @@ #include "swmod_host.h" #include "swmod.h" -void get_host_system_info(ExecEnv_t *host_ee) +void get_host_system_info(ExecEnv_t *host_ee, const char *bundle_root) { if (host_ee == NULL) return; - struct utsname utsname; - - if (uname(&utsname) >= 0) { - swmod_strncpy(host_ee->type, "Virtual_EE", MAX_LEN_64); - swmod_strncpy(host_ee->vendor, utsname.nodename, MAX_LEN_128); - swmod_strncpy(host_ee->version, utsname.release, MAX_LEN_16); - } + swmod_strncpy(host_ee->type, "Virtual_EE", MAX_LEN_64); struct sysinfo sinfo; @@ -40,7 +34,7 @@ void get_host_system_info(ExecEnv_t *host_ee) } struct statvfs dinfo; - if (statvfs("/", &dinfo) == 0) { + if (statvfs(bundle_root, &dinfo) == 0) { host_ee->allocated_disk_space = (dinfo.f_bsize * dinfo.f_blocks) / 1024; host_ee->available_disk_space = (dinfo.f_bsize * dinfo.f_bfree) / 1024; } else { @@ -49,7 +43,7 @@ void get_host_system_info(ExecEnv_t *host_ee) } } -void populate_host_system_environment(struct list_head *ee_head) +void populate_host_system_environment(struct list_head *ee_head, const char *bundle_root) { ExecEnv_t host_ee; memset(&host_ee, 0, sizeof(ExecEnv_t)); @@ -60,7 +54,12 @@ void populate_host_system_environment(struct list_head *ee_head) host_ee.autoboot = 1; host_ee.eeid = PARENT_EEID; host_ee.parent_eeid = 0; - swmod_strncpy(host_ee.name, HOST_SYSTEM, MAX_LEN_32); + + char *tmp = strrchr(bundle_root, '/'); + if (tmp) { + swmod_strncpy(host_ee.name, tmp+1, MAX_LEN_32); + } + swmod_strncpy(host_ee.status, "Up", MAX_LEN_32); swmod_add_ee_in_list(ee_head, &host_ee); diff --git a/src/swmod_host.h b/src/swmod_host.h index ce2164d85136fb548fd72408f36566962518da75..073ee2d105a124f0cbc80248608096389cf13eab 100644 --- a/src/swmod_host.h +++ b/src/swmod_host.h @@ -13,7 +13,7 @@ #include "swmod.h" #include <libubox/list.h> -void populate_host_system_environment(struct list_head *ee_list); -void get_host_system_info(ExecEnv_t *host_ee); +void populate_host_system_environment(struct list_head *ee_list, const char *bundle_root); +void get_host_system_info(ExecEnv_t *host_ee, const char *bundle_root); #endif //HOST_H diff --git a/src/tools.c b/src/tools.c index 53d30371b0b48b2255dc6579279da5ffabd745d4..e3c925ec07b46bfe30badfd8e683a6fd43ae3ef0 100644 --- a/src/tools.c +++ b/src/tools.c @@ -695,6 +695,9 @@ void swmod_delete_eu_list(struct list_head *eu_list) void get_swmod_config_params(ConfigParams *cfg) { + const char *root = NULL; + char bundle_root[MAX_LEN_64] = {0}; + if (cfg == NULL) exit(0); @@ -704,22 +707,38 @@ void get_swmod_config_params(ConfigParams *cfg) struct uci_section *s = NULL; swmod_uci_foreach_section(SWMOD_UCI_FILE, "globals", s) { if (strcmp(s->e.name, "globals") == 0) { - const char *oci_bundle_root = swmod_uci_get_value_by_section(s, "oci_bundle_root"); - snprintf(cfg->oci_bundle_root, MAX_LEN_32, "%s", oci_bundle_root); - - const char *lxc_bundle_root = swmod_uci_get_value_by_section(s, "lxc_bundle_root"); - snprintf(cfg->lxc_bundle_root, MAX_LEN_32, "%s", lxc_bundle_root); + root = swmod_uci_get_value_by_section(s, "root"); break; } } - swmod_uci_fini(SWMOD_UCI_FILE); - if (strlen(cfg->oci_bundle_root) == 0) + int root_len = (root != NULL) ? strlen(root) : 0; + if (root_len == 0) exit(0); - if (!dir_exist(cfg->oci_bundle_root)) + s = NULL; + swmod_uci_foreach_section(SWMOD_UCI_FILE, "execenv", s) { + const char *name = swmod_uci_get_value_by_section(s, "name"); + + if (name == NULL || strlen(name) == 0) + continue; + + if (root[root_len - 1] == '/') + snprintf(bundle_root, MAX_LEN_64, "%s%s", root, name); + else + snprintf(bundle_root, MAX_LEN_64, "%s/%s", root, name); + + break; + } + + swmod_uci_fini(SWMOD_UCI_FILE); + + if (strlen(bundle_root) == 0 || !dir_exist(bundle_root)) exit(0); + snprintf(cfg->oci_bundle_root, MAX_LEN_64, "%s", bundle_root); + snprintf(cfg->lxc_bundle_root, MAX_LEN_64, "%s", bundle_root); + char oci_du_uci[MAX_LEN_256] = {0}; snprintf(oci_du_uci, sizeof(oci_du_uci), "%s/%s", cfg->oci_bundle_root, SWMOD_OCI_DU_UCI); if (!file_exists(oci_du_uci)) { @@ -730,12 +749,6 @@ void get_swmod_config_params(ConfigParams *cfg) fclose(fp); } - if (strlen(cfg->lxc_bundle_root) == 0) - exit(0); - - if (!dir_exist(cfg->lxc_bundle_root)) - exit(0); - char lxc_du_uci[MAX_LEN_256] = {0}; snprintf(lxc_du_uci, sizeof(lxc_du_uci), "%s/%s", cfg->lxc_bundle_root, SWMOD_LXC_DU_UCI); if (!file_exists(lxc_du_uci)) { diff --git a/templates/lxc-iopsys b/templates/lxc-iopsys index 95b414823d6a147372d8b2cb9d323f8476ea9dce..b89c2c7b63ae1cf0aba5eeea06a03576b176fa2c 100755 --- a/templates/lxc-iopsys +++ b/templates/lxc-iopsys @@ -412,5 +412,7 @@ if ! copy_prereq "${rootfs}"; then fi if command -v crun_create; then - crun_create -c -n "$name" + bundle_path=$(echo ${path%/$name}) + env_name=$(echo ${bundle_path##/*/}) + crun_create -c -e "$env_name" -n "$name" -u "local://lxc_template" fi diff --git a/test/files/etc/config/swmodd b/test/files/etc/config/swmodd index 5347db0b77f46f82d8d26fac0c550f2cde107f55..d196e2496c8e43cbbe58a1f8f84d2670b0589da6 100644 --- a/test/files/etc/config/swmodd +++ b/test/files/etc/config/swmodd @@ -2,6 +2,8 @@ config globals 'globals' option enabled '1' option debug '1' option log_level '3' - option oci_bundle_root '/tmp/crun' - option lxc_bundle_root '/tmp/crun' + option root '/tmp' option lan_bridge 'br-lan' + +config execenv 'execenv_1' + option name 'crun'