Merge branch 'master' into staging
[jackhill/guix/guix.git] / gnu / build / image.scm
CommitLineData
f19cf27c
MO
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3;;; Copyright © 2016 Christopher Allan Webber <cwebber@dustycloud.org>
4;;; Copyright © 2016, 2017 Leo Famulari <leo@famulari.name>
5;;; Copyright © 2017 Marius Bakke <mbakke@fastmail.com>
6;;; Copyright © 2020 Tobias Geerinckx-Rice <me@tobias.gr>
7;;; Copyright © 2020 Mathieu Othacehe <m.othacehe@gmail.com>
8;;;
9;;; This file is part of GNU Guix.
10;;;
11;;; GNU Guix is free software; you can redistribute it and/or modify it
12;;; under the terms of the GNU General Public License as published by
13;;; the Free Software Foundation; either version 3 of the License, or (at
14;;; your option) any later version.
15;;;
16;;; GNU Guix is distributed in the hope that it will be useful, but
17;;; WITHOUT ANY WARRANTY; without even the implied warranty of
18;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;;; GNU General Public License for more details.
20;;;
21;;; You should have received a copy of the GNU General Public License
22;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
23
24(define-module (gnu build image)
25 #:use-module (guix build store-copy)
26 #:use-module (guix build syscalls)
27 #:use-module (guix build utils)
28 #:use-module (guix store database)
29 #:use-module (gnu build bootloader)
30 #:use-module (gnu build install)
31 #:use-module (gnu build linux-boot)
32 #:use-module (gnu image)
33 #:use-module (gnu system uuid)
34 #:use-module (ice-9 ftw)
35 #:use-module (ice-9 match)
36 #:use-module (srfi srfi-19)
37 #:use-module (srfi srfi-34)
38 #:use-module (srfi srfi-35)
39 #:export (make-partition-image
40 genimage
41 initialize-efi-partition
42 initialize-root-partition
43
44 make-iso9660-image))
45
46(define (sexp->partition sexp)
47 "Take SEXP, a tuple as returned by 'partition->gexp', and turn it into a
48<partition> record."
49 (match sexp
bd3716f6 50 ((size file-system file-system-options label uuid)
f19cf27c
MO
51 (partition (size size)
52 (file-system file-system)
bd3716f6 53 (file-system-options file-system-options)
f19cf27c
MO
54 (label label)
55 (uuid uuid)))))
56
57(define (size-in-kib size)
58 "Convert SIZE expressed in bytes, to kilobytes and return it as a string."
59 (number->string
60 (inexact->exact (ceiling (/ size 1024)))))
61
62(define (estimate-partition-size root)
63 "Given the ROOT directory, evalute and return its size. As this doesn't
64take the partition metadata size into account, take a 25% margin."
65 (* 1.25 (file-size root)))
66
16f9124d
MO
67(define* (make-ext-image partition target root
68 #:key
69 (owner-uid 0)
70 (owner-gid 0))
71 "Handle the creation of EXT2/3/4 partition images. See
72'make-partition-image'."
f19cf27c 73 (let ((size (partition-size partition))
16f9124d 74 (fs (partition-file-system partition))
bd3716f6 75 (fs-options (partition-file-system-options partition))
f19cf27c
MO
76 (label (partition-label partition))
77 (uuid (partition-uuid partition))
bd3716f6
MO
78 (journal-options "lazy_itable_init=1,lazy_journal_init=1"))
79 (apply invoke
80 `("mke2fs" "-t" ,fs "-d" ,root
81 "-L" ,label "-U" ,(uuid->string uuid)
82 "-E" ,(format #f "root_owner=~a:~a,~a"
83 owner-uid owner-gid journal-options)
84 ,@fs-options
85 ,target
86 ,(format #f "~ak"
87 (size-in-kib
88 (if (eq? size 'guess)
89 (estimate-partition-size root)
90 size)))))))
f19cf27c
MO
91
92(define* (make-vfat-image partition target root)
93 "Handle the creation of VFAT partition images. See 'make-partition-image'."
94 (let ((size (partition-size partition))
95 (label (partition-label partition)))
96 (invoke "mkdosfs" "-n" label "-C" target "-F" "16" "-S" "1024"
97 (size-in-kib
98 (if (eq? size 'guess)
99 (estimate-partition-size root)
100 size)))
101 (for-each (lambda (file)
102 (unless (member file '("." ".."))
103 (invoke "mcopy" "-bsp" "-i" target
104 (string-append root "/" file)
105 (string-append "::" file))))
106 (scandir root))))
107
108(define* (make-partition-image partition-sexp target root)
109 "Create and return the image of PARTITION-SEXP as TARGET. Use the given
110ROOT directory to populate the image."
111 (let* ((partition (sexp->partition partition-sexp))
112 (type (partition-file-system partition)))
113 (cond
16f9124d
MO
114 ((string-prefix? "ext" type)
115 (make-ext-image partition target root))
f19cf27c
MO
116 ((string=? type "vfat")
117 (make-vfat-image partition target root))
118 (else
119 (format (current-error-port)
120 "Unsupported partition type~%.")))))
121
122(define* (genimage config target)
123 "Use genimage to generate in TARGET directory, the image described in the
124given CONFIG file."
125 ;; genimage needs a 'root' directory.
126 (mkdir "root")
127 (invoke "genimage" "--config" config
128 "--outputpath" target))
129
130(define* (register-closure prefix closure
131 #:key
132 (deduplicate? #t) (reset-timestamps? #t)
133 (schema (sql-schema)))
134 "Register CLOSURE in PREFIX, where PREFIX is the directory name of the
135target store and CLOSURE is the name of a file containing a reference graph as
136produced by #:references-graphs.. As a side effect, if RESET-TIMESTAMPS? is
137true, reset timestamps on store files and, if DEDUPLICATE? is true,
138deduplicates files common to CLOSURE and the rest of PREFIX."
139 (let ((items (call-with-input-file closure read-reference-graph)))
140 (register-items items
141 #:prefix prefix
142 #:deduplicate? deduplicate?
143 #:reset-timestamps? reset-timestamps?
144 #:registration-time %epoch
145 #:schema schema)))
146
147(define* (initialize-efi-partition root
148 #:key
05f37c16 149 grub-efi
f19cf27c 150 #:allow-other-keys)
72d1562a 151 "Install in ROOT directory, an EFI loader using GRUB-EFI."
05f37c16 152 (install-efi-loader grub-efi root))
f19cf27c
MO
153
154(define* (initialize-root-partition root
155 #:key
156 bootcfg
157 bootcfg-location
9c1adb24
MO
158 bootloader-package
159 bootloader-installer
f19cf27c
MO
160 (deduplicate? #t)
161 references-graphs
162 (register-closures? #t)
163 system-directory
8423c2d3 164 make-device-nodes
f19cf27c
MO
165 #:allow-other-keys)
166 "Initialize the given ROOT directory. Use BOOTCFG and BOOTCFG-LOCATION to
167install the bootloader configuration.
168
169If REGISTER-CLOSURES? is true, register REFERENCES-GRAPHS in the store. If
170DEDUPLICATE? is true, then also deduplicate files common to CLOSURES and the
171rest of the store when registering the closures. SYSTEM-DIRECTORY is the name
172of the directory of the 'system' derivation."
173 (populate-root-file-system system-directory root)
174 (populate-store references-graphs root)
175
c77b9285 176 ;; Populate /dev.
8423c2d3
MO
177 (when make-device-nodes
178 (make-device-nodes root))
c77b9285 179
f19cf27c
MO
180 (when register-closures?
181 (for-each (lambda (closure)
182 (register-closure root
183 closure
184 #:reset-timestamps? #t
185 #:deduplicate? deduplicate?))
186 references-graphs))
187
9c1adb24
MO
188 (when bootloader-installer
189 (display "installing bootloader...\n")
190 (bootloader-installer bootloader-package #f root))
f19cf27c
MO
191 (when bootcfg
192 (install-boot-config bootcfg bootcfg-location root)))
193
194(define* (make-iso9660-image xorriso grub-mkrescue-environment
195 grub bootcfg system-directory root target
196 #:key (volume-id "Guix_image") (volume-uuid #f)
197 register-closures? (references-graphs '())
198 (compression? #t))
199 "Given a GRUB package, creates an iso image as TARGET, using BOOTCFG as
200GRUB configuration and OS-DRV as the stuff in it."
201 (define grub-mkrescue
202 (string-append grub "/bin/grub-mkrescue"))
203
204 (define grub-mkrescue-sed.sh
205 (string-append (getcwd) "/" "grub-mkrescue-sed.sh"))
206
207 ;; Use a modified version of grub-mkrescue-sed.sh, see below.
208 (copy-file (string-append xorriso
209 "/bin/grub-mkrescue-sed.sh")
210 grub-mkrescue-sed.sh)
211
212 ;; Force grub-mkrescue-sed.sh to use the build directory instead of /tmp
213 ;; that is read-only inside the build container.
214 (substitute* grub-mkrescue-sed.sh
215 (("/tmp/") (string-append (getcwd) "/"))
216 (("MKRESCUE_SED_XORRISO_ARGS \\$x")
217 (format #f "MKRESCUE_SED_XORRISO_ARGS $(echo $x | sed \"s|/tmp|~a|\")"
218 (getcwd))))
219
220 ;; 'grub-mkrescue' calls out to mtools programs to create 'efi.img', a FAT
221 ;; file system image, and mtools honors SOURCE_DATE_EPOCH for the mtime of
222 ;; those files. The epoch for FAT is Jan. 1st 1980, not 1970, so choose
223 ;; that.
224 (setenv "SOURCE_DATE_EPOCH"
225 (number->string
226 (time-second
227 (date->time-utc (make-date 0 0 0 0 1 1 1980 0)))))
228
229 ;; Our patched 'grub-mkrescue' honors this environment variable and passes
230 ;; it to 'mformat', which makes it the serial number of 'efi.img'. This
231 ;; allows for deterministic builds.
232 (setenv "GRUB_FAT_SERIAL_NUMBER"
233 (number->string (if volume-uuid
234
235 ;; On 32-bit systems the 2nd argument must be
236 ;; lower than 2^32.
237 (string-hash (iso9660-uuid->string volume-uuid)
238 (- (expt 2 32) 1))
239
240 #x77777777)
241 16))
242
243 (setenv "MKRESCUE_SED_MODE" "original")
244 (setenv "MKRESCUE_SED_XORRISO" (string-append xorriso "/bin/xorriso"))
245 (setenv "MKRESCUE_SED_IN_EFI_NO_PT" "yes")
246
247 (for-each (match-lambda
248 ((name . value) (setenv name value)))
249 grub-mkrescue-environment)
250
251 (apply invoke grub-mkrescue
252 (string-append "--xorriso=" grub-mkrescue-sed.sh)
253 "-o" target
254 (string-append "boot/grub/grub.cfg=" bootcfg)
255 root
256 "--"
257 ;; Set all timestamps to 1.
258 "-volume_date" "all_file_dates" "=1"
259
260 `(,@(if compression?
261 '(;; ‘zisofs’ compression reduces the total image size by
262 ;; ~60%.
263 "-zisofs" "level=9:block_size=128k" ; highest compression
264 ;; It's transparent to our Linux-Libre kernel but not to
265 ;; GRUB. Don't compress the kernel, initrd, and other
266 ;; files read by grub.cfg, as well as common
267 ;; already-compressed file names.
268 "-find" "/" "-type" "f"
269 ;; XXX Even after "--" above, and despite documentation
270 ;; claiming otherwise, "-or" is stolen by grub-mkrescue
271 ;; which then chokes on it (as ‘-o …’) and dies. Don't use
272 ;; "-or".
273 "-not" "-wholename" "/boot/*"
274 "-not" "-wholename" "/System/*"
275 "-not" "-name" "unicode.pf2"
276 "-not" "-name" "bzImage"
277 "-not" "-name" "*.gz" ; initrd & all man pages
278 "-not" "-name" "*.png" ; includes grub-image.png
279 "-exec" "set_filter" "--zisofs"
280 "--")
281 '())
282 "-volid" ,(string-upcase volume-id)
283 ,@(if volume-uuid
284 `("-volume_date" "uuid"
285 ,(string-filter (lambda (value)
286 (not (char=? #\- value)))
287 (iso9660-uuid->string
288 volume-uuid)))
289 '()))))