boot-parameters.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 gexp)
  30. #:use-module (guix store)
  31. #:use-module (guix tests)
  32. #:use-module (srfi srfi-64)
  33. #:use-module (rnrs bytevectors))
  34. (define %default-label "GNU with Linux-libre 99.1.2")
  35. (define %default-kernel-path
  36. (string-append (%store-prefix)
  37. "/zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz-linux-libre-99.1.2"))
  38. (define %default-kernel
  39. (string-append %default-kernel-path "/" (system-linux-image-file-name)))
  40. (define %default-kernel-arguments '())
  41. (define %default-initrd-path
  42. (string-append (%store-prefix) "/wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww-initrd"))
  43. (define %default-initrd (string-append %default-initrd-path "/initrd.cpio.gz"))
  44. (define %default-root-device (uuid "abcdef12-3456-7890-abcd-ef1234567890"))
  45. (define %default-store-device (uuid "01234567-89ab-cdef-0123-456789abcdef"))
  46. (define %default-btrfs-subvolume "testfs")
  47. (define %default-store-directory-prefix
  48. (string-append "/" %default-btrfs-subvolume))
  49. (define %default-store-mount-point (%store-prefix))
  50. (define %default-store-crypto-devices
  51. (list (uuid "00000000-1111-2222-3333-444444444444")
  52. (uuid "55555555-6666-7777-8888-999999999999")))
  53. (define %default-multiboot-modules '())
  54. (define %default-locale "es_ES.utf8")
  55. (define %root-path "/")
  56. (define %grub-boot-parameters
  57. (boot-parameters
  58. (bootloader-name 'grub)
  59. (bootloader-menu-entries '())
  60. (root-device %default-root-device)
  61. (label %default-label)
  62. (kernel %default-kernel)
  63. (kernel-arguments %default-kernel-arguments)
  64. (initrd %default-initrd)
  65. (multiboot-modules %default-multiboot-modules)
  66. (locale %default-locale)
  67. (store-device %default-store-device)
  68. (store-directory-prefix %default-store-directory-prefix)
  69. (store-crypto-devices %default-store-crypto-devices)
  70. (store-mount-point %default-store-mount-point)))
  71. (define %default-operating-system
  72. (operating-system
  73. (host-name "host")
  74. (timezone "Europe/Berlin")
  75. (locale %default-locale)
  76. (bootloader (bootloader-configuration
  77. (bootloader grub-bootloader)
  78. (target "/dev/sda")))
  79. (file-systems (cons* (file-system
  80. (device %default-root-device)
  81. (mount-point %root-path)
  82. (type "ext4"))
  83. (file-system
  84. (device %default-store-device)
  85. (mount-point %default-store-mount-point)
  86. (type "btrfs")
  87. (options
  88. (string-append "subvol="
  89. %default-btrfs-subvolume)))
  90. %base-file-systems))))
  91. (define (quote-uuid uuid)
  92. (list 'uuid (uuid-type uuid) (uuid-bytevector uuid)))
  93. ;; Call read-boot-parameters with the desired string as input.
  94. (define* (test-read-boot-parameters
  95. #:key
  96. (version 0)
  97. (bootloader-name 'grub)
  98. (bootloader-menu-entries '())
  99. (label %default-label)
  100. (root-device (quote-uuid %default-root-device))
  101. (kernel %default-kernel)
  102. (kernel-arguments %default-kernel-arguments)
  103. (initrd %default-initrd)
  104. (multiboot-modules %default-multiboot-modules)
  105. (locale %default-locale)
  106. (with-store #t)
  107. (store-device
  108. (quote-uuid %default-store-device))
  109. (store-crypto-devices
  110. (map quote-uuid %default-store-crypto-devices))
  111. (store-directory-prefix %default-store-directory-prefix)
  112. (store-mount-point %default-store-mount-point))
  113. (define (generate-boot-parameters)
  114. (define (sexp-or-nothing fmt val)
  115. (cond ((eq? 'false val) (format #false fmt #false))
  116. (val (format #false fmt val))
  117. (else "")))
  118. (format #false "(boot-parameters~a~a~a~a~a~a~a~a~a~a)"
  119. (sexp-or-nothing " (version ~S)" version)
  120. (sexp-or-nothing " (label ~S)" label)
  121. (sexp-or-nothing " (root-device ~S)" root-device)
  122. (sexp-or-nothing " (kernel ~S)" kernel)
  123. (sexp-or-nothing " (kernel-arguments ~S)" kernel-arguments)
  124. (sexp-or-nothing " (initrd ~S)" initrd)
  125. (if with-store
  126. (format #false " (store~a~a~a~a)"
  127. (sexp-or-nothing " (device ~S)" store-device)
  128. (sexp-or-nothing " (mount-point ~S)"
  129. store-mount-point)
  130. (sexp-or-nothing " (directory-prefix ~S)"
  131. store-directory-prefix)
  132. (sexp-or-nothing " (crypto-devices ~S)"
  133. store-crypto-devices))
  134. "")
  135. (sexp-or-nothing " (locale ~S)" locale)
  136. (sexp-or-nothing " (bootloader-name ~a)" bootloader-name)
  137. (sexp-or-nothing " (bootloader-menu-entries ~S)"
  138. bootloader-menu-entries)))
  139. (let ((str (generate-boot-parameters)))
  140. (call-with-input-string str read-boot-parameters)))
  141. (test-begin "boot-parameters")
  142. ;; XXX: <warning: unrecognized boot parameters at '#f'>
  143. (test-assert "read, construction, mandatory fields"
  144. (not (or (test-read-boot-parameters #:version #false)
  145. (test-read-boot-parameters #:version 'false)
  146. (test-read-boot-parameters #:version -1)
  147. (test-read-boot-parameters #:version "0")
  148. (test-read-boot-parameters #:root-device #false)
  149. (test-read-boot-parameters #:kernel #false)
  150. (test-read-boot-parameters #:label #false))))
  151. (test-assert "read, construction, optional fields"
  152. (and (test-read-boot-parameters #:bootloader-name #false)
  153. (test-read-boot-parameters #:bootloader-menu-entries #false)
  154. (test-read-boot-parameters #:kernel-arguments #false)
  155. (test-read-boot-parameters #:with-store #false)
  156. (test-read-boot-parameters #:store-device #false)
  157. (test-read-boot-parameters #:store-device 'false)
  158. (test-read-boot-parameters #:store-crypto-devices #false)
  159. (test-read-boot-parameters #:store-mount-point #false)
  160. (test-read-boot-parameters #:store-directory-prefix #false)
  161. (test-read-boot-parameters #:multiboot-modules #false)
  162. (test-read-boot-parameters #:locale #false)
  163. (test-read-boot-parameters #:bootloader-name #false
  164. #:kernel-arguments #false
  165. #:with-store #false
  166. #:locale #false)))
  167. (test-equal "read, default equality"
  168. %grub-boot-parameters
  169. (test-read-boot-parameters))
  170. (test-equal "read, root-device, label"
  171. (file-system-label "my-root")
  172. (boot-parameters-root-device
  173. (test-read-boot-parameters #:root-device '(file-system-label "my-root"))))
  174. (test-equal "read, root-device, /dev node"
  175. "/dev/sda2"
  176. (boot-parameters-root-device
  177. (test-read-boot-parameters #:root-device "/dev/sda2")))
  178. (test-equal "read, kernel, only store path"
  179. %default-kernel
  180. (boot-parameters-kernel
  181. (test-read-boot-parameters #:kernel %default-kernel-path)))
  182. (test-equal "read, kernel, full-path"
  183. %default-kernel
  184. (boot-parameters-kernel
  185. (test-read-boot-parameters #:kernel %default-kernel)))
  186. (test-assert "read, construction, missing initrd"
  187. (not (boot-parameters-initrd (test-read-boot-parameters #:initrd #false))))
  188. (test-equal "read, initrd, old format"
  189. "/a/b"
  190. (boot-parameters-initrd
  191. (test-read-boot-parameters #:initrd (list 'string-append "/a" "/b"))))
  192. ;; Compatibility reasons specified in gnu/system.scm.
  193. (test-eq "read, bootloader-name, default value"
  194. 'grub
  195. (boot-parameters-bootloader-name
  196. (test-read-boot-parameters #:bootloader-name #false)))
  197. (test-eq "read, bootloader-menu-entries, default value"
  198. '()
  199. (boot-parameters-bootloader-menu-entries
  200. (test-read-boot-parameters #:bootloader-menu-entries #false)))
  201. (test-eq "read, kernel-arguments, default value"
  202. '()
  203. (boot-parameters-kernel-arguments
  204. (test-read-boot-parameters #:kernel-arguments #false)))
  205. (test-assert "read, store-device, filter /dev"
  206. (not (boot-parameters-store-device
  207. (test-read-boot-parameters #:store-device "/dev/sda3"))))
  208. (test-assert "read, no-store, filter /dev from root"
  209. (not (boot-parameters-store-device
  210. (test-read-boot-parameters #:root-device "/dev/sda3"
  211. #:with-store #false))))
  212. (test-assert "read, no store-device, filter /dev from root"
  213. (not (boot-parameters-store-device
  214. (test-read-boot-parameters #:root-device "/dev/sda3"
  215. #:store-device #false))))
  216. (test-assert "read, store-device #false, filter /dev from root"
  217. (not (boot-parameters-store-device
  218. (test-read-boot-parameters #:root-device "/dev/sda3"
  219. #:store-device 'false))))
  220. (test-equal "read, store-device, label (legacy)"
  221. (file-system-label "my-store")
  222. (boot-parameters-store-device
  223. (test-read-boot-parameters #:store-device "my-store")))
  224. (test-equal "read, store-device, from root"
  225. %default-root-device
  226. (boot-parameters-store-device
  227. (test-read-boot-parameters #:with-store #false)))
  228. (test-equal "read, no store-mount-point, default"
  229. %root-path
  230. (boot-parameters-store-mount-point
  231. (test-read-boot-parameters #:store-mount-point #false)))
  232. (test-equal "read, no store, default store-mount-point"
  233. %root-path
  234. (boot-parameters-store-mount-point
  235. (test-read-boot-parameters #:with-store #false)))
  236. (test-equal "read, store-crypto-devices, default"
  237. '()
  238. (boot-parameters-store-crypto-devices
  239. (test-read-boot-parameters #:store-crypto-devices #false)))
  240. ;; XXX: <warning: unrecognized crypto-devices #f at '#f'>
  241. (test-equal "read, store-crypto-devices, false"
  242. '()
  243. (boot-parameters-store-crypto-devices
  244. (test-read-boot-parameters #:store-crypto-devices 'false)))
  245. ;; XXX: <warning: unrecognized crypto-device "bad" at '#f'>
  246. (test-equal "read, store-crypto-devices, string"
  247. '()
  248. (boot-parameters-store-crypto-devices
  249. (test-read-boot-parameters #:store-crypto-devices "bad")))
  250. ;; For whitebox testing
  251. (define operating-system-boot-parameters
  252. (@@ (gnu system) operating-system-boot-parameters))
  253. (test-equal "from os, locale"
  254. %default-locale
  255. (boot-parameters-locale
  256. (operating-system-boot-parameters %default-operating-system
  257. %default-root-device)))
  258. (test-equal "from os, store-directory-prefix"
  259. %default-store-directory-prefix
  260. (boot-parameters-store-directory-prefix
  261. (operating-system-boot-parameters %default-operating-system
  262. %default-root-device)))
  263. (test-end "boot-parameters")