1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (gnu services base)
20 #:use-module ((guix store)
21 #:select (%store-prefix))
22 #:use-module (gnu services)
23 #:use-module (gnu services networking)
24 #:use-module (gnu system shadow) ; 'user-account', etc.
25 #:use-module (gnu system linux) ; 'pam-service', etc.
26 #:use-module (gnu packages admin)
27 #:use-module ((gnu packages linux)
28 #:select (udev kbd e2fsprogs lvm2 fuse alsa-utils))
29 #:use-module ((gnu packages base)
30 #:select (canonical-package glibc))
31 #:use-module (gnu packages package-management)
32 #:use-module ((gnu build file-systems)
33 #:select (mount-flags->bit-mask))
34 #:use-module (guix gexp)
35 #:use-module (guix monads)
36 #:use-module (srfi srfi-1)
37 #:use-module (srfi srfi-26)
38 #:use-module (ice-9 format)
39 #:export (root-file-system-service
41 device-mapping-service
42 user-processes-service
54 ;;; Base system services---i.e., services that 99% of the users will want to
59 (define (root-file-system-service)
60 "Return a service whose sole purpose is to re-mount read-only the root file
61 system upon shutdown (aka. cleanly \"umounting\" root.)
63 This service must be the root of the service dependency graph so that its
64 'stop' action is invoked when dmd is the only process left."
65 (with-monad %store-monad
68 (documentation "Take care of the root file system.")
69 (provision '(root-file-system))
72 ;; Return #f if successfully stopped.
75 (call-with-blocked-asyncs
77 (let ((null (%make-void-port "w")))
79 (display "closing log\n")
80 ;; XXX: Ideally we'd use 'stop-logging', but that one
81 ;; doesn't actually close the port as of dmd 0.1.
82 (close-port (@@ (dmd comm) log-output-port))
83 (set! (@@ (dmd comm) log-output-port) null)
85 ;; Redirect the default output ports..
86 (set-current-output-port null)
87 (set-current-error-port null)
89 ;; Close /dev/console.
90 (for-each close-fdes '(0 1 2))
92 ;; At this point, there are no open files left, so the
93 ;; root file system can be re-mounted read-only.
95 (logior MS_REMOUNT MS_RDONLY)
101 (define* (file-system-service device target type
102 #:key (flags '()) (check? #t)
103 create-mount-point? options (title 'any)
105 "Return a service that mounts DEVICE on TARGET as a file system TYPE with
106 OPTIONS. TITLE is a symbol specifying what kind of name DEVICE is: 'label for
107 a partition label, 'device for a device file name, or 'any. When CHECK? is
108 true, check the file system before mounting it. When CREATE-MOUNT-POINT? is
109 true, create TARGET if it does not exist yet. FLAGS is a list of symbols,
110 such as 'read-only' etc. Optionally, REQUIREMENTS may be a list of service
111 names such as device-mapping services."
112 (with-monad %store-monad
115 (provision (list (symbol-append 'file-system- (string->symbol target))))
116 (requirement `(root-file-system ,@requirements))
117 (documentation "Check, mount, and unmount the given file system.")
118 (start #~(lambda args
119 (let ((device (canonicalize-device-spec #$device '#$title)))
120 #$(if create-mount-point?
125 ;; Make sure fsck.ext2 & co. can be found.
129 "/run/current-system/profile/sbin:"
131 (check-file-system device #$type))
133 (mount device #$target #$type
134 #$(mount-flags->bit-mask flags)
138 ;; Normally there are no processes left at this point, so
139 ;; TARGET can be safely unmounted.
143 (define %do-not-kill-file
144 ;; Name of the file listing PIDs of processes that must survive when halting
145 ;; the system. Typical example is user-space file systems.
146 "/etc/dmd/do-not-kill")
148 (define* (user-processes-service requirements #:key (grace-delay 2))
149 "Return the service that is responsible for terminating all the processes so
150 that the root file system can be re-mounted read-only, just before
151 rebooting/halting. Processes still running GRACE-DELAY seconds after SIGTERM
152 has been sent are terminated with SIGKILL.
154 The returned service will depend on 'root-file-system' and on all the services
155 listed in REQUIREMENTS.
157 All the services that spawn processes must depend on this one so that they are
158 stopped before 'kill' is called."
159 (with-monad %store-monad
161 (documentation "When stopped, terminate all user processes.")
162 (provision '(user-processes))
163 (requirement (cons 'root-file-system requirements))
166 (define (kill-except omit signal)
167 ;; Kill all the processes with SIGNAL except those
168 ;; listed in OMIT and the current process.
169 (let ((omit (cons (getpid) omit)))
170 (for-each (lambda (pid)
171 (unless (memv pid omit)
177 ;; List of PIDs that must not be killed.
178 (if (file-exists? #$%do-not-kill-file)
180 (call-with-input-file #$%do-not-kill-file
181 (compose string-tokenize
182 (@ (ice-9 rdelim) read-string))))
185 ;; When this happens, all the processes have been
186 ;; killed, including 'deco', so DMD-OUTPUT-PORT and
187 ;; thus CURRENT-OUTPUT-PORT are dangling.
188 (call-with-output-file "/dev/console"
190 (display "sending all processes the TERM signal\n"
193 (if (null? omitted-pids)
195 ;; Easy: terminate all of them.
197 (sleep #$grace-delay)
200 ;; Kill them all except OMITTED-PIDS. XXX: We
201 ;; would like to (kill -1 SIGSTOP) to get a fixed
202 ;; list of processes, like 'killall5' does, but
203 ;; that seems unreliable.
204 (kill-except omitted-pids SIGTERM)
205 (sleep #$grace-delay)
206 (kill-except omitted-pids SIGKILL)
207 (delete-file #$%do-not-kill-file)))
209 (display "all processes have been terminated\n")
213 (define (host-name-service name)
214 "Return a service that sets the host name to @var{name}."
215 (with-monad %store-monad
217 (documentation "Initialize the machine's host name.")
218 (provision '(host-name))
220 (sethostname #$name)))
223 (define (unicode-start tty)
224 "Return a gexp to start Unicode support on @var{tty}."
226 ;; We have to run 'unicode_start' in a pipe so that when it invokes the
227 ;; 'tty' command, that command returns TTY.
229 (let ((pid (primitive-fork)))
233 (dup2 (open-fdes #$tty O_RDONLY) 0)
235 (dup2 (open-fdes #$tty O_WRONLY) 1)
236 (execl (string-append #$kbd "/bin/unicode_start")
239 (zero? (cdr (waitpid pid))))))))
241 (define* (console-font-service tty #:optional (font "LatGrkCyr-8x16"))
242 "Return a service that sets up Unicode support in @var{tty} and loads
243 @var{font} for that tty (fonts are per virtual console in Linux.)"
244 ;; Note: 'LatGrkCyr-8x16' has the advantage of providing three common
245 ;; scripts as well as glyphs for em dash, quotation marks, and other Unicode
246 ;; codepoints notably found in the UTF-8 manual.
247 (let ((device (string-append "/dev/" tty)))
248 (with-monad %store-monad
250 (documentation "Load a Unicode console font.")
251 (provision (list (symbol-append 'console-font-
252 (string->symbol tty))))
254 ;; Start after mingetty has been started on TTY, otherwise the
255 ;; settings are ignored.
256 (requirement (list (symbol-append 'term-
257 (string->symbol tty))))
260 (and #$(unicode-start device)
262 (system* (string-append #$kbd "/bin/setfont")
263 "-C" #$device #$font)))))
267 (define* (mingetty-service tty
269 (motd (text-file "motd" "Welcome.\n"))
274 ;; Allow empty passwords by default so that
275 ;; first-time users can log in when the 'root'
276 ;; account has just been created.
277 (allow-empty-passwords? #t))
278 "Return a service to run mingetty on @var{tty}.
280 When @var{allow-empty-passwords?} is true, allow empty log-in password. When
281 @var{auto-login} is true, it must be a user name under which to log-in
282 automatically. @var{login-pause?} can be set to @code{#t} in conjunction with
283 @var{auto-login}, in which case the user will have to press a key before the
284 login shell is launched.
286 When true, @var{login-program} is a gexp or a monadic gexp denoting the name
287 of the log-in program (the default is the @code{login} program from the Shadow
290 @var{motd} is a monadic value containing a text file to use as
291 the ``message of the day''."
292 (mlet %store-monad ((motd motd)
293 (login-program (cond ((gexp? login-program)
294 (return login-program))
301 (documentation (string-append "Run mingetty on " tty "."))
302 (provision (list (symbol-append 'term- (string->symbol tty))))
304 ;; Since the login prompt shows the host name, wait for the 'host-name'
305 ;; service to be done.
306 (requirement '(user-processes host-name))
308 (start #~(make-forkexec-constructor
309 (list (string-append #$mingetty "/sbin/mingetty")
312 #~("--autologin" #$auto-login)
315 #~("--loginprog" #$login-program)
320 (stop #~(make-kill-destructor))
323 ;; Let 'login' be known to PAM. All the mingetty services will have
324 ;; that PAM service, but that's fine because they're all identical and
325 ;; duplicates are removed.
326 (list (unix-pam-service "login"
327 #:allow-empty-passwords? allow-empty-passwords?
330 (define* (nscd-service #:key (glibc (canonical-package glibc)))
331 "Return a service that runs libc's name service cache daemon (nscd)."
332 (with-monad %store-monad
334 (documentation "Run libc's name service cache daemon (nscd).")
336 (requirement '(user-processes))
339 (use-modules (guix build utils))
340 (mkdir-p "/var/run/nscd")))
342 (start #~(make-forkexec-constructor
343 (list (string-append #$glibc "/sbin/nscd")
344 "-f" "/dev/null" "--foreground")))
345 (stop #~(make-kill-destructor))
349 (define (syslog-service)
350 "Return a service that runs @code{syslogd} with reasonable default settings."
352 ;; Snippet adapted from the GNU inetutils manual.
354 # Log all error messages, authentication messages of
355 # level notice or higher and anything of level err or
356 # higher to the console.
357 # Don't log private authentication messages!
358 *.alert;auth.notice;authpriv.none /dev/console
360 # Log anything (except mail) of level info or higher.
361 # Don't log private authentication messages!
362 *.info;mail.none;authpriv.none /var/log/messages
364 # Same, in a different place.
365 *.info;mail.none;authpriv.none /dev/tty12
367 # The authpriv file has restricted access.
368 authpriv.* /var/log/secure
370 # Log all the mail messages in one place.
371 mail.* /var/log/maillog
375 ((syslog.conf (text-file "syslog.conf" contents)))
378 (documentation "Run the syslog daemon (syslogd).")
379 (provision '(syslogd))
380 (requirement '(user-processes))
382 #~(make-forkexec-constructor
383 (list (string-append #$inetutils "/libexec/syslogd")
384 "--no-detach" "--rcfile" #$syslog.conf)))
385 (stop #~(make-kill-destructor))))))
387 (define* (guix-build-accounts count #:key
391 "Return a list of COUNT user accounts for Guix build users, with UIDs
392 starting at FIRST-UID, and under GID."
393 (with-monad %store-monad
394 (return (unfold (cut > <> count)
397 (name (format #f "guixbuilder~2,'0d" n))
399 (uid (+ first-uid n -1))
402 ;; guix-daemon expects GROUP to be listed as a
403 ;; supplementary group too:
404 ;; <http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html>.
405 (supplementary-groups (list group))
407 (comment (format #f "Guix Build User ~2d" n))
408 (home-directory "/var/empty")
409 (shell #~(string-append #$shadow "/sbin/nologin"))))
413 (define (hydra-key-authorization guix)
414 "Return a gexp with code to register the hydra.gnu.org public key with
416 #~(unless (file-exists? "/etc/guix/acl")
417 (let ((pid (primitive-fork)))
420 (let* ((key (string-append #$guix
421 "/share/guix/hydra.gnu.org.pub"))
422 (port (open-file key "r0b")))
423 (format #t "registering public key '~a'...~%" key)
424 (close-port (current-input-port))
426 (execl (string-append #$guix "/bin/guix")
427 "guix" "archive" "--authorize")
430 (let ((status (cdr (waitpid pid))))
431 (unless (zero? status)
432 (format (current-error-port) "warning: \
433 failed to register hydra.gnu.org public key: ~a~%" status))))))))
435 (define* (guix-service #:key (guix guix) (builder-group "guixbuild")
436 (build-accounts 10) authorize-hydra-key?
437 (use-substitutes? #t)
439 "Return a service that runs the build daemon from @var{guix}, and has
440 @var{build-accounts} user accounts available under @var{builder-group}.
442 When @var{authorize-hydra-key?} is true, the @code{hydra.gnu.org} public key
443 provided by @var{guix} is authorized upon activation, meaning that substitutes
444 from @code{hydra.gnu.org} are used by default.
446 If @var{use-substitutes?} is false, the daemon is run with
447 @option{--no-substitutes} (@pxref{Invoking guix-daemon,
448 @option{--no-substitutes}}).
450 Finally, @var{extra-options} is a list of additional command-line options
451 passed to @command{guix-daemon}."
453 ;; Assume that the store has BUILDER-GROUP as its group. We could
454 ;; otherwise call 'chown' here, but the problem is that on a COW unionfs,
455 ;; chown leads to an entire copy of the tree, which is a bad idea.
457 ;; Optionally authorize hydra.gnu.org's key.
458 (and authorize-hydra-key?
459 (hydra-key-authorization guix)))
461 (mlet %store-monad ((accounts (guix-build-accounts build-accounts
462 #:group builder-group)))
464 (provision '(guix-daemon))
465 (requirement '(user-processes))
467 #~(make-forkexec-constructor
468 (list (string-append #$guix "/bin/guix-daemon")
469 "--build-users-group" #$builder-group
470 #$@(if use-substitutes?
472 '("--no-substitutes"))
474 (stop #~(make-kill-destructor))
475 (user-accounts accounts)
476 (user-groups (list (user-group
480 ;; Use a fixed GID so that we can create the
481 ;; store with the right owner.
483 (activate activate)))))
485 (define (udev-rules-union packages)
486 "Return the union of the @code{lib/udev/rules.d} directories found in each
487 item of @var{packages}."
490 (use-modules (guix build union)
495 (define %standard-locations
496 '("/lib/udev/rules.d" "/libexec/udev/rules.d"))
498 (define (rules-sub-directory directory)
499 ;; Return the sub-directory of DIRECTORY containing udev rules, or
500 ;; #f if none was found.
501 (find directory-exists?
502 (map (cut string-append directory <>) %standard-locations)))
504 (mkdir-p (string-append #$output "/lib/udev"))
505 (union-build (string-append #$output "/lib/udev/rules.d")
506 (filter-map rules-sub-directory '#$packages))))
508 (gexp->derivation "udev-rules" build
509 #:modules '((guix build union)
513 (define* (udev-service #:key (udev udev) (rules '()))
514 "Run @var{udev}, which populates the @file{/dev} directory dynamically. Get
515 extra rules from the packages listed in @var{rules}."
516 (mlet* %store-monad ((rules (udev-rules-union (cons udev rules)))
517 (udev.conf (text-file* "udev.conf"
518 "udev_rules=\"" rules
519 "/lib/udev/rules.d\"\n")))
523 ;; Udev needs /dev to be a 'devtmpfs' mount so that new device
524 ;; nodes can be added: see
525 ;; <http://www.linuxfromscratch.org/lfs/view/development/chapter07/udev.html>.
526 (requirement '(root-file-system))
528 (documentation "Populate the /dev directory, dynamically.")
531 (@ (srfi srfi-1) find))
534 ;; Choose the right 'udevd'.
536 (map (lambda (suffix)
537 (string-append #$udev suffix))
538 '("/libexec/udev/udevd" ;udev
539 "/sbin/udevd")))) ;eudev
541 (define (wait-for-udevd)
542 ;; Wait until someone's listening on udevd's control
544 (let ((sock (socket AF_UNIX SOCK_SEQPACKET 0)))
548 (connect sock PF_UNIX "/run/udev/control")
551 (format #t "waiting for udevd...~%")
555 ;; Allow udev to find the modules.
556 (setenv "LINUX_MODULE_DIRECTORY"
557 "/run/booted-system/kernel/lib/modules")
559 ;; The first one is for udev, the second one for eudev.
560 (setenv "UDEV_CONFIG_FILE" #$udev.conf)
561 (setenv "EUDEV_RULES_DIRECTORY" #$rules)
563 (let ((pid (primitive-fork)))
566 (exec-command (list udevd)))
568 ;; Wait until udevd is up and running. This
569 ;; appears to be needed so that the events
570 ;; triggered below are actually handled.
573 ;; Trigger device node creation.
574 (system* (string-append #$udev "/bin/udevadm")
575 "trigger" "--action=add")
577 ;; Wait for things to settle down.
578 (system* (string-append #$udev "/bin/udevadm")
581 (stop #~(make-kill-destructor))))))
583 (define (device-mapping-service target command)
584 "Return a service that maps device @var{target}, a string such as
585 @code{\"home\"} (meaning @code{/dev/mapper/home}), by executing @var{command},
587 (with-monad %store-monad
589 (provision (list (symbol-append 'device-mapping-
590 (string->symbol target))))
591 (requirement '(udev))
592 (documentation "Map a device node using Linux's device mapper.")
598 (define %base-services
599 ;; Convenience variable holding the basic services.
600 (let ((motd (text-file "motd" "
601 This is the GNU operating system, welcome!\n\n")))
602 (list (console-font-service "tty1")
603 (console-font-service "tty2")
604 (console-font-service "tty3")
605 (console-font-service "tty4")
606 (console-font-service "tty5")
607 (console-font-service "tty6")
609 (mingetty-service "tty1" #:motd motd)
610 (mingetty-service "tty2" #:motd motd)
611 (mingetty-service "tty3" #:motd motd)
612 (mingetty-service "tty4" #:motd motd)
613 (mingetty-service "tty5" #:motd motd)
614 (mingetty-service "tty6" #:motd motd)
615 (static-networking-service "lo" "127.0.0.1"
616 #:provision '(loopback))
621 ;; The LVM2 rules are needed as soon as LVM2 or the device-mapper is
622 ;; used, so enable them by default. The FUSE and ALSA rules are
623 ;; less critical, but handy.
624 (udev-service #:rules (list lvm2 fuse alsa-utils)))))
626 ;;; base.scm ends here