2020-04-28 12:15:28 +00:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2022-08-30 17:18:26 +00:00
|
|
|
|
;;; Copyright © 2020, 2022 Mathieu Othacehe <othacehe@gnu.org>
|
2020-04-28 12:15:28 +00: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 20:37:26 +00:00
|
|
|
|
#:use-module (guix platform)
|
2020-04-28 12:15:28 +00:00
|
|
|
|
#:use-module (guix records)
|
2022-07-01 10:06:39 +00:00
|
|
|
|
#:use-module (guix diagnostics)
|
|
|
|
|
#:use-module (guix i18n)
|
2022-08-30 17:18:26 +00:00
|
|
|
|
#:use-module (srfi srfi-1)
|
2022-07-01 10:06:39 +00:00
|
|
|
|
#:use-module (srfi srfi-34)
|
|
|
|
|
#:use-module (srfi srfi-35)
|
2020-04-28 12:15:28 +00:00
|
|
|
|
#:export (partition
|
|
|
|
|
partition?
|
|
|
|
|
partition-device
|
|
|
|
|
partition-size
|
2020-05-20 14:09:53 +00:00
|
|
|
|
partition-offset
|
2020-04-28 12:15:28 +00:00
|
|
|
|
partition-file-system
|
2020-05-26 14:25:25 +00:00
|
|
|
|
partition-file-system-options
|
2020-04-28 12:15:28 +00:00
|
|
|
|
partition-label
|
|
|
|
|
partition-uuid
|
|
|
|
|
partition-flags
|
|
|
|
|
partition-initializer
|
|
|
|
|
|
|
|
|
|
image
|
2021-01-20 11:06:56 +00:00
|
|
|
|
image?
|
2020-04-28 12:15:28 +00:00
|
|
|
|
image-name
|
|
|
|
|
image-format
|
2021-08-30 16:24:27 +00:00
|
|
|
|
image-platform
|
2020-04-28 12:15:28 +00:00
|
|
|
|
image-size
|
|
|
|
|
image-operating-system
|
2021-11-04 08:35:11 +00:00
|
|
|
|
image-partition-table-type
|
2020-04-28 12:15:28 +00:00
|
|
|
|
image-partitions
|
|
|
|
|
image-compression?
|
|
|
|
|
image-volatile-root?
|
2021-12-16 07:45:01 +00:00
|
|
|
|
image-shared-store?
|
2021-12-16 08:25:49 +00:00
|
|
|
|
image-shared-network?
|
2020-07-31 14:49:27 +00:00
|
|
|
|
image-substitutable?
|
|
|
|
|
|
|
|
|
|
image-type
|
|
|
|
|
image-type?
|
|
|
|
|
image-type-name
|
|
|
|
|
image-type-constructor
|
|
|
|
|
|
2021-08-30 16:24:27 +00:00
|
|
|
|
os->image
|
|
|
|
|
os+platform->image))
|
2020-04-28 12:15:28 +00:00
|
|
|
|
|
2022-08-30 17:18:26 +00:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Sanitizers.
|
|
|
|
|
;;;
|
|
|
|
|
|
2022-08-31 12:59:06 +00: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 17:18:26 +00: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 12:15:28 +00:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Partition record.
|
|
|
|
|
;;;
|
|
|
|
|
|
2022-08-31 12:59:06 +00:00
|
|
|
|
;; The partition offset should be a bytes count as an integer.
|
2022-08-30 17:18:26 +00: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 12:59:06 +00:00
|
|
|
|
;; The supported partition flags.
|
2022-08-30 17:18:26 +00: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 12:15:28 +00:00
|
|
|
|
(define-record-type* <partition> partition make-partition
|
|
|
|
|
partition?
|
2022-08-30 17:18:26 +00:00
|
|
|
|
(size partition-size ;size in bytes as integer or 'guess
|
|
|
|
|
(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 14:25:25 +00:00
|
|
|
|
(file-system-options partition-file-system-options
|
2022-08-30 17:18:26 +00:00
|
|
|
|
(default '())) ;list of strings
|
|
|
|
|
(label partition-label) ;string
|
|
|
|
|
(uuid partition-uuid
|
|
|
|
|
(default #f)) ;<uuid>
|
|
|
|
|
(flags partition-flags
|
|
|
|
|
(default '()) ;list of symbols
|
|
|
|
|
(sanitize validate-partition-flags))
|
|
|
|
|
(initializer partition-initializer
|
|
|
|
|
(default #f))) ;gexp | #f
|
2020-04-28 12:15:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Image record.
|
|
|
|
|
;;;
|
|
|
|
|
|
2022-07-01 10:06:39 +00: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 12:59:06 +00:00
|
|
|
|
;; The supported image formats.
|
2022-07-01 10:06:39 +00:00
|
|
|
|
(define-set-sanitizer validate-image-format format
|
|
|
|
|
(disk-image compressed-qcow2 docker iso9660))
|
2022-08-31 12:59:06 +00:00
|
|
|
|
|
|
|
|
|
;; The supported partition table types.
|
2022-07-01 10:06:39 +00:00
|
|
|
|
(define-set-sanitizer validate-partition-table-type partition-table-type
|
|
|
|
|
(mbr gpt))
|
|
|
|
|
|
2020-04-28 12:15:28 +00:00
|
|
|
|
(define-record-type* <image>
|
|
|
|
|
image make-image
|
|
|
|
|
image?
|
2020-06-13 15:33:10 +00:00
|
|
|
|
(name image-name ;symbol
|
|
|
|
|
(default #f))
|
2022-07-01 10:06:39 +00:00
|
|
|
|
(format image-format ;symbol
|
|
|
|
|
(sanitize validate-image-format))
|
2021-08-30 16:24:27 +00:00
|
|
|
|
(platform image-platform ;<platform>
|
2020-06-13 12:01:18 +00:00
|
|
|
|
(default #f))
|
2020-04-28 12:15:28 +00:00
|
|
|
|
(size image-size ;size in bytes as integer
|
2022-08-30 17:18:26 +00:00
|
|
|
|
(default 'guess)
|
|
|
|
|
(sanitize validate-size))
|
2020-04-28 12:15:28 +00:00
|
|
|
|
(operating-system image-operating-system ;<operating-system>
|
|
|
|
|
(default #f))
|
2021-11-04 08:35:11 +00:00
|
|
|
|
(partition-table-type image-partition-table-type ; 'mbr or 'gpt
|
2022-07-01 10:06:39 +00:00
|
|
|
|
(default 'mbr)
|
|
|
|
|
(sanitize validate-partition-table-type))
|
2020-04-28 12:15:28 +00:00
|
|
|
|
(partitions image-partitions ;list of <partition>
|
|
|
|
|
(default '()))
|
|
|
|
|
(compression? image-compression? ;boolean
|
|
|
|
|
(default #t))
|
|
|
|
|
(volatile-root? image-volatile-root? ;boolean
|
|
|
|
|
(default #t))
|
2021-12-16 07:45:01 +00:00
|
|
|
|
(shared-store? image-shared-store? ;boolean
|
|
|
|
|
(default #f))
|
2021-12-16 08:25:49 +00:00
|
|
|
|
(shared-network? image-shared-network? ;boolean
|
|
|
|
|
(default #f))
|
2020-04-28 12:15:28 +00:00
|
|
|
|
(substitutable? image-substitutable? ;boolean
|
|
|
|
|
(default #t)))
|
2020-07-31 14:49:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Image type.
|
|
|
|
|
;;;
|
|
|
|
|
|
2022-08-31 12:59:06 +00: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 14:49:27 +00: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 12:59:06 +00: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 14:49:27 +00:00
|
|
|
|
(let ((constructor (image-type-constructor type)))
|
|
|
|
|
(constructor os)))
|
2021-08-30 16:24:27 +00:00
|
|
|
|
|
|
|
|
|
(define* (os+platform->image os platform #:key type)
|
2022-08-31 12:59:06 +00: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 16:24:27 +00:00
|
|
|
|
(image
|
|
|
|
|
(inherit (os->image os #:type type))
|
|
|
|
|
(platform platform)))
|