hurd-boot.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020, 2021 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. (if (string-contains %host-type "linux-gnu")
  117. (passive-translator-xattr? file-name)
  118. (passive-translator-installed? file-name)))
  119. (define* (set-translator file-name command #:optional (mode #o600))
  120. "Setup translator COMMAND on FILE-NAME."
  121. (unless (translated? file-name)
  122. (let ((dir (dirname file-name)))
  123. (unless (directory-exists? dir)
  124. (mkdir-p dir))
  125. (unless (file-exists? file-name)
  126. (call-with-output-file file-name
  127. (lambda (port)
  128. (display file-name port) ;avoid hard-linking
  129. (chmod port mode)))))
  130. (catch 'system-error
  131. (lambda _
  132. (setxattr file-name "gnu.translator" (string-join command "\0" 'suffix)))
  133. (lambda (key . args)
  134. (let ((errno (system-error-errno (cons key args))))
  135. (format (current-error-port) "~a: ~a\n"
  136. (strerror errno) file-name)
  137. (format (current-error-port) "Ignoring...Good Luck!\n"))))))
  138. (define-syntax-rule (false-if-EEXIST exp)
  139. "Evaluate EXP but return #f if it raises to 'system-error with EEXIST."
  140. (catch 'system-error
  141. (lambda () exp)
  142. (lambda args
  143. (if (= EEXIST (system-error-errno args))
  144. #f
  145. (apply throw args)))))
  146. (define* (set-hurd-device-translators #:optional (root "/"))
  147. "Make some of the device nodes needed on GNU/Hurd."
  148. (define (scope dir)
  149. (string-append root (if (string-suffix? "/" root) "" "/") dir))
  150. (define scope-set-translator
  151. (match-lambda
  152. ((file-name command)
  153. (scope-set-translator (list file-name command #o600)))
  154. ((file-name command mode)
  155. (let ((mount-point (scope file-name)))
  156. (set-translator mount-point command mode)))))
  157. (define (mkdir* dir)
  158. (let ((dir (scope dir)))
  159. (unless (file-exists? dir)
  160. (mkdir-p dir))))
  161. (define servers
  162. '(("servers/crash-dump-core" ("/hurd/crash" "--dump-core"))
  163. ("servers/crash-kill" ("/hurd/crash" "--kill"))
  164. ("servers/crash-suspend" ("/hurd/crash" "--suspend"))
  165. ("servers/password" ("/hurd/password"))
  166. ("servers/socket/1" ("/hurd/pflocal"))
  167. ;; /servers/socket/2 and /26 are created by 'static-networking-service'.
  168. ;; XXX: Spawn pfinet without arguments on these nodes so that a DHCP
  169. ;; client has someone to talk to?
  170. ("proc" ("/hurd/procfs" "--stat-mode=444"))))
  171. (define devices
  172. '(("dev/full" ("/hurd/null" "--full") #o666)
  173. ("dev/null" ("/hurd/null") #o666)
  174. ("dev/random" ("/hurd/random" "--seed-file" "/var/lib/random-seed")
  175. #o644)
  176. ("dev/zero" ("/hurd/storeio" "--store-type=zero") #o666)
  177. ("dev/console" ("/hurd/term" "/dev/console" "device" "console"))
  178. ("dev/klog" ("/hurd/streamio" "kmsg"))
  179. ("dev/mem" ("/hurd/storeio" "--no-cache" "mem") #o660)
  180. ("dev/shm" ("/hurd/tmpfs" "--mode=1777" "50%") #o644)
  181. ("dev/time" ("/hurd/storeio" "--no-cache" "time") #o644)
  182. ("dev/vcs" ("/hurd/console"))
  183. ("dev/tty" ("/hurd/magic" "tty") #o666)
  184. ;; 'fd_to_filename' in libc expects it.
  185. ("dev/fd" ("/hurd/magic" "--directory" "fd") #o555)
  186. ("dev/tty1" ("/hurd/term" "/dev/tty1" "hurdio" "/dev/vcs/1/console")
  187. #o666)
  188. ("dev/tty2" ("/hurd/term" "/dev/tty2" "hurdio" "/dev/vcs/2/console")
  189. #o666)
  190. ("dev/tty3" ("/hurd/term" "/dev/tty3" "hurdio" "/dev/vcs/3/console")
  191. #o666)
  192. ("dev/ptyp0" ("/hurd/term" "/dev/ptyp0" "pty-master" "/dev/ttyp0")
  193. #o666)
  194. ("dev/ptyp1" ("/hurd/term" "/dev/ptyp1" "pty-master" "/dev/ttyp1")
  195. #o666)
  196. ("dev/ptyp2" ("/hurd/term" "/dev/ptyp2" "pty-master" "/dev/ttyp2")
  197. #o666)
  198. ("dev/ttyp0" ("/hurd/term" "/dev/ttyp0" "pty-slave" "/dev/ptyp0")
  199. #o666)
  200. ("dev/ttyp1" ("/hurd/term" "/dev/ttyp1" "pty-slave" "/dev/ptyp1")
  201. #o666)
  202. ("dev/ttyp2" ("/hurd/term" "/dev/ttyp2" "pty-slave" "/dev/ptyp2")
  203. #o666)))
  204. (for-each scope-set-translator servers)
  205. (mkdir* "dev/vcs/1")
  206. (mkdir* "dev/vcs/2")
  207. (mkdir* "dev/vcs/2")
  208. (rename-file (scope "dev/console") (scope "dev/console-"))
  209. (for-each scope-set-translator devices)
  210. (false-if-EEXIST (symlink "/dev/random" (scope "dev/urandom")))
  211. (false-if-EEXIST (symlink "/dev/fd/0" (scope "dev/stdin")))
  212. (false-if-EEXIST (symlink "/dev/fd/1" (scope "dev/stdout")))
  213. (false-if-EEXIST (symlink "/dev/fd/2" (scope "dev/stderr")))
  214. (false-if-EEXIST (symlink "crash-dump-core" (scope "servers/crash")))
  215. ;; Make sure /etc/mtab is a symlink to /proc/mounts.
  216. (false-if-exception (delete-file (scope "etc/mtab")))
  217. (mkdir* (scope "etc"))
  218. (symlink "/proc/mounts" (scope "etc/mtab")))
  219. (define* (boot-hurd-system #:key (on-error 'debug))
  220. "This procedure is meant to be called from an early RC script.
  221. Install the relevant passive translators on the first boot. Then, run system
  222. activation by using the kernel command-line options '--system' and '--load';
  223. starting the Shepherd.
  224. XXX TODO: see linux-boot.scm:boot-system.
  225. XXX TODO: add proper file-system checking, mounting
  226. XXX TODO: move bits to (new?) (hurd?) (activation?) services
  227. XXX TODO: use Linux xattr/setxattr to remove (settrans in) /libexec/RUNSYSTEM
  228. "
  229. (display "Welcome, this is GNU's early boot Guile.\n")
  230. (display "Use '--repl' for an initrd REPL.\n\n")
  231. (call-with-error-handling
  232. (lambda ()
  233. (let* ((args (command-line))
  234. (system (find-long-option "--system" args))
  235. (to-load (find-long-option "--load" args)))
  236. (format #t "Setting-up essential translators...\n")
  237. (setenv "PATH" (string-append system "/profile/bin"))
  238. (set-hurd-device-translators)
  239. (false-if-exception (delete-file "/hurd"))
  240. (let ((hurd/hurd (readlink* (string-append system "/profile/hurd"))))
  241. (symlink hurd/hurd "/hurd"))
  242. (format #t "Starting pager...\n")
  243. (unless (zero? (system* "/hurd/mach-defpager"))
  244. (format #t "FAILED...Good luck!\n"))
  245. (cond ((member "--repl" args)
  246. (format #t "Starting repl...\n")
  247. (start-repl))
  248. (to-load
  249. (format #t "loading '~a'...\n" to-load)
  250. (primitive-load to-load)
  251. (format (current-error-port)
  252. "boot program '~a' terminated, rebooting~%"
  253. to-load)
  254. (sleep 2)
  255. (reboot))
  256. (else
  257. (display "no boot file passed via '--load'\n")
  258. (display "entering a warm and cozy REPL\n")
  259. (start-repl)))))
  260. #:on-error on-error))
  261. ;;; hurd-boot.scm ends here