lfs-uefi: fix efivar-37 FTBFS
[linux_from_scratch_hints.git] / systemd.txt
blobaa2f719ea505260f6ad8307254e33af2eb830fbb
1 AUTHOR: Chris Wagner <wagnerlia-lists at yahoo.com>
3 DATE: 2012-10-03
5 LICENSE: GNU Free Documentation License Version 1.2
7 SYNOPSIS: How to install Systemd, an alternative system and service
8           manager, on LFS
10 ### DESCRIPTION:
12 I wanted to better understand the inner workings of systemd. Just having
13 finished a LFS install on a test server, I thought LFS 7.2 might be a
14 good basis for this. My goal was to eventually replace SysVinit
15 completely with systemd. I fully expected lots of things to break, but
16 was pleasantly surprised, that getting systemd to work was not all that
17 hard. I started out with a guide from Lemon Lime which he posted on this
18 list a year ago. Because LFS 7.2 is using a customized non-standard
19 installation of Systemd/Udevd, additional steps were required. Systemd
20 has matured quite a bit since last year and more distributions are using
21 it, among them Arch Linux. Having lots of unit files available from
22 other distributions, makes the switch a lot easier. 
24 I have everything working on my test server with a Plone CMS installed
25 and find the built-in monitoring and logging capabilities of systemd
26 quite remarkable. Bootup and shutdown times are considerably faster than
27 with SysVinit. The following guide was put together as I documented the
28 steps I took, and is intended help others to get started with systemd. I
29 have put it in a similar format as instructions in the BLFS book to make
30 it easier to apply. 
32 I hope you'll find this guide helpful and would welcome your comments
33 and suggestions.
35 ### ATTACHMENTS:
37 see Package Information below
39 ### PREREQUISITES:
41 This hint requires that you have sufficient knowledge of LinuxFromScratch 
42 and be familiar with adding software from BLFS. Software versions used
43 are from LFS 7.2 and BLFS 2012-09-14 or above.
45 HINT: How to install Systemd-193 on LFS
46 =======================================
48 Introduction to systemd
49 -----------------------
51 systemd is a system and service manager for Linux, compatible with SysV
52 and LSB init scripts. systemd provides aggressive parallelization
53 capabilities, uses socket and D-Bus activation for starting services,
54 offers on-demand starting of daemons, keeps track of processes using
55 Linux control groups, supports snapshotting and restoring of the system
56 state, maintains mount and automount points and implements an elaborate
57 transactional dependency-based service control logic. It can work as a
58 drop-in replacement for sysvinit.
60 I was able to have it build and work properly using an LFS-7.2 platform.
62 ### Package Information
64 * Download (HTTP): 
65       `http://www.freedesktop.org/software/systemd/systemd-193.tar.xz`
67 * Download MD5 sum: `732a9de2b1d2a15cab639c987ff9e90e`
69 #### Required (versions from BLFS 2012-09-14)
71 libcap2-2.22, D-Bus-1.6.4, Gperf-3.0.4, USB Utils-006, PCI Utils-3.1.10,
72 GLib-2.32.4, (D-Bus will have to be re-installed after installing
73 systemd!)
75 Preparation
76 -----------
78 systemd can be installed alongside SysVinit and will start any init
79 scripts which do not have systemd equivalent unit files yet. It can also
80 be installed to fully replace SysVinit. In this guide systemd will first
81 be installed alongside SysVinit, then it can be tested, while optionally
82 booting into SysVinit or systemd. When satisfied that everything works
83 well in systemd, SysVinit can be completely replaced by systemd, and the
84 init scripts can be removed.
86 The required dependencies above will need a number of their own
87 dependencies to be fulfilled and can be installed in the
88 following order:
90     {(attr-2.4.46), libcap2-2.22 },
91     {(Expat-2.1.0 or libxml2-2.8.0), D-Bus-1.6.4}, 
92     {(XML-Parser-2.41), Intltool-0.50.2}, Gperf-3.0.4,
93     {(libusb-1.0.9), USB Utils-006}, PCI Utils-3.1.10,          
94     {(pkg-config-0.27, libffi-3.0.11, PCRE-8.31, 
95       Python-2.7.3),GLib-2.32.4}
97 We need a Linux kernel with devtmpfs and cgroups activated while autofs4
98 and ipv6 is recommended. systemd will work using the default options on
99 a 3.5.2 kernel with just the following two options added:
100 `CONFIG_DEVTMPFS=y CONFIG_DEVTMPFS_MOUNT=y`
102 In `make menuconfig` these options can be found here:
104     Device Drivers  --->
105       Generic Driver Options  --->
106         [*] Maintain a devtmpfs filesystem to mount at /dev                                                         
107         [*]   Automount devtmpfs at /dev, after the kernel
108               mounted the rootfs
110 In LFS chapter six Udev-188, a subset of systemd was installed using a
111 LFS specific makefile. I found, that the complete systemd-188 cannot be
112 easily installed over it, as this introduces various problems. Thus
113 Udev-188 must be uninstalled first, which will also allow us to use a
114 newer version of systemd than was used in LFS 7.2.
116 As the root user stop udevd first
118     udevadm control --exit
120 and then remove most files from Udev-188, while keeping the LFS specific
121 scripts:
123     rm -v /lib/udev/rules.d/*
124     rm -v /lib/{libudev.so.1,libudev.so.1.0.2}
125     rm -v /usr/lib/pkgconfig/{libudev.pc,udev.pc}
126     rm -v /usr/share/man/man7/udev.7*
127     rm -v /usr/share/man/man8/{udevadm.8*,udevd.8*}
128     rm -v /sbin/udevadm /usr/include/libudev.h /usr/lib/libudev.so
129     rm -v /lib/udev/{accelerometer,ata_id,cdrom_id,collect}
130     rm -v /lib/udev/{mtd_probe,scsi_id,udevd,v4l_id}
131     
132 This will leave the following 7 files in place:
134 /lib/udev:  init-net-rules.sh rule_generator.functions
135            write_cd_rules write_net_rules
136            
137 /usr/share/doc/udev/lfs:  55-lfs.txt README
139 /etc/udev/rules.d:  55-lfs.rules
141 Installation of systemd
142 -----------------------
144 `systemd-tmpfiles` requires a 'lock' group, thus as the root user,
145 create a group:
147     groupadd -g 54 lock
149 Install systemd by running the following commands (you may wish to
150 review the output from ./configure --help first and add any desired
151 parameters to the configure command shown below):
153     ./configure --with-distro=other   \
154                 --libexecdir=/usr/lib \
155                 --localstatedir=/var  \
156                 --sysconfdir=/etc && 
157     make
159 When running tests before installing sytemd, 4 out of 14 tests will
160 fail. If run after systemd is installed, they should complete without
161 errors. The tests also require an existing /usr/lib/udev directory. If
162 you would like to run the unit tests, issue the following commands:
163         
164     mkdir -pv /usr/lib/udev/rules.d
165     make check
167 Now, as the root user:
169     make install
170     # remove static libraries
171     rm -v /usr/lib/{libgudev-*.la,libsystemd-daemon.la}
172     rm -v /usr/lib/{libsystemd-id128.la,libsystemd-journal.la}
173     rm -v /usr/lib/{libsystemd-login.la,libudev.la}
175     ln -sfv /usr/lib/systemd/systemd /bin/systemd
176     mkdir -pv /var/log/journal
178 Move the rule `73-seat-late.rules`, which causes an error. There are
179 probably other rules we do not need, which could be moved out. (With the
180 LFS version of Udev, only a subset of rules were installed.)
181         
182     mkdir -pv /usr/lib/udev/rules-unused/
183     mv -v /usr/lib/udev/rules.d/73-seat-late.rules \
184            /usr/lib/udev/rules-unused/
186 Setup /etc/machine-id, if it does not exist:
188     systemd-machine-id-setup
190 As the path to udevadm is hardcoded in some places, link to it:
192     ln -sv /usr/bin/udevadm /sbin/udevadm
193         
194 Create other links, because udevd is no longer called udevd, but LFS
195 init.d/udev expects it here:
197     ln -sv /usr/lib/systemd/systemd-udevd /lib/udev/udevd
198     ln -sv /usr/share/man/man8/systemd-udevd.8 \
199             /usr/share/man/man8/udevd.8
201 Command Explanations
202 --------------------
204 `--libexecdir=/usr/lib`: Note, that D-Bus ./configure may not find
205 systemd, when the systemd install paths are changed to be similar to the
206 paths used by Udev-188.
208 `--with-distro=other`: some major distributions have adapted systemd to
209 their distribution. Others will work just fine with the default
210 settings.
212 Configuring systemd
213 -------------------
215 ### Config Files
217 /etc/systemd/*, /etc/dbus-1/system.d/*, /etc/hostname,
218 /etc/vconsole.conf, /etc/locale.conf, /etc/modules-load.d/*.conf,
219 /etc/sysctl.d/*.conf, /etc/tmpfiles.d/*.conf, /etc/binfmt.d/*.conf,
220 /etc/os-release, /etc/machine-id, /etc/machine-info
222 ### Configuration Information
224 Most of the config files do not need to be modified, as the defaults
225 will work just fine.
227 Create /etc/hostname by running the following command as the root user
229     grep '^HOSTNAME=' \
230        /etc/sysconfig/network | sed 's/^HOSTNAME=//' >/etc/hostname
232 Adapt and create a locale file, by performing the following commands as
233 the root user:
235     cat > /etc/locale.conf << "EOF"
236     LANG=en_US
237     LC_COLLATE=C
238     EOF
240 We do not need to create /etc/timezone any more. In the release notes to
241 systemd-190:  "timedated will no longer write or process /etc/timezone.
242 As we do not support late mounted /usr anymore /etc/localtime always
243 being a symlink is now safe, and hence the information in /etc/timezone
244 is not necessary anymore." 
246 ### Unit files for services
248 As the root user create some unit files to start important services.
249 Unit files of the appropriate name will mask the init scripts. Therefore
250 systemd will use the unit file in preference over the init script, if
251 available.
253 This unit file creates the /run/var directory to avoid errors from LSB
254 init scripts:
256     cat > /etc/systemd/system/lsb-prep.service << "EOF"
257     [Unit]
258     Description=Network Connectivity
259     Wants=local-fs.target
260     
261     [Service]
262     Type=oneshot
263     RemainAfterExit=yes
264     ExecStart=/bin/mkdir -p /run/var 
265     
266     [Install]
267     WantedBy=multi-user.target
268     EOF
269         
270 Create sysklogd service unit files:
271         
272     cat > /etc/systemd/system/syslogd.service << "EOF"
273     [Unit]
274     Description=System Logging Service
275     Requires=syslog.target
276     Before=syslog.target
277     
278     [Service]
279     ExecStart=/sbin/syslogd -p /run/systemd/journal/syslog -n -m 0
280     Sockets=syslog.socket
281     StandardOutput=null
282     
283     [Install]
284     WantedBy=multi-user.target
285     Alias=syslog.service
286     EOF
287     
288     cat > /etc/systemd/system/sysklogd.service << "EOF"
289     [Unit]
290     Description=System Kernel Logging Service
291     Requisite=syslog.service
292     BindTo=syslog.service
293     After=syslog.service
294     RefuseManualStart=true
295     
296     [Service]
297     Type=forking
298     StandardOutput=syslog+console
299     StandardError=syslog+console
300     ExecStart=/sbin/klogd -c 6 -x
301     
302     [Install]
303     WantedBy=multi-user.target
304     EOF
306 To enable the network, create a simple unit file for a static network
307 which uses the settings in /etc/sysconfig/ifconfig.eth0. Alternatively
308 enable the dhcpcd at eth0.service. One could also create a unit file which
309 will call the network/ifup scripts, that provide more functionality with
310 multiple interfaces.
312 Note, that this unit file requires the BROADCAST ip addr to be set in
313 ifconfig.eth0
314         
315     cat > /etc/systemd/system/network.service << "EOF"
316     [Unit]
317     Description=Network Connectivity
318     Wants=network.target
319     Before=network.target
320     
321     [Service]
322     Type=oneshot
323     RemainAfterExit=yes
324     EnvironmentFile=/etc/sysconfig/ifconfig.eth0
325     ExecStart=/sbin/ip link set dev ${IFACE} up
326     ExecStart=/sbin/ip addr add ${IP}/${PREFIX} \
327                   broadcast ${BROADCAST} dev ${IFACE}
328     ExecStart=/sbin/ip route add default via ${GATEWAY}
329     ExecStop=/sbin/ip addr flush dev ${IFACE}
330     ExecStop=/sbin/ip link set dev ${IFACE} down
331     
332     [Install]
333     WantedBy=multi-user.target
334     EOF
335         
336 Create the following links, which will enable essential services before
337 rebooting into systemd:
339     ln -sv /etc/systemd/system/lsb-prep.service \
340       /etc/systemd/system/multi-user.target.wants/lsb-prep.service
341     ln -sv /etc/systemd/system/syslogd.service \
342       /etc/systemd/system/syslog.service
343     ln -sv /etc/systemd/system/syslogd.service \
344       /etc/systemd/system/multi-user.target.wants/syslogd.service
345     ln -sv /etc/systemd/system/sysklogd.service \
346       /etc/systemd/system/multi-user.target.wants/sysklogd.service
347     ln -sv /etc/systemd/system/network.service \
348       /etc/systemd/system/multi-user.target.wants/network.service
349         
350 Add a systemd entry to grub, check the `root=` entry to match your disk:
352     echo "menuentry \"Linux from Scratch (systemd)\" {" >> \
353          /boot/grub/grub.cfg
354     echo \
355     "       linux /boot/vmlinuz root=/dev/sda1 ro init=/bin/systemd" \
356          >> /boot/grub/grub.cfg
357     echo "}" >> /boot/grub/grub.cfg
359 Remember to rebuild and reinstall D-Bus-1.6.4, before rebooting into
360 systemd.
362 After booting with systemd, look at the systemd journal log from the 
363 boot process:
365     journalctl -b 
367 Optionally Replace SysVinit
368 ---------------------------
370 The lfs-bootscripts scripts provide a lot of rich functionality to LFS.
371 Care should be taken, that all required features are covered by unit
372 files or by systemd itself, before removing them.
374 ### Uninstall SysVinit
376 After systemd is running well, SysVinit can be removed . Check, that no
377 more LSB scripts are used by systemd using the following command:
379     systemctl | grep LSB
381 Remove the files belonging to SysVinit:
383     rm -v /usr/bin/{last,lastb,mesg,utmpdump}
384     rm -v /usr/include/initreq.h /etc/inittab /bin/pidof
385     rm -v /usr/share/man/man1/{last.1*,lastb.1*,mesg.1*,utmpdump.1*}
386     rm -v /usr/share/man/man5/{initscript.5*,inittab.5*}
387     rm -v /usr/share/man/man8/{bootlogd.8*,fstab-decode.8*}
388     rm -v /usr/share/man/man8/{fstab-decode.8*,init.8*}
389     rm -v /usr/share/man/man8/{killall5.8*,pidof.8*,sulogin.8*}
390     rm -v /sbin/{bootlogd,fstab-decode,halt,init,killall5,poweroff}
391     rm -v /sbin/{reboot,runlevel,shutdown,sulogin,telinit}  
392         
393 Replace sysvinit commands with links to systemd:
395     ln -sv /usr/lib/systemd/systemd /sbin/init
396     for tool in runlevel reboot shutdown poweroff halt telinit; do
397         ln -svf '/usr/bin/systemctl' "/sbin/$tool"
398     done
399     
400 #### Uninstall LFS-Bootscripts
402 Now we can also move out the lfs-bootscripts and those services
403 installed with other programs:
405     mkdir -pv /etc/rc.bak
406     tar -czpvf "/etc/rc.bak/rc.d-backup.tar.gz" /etc/rc.d 
407     
408     rm -v /etc/rc.d/init.d/*    
409     for rcX in rc0 rc1 rc2 rc3 rc4 rc5 rc6 rcS; do
410         rm -v /etc/rc.d/$rcX.d/*
411     done
413 The files in /etc/sysconfig, which were installed by lfs-bootscripts
414 might still come in handy to refer to or to utilize their settings from
415 within unit files. We could also keep ipup/ifdown for networking and the
416 lsb functions in /lib/services.
418 #### Mtab symlink
420 The version of Util-linux used in LFS 7.2 contains support for systems 
421 without the /etc/mtab file, which use asymlink to `/proc/mounts`
422 instead.
424 Making `mtab` a symlink to `/proc/mounts` is advised by systemd,
425 otherwise there is a reminder during boot: `/etc/mtab is not a symlink
426 or not pointing to /proc/self/mounts. This is not supported anymore. 
427 Please make sure to replace this file by a symlink to avoid incorrect 
428 or misleading mount output.`
430 Removing /etc/mtab earlier would cause an error in /init.d/mountfs when
431 booting LFS via sysvinit. Now that we are not using the init scripts any
432 more, we can make this change:
434     ln -svf /proc/mounts /etc/mtab
435     
437 Contents
438 --------
440 **Installed Programs:**     systemctl, journalctl, udevadm,
441 systemd-udevd, ...
442                             
443 **Installed Libraries:**    libudev.so, libgudev-1.0.so,
444 libsystemd-daemon.so, libsystemd-id128.so, libsystemd-journal.so,
445 libsystemd-login.so
447 **Installed Directories:**  /etc/systemd/, /usr/include/systemd/,
448 /usr/lib/udev/, /usr/lib/systemd/
450 ### Short Descriptions
452 * _systemctl_ may be used to control the state of the systemd system and
453 service manager
455 * _journalctl_ may be used to query the contents of the systemd journal
456 as written by systemd-journald.service
457         
458 ### Further information:
460 [ Official Website ]( http://www.freedesktop.org/wiki/Software/systemd )
462 [ Arch Linux Wiki ]( https://wiki.archlinux.org/index.php/Systemd )
464 ### ACKNOWLEDGEMENTS:
466   * Lemon Lime <lemonlime51 at gmail.com> who posted a similar guide 
467     on lfs-dev in Aug. 2011
468     
469   * Matthew Burgess <matthew at linuxfromscratch.org> and Bruce Dubbs 
470     <bruce.dubbs at gmail.com> for encouraging me to publish this as
471     a hint
473 ### CHANGELOG:
475 [2012-10-03]
477   * Initial hint.