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)
93 "Return a service that mounts DEVICE on TARGET as a file system TYPE with
94 OPTIONS. When CHECK? is true, check the file system before mounting it."
95 (with-monad %store-monad
98 (provision (list (symbol-append 'file-system- (string->symbol target))))
99 (requirement '(root-file-system))
100 (documentation "Check, mount, and unmount the given file system.")
101 (start #~(lambda args
103 #~(check-file-system #$device #$type)
105 (mount #$device #$target #$type 0 #$options)
108 ;; Normally there are no processes left at this point, so
109 ;; TARGET can be safely unmounted.
113 (define %do-not-kill-file
114 ;; Name of the file listing PIDs of processes that must survive when halting
115 ;; the system. Typical example is user-space file systems.
116 "/etc/dmd/do-not-kill")
118 (define* (user-processes-service requirements #:key (grace-delay 2))
119 "Return the service that is responsible for terminating all the processes so
120 that the root file system can be re-mounted read-only, just before
121 rebooting/halting. Processes still running GRACE-DELAY seconds after SIGTERM
122 has been sent are terminated with SIGKILL.
124 The returned service will depend on 'root-file-system' and on all the services
125 listed in REQUIREMENTS.
127 All the services that spawn processes must depend on this one so that they are
128 stopped before 'kill' is called."
129 (with-monad %store-monad
131 (documentation "When stopped, terminate all user processes.")
132 (provision '(user-processes))
133 (requirement (cons 'root-file-system requirements))
136 (define (kill-except omit signal)
137 ;; Kill all the processes with SIGNAL except those
138 ;; listed in OMIT and the current process.
139 (let ((omit (cons (getpid) omit)))
140 (for-each (lambda (pid)
141 (unless (memv pid omit)
147 ;; List of PIDs that must not be killed.
148 (if (file-exists? #$%do-not-kill-file)
150 (call-with-input-file #$%do-not-kill-file
151 (compose string-tokenize
152 (@ (ice-9 rdelim) read-string))))
155 ;; When this happens, all the processes have been
156 ;; killed, including 'deco', so DMD-OUTPUT-PORT and
157 ;; thus CURRENT-OUTPUT-PORT are dangling.
158 (call-with-output-file "/dev/console"
160 (display "sending all processes the TERM signal\n"
163 (if (null? omitted-pids)
165 ;; Easy: terminate all of them.
167 (sleep #$grace-delay)
170 ;; Kill them all except OMITTED-PIDS. XXX: We
171 ;; would like to (kill -1 SIGSTOP) to get a fixed
172 ;; list of processes, like 'killall5' does, but
173 ;; that seems unreliable.
174 (kill-except omitted-pids SIGTERM)
175 (sleep #$grace-delay)
176 (kill-except omitted-pids SIGKILL)
177 (delete-file #$%do-not-kill-file)))
179 (display "all processes have been terminated\n")
183 (define (host-name-service name)
184 "Return a service that sets the host name to NAME."
185 (with-monad %store-monad
187 (documentation "Initialize the machine's host name.")
188 (provision '(host-name))
190 (sethostname #$name)))
193 (define* (mingetty-service tty
195 (motd (text-file "motd" "Welcome.\n"))
196 (allow-empty-passwords? #t))
197 "Return a service to run mingetty on TTY."
198 (mlet %store-monad ((motd motd))
201 (documentation (string-append "Run mingetty on " tty "."))
202 (provision (list (symbol-append 'term- (string->symbol tty))))
204 ;; Since the login prompt shows the host name, wait for the 'host-name'
205 ;; service to be done.
206 (requirement '(user-processes host-name))
208 (start #~(make-forkexec-constructor
209 (string-append #$mingetty "/sbin/mingetty")
211 (stop #~(make-kill-destructor))
214 ;; Let 'login' be known to PAM. All the mingetty services will have
215 ;; that PAM service, but that's fine because they're all identical and
216 ;; duplicates are removed.
217 (list (unix-pam-service "login"
218 #:allow-empty-passwords? allow-empty-passwords?
221 (define* (nscd-service #:key (glibc glibc-final))
222 "Return a service that runs libc's name service cache daemon (nscd)."
223 (with-monad %store-monad
225 (documentation "Run libc's name service cache daemon (nscd).")
227 (requirement '(user-processes))
229 #~(make-forkexec-constructor (string-append #$glibc "/sbin/nscd")
232 (stop #~(make-kill-destructor))
236 (define (syslog-service)
237 "Return a service that runs 'syslogd' with reasonable default settings."
239 ;; Snippet adapted from the GNU inetutils manual.
241 # Log all kernel messages, authentication messages of
242 # level notice or higher and anything of level err or
243 # higher to the console.
244 # Don't log private authentication messages!
245 *.err;kern.*;auth.notice;authpriv.none /dev/console
247 # Log anything (except mail) of level info or higher.
248 # Don't log private authentication messages!
249 *.info;mail.none;authpriv.none /var/log/messages
251 # Same, in a different place.
252 *.info;mail.none;authpriv.none /dev/tty12
254 # The authpriv file has restricted access.
255 authpriv.* /var/log/secure
257 # Log all the mail messages in one place.
258 mail.* /var/log/maillog
262 ((syslog.conf (text-file "syslog.conf" contents)))
265 (documentation "Run the syslog daemon (syslogd).")
266 (provision '(syslogd))
267 (requirement '(user-processes))
269 #~(make-forkexec-constructor (string-append #$inetutils
272 "--rcfile" #$syslog.conf))
273 (stop #~(make-kill-destructor))))))
275 (define* (guix-build-accounts count #:key
279 "Return a list of COUNT user accounts for Guix build users, with UIDs
280 starting at FIRST-UID, and under GID."
281 (with-monad %store-monad
282 (return (unfold (cut > <> count)
285 (name (format #f "guixbuilder~2,'0d" n))
286 (uid (+ first-uid n -1))
288 (comment (format #f "Guix Build User ~2d" n))
289 (home-directory "/var/empty")
290 (shell #~(string-append #$shadow "/sbin/nologin"))))
294 (define* (guix-service #:key (guix guix) (builder-group "guixbuild")
296 "Return a service that runs the build daemon from GUIX, and has
297 BUILD-ACCOUNTS user accounts available under BUILD-USER-GID."
298 (mlet %store-monad ((accounts (guix-build-accounts build-accounts
299 #:group builder-group)))
301 (provision '(guix-daemon))
302 (requirement '(user-processes))
304 #~(make-forkexec-constructor (string-append #$guix
306 "--build-users-group"
308 (stop #~(make-kill-destructor))
309 (user-accounts accounts)
310 (user-groups (list (user-group
312 (members (map user-account-name
313 user-accounts)))))))))
315 (define %base-services
316 ;; Convenience variable holding the basic services.
317 (let ((motd (text-file "motd" "
318 This is the GNU operating system, welcome!\n\n")))
319 (list (mingetty-service "tty1" #:motd motd)
320 (mingetty-service "tty2" #:motd motd)
321 (mingetty-service "tty3" #:motd motd)
322 (mingetty-service "tty4" #:motd motd)
323 (mingetty-service "tty5" #:motd motd)
324 (mingetty-service "tty6" #:motd motd)
329 ;;; base.scm ends here