hurd-boot.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020-2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  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.
  11. ;;;
  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.
  16. ;;;
  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/>.
  19. (define-module (gnu build hurd-boot)
  20. #:use-module (system repl error-handling)
  21. #:autoload (system repl repl) (start-repl)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-26)
  24. #:use-module (ice-9 match)
  25. #:use-module (guix build utils)
  26. #:use-module ((guix build syscalls)
  27. #:hide (file-system-type))
  28. #:export (make-hurd-device-nodes
  29. boot-hurd-system))
  30. ;;; Commentary:
  31. ;;;
  32. ;;; Utility procedures useful to boot a Hurd system.
  33. ;;;
  34. ;;; Code:
  35. ;; XXX FIXME c&p from linux-boot.scm
  36. (define (find-long-option option arguments)
  37. "Find OPTION among ARGUMENTS, where OPTION is something like \"--load\".
  38. Return the value associated with OPTION, or #f on failure."
  39. (let ((opt (string-append option "=")))
  40. (and=> (find (cut string-prefix? opt <>)
  41. arguments)
  42. (lambda (arg)
  43. (substring arg (+ 1 (string-index arg #\=)))))))
  44. ;; XXX FIXME c&p from guix/utils.scm
  45. (define (readlink* file)
  46. "Call 'readlink' until the result is not a symlink."
  47. (define %max-symlink-depth 50)
  48. (let loop ((file file)
  49. (depth 0))
  50. (define (absolute target)
  51. (if (absolute-file-name? target)
  52. target
  53. (string-append (dirname file) "/" target)))
  54. (if (>= depth %max-symlink-depth)
  55. file
  56. (call-with-values
  57. (lambda ()
  58. (catch 'system-error
  59. (lambda ()
  60. (values #t (readlink file)))
  61. (lambda args
  62. (let ((errno (system-error-errno args)))
  63. (if (or (= errno EINVAL))
  64. (values #f file)
  65. (apply throw args))))))
  66. (lambda (success? target)
  67. (if success?
  68. (loop (absolute target) (+ depth 1))
  69. file))))))
  70. (define* (make-hurd-device-nodes #:optional (root "/"))
  71. "Make some of the nodes needed on GNU/Hurd."
  72. (define (scope dir)
  73. (string-append root (if (string-suffix? "/" root) "" "/") dir))
  74. (mkdir (scope "dev"))
  75. ;; Don't create /dev/null etc just yet; the store
  76. ;; messes-up the permission bits.
  77. ;; Don't create /dev/console, /dev/vcs, etc.: they are created by
  78. ;; console-run on first boot.
  79. (mkdir (scope "servers"))
  80. (for-each (lambda (file)
  81. (call-with-output-file (scope (string-append "servers/" file))
  82. (lambda (port)
  83. (display file port) ;avoid hard-linking
  84. (chmod port #o444))))
  85. '("startup"
  86. "exec"
  87. "proc"
  88. "password"
  89. "default-pager"
  90. "crash-dump-core"
  91. "kill"
  92. "suspend"))
  93. (mkdir (scope "servers/socket"))
  94. ;; Don't create /servers/socket/1 & co: runsystem does that on first boot.
  95. ;; TODO: Set the 'gnu.translator' extended attribute for passive translator
  96. ;; settings?
  97. )
  98. (define (passive-translator-xattr? file-name)
  99. "Return true if FILE-NAME has an extended @code{gnu.translator} attribute
  100. set."
  101. (catch 'system-error
  102. (lambda _ (not (string-null? (getxattr file-name "gnu.translator"))))
  103. (lambda args
  104. (if (= ENODATA (system-error-errno args))
  105. #f
  106. (apply throw args)))))
  107. (define (passive-translator-installed? file-name)
  108. "Return true if @file{showtrans} finds a translator installed on FILE-NAME."
  109. (with-output-to-port (%make-void-port "w")
  110. (lambda _
  111. (with-error-to-port (%make-void-port "w")
  112. (lambda _
  113. (zero? (system* "showtrans" "--silent" file-name)))))))
  114. (define (translated? file-name)
  115. "Return true if a translator is installed on FILE-NAME."
  116. ;; On GNU/Hurd, 'getxattr' in glibc opens the file without O_NOTRANS, and
  117. ;; then, for "gnu.translator", it calls 'file_get_translator', resulting in
  118. ;; EOPNOTSUPP (conversely, 'showtrans' opens the file with O_NOTRANS).
  119. (if (string-contains %host-type "linux-gnu")
  120. (passive-translator-xattr? file-name)
  121. (passive-translator-installed? file-name)))
  122. (define* (set-translator file-name command #:optional (mode #o600))
  123. "Setup translator COMMAND on FILE-NAME."
  124. (unless (translated? file-name)
  125. (let ((dir (dirname file-name)))
  126. (unless (directory-exists? dir)
  127. (mkdir-p dir))
  128. (unless (file-exists? file-name)
  129. (call-with-output-file file-name
  130. (lambda (port)
  131. (display file-name port) ;avoid hard-linking
  132. (chmod port mode)))))
  133. (catch 'system-error
  134. (lambda _
  135. (setxattr file-name "gnu.translator" (string-join command "\0" 'suffix)))
  136. (lambda (key . args)
  137. (let ((errno (system-error-errno (cons key args))))
  138. (format (current-error-port) "~a: ~a\n"
  139. (strerror errno) file-name)
  140. (format (current-error-port) "Ignoring...Good Luck!\n"))))))
  141. (define-syntax-rule (false-if-EEXIST exp)
  142. "Evaluate EXP but return #f if it raises to 'system-error with EEXIST."
  143. (catch 'system-error
  144. (lambda () exp)
  145. (lambda args
  146. (if (= EEXIST (system-error-errno args))
  147. #f
  148. (apply throw args)))))
  149. (define* (set-hurd-device-translators #:optional (root "/"))
  150. "Make some of the device nodes needed on GNU/Hurd."
  151. (define (scope dir)
  152. (string-append root (if (string-suffix? "/" root) "" "/") dir))
  153. (define scope-set-translator
  154. (match-lambda
  155. ((file-name command)
  156. (scope-set-translator (list file-name command #o600)))
  157. ((file-name command mode)
  158. (let ((mount-point (scope file-name)))
  159. (set-translator mount-point command mode)))))
  160. (define (mkdir* dir)
  161. (let ((dir (scope dir)))
  162. (unless (file-exists? dir)
  163. (mkdir-p dir))))
  164. (define servers
  165. '(("servers/crash-dump-core" ("/hurd/crash" "--dump-core"))
  166. ("servers/crash-kill" ("/hurd/crash" "--kill"))
  167. ("servers/crash-suspend" ("/hurd/crash" "--suspend"))
  168. ("servers/password" ("/hurd/password"))
  169. ("servers/socket/1" ("/hurd/pflocal"))
  170. ;; /servers/socket/2 and /26 are created by 'static-networking-service'.
  171. ;; XXX: Spawn pfinet without arguments on these nodes so that a DHCP
  172. ;; client has someone to talk to?
  173. ("proc" ("/hurd/procfs" "--stat-mode=444"))))
  174. (define devices
  175. `(("dev/full" ("/hurd/null" "--full") #o666)
  176. ("dev/null" ("/hurd/null") #o666)
  177. ("dev/random" ("/hurd/random" "--seed-file" "/var/lib/random-seed")
  178. #o644)
  179. ("dev/zero" ("/hurd/storeio" "--store-type=zero") #o666)
  180. ("dev/console" ("/hurd/term" "/dev/console" "device" "console"))
  181. ("dev/klog" ("/hurd/streamio" "kmsg"))
  182. ("dev/mem" ("/hurd/storeio" "--no-cache" "mem") #o660)
  183. ("dev/shm" ("/hurd/tmpfs" "--mode=1777" "50%") #o644)
  184. ("dev/time" ("/hurd/storeio" "--no-cache" "time") #o644)
  185. ("dev/vcs" ("/hurd/console"))
  186. ("dev/tty" ("/hurd/magic" "tty") #o666)
  187. ;; 'fd_to_filename' in libc expects it.
  188. ("dev/fd" ("/hurd/magic" "--directory" "fd") #o555)
  189. ;; Create a number of ttys; syslogd writes to tty12 by default.
  190. ;; FIXME: Creating /dev/tty12 leads the console client to switch to
  191. ;; tty12 when syslogd starts, which is confusing for users. Thus, do
  192. ;; not create tty12.
  193. ,@(map (lambda (n)
  194. (let ((n (number->string n)))
  195. `(,(string-append "dev/tty" n)
  196. ("/hurd/term" ,(string-append "/dev/tty" n)
  197. "hurdio" ,(string-append "/dev/vcs/" n "/console"))
  198. #o666)))
  199. (iota 11 1))
  200. ,@(append-map (lambda (n)
  201. (let ((n (number->string n)))
  202. `((,(string-append "dev/ptyp" n)
  203. ("/hurd/term" ,(string-append "/dev/ptyp" n)
  204. "pty-master" ,(string-append "/dev/ttyp" n))
  205. #o666)
  206. (,(string-append "dev/ttyp" n)
  207. ("/hurd/term" ,(string-append "/dev/ttyp" n)
  208. "pty-slave" ,(string-append "/dev/ptyp" n))
  209. #o666))))
  210. (iota 10 0))))
  211. (for-each scope-set-translator servers)
  212. (mkdir* "dev/vcs/1")
  213. (mkdir* "dev/vcs/2")
  214. (rename-file (scope "dev/console") (scope "dev/console-"))
  215. (for-each scope-set-translator devices)
  216. (false-if-EEXIST (symlink "/dev/random" (scope "dev/urandom")))
  217. (false-if-EEXIST (symlink "/dev/fd/0" (scope "dev/stdin")))
  218. (false-if-EEXIST (symlink "/dev/fd/1" (scope "dev/stdout")))
  219. (false-if-EEXIST (symlink "/dev/fd/2" (scope "dev/stderr")))
  220. (false-if-EEXIST (symlink "crash-dump-core" (scope "servers/crash")))
  221. ;; Make sure /etc/mtab is a symlink to /proc/mounts.
  222. (false-if-exception (delete-file (scope "etc/mtab")))
  223. (mkdir* (scope "etc"))
  224. (symlink "/proc/mounts" (scope "etc/mtab")))
  225. (define* (boot-hurd-system #:key (on-error 'debug))
  226. "This procedure is meant to be called from an early RC script.
  227. Install the relevant passive translators on the first boot. Then, run system
  228. activation by using the kernel command-line options 'gnu.system' and 'gnu.load';
  229. starting the Shepherd.
  230. XXX TODO: see linux-boot.scm:boot-system.
  231. XXX TODO: add proper file-system checking, mounting
  232. XXX TODO: move bits to (new?) (hurd?) (activation?) services
  233. XXX TODO: use Linux xattr/setxattr to remove (settrans in) /libexec/RUNSYSTEM
  234. "
  235. (display "Welcome, this is GNU's early boot Guile.\n")
  236. (display "Use 'gnu.repl' for an initrd REPL.\n\n")
  237. (call-with-error-handling
  238. (lambda ()
  239. (let* ((args (command-line))
  240. (system (find-long-option "gnu.system" args))
  241. (to-load (find-long-option "gnu.load" args)))
  242. (false-if-exception (delete-file "/hurd"))
  243. (let ((hurd/hurd (readlink* (string-append system "/profile/hurd"))))
  244. (symlink hurd/hurd "/hurd"))
  245. (format #t "Setting-up essential translators...\n")
  246. (setenv "PATH" (string-append system "/profile/bin"))
  247. (set-hurd-device-translators)
  248. (format #t "Starting pager...\n")
  249. (unless (zero? (system* "/hurd/mach-defpager"))
  250. (format #t "FAILED...Good luck!\n"))
  251. (cond ((member "gnu.repl" args)
  252. (format #t "Starting repl...\n")
  253. (start-repl))
  254. (to-load
  255. (format #t "loading '~a'...\n" to-load)
  256. (primitive-load to-load)
  257. (format (current-error-port)
  258. "boot program '~a' terminated, rebooting~%"
  259. to-load)
  260. (sleep 2)
  261. (reboot))
  262. (else
  263. (display "no boot file passed via 'gnu.load'\n")
  264. (display "entering a warm and cozy REPL\n")
  265. (start-repl)))))
  266. #:on-error on-error))
  267. ;;; hurd-boot.scm ends here