In this chapter, you will look at systemd, what it is, and how to write Go applications to interact with it. systemd is an important piece of software inside the Linux system, and it is too big to be covered entirely in this chapter.
Learn what systemd provides
Interact with systemd using systemctl and journalctl
Use the go-systemd library to write code
Write log messages to a systemd journal
Query systemd to get a list of registered services
Source Code
The source code for this chapter is available from the https://github.com/Apress/Software-Development-Go repository.
systemd
systemd is a suite of applications that are used in Linux systems to get them up and running. It provides more than just starting the core Linux systems run; it also starts a number of programs such as network stack, user logins, the logging server, and more. It uses socket and D-Bus activation to start up services, on-demand starting of background applications, and more.
D-Bus stands for Desktop Bus. It is a specification that is used for an inter-process communication mechanism, allowing different processes to communicate with one another on the same machine. Implementation of D-Bus consists of server components and a client library. For systemd, the implementation is known as sd-bus, and in a later section, you will look at using the D-Bus client library to communicate with the server component.
Socket activation is a mechanism in systemd to listen to a network port or Unix socket. When connected from an external source, it will trigger the running of a server application. This is useful in situations when a resource-hungry application needs to run only when it is needed and not during the time when the Linux system is started up.
systemd Units
.service: Describe a service or application and how to start or stop the service
.socket: Describes a network or Unix socket used for socket-based activation
.device: Describes a device exposed in the sysfs/udev device tree
.timer: Defines a timer that will be managed by systemd
You looked at systemd and what it is used for. In the next section, you will look at using the provided tools to look at the services provided by systemd using systemctl.
systemctl
![](https://imgdetail.ebookreading.net/2023/10/9781484287316/9781484287316__9781484287316__files__images__527551_1_En_17_Chapter__527551_1_En_17_Fig1_HTML.jpg)
A screenshot of the output lists the services and their status as loaded, active, and exited.
Registered service in systemd
The output shows information about the service such as the amount of memory the service is using, the process ID (PID), location of the .service file, and whether the service is active or not.
Using systemctl allows you to take a look at the status of the registered service in systemd. In the next section, you will write a simple server application and control it using systemctl.
Hello Server systemd
![](https://imgdetail.ebookreading.net/2023/10/9781484287316/9781484287316__9781484287316__files__images__527551_1_En_17_Chapter__527551_1_En_17_Fig2_HTML.jpg)
A screenshot of the output screen reads Hello you are getting a response from an app running via systems.
HTTP server output
- 1.
Copy the httpservice executable file into the /usr/local/bin directory in your terminal, as shown:
- 2.
Copy the httpservice.service file into the /etc/systemd/system directory in your terminal, as shown:
- 3.
Take a look at the status of your newly created service using the following command:
- 4.
Start/enable the service using the following command:
- 5.
If you run the same status command (step 3), you will get the following output:
- 6.
To ensure that the service starts up when you reboot your machine, use the following command:
Now you can access the application by pointing your browser to http://localhost:8111.
You have successfully deployed your sample app. It is configured to start up when you boot up your machine. In next section, you will look at using a Go library to write a system application.
go-systemd Library
You learned early in this chapter that the D-Bus specification contains a client library. The client library allows applications to interact with system. The client library that you are going to take a look at is for a Go application called go-systemd. The library can be found at http:/github.com/coreos/go-systemd.
Using socket activation
Notifying systemd of service status changes
Starting, stopping, and inspecting services and units
Registering machines or containers
…and many more
For this chapter, you will look at code samples using the library to write to journal logs, list services available on local machines, and query machines.
Querying Services
The sample code for this section can be found inside the chapter17/listservices directory. The sample code queryies from systemd all the services that are registered, similar to how systemctl list-units works.
The library takes care of all the heavy lifting of connecting to systemd, sending requests, and converting requests to a format that it passes to the application.
Journal
The parameter -r shows the latest log message on the top. Now you know how to look at the journal logging service. Let's run your sample application to write log messages into it.
The following priorities are assigned the red color: PriErr, PriCrit, PriAlert, and PriEmerg. PriNotice and PriWarning are highlighted, and PriDebug is in lighter grey. One of the interesting priorities is PriEmerg, which broadcasts the log message to all open terminals in the local machine.
In the next section, you will look at an advanced feature of systemd, which is registering and running a machine or container.
Machines
One advanced feature that systemd provides is the ability to run virtual machines or containers in local machines. This feature does not come by default; there is extra installation of services and steps performed in order to use this feature. This feature is made available by installing a package called systemd-container. Let’s understand what this package is all about.
The systemd-container package contains a number of tools, particularly the tool called systemd-nspawn. This tool is similar to chroot (which I discussed in Chapter 4) but provides more advanced features such as virtualizing the file system hierarchy, process tree, and various IPC subsystems. Basically, it allows you to run a lightweight container with its own rootfs.
- 1.
Copy the file systemd-machined.service from the chapter17/machine directory to /usr/lib/systemd/user.
- 2.
Install the systemd-container package using the following command:
- 3.
Start the service using the following command:
- 4.
Check the status using the command:
Use the machinectl command-line tool to interact with the new machine service that you just installed. Use the tool to download and run Ubuntu operating system images locally as a container.
Summary
In this chapter, you learned about systemd and its functions in the Linux operating system. You explored the different tools that are available to allow you to interact with systemd. You looked at Go code samples that show how to interact with systemd using the go-systemd library.
go-systemd provides a different capability to interact with system. One of the advanced features you looked at was interacting with the systemd-machine service that provides virtual machine and container registration capability.