]>
Commit | Line | Data |
---|---|---|
c3e270f4 FB |
1 | --- |
2 | title: Hacking on systemd | |
4cdca0af | 3 | category: Contributing |
b41a3f66 | 4 | layout: default |
c3e270f4 FB |
5 | --- |
6 | ||
5a8a9dee FA |
7 | # Hacking on systemd |
8 | ||
9 | We welcome all contributions to systemd. If you notice a bug or a missing | |
10 | feature, please feel invited to fix it, and submit your work as a GitHub Pull | |
11 | Request (PR) at https://github.com/systemd/systemd/pull/new. | |
12 | ||
13 | Please make sure to follow our [Coding Style](CODING_STYLE.md) when submitting patches. | |
14 | Also have a look at our [Contribution Guidelines](CONTRIBUTING.md). | |
15 | ||
16 | When adding new functionality, tests should be added. For shared functionality | |
17 | (in `src/basic/` and `src/shared/`) unit tests should be sufficient. The general | |
18 | policy is to keep tests in matching files underneath `src/test/`, | |
19 | e.g. `src/test/test-path-util.c` contains tests for any functions in | |
20 | `src/basic/path-util.c`. If adding a new source file, consider adding a matching | |
21 | test executable. For features at a higher level, tests in `src/test/` are very | |
1e268f42 | 22 | strongly recommended. If that is not possible, integration tests in `test/` are |
5a8a9dee FA |
23 | encouraged. |
24 | ||
25 | Please also have a look at our list of [code quality tools](CODE_QUALITY.md) we have setup for systemd, | |
26 | to ensure our codebase stays in good shape. | |
27 | ||
28 | Please always test your work before submitting a PR. For many of the components | |
29 | of systemd testing is straight-forward as you can simply compile systemd and | |
30 | run the relevant tool from the build directory. | |
31 | ||
32 | For some components (most importantly, systemd/PID1 itself) this is not | |
33 | possible, however. In order to simplify testing for cases like this we provide | |
34 | a set of `mkosi` build files directly in the source tree. `mkosi` is a tool for | |
35 | building clean OS images from an upstream distribution in combination with a | |
36 | fresh build of the project in the local working directory. To make use of this, | |
37 | please acquire `mkosi` from https://github.com/systemd/mkosi first, unless your | |
38 | distribution has packaged it already and you can get it from there. After the | |
172ad053 DDM |
39 | tool is installed, symlink the settings file for your distribution of choice from |
40 | .mkosi/ to mkosi.default in the project root directory (note that the package | |
41 | manager for this distro needs to be installed on your host system). After doing | |
42 | that, it is sufficient to type `mkosi` in the systemd project directory to | |
43 | generate a disk image `image.raw` you can boot either in `systemd-nspawn` or in | |
44 | an UEFI-capable VM: | |
5a8a9dee FA |
45 | |
46 | ``` | |
c38667f7 | 47 | # mkosi boot |
5a8a9dee FA |
48 | ``` |
49 | ||
50 | or: | |
51 | ||
52 | ``` | |
c38667f7 | 53 | # mkosi qemu |
5a8a9dee FA |
54 | ``` |
55 | ||
56 | Every time you rerun the `mkosi` command a fresh image is built, incorporating | |
c38667f7 DDM |
57 | all current changes you made to the project tree. To save time when rebuilding, |
58 | you can use mkosi's incremental mode (`-i`). This instructs mkosi to build a set | |
59 | of cache images that make future builds a lot faster. Note that the `-i` flag | |
60 | both instructs mkosi to build cached images if they don't exist yet and to use | |
61 | cached images if they already exist so make sure to always specify `-i` if you | |
62 | want mkosi to use the cached images. | |
63 | ||
64 | If you're going to build mkosi images that use the same distribution and release | |
65 | that you're currently using, you can speed up the initial mkosi run by having it | |
66 | reuse the host's package cache. To do this, create a mkosi override file in | |
67 | mkosi.default.d/ (e.g 20-local.conf) and add the following contents: | |
5a8a9dee | 68 | |
c38667f7 DDM |
69 | ``` |
70 | [Packages] | |
71 | Cache=<full-path-to-package-manager-cache> # (e.g. /var/cache/dnf) | |
72 | ``` | |
5a8a9dee | 73 | |
c38667f7 DDM |
74 | If you want to do a local build without mkosi, most distributions also provide |
75 | very simple and convenient ways to install all development packages necessary | |
76 | to build systemd. For example, on Fedora the following command line should be | |
77 | sufficient to install all of systemd's build dependencies: | |
5a8a9dee FA |
78 | |
79 | ``` | |
80 | # dnf builddep systemd | |
81 | ``` | |
82 | ||
83 | Putting this all together, here's a series of commands for preparing a patch | |
84 | for systemd (this example is for Fedora): | |
85 | ||
86 | ```sh | |
172ad053 DDM |
87 | $ sudo dnf builddep systemd # install build dependencies |
88 | $ sudo dnf install mkosi # install tool to quickly build images | |
5a8a9dee FA |
89 | $ git clone https://github.com/systemd/systemd.git |
90 | $ cd systemd | |
172ad053 DDM |
91 | $ vim src/core/main.c # or wherever you'd like to make your changes |
92 | $ meson build # configure the build | |
8b08be40 LP |
93 | $ meson compile -C build # build it locally, see if everything compiles fine |
94 | $ meson test -C build # run some simple regression tests | |
172ad053 | 95 | $ ln -s .mkosi/mkosi.fedora mkosi.default # Configure mkosi to build a fedora image |
172ad053 | 96 | $ sudo mkosi # build a test image |
c38667f7 | 97 | $ sudo mkosi boot # boot up the test image |
172ad053 DDM |
98 | $ git add -p # interactively put together your patch |
99 | $ git commit # commit it | |
5a8a9dee | 100 | $ git push REMOTE HEAD:refs/heads/BRANCH |
172ad053 DDM |
101 | # where REMOTE is your "fork" on GitHub |
102 | # and BRANCH is a branch name. | |
5a8a9dee FA |
103 | ``` |
104 | ||
105 | And after that, head over to your repo on GitHub and click "Compare & pull request" | |
106 | ||
107 | Happy hacking! | |
108 | ||
109 | ||
4c8e5f44 ZJS |
110 | ## Developer and release modes |
111 | ||
112 | In the default meson configuration (`-Dmode=developer`), certain checks are | |
113 | enabled that are suitable when hacking on systemd (such as internal | |
114 | documentation consistency checks). Those are not useful when compiling for code | |
115 | for distribution and can be disabled by setting `-Dmode=release`. | |
116 | ||
5a8a9dee FA |
117 | ## Fuzzers |
118 | ||
119 | systemd includes fuzzers in `src/fuzz/` that use libFuzzer and are automatically | |
135a1add | 120 | run by [OSS-Fuzz](https://github.com/google/oss-fuzz) with sanitizers. |
53a42e62 | 121 | To add a fuzz target, create a new `src/fuzz/fuzz-foo.c` file with a `LLVMFuzzerTestOneInput` |
5a8a9dee FA |
122 | function and add it to the list in `src/fuzz/meson.build`. |
123 | ||
124 | Whenever possible, a seed corpus and a dictionary should also be added with new | |
125 | fuzz targets. The dictionary should be named `src/fuzz/fuzz-foo.dict` and the seed | |
126 | corpus should be built and exported as `$OUT/fuzz-foo_seed_corpus.zip` in | |
127 | `tools/oss-fuzz.sh`. | |
128 | ||
129 | The fuzzers can be built locally if you have libFuzzer installed by running | |
130 | `tools/oss-fuzz.sh`. You should also confirm that the fuzzer runs in the | |
131 | OSS-Fuzz environment by checking out the OSS-Fuzz repo, and then running | |
132 | commands like this: | |
133 | ||
134 | ``` | |
135 | python infra/helper.py build_image systemd | |
136 | python infra/helper.py build_fuzzers --sanitizer memory systemd ../systemd | |
137 | python infra/helper.py run_fuzzer systemd fuzz-foo | |
138 | ``` | |
139 | ||
140 | If you find a bug that impacts the security of systemd, please follow the | |
141 | guidance in [CONTRIBUTING.md](CONTRIBUTING.md) on how to report a security vulnerability. | |
142 | ||
143 | For more details on building fuzzers and integrating with OSS-Fuzz, visit: | |
144 | ||
6cec69fc LK |
145 | - [Setting up a new project - OSS-Fuzz](https://google.github.io/oss-fuzz/getting-started/new-project-guide/) |
146 | - [Tutorials - OSS-Fuzz](https://google.github.io/oss-fuzz/reference/useful-links/#tutorials) | |
4cc06b80 DDM |
147 | |
148 | ## mkosi + clangd | |
149 | ||
150 | [clangd](https://clangd.llvm.org/) is a language server that provides code completion, diagnostics and more | |
151 | right in your editor of choice (with the right plugin installed). When using mkosi, we can run clangd in the | |
152 | mkosi build container to avoid needing to build systemd on the host machine just to make clangd work. To | |
153 | achieve this, create a script with the following contents in systemd's project directory on the host: | |
154 | ||
155 | ```sh | |
156 | #!/usr/bin/env sh | |
157 | tee mkosi-clangd.build > /dev/null << EOF | |
158 | #!/usr/bin/env sh | |
159 | exec clangd \\ | |
160 | --compile-commands-dir=/root/build \\ | |
161 | --path-mappings=\\ | |
162 | "\\ | |
163 | $(pwd)=/root/src,\\ | |
164 | $(pwd)/mkosi.builddir=/root/build,\\ | |
165 | $(pwd)/mkosi.includedir=/usr/include,\\ | |
166 | $(pwd)/mkosi.installdir=/root/dest\\ | |
167 | " \\ | |
168 | --header-insertion=never | |
169 | EOF | |
170 | chmod +x mkosi-clangd.build | |
171 | exec sudo mkosi --source-file-transfer=mount --incremental --skip-final-phase --build-script mkosi-clangd.build build | |
172 | ``` | |
173 | ||
174 | Next, mark the script as executable and point your editor plugin to use this script to start clangd. For | |
175 | vscode's clangd extension, this is done via setting the `clangd.path` option to the path of the | |
176 | mkosi-clangd.sh script. | |
177 | ||
178 | To be able to navigate to include files of systemd's dependencies, we need to make the /usr/include folder of | |
179 | the build image available on the host. mkosi supports this by setting the `IncludeDirectory` option in | |
180 | mkosi's config. The easiest way to set the option is to create a file 20-local.conf in mkosi.default.d/ and | |
181 | add the following contents: | |
182 | ||
183 | ``` | |
184 | [Packages] | |
185 | IncludeDirectory=mkosi.includedir | |
186 | ``` | |
187 | ||
188 | This will make the contents of /usr/include available in mkosi.includedir in the systemd project directory. | |
189 | We already configured clangd to map any paths in /usr/include in the build image to mkosi.includedir/ on the | |
190 | host in the mkosi-clangd.sh script. | |
191 | ||
192 | We also need to make sure clangd is installed in the build image. To have mkosi install clangd in the build | |
193 | image, edit the 20-local.conf file we created earlier and add the following contents under the `[Packages]` | |
194 | section: | |
195 | ||
196 | ``` | |
197 | BuildPackages=<clangd-package> | |
198 | ``` | |
199 | ||
200 | Note that the exact package containing clangd will differ depending on the distribution used. Some | |
201 | distributions have a separate clangd package, others put the clangd binary in a clang-tools-extra package and | |
202 | some bundle clangd in the clang package. | |
203 | ||
204 | Because mkosi needs to run as root, we also need to make sure we can enter the root password when the editor | |
205 | plugin tries to run the mkosi-clangd.sh script. To be able to enter the root password in non-interactive | |
206 | scripts, we use an askpass provider. This is a program that sudo will launch if it detects it's being | |
207 | executed from a non-interactive shell so that the root password can still be entered. There are multiple | |
208 | implementations such as gnome askpass and KDE askpass. Install one of the askpass packages your distro | |
209 | provides and set the `SUDO_ASKPASS` environment variable to the path of the askpass binary you want to use. | |
210 | If configured correctly, a window will appear when your editor plugin tries to run the mkosi-clangd.sh script | |
211 | allowing you to enter the root password. | |
212 | ||
213 | Due to a bug in btrfs, it's currently impossible to mount two mkosi btrfs images at the same time. Because of | |
214 | this, trying to do a regular build while the clangd image is running will fail. To circumvent this, use ext4 | |
215 | instead of btrfs for the images by adding the following contents to 20-local.conf: | |
216 | ||
217 | ``` | |
218 | [Output] | |
219 | Format=gpt_ext4 | |
220 | ``` | |
221 | ||
222 | Finally, to ensure clangd starts up quickly in the editor, run an incremental build with mkosi to make sure | |
223 | the cached images are initialized (`mkosi -i`). | |
224 | ||
225 | Now, your editor will start clangd in the mkosi build image and all of clangd's features will work as | |
226 | expected. |