summary refs log tree commit diff
path: root/gnu/system/image.scm
diff options
context:
space:
mode:
authorVagrant Cascadian <vagrant@debian.org>2021-01-25 16:08:07 -0800
committerVagrant Cascadian <vagrant@debian.org>2021-01-25 16:08:35 -0800
commitd8cc2683d00d975dea85a0958584cae26ff2c31c (patch)
tree9f9a3340b617677fad6d62200687056efb711032 /gnu/system/image.scm
parent47a5442aa7dad8b1904483954e91640c3cac5e90 (diff)
parent59c03bd4f9aba7ccd90428508ad072f8db01b9ed (diff)
downloadguix-d8cc2683d00d975dea85a0958584cae26ff2c31c.tar.gz
Merge branch 'master' into wip-pinebook-pro
Diffstat (limited to 'gnu/system/image.scm')
-rw-r--r--gnu/system/image.scm197
1 files changed, 163 insertions, 34 deletions
diff --git a/gnu/system/image.scm b/gnu/system/image.scm
index 36f56e237d..1012fa6158 100644
--- a/gnu/system/image.scm
+++ b/gnu/system/image.scm
@@ -18,6 +18,8 @@
 ;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.
 
 (define-module (gnu system image)
+  #:use-module (guix diagnostics)
+  #:use-module (guix discovery)
   #:use-module (guix gexp)
   #:use-module (guix modules)
   #:use-module (guix monads)
@@ -47,11 +49,14 @@
   #:use-module (gnu packages hurd)
   #:use-module (gnu packages linux)
   #:use-module (gnu packages mtools)
+  #:use-module (gnu packages virtualization)
   #:use-module ((srfi srfi-1) #:prefix srfi-1:)
   #:use-module (srfi srfi-11)
   #:use-module (srfi srfi-26)
+  #:use-module (srfi srfi-34)
   #:use-module (srfi srfi-35)
   #:use-module (rnrs bytevectors)
+  #:use-module (ice-9 format)
   #:use-module (ice-9 match)
   #:export (root-offset
             root-label
@@ -61,9 +66,22 @@
 
             efi-disk-image
             iso9660-image
+            arm32-disk-image
+            arm64-disk-image
 
-            find-image
-            system-image))
+            image-with-os
+            efi-raw-image-type
+            qcow2-image-type
+            iso-image-type
+            uncompressed-iso-image-type
+            arm32-image-type
+            arm64-image-type
+
+            image-with-label
+            system-image
+
+            %image-types
+            lookup-image-type-by-name))
 
 
 ;;;
@@ -110,6 +128,74 @@
            (label "GUIX_IMAGE")
            (flags '(boot)))))))
 
+(define* (arm32-disk-image #:optional (offset root-offset))
+  (image
+   (format 'disk-image)
+   (target "arm-linux-gnueabihf")
+   (partitions
+    (list (partition
+           (inherit root-partition)
+           (offset offset))))
+   ;; FIXME: Deleting and creating "/var/run" and "/tmp" on the overlayfs
+   ;; fails.
+   (volatile-root? #f)))
+
+(define* (arm64-disk-image #:optional (offset root-offset))
+  (image
+   (inherit (arm32-disk-image offset))
+   (target "aarch64-linux-gnu")))
+
+
+;;;
+;;; Images types.
+;;;
+
+(define-syntax-rule (image-with-os base-image os)
+  "Return an image inheriting from BASE-IMAGE, with the operating-system field
+set to the given OS."
+  (image
+   (inherit base-image)
+   (operating-system os)))
+
+(define efi-raw-image-type
+  (image-type
+   (name 'efi-raw)
+   (constructor (cut image-with-os efi-disk-image <>))))
+
+(define qcow2-image-type
+  (image-type
+   (name 'qcow2)
+   (constructor (cut image-with-os
+                 (image
+                  (inherit efi-disk-image)
+                  (name 'image.qcow2)
+                  (format 'compressed-qcow2))
+                 <>))))
+
+(define iso-image-type
+  (image-type
+   (name 'iso9660)
+   (constructor (cut image-with-os iso9660-image <>))))
+
+(define uncompressed-iso-image-type
+  (image-type
+   (name 'uncompressed-iso9660)
+   (constructor (cut image-with-os
+                 (image
+                  (inherit iso9660-image)
+                  (compression? #f))
+                 <>))))
+
+(define arm32-image-type
+  (image-type
+   (name 'arm32-raw)
+   (constructor (cut image-with-os (arm32-disk-image) <>))))
+
+(define arm64-image-type
+  (image-type
+   (name 'arm64-raw)
+   (constructor (cut image-with-os (arm64-disk-image) <>))))
+
 
 ;;
 ;; Helpers.
@@ -148,6 +234,7 @@
     (with-imported-modules `(,@(source-module-closure
                                 '((gnu build vm)
                                   (gnu build image)
+                                  (gnu build bootloader)
                                   (gnu build hurd-boot)
                                   (gnu build linux-boot)
                                   (guix store database))
@@ -156,6 +243,7 @@
       #~(begin
           (use-modules (gnu build vm)
                        (gnu build image)
+                       (gnu build bootloader)
                        (gnu build hurd-boot)
                        (gnu build linux-boot)
                        (guix store database)
@@ -206,8 +294,8 @@ used in the image."
     (define (format->image-type format)
       ;; Return the genimage format corresponding to FORMAT.  For now, only
       ;; the hdimage format (raw disk-image) is supported.
-      (case format
-        ((disk-image) "hdimage")
+      (cond
+       ((memq format '(disk-image compressed-qcow2)) "hdimage")
         (else
          (raise (condition
                  (&message
@@ -266,6 +354,9 @@ used in the image."
                                        #$output
                                        image-root)))))
         (computed-file "partition.img" image-builder
+                       ;; Allow offloading so that this I/O-intensive process
+                       ;; doesn't run on the build farm's head node.
+                       #:local-build? #f
                        #:options `(#:references-graphs ,inputs))))
 
     (define (partition->config partition)
@@ -305,25 +396,25 @@ image ~a {
          (name (if image-name
                    (symbol->string image-name)
                    name))
+         (format (image-format image))
          (substitutable? (image-substitutable? image))
          (builder
           (with-imported-modules*
-           (let ((inputs '#+(list genimage coreutils findutils))
+           (let ((inputs '#+(list genimage coreutils findutils qemu-minimal))
                  (bootloader-installer
-                  #+(bootloader-disk-image-installer bootloader)))
+                  #+(bootloader-disk-image-installer bootloader))
+                 (out-image (string-append "images/" #$genimage-name)))
              (set-path-environment-variable "PATH" '("bin" "sbin") inputs)
-             (genimage #$(image->genimage-cfg image) #$output)
+             (genimage #$(image->genimage-cfg image))
              ;; Install the bootloader directly on the disk-image.
              (when bootloader-installer
                (bootloader-installer
                 #+(bootloader-package bootloader)
                 #$(root-partition-index image)
-                (string-append #$output "/" #$genimage-name))))))
-         (image-dir (computed-file "image-dir" builder)))
-    (computed-file name
-                   #~(symlink
-                      (string-append #$image-dir "/" #$genimage-name)
-                      #$output)
+                out-image))
+             (convert-disk-image out-image '#$format #$output)))))
+    (computed-file name builder
+                   #:local-build? #f              ;too I/O-intensive
                    #:options `(#:substitutable? ,substitutable?))))
 
 
@@ -339,7 +430,7 @@ image ~a {
 
 (define* (system-iso9660-image image
                                #:key
-                               (name "iso9660-image")
+                               (name "image.iso")
                                bootcfg
                                bootloader
                                register-closures?
@@ -401,9 +492,26 @@ used in the image. "
                                  #:volume-id #$root-label
                                  #:volume-uuid #$root-uuid)))))
     (computed-file name builder
+                   ;; Allow offloading so that this I/O-intensive process
+                   ;; doesn't run on the build farm's head node.
+                   #:local-build? #f
                    #:options `(#:references-graphs ,inputs
                                #:substitutable? ,substitutable?))))
 
+(define (image-with-label base-image label)
+  "The volume ID of an ISO is the label of the first partition.  This procedure
+returns an image record where the first partition's label is set to <label>."
+  (image
+    (inherit base-image)
+    (partitions
+      (match (image-partitions base-image)
+        ((boot others ...)
+         (cons
+           (partition
+             (inherit boot)
+             (label label))
+           others))))))
+
 
 ;;
 ;; Image creation.
@@ -426,7 +534,7 @@ used in the image. "
       image-size)
      (else root-size))))
 
-(define* (image-with-os base-image os)
+(define* (image-with-os* base-image os)
   "Return an image based on BASE-IMAGE but with the operating-system field set
 to OS.  Also set the UUID and the size of the root partition."
   (define root-file-system
@@ -452,7 +560,9 @@ to OS.  Also set the UUID and the size of the root partition."
   "Return an operating-system based on the one specified in IMAGE, but
 suitable for image creation.  Assign an UUID to the root file-system, so that
 it can be used for bootloading."
-  (define volatile-root? (image-volatile-root? image))
+  (define volatile-root? (if (eq? (image-format image) 'iso9660)
+                             #t
+                             (image-volatile-root? image)))
 
   (define (root-uuid os)
     ;; UUID of the root file system, computed in a deterministic fashion.
@@ -468,7 +578,9 @@ it can be used for bootloading."
          (file-systems-to-keep
           (srfi-1:remove
            (lambda (fs)
-             (string=? (file-system-mount-point fs) "/"))
+             (let ((mount-point (file-system-mount-point fs)))
+               (or (string=? mount-point "/")
+                   (string=? mount-point "/boot/efi"))))
            (operating-system-file-systems base-os)))
          (format (image-format image))
          (os
@@ -507,20 +619,21 @@ image, depending on IMAGE format."
 
   (with-parameters ((%current-target-system target))
     (let* ((os (operating-system-for-image image))
-           (image* (image-with-os image os))
+           (image* (image-with-os* image os))
+           (image-format (image-format image))
            (register-closures? (has-guix-service-type? os))
            (bootcfg (operating-system-bootcfg os))
            (bootloader (bootloader-configuration-bootloader
                         (operating-system-bootloader os))))
-      (case (image-format image)
-        ((disk-image)
+      (cond
+       ((memq image-format '(disk-image compressed-qcow2))
          (system-disk-image image*
                             #:bootcfg bootcfg
                             #:bootloader bootloader
                             #:register-closures? register-closures?
                             #:inputs `(("system" ,os)
                                        ("bootcfg" ,bootcfg))))
-        ((iso9660)
+       ((memq image-format '(iso9660))
          (system-iso9660-image
           image*
           #:bootcfg bootcfg
@@ -539,18 +652,34 @@ image, depending on IMAGE format."
           #:grub-mkrescue-environment
           '(("MKRESCUE_SED_MODE" . "mbr_only"))))))))
 
-(define (find-image file-system-type target)
-  "Find and return an image built that could match the given FILE-SYSTEM-TYPE,
-built for TARGET.  This is useful to adapt to interfaces written before the
-addition of the <image> record."
-  (match file-system-type
-    ("iso9660" iso9660-image)
-    (_ (cond
-        ((and target
-              (hurd-triplet? target))
-         (module-ref (resolve-interface '(gnu system images hurd))
-                     'hurd-disk-image))
-        (else
-         efi-disk-image)))))
+
+;;
+;; Image detection.
+;;
+
+(define (image-modules)
+  "Return the list of image modules."
+  (cons (resolve-interface '(gnu system image))
+        (all-modules (map (lambda (entry)
+                            `(,entry . "gnu/system/images/"))
+                          %load-path)
+                     #:warn warn-about-load-error)))
+
+(define %image-types
+  ;; The list of publically-known image types.
+  (delay (fold-module-public-variables (lambda (obj result)
+                                         (if (image-type? obj)
+                                             (cons obj result)
+                                             result))
+                                       '()
+                                       (image-modules))))
+
+(define (lookup-image-type-by-name name)
+  "Return the image type called NAME."
+  (or (srfi-1:find (lambda (image-type)
+                     (eq? name (image-type-name image-type)))
+                   (force %image-types))
+      (raise
+       (formatted-message (G_ "~a: no such image type") name))))
 
 ;;; image.scm ends here