linux-boot.scm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
  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 linux-boot)
  20. #:use-module (rnrs io ports)
  21. #:use-module (system repl error-handling)
  22. #:autoload (system repl repl) (start-repl)
  23. #:autoload (system base compile) (compile-file)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-26)
  26. #:use-module (ice-9 match)
  27. #:use-module (ice-9 ftw)
  28. #:use-module (guix build utils)
  29. #:use-module (guix build syscalls)
  30. #:use-module (gnu build linux-modules)
  31. #:use-module (gnu build file-systems)
  32. #:export (mount-essential-file-systems
  33. linux-command-line
  34. find-long-option
  35. make-essential-device-nodes
  36. configure-qemu-networking
  37. bind-mount
  38. device-number
  39. boot-system))
  40. ;;; Commentary:
  41. ;;;
  42. ;;; Utility procedures useful in a Linux initial RAM disk (initrd). Note that
  43. ;;; many of these use procedures not yet available in vanilla Guile (`mount',
  44. ;;; `load-linux-module', etc.); these are provided by a Guile patch used in
  45. ;;; the GNU distribution.
  46. ;;;
  47. ;;; Code:
  48. (define* (mount-essential-file-systems #:key (root "/"))
  49. "Mount /dev, /proc, and /sys under ROOT."
  50. (define (scope dir)
  51. (string-append root
  52. (if (string-suffix? "/" root)
  53. ""
  54. "/")
  55. dir))
  56. (unless (file-exists? (scope "proc"))
  57. (mkdir (scope "proc")))
  58. (mount "none" (scope "proc") "proc")
  59. (unless (file-exists? (scope "dev"))
  60. (mkdir (scope "dev")))
  61. (mount "none" (scope "dev") "devtmpfs")
  62. (unless (file-exists? (scope "sys"))
  63. (mkdir (scope "sys")))
  64. (mount "none" (scope "sys") "sysfs"))
  65. (define (move-essential-file-systems root)
  66. "Move currently mounted essential file systems to ROOT."
  67. (for-each (lambda (dir)
  68. (let ((target (string-append root dir)))
  69. (unless (file-exists? target)
  70. (mkdir target))
  71. (mount dir target "" MS_MOVE)))
  72. '("/dev" "/proc" "/sys")))
  73. (define (linux-command-line)
  74. "Return the Linux kernel command line as a list of strings."
  75. (string-tokenize
  76. (call-with-input-file "/proc/cmdline"
  77. get-string-all)))
  78. (define (find-long-option option arguments)
  79. "Find OPTION among ARGUMENTS, where OPTION is something like \"--load\".
  80. Return the value associated with OPTION, or #f on failure."
  81. (let ((opt (string-append option "=")))
  82. (and=> (find (cut string-prefix? opt <>)
  83. arguments)
  84. (lambda (arg)
  85. (substring arg (+ 1 (string-index arg #\=)))))))
  86. (define* (make-disk-device-nodes base major #:optional (minor 0))
  87. "Make the block device nodes around BASE (something like \"/root/dev/sda\")
  88. with the given MAJOR number, starting with MINOR."
  89. (mknod base 'block-special #o644 (device-number major minor))
  90. (let loop ((i 1))
  91. (when (< i 16)
  92. (mknod (string-append base (number->string i))
  93. 'block-special #o644 (device-number major (+ minor i)))
  94. (loop (+ i 1)))))
  95. (define* (make-essential-device-nodes #:key (root "/"))
  96. "Make essential device nodes under ROOT/dev."
  97. ;; The hand-made devtmpfs/udev!
  98. (define (scope dir)
  99. (string-append root
  100. (if (string-suffix? "/" root)
  101. ""
  102. "/")
  103. dir))
  104. (unless (file-exists? (scope "dev"))
  105. (mkdir (scope "dev")))
  106. ;; Make the device nodes for SCSI disks.
  107. (make-disk-device-nodes (scope "dev/sda") 8)
  108. (make-disk-device-nodes (scope "dev/sdb") 8 16)
  109. (make-disk-device-nodes (scope "dev/sdc") 8 32)
  110. (make-disk-device-nodes (scope "dev/sdd") 8 48)
  111. ;; SCSI CD-ROM devices (aka. "/dev/sr0" etc.).
  112. (mknod (scope "dev/scd0") 'block-special #o644 (device-number 11 0))
  113. (mknod (scope "dev/scd1") 'block-special #o644 (device-number 11 1))
  114. ;; The virtio (para-virtualized) block devices, as supported by QEMU/KVM.
  115. (make-disk-device-nodes (scope "dev/vda") 252)
  116. ;; Memory (used by Xorg's VESA driver.)
  117. (mknod (scope "dev/mem") 'char-special #o640 (device-number 1 1))
  118. (mknod (scope "dev/kmem") 'char-special #o640 (device-number 1 2))
  119. ;; Inputs (used by Xorg.)
  120. (unless (file-exists? (scope "dev/input"))
  121. (mkdir (scope "dev/input")))
  122. (mknod (scope "dev/input/mice") 'char-special #o640 (device-number 13 63))
  123. (mknod (scope "dev/input/mouse0") 'char-special #o640 (device-number 13 32))
  124. (mknod (scope "dev/input/event0") 'char-special #o640 (device-number 13 64))
  125. ;; System console. This node is magically created by the kernel on the
  126. ;; initrd's root, so don't try to create it in that case.
  127. (unless (string=? root "/")
  128. (mknod (scope "dev/console") 'char-special #o600
  129. (device-number 5 1)))
  130. ;; TTYs.
  131. (mknod (scope "dev/tty") 'char-special #o600
  132. (device-number 5 0))
  133. (chmod (scope "dev/tty") #o666)
  134. (let loop ((n 0))
  135. (and (< n 50)
  136. (let ((name (format #f "dev/tty~a" n)))
  137. (mknod (scope name) 'char-special #o600
  138. (device-number 4 n))
  139. (loop (+ 1 n)))))
  140. ;; Serial line.
  141. (mknod (scope "dev/ttyS0") 'char-special #o660
  142. (device-number 4 64))
  143. ;; Pseudo ttys.
  144. (mknod (scope "dev/ptmx") 'char-special #o666
  145. (device-number 5 2))
  146. (chmod (scope "dev/ptmx") #o666)
  147. ;; Create /dev/pts; it will be mounted later, at boot time.
  148. (unless (file-exists? (scope "dev/pts"))
  149. (mkdir (scope "dev/pts")))
  150. ;; Rendez-vous point for syslogd.
  151. (mknod (scope "dev/log") 'socket #o666 0)
  152. (mknod (scope "dev/kmsg") 'char-special #o600 (device-number 1 11))
  153. ;; Other useful nodes, notably relied on by guix-daemon.
  154. (for-each (match-lambda
  155. ((file major minor)
  156. (mknod (scope file) 'char-special #o666
  157. (device-number major minor))
  158. (chmod (scope file) #o666)))
  159. '(("dev/null" 1 3)
  160. ("dev/zero" 1 5)
  161. ("dev/full" 1 7)
  162. ("dev/random" 1 8)
  163. ("dev/urandom" 1 9)))
  164. (symlink "/proc/self/fd" (scope "dev/fd"))
  165. (symlink "/proc/self/fd/0" (scope "dev/stdin"))
  166. (symlink "/proc/self/fd/1" (scope "dev/stdout"))
  167. (symlink "/proc/self/fd/2" (scope "dev/stderr"))
  168. ;; Loopback devices.
  169. (let loop ((i 0))
  170. (when (< i 8)
  171. (mknod (scope (string-append "dev/loop" (number->string i)))
  172. 'block-special #o660
  173. (device-number 7 i))
  174. (loop (+ 1 i))))
  175. ;; File systems in user space (FUSE).
  176. (mknod (scope "dev/fuse") 'char-special #o666 (device-number 10 229)))
  177. (define %host-qemu-ipv4-address
  178. (inet-pton AF_INET "10.0.2.10"))
  179. (define* (configure-qemu-networking #:optional (interface "eth0"))
  180. "Setup the INTERFACE network interface and /etc/resolv.conf according to
  181. QEMU's default networking settings (see net/slirp.c in QEMU for default
  182. networking values.) Return #t if INTERFACE is up, #f otherwise."
  183. (display "configuring QEMU networking...\n")
  184. (let* ((sock (socket AF_INET SOCK_STREAM 0))
  185. (address (make-socket-address AF_INET %host-qemu-ipv4-address 0))
  186. (flags (network-interface-flags sock interface)))
  187. (set-network-interface-address sock interface address)
  188. (set-network-interface-flags sock interface (logior flags IFF_UP))
  189. ;; Hello! We used to create /etc/resolv.conf here, with "nameserver
  190. ;; 10.0.2.3\n". However, with Linux-libre 3.16, we're getting ENOSPC.
  191. ;; And since it's actually unnecessary, it's gone.
  192. (logand (network-interface-flags sock interface) IFF_UP)))
  193. (define (device-number major minor)
  194. "Return the device number for the device with MAJOR and MINOR, for use as
  195. the last argument of `mknod'."
  196. (+ (* major 256) minor))
  197. (define (pidof program)
  198. "Return the PID of the first presumed instance of PROGRAM."
  199. (let ((program (basename program)))
  200. (find (lambda (pid)
  201. (let ((exe (format #f "/proc/~a/exe" pid)))
  202. (and=> (false-if-exception (readlink exe))
  203. (compose (cut string=? program <>) basename))))
  204. (filter-map string->number (scandir "/proc")))))
  205. (define* (mount-root-file-system root type
  206. #:key volatile-root? (unionfs "unionfs"))
  207. "Mount the root file system of type TYPE at device ROOT. If VOLATILE-ROOT?
  208. is true, mount ROOT read-only and make it a union with a writable tmpfs using
  209. UNIONFS."
  210. (define (mark-as-not-killable pid)
  211. ;; Tell the 'user-processes' shepherd service that PID must be kept alive
  212. ;; when shutting down.
  213. (mkdir-p "/root/etc/shepherd")
  214. (let ((port (open-file "/root/etc/shepherd/do-not-kill" "a")))
  215. (chmod port #o600)
  216. (write pid port)
  217. (newline port)
  218. (close-port port)))
  219. (if volatile-root?
  220. (begin
  221. (mkdir-p "/real-root")
  222. (mount root "/real-root" type MS_RDONLY)
  223. (mkdir-p "/rw-root")
  224. (mount "none" "/rw-root" "tmpfs")
  225. ;; We want read-write /dev nodes.
  226. (mkdir-p "/rw-root/dev")
  227. (mount "none" "/rw-root/dev" "devtmpfs")
  228. ;; Make /root a union of the tmpfs and the actual root. Use
  229. ;; 'max_files' to set a high RLIMIT_NOFILE for the unionfs process
  230. ;; itself. Failing to do that, we quickly run out of file
  231. ;; descriptors; see <http://bugs.gnu.org/17827>.
  232. (unless (zero? (system* unionfs "-o"
  233. "cow,allow_other,use_ino,suid,dev,max_files=65536"
  234. "/rw-root=RW:/real-root=RO"
  235. "/root"))
  236. (error "unionfs failed"))
  237. ;; Make sure unionfs remains alive till the end. Because
  238. ;; 'fuse_daemonize' doesn't tell the PID of the forked daemon, we
  239. ;; have to resort to 'pidof' here.
  240. (mark-as-not-killable (pidof unionfs)))
  241. (begin
  242. (check-file-system root type)
  243. (mount root "/root" type)))
  244. ;; Make sure /root/etc/mtab is a symlink to /proc/self/mounts.
  245. (false-if-exception
  246. (delete-file "/root/etc/mtab"))
  247. (symlink "/proc/self/mounts" "/root/etc/mtab"))
  248. (define (switch-root root)
  249. "Switch to ROOT as the root file system, in a way similar to what
  250. util-linux' switch_root(8) does."
  251. (move-essential-file-systems root)
  252. (chdir root)
  253. ;; Since we're about to 'rm -rf /', try to make sure we're on an initrd.
  254. ;; TODO: Use 'statfs' to check the fs type, like klibc does.
  255. (when (or (not (file-exists? "/init")) (directory-exists? "/home"))
  256. (format (current-error-port)
  257. "The root file system is probably not an initrd; \
  258. bailing out.~%root contents: ~s~%" (scandir "/"))
  259. (force-output (current-error-port))
  260. (exit 1))
  261. ;; Delete files from the old root, without crossing mount points (assuming
  262. ;; there are no mount points in sub-directories.) That means we're leaving
  263. ;; the empty ROOT directory behind us, but that's OK.
  264. (let ((root-device (stat:dev (stat "/"))))
  265. (for-each (lambda (file)
  266. (unless (member file '("." ".."))
  267. (let* ((file (string-append "/" file))
  268. (device (stat:dev (lstat file))))
  269. (when (= device root-device)
  270. (delete-file-recursively file)))))
  271. (scandir "/")))
  272. ;; Make ROOT the new root.
  273. (mount root "/" "" MS_MOVE)
  274. (chroot ".")
  275. (chdir "/")
  276. (when (file-exists? "/dev/console")
  277. ;; Close the standard file descriptors since they refer to the old
  278. ;; /dev/console, and reopen them.
  279. (let ((console (open-file "/dev/console" "r+b0")))
  280. (for-each close-fdes '(0 1 2))
  281. (dup2 (fileno console) 0)
  282. (dup2 (fileno console) 1)
  283. (dup2 (fileno console) 2)
  284. (close-port console))))
  285. (define* (boot-system #:key
  286. (linux-modules '())
  287. linux-module-directory
  288. qemu-guest-networking?
  289. volatile-root?
  290. pre-mount
  291. (mounts '()))
  292. "This procedure is meant to be called from an initrd. Boot a system by
  293. first loading LINUX-MODULES (a list of module names) from
  294. LINUX-MODULE-DIRECTORY, then setting up QEMU guest networking if
  295. QEMU-GUEST-NETWORKING? is true, calling PRE-MOUNT, mounting the file systems
  296. specified in MOUNTS, and finally booting into the new root if any. The initrd
  297. supports kernel command-line options '--load', '--root', and '--repl'.
  298. Mount the root file system, specified by the '--root' command-line argument,
  299. if any.
  300. MOUNTS must be a list suitable for 'mount-file-system'.
  301. When VOLATILE-ROOT? is true, the root file system is writable but any changes
  302. to it are lost."
  303. (define root-mount-point?
  304. (match-lambda
  305. ((device _ "/" _ ...) #t)
  306. (_ #f)))
  307. (define root-fs-type
  308. (or (any (match-lambda
  309. ((device _ "/" type _ ...) type)
  310. (_ #f))
  311. mounts)
  312. "ext4"))
  313. (define (lookup-module name)
  314. (string-append linux-module-directory "/"
  315. (ensure-dot-ko name)))
  316. (display "Welcome, this is GNU's early boot Guile.\n")
  317. (display "Use '--repl' for an initrd REPL.\n\n")
  318. (call-with-error-handling
  319. (lambda ()
  320. (mount-essential-file-systems)
  321. (let* ((args (linux-command-line))
  322. (to-load (find-long-option "--load" args))
  323. (root (find-long-option "--root" args)))
  324. (when (member "--repl" args)
  325. (start-repl))
  326. (display "loading kernel modules...\n")
  327. (for-each (cut load-linux-module* <>
  328. #:lookup-module lookup-module)
  329. (map lookup-module linux-modules))
  330. (when qemu-guest-networking?
  331. (unless (configure-qemu-networking)
  332. (display "network interface is DOWN\n")))
  333. ;; Prepare the real root file system under /root.
  334. (unless (file-exists? "/root")
  335. (mkdir "/root"))
  336. (when (procedure? pre-mount)
  337. ;; Do whatever actions are needed before mounting the root file
  338. ;; system--e.g., installing device mappings. Error out when the
  339. ;; return value is false.
  340. (unless (pre-mount)
  341. (error "pre-mount actions failed")))
  342. (if root
  343. (mount-root-file-system (canonicalize-device-spec root)
  344. root-fs-type
  345. #:volatile-root? volatile-root?)
  346. (mount "none" "/root" "tmpfs"))
  347. ;; Mount the specified file systems.
  348. (for-each mount-file-system
  349. (remove root-mount-point? mounts))
  350. (if to-load
  351. (begin
  352. (switch-root "/root")
  353. (format #t "loading '~a'...\n" to-load)
  354. (primitive-load to-load)
  355. (format (current-error-port)
  356. "boot program '~a' terminated, rebooting~%"
  357. to-load)
  358. (sleep 2)
  359. (reboot))
  360. (begin
  361. (display "no boot file passed via '--load'\n")
  362. (display "entering a warm and cozy REPL\n")
  363. (start-repl)))))))
  364. ;;; linux-initrd.scm ends here