2020-04-28 14:15:28 +02:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2022-08-30 19:18:26 +02:00
|
|
|
|
;;; Copyright © 2020, 2022 Mathieu Othacehe <othacehe@gnu.org>
|
2020-04-28 14:15:28 +02:00
|
|
|
|
;;;
|
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
|
;;;
|
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
|
;;; your option) any later version.
|
|
|
|
|
;;;
|
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
|
;;;
|
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
(define-module (gnu image)
|
2022-05-23 22:37:26 +02:00
|
|
|
|
#:use-module (guix platform)
|
2020-04-28 14:15:28 +02:00
|
|
|
|
#:use-module (guix records)
|
2022-07-01 12:06:39 +02:00
|
|
|
|
#:use-module (guix diagnostics)
|
|
|
|
|
#:use-module (guix i18n)
|
2022-08-30 19:18:26 +02:00
|
|
|
|
#:use-module (srfi srfi-1)
|
2022-07-01 12:06:39 +02:00
|
|
|
|
#:use-module (srfi srfi-34)
|
|
|
|
|
#:use-module (srfi srfi-35)
|
2020-04-28 14:15:28 +02:00
|
|
|
|
#:export (partition
|
|
|
|
|
partition?
|
|
|
|
|
partition-device
|
|
|
|
|
partition-size
|
2020-05-20 16:09:53 +02:00
|
|
|
|
partition-offset
|
2020-04-28 14:15:28 +02:00
|
|
|
|
partition-file-system
|
2020-05-26 16:25:25 +02:00
|
|
|
|
partition-file-system-options
|
2020-04-28 14:15:28 +02:00
|
|
|
|
partition-label
|
|
|
|
|
partition-uuid
|
|
|
|
|
partition-flags
|
|
|
|
|
partition-initializer
|
|
|
|
|
|
|
|
|
|
image
|
2021-01-20 12:06:56 +01:00
|
|
|
|
image?
|
2020-04-28 14:15:28 +02:00
|
|
|
|
image-name
|
|
|
|
|
image-format
|
2021-08-30 18:24:27 +02:00
|
|
|
|
image-platform
|
2020-04-28 14:15:28 +02:00
|
|
|
|
image-size
|
|
|
|
|
image-operating-system
|
2021-11-04 09:35:11 +01:00
|
|
|
|
image-partition-table-type
|
2020-04-28 14:15:28 +02:00
|
|
|
|
image-partitions
|
|
|
|
|
image-compression?
|
|
|
|
|
image-volatile-root?
|
2021-12-16 08:45:01 +01:00
|
|
|
|
image-shared-store?
|
2021-12-16 09:25:49 +01:00
|
|
|
|
image-shared-network?
|
2020-07-31 16:49:27 +02:00
|
|
|
|
image-substitutable?
|
|
|
|
|
|
|
|
|
|
image-type
|
|
|
|
|
image-type?
|
|
|
|
|
image-type-name
|
|
|
|
|
image-type-constructor
|
|
|
|
|
|
2021-08-30 18:24:27 +02:00
|
|
|
|
os->image
|
|
|
|
|
os+platform->image))
|
2020-04-28 14:15:28 +02:00
|
|
|
|
|
2022-08-30 19:18:26 +02:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Sanitizers.
|
|
|
|
|
;;;
|
|
|
|
|
|
2022-08-31 14:59:06 +02:00
|
|
|
|
;; Image and partition sizes can be either be a size in bytes or the 'guess
|
|
|
|
|
;; symbol denoting that the size should be estimated by Guix, according to the
|
|
|
|
|
;; image content.
|
2022-08-30 19:18:26 +02:00
|
|
|
|
(define-with-syntax-properties (validate-size (value properties))
|
|
|
|
|
(unless (and value
|
|
|
|
|
(or (eq? value 'guess) (integer? value)))
|
|
|
|
|
(raise
|
|
|
|
|
(make-compound-condition
|
|
|
|
|
(condition
|
|
|
|
|
(&error-location
|
|
|
|
|
(location (source-properties->location properties))))
|
|
|
|
|
(formatted-message
|
|
|
|
|
(G_ "size (~a) can only be 'guess or a numeric expression ~%")
|
|
|
|
|
value 'field))))
|
|
|
|
|
value)
|
|
|
|
|
|
2020-04-28 14:15:28 +02:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Partition record.
|
|
|
|
|
;;;
|
|
|
|
|
|
2022-08-31 14:59:06 +02:00
|
|
|
|
;; The partition offset should be a bytes count as an integer.
|
2022-08-30 19:18:26 +02:00
|
|
|
|
(define-with-syntax-properties (validate-partition-offset (value properties))
|
|
|
|
|
(unless (and value (integer? value))
|
|
|
|
|
(raise
|
|
|
|
|
(make-compound-condition
|
|
|
|
|
(condition
|
|
|
|
|
(&error-location
|
|
|
|
|
(location (source-properties->location properties))))
|
|
|
|
|
(formatted-message
|
|
|
|
|
(G_ "the partition offset (~a) can only be a \
|
|
|
|
|
numeric expression ~%") value 'field))))
|
|
|
|
|
value)
|
|
|
|
|
|
2022-08-31 14:59:06 +02:00
|
|
|
|
;; The supported partition flags.
|
2022-08-30 19:18:26 +02:00
|
|
|
|
(define-with-syntax-properties (validate-partition-flags (value properties))
|
|
|
|
|
(let ((bad-flags (lset-difference eq? value '(boot esp))))
|
|
|
|
|
(unless (and (list? value) (null? bad-flags))
|
|
|
|
|
(raise
|
|
|
|
|
(make-compound-condition
|
|
|
|
|
(condition
|
|
|
|
|
(&error-location
|
|
|
|
|
(location (source-properties->location properties))))
|
|
|
|
|
(formatted-message
|
|
|
|
|
(G_ "unsupported partition flag(s): ~a ~%") bad-flags)))))
|
|
|
|
|
value)
|
|
|
|
|
|
2020-04-28 14:15:28 +02:00
|
|
|
|
(define-record-type* <partition> partition make-partition
|
|
|
|
|
partition?
|
2022-08-30 19:18:26 +02:00
|
|
|
|
(size partition-size ;size in bytes as integer or 'guess
|
2022-09-02 19:45:38 +02:00
|
|
|
|
(default 'guess)
|
2022-08-30 19:18:26 +02:00
|
|
|
|
(sanitize validate-size))
|
|
|
|
|
(offset partition-offset
|
|
|
|
|
(default 0) ;offset in bytes as integer
|
|
|
|
|
(sanitize validate-partition-offset))
|
|
|
|
|
(file-system partition-file-system
|
|
|
|
|
(default "ext4")) ;string
|
2020-05-26 16:25:25 +02:00
|
|
|
|
(file-system-options partition-file-system-options
|
2022-08-30 19:18:26 +02:00
|
|
|
|
(default '())) ;list of strings
|
|
|
|
|
(label partition-label) ;string
|
|
|
|
|
(uuid partition-uuid
|
2022-09-02 19:44:59 +02:00
|
|
|
|
(default #false)) ;<uuid>
|
2022-08-30 19:18:26 +02:00
|
|
|
|
(flags partition-flags
|
|
|
|
|
(default '()) ;list of symbols
|
|
|
|
|
(sanitize validate-partition-flags))
|
|
|
|
|
(initializer partition-initializer
|
2022-09-02 19:44:59 +02:00
|
|
|
|
(default #false))) ;gexp | #false
|
2020-04-28 14:15:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Image record.
|
|
|
|
|
;;;
|
|
|
|
|
|
2022-07-01 12:06:39 +02:00
|
|
|
|
(define-syntax-rule (define-set-sanitizer name field set)
|
|
|
|
|
"Define NAME as a procedure or macro that raises an error if passed a value
|
|
|
|
|
that is not in SET, mentioning FIELD in the error message."
|
|
|
|
|
(define-with-syntax-properties (name (value properties))
|
|
|
|
|
(unless (memq value 'set)
|
|
|
|
|
(raise
|
|
|
|
|
(make-compound-condition
|
|
|
|
|
(condition
|
|
|
|
|
(&error-location
|
|
|
|
|
(location (source-properties->location properties))))
|
|
|
|
|
(formatted-message (G_ "~s: invalid '~a' value") value 'field))))
|
|
|
|
|
value))
|
|
|
|
|
|
2022-08-31 14:59:06 +02:00
|
|
|
|
;; The supported image formats.
|
2022-07-01 12:06:39 +02:00
|
|
|
|
(define-set-sanitizer validate-image-format format
|
2022-02-08 01:37:25 +01:00
|
|
|
|
(disk-image compressed-qcow2 docker iso9660 tarball wsl2))
|
2022-08-31 14:59:06 +02:00
|
|
|
|
|
|
|
|
|
;; The supported partition table types.
|
2022-07-01 12:06:39 +02:00
|
|
|
|
(define-set-sanitizer validate-partition-table-type partition-table-type
|
|
|
|
|
(mbr gpt))
|
|
|
|
|
|
2020-04-28 14:15:28 +02:00
|
|
|
|
(define-record-type* <image>
|
|
|
|
|
image make-image
|
|
|
|
|
image?
|
2020-06-13 17:33:10 +02:00
|
|
|
|
(name image-name ;symbol
|
2022-09-02 19:44:59 +02:00
|
|
|
|
(default #false))
|
2022-07-01 12:06:39 +02:00
|
|
|
|
(format image-format ;symbol
|
|
|
|
|
(sanitize validate-image-format))
|
2021-08-30 18:24:27 +02:00
|
|
|
|
(platform image-platform ;<platform>
|
2022-09-02 19:44:59 +02:00
|
|
|
|
(default #false))
|
2020-04-28 14:15:28 +02:00
|
|
|
|
(size image-size ;size in bytes as integer
|
2022-08-30 19:18:26 +02:00
|
|
|
|
(default 'guess)
|
|
|
|
|
(sanitize validate-size))
|
2022-09-05 18:12:22 +02:00
|
|
|
|
(operating-system image-operating-system) ;<operating-system>
|
2021-11-04 09:35:11 +01:00
|
|
|
|
(partition-table-type image-partition-table-type ; 'mbr or 'gpt
|
2022-07-01 12:06:39 +02:00
|
|
|
|
(default 'mbr)
|
|
|
|
|
(sanitize validate-partition-table-type))
|
2020-04-28 14:15:28 +02:00
|
|
|
|
(partitions image-partitions ;list of <partition>
|
|
|
|
|
(default '()))
|
|
|
|
|
(compression? image-compression? ;boolean
|
2022-09-02 19:44:59 +02:00
|
|
|
|
(default #true))
|
2020-04-28 14:15:28 +02:00
|
|
|
|
(volatile-root? image-volatile-root? ;boolean
|
2022-09-02 19:44:59 +02:00
|
|
|
|
(default #true))
|
2021-12-16 08:45:01 +01:00
|
|
|
|
(shared-store? image-shared-store? ;boolean
|
2022-09-02 19:44:59 +02:00
|
|
|
|
(default #false))
|
2021-12-16 09:25:49 +01:00
|
|
|
|
(shared-network? image-shared-network? ;boolean
|
2022-09-02 19:44:59 +02:00
|
|
|
|
(default #false))
|
2020-04-28 14:15:28 +02:00
|
|
|
|
(substitutable? image-substitutable? ;boolean
|
2022-09-02 19:44:59 +02:00
|
|
|
|
(default #true)))
|
2020-07-31 16:49:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Image type.
|
|
|
|
|
;;;
|
|
|
|
|
|
2022-08-31 14:59:06 +02:00
|
|
|
|
;; The role of this record is to provide a constructor that is able to turn an
|
|
|
|
|
;; <operating-system> record into an <image> record. Some basic <image-type>
|
|
|
|
|
;; records are defined in the (gnu system image) module. They are able to
|
|
|
|
|
;; turn an <operating-system> record into an EFI or an ISO 9660 bootable
|
|
|
|
|
;; image, a Docker image or even a QCOW2 image.
|
|
|
|
|
;;
|
|
|
|
|
;; Other <image-type> records are defined in the (gnu system images ...)
|
|
|
|
|
;; modules. They are dedicated to specific machines such as Novena and Pine64
|
|
|
|
|
;; SoC boards that require specific images.
|
|
|
|
|
;;
|
|
|
|
|
;; All the available <image-type> records are collected by the 'image-modules'
|
|
|
|
|
;; procedure. This allows the "guix system image" command to turn a given
|
|
|
|
|
;; <operating-system> record into an image, thanks to the specified
|
|
|
|
|
;; <image-type>. In that case, the <image-type> look up is done using the
|
|
|
|
|
;; name field of the <image-type> record.
|
|
|
|
|
|
2020-07-31 16:49:27 +02:00
|
|
|
|
(define-record-type* <image-type>
|
|
|
|
|
image-type make-image-type
|
|
|
|
|
image-type?
|
|
|
|
|
(name image-type-name) ;symbol
|
|
|
|
|
(constructor image-type-constructor)) ;<operating-system> -> <image>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Image creation.
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
(define* (os->image os #:key type)
|
2022-08-31 14:59:06 +02:00
|
|
|
|
"Use the image constructor from TYPE, an <image-type> record to turn the
|
|
|
|
|
given OS, an <operating-system> record into an image and return it."
|
2020-07-31 16:49:27 +02:00
|
|
|
|
(let ((constructor (image-type-constructor type)))
|
|
|
|
|
(constructor os)))
|
2021-08-30 18:24:27 +02:00
|
|
|
|
|
|
|
|
|
(define* (os+platform->image os platform #:key type)
|
2022-08-31 14:59:06 +02:00
|
|
|
|
"Use the image constructor from TYPE, an <image-type> record to turn the
|
|
|
|
|
given OS, an <operating-system> record into an image targeting PLATFORM, a
|
|
|
|
|
<platform> record and return it."
|
2021-08-30 18:24:27 +02:00
|
|
|
|
(image
|
|
|
|
|
(inherit (os->image os #:type type))
|
|
|
|
|
(platform platform)))
|