hurd-boot.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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>
  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" ("/hurd/pfinet"
  168. "--interface" "eth0"
  169. "--address"
  170. "10.0.2.15" ;the default QEMU guest IP
  171. "--netmask" "255.255.255.0"
  172. "--gateway" "10.0.2.2"
  173. "--ipv6" "/servers/socket/16"))
  174. ("proc" ("/hurd/procfs" "--stat-mode=444"))))
  175. (define devices
  176. '(("dev/full" ("/hurd/null" "--full") #o666)
  177. ("dev/null" ("/hurd/null") #o666)
  178. ("dev/random" ("/hurd/random" "--seed-file" "/var/lib/random-seed")
  179. #o644)
  180. ("dev/zero" ("/hurd/storeio" "--store-type=zero") #o666)
  181. ("dev/console" ("/hurd/term" "/dev/console" "device" "console"))
  182. ("dev/klog" ("/hurd/streamio" "kmsg"))
  183. ("dev/mem" ("/hurd/storeio" "--no-cache" "mem") #o660)
  184. ("dev/shm" ("/hurd/tmpfs" "--mode=1777" "50%") #o644)
  185. ("dev/time" ("/hurd/storeio" "--no-cache" "time") #o644)
  186. ("dev/vcs" ("/hurd/console"))
  187. ("dev/tty" ("/hurd/magic" "tty") #o666)
  188. ;; 'fd_to_filename' in libc expects it.
  189. ("dev/fd" ("/hurd/magic" "--directory" "fd") #o555)
  190. ("dev/tty1" ("/hurd/term" "/dev/tty1" "hurdio" "/dev/vcs/1/console")
  191. #o666)
  192. ("dev/tty2" ("/hurd/term" "/dev/tty2" "hurdio" "/dev/vcs/2/console")
  193. #o666)
  194. ("dev/tty3" ("/hurd/term" "/dev/tty3" "hurdio" "/dev/vcs/3/console")
  195. #o666)
  196. ("dev/ptyp0" ("/hurd/term" "/dev/ptyp0" "pty-master" "/dev/ttyp0")
  197. #o666)
  198. ("dev/ptyp1" ("/hurd/term" "/dev/ptyp1" "pty-master" "/dev/ttyp1")
  199. #o666)
  200. ("dev/ptyp2" ("/hurd/term" "/dev/ptyp2" "pty-master" "/dev/ttyp2")
  201. #o666)
  202. ("dev/ttyp0" ("/hurd/term" "/dev/ttyp0" "pty-slave" "/dev/ptyp0")
  203. #o666)
  204. ("dev/ttyp1" ("/hurd/term" "/dev/ttyp1" "pty-slave" "/dev/ptyp1")
  205. #o666)
  206. ("dev/ttyp2" ("/hurd/term" "/dev/ttyp2" "pty-slave" "/dev/ptyp2")
  207. #o666)))
  208. (for-each scope-set-translator servers)
  209. (mkdir* "dev/vcs/1")
  210. (mkdir* "dev/vcs/2")
  211. (mkdir* "dev/vcs/2")
  212. (rename-file (scope "dev/console") (scope "dev/console-"))
  213. (for-each scope-set-translator devices)
  214. (false-if-EEXIST (symlink "/dev/random" (scope "dev/urandom")))
  215. (false-if-EEXIST (symlink "/dev/fd/0" (scope "dev/stdin")))
  216. (false-if-EEXIST (symlink "/dev/fd/1" (scope "dev/stdout")))
  217. (false-if-EEXIST (symlink "/dev/fd/2" (scope "dev/stderr")))
  218. (false-if-EEXIST (symlink "crash-dump-core" (scope "servers/crash")))
  219. ;; Make sure /etc/mtab is a symlink to /proc/mounts.
  220. (false-if-exception (delete-file (scope "etc/mtab")))
  221. (mkdir* (scope "etc"))
  222. (symlink "/proc/mounts" (scope "etc/mtab")))
  223. (define* (boot-hurd-system #:key (on-error 'debug))
  224. "This procedure is meant to be called from an early RC script.
  225. Install the relevant passive translators on the first boot. Then, run system
  226. activation by using the kernel command-line options '--system' and '--load';
  227. starting the Shepherd.
  228. XXX TODO: see linux-boot.scm:boot-system.
  229. XXX TODO: add proper file-system checking, mounting
  230. XXX TODO: move bits to (new?) (hurd?) (activation?) services
  231. XXX TODO: use Linux xattr/setxattr to remove (settrans in) /libexec/RUNSYSTEM
  232. "
  233. (display "Welcome, this is GNU's early boot Guile.\n")
  234. (display "Use '--repl' for an initrd REPL.\n\n")
  235. (call-with-error-handling
  236. (lambda ()
  237. (let* ((args (command-line))
  238. (system (find-long-option "--system" args))
  239. (to-load (find-long-option "--load" args)))
  240. (format #t "Setting-up essential translators...\n")
  241. (setenv "PATH" (string-append system "/profile/bin"))
  242. (set-hurd-device-translators)
  243. (false-if-exception (delete-file "/hurd"))
  244. (let ((hurd/hurd (readlink* (string-append system "/profile/hurd"))))
  245. (symlink hurd/hurd "/hurd"))
  246. (format #t "Starting pager...\n")
  247. (unless (zero? (system* "/hurd/mach-defpager"))
  248. (format #t "FAILED...Good luck!\n"))
  249. (cond ((member "--repl" args)
  250. (format #t "Starting repl...\n")
  251. (start-repl))
  252. (to-load
  253. (format #t "loading '~a'...\n" to-load)
  254. (primitive-load to-load)
  255. (format (current-error-port)
  256. "boot program '~a' terminated, rebooting~%"
  257. to-load)
  258. (sleep 2)
  259. (reboot))
  260. (else
  261. (display "no boot file passed via '--load'\n")
  262. (display "entering a warm and cozy REPL\n")
  263. (start-repl)))))
  264. #:on-error on-error))
  265. ;;; hurd-boot.scm ends here