Debian Codegraph @

doc/source/plugins/stackbuild.rst

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