]>
Commit | Line | Data |
---|---|---|
7c20daf6 FS |
1 | --- |
2 | title: Portable Services Introduction | |
46cdbd49 BR |
3 | category: Concepts |
4 | layout: default | |
7c20daf6 FS |
5 | --- |
6 | ||
b012e921 MB |
7 | # Portable Services Introduction |
8 | ||
9 | This systemd version includes a preview of the "portable service" | |
10 | concept. "Portable Services" are supposed to be an incremental improvement over | |
11 | traditional system services, making two specific facets of container management | |
12 | available to system services more readily. Specifically: | |
13 | ||
14 | 1. The bundling of applications, i.e. packing up multiple services, their | |
15 | binaries and all their dependencies in a single image, and running them | |
16 | directly from it. | |
17 | ||
18 | 2. Stricter default security policies, i.e. sand-boxing of applications. | |
19 | ||
20 | The primary tool for interfacing with "portable services" is the new | |
21 | "portablectl" program. It's currently shipped in /usr/lib/systemd/portablectl | |
22 | (i.e. not in the `$PATH`), since it's not yet considered part of the officially | |
23 | supported systemd interfaces — it's a preview still after all. | |
24 | ||
25 | Portable services don't bring anything inherently new to the table. All they do | |
26 | is put together known concepts in a slightly nicer way to cover a specific set | |
27 | of use-cases in a nicer way. | |
28 | ||
6e866b33 | 29 | ## So, what *is* a "Portable Service"? |
b012e921 MB |
30 | |
31 | A portable service is ultimately just an OS tree, either inside of a directory | |
32 | tree, or inside a raw disk image containing a Linux file system. This tree is | |
33 | called the "image". It can be "attached" or "detached" from the system. When | |
34 | "attached" specific systemd units from the image are made available on the host | |
35 | system, then behaving pretty much exactly like locally installed system | |
36 | services. When "detached" these units are removed again from the host, leaving | |
37 | no artifacts around (except maybe messages they might have logged). | |
38 | ||
39 | The OS tree/image can be created with any tool of your choice. For example, you | |
40 | can use `dnf --installroot=` if you like, or `debootstrap`, the image format is | |
41 | entirely generic, and doesn't have to carry any specific metadata beyond what | |
42 | distribution images carry anyway. Or to say this differently: the image format | |
43 | doesn't define any new metadata as unit files and OS tree directories or disk | |
44 | images are already sufficient, and pretty universally available these days. One | |
45 | particularly nice tool for creating suitable images is | |
46 | [mkosi](https://github.com/systemd/mkosi), but many other existing tools will | |
47 | do too. | |
48 | ||
49 | If you so will, "Portable Services" are a nicer way to manage chroot() | |
50 | environments, with better security, tooling and behavior. | |
51 | ||
6e866b33 | 52 | ## Where's the difference to a "Container"? |
b012e921 MB |
53 | |
54 | "Container" is a very vague term, after all it is used for | |
55 | systemd-nspawn/LXC-type OS containers, for Docker/rkt-like micro service | |
56 | containers, and even certain 'lightweight' VM runtimes. | |
57 | ||
58 | The "portable service" concept ultimately will not provide a fully isolated | |
59 | environment to the payload, like containers mostly intend to. Instead they are | |
60 | from the beginning more alike regular system services, can be controlled with | |
61 | the same tools, are exposed the same way in all infrastructure and so on. Their | |
8b3d4ff0 | 62 | main difference is that they use a different root directory than the rest of the |
b012e921 | 63 | system. Hence, the intention is not to run code in a different, isolated world |
8b3d4ff0 | 64 | from the host — like most containers would do it — but to run it in the same |
b012e921 MB |
65 | world, but with stricter access controls on what the service can see and do. |
66 | ||
67 | As one point of differentiation: as programs run as "portable services" are | |
68 | pretty much regular system services, they won't run as PID 1 (like Docker would | |
8b3d4ff0 | 69 | do it), but as normal processes. A corollary of that is that they aren't supposed |
b012e921 MB |
70 | to manage anything in their own environment (such as the network) as the |
71 | execution environment is mostly shared with the rest of the system. | |
72 | ||
73 | The primary focus use-case of "portable services" is to extend the host system | |
74 | with encapsulated extensions, but provide almost full integration with the rest | |
75 | of the system, though possibly restricted by effective security knobs. This | |
76 | focus includes system extensions otherwise sometimes called "super-privileged | |
77 | containers". | |
78 | ||
79 | Note that portable services are only available for system services, not for | |
8b3d4ff0 MB |
80 | user services (i.e. the functionality cannot be used for the stuff |
81 | bubblewrap/flatpak is focusing on). | |
b012e921 | 82 | |
6e866b33 | 83 | ## Mode of Operation |
b012e921 | 84 | |
8b3d4ff0 | 85 | If you have a portable service image, maybe in a raw disk image called |
b012e921 MB |
86 | `foobar_0.7.23.raw`, then attaching the services to the host is as easy as: |
87 | ||
88 | ``` | |
89 | # /usr/lib/systemd/portablectl attach foobar_0.7.23.raw | |
90 | ``` | |
91 | ||
92 | This command does the following: | |
93 | ||
7c20daf6 FS |
94 | 1. It dissects the image, checks and validates the `/etc/os-release` |
95 | (or `/usr/lib/os-release`, see below) data of the image, and looks for | |
96 | all included unit files. | |
b012e921 MB |
97 | |
98 | 2. It copies out all unit files with a suffix of `.service`, `.socket`, | |
99 | `.target`, `.timer` and `.path`. whose name begins with the image's name | |
100 | (with the .raw removed), truncated at the first underscore (if there is | |
101 | one). This prefix name generated from the image name must be followed by a | |
102 | ".", "-" or "@" character in the unit name. Or in other words, given the | |
103 | image name of `foobar_0.7.23.raw` all unit files matching | |
104 | `foobar-*.{service|socket|target|timer|path}`, | |
105 | `foobar@.{service|socket|target|timer|path}` as well as | |
106 | `foobar.*.{service|socket|target|timer|path}` and | |
107 | `foobar.{service|socket|target|timer|path}` are copied out. These unit files | |
6e866b33 MB |
108 | are placed in `/etc/systemd/system.attached/` (which is part of the normal |
109 | unit file search path of PID 1, and thus loaded exactly like regular unit | |
110 | files). Within the images the unit files are looked for at the usual | |
111 | locations, i.e. in `/usr/lib/systemd/system/` and `/etc/systemd/system/` and | |
112 | so on, relative to the image's root. | |
b012e921 MB |
113 | |
114 | 3. For each such unit file a drop-in file is created. Let's say | |
115 | `foobar-waldo.service` was one of the unit files copied to | |
6e866b33 MB |
116 | `/etc/systemd/system.attached/`, then a drop-in file |
117 | `/etc/systemd/system.attached/foobar-waldo.service.d/20-portable.conf` is | |
118 | created, containing a few lines of additional configuration: | |
b012e921 MB |
119 | |
120 | ``` | |
121 | [Service] | |
122 | RootImage=/path/to/foobar.raw | |
123 | Environment=PORTABLE=foobar | |
124 | LogExtraFields=PORTABLE=foobar | |
125 | ``` | |
126 | ||
127 | 4. For each such unit a "profile" drop-in is linked in. This "profile" drop-in | |
128 | generally contains security options that lock down the service. By default | |
129 | the `default` profile is used, which provides a medium level of | |
130 | security. There's also `trusted` which runs the service at the highest | |
131 | privileges, i.e. host's root and everything. The `strict` profile comes with | |
132 | the toughest security restrictions. Finally, `nonetwork` is like `default` | |
133 | but without network access. Users may define their own profiles too (or | |
134 | modify the existing ones) | |
135 | ||
136 | And that's already it. | |
137 | ||
8b3d4ff0 | 138 | Note that the images need to stay around (and in the same location) as long as the |
b012e921 MB |
139 | portable service is attached. If an image is moved, the `RootImage=` line |
140 | written to the unit drop-in would point to an non-existing place, and break the | |
141 | logic. | |
142 | ||
143 | The `portablectl detach` command executes the reverse operation: it looks for | |
144 | the drop-ins and the unit files associated with the image, and removes them | |
145 | again. | |
146 | ||
8b3d4ff0 | 147 | Note that `portablectl attach` won't enable or start any of the units it copies |
b012e921 MB |
148 | out. This still has to take place in a second, separate step. (That said We |
149 | might add options to do this automatically later on.). | |
150 | ||
6e866b33 | 151 | ## Requirements on Images |
b012e921 MB |
152 | |
153 | Note that portable services don't introduce any new image format, but most OS | |
154 | images should just work the way they are. Specifically, the following | |
155 | requirements are made for an image that can be attached/detached with | |
156 | `portablectl`. | |
157 | ||
bb4f798a MB |
158 | 1. It must contain an executable that shall be invoked, along with all its |
159 | dependencies. If binary code, the code needs to be compiled for an | |
160 | architecture compatible with the host. | |
b012e921 MB |
161 | |
162 | 2. The image must either be a plain sub-directory (or btrfs subvolume) | |
163 | containing the binaries and its dependencies in a classic Linux OS tree, or | |
164 | must be a raw disk image either containing only one, naked file system, or | |
165 | an image with a partition table understood by the Linux kernel with only a | |
166 | single partition defined, or alternatively, a GPT partition table with a set | |
167 | of properly marked partitions following the [Discoverable Partitions | |
46cdbd49 | 168 | Specification](https://systemd.io/DISCOVERABLE_PARTITIONS). |
b012e921 MB |
169 | |
170 | 3. The image must at least contain one matching unit file, with the right name | |
171 | prefix and suffix (see above). The unit file is searched in the usual paths, | |
172 | i.e. primarily /etc/systemd/system/ and /usr/lib/systemd/system/ within the | |
173 | image. (The implementation will check a couple of other paths too, but it's | |
174 | recommended to use these two paths.) | |
175 | ||
7c20daf6 FS |
176 | 4. The image must contain an os-release file, either in `/etc/os-release` or |
177 | `/usr/lib/os-release`. The file should follow the standard format. | |
178 | ||
179 | 5. The image must contain the files `/etc/resolv.conf` and `/etc/machine-id` | |
180 | (empty files are ok), they will be bind mounted from the host at runtime. | |
b012e921 | 181 | |
bb4f798a MB |
182 | 6. The image must contain directories `/proc/`, `/sys/`, `/dev/`, `/run/`, |
183 | `/tmp/`, `/var/tmp/` that can be mounted over with the corresponding version | |
184 | from the host. | |
185 | ||
186 | 7. The OS might require other files or directories to be in place. For example, | |
187 | if the image is built based on glibc, the dynamic loader needs to be | |
188 | available in `/lib/ld-linux.so.2` or `/lib64/ld-linux-x86-64.so.2` (or | |
189 | similar, depending on architecture), and if the distribution implements a | |
190 | merged `/usr/` tree, this means `/lib` and/or `/lib64` need to be symlinks | |
191 | to their respective counterparts below `/usr/`. For details see your | |
192 | distribution's documentation. | |
193 | ||
194 | Note that images created by tools such as `debootstrap`, `dnf --installroot=` | |
195 | or `mkosi` generally qualify for all of the above in one way or another. If you | |
196 | wonder what the most minimal image would be that complies with the requirements | |
197 | above, it could consist of this: | |
b012e921 MB |
198 | |
199 | ``` | |
bb4f798a MB |
200 | /usr/bin/minimald # a statically compiled binary |
201 | /usr/lib/systemd/system/minimal-test.service # the unit file for the service, with ExecStart=/usr/bin/minimald | |
202 | /usr/lib/os-release # an os-release file explaining what this is | |
203 | /etc/resolv.conf # empty file to mount over with host's version | |
204 | /etc/machine-id # ditto | |
205 | /proc/ # empty directory to use as mount point for host's API fs | |
206 | /sys/ # ditto | |
207 | /dev/ # ditto | |
208 | /run/ # ditto | |
209 | /tmp/ # ditto | |
210 | /var/tmp/ # ditto | |
b012e921 MB |
211 | ``` |
212 | ||
213 | And that's it. | |
214 | ||
215 | Note that qualifying images do not have to contain an init system of their | |
216 | own. If they do, it's fine, it will be ignored by the portable service logic, | |
217 | but they generally don't have to, and it might make sense to avoid any, to keep | |
218 | images minimal. | |
219 | ||
bb4f798a MB |
220 | If the image is writable, and some of the files or directories that are |
221 | overmounted from the host do not exist yet they are automatically created. On | |
222 | read-only, immutable images (e.g. squashfs images) all files and directories to | |
223 | over-mount must exist already. | |
224 | ||
b012e921 | 225 | Note that as no new image format or metadata is defined, it's very |
8b3d4ff0 | 226 | straightforward to define images than can be made use of in a number of |
b012e921 MB |
227 | different ways. For example, by using `mkosi -b` you can trivially build a |
228 | single, unified image that: | |
229 | ||
230 | 1. Can be attached as portable service, to run any container services natively | |
231 | on the host. | |
232 | ||
233 | 2. Can be run as OS container, using `systemd-nspawn`, by booting the image | |
234 | with `systemd-nspawn -i -b`. | |
235 | ||
236 | 3. Can be booted directly as VM image, using a generic VM executor such as | |
237 | `virtualbox`/`qemu`/`kvm` | |
238 | ||
239 | 4. Can be booted directly on bare-metal systems. | |
240 | ||
241 | Of course, to facilitate 2, 3 and 4 you need to include an init system in the | |
242 | image. To facility 3 and 4 you also need to include a boot loader in the | |
243 | image. As mentioned `mkosi -b` takes care of all of that for you, but any other | |
244 | image generator should work too. | |
245 | ||
6e866b33 | 246 | ## Execution Environment |
b012e921 MB |
247 | |
248 | Note that the code in portable service images is run exactly like regular | |
249 | services. Hence there's no new execution environment to consider. Oh, unlike | |
250 | Docker would do it, as these are regular system services they aren't run as PID | |
251 | 1 either, but with regular PID values. | |
252 | ||
6e866b33 | 253 | ## Access to host resources |
b012e921 MB |
254 | |
255 | If services shipped with this mechanism shall be able to access host resources | |
256 | (such as files or AF_UNIX sockets for IPC), use the normal `BindPaths=` and | |
257 | `BindReadOnlyPaths=` settings in unit files to mount them in. In fact the | |
258 | `default` profile mentioned above makes use of this to ensure | |
259 | `/etc/resolv.conf`, the D-Bus system bus socket or write access to the logging | |
260 | subsystem are available to the service. | |
261 | ||
6e866b33 | 262 | ## Instantiation |
b012e921 MB |
263 | |
264 | Sometimes it makes sense to instantiate the same set of services multiple | |
265 | times. The portable service concept does not introduce a new logic for this. It | |
266 | is recommended to use the regular unit templating of systemd for this, i.e. to | |
267 | include template units such as `foobar@.service`, so that instantiation is as | |
268 | simple as: | |
269 | ||
270 | ``` | |
271 | # /usr/lib/systemd/portablectl attach foobar_0.7.23.raw | |
272 | # systemctl enable --now foobar@instancea.service | |
273 | # systemctl enable --now foobar@instanceb.service | |
274 | … | |
275 | ``` | |
276 | ||
277 | The benefit of this approach is that templating works exactly the same for | |
278 | units shipped with the OS itself as for attached portable services. | |
279 | ||
6e866b33 | 280 | ## Immutable images with local data |
b012e921 MB |
281 | |
282 | It's a good idea to keep portable service images read-only during normal | |
283 | operation. In fact all but the `trusted` profile will default to this kind of | |
284 | behaviour, by setting the `ProtectSystem=strict` option. In this case writable | |
285 | service data may be placed on the host file system. Use `StateDirectory=` in | |
286 | the unit files to enable such behaviour and add a local data directory to the | |
287 | services copied onto the host. |