boot-parameters.scm 13 KB

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