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 system shadow) ; 'user-account', etc.
24 #:use-module (gnu system linux) ; 'pam-service', etc.
25 #:use-module (gnu packages admin)
26 #:use-module ((gnu packages linux)
28 #:use-module ((gnu packages base)
29 #:select (glibc-final))
30 #:use-module (gnu packages package-management)
31 #:use-module (guix gexp)
32 #:use-module (guix monads)
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-26)
35 #:use-module (ice-9 format)
36 #:export (root-file-system-service
38 user-processes-service
49 ;;; Base system services---i.e., services that 99% of the users will want to
54 (define (root-file-system-service)
55 "Return a service whose sole purpose is to re-mount read-only the root file
56 system upon shutdown (aka. cleanly \"umounting\" root.)
58 This service must be the root of the service dependency graph so that its
59 'stop' action is invoked when dmd is the only process left."
60 (with-monad %store-monad
63 (documentation "Take care of the root file system.")
64 (provision '(root-file-system))
67 ;; Return #f if successfully stopped.
70 (call-with-blocked-asyncs
72 (let ((null (%make-void-port "w")))
74 (display "closing log\n")
75 ;; XXX: Ideally we'd use 'stop-logging', but that one
76 ;; doesn't actually close the port as of dmd 0.1.
77 (close-port (@@ (dmd comm) log-output-port))
78 (set! (@@ (dmd comm) log-output-port) null)
80 ;; Redirect the default output ports..
81 (set-current-output-port null)
82 (set-current-error-port null)
84 ;; Close /dev/console.
85 (for-each close-fdes '(0 1 2))
87 ;; At this point, there are no open files left, so the
88 ;; root file system can be re-mounted read-only.
90 (logior MS_REMOUNT MS_RDONLY)
96 (define* (file-system-service device target type
97 #:key (check? #t) options (title 'any))
98 "Return a service that mounts DEVICE on TARGET as a file system TYPE with
99 OPTIONS. TITLE is a symbol specifying what kind of name DEVICE is: 'label for
100 a partition label, 'device for a device file name, or 'any. When CHECK? is
101 true, check the file system before mounting it."
102 (with-monad %store-monad
105 (provision (list (symbol-append 'file-system- (string->symbol target))))
106 (requirement '(root-file-system))
107 (documentation "Check, mount, and unmount the given file system.")
108 (start #~(lambda args
109 (let ((device (canonicalize-device-spec #$device '#$title)))
111 #~(check-file-system device #$type)
113 (mount device #$target #$type 0 #$options))
116 ;; Normally there are no processes left at this point, so
117 ;; TARGET can be safely unmounted.
121 (define %do-not-kill-file
122 ;; Name of the file listing PIDs of processes that must survive when halting
123 ;; the system. Typical example is user-space file systems.
124 "/etc/dmd/do-not-kill")
126 (define* (user-processes-service requirements #:key (grace-delay 2))
127 "Return the service that is responsible for terminating all the processes so
128 that the root file system can be re-mounted read-only, just before
129 rebooting/halting. Processes still running GRACE-DELAY seconds after SIGTERM
130 has been sent are terminated with SIGKILL.
132 The returned service will depend on 'root-file-system' and on all the services
133 listed in REQUIREMENTS.
135 All the services that spawn processes must depend on this one so that they are
136 stopped before 'kill' is called."
137 (with-monad %store-monad
139 (documentation "When stopped, terminate all user processes.")
140 (provision '(user-processes))
141 (requirement (cons 'root-file-system requirements))
144 (define (kill-except omit signal)
145 ;; Kill all the processes with SIGNAL except those
146 ;; listed in OMIT and the current process.
147 (let ((omit (cons (getpid) omit)))
148 (for-each (lambda (pid)
149 (unless (memv pid omit)
155 ;; List of PIDs that must not be killed.
156 (if (file-exists? #$%do-not-kill-file)
158 (call-with-input-file #$%do-not-kill-file
159 (compose string-tokenize
160 (@ (ice-9 rdelim) read-string))))
163 ;; When this happens, all the processes have been
164 ;; killed, including 'deco', so DMD-OUTPUT-PORT and
165 ;; thus CURRENT-OUTPUT-PORT are dangling.
166 (call-with-output-file "/dev/console"
168 (display "sending all processes the TERM signal\n"
171 (if (null? omitted-pids)
173 ;; Easy: terminate all of them.
175 (sleep #$grace-delay)
178 ;; Kill them all except OMITTED-PIDS. XXX: We
179 ;; would like to (kill -1 SIGSTOP) to get a fixed
180 ;; list of processes, like 'killall5' does, but
181 ;; that seems unreliable.
182 (kill-except omitted-pids SIGTERM)
183 (sleep #$grace-delay)
184 (kill-except omitted-pids SIGKILL)
185 (delete-file #$%do-not-kill-file)))
187 (display "all processes have been terminated\n")
191 (define (host-name-service name)
192 "Return a service that sets the host name to NAME."
193 (with-monad %store-monad
195 (documentation "Initialize the machine's host name.")
196 (provision '(host-name))
198 (sethostname #$name)))
201 (define* (mingetty-service tty
203 (motd (text-file "motd" "Welcome.\n"))
207 (allow-empty-passwords? #t))
208 "Return a service to run mingetty on @var{tty}.
210 When @var{allow-empty-passwords?} is true, allow empty log-in password. When
211 @var{auto-login} is true, it must be a user name under which to log-in
212 automatically. @var{login-pause?} can be set to @code{#t} in conjunction with
213 @var{auto-login}, in which case the user will have to press a key before the
214 login shell is launched.
216 When true, @var{login-program} is a gexp or a monadic gexp denoting the name
217 of the log-in program (the default is the @code{login} program from the Shadow
220 @var{motd} is a monadic value containing a text file to use as
221 the \"message of the day\"."
222 (mlet %store-monad ((motd motd)
223 (login-program (cond ((gexp? login-program)
224 (return login-program))
231 (documentation (string-append "Run mingetty on " tty "."))
232 (provision (list (symbol-append 'term- (string->symbol tty))))
234 ;; Since the login prompt shows the host name, wait for the 'host-name'
235 ;; service to be done.
236 (requirement '(user-processes host-name))
238 (start #~(make-forkexec-constructor
239 (string-append #$mingetty "/sbin/mingetty")
242 #~("--autologin" #$auto-login)
245 #~("--loginprog" #$login-program)
250 (stop #~(make-kill-destructor))
253 ;; Let 'login' be known to PAM. All the mingetty services will have
254 ;; that PAM service, but that's fine because they're all identical and
255 ;; duplicates are removed.
256 (list (unix-pam-service "login"
257 #:allow-empty-passwords? allow-empty-passwords?
260 (define* (nscd-service #:key (glibc glibc-final))
261 "Return a service that runs libc's name service cache daemon (nscd)."
262 (with-monad %store-monad
264 (documentation "Run libc's name service cache daemon (nscd).")
266 (requirement '(user-processes))
269 (use-modules (guix build utils))
270 (mkdir-p "/var/run/nscd")))
273 #~(make-forkexec-constructor (string-append #$glibc "/sbin/nscd")
276 (stop #~(make-kill-destructor))
280 (define (syslog-service)
281 "Return a service that runs 'syslogd' with reasonable default settings."
283 ;; Snippet adapted from the GNU inetutils manual.
285 # Log all error messages, authentication messages of
286 # level notice or higher and anything of level err or
287 # higher to the console.
288 # Don't log private authentication messages!
289 *.err;auth.notice;authpriv.none /dev/console
291 # Log anything (except mail) of level info or higher.
292 # Don't log private authentication messages!
293 *.info;mail.none;authpriv.none /var/log/messages
295 # Same, in a different place.
296 *.info;mail.none;authpriv.none /dev/tty12
298 # The authpriv file has restricted access.
299 authpriv.* /var/log/secure
301 # Log all the mail messages in one place.
302 mail.* /var/log/maillog
306 ((syslog.conf (text-file "syslog.conf" contents)))
309 (documentation "Run the syslog daemon (syslogd).")
310 (provision '(syslogd))
311 (requirement '(user-processes))
313 #~(make-forkexec-constructor (string-append #$inetutils
316 "--rcfile" #$syslog.conf))
317 (stop #~(make-kill-destructor))))))
319 (define* (guix-build-accounts count #:key
323 "Return a list of COUNT user accounts for Guix build users, with UIDs
324 starting at FIRST-UID, and under GID."
325 (with-monad %store-monad
326 (return (unfold (cut > <> count)
329 (name (format #f "guixbuilder~2,'0d" n))
331 (uid (+ first-uid n -1))
334 ;; guix-daemon expects GROUP to be listed as a
335 ;; supplementary group too:
336 ;; <http://lists.gnu.org/archive/html/bug-guix/2013-01/msg00239.html>.
337 (supplementary-groups (list group))
339 (comment (format #f "Guix Build User ~2d" n))
340 (home-directory "/var/empty")
341 (shell #~(string-append #$shadow "/sbin/nologin"))))
345 (define (hydra-key-authorization guix)
346 "Return a gexp with code to register the hydra.gnu.org public key with
348 #~(unless (file-exists? "/etc/guix/acl")
349 (let ((pid (primitive-fork)))
352 (let* ((key (string-append #$guix
353 "/share/guix/hydra.gnu.org.pub"))
354 (port (open-file key "r0b")))
355 (format #t "registering public key '~a'...~%" key)
356 (close-port (current-input-port))
358 (execl (string-append #$guix "/bin/guix")
359 "guix" "archive" "--authorize")
362 (let ((status (cdr (waitpid pid))))
363 (unless (zero? status)
364 (format (current-error-port) "warning: \
365 failed to register hydra.gnu.org public key: ~a~%" status))))))))
367 (define* (guix-service #:key (guix guix) (builder-group "guixbuild")
368 (build-accounts 10) authorize-hydra-key?)
369 "Return a service that runs the build daemon from GUIX, and has
370 BUILD-ACCOUNTS user accounts available under BUILD-USER-GID.
372 When AUTHORIZE-HYDRA-KEY? is true, the hydra.gnu.org public key provided by
373 GUIX is authorized upon activation, meaning that substitutes from
374 hydra.gnu.org are used by default."
376 ;; Assume that the store has BUILDER-GROUP as its group. We could
377 ;; otherwise call 'chown' here, but the problem is that on a COW unionfs,
378 ;; chown leads to an entire copy of the tree, which is a bad idea.
380 ;; Optionally authorize hydra.gnu.org's key.
381 (and authorize-hydra-key?
382 (hydra-key-authorization guix)))
384 (mlet %store-monad ((accounts (guix-build-accounts build-accounts
385 #:group builder-group)))
387 (provision '(guix-daemon))
388 (requirement '(user-processes))
390 #~(make-forkexec-constructor (string-append #$guix
392 "--build-users-group"
394 (stop #~(make-kill-destructor))
395 (user-accounts accounts)
396 (user-groups (list (user-group
399 ;; Use a fixed GID so that we can create the
400 ;; store with the right owner.
402 (activate activate)))))
404 (define* (udev-service #:key (udev udev))
405 "Run @var{udev}, which populates the @file{/dev} directory dynamically."
406 (with-monad %store-monad
409 (requirement '(root-file-system))
410 (documentation "Populate the /dev directory.")
412 ;; Allow udev to find the modules.
413 (setenv "LINUX_MODULE_DIRECTORY"
414 "/run/booted-system/kernel/lib/modules")
416 (let ((pid (primitive-fork)))
419 ;; In dmd 0.1, file descriptor 0 is closed, thus
420 ;; is gets reused when open(2) is called, and it
421 ;; turns out that EPOLL_CTL_ADD of 0 returns
422 ;; EPERM for some reason. So make sure 0 is
424 ;; FIXME: Close the other descriptors.
425 (execl (string-append #$udev "/libexec/udev/udevd")
428 ;; Wait for things to settle down.
429 (system* (string-append #$udev "/bin/udevadm")
431 ;; Create a bunch of devices.
432 (system* (string-append #$udev "/bin/udevadm")
435 (stop #~(make-kill-destructor))))))
437 (define %base-services
438 ;; Convenience variable holding the basic services.
439 (let ((motd (text-file "motd" "
440 This is the GNU operating system, welcome!\n\n")))
441 (list (mingetty-service "tty1" #:motd motd)
442 (mingetty-service "tty2" #:motd motd)
443 (mingetty-service "tty3" #:motd motd)
444 (mingetty-service "tty4" #:motd motd)
445 (mingetty-service "tty5" #:motd motd)
446 (mingetty-service "tty6" #:motd motd)
452 ;;; base.scm ends here