(define-module (services resize-fs) #:use-module (guix gexp) #:use-module (guix records) #:use-module (gnu packages bash) #:use-module (gnu packages disk) ; parted #:use-module (gnu packages linux); e2fsprogs #:use-module (guix build utils) #:use-module (guix packages) #:use-module (gnu services) #:use-module (gnu services configuration) #:use-module (gnu services shepherd) #:export (resize-fs-configuration resize-fs-configuration? resize-fs-configuration-parted resize-fs-configuration-e2fsprogs resize-fs-configuration-device resize-fs-configuration-partition resize-fs-configuration-end resize-fs-service-type)) (define-configuration/no-serialization resize-fs-configuration (parted (file-like parted) "The parted package to use.") (e2fsprogs (file-like e2fsprogs) "The e2fsprogs package providing the resize2fs utility.") (device (string "/dev/mmcblk0") ;; #f may be preferrable here to prevent accidental resizing of wrong file-system "The device containing the partition to be resized.") (partition (number 2) "The partition number that is to be resized.") (end (string "100%") "The end position of the resized partition as understood by the parted \ utility (e.g. \"100%\", \"500M\" or \"16GiB\").")) (define (resize-fs-script config) (match-record config (parted e2fsprogs device partition end) (let ((parted-bin (file-append parted "/sbin/parted")) (resize2fs (file-append e2fsprogs "/sbin/resize2fs")) (device+partition (string-append device "p" (number->string partition)))) (mixed-text-file "resize-fs.sh" "#!/bin/sh echoerr() { printf \"$*\\n\" >&2 ; } cmd() { " parted-bin " " device " ---pretend-input-tty <string partition) " Yes " end " EOF } set -o errexit set -o pipefail echoerr hello from resize-fs script if cmd; then echoerr \"Resizing successful\" else echoerr \"resize-script returned $?\" fi ")))) (define (resize-fs-shepherd-service config) "Return a list of for resize-fs-service for CONFIG" (let ((resize-script (resize-fs-script config))) (shepherd-service (documentation "Resize a file-system. Intended for Guix Systems that are booted from an image") (provision '(resize-fs)) (requirement '(user-processes)) (one-shot? #t) (respawn? #f) (start #~(make-forkexec-constructor (list #$(file-append bash "/bin/sh") #$resize-script)))))) (define resize-fs-service-type (service-type (name 'resize-fs) (description "Resize a partition.") (extensions (list (service-extension shepherd-root-service-type (compose list resize-fs-shepherd-service)))) (default-value (resize-fs-configuration))))