| 1 |
.. _stackbuild: |
| 2 |
|
| 3 |
Building based on Containers |
| 4 |
============================ |
| 5 |
|
| 6 |
.. note:: **Abstract** |
| 7 |
|
| 8 |
When building images, the {kiwi} `stash` and `stackbuild` |
| 9 |
commands provide an opportunity to address the following |
| 10 |
requirements: |
| 11 |
|
| 12 |
* Preserve the image rootfs for a later rebuild without |
| 13 |
requiring the original software repositories. |
| 14 |
|
| 15 |
* Build an image based on an existing container. |
| 16 |
|
| 17 |
* Build an image based on a container stack. |
| 18 |
|
| 19 |
* Transform a container into a {kiwi} image type. |
| 20 |
|
| 21 |
Requirements |
| 22 |
------------ |
| 23 |
|
| 24 |
The `stash` and `stackbuild` commands are part of {kiwi}. Add the |
| 25 |
{kiwi} repo from the Open Build Service. For details, see |
| 26 |
:ref:`installation-from-obs`. The following package pulls in the |
| 27 |
host requirements for the `stash` and `stackbuild` commands, like |
| 28 |
`podman` and the OCI container tools: |
| 29 |
|
| 30 |
.. code:: bash |
| 31 |
|
| 32 |
$ sudo zypper in kiwi-systemdeps-stackbuild |
| 33 |
|
| 34 |
.. note:: |
| 35 |
|
| 36 |
The `stash` and `stackbuild` commands were formerly provided by the |
| 37 |
`kiwi-stackbuild-plugin`. The plugin is no longer needed and is |
| 38 |
replaced by the `kiwi-systemdeps-stackbuild` package. |
| 39 |
|
| 40 |
Concept |
| 41 |
------- |
| 42 |
|
| 43 |
The design of the stackbuild concept is twofold: |
| 44 |
|
| 45 |
First, {kiwi} comes with a command called `stash` that allows |
| 46 |
you to store a kiwi-built root tree as an OCI container. OCI stands for |
| 47 |
*Open Container Initiative* and is a de facto standard format in the |
| 48 |
container world. Once the container has been created, it can be managed |
| 49 |
using the preferred container toolchain. The `stash` and `stackbuild` |
| 50 |
commands use `podman` to work with containers. |
| 51 |
|
| 52 |
As a next step, and with the root tree as a container, {kiwi} offers |
| 53 |
the opportunity to build images based on one or more containers. |
| 54 |
That's also the reason why the other command is called *stackbuild*, |
| 55 |
as it allows you to stack different root containers together. |
| 56 |
|
| 57 |
The `stash` and `stackbuild` commands can be used independently |
| 58 |
from each other. If there is already a registry with containers |
| 59 |
that should be used to build images from, `stackbuild` can |
| 60 |
directly consume them. |
| 61 |
|
| 62 |
This concept leads to a number of use cases, and a few of them were |
| 63 |
picked and put into the abstract of this article. For the purpose |
| 64 |
of documenting the functionality of the commands, only a part of the |
| 65 |
possibilities are taken into account as follows: |
| 66 |
|
| 67 |
.. _stash: |
| 68 |
|
| 69 |
Create a stash |
| 70 |
-------------- |
| 71 |
|
| 72 |
The `stash` command creates an OCI-compliant container from a given |
| 73 |
{kiwi-product} image root tree and registers it in the local |
| 74 |
container registry. From there, a user can push it to any registry |
| 75 |
of choice. |
| 76 |
|
| 77 |
The following example creates a stash of a Tumbleweed build |
| 78 |
and illustrates how to register it in a foreign container |
| 79 |
registry: |
| 80 |
|
| 81 |
.. code:: bash |
| 82 |
|
| 83 |
# Build some image... |
| 84 |
$ git clone https://github.com/OSInside/kiwi.git |
| 85 |
$ sudo kiwi-ng system build \ |
| 86 |
--description kiwi/build-tests/x86/tumbleweed/test-image-MicroOS/ \ |
| 87 |
--set-repo http://download.opensuse.org/tumbleweed/repo/oss \ |
| 88 |
--target-dir /tmp/myTWToday |
| 89 |
|
| 90 |
# Stash the image root into a container |
| 91 |
$ sudo kiwi-ng system stash \ |
| 92 |
--root /tmp/myTWToday/build/image-root \ |
| 93 |
--container-name twmos-snapshot |
| 94 |
|
| 95 |
# Register the stash in a registry |
| 96 |
$ podman login |
| 97 |
$ podman push twmos-snapshot \ |
| 98 |
docker://docker.io/.../twmos-snapshot:2021-10-08 |
| 99 |
|
| 100 |
If the `stash` command is called multiple times with the same |
| 101 |
container name, this leads to a new layer in the container for |
| 102 |
each call. To inspect the number of layers added to the |
| 103 |
container, the following command can be used: |
| 104 |
|
| 105 |
.. code:: bash |
| 106 |
|
| 107 |
$ podman inspect twmos-snapshot |
| 108 |
|
| 109 |
To list all stashes created by the `stash` command, the following |
| 110 |
command can be used: |
| 111 |
|
| 112 |
.. code:: bash |
| 113 |
|
| 114 |
$ kiwi-ng system stash --list |
| 115 |
|
| 116 |
Rebuild from a stash |
| 117 |
-------------------- |
| 118 |
|
| 119 |
The `stackbuild` command takes the given container(s) from the local or |
| 120 |
remote registry and uses it/them to either rebuild an image from that |
| 121 |
data or build a new image on top of that data. If multiple containers |
| 122 |
are given, the `stackbuild` command stacks them together in the order |
| 123 |
in which they were provided. |
| 124 |
|
| 125 |
.. note:: |
| 126 |
|
| 127 |
When using multiple containers, the result stack root tree is |
| 128 |
created from a sequence of rsync commands into the same target |
| 129 |
directory. The `stackbuild` command does this with any container |
| 130 |
content given and does not check, validate, or guarantee that |
| 131 |
the selection of containers is actually stackable or leads to a |
| 132 |
usable root tree. This means it's the caller's responsibility |
| 133 |
to make sure the provided containers can actually |
| 134 |
be stacked together in the given order. |
| 135 |
|
| 136 |
To simply rebuild the image from the stash created in :ref:`stash`, |
| 137 |
call `stackbuild` as follows: |
| 138 |
|
| 139 |
.. code:: bash |
| 140 |
|
| 141 |
# Delete the image |
| 142 |
$ sudo rm -rf /tmp/myTWToday |
| 143 |
|
| 144 |
# Rebuild image from stash |
| 145 |
$ sudo kiwi-ng system stackbuild \ |
| 146 |
--stash twmos-snapshot \ |
| 147 |
--target-dir /tmp/myTWToday |
| 148 |
|
| 149 |
This rebuilds the image from the stash and the {kiwi} configuration |
| 150 |
inside of the stash. As all rootfs data is already in the stash, the |
| 151 |
command will not need external resources to rebuild the image. |
| 152 |
|
| 153 |
Additional arguments for the `kiwi-ng system create` command, or for the |
| 154 |
`kiwi-ng system build` command if `--description` is given, can be passed |
| 155 |
after the `kiwi` subcommand. For compatibility with the former |
| 156 |
`kiwi-stackbuild-plugin`, the `--` separator is accepted as an alias for |
| 157 |
`kiwi`. For all options, see :ref:`kiwi_system_stackbuild` and |
| 158 |
:ref:`kiwi_system_stash`. |
| 159 |
|
| 160 |
Turn a container into a VM image |
| 161 |
-------------------------------- |
| 162 |
|
| 163 |
Another use case for the `stackbuild` command is the transformation |
| 164 |
of container images into another image type that is supported by {kiwi}. |
| 165 |
The following example demonstrates how an existing container image |
| 166 |
from the openSUSE registry can be turned into a virtual machine image. |
| 167 |
|
| 168 |
When moving a container into a virtual machine image, the following |
| 169 |
aspects have to be taken into account: |
| 170 |
|
| 171 |
1. A container image usually has no kernel installed. |
| 172 |
2. A container image usually has no bootloader installed. |
| 173 |
3. A container image usually has no user configured. |
| 174 |
|
| 175 |
For a VM image, the mentioned aspects are mandatory. Therefore, |
| 176 |
the following {kiwi} image description contains this additional |
| 177 |
information that the container cannot provide: Create the |
| 178 |
{kiwi} description as follows: |
| 179 |
|
| 180 |
.. code:: bash |
| 181 |
|
| 182 |
$ mkdir container_to_VM_layer |
| 183 |
$ vi container_to_VM_layer/config.kiwi |
| 184 |
|
| 185 |
And place the following content: |
| 186 |
|
| 187 |
.. code:: xml |
| 188 |
|
| 189 |
<?xml version="1.0" encoding="utf-8"?> |
| 190 |
|
| 191 |
<image schemaversion="{schema_version}" name="Leap-VM"> |
| 192 |
<description type="system"> |
| 193 |
<author>The Author</author> |
| 194 |
<contact>[email protected]</contact> |
| 195 |
<specification> |
| 196 |
Leap Container as VM |
| 197 |
</specification> |
| 198 |
</description> |
| 199 |
<preferences> |
| 200 |
<type image="oem" filesystem="xfs" firmware="uefi"> |
| 201 |
<oemconfig> |
| 202 |
<oem-resize>false</oem-resize> |
| 203 |
</oemconfig> |
| 204 |
</type> |
| 205 |
<version>1.99.1</version> |
| 206 |
<packagemanager>zypper</packagemanager> |
| 207 |
<locale>en_US</locale> |
| 208 |
<keytable>us</keytable> |
| 209 |
<timezone>UTC</timezone> |
| 210 |
</preferences> |
| 211 |
<repository type="rpm-md" alias="Leap"> |
| 212 |
<source path="{exc_repo_leap}"/> |
| 213 |
</repository> |
| 214 |
<packages type="image"> |
| 215 |
<package name="grub2"/> |
| 216 |
<package name="grub2-x86_64-efi" arch="x86_64"/> |
| 217 |
<package name="grub2-i386-pc"/> |
| 218 |
<package name="shim"/> |
| 219 |
<package name="kernel-default"/> |
| 220 |
</packages> |
| 221 |
<users> |
| 222 |
<user password="$1$wYJUgpM5$RXMMeASDc035eX.NbYWFl0" home="/root" name="root" groups="root"/> |
| 223 |
</users> |
| 224 |
</image> |
| 225 |
|
| 226 |
To build the virtual machine image from the currently hosted |
| 227 |
Leap {exc_os_version} container at SUSE, call the following `stackbuild` |
| 228 |
command: |
| 229 |
|
| 230 |
.. code:: bash |
| 231 |
|
| 232 |
$ sudo kiwi-ng system stackbuild \ |
| 233 |
--stash leap:{exc_os_version} \ |
| 234 |
--from-registry registry.opensuse.org/opensuse \ |
| 235 |
--target-dir /tmp/myLeap \ |
| 236 |
--description container_to_VM_layer |
| 237 |
|
| 238 |
The resulting virtual machine image can be booted as follows: |
| 239 |
|
| 240 |
.. code:: bash |
| 241 |
|
| 242 |
$ qemu-kvm Leap-VM.x86_64-1.99.1.raw |