Skip to content

Support for Go library and utilities#36914

Closed
Foxboron wants to merge 4 commits into
systemd:mainfrom
Foxboron:morten/go
Closed

Support for Go library and utilities#36914
Foxboron wants to merge 4 commits into
systemd:mainfrom
Foxboron:morten/go

Conversation

@Foxboron

@Foxboron Foxboron commented Mar 31, 2025

Copy link
Copy Markdown
Contributor

This PR introduces two new changes.

A Go Library

systemd is an important set of libraries and utilities on modern Linux systems. The need to remove C in favour of memory-safe alternative is of peak priority.

Rust has been a hot contender for this for a while, but after several attempts at getting support for object file creation in rustc has failed, Rust falls short of integration into existing codebases.

A popular, and memory-safe, alternative with proper C interop is needed and the natural choice is of course to use Go.

Go enables systemd to leverage state-of-the-art concurrency primitives with a garbage collected runtime. This will enable systemd developers to accelerate their work for towards more awesome features.

The Go runtime is built with the shared systemd library. The size difference is negligible.

/etc/rc.conf generator

As PLT peaked in the 90s, we also wanted to reintroduce init service as they where meant to be handled, with a unified /etc/rc.conf.

systemd-rc-conf-generator supports enabling locale, keymaps, timezone, network configuraiton, job initialization and hostname configuration. All written in a terse 200 lines of go code.

Systemd interaction is also enabled with the help of coreos/go-systemd library through dbus calls.

We thank @teg for all his wisdom over the years, now we can finally manage our system from one central file like the good ol' days.

All functionalities are written in Pure 100% Go, and then called from the proper C-binaries as you would expect from the systemd codebase.

Foxboron and others added 4 commits April 1, 2025 00:07
Signed-off-by: Morten Linderud <morten@linderud.pw>
Allow users to configure their whole system using one rc.conf, the true
UNIX way. Heavily inspired by https://github.com/teg/initscripts-arch/

Co-authored-by: Jelle van der Waa <jelle@vdwaa.nl>
Signed-off-by: Morten Linderud <morten@linderud.pw>
Using the power of go modules(!) directly talk to the system bus and
enable/disable daemons with it! *poof* magic!
Go is good, we need go for building.
@github-actions github-actions Bot added build-system util-lib mkosi meson please-review PR is ready for (re-)review by a maintainer labels Mar 31, 2025

@YHNdnzj YHNdnzj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work, thanks! LGTM and definitely makes systemd more future-proof 😄

@YHNdnzj YHNdnzj added good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed do-not-merge 💣 and removed please-review PR is ready for (re-)review by a maintainer labels Mar 31, 2025

@bluca bluca left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CI is red, but that's probably because it's not memory safe

Comment thread src/go/go.mod
require github.com/coreos/go-systemd/v22 v22.5.0

require (
github.com/foxboron/go-uefi v0.0.0-20241219185318-19dc140271bf // indirect

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a bad rebase maybe? I don't understand why that pulls uefi here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@baloo Ah sorry, I was originally going to rewrite some ASN1 sbsign code :)

Will fix tomorrow with CI failures!

# - prefix a daemon with a ! to disable it
# - prefix a daemon with a @ to start it up in the background
#
DAEMONS=(nginx @sshd !lvm2-monitor)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the most valuable features ever observed in this tracker. Why ever work with elaborate and convoluted configs when you had everything presented in a single file. Units? Drop-ins? Never heard of them.

Small nit though: arrays are bashism. To give people 100% original flavor let's embrace posix sh

@AdrianVovk

Copy link
Copy Markdown
Contributor

100% on board with this.

First, the choice of language: It's clear that Rust isn't the future... I'm glad we chose a reasonable systems-level language like golang instead!

Second, the functionality: I love how much this simplified the config file formats! Just awesome! We can start deleting all the bloat, like systemd-datetime, hostnamed, networkd, and all those other pointless services

Bravo. Once CI is green we should merge ASAP, to make it in time for v258

@AdrianVovk AdrianVovk added this to the v258 milestone Mar 31, 2025
Comment thread src/go/rc-conf/conf.go
}

// Should all be well formed configs
parts := strings.Split(line, "=")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like strings.Cut is better suited for this task.

@yuwata yuwata left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding go as a second language is OK.
But, I am not a fan of introducing rc-generator.
We already deprecate rc-local-generator, and rc-generator added by this PR looks something similar.

Comment thread meson.build
'.')

libsystemd_includes = [basic_includes, include_directories(
'src/go',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? I cannot find any reasons. Please drop.

Comment thread meson.build
subdir('src/shared')
subdir('src/libudev')


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please drop unnecessary empty line insertion.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disagree, please insert more empty lines

Comment thread meson.build
subdir('src/run')
subdir('src/run-generator')
subdir('src/sbsign')
subdir('src/gobuildversion')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sort if there is no particular reason.

Comment thread src/go/meson.build
Comment on lines +7 to +9
command: ['go', 'build',
'-C', meson.current_source_dir(),
'-buildmode=c-archive', '-o', meson.current_build_dir() / 'go_library.a' , '.'],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big matter, but spurious indentation.
Please break line after [

Comment thread src/go/meson.build
'-buildmode=c-archive', '-o', meson.current_build_dir() / 'go_library.a' , '.'],
)

libgo_dep = declare_dependency (sources: libgo)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please drop a space before (

Comment thread src/sbsign/meson.build
],
'sources' : files('sbsign.c'),
'dependencies' : libopenssl,
'dependencies' : [libopenssl, libgo_dep],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What? Please drop.

Comment thread src/shared/meson.build
libbasic_static,
libsystemd_static],
libsystemd_static,
libgo],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please drop.

}


static int verb_version(int argc, char *argv[], void *userdata) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename to verb_generate() or so.


static int run(int argc, char *argv[]) {
static const Verb verbs[] = {
{ "help", VERB_ANY, VERB_ANY, 0, help },

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please drop help from verb.
But support -h/--help and --version.

diffutils
erofs-utils
git
go

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also add go dependency for opensuse and centos ?

@yuwata yuwata added reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks and removed good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed labels Apr 1, 2025
@keszybz

keszybz commented Apr 1, 2025

Copy link
Copy Markdown
Member

CodeQL passes, so we know the code is good. This is the most important part.

@keszybz keszybz added good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed and removed reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks labels Apr 1, 2025
@poettering

Copy link
Copy Markdown
Member

lgtm

@dtardon

dtardon commented Apr 1, 2025

Copy link
Copy Markdown
Collaborator

/etc/rc.conf generator

As PLT peaked in the 90s, we also wanted to reintroduce init service as they where meant to be handled, with a unified /etc/rc.conf.

systemd-rc-conf-generator supports enabling locale, keymaps, timezone, network configuraiton, job initialization and hostname configuration. All written in a terse 200 lines of go code.

I embrace the idea, but just one nitpick: I think it should use XML, the One True Format For Everything. It's not just more flexible and modern than shell, but also more readable:

<?xml version="1.0" encoding="utf-8"?>
<!--
  /etc/rc.conf - Main Configuration for Modern Linux
-->

<rc xmlns="urn:io.systemd.system1.rc.conf">

  <!-- Localization -->
  <!--
    locale: available languages can be listed with the 'locale -a' command
    timezone: timezones are found in /usr/share/zoneinfo
    keymap: keymaps are found in /usr/share/kbd/keymaps
  -->

  <localization>
    <locale>en_US.UTF-8</locale>
    <timezone>Europe/Berlin</timezone>
    <keymap>us</keymap>
  </localization>

  <!-- Networking -->

  <!-- hostname: Hostname of machine. Should also be put in /etc/hosts -->
  <network hostname="myhost">

     <!--
       Use 'ip addr' or 'ls /sys/class/net/' to see all available interfaces.

       Wired network setup
         - interface: name of device (required)
         - address: IP address (leave blank for DHCP)
         - netmask: subnet mask (ignored for DHCP) (optional, defaults to 255.255.255.0)
         - broadcast: broadcast address (ignored for DHCP) (optional)
         - gateway: default route (ignored for DHCP)

       Static IP example
       interface=eth0
       address=192.168.0.2
       netmask=255.255.255.0
       broadcast=192.168.0.255
       gateway=192.168.0.1

       DHCP example
       interface=eth0
       address=
       netmask=
       gateway=
     -->

    <interface/>
    <address/>
    <netmask/>
    <broadcast/>
    <gateway/>

  </network>

  <!--  Daemons -->
  <!--
    Daemons to start at boot-up (in this order)
  -->
  <daemons>
    <daemon>nginx</daemon>
    <daemon start="background">sshd</daemon>
    <daemon action="disable">lvm2-monitor"</daemon>
  </daemons>

</rc>
@poettering

Copy link
Copy Markdown
Member

Nah, S-Expressions are the way to go.

@Conan-Kudo

Copy link
Copy Markdown
Contributor

100% agree with @dtardon 👍🏾

@Conan-Kudo Conan-Kudo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can close up shop folks, all problems solved!

@Foxboron

Foxboron commented Apr 2, 2025

Copy link
Copy Markdown
Contributor Author

Thanks everyone :) ❤️

@Foxboron Foxboron closed this Apr 2, 2025
@github-actions github-actions Bot removed good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed do-not-merge 💣 labels Apr 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment