summaryrefslogtreecommitdiff
path: root/px/services/disk.scm
blob: 0e2e5adc2c5544ddfebdfea9ead423e611f6c709 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
;;; Package Repository for GNU Guix
;;; Copyright © 2021-2023 Reza Alizadeh Majd <r.majd@pantherx.org>
;;; Copyright © 2021-2025 Franz Geffke <m@f-a.nz>

(define-module (px services disk)
  #:use-module (gnu packages admin)
  #:use-module (gnu packages base)
  #:use-module (gnu packages linux)
  #:use-module (gnu packages xdisorg)
  #:use-module (gnu services)
  #:use-module (gnu services base)
  #:use-module (gnu services cups)
  #:use-module (gnu services dbus)
  #:use-module (gnu services desktop)
  #:use-module (gnu services networking)
  #:use-module (gnu services shepherd)
  #:use-module (gnu system)
  #:use-module (gnu system setuid)
  #:use-module (guix build utils)
  #:use-module (guix gexp)
  #:use-module (guix packages)
  #:use-module (guix records)
  #:use-module (srfi srfi-1)
  #:use-module (ice-9 match)
  #:export (create-swap-space-service
            disk-init-configuration
            disk-init-service-type))

;;
;; Create swap-file service
;; primarily for ARM
;;

(define (create-swap-space-service size)
  (simple-service 'create-swap-space activation-service-type
                  (with-imported-modules '((guix build utils))
                                         #~(begin
                                             (use-modules (guix build utils))
                                             (let ((swapfile "/swapfile"))
                                               (when (not (file-exists?
                                                           swapfile))
                                                 (invoke #+(file-append
                                                            util-linux
                                                            "/bin/fallocate")
                                                         "-l"
                                                         #$size swapfile)
                                                 (chmod swapfile #o600)
                                                 (invoke #+(file-append
                                                            util-linux
                                                            "/sbin/mkswap")
                                                         swapfile)))))))

;;
;; Disk initiation service
;; primarily for ARM
;;

(define-record-type* <disk-init-configuration> disk-init-configuration
                     make-disk-init-configuration
  disk-init-configuration?
  (device disk-init-configuration-device)
  (index disk-init-configuration-index)
  (target disk-init-configuration-target)
  (swap-size disk-init-configuration-swap-size
             (default #f))
  (swap-path disk-init-configuration-swap-path
             (default "/swapfile")))

(define disk-init-activation
  (match-lambda
    (($ <disk-init-configuration>
        device
        index
        target
        swap-size
        swap-path)
     (with-imported-modules '((guix build utils))
                            #~(begin
                                (use-modules (guix build utils))
                                (let ((lock-file "/etc/disk-init.lock"))
                                  (when (not (file-exists? lock-file))
                                    ;; resize root partition
                                    ;; workaround to fix growpart execution
                                    (setenv "PATH"
                                            (string-append
                                             "/run/current-system/profile/bin:"
                                             (getenv "PATH")))
                                    (invoke #+(file-append cloud-utils
                                                           "/bin/growpart")
                                            #$device
                                            #$index)
                                    (invoke #+(file-append e2fsprogs
                                                           "/sbin/resize2fs")
                                            #$target)
                                    (invoke #+(file-append coreutils
                                                           "/bin/sync"))

                                    ;; create swap-file
                                    (when (and #$swap-size
                                               (not (file-exists? #$swap-path)))
                                      (invoke #+(file-append util-linux
                                                             "/bin/fallocate")
                                              "-l"
                                              #$swap-size
                                              #$swap-path)
                                      (chmod #$swap-path #o600)
                                      (invoke #+(file-append util-linux
                                                             "/sbin/mkswap")
                                              #$swap-path))

                                    (call-with-output-file lock-file
                                      (lambda (port)
                                        (display "disk image initiated\n" port))))))))))

(define disk-init-service-type
  (service-type (name 'disk-init)
                (extensions (list (service-extension activation-service-type
                                                     disk-init-activation)))
                (description
                 "Resize root partition on first boot and create swapfile")))