1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu build hurd-boot)
21 #:use-module (system repl error-handling)
22 #:autoload (system repl repl) (start-repl)
23 #:use-module (srfi srfi-1)
24 #:use-module (srfi srfi-26)
25 #:use-module (ice-9 match)
26 #:use-module (guix build utils)
27 #:use-module ((guix build syscalls)
28 #:hide (file-system-type))
29 #:export (make-hurd-device-nodes
34 ;;; Utility procedures useful to boot a Hurd system.
38 ;; XXX FIXME c&p from linux-boot.scm
39 (define (find-long-option option arguments)
40 "Find OPTION among ARGUMENTS, where OPTION is something like \"--load\".
41 Return the value associated with OPTION, or #f on failure."
42 (let ((opt (string-append option "=")))
43 (and=> (find (cut string-prefix? opt <>)
46 (substring arg (+ 1 (string-index arg #\=)))))))
48 ;; XXX FIXME c&p from guix/utils.scm
49 (define (readlink* file)
50 "Call 'readlink' until the result is not a symlink."
51 (define %max-symlink-depth 50)
53 (let loop ((file file)
55 (define (absolute target)
56 (if (absolute-file-name? target)
58 (string-append (dirname file) "/" target)))
60 (if (>= depth %max-symlink-depth)
66 (values #t (readlink file)))
68 (let ((errno (system-error-errno args)))
69 (if (or (= errno EINVAL))
71 (apply throw args))))))
72 (lambda (success? target)
74 (loop (absolute target) (+ depth 1))
77 (define* (make-hurd-device-nodes #:optional (root "/"))
78 "Make some of the nodes needed on GNU/Hurd."
80 (string-append root (if (string-suffix? "/" root) "" "/") dir))
83 ;; Don't create /dev/null etc just yet; the store
84 ;; messes-up the permission bits.
85 ;; Don't create /dev/console, /dev/vcs, etc.: they are created by
86 ;; console-run on first boot.
88 (mkdir (scope "servers"))
89 (for-each (lambda (file)
90 (call-with-output-file (scope (string-append "servers/" file))
92 (display file port) ;avoid hard-linking
103 (mkdir (scope "servers/socket"))
104 ;; Don't create /servers/socket/1 & co: runsystem does that on first boot.
106 ;; TODO: Set the 'gnu.translator' extended attribute for passive translator
110 (define (passive-translator-xattr? file-name)
111 "Return true if FILE-NAME has an extended @code{gnu.translator} attribute
114 (lambda _ (not (string-null? (getxattr file-name "gnu.translator"))))
116 (if (= ENODATA (system-error-errno args))
118 (apply throw args)))))
120 (define (passive-translator-installed? file-name)
121 "Return true if @file{showtrans} finds a translator installed on FILE-NAME."
122 (with-output-to-port (%make-void-port "w")
124 (with-error-to-port (%make-void-port "w")
126 (zero? (system* "showtrans" "--silent" file-name)))))))
128 (define (translated? file-name)
129 "Return true if a translator is installed on FILE-NAME."
130 (if (string-contains %host-type "linux-gnu")
131 (passive-translator-xattr? file-name)
132 (passive-translator-installed? file-name)))
134 (define* (set-translator file-name command #:optional (mode #o600))
135 "Setup translator COMMAND on FILE-NAME."
136 (unless (translated? file-name)
137 (let ((dir (dirname file-name)))
138 (unless (directory-exists? dir)
140 (unless (file-exists? file-name)
141 (call-with-output-file file-name
143 (display file-name port) ;avoid hard-linking
144 (chmod port mode)))))
147 (setxattr file-name "gnu.translator" (string-join command "\0" 'suffix)))
149 (let ((errno (system-error-errno (cons key args))))
150 (format (current-error-port) "~a: ~a\n"
151 (strerror errno) file-name)
152 (format (current-error-port) "Ignoring...Good Luck!\n"))))))
154 (define-syntax-rule (false-if-EEXIST exp)
155 "Evaluate EXP but return #f if it raises to 'system-error with EEXIST."
159 (if (= EEXIST (system-error-errno args))
161 (apply throw args)))))
163 (define* (set-hurd-device-translators #:optional (root "/"))
164 "Make some of the device nodes needed on GNU/Hurd."
167 (string-append root (if (string-suffix? "/" root) "" "/") dir))
169 (define scope-set-translator
172 (scope-set-translator (list file-name command #o600)))
173 ((file-name command mode)
174 (let ((mount-point (scope file-name)))
175 (set-translator mount-point command mode)))))
178 (let ((dir (scope dir)))
179 (unless (file-exists? dir)
183 '(("servers/crash-dump-core" ("/hurd/crash" "--dump-core"))
184 ("servers/crash-kill" ("/hurd/crash" "--kill"))
185 ("servers/crash-suspend" ("/hurd/crash" "--suspend"))
186 ("servers/password" ("/hurd/password"))
187 ("servers/socket/1" ("/hurd/pflocal"))
188 ("servers/socket/2" ("/hurd/pfinet"
191 "10.0.2.15" ;the default QEMU guest IP
192 "--netmask" "255.255.255.0"
193 "--gateway" "10.0.2.2"
194 "--ipv6" "/servers/socket/16"))))
197 '(("dev/full" ("/hurd/null" "--full") #o666)
198 ("dev/null" ("/hurd/null") #o666)
199 ("dev/random" ("/hurd/random" "--seed-file" "/var/lib/random-seed")
201 ("dev/zero" ("/hurd/storeio" "--store-type=zero") #o666)
203 ("dev/console" ("/hurd/term" "/dev/console" "device" "console"))
205 ("dev/klog" ("/hurd/streamio" "kmsg"))
206 ("dev/mem" ("/hurd/storeio" "--no-cache" "mem") #o660)
207 ("dev/shm" ("/hurd/tmpfs" "--mode=1777" "50%") #o644)
208 ("dev/time" ("/hurd/storeio" "--no-cache" "time") #o644)
210 ("dev/vcs" ("/hurd/console"))
211 ("dev/tty" ("/hurd/magic" "tty") #o666)
213 ("dev/tty1" ("/hurd/term" "/dev/tty1" "hurdio" "/dev/vcs/1/console")
215 ("dev/tty2" ("/hurd/term" "/dev/tty2" "hurdio" "/dev/vcs/2/console")
217 ("dev/tty3" ("/hurd/term" "/dev/tty3" "hurdio" "/dev/vcs/3/console")
220 ("dev/ptyp0" ("/hurd/term" "/dev/ptyp0" "pty-master" "/dev/ttyp0")
222 ("dev/ptyp1" ("/hurd/term" "/dev/ptyp1" "pty-master" "/dev/ttyp1")
224 ("dev/ptyp2" ("/hurd/term" "/dev/ptyp2" "pty-master" "/dev/ttyp2")
227 ("dev/ttyp0" ("/hurd/term" "/dev/ttyp0" "pty-slave" "/dev/ptyp0")
229 ("dev/ttyp1" ("/hurd/term" "/dev/ttyp1" "pty-slave" "/dev/ptyp1")
231 ("dev/ttyp2" ("/hurd/term" "/dev/ttyp2" "pty-slave" "/dev/ptyp2")
234 (for-each scope-set-translator servers)
238 (rename-file (scope "dev/console") (scope "dev/console-"))
239 (for-each scope-set-translator devices)
241 (false-if-EEXIST (symlink "/dev/random" (scope "dev/urandom")))
243 (false-if-EEXIST (symlink "/dev/fd/0" (scope "dev/stdin")))
244 (false-if-EEXIST (symlink "/dev/fd/1" (scope "dev/stdout")))
245 (false-if-EEXIST (symlink "/dev/fd/2" (scope "dev/stderr"))))
248 (define* (boot-hurd-system #:key (on-error 'debug))
249 "This procedure is meant to be called from an early RC script.
251 Install the relevant passive translators on the first boot. Then, run system
252 activation by using the kernel command-line options '--system' and '--load';
253 starting the Shepherd.
255 XXX TODO: see linux-boot.scm:boot-system.
256 XXX TODO: add proper file-system checking, mounting
257 XXX TODO: move bits to (new?) (hurd?) (activation?) services
258 XXX TODO: use Linux xattr/setxattr to remove (settrans in) /libexec/RUNSYSTEM
262 (display "Welcome, this is GNU's early boot Guile.\n")
263 (display "Use '--repl' for an initrd REPL.\n\n")
265 (call-with-error-handling
268 (let* ((args (command-line))
269 (system (find-long-option "--system" args))
270 (to-load (find-long-option "--load" args)))
272 (format #t "Setting-up essential translators...\n")
273 (setenv "PATH" (string-append system "/profile/bin"))
274 (set-hurd-device-translators)
276 (false-if-exception (delete-file "/hurd"))
277 (let ((hurd/hurd (readlink* (string-append system "/profile/hurd"))))
278 (symlink hurd/hurd "/hurd"))
280 (format #t "Starting pager...\n")
281 (unless (zero? (system* "/hurd/mach-defpager"))
282 (format #t "FAILED...Good luck!\n"))
284 (cond ((member "--repl" args)
285 (format #t "Starting repl...\n")
288 (format #t "loading '~a'...\n" to-load)
289 (primitive-load to-load)
290 (format (current-error-port)
291 "boot program '~a' terminated, rebooting~%"
296 (display "no boot file passed via '--load'\n")
297 (display "entering a warm and cozy REPL\n")
299 #:on-error on-error))
301 ;;; hurd-boot.scm ends here