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