gnu: dune: Update to 4.4.1.
[jackhill/guix/guix.git] / gnu / image.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2020 Mathieu Othacehe <m.othacehe@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
19 (define-module (gnu image)
20 #:use-module (guix platform)
21 #:use-module (guix records)
22 #:use-module (guix diagnostics)
23 #:use-module (guix i18n)
24 #:use-module (srfi srfi-34)
25 #:use-module (srfi srfi-35)
26 #:export (partition
27 partition?
28 partition-device
29 partition-size
30 partition-offset
31 partition-file-system
32 partition-file-system-options
33 partition-label
34 partition-uuid
35 partition-flags
36 partition-initializer
37
38 image
39 image?
40 image-name
41 image-format
42 image-platform
43 image-size
44 image-operating-system
45 image-partition-table-type
46 image-partitions
47 image-compression?
48 image-volatile-root?
49 image-shared-store?
50 image-shared-network?
51 image-substitutable?
52
53 image-type
54 image-type?
55 image-type-name
56 image-type-constructor
57
58 os->image
59 os+platform->image))
60
61 \f
62 ;;;
63 ;;; Partition record.
64 ;;;
65
66 (define-record-type* <partition> partition make-partition
67 partition?
68 (device partition-device (default #f))
69 (size partition-size)
70 (offset partition-offset (default 0))
71 (file-system partition-file-system (default "ext4"))
72 (file-system-options partition-file-system-options
73 (default '()))
74 (label partition-label (default #f))
75 (uuid partition-uuid (default #f))
76 (flags partition-flags (default '()))
77 (initializer partition-initializer (default #f))) ;gexp | #f
78
79 \f
80 ;;;
81 ;;; Image record.
82 ;;;
83
84 (define-syntax-rule (define-set-sanitizer name field set)
85 "Define NAME as a procedure or macro that raises an error if passed a value
86 that is not in SET, mentioning FIELD in the error message."
87 (define-with-syntax-properties (name (value properties))
88 (unless (memq value 'set)
89 (raise
90 (make-compound-condition
91 (condition
92 (&error-location
93 (location (source-properties->location properties))))
94 (formatted-message (G_ "~s: invalid '~a' value") value 'field))))
95 value))
96
97 (define-set-sanitizer validate-image-format format
98 (disk-image compressed-qcow2 docker iso9660))
99 (define-set-sanitizer validate-partition-table-type partition-table-type
100 (mbr gpt))
101
102 (define-record-type* <image>
103 image make-image
104 image?
105 (name image-name ;symbol
106 (default #f))
107 (format image-format ;symbol
108 (sanitize validate-image-format))
109 (platform image-platform ;<platform>
110 (default #f))
111 (size image-size ;size in bytes as integer
112 (default 'guess))
113 (operating-system image-operating-system ;<operating-system>
114 (default #f))
115 (partition-table-type image-partition-table-type ; 'mbr or 'gpt
116 (default 'mbr)
117 (sanitize validate-partition-table-type))
118 (partitions image-partitions ;list of <partition>
119 (default '()))
120 (compression? image-compression? ;boolean
121 (default #t))
122 (volatile-root? image-volatile-root? ;boolean
123 (default #t))
124 (shared-store? image-shared-store? ;boolean
125 (default #f))
126 (shared-network? image-shared-network? ;boolean
127 (default #f))
128 (substitutable? image-substitutable? ;boolean
129 (default #t)))
130
131 \f
132 ;;;
133 ;;; Image type.
134 ;;;
135
136 (define-record-type* <image-type>
137 image-type make-image-type
138 image-type?
139 (name image-type-name) ;symbol
140 (constructor image-type-constructor)) ;<operating-system> -> <image>
141
142 \f
143 ;;;
144 ;;; Image creation.
145 ;;;
146
147 (define* (os->image os #:key type)
148 (let ((constructor (image-type-constructor type)))
149 (constructor os)))
150
151 (define* (os+platform->image os platform #:key type)
152 (image
153 (inherit (os->image os #:type type))
154 (platform platform)))