-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup
executable file
·243 lines (220 loc) · 7.63 KB
/
setup
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env -S guix repl --
!#
;;; Copyright © 2023 aurtzy <aurtzy@gmail.com>
;;;
;;; This file is NOT part of GNU Guix.
;;;
;;; This program 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.
;;;
;;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;;
;;; This script is useful for setting up initial installation tasks for Guix
;;; System.
(use-modules (guix build utils)
(guix scripts system)
(ice-9 exceptions)
(ice-9 getopt-long)
(ice-9 match)
(ice-9 popen)
(ice-9 pretty-print)
(ice-9 regex)
(ice-9 textual-ports))
(define PROGRAM (car (program-arguments)))
(define GUIX_CONFIG_DIR (dirname (dirname PROGRAM)))
(define CONFIG-FILE (string-append GUIX_CONFIG_DIR "/system.scm"))
(define (format-usage . args)
(format #f
"Usage: ~a ~a"
(basename PROGRAM)
(string-join args " ")))
(define (display-help . lines)
(display (string-join lines "\n" 'suffix))
(exit 1))
(define (need-help? args)
(or (<= (length args) 1)
(member "--help" args)
(member "-h" args)))
(define (display-swapfile-configuration path)
(pretty-print
`(swapfile-configuration
(file ,path)
(device ,(match:substring
(string-match
"/[^\n]*"
(let* ((port (open-input-pipe
(string-append
"df --output=source "path)))
(str (get-string-all port)))
(close-pipe port)
str))))
(offset ,(match:substring
(string-match
"\n *0:[^:]*: *([0-9]+)\\."
(let* ((port (open-input-pipe
(string-append
"filefrag -e "path)))
(str (get-string-all port)))
(close-pipe port)
str))
1)))))
(define (setup-swapfile-help)
(display-help
(format-usage "install [OPTION ...] PATH")
"Set up swapfile for Guix System. Outputs information to be included in"
"system configuration."
""
"Options:"
" --type=FILESYSTEM"
" Set up swapfile for FILESYSTEM type. This option is required."
" Supported file systems: btrfs"
" --size=SIZE"
" Size of the swapfile in MiB. This option is required."))
(define (setup-swapfile-parse-options args)
(getopt-long args
`((type (value #t)
(predicate ,(lambda (value)
(member value
'("btrfs"))))
(required? #t))
(size (value #t)
(predicate ,(lambda (value)
(false-if-exception
(exact? (string->number value)))))
(required? #t)))))
(define (setup-swapfile args)
(when (need-help? args)
(setup-swapfile-help))
(let* ((options (setup-swapfile-parse-options args))
(cmd-args (option-ref options '() #f))
(type (option-ref options 'type #f))
(size (option-ref options 'size #f)))
;; type isn't actually used here since btrfs is the only type so far, but
;; in the future this setup should make it easier to add more types
(match cmd-args
((path)
(when (file-exists? path)
(delete-file path))
(invoke "touch" path)
(invoke "chattr" "+C" path)
(invoke "dd"
"if=/dev/zero"
(string-append "of=" path)
"bs=1MiB"
(string-append "count=" size))
(chmod path #o600)
(invoke "mkswap" path)
(invoke "swapon" path)
(format (current-error-port) "\n")
(display-swapfile-configuration path))
(else
(setup-swapfile-help)))))
(define (setup-install-help)
(display-help
(format-usage "install [OPTION ...] TARGET")
"Sets up Guix System at TARGET root using the system configuration from"
"the guix-config that this script is located in."
(format #f "(~s)" CONFIG-FILE)
""
"Options: none"))
(define (setup-install-parse-options args)
(getopt-long args
'()))
(define (setup-install args)
(when (need-help? args)
(setup-install-help))
(let* ((options (setup-install-parse-options args))
(cmd-args (option-ref options '() #f)))
(unless (file-exists? CONFIG-FILE)
(format (current-error-port)
"error: config file does not exist: ~s\n"
CONFIG-FILE)
(exit #f))
(match cmd-args
((target)
(invoke "herd" "start" "cow-store" (canonicalize-path target))
(guix-system "init"
;; Enable loading custom guix-config modules
"-L" (string-append GUIX_CONFIG_DIR "/modules")
CONFIG-FILE
target))
(else
(setup-install-help)))))
(define (display-luks-keyfile-configuration keyfile)
(pretty-print
`(luks-device-mapping-with-options
#:key-file ,keyfile)))
(define (setup-luks-keyfile-help)
(display-help
(format-usage "luks-keyfile UUID")
"Create a keyfile for partition with UUID and add it to the LUKS header."
""
"Options: none"))
(define (setup-luks-keyfile-parse-options args)
(getopt-long args
'()))
(define (setup-luks-keyfile args)
(when (need-help? args)
(setup-luks-keyfile-help))
(let* ((options (setup-luks-keyfile-parse-options args))
(cmd-args (option-ref options '() #f)))
(match cmd-args
((uuid)
(let ((target-partition (string-append "/dev/disk/by-uuid/" uuid))
(keyfile (string-append "/root/keys/" uuid)))
(unless (file-exists? target-partition)
(format (current-error-port)
"error: Partition does not exist: ~s\n"
target-partition)
(exit #f))
(invoke "dd"
"bs=512"
"count=4"
"if=/dev/random"
(string-append "of=" keyfile)
"iflag=fullblock")
(invoke "guix"
"shell"
"cryptsetup"
"--"
"cryptsetup"
"luksAddKey"
target-partition
keyfile)
(format (current-error-port) "\n")
(display-luks-keyfile-configuration keyfile)))
(else
(setup-luks-keyfile-help)))))
(define (setup-help)
(display-help
(format-usage "COMMAND ARG ...")
"Set up various tasks during a Guix System installation."
""
"Commands:"
" swapfile"
" install"
;; TODO: remove experimental tag once it's field-tested
" luks-keyfile (experimental)"))
(define (main args)
(let ((cmd-args (if (null? args)
args
(cdr args))))
(match cmd-args
(("swapfile" _ ...)
(setup-swapfile cmd-args))
(("install" _ ...)
(setup-install cmd-args))
(("luks-keyfile" _ ...)
(setup-luks-keyfile cmd-args))
(else
(setup-help)))))
(main (program-arguments))