2017-12-24 09:53:20 +03:00
#!/bin/sh
2021-10-01 14:04:32 +03:00
# SPDX-License-Identifier: LGPL-2.1-or-later
2020-12-06 19:45:01 +03:00
set -e
2016-07-16 03:00:44 +03:00
# This is a build script for OS image generation using mkosi (https://github.com/systemd/mkosi).
# Simply invoke "mkosi" in the project directory to build an OS image.
2022-07-15 03:26:52 +03:00
ASAN_OPTIONS=strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1:disable_coredump=0:use_madv_dontdump=1
UBSAN_OPTIONS=print_stacktrace=1:print_summary=1:halt_on_error=1
2021-04-01 18:58:54 +03:00
# On Fedora "ld" is (unfortunately — if you ask me) managed via
# "alternatives". Since we'd like to support building images in environments
# with only /usr/ around (e.g. mkosi's UsrOnly=1 option), we have the problem
# that /usr/bin/ld is a symlink that points to a non-existing file in
# /etc/alternative/ in this mode. Let's work around this for now by manually
# redirect "ld" to "ld.bfd", i.e. circumventing the /usr/bin/ld symlink.
2021-09-02 15:22:19 +03:00
if [ ! -x /usr/bin/ld ] && [ -x /usr/bin/ld.bfd ]; then
2021-04-01 18:58:54 +03:00
mkdir -p "$HOME"/bin
ln -s /usr/bin/ld.bfd "$HOME"/bin/ld
PATH="$HOME/bin:$PATH"
fi
2017-07-12 20:58:53 +03:00
# If mkosi.builddir/ exists mkosi will set $BUILDDIR to it, let's then use it
# as out-of-tree build dir. Otherwise, let's make up our own builddir.
[ -z "$BUILDDIR" ] && BUILDDIR=build
2018-03-05 19:03:52 +03:00
# Meson uses Python 3 and requires a locale with an UTF-8 character map.
# Not running under UTF-8 makes the `ninja test` step break with a CodecError.
# So let's ensure we're running under UTF-8.
#
# If our current locale already is UTF-8, then we don't need to do anything:
2021-09-02 15:22:19 +03:00
if [ "$(locale charmap 2>/dev/null)" != "UTF-8" ] ; then
2018-03-05 19:03:52 +03:00
# Try using C.UTF-8 locale, if available. This locale is not shipped
# by upstream glibc, so it's not available in all distros.
# (In particular, it's not available in Arch Linux.)
2020-12-06 21:16:00 +03:00
if locale -a | grep -q -E "C.UTF-8|C.utf8"; then
export LC_CTYPE=C.UTF-8
# Finally, try something like en_US.UTF-8, which should be
# available in Arch Linux, but is not present in Debian's
# minimal image in our mkosi config.
elif locale -a | grep -q en_US.utf8; then
2018-03-05 19:03:52 +03:00
export LC_CTYPE=en_US.UTF-8
2020-12-06 21:16:00 +03:00
else
# If nothing works, fail early.
echo "*** Could not find a valid locale that supports UTF-8. ***" >&2
exit 1
2018-03-05 19:03:52 +03:00
fi
fi
2017-07-18 11:30:52 +03:00
2022-08-22 14:21:07 +03:00
# The bpftool script shipped by Ubuntu tries to find the actual program to run via querying `uname -r` and
# using the current kernel version. This obviously doesn't work in containers. As a workaround, we override
# the ubuntu script with a symlink to the first bpftool program we can find.
for bpftool in /usr/lib/linux-tools/*/bpftool; do
[ -x "$bpftool" ] || continue
ln -sf "$bpftool" /usr/sbin/bpftool
break
done
2022-10-14 15:53:41 +03:00
# CentOS Stream 8 includes bpftool 4.18.0 which is lower than what we need. However, they've backported the
# specific feature we need ("gen skeleton") to this version, so we replace bpftool with a script that reports
# version 5.6.0 to satisfy meson which makes bpf work on CentOS Stream 8 as well.
if [ "$(grep '^ID=' /etc/os-release)" = "ID=\"centos\"" ] && [ "$(grep '^VERSION=' /etc/os-release)" = "VERSION=\"8\"" ]; then
cp /usr/sbin/bpftool /usr/sbin/bpftool.real
cat > /usr/sbin/bpftool <<EOF
#!/bin/sh
if [ "\$1" = --version ]; then
echo 5.6.0
else
exec /usr/sbin/bpftool.real \$@
fi
EOF
chmod +x /usr/sbin/bpftool
fi
2018-02-09 21:22:40 +03:00
if [ ! -f "$BUILDDIR"/build.ninja ] ; then
2021-09-02 15:22:19 +03:00
sysvinit_path=$(realpath /etc/init.d)
2017-10-25 21:42:38 +03:00
2021-09-02 15:22:19 +03:00
init_path=$(realpath /sbin/init 2>/dev/null)
2020-04-21 19:08:23 +03:00
if [ -z "$init_path" ] ; then
2022-07-16 11:15:29 +03:00
rootprefix=""
2020-04-21 19:08:23 +03:00
else
2022-07-16 11:15:29 +03:00
rootprefix=${init_path%/lib/systemd/systemd}
rootprefix=/${rootprefix#/}
2020-04-21 19:08:23 +03:00
fi
2021-01-09 22:05:56 +03:00
meson "$BUILDDIR" \
-D "sysvinit-path=$sysvinit_path" \
-D "rootprefix=$rootprefix" \
-D man=false \
2021-01-15 22:51:42 +03:00
-D translations=false \
2022-07-15 15:59:03 +03:00
-D version-tag="${VERSION_TAG}" \
2022-07-15 03:26:52 +03:00
-D mode=developer \
2022-08-16 13:50:29 +03:00
-D b_sanitize="${SANITIZERS:-none}" \
2022-08-22 14:21:07 +03:00
-D install-tests=true \
-D tests=unsafe \
-D slow-tests=true \
-D utmp=true \
-D hibernate=true \
-D ldconfig=true \
-D resolve=true \
-D efi=true \
-D tpm=true \
-D environment-d=true \
-D binfmt=true \
-D repart=true \
-D sysupdate=true \
-D coredump=true \
-D pstore=true \
-D oomd=true \
-D logind=true \
-D hostnamed=true \
-D localed=true \
-D machined=true \
-D portabled=true \
-D sysext=true \
-D userdb=true \
-D homed=true \
-D networkd=true \
-D timedated=true \
-D timesyncd=true \
-D remote=true \
-D nss-myhostname=true \
-D nss-mymachines=true \
-D nss-resolve=true \
-D nss-systemd=true \
-D firstboot=true \
-D randomseed=true \
-D backlight=true \
-D vconsole=true \
-D quotacheck=true \
-D sysusers=true \
-D tmpfiles=true \
-D importd=true \
-D hwdb=true \
-D rfkill=true \
-D xdg-autostart=true \
-D translations=true \
-D polkit=true \
-D acl=true \
-D audit=true \
-D blkid=true \
-D fdisk=true \
-D kmod=true \
-D pam=true \
-D pwquality=true \
-D microhttpd=true \
-D libcryptsetup=true \
-D libcurl=true \
-D idn=true \
-D libidn2=true \
-D qrencode=true \
-D gcrypt=true \
-D gnutls=true \
-D openssl=true \
-D cryptolib=openssl \
-D p11kit=true \
-D libfido2=true \
-D tpm2=true \
-D elfutils=true \
-D zstd=true \
-D xkbcommon=true \
-D pcre2=true \
-D glib=true \
-D dbus=true \
-D gnu-efi=true \
-D kernel-install=true \
-D analyze=true \
2022-10-14 15:26:50 +03:00
-D bpf-framework=true
2017-12-06 15:53:39 +03:00
fi
2021-01-09 19:32:00 +03:00
cd "$BUILDDIR"
2021-01-24 17:13:58 +03:00
ninja "$@"
2020-04-22 02:58:44 +03:00
if [ "$WITH_TESTS" = 1 ] ; then
for id in 1 2 3; do
2022-08-25 14:30:58 +03:00
getent group $id >/dev/null || echo "g testgroup$id $id -" | ./systemd-sysusers -
2020-04-22 02:58:44 +03:00
done
2022-07-15 03:26:52 +03:00
if [ -n "$SANITIZERS" ]; then
export ASAN_OPTIONS="$ASAN_OPTIONS"
export UBSAN_OPTIONS="$UBSAN_OPTIONS"
TIMEOUT_MULTIPLIER=3
else
TIMEOUT_MULTIPLIER=1
fi
2022-08-25 13:13:35 +03:00
meson test --print-errorlogs --timeout-multiplier=$TIMEOUT_MULTIPLIER
2020-04-22 02:58:44 +03:00
fi
2021-01-09 19:32:00 +03:00
cd "$SRCDIR"
2020-12-06 19:45:45 +03:00
# Ubuntu Focal is stuck with meson 0.53.0.
if [ "$(meson -v | cut -d . -f 2)" -gt 53 ] ; then
2020-12-19 17:25:41 +03:00
meson install -C "$BUILDDIR" --quiet --no-rebuild --only-changed
2020-12-06 19:45:45 +03:00
else
2020-12-19 17:25:41 +03:00
meson install -C "$BUILDDIR" --no-rebuild --only-changed
2020-12-06 19:45:45 +03:00
fi
2016-12-07 01:17:57 +03:00
2017-07-12 20:58:53 +03:00
mkdir -p "$DESTDIR"/etc
2016-12-07 01:17:57 +03:00
2021-09-02 15:22:19 +03:00
cat >"$DESTDIR"/etc/issue <<EOF
2016-12-07 01:17:57 +03:00
\S (built from systemd tree)
Kernel \r on an \m (\l)
EOF
2018-06-21 19:47:33 +03:00
2021-07-02 15:59:58 +03:00
if [ -n "$IMAGE_ID" ] ; then
2022-07-16 11:15:29 +03:00
mkdir -p "$DESTDIR"/usr/lib
sed -n \
-e '/^IMAGE_ID=/!p' \
-e "\$aIMAGE_ID=$IMAGE_ID" <"/usr/lib/os-release" >"${DESTDIR}/usr/lib/os-release"
2021-07-02 15:59:58 +03:00
2022-07-16 11:15:29 +03:00
OSRELEASEFILE="$DESTDIR"/usr/lib/os-release
2021-07-02 15:59:58 +03:00
else
2022-07-16 11:15:29 +03:00
OSRELEASEFILE=/usr/lib/os-release
2021-07-02 15:59:58 +03:00
fi
if [ -n "$IMAGE_VERSION" ] ; then
2022-07-16 11:15:29 +03:00
mkdir -p "$DESTDIR"/usr/lib
sed -n \
-e '/^IMAGE_VERSION=/!p' \
-e "\$aIMAGE_VERSION=$IMAGE_VERSION" <$OSRELEASEFILE >"/tmp/os-release.tmp"
2021-07-02 15:59:58 +03:00
2022-07-16 11:15:29 +03:00
cat /tmp/os-release.tmp > "$DESTDIR"/usr/lib/os-release
rm /tmp/os-release.tmp
2021-07-02 15:59:58 +03:00
fi
2021-12-08 21:31:25 +03:00
# If $CI_BUILD is set, copy over the CI service which executes a service check
# after boot and then shuts down the machine
if [ -n "$CI_BUILD" ]; then
2022-07-16 11:15:29 +03:00
mkdir -p "$DESTDIR/usr/lib/systemd/system"
cp -v "$SRCDIR/test/mkosi-check-and-shutdown.service" "$DESTDIR/usr/lib/systemd/system/mkosi-check-and-shutdown.service"
cp -v "$SRCDIR/test/mkosi-check-and-shutdown.sh" "$DESTDIR/usr/lib/systemd/mkosi-check-and-shutdown.sh"
chmod +x "$DESTDIR/usr/lib/systemd/mkosi-check-and-shutdown.sh"
2021-12-08 21:31:25 +03:00
fi
2022-07-15 03:26:52 +03:00
if [ -n "$SANITIZERS" ]; then
LD_PRELOAD=$(ldd $BUILDDIR/systemd | grep libasan.so | awk '{print $3}')
mkdir -p "$DESTDIR/etc/systemd/system.conf.d"
cat > "$DESTDIR/etc/systemd/system.conf.d/10-asan.conf" <<EOF
[Manager]
ManagerEnvironment=ASAN_OPTIONS=$ASAN_OPTIONS\\
UBSAN_OPTIONS=$UBSAN_OPTIONS\\
LD_PRELOAD=$LD_PRELOAD
DefaultEnvironment=ASAN_OPTIONS=$ASAN_OPTIONS\\
UBSAN_OPTIONS=$UBSAN_OPTIONS\\
LD_PRELOAD=$LD_PRELOAD
EOF
# ASAN logs to stderr by default. However, journald's stderr is connected to /dev/null, so we lose
# all the ASAN logs. To rectify that, let's connect journald's stdout to the console so that any
# sanitizer failures appear directly on the user's console.
mkdir -p "$DESTDIR/etc/systemd/system/systemd-journald.service.d"
cat > "$DESTDIR/etc/systemd/system/systemd-journald.service.d/10-stdout-tty.conf" <<EOF
[Service]
StandardOutput=tty
EOF
# Both systemd and util-linux's login call vhangup() on /dev/console which disconnects all users.
# This means systemd-journald can't log to /dev/console even if we configure `StandardOutput=tty`. As
# a workaround, we modify console-getty.service to disable systemd's vhangup() and disallow login
# from calling vhangup() so that journald's ASAN logs correctly end up in the console.
mkdir -p "$DESTDIR/etc/systemd/system/console-getty.service.d"
cat > "$DESTDIR/etc/systemd/system/console-getty.service.d/10-no-vhangup.conf" <<EOF
[Service]
TTYVHangup=no
CapabilityBoundingSet=~CAP_SYS_TTY_CONFIG
EOF
fi
2022-08-22 14:21:07 +03:00
# Make sure services aren't enabled by default on Debian/Ubuntu.
mkdir -p "$DESTDIR/etc/systemd/system-preset"
echo "disable *" > "$DESTDIR/etc/systemd/system-preset/99-mkosi.preset"
2022-09-09 09:53:37 +03:00
if [ -d mkosi.kernel/ ]; then
cd "$SRCDIR/mkosi.kernel"
mkdir -p "$BUILDDIR/mkosi.kernel"
2022-11-02 14:19:11 +03:00
# Ensure fast incremental builds by fixating these values which usually change for each build.
export KBUILD_BUILD_TIMESTAMP="Fri Jun 5 15:58:00 CEST 2015"
export KBUILD_BUILD_HOST="mkosi"
2022-09-09 09:53:37 +03:00
make O="$BUILDDIR/mkosi.kernel" defconfig
scripts/config \
2022-11-02 14:19:11 +03:00
--file "$BUILDDIR/mkosi.kernel/.config" \
2022-09-09 09:53:37 +03:00
--enable BPF_SYSCALL \
--enable BPF_JIT \
--enable BPF_JIT_ALWAYS_ON \
--enable BPF_JIT_DEFAULT_ON \
--enable BPF_UNPRIV_DEFAULT_OFF \
--enable USERMODE_DRIVER \
--enable BPF_PRELOAD \
--enable BPF_PRELOAD_UMD \
--enable BPF_LSM \
2022-11-02 14:19:11 +03:00
--enable DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT \
--enable DEBUG_INFO_BTF \
2022-09-09 09:53:37 +03:00
--enable BTRFS_FS \
--enable BTRFS_FS_POSIX_ACL \
--enable PSI \
--enable CGROUPS \
--enable CGROUP_BPF \
--enable MEMCG \
--enable MEMCG_SWAP \
2022-11-02 14:19:11 +03:00
--enable MEMCG_KMEM \
--enable NETFILTER_ADVANCED \
--enable NF_CONNTRACK_MARK
2022-09-09 09:53:37 +03:00
# Make sure all unset options are set to their default value.
make O="$BUILDDIR/mkosi.kernel" olddefconfig
make O="$BUILDDIR/mkosi.kernel" -j "$(nproc)"
KERNEL_RELEASE="$(make O=$BUILDDIR/mkosi.kernel -s kernelrelease)"
mkdir -p "$DESTDIR/usr/lib/modules/$KERNEL_RELEASE"
2022-11-02 14:19:11 +03:00
make O="$BUILDDIR/mkosi.kernel" INSTALL_HDR_PATH=/usr headers_install
2022-09-09 09:53:37 +03:00
make O="$BUILDDIR/mkosi.kernel" INSTALL_MOD_PATH="$DESTDIR/usr" modules_install
make O="$BUILDDIR/mkosi.kernel" INSTALL_PATH="$DESTDIR/usr/lib/modules/$KERNEL_RELEASE" install
2022-11-02 14:19:11 +03:00
mkdir -p "$DESTDIR/usr/lib/kernel/selftests"
make -C tools/testing/selftests -j "$(nproc)" O="$BUILDDIR/mkosi.kernel" KSFT_INSTALL_PATH="$DESTDIR/usr/lib/kernel/selftests" SKIP_TARGETS="" install
2022-09-09 09:53:37 +03:00
fi