boot-parameters.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019, 2020 Miguel Ángel Arruga Vivas <rosen644835@gmail.com>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;;
  20. ;;; Test boot parameters value storage and compatibility.
  21. ;;;
  22. ;;; Code:
  23. (define-module (test-boot-parameters)
  24. #:use-module (gnu bootloader)
  25. #:use-module (gnu bootloader grub)
  26. #:use-module (gnu system)
  27. #:use-module (gnu system file-systems)
  28. #:use-module (gnu system uuid)
  29. #:use-module ((guix diagnostics) #:select (formatted-message?))
  30. #:use-module (guix gexp)
  31. #:use-module (guix store)
  32. #:use-module (guix tests)
  33. #:use-module (srfi srfi-34)
  34. #:use-module (srfi srfi-64)
  35. #:use-module (rnrs bytevectors))
  36. (define %default-label "GNU with Linux-libre 99.1.2")
  37. (define %default-kernel-path
  38. (string-append (%store-prefix)
  39. "/zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz-linux-libre-99.1.2"))
  40. (define %default-kernel
  41. (string-append %default-kernel-path "/" (system-linux-image-file-name)))
  42. (define %default-kernel-arguments '())
  43. (define %default-initrd-path
  44. (string-append (%store-prefix) "/wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww-initrd"))
  45. (define %default-initrd (string-append %default-initrd-path "/initrd.cpio.gz"))
  46. (define %default-root-device (uuid "abcdef12-3456-7890-abcd-ef1234567890"))
  47. (define %default-store-device (uuid "01234567-89ab-cdef-0123-456789abcdef"))
  48. (define %default-btrfs-subvolume "testfs")
  49. (define %default-store-directory-prefix
  50. (string-append "/" %default-btrfs-subvolume))
  51. (define %default-store-mount-point (%store-prefix))
  52. (define %default-store-crypto-devices
  53. (list (uuid "00000000-1111-2222-3333-444444444444")
  54. (uuid "55555555-6666-7777-8888-999999999999")))
  55. (define %default-multiboot-modules '())
  56. (define %default-locale "es_ES.utf8")
  57. (define %root-path "/")
  58. (define %grub-boot-parameters
  59. (boot-parameters
  60. (bootloader-name 'grub)
  61. (bootloader-menu-entries '())
  62. (root-device %default-root-device)
  63. (label %default-label)
  64. (kernel %default-kernel)
  65. (kernel-arguments %default-kernel-arguments)
  66. (initrd %default-initrd)
  67. (multiboot-modules %default-multiboot-modules)
  68. (locale %default-locale)
  69. (store-device %default-store-device)
  70. (store-directory-prefix %default-store-directory-prefix)
  71. (store-crypto-devices %default-store-crypto-devices)
  72. (store-mount-point %default-store-mount-point)))
  73. (define %default-operating-system
  74. (operating-system
  75. (host-name "host")
  76. (timezone "Europe/Berlin")
  77. (locale %default-locale)
  78. (bootloader (bootloader-configuration
  79. (bootloader grub-bootloader)
  80. (targets '("/dev/sda"))))
  81. (file-systems (cons* (file-system
  82. (device %default-root-device)
  83. (mount-point %root-path)
  84. (type "ext4"))
  85. (file-system
  86. (device %default-store-device)
  87. (mount-point %default-store-mount-point)
  88. (type "btrfs")
  89. (options
  90. (string-append "subvol="
  91. %default-btrfs-subvolume)))
  92. %base-file-systems))))
  93. (define (quote-uuid uuid)
  94. (list 'uuid (uuid-type uuid) (uuid-bytevector uuid)))
  95. ;; Call read-boot-parameters with the desired string as input.
  96. (define* (test-read-boot-parameters
  97. #:key
  98. (version %boot-parameters-version)
  99. (bootloader-name 'grub)
  100. (bootloader-menu-entries '())
  101. (label %default-label)
  102. (root-device (quote-uuid %default-root-device))
  103. (kernel %default-kernel)
  104. (kernel-arguments %default-kernel-arguments)
  105. (initrd %default-initrd)
  106. (multiboot-modules %default-multiboot-modules)
  107. (locale %default-locale)
  108. (with-store #t)
  109. (store-device
  110. (quote-uuid %default-store-device))
  111. (store-crypto-devices
  112. (map quote-uuid %default-store-crypto-devices))
  113. (store-directory-prefix %default-store-directory-prefix)
  114. (store-mount-point %default-store-mount-point))
  115. (define (generate-boot-parameters)
  116. (define (sexp-or-nothing fmt val)
  117. (cond ((eq? 'false val) (format #false fmt #false))
  118. (val (format #false fmt val))
  119. (else "")))
  120. (format #false "(boot-parameters~a~a~a~a~a~a~a~a~a~a)"
  121. (sexp-or-nothing " (version ~S)" version)
  122. (sexp-or-nothing " (label ~S)" label)
  123. (sexp-or-nothing " (root-device ~S)" root-device)
  124. (sexp-or-nothing " (kernel ~S)" kernel)
  125. (sexp-or-nothing " (kernel-arguments ~S)" kernel-arguments)
  126. (sexp-or-nothing " (initrd ~S)" initrd)
  127. (if with-store
  128. (format #false " (store~a~a~a~a)"
  129. (sexp-or-nothing " (device ~S)" store-device)
  130. (sexp-or-nothing " (mount-point ~S)"
  131. store-mount-point)
  132. (sexp-or-nothing " (directory-prefix ~S)"
  133. store-directory-prefix)
  134. (sexp-or-nothing " (crypto-devices ~S)"
  135. store-crypto-devices))
  136. "")
  137. (sexp-or-nothing " (locale ~S)" locale)
  138. (sexp-or-nothing " (bootloader-name ~a)" bootloader-name)
  139. (sexp-or-nothing " (bootloader-menu-entries ~S)"
  140. bootloader-menu-entries)))
  141. (let ((str (generate-boot-parameters)))
  142. (call-with-input-string str read-boot-parameters)))
  143. (test-begin "boot-parameters")
  144. ;; XXX: <warning: unrecognized boot parameters at '#f'>
  145. (test-assert "read, construction, mandatory fields"
  146. (let-syntax ((test-read-boot-parameters
  147. (syntax-rules ()
  148. ((_ args ...)
  149. (guard (c ((formatted-message? c) #f))
  150. (test-read-boot-parameters args ...))))))
  151. (not (or (test-read-boot-parameters #:version #false)
  152. (test-read-boot-parameters #:version 'false)
  153. (test-read-boot-parameters #:version -1)
  154. (test-read-boot-parameters #:version "0")
  155. (test-read-boot-parameters #:root-device #false)
  156. (test-read-boot-parameters #:kernel #false)
  157. (test-read-boot-parameters #:label #false)))))
  158. (test-assert "read, construction, optional fields"
  159. (and (test-read-boot-parameters #:bootloader-name #false)
  160. (test-read-boot-parameters #:bootloader-menu-entries #false)
  161. (test-read-boot-parameters #:kernel-arguments #false)
  162. (test-read-boot-parameters #:with-store #false)
  163. (test-read-boot-parameters #:store-device #false)
  164. (test-read-boot-parameters #:store-device 'false)
  165. (test-read-boot-parameters #:store-crypto-devices #false)
  166. (test-read-boot-parameters #:store-mount-point #false)
  167. (test-read-boot-parameters #:store-directory-prefix #false)
  168. (test-read-boot-parameters #:multiboot-modules #false)
  169. (test-read-boot-parameters #:locale #false)
  170. (test-read-boot-parameters #:bootloader-name #false
  171. #:kernel-arguments #false
  172. #:with-store #false
  173. #:locale #false)))
  174. (test-equal "read, default equality"
  175. %grub-boot-parameters
  176. (test-read-boot-parameters))
  177. (test-equal "read, root-device, label"
  178. (file-system-label "my-root")
  179. (boot-parameters-root-device
  180. (test-read-boot-parameters #:root-device '(file-system-label "my-root"))))
  181. (test-equal "read, root-device, /dev node"
  182. "/dev/sda2"
  183. (boot-parameters-root-device
  184. (test-read-boot-parameters #:root-device "/dev/sda2")))
  185. (test-equal "read, kernel, only store path"
  186. %default-kernel
  187. (boot-parameters-kernel
  188. (test-read-boot-parameters #:kernel %default-kernel-path)))
  189. (test-equal "read, kernel, full-path"
  190. %default-kernel
  191. (boot-parameters-kernel
  192. (test-read-boot-parameters #:kernel %default-kernel)))
  193. (test-assert "read, construction, missing initrd"
  194. (not (boot-parameters-initrd (test-read-boot-parameters #:initrd #false))))
  195. (test-equal "read, initrd, old format"
  196. "/a/b"
  197. (boot-parameters-initrd
  198. (test-read-boot-parameters #:initrd (list 'string-append "/a" "/b"))))
  199. ;; Compatibility reasons specified in gnu/system.scm.
  200. (test-eq "read, bootloader-name, default value"
  201. 'grub
  202. (boot-parameters-bootloader-name
  203. (test-read-boot-parameters #:bootloader-name #false)))
  204. (test-eq "read, bootloader-menu-entries, default value"
  205. '()
  206. (boot-parameters-bootloader-menu-entries
  207. (test-read-boot-parameters #:bootloader-menu-entries #false)))
  208. (test-eq "read, kernel-arguments, default value"
  209. '()
  210. (boot-parameters-kernel-arguments
  211. (test-read-boot-parameters #:kernel-arguments #false)))
  212. (test-assert "read, store-device, filter /dev"
  213. (not (boot-parameters-store-device
  214. (test-read-boot-parameters #:store-device "/dev/sda3"))))
  215. (test-assert "read, no-store, filter /dev from root"
  216. (not (boot-parameters-store-device
  217. (test-read-boot-parameters #:root-device "/dev/sda3"
  218. #:with-store #false))))
  219. (test-assert "read, no store-device, filter /dev from root"
  220. (not (boot-parameters-store-device
  221. (test-read-boot-parameters #:root-device "/dev/sda3"
  222. #:store-device #false))))
  223. (test-assert "read, store-device #false, filter /dev from root"
  224. (not (boot-parameters-store-device
  225. (test-read-boot-parameters #:root-device "/dev/sda3"
  226. #:store-device 'false))))
  227. (test-equal "read, store-device, label (legacy)"
  228. (file-system-label "my-store")
  229. (boot-parameters-store-device
  230. (test-read-boot-parameters #:store-device "my-store")))
  231. (test-equal "read, store-device, from root"
  232. %default-root-device
  233. (boot-parameters-store-device
  234. (test-read-boot-parameters #:with-store #false)))
  235. (test-equal "read, no store-mount-point, default"
  236. %root-path
  237. (boot-parameters-store-mount-point
  238. (test-read-boot-parameters #:store-mount-point #false)))
  239. (test-equal "read, no store, default store-mount-point"
  240. %root-path
  241. (boot-parameters-store-mount-point
  242. (test-read-boot-parameters #:with-store #false)))
  243. (test-equal "read, store-crypto-devices, default"
  244. '()
  245. (boot-parameters-store-crypto-devices
  246. (test-read-boot-parameters #:store-crypto-devices #false)))
  247. ;; XXX: <warning: unrecognized crypto-devices #f at '#f'>
  248. (test-equal "read, store-crypto-devices, false"
  249. '()
  250. (boot-parameters-store-crypto-devices
  251. (test-read-boot-parameters #:store-crypto-devices 'false)))
  252. ;; XXX: <warning: unrecognized crypto-device "bad" at '#f'>
  253. (test-equal "read, store-crypto-devices, string"
  254. '()
  255. (boot-parameters-store-crypto-devices
  256. (test-read-boot-parameters #:store-crypto-devices "bad")))
  257. ;; For whitebox testing
  258. (define operating-system-boot-parameters
  259. (@@ (gnu system) operating-system-boot-parameters))
  260. (test-equal "from os, locale"
  261. %default-locale
  262. (boot-parameters-locale
  263. (operating-system-boot-parameters %default-operating-system
  264. %default-root-device)))
  265. (test-equal "from os, store-directory-prefix"
  266. %default-store-directory-prefix
  267. (boot-parameters-store-directory-prefix
  268. (operating-system-boot-parameters %default-operating-system
  269. %default-root-device)))
  270. (test-end "boot-parameters")