hurd-boot.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2020-2022 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2020, 2023 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. (mkdir-p (scope "servers/bus/pci")))
  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/bus/pci" ("/hurd/pci-arbiter"))
  166. ("servers/crash-dump-core" ("/hurd/crash" "--dump-core"))
  167. ("servers/crash-kill" ("/hurd/crash" "--kill"))
  168. ("servers/crash-suspend" ("/hurd/crash" "--suspend"))
  169. ("servers/password" ("/hurd/password"))
  170. ("servers/socket/1" ("/hurd/pflocal"))
  171. ;; /servers/socket/2 and /26 are created by 'static-networking-service'.
  172. ;; XXX: Spawn pfinet without arguments on these nodes so that a DHCP
  173. ;; client has someone to talk to?
  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/rumpdisk" ("/hurd/rumpdisk") #o660)
  191. ("dev/netdde" ("/hurd/netdde") #o660)
  192. ("dev/eth0" ("/hurd/devnode" "--master-device=/dev/net"
  193. "eth0")
  194. #o660)
  195. ("dev/eth1" ("/hurd/devnode" "--master-device=/dev/net"
  196. "eth1")
  197. #o660)
  198. ;; Create a number of ttys; syslogd writes to tty12 by default.
  199. ;; FIXME: Creating /dev/tty12 leads the console client to switch to
  200. ;; tty12 when syslogd starts, which is confusing for users. Thus, do
  201. ;; not create tty12.
  202. ,@(map (lambda (n)
  203. (let ((n (number->string n)))
  204. `(,(string-append "dev/tty" n)
  205. ("/hurd/term" ,(string-append "/dev/tty" n)
  206. "hurdio" ,(string-append "/dev/vcs/" n "/console"))
  207. #o666)))
  208. (iota 11 1))
  209. ,@(append-map (lambda (n)
  210. (let ((n (number->string n)))
  211. `((,(string-append "dev/ptyp" n)
  212. ("/hurd/term" ,(string-append "/dev/ptyp" n)
  213. "pty-master" ,(string-append "/dev/ttyp" n))
  214. #o666)
  215. (,(string-append "dev/ttyp" n)
  216. ("/hurd/term" ,(string-append "/dev/ttyp" n)
  217. "pty-slave" ,(string-append "/dev/ptyp" n))
  218. #o666))))
  219. (iota 10 0))
  220. ,@(append-map (lambda (n)
  221. (let* ((n (number->string n))
  222. (drive (string-append "dev/wd" n))
  223. (disk (string-append "@/dev/disk:wd" n)))
  224. `((,drive ("/hurd/storeio" ,disk) #o600)
  225. ,@(map (lambda (p)
  226. (let ((p (number->string p)))
  227. `(,(string-append drive "s" p)
  228. ("/hurd/storeio"
  229. "--store-type=typed"
  230. ,(string-append
  231. "part:" p ":device:" disk))
  232. #o660)))
  233. (iota 4 1)))))
  234. (iota 4 0))))
  235. (for-each scope-set-translator servers)
  236. (mkdir* "dev/vcs/1")
  237. (mkdir* "dev/vcs/2")
  238. (rename-file (scope "dev/console") (scope "dev/console-"))
  239. (for-each scope-set-translator devices)
  240. (false-if-EEXIST (symlink "/dev/random" (scope "dev/urandom")))
  241. (false-if-EEXIST (symlink "/dev/fd/0" (scope "dev/stdin")))
  242. (false-if-EEXIST (symlink "/dev/fd/1" (scope "dev/stdout")))
  243. (false-if-EEXIST (symlink "/dev/fd/2" (scope "dev/stderr")))
  244. (false-if-EEXIST (symlink "crash-dump-core" (scope "servers/crash")))
  245. (false-if-EEXIST (symlink "/dev/rumpdisk" (scope "dev/disk")))
  246. (false-if-EEXIST (symlink "/dev/netdde" (scope "dev/net")))
  247. (false-if-EEXIST (symlink "/servers/socket/2" (scope "servers/socket/inet")))
  248. (false-if-EEXIST (symlink "/servers/socket/26" (scope "servers/socket/inet6")))
  249. ;; Make sure /etc/mtab is a symlink to /proc/mounts.
  250. (false-if-exception (delete-file (scope "etc/mtab")))
  251. (mkdir* (scope "etc"))
  252. (symlink "/proc/mounts" (scope "etc/mtab")))
  253. (define* (boot-hurd-system #:key (on-error 'debug))
  254. "This procedure is meant to be called from an early RC script.
  255. Install the relevant passive translators on the first boot. Then, run system
  256. activation by using the kernel command-line options 'gnu.system' and 'gnu.load';
  257. starting the Shepherd.
  258. XXX TODO: see linux-boot.scm:boot-system.
  259. XXX TODO: add proper file-system checking, mounting
  260. XXX TODO: move bits to (new?) (hurd?) (activation?) services
  261. XXX TODO: use Linux xattr/setxattr to remove (settrans in) /libexec/RUNSYSTEM
  262. "
  263. (display "Welcome, this is GNU's early boot Guile.\n")
  264. (display "Use 'gnu.repl' for an initrd REPL.\n\n")
  265. (call-with-error-handling
  266. (lambda ()
  267. (let* ((args (command-line))
  268. (system (find-long-option "gnu.system" args))
  269. (to-load (find-long-option "gnu.load" args)))
  270. (false-if-exception (delete-file "/hurd"))
  271. (let ((hurd/hurd (readlink* (string-append system "/profile/hurd"))))
  272. (symlink hurd/hurd "/hurd"))
  273. (format #t "Setting-up essential translators...\n")
  274. (setenv "PATH" (string-append system "/profile/bin"))
  275. (set-hurd-device-translators)
  276. (format #t "Starting pager...\n")
  277. (unless (zero? (system* "/hurd/mach-defpager"))
  278. (format #t "FAILED...Good luck!\n"))
  279. (cond ((member "gnu.repl" args)
  280. (format #t "Starting repl...\n")
  281. (start-repl))
  282. (to-load
  283. (format #t "loading '~a'...\n" to-load)
  284. (primitive-load to-load)
  285. (format (current-error-port)
  286. "boot program '~a' terminated, rebooting~%"
  287. to-load)
  288. (sleep 2)
  289. (reboot))
  290. (else
  291. (display "no boot file passed via 'gnu.load'\n")
  292. (display "entering a warm and cozy REPL\n")
  293. (start-repl)))))
  294. #:on-error on-error))
  295. ;;; hurd-boot.scm ends here