From f21aa7d22cc8128f6c60632e3108bacf69351dac Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 1 Jun 2012 20:15:07 -0400 Subject: [PATCH] First attempt at using systemd Basic booting works. Systemd starts agetty instances on tty1 and tty2. Shutdown and journald also work. --- modules/config/system-path.nix | 1 - modules/module-list.nix | 1 + modules/system/activation/top-level.nix | 6 +- modules/system/boot/stage-2-init.sh | 14 ++- modules/system/boot/systemd.nix | 158 ++++++++++++++++++++++++ 5 files changed, 172 insertions(+), 8 deletions(-) create mode 100644 modules/system/boot/systemd.nix diff --git a/modules/config/system-path.nix b/modules/config/system-path.nix index 9eab8dde473..2a25cd411a1 100644 --- a/modules/config/system-path.nix +++ b/modules/config/system-path.nix @@ -17,7 +17,6 @@ let requiredPackages = [ config.system.sbin.modprobe # must take precedence over module_init_tools - config.system.build.upstart config.environment.nix pkgs.acl pkgs.attr diff --git a/modules/module-list.nix b/modules/module-list.nix index 48171f62150..870d316006f 100644 --- a/modules/module-list.nix +++ b/modules/module-list.nix @@ -198,6 +198,7 @@ ./system/boot/stage-1.nix ./system/boot/stage-1-extratools.nix ./system/boot/stage-2.nix + ./system/boot/systemd.nix ./system/etc/etc.nix ./system/upstart-events/control-alt-delete.nix ./system/upstart-events/runlevel.nix diff --git a/modules/system/activation/top-level.nix b/modules/system/activation/top-level.nix index 3edc92d502c..ef32d991d9d 100644 --- a/modules/system/activation/top-level.nix +++ b/modules/system/activation/top-level.nix @@ -117,12 +117,12 @@ let ln -s ${config.system.build.etc}/etc $out/etc ln -s ${config.system.path} $out/sw - ln -s ${config.system.build.upstart} $out/upstart + ln -s ${pkgs.systemd} $out/systemd ln -s ${config.hardware.firmware} $out/firmware echo -n "$kernelParams" > $out/kernel-params echo -n "$configurationName" > $out/configuration-name - echo -n "${toString config.system.build.upstart.interfaceVersion}" > $out/upstart-interface-version + #echo -n "${toString config.system.build.upstart.interfaceVersion}" > $out/upstart-interface-version echo -n "$nixosVersion" > $out/nixos-version mkdir $out/fine-tune @@ -173,7 +173,7 @@ let pkgs.gnugrep pkgs.findutils pkgs.diffutils - config.system.build.upstart # for initctl + pkgs.systemd ]; # Boot loaders diff --git a/modules/system/boot/stage-2-init.sh b/modules/system/boot/stage-2-init.sh index bd48f463dcd..33d0a6ad3e8 100644 --- a/modules/system/boot/stage-2-init.sh +++ b/modules/system/boot/stage-2-init.sh @@ -56,7 +56,7 @@ fi mkdir -m 0755 -p /etc test -e /etc/fstab || touch /etc/fstab # to shut up mount rm -f /etc/mtab* # not that we care about stale locks -cat /proc/mounts > /etc/mtab +ln -s /proc/mounts /etc/mtab # Process the kernel command line. @@ -184,6 +184,12 @@ if [ -n "$debug2" ]; then fi -# Start Upstart's init. -echo "starting Upstart..." -PATH=/var/run/current-system/upstart/sbin exec init --no-sessions ${debug2:+--verbose} +# FIXME: auto-loading of kernel modules in systemd doesn't work yet +# because it uses libkmod. +"$(cat /proc/sys/kernel/modprobe)" autofs4 +"$(cat /proc/sys/kernel/modprobe)" ipv6 + + +# Start systemd. +echo "starting systemd..." +PATH=/var/run/current-system/systemd/lib/systemd exec systemd --log-level debug --log-target=console --crash-shell diff --git a/modules/system/boot/systemd.nix b/modules/system/boot/systemd.nix new file mode 100644 index 00000000000..3b51ebf43fc --- /dev/null +++ b/modules/system/boot/systemd.nix @@ -0,0 +1,158 @@ +{ config, pkgs, ... }: + +with pkgs.lib; + +let + + systemd = pkgs.systemd; + + makeUnit = name: text: + pkgs.writeTextFile { name = "unit"; inherit text; destination = "/${name}"; }; + + defaultTarget = makeUnit "default.target" + '' + [Unit] + Description=Default System + Requires=getty.target + After=getty.target + Conflicts=rescue.target + AllowIsolate=yes + ''; + + gettyTarget = makeUnit "getty.target" + '' + [Unit] + Description=Login Prompts + Requires=getty@tty1.service getty@tty2.service + After=getty@tty1.service getty@tty2.service + ''; + + gettyService = makeUnit "getty@.service" + '' + [Unit] + Description=Getty on %I + #BindTo=dev-%i.device + #After=dev-%i.device systemd-user-sessions.service plymouth-quit-wait.service + Before=getty.target + + [Service] + Environment=TERM=linux + ExecStart=-${pkgs.utillinux}/sbin/agetty --noclear --login-program ${pkgs.shadow}/bin/login %I 38400 + Restart=always + RestartSec=0 + UtmpIdentifier=%I + TTYPath=/dev/%I + TTYReset=yes + TTYVHangup=yes + TTYVTDisallocate=yes + KillMode=process + IgnoreSIGPIPE=no + + # Unset locale for the console getty since the console has problems + # displaying some internationalized messages. + Environment=LANG= LANGUAGE= LC_CTYPE= LC_NUMERIC= LC_TIME= LC_COLLATE= LC_MONETARY= LC_MESSAGES= LC_PAPER= LC_NAME= LC_ADDRESS= LC_TELEPHONE= LC_MEASUREMENT= LC_IDENTIFICATION= + + # Some login implementations ignore SIGTERM, so we send SIGHUP + # instead, to ensure that login terminates cleanly. + KillSignal=SIGHUP + ''; + + rescueTarget = makeUnit "rescue.target" + '' + [Unit] + Description=Rescue Mode + Requires=rescue.service + After=rescue.service + AllowIsolate=yes + ''; + + rescueService = makeUnit "rescue.service" + '' + [Unit] + Description=Rescue Shell + DefaultDependencies=no + #After=basic.target + #Before=shutdown.target + + [Service] + Environment=HOME=/root + WorkingDirectory=/root + ExecStartPre=-${pkgs.coreutils}/bin/echo 'Welcome to rescue mode. Use "systemctl default" or ^D to enter default mode.' + #ExecStart=-/sbin/sulogin + ExecStart=-${pkgs.bashInteractive}/bin/bash --login + ExecStopPost=-${systemd}/bin/systemctl --fail --no-block default + StandardInput=tty-force + StandardOutput=inherit + StandardError=inherit + KillMode=process + + # Bash ignores SIGTERM, so we send SIGHUP instead, to ensure that bash + # terminates cleanly. + KillSignal=SIGHUP + ''; + + upstreamUnits = + [ "systemd-journald.socket" + "systemd-journald.service" + "basic.target" + "sysinit.target" + "sysinit.target.wants" + "sockets.target" + "sockets.target.wants" + + # Filesystems. + "local-fs.target" + "local-fs.target.wants" + "local-fs-pre.target" + "remote-fs.target" + "remote-fs-pre.target" + "swap.target" + "media.mount" + "dev-mqueue.mount" + + # Reboot stuff. + "reboot.target" + "reboot.service" + "poweroff.target" + "poweroff.service" + "halt.target" + "halt.service" + "ctrl-alt-del.target" + "shutdown.target" + "umount.target" + "final.target" + ]; + + systemUnits = pkgs.runCommand "system-units" { } + '' + mkdir -p $out/system + for i in ${toString upstreamUnits}; do + fn=${systemd}/example/systemd/system/$i + echo $fn + [ -e $fn ] + ln -s $fn $out/system + done + for i in ${toString [ defaultTarget gettyTarget gettyService rescueTarget rescueService ]}; do + cp $i/* $out/system + done + ''; # */ + +in + +{ + + ###### implementation + + config = { + + environment.systemPackages = [ systemd ]; + + environment.etc = + [ { source = systemUnits; + target = "systemd"; + } + ]; + + }; + +}