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 (gnu services)
21 #:use-module (gnu system shadow) ; 'user-account', etc.
22 #:use-module (gnu system linux) ; 'pam-service', etc.
23 #:use-module (gnu packages admin)
24 #:use-module ((gnu packages base)
25 #:select (glibc-final))
26 #:use-module (gnu packages package-management)
27 #:use-module (guix gexp)
28 #:use-module (guix monads)
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-26)
31 #:use-module (ice-9 format)
32 #:export (root-file-system-service
34 user-processes-service
44 ;;; Base system services---i.e., services that 99% of the users will want to
49 (define (root-file-system-service)
50 "Return a service whose sole purpose is to re-mount read-only the root file
51 system upon shutdown (aka. cleanly \"umounting\" root.)
53 This service must be the root of the service dependency graph so that its
54 'stop' action is invoked when dmd is the only process left."
55 (with-monad %store-monad
58 (documentation "Take care of the root file system.")
59 (provision '(root-file-system))
62 ;; Return #f if successfully stopped.
65 (call-with-blocked-asyncs
67 (let ((null (%make-void-port "w")))
69 (display "closing log\n")
70 ;; XXX: Ideally we'd use 'stop-logging', but that one
71 ;; doesn't actually close the port as of dmd 0.1.
72 (close-port (@@ (dmd comm) log-output-port))
73 (set! (@@ (dmd comm) log-output-port) null)
75 ;; Redirect the default output ports..
76 (set-current-output-port null)
77 (set-current-error-port null)
79 ;; Close /dev/console.
80 (for-each close-fdes '(0 1 2))
82 ;; At this point, there are no open files left, so the
83 ;; root file system can be re-mounted read-only.
85 (logior MS_REMOUNT MS_RDONLY)
91 (define* (file-system-service device target type
92 #:key (check? #t) options (title 'any))
93 "Return a service that mounts DEVICE on TARGET as a file system TYPE with
94 OPTIONS. TITLE is a symbol specifying what kind of name DEVICE is: 'label for
95 a partition label, 'device for a device file name, or 'any. When CHECK? is
96 true, check the file system before mounting it."
97 (with-monad %store-monad
100 (provision (list (symbol-append 'file-system- (string->symbol target))))
101 (requirement '(root-file-system))
102 (documentation "Check, mount, and unmount the given file system.")
103 (start #~(lambda args
104 (let ((device (canonicalize-device-spec #$device '#$title)))
106 #~(check-file-system device #$type)
108 (mount device #$target #$type 0 #$options))
111 ;; Normally there are no processes left at this point, so
112 ;; TARGET can be safely unmounted.
116 (define %do-not-kill-file
117 ;; Name of the file listing PIDs of processes that must survive when halting
118 ;; the system. Typical example is user-space file systems.
119 "/etc/dmd/do-not-kill")
121 (define* (user-processes-service requirements #:key (grace-delay 2))
122 "Return the service that is responsible for terminating all the processes so
123 that the root file system can be re-mounted read-only, just before
124 rebooting/halting. Processes still running GRACE-DELAY seconds after SIGTERM
125 has been sent are terminated with SIGKILL.
127 The returned service will depend on 'root-file-system' and on all the services
128 listed in REQUIREMENTS.
130 All the services that spawn processes must depend on this one so that they are
131 stopped before 'kill' is called."
132 (with-monad %store-monad
134 (documentation "When stopped, terminate all user processes.")
135 (provision '(user-processes))
136 (requirement (cons 'root-file-system requirements))
139 (define (kill-except omit signal)
140 ;; Kill all the processes with SIGNAL except those
141 ;; listed in OMIT and the current process.
142 (let ((omit (cons (getpid) omit)))
143 (for-each (lambda (pid)
144 (unless (memv pid omit)
150 ;; List of PIDs that must not be killed.
151 (if (file-exists? #$%do-not-kill-file)
153 (call-with-input-file #$%do-not-kill-file
154 (compose string-tokenize
155 (@ (ice-9 rdelim) read-string))))
158 ;; When this happens, all the processes have been
159 ;; killed, including 'deco', so DMD-OUTPUT-PORT and
160 ;; thus CURRENT-OUTPUT-PORT are dangling.
161 (call-with-output-file "/dev/console"
163 (display "sending all processes the TERM signal\n"
166 (if (null? omitted-pids)
168 ;; Easy: terminate all of them.
170 (sleep #$grace-delay)
173 ;; Kill them all except OMITTED-PIDS. XXX: We
174 ;; would like to (kill -1 SIGSTOP) to get a fixed
175 ;; list of processes, like 'killall5' does, but
176 ;; that seems unreliable.
177 (kill-except omitted-pids SIGTERM)
178 (sleep #$grace-delay)
179 (kill-except omitted-pids SIGKILL)
180 (delete-file #$%do-not-kill-file)))
182 (display "all processes have been terminated\n")
186 (define (host-name-service name)
187 "Return a service that sets the host name to NAME."
188 (with-monad %store-monad
190 (documentation "Initialize the machine's host name.")
191 (provision '(host-name))
193 (sethostname #$name)))
196 (define* (mingetty-service tty
198 (motd (text-file "motd" "Welcome.\n"))
202 (allow-empty-passwords? #t))
203 "Return a service to run mingetty on @var{tty}.
205 When @var{allow-empty-passwords?} is true, allow empty log-in password. When
206 @var{auto-login} is true, it must be a user name under which to log-in
207 automatically. @var{login-pause?} can be set to @code{#t} in conjunction with
208 @var{auto-login}, in which case the user will have to press a key before the
209 login shell is launched.
211 When true, @var{login-program} is a gexp or a monadic gexp denoting the name
212 of the log-in program (the default is the @code{login} program from the Shadow
215 @var{motd} is a monadic value containing a text file to use as
216 the \"message of the day\"."
217 (mlet %store-monad ((motd motd)
218 (login-program (cond ((gexp? login-program)
219 (return login-program))
226 (documentation (string-append "Run mingetty on " tty "."))
227 (provision (list (symbol-append 'term- (string->symbol tty))))
229 ;; Since the login prompt shows the host name, wait for the 'host-name'
230 ;; service to be done.
231 (requirement '(user-processes host-name))
233 (start #~(make-forkexec-constructor
234 (string-append #$mingetty "/sbin/mingetty")
237 #~("--autologin" #$auto-login)
240 #~("--loginprog" #$login-program)
245 (stop #~(make-kill-destructor))
248 ;; Let 'login' be known to PAM. All the mingetty services will have
249 ;; that PAM service, but that's fine because they're all identical and
250 ;; duplicates are removed.
251 (list (unix-pam-service "login"
252 #:allow-empty-passwords? allow-empty-passwords?
255 (define* (nscd-service #:key (glibc glibc-final))
256 "Return a service that runs libc's name service cache daemon (nscd)."
257 (with-monad %store-monad
259 (documentation "Run libc's name service cache daemon (nscd).")
261 (requirement '(user-processes))
264 (use-modules (guix build utils))
265 (mkdir-p "/var/run/nscd")))
268 #~(make-forkexec-constructor (string-append #$glibc "/sbin/nscd")
271 (stop #~(make-kill-destructor))
275 (define (syslog-service)
276 "Return a service that runs 'syslogd' with reasonable default settings."
278 ;; Snippet adapted from the GNU inetutils manual.
280 # Log all error messages, authentication messages of
281 # level notice or higher and anything of level err or
282 # higher to the console.
283 # Don't log private authentication messages!
284 *.err;auth.notice;authpriv.none /dev/console
286 # Log anything (except mail) of level info or higher.
287 # Don't log private authentication messages!
288 *.info;mail.none;authpriv.none /var/log/messages
290 # Same, in a different place.
291 *.info;mail.none;authpriv.none /dev/tty12
293 # The authpriv file has restricted access.
294 authpriv.* /var/log/secure
296 # Log all the mail messages in one place.
297 mail.* /var/log/maillog
301 ((syslog.conf (text-file "syslog.conf" contents)))
304 (documentation "Run the syslog daemon (syslogd).")
305 (provision '(syslogd))
306 (requirement '(user-processes))
308 #~(make-forkexec-constructor (string-append #$inetutils
311 "--rcfile" #$syslog.conf))
312 (stop #~(make-kill-destructor))))))
314 (define* (guix-build-accounts count #:key
318 "Return a list of COUNT user accounts for Guix build users, with UIDs
319 starting at FIRST-UID, and under GID."
320 (with-monad %store-monad
321 (return (unfold (cut > <> count)
324 (name (format #f "guixbuilder~2,'0d" n))
325 (uid (+ first-uid n -1))
327 (comment (format #f "Guix Build User ~2d" n))
328 (home-directory "/var/empty")
329 (shell #~(string-append #$shadow "/sbin/nologin"))))
333 (define (hydra-key-authorization guix)
334 "Return a gexp with code to register the hydra.gnu.org public key with
336 #~(unless (file-exists? "/etc/guix/acl")
337 (let ((pid (primitive-fork)))
340 (let* ((key (string-append #$guix
341 "/share/guix/hydra.gnu.org.pub"))
342 (port (open-file key "r0b")))
343 (format #t "registering public key '~a'...~%" key)
344 (close-port (current-input-port))
347 (execl (string-append #$guix "/bin/guix")
348 "guix" "archive" "--authorize")
351 (let ((status (cdr (waitpid pid))))
352 (unless (zero? status)
353 (format (current-error-port) "warning: \
354 failed to register hydra.gnu.org public key: ~a~%" status))))))))
356 (define* (guix-service #:key (guix guix) (builder-group "guixbuild")
357 (build-accounts 10) authorize-hydra-key?)
358 "Return a service that runs the build daemon from GUIX, and has
359 BUILD-ACCOUNTS user accounts available under BUILD-USER-GID.
361 When AUTHORIZE-HYDRA-KEY? is true, the hydra.gnu.org public key provided by
362 GUIX is authorized upon activation, meaning that substitutes from
363 hydra.gnu.org are used by default."
364 (mlet %store-monad ((accounts (guix-build-accounts build-accounts
365 #:group builder-group)))
367 (provision '(guix-daemon))
368 (requirement '(user-processes))
370 #~(make-forkexec-constructor (string-append #$guix
372 "--build-users-group"
374 (stop #~(make-kill-destructor))
375 (user-accounts accounts)
376 (user-groups (list (user-group
378 (members (map user-account-name
380 (activate (and authorize-hydra-key?
381 (hydra-key-authorization guix)))))))
383 (define %base-services
384 ;; Convenience variable holding the basic services.
385 (let ((motd (text-file "motd" "
386 This is the GNU operating system, welcome!\n\n")))
387 (list (mingetty-service "tty1" #:motd motd)
388 (mingetty-service "tty2" #:motd motd)
389 (mingetty-service "tty3" #:motd motd)
390 (mingetty-service "tty4" #:motd motd)
391 (mingetty-service "tty5" #:motd motd)
392 (mingetty-service "tty6" #:motd motd)
397 ;;; base.scm ends here