Happy Chinsese New Year !

I last got time to deal with this systemd thing :).

Why

We all know Archlinux had been migrated to system about a year ago. Systemd was first released in 2010, not such long time so Distros barely take usage of it. Anyway, Archlinux lead the way :)

systemd is a new init system for linux, intends to replace the traditional init script way, has many advantage over other init system3. More and more new (version of) software depends on it now (say Gnome 3.8, I never use it though XDD). systemd also has an implementation of udev, so we will never need sys-fs/udev anymore.

But traditional init way like OpenRC on gentoo works very well. Why take time to migrate to systemd now? Linux let user take their own choise isn’t it ? Answer is I’d like to figure things out :) I’ve tried sysvinit-style system, FreeBSD OpenRC and … well upstart (debian and ubuntu, but I don’t like it). systemd may be a better way to boot system up. So I’d like to give it a try.

Fortunately, gentoo already has systemd support and quite in detail. So I guess I’m getting my hands dirty :)

If you, like me, want to try systemd, better back your data, but don’t worry, you can switch back to OpenRC anytime.

My setup

My situation is a bit more complex than it on wiki page. I use soft raid (a.k.a. mdadm or mdraid), LVM for my gentoo system. The partition layout looks like

+--------------+----------+------------+----------+-----------+-----------------------+
| mount point  |    FS    |    LVM     |  MD      | disk 1    |   disk 2  |   note    |
|              |          |            |          |           |           |           |
|              |          |            |          | /dev/sda1 | /dev/sdb1 | bios_grub |
+--------------+----------+------------+----------+-----------+-----------------------+
| /boot        |   ext2   |            | /dev/md0 | /dev/sda2 | /dev/sdb2 | RAID1     |
+--------------+----------+------------+----------+-----------+-----------------------+
| swap         |          |            |          | /dev/sda3 | /dev/sdb3 |           |
+--------------+----------+------------+----------+-----------+-----------------------+
| /            |  btrfs   | vg-root    |          |           |           |           |
| /usr         |  btrfs   | vg-usr     |          |           |           |           |
| /var         |  btrfs   | vg-var     | /dev/md1 | /dev/sda4 | /dev/sda4 | RAID0     |
| /home        |  btrfs   | vg-home    |          |           |           |           |
| /usr/portage | reiserfs | vg-portage |          |           |           |           |
+--------------+----------+------------+----------+-----------+-----------------------+

Except the /boot is seperate partition, all other partitions are inside the mdraid and lvm, which make things complex. Because we are using systemd as our init process (PID is 1), after kernel boots the system, it first execute systemd process which located in /usr/lib/systemd/systemd. That requires rootfs and /usr are present when kernel boots. So we have to use initramfs to help mount the disks, before kernel boots the system.

If we use grub2 for bootloader, it’s ok to use ext3 or ext4 for /boot partition. But for most compatibility, I use ext2.

prepare the kernel

Do what is suggested on wiki page :)

initramfs

initramfs is a minimal linux system help prepare the booting sequence, like mount the disk to proper position, decrypt the crypted partition (like LUKS), via assemble many, lightweight useful tools into a binary.

Usually genkernel is good enough for daily use, but initramfs generated by genkernel (or even genkernel-next suggested in wiki) didn’t work for me. I guess rootfs and seperate /usr inside a mdraid and LVM cause this. So we have to use another way. Early userspace mouting by hand or use dracut. I tried dracut, it partailly solved my problem, with a little tweak :)

First, unmask keywords of dracut and install by

echo '=sys-kernel/dracut-034-r4 device-mapper net' >> /etc/portage/package.use/sys-kernel
echo '=sys-kernel/dracut-034-r4 ~amd64' >> /etc/portage/package.keywords/sys-kernel
$ emerge -qv sys-kernel/dracut

We need to tell dracut which modules we’re using, specify in config file /etc/dracut.conf

# use these modules when building
dracutmodules+="dash kernel-modules rootfs-block udev-rules usrmount local base fs-lib shutdown"

# add these modules to initramfs
add_dracutmodules+="btrfs dm lvm mdraid systemd"

# add these filesystems support
filesystems+="btrfs ext2 ext4 reiserfs"

dracut initramfs does’t regonize the mdraid and lvm partition, we need to add a custom module to activate mdadm and lvm before mouting roofs and /usr.

add a custom module, reference.

$ cd /usr/lib/dracut/modules.d/
$ mkdir 91local
$ cat > 91local/module-setup.sh
#!/bin/bash

check() {
return 0
}

depends() {
return 0
}

install() {
inst_hook pre-trigger 91 "$moddir/mount-local.sh"
}


$ cat > 91local/mount-local.sh
#!/bin/sh

mount_local()
{
mdadm -As
lvm pvscan
lvm vgcan
lvm lvscan
lvm vgchange -ay
}

mount_local

Build initramfs

by using dracut output-initramfs [kernel-version], usually

$ mount /boot
$ dracut -f /boot/initrd-3.11.7- gentoo-r1 3.11.7-gentoo-r1

Now kernel and initramfs are ready.

install systemd

systemd depends on dbus, and dbus also has a systemd USE flag, this will cause circular dependecy issue. Usually if you have dbus installed, that’s not a problem, just enable global systemd USE flag and update @world.

We have to remove sys-fs/udev because systemd already has an implementation for that.

$ emerge -aC sys-fs/udev virtual/udev && emerge -qv sys-apps/systemd

If you’re seting up a branch new gentoo system, emerge dbus without systemd USE flag first and emerge it again with systemd USE flag enabled.

Be sure to disable consolekit USE flag, it will conflict with systemd-logind.

Final global USE flag will at least looks like USE="systemd -consolekit". select a proper portage profile by eselect profile list and eselect profile set <n> may also help.

grub config

Tell kernel to use /usr/lib/systemd/systemd to initialize system, by editing /etc/default/grub

GRUB_CMDLINE_LINUX="init=/usr/lib/systemd/systemd"

then run grub2-mkconfig -o /boot/grub/grub.cfg

reboot and use systemd

If all things go well, your system will now boot through systemd, check it out by looking for systemd process which PID is 1.

If anything goes wrong, you can always revert back to OpenRC by removing init=xxx thing in kernel command line.

post install

Because OpenRC is not used anymore, any settings by openrc is not working now. We should use systemd way.

basic

reload, start, stop, status of a OpenRC service is simply /etc/init.d/<service> reload|start|stop|status or rc-service reload|start|stop|status <service>. That will never work in systemd situation.

Use systemctl start|stop|stop|status <service>.service for that. And systemd have a seperate <service>.socket unit for socket and pid files. For instance, we start sshd by systemctl start sshd just works :)

Adding a service to start automatically on boot is also simple, just systemctl enable <service> and that’s that.

hostname

$ hostnamectl 

network setup

DHCP , by hand dhcpcd <interface>

$ systemctl start dhcpcd

Archlinux style systemctl start dhcpcd@<interface>.service didn’t work for gentoo default. We can use netctl@.service for that, more info about how to setup network on systemd, please refer to Archlinux wiki page.

However, network interface naming rule for udev (that 80-net-name-slot.rules didn’t work for me orz )

Xorg issue

After using systemd, automatically detected Xorg.conf didn’t work, I’ve to use X -configure to generate a config file, tweak it a bit to make xorg work again. I guess it’s something related to Dual graphical card. I’m using Intel Core i7 4770, which has an intergarted graphic cards and a seperate Radeon HD6950 (CAYMAN). Specifying only radeon card in xorg.conf works for me.

other things

I’m using awesome wm (3.4), chromium browser and fcitx (for input chinese and japanese characters). They work just fine under systemd. So I guess I will migrate my working computer to systemd too, to see if there are other problem.

Any I will also give a try to use KDE 4.11+ or Gnome 3.8+ under systemd to report more issues.

May this help you too :)