image.scm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2022 Mathieu Othacehe <othacehe@gnu.org>
  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. (define-module (gnu tests image)
  19. #:use-module (gnu)
  20. #:use-module (gnu image)
  21. #:use-module (gnu tests)
  22. #:autoload (gnu system image) (system-image root-offset)
  23. #:use-module (gnu system uuid)
  24. #:use-module (gnu system vm)
  25. #:use-module (gnu packages guile)
  26. #:use-module (gnu packages guile-xyz)
  27. #:use-module (guix gexp)
  28. #:use-module (guix monads)
  29. #:use-module (ice-9 format)
  30. #:export (%test-images))
  31. ;;; Commentary:
  32. ;;;
  33. ;;; This module provides tests for the image creation process that is
  34. ;;; performed by "genimage" under the hood.
  35. ;;;
  36. ;;; The image partitionment is checked using Guile-Parted. The content of the
  37. ;;; images is out of the scope of this module. Other test modules such as
  38. ;;; (gnu tests installation) make sure that the produced images are viable.
  39. ;;;
  40. ;;; Code:
  41. ;; A dummy initializer creating a simple file in the partition.
  42. (define dummy-initializer
  43. #~(lambda* (root . rest)
  44. (mkdir root)
  45. (call-with-output-file
  46. (string-append root "/test")
  47. (lambda (port)
  48. (format port "content")))))
  49. (define %simple-efi-os
  50. (operating-system
  51. (inherit %simple-os)
  52. (bootloader (bootloader-configuration
  53. (bootloader grub-efi-bootloader)
  54. (targets '("/boot/efi"))))))
  55. ;; An MBR disk image with a single ext4 partition.
  56. (define i1
  57. (image
  58. (format 'disk-image)
  59. (operating-system %simple-os)
  60. (partitions
  61. (list
  62. (partition
  63. (size (* 1024 1024)) ;1MiB
  64. (offset root-offset)
  65. (label "test")
  66. (file-system "ext4")
  67. (flags '(boot))
  68. (initializer dummy-initializer))))))
  69. ;; A GPT disk image with a single ext4 partition.
  70. (define i2
  71. (image
  72. (format 'disk-image)
  73. (operating-system %simple-efi-os)
  74. (partition-table-type 'gpt)
  75. (partitions
  76. (list
  77. (partition
  78. (size (* 1024 1024)) ;1MiB
  79. (offset root-offset)
  80. (label "test")
  81. (file-system "ext4")
  82. (flags '(boot))
  83. (initializer dummy-initializer))))))
  84. ;; An MBR disk image with multiple ext4 partitions.
  85. (define i3
  86. (image
  87. (format 'disk-image)
  88. (operating-system %simple-os)
  89. (partitions
  90. (list
  91. (partition
  92. (size (* 1024 1024)) ;1MiB
  93. (offset root-offset)
  94. (label "test")
  95. (file-system "ext4")
  96. (flags '(boot))
  97. (initializer dummy-initializer))
  98. (partition
  99. (size (* 1024 1024)) ;1MiB
  100. (label "test2")
  101. (file-system "ext4")
  102. (flags '())
  103. (initializer dummy-initializer))))))
  104. ;; A GPT disk image with multiple ext4 partitions.
  105. (define i4
  106. (image
  107. (format 'disk-image)
  108. (operating-system %simple-efi-os)
  109. (partition-table-type 'gpt)
  110. (partitions
  111. (list
  112. (partition
  113. (size (* 1024 1024)) ;1MiB
  114. (offset root-offset)
  115. (label "test")
  116. (file-system "ext4")
  117. (flags '(boot))
  118. (initializer dummy-initializer))
  119. (partition
  120. (size (* 1024 1024)) ;1MiB
  121. (label "test2")
  122. (file-system "ext4")
  123. (flags '())
  124. (initializer dummy-initializer))))))
  125. ;; A GPT disk image with fat32 and ext4 partitions.
  126. (define i5
  127. (image
  128. (format 'disk-image)
  129. (operating-system %simple-efi-os)
  130. (partition-table-type 'gpt)
  131. (partitions
  132. (list
  133. (partition
  134. (size (* 1024 1024 128)) ;128MiB
  135. (offset root-offset)
  136. (label "test")
  137. (file-system "fat32")
  138. (flags '(esp))
  139. (initializer dummy-initializer))
  140. (partition
  141. (size (* 1024 1024 256)) ;256MiB
  142. (label "test2")
  143. (file-system "ext4")
  144. (flags '(boot))
  145. (initializer dummy-initializer))))))
  146. (define (run-images-test)
  147. (define test
  148. (with-imported-modules '((srfi srfi-64)
  149. (gnu build marionette))
  150. (with-extensions (list guile-parted guile-bytestructures)
  151. #~(begin
  152. (use-modules (gnu build marionette)
  153. (srfi srfi-1)
  154. (srfi srfi-26)
  155. (srfi srfi-64)
  156. (parted))
  157. (define (image->disk img)
  158. (disk-new (get-device img)))
  159. (test-runner-current (system-test-runner #$output))
  160. (test-begin "images")
  161. ;; Image i1.
  162. (define i1-image
  163. #$(system-image i1))
  164. (define d1-device
  165. (get-device i1-image))
  166. (test-equal "msdos"
  167. (disk-type-name (disk-probe d1-device)))
  168. (test-equal 1
  169. (disk-get-primary-partition-count (disk-new d1-device)))
  170. (test-assert
  171. (let* ((disk (disk-new d1-device))
  172. (partitions (disk-partitions disk))
  173. (boot-partition (find normal-partition? partitions)))
  174. (partition-get-flag boot-partition PARTITION-FLAG-BOOT)))
  175. ;; Image i2.
  176. (define i2-image
  177. #$(system-image i2))
  178. (define d2-device
  179. (get-device i2-image))
  180. (test-equal "gpt"
  181. (disk-type-name (disk-probe d2-device)))
  182. (test-equal 1
  183. (disk-get-primary-partition-count (disk-new d2-device)))
  184. (test-equal "test"
  185. (let* ((disk (disk-new d2-device))
  186. (partitions (disk-partitions disk))
  187. (boot-partition (find normal-partition? partitions)))
  188. (partition-get-name boot-partition)))
  189. ;; Image i3.
  190. (define i3-image
  191. #$(system-image i3))
  192. (define d3-device
  193. (get-device i3-image))
  194. (test-equal "msdos"
  195. (disk-type-name (disk-probe d3-device)))
  196. (test-equal 2
  197. (disk-get-primary-partition-count (disk-new d3-device)))
  198. ;; Image i4.
  199. (define i4-image
  200. #$(system-image i4))
  201. (define d4-device
  202. (get-device i4-image))
  203. (test-equal "gpt"
  204. (disk-type-name (disk-probe d4-device)))
  205. (test-equal 2
  206. (disk-get-primary-partition-count (disk-new d4-device)))
  207. ;; Image i5.
  208. (define i5-image
  209. #$(system-image i5))
  210. (define d5-device
  211. (get-device i5-image))
  212. (define (sector->byte sector)
  213. (/ (* sector (device-sector-size d5-device))
  214. MEBIBYTE-SIZE))
  215. (test-equal "gpt"
  216. (disk-type-name (disk-probe d5-device)))
  217. (test-equal 2
  218. (disk-get-primary-partition-count (disk-new d5-device)))
  219. (test-equal '("fat32" "ext4")
  220. (map (compose filesystem-type-name partition-fs-type)
  221. (filter data-partition?
  222. (disk-partitions (disk-new d5-device)))))
  223. ;; The first partition has a 1MiB offset has a 128MiB size. The
  224. ;; second partition should then start at 129MiB.
  225. (test-equal '(1 129)
  226. (map (compose sector->byte partition-start)
  227. (filter data-partition?
  228. (disk-partitions (disk-new d5-device)))))
  229. (test-end)))))
  230. (gexp->derivation "images-test" test))
  231. (define %test-images
  232. (system-test
  233. (name "images")
  234. (description "Build and test basic system images.")
  235. (value (run-images-test))))