From patchwork Mon Feb 12 23:21:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gabriel Wicki X-Patchwork-Id: 60255 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 1D26C27BBE9; Tue, 13 Feb 2024 02:41:27 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id B683427BBE2 for ; Tue, 13 Feb 2024 02:41:24 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rZij7-0001U7-HN; Mon, 12 Feb 2024 21:40:53 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rZij0-0001R6-6h for guix-patches@gnu.org; Mon, 12 Feb 2024 21:40:46 -0500 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rZiiz-0002tW-Sm for guix-patches@gnu.org; Mon, 12 Feb 2024 21:40:45 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rZijG-0004Pb-Vt for guix-patches@gnu.org; Mon, 12 Feb 2024 21:41:03 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#69090] [PATCH] gnu: services: Add resize-fs-service. Resent-From: Gabriel Wicki Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 13 Feb 2024 02:41:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 69090 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 69090@debbugs.gnu.org X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.170779202716858 (code B ref -1); Tue, 13 Feb 2024 02:41:02 +0000 Received: (at submit) by debbugs.gnu.org; 13 Feb 2024 02:40:27 +0000 Received: from localhost ([127.0.0.1]:38411 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rZiig-0004Ni-D7 for submit@debbugs.gnu.org; Mon, 12 Feb 2024 21:40:27 -0500 Received: from lists.gnu.org ([209.51.188.17]:39802) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rZiWw-0003rl-63 for submit@debbugs.gnu.org; Mon, 12 Feb 2024 21:28:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rZfc5-0006Zz-Gl for guix-patches@gnu.org; Mon, 12 Feb 2024 18:21:25 -0500 Received: from cotopaxi.ee.ethz.ch ([129.132.148.196]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rZfc3-0004KI-Bc for guix-patches@gnu.org; Mon, 12 Feb 2024 18:21:25 -0500 Received: from localhost (antispam.ee.ethz.ch [129.132.2.16]) by cotopaxi.ee.ethz.ch (Postfix) with ESMTP id 493301FE40 for ; Tue, 13 Feb 2024 00:21:07 +0100 (CET) X-Virus-Scanned: by amavisd at antispam.ee.ethz.ch Received: from cotopaxi.ee.ethz.ch ([129.132.148.196]) by localhost (antispam.ee.ethz.ch [129.132.2.16]) (amavisd-new, port 10028) with ESMTP id BXgq4H3Bg5Kz for ; Tue, 13 Feb 2024 00:21:05 +0100 (CET) Received: from blackbox (212-51-128-25.fiber7.init7.net [212.51.128.25]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: gabriel) by cotopaxi.ee.ethz.ch (Postfix) with ESMTPSA for ; Tue, 13 Feb 2024 00:21:05 +0100 (CET) Date: Tue, 13 Feb 2024 00:21:02 +0100 From: Gabriel Wicki Message-ID: MIME-Version: 1.0 Content-Disposition: inline Received-SPF: permerror client-ip=129.132.148.196; envelope-from=gabriel@erlikon.ch; helo=cotopaxi.ee.ethz.ch X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, T_SCC_BODY_TEXT_LINE=-0.01, T_SPF_PERMERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches Aloha and hello As promised a while back here is my patch adding a resize filesystem service that would typically be used either when flashing images onto an SD card or using an uploadable image on a VPS. Thanks for review and merge in advance, gabber From 9b44e851252445bc628d70eb9e26574455620caf Mon Sep 17 00:00:00 2001 Message-ID: <9b44e851252445bc628d70eb9e26574455620caf.1707779839.git.gabriel@erlikon.ch> From: Gabriel Wicki Date: Tue, 13 Feb 2024 00:07:24 +0100 Subject: [PATCH] gnu: services: Add resize-fs-service. * gnu/services/admin.scm (resize-fs-configuration): New configuration type. (resize-fs-script, resize-fs-shepherd-service): New procedures. resize-fs-service-type): New variable. * doc/guix.texi (Miscallaneous Services): Document it. Change-Id: Ib80c1af99ff62b68a79d7c463a5173f530514227 --- doc/guix.texi | 41 +++++++++++++++++++ gnu/services/admin.scm | 89 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 129 insertions(+), 1 deletion(-) base-commit: bb4f0509b7cce750fc944e604aa919ea89910ea7 diff --git a/doc/guix.texi b/doc/guix.texi index 04119a5955..093a35a331 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -40746,6 +40746,47 @@ Miscellaneous Services @c End of auto-generated fail2ban documentation. +@cindex resize-fs +@subsubheading Resize Filesystem service + +This service type lets you resize a live file-system during boot, which +can be convenient if you flashed Guix on an SD Card (e.g. for an +embedded device) or uploaded the image to a VPS. In both cases the +medium the image will reside upon may be larger than the image you want +to produce. +For an embedded device booting from an SD card you may use something like: +@lisp +(service resize-fs-service-type + (resize-fs-configuration + (device "/dev/mmcblk0") + (partition 2))) +@end lisp + +Be extra cautious to use the correct device, partiion and end value, for +the service will circumvent parted's safety checks - wrong use could end +in loss of data or the corruption of your operating system. + +@table @asis + +@item @code{parted} (default: @code{parted}) (type: file-like) +The parted package to use. + +@item @code{e2fsprogs} (default: @code{e2fsprogs}) (type: file-like) +The e2fsprogs package to use. + +@item @code{device} (default: @code{"/dev/sdZ"}) (type: string) +The device containing the file-system that shall be resized. + +@item @code{partition} (default: @code{-1}) (type: number) +The partition number of the file-system that shall be resized. + +@item @code{end} (default: @code{"100%"}) (type: string) +The end position of the resized partition as understood by the parted +utility (e.g. "100%", "500M" or "16GiB"). + +@end table + + @node Setuid Programs @section Setuid Programs diff --git a/gnu/services/admin.scm b/gnu/services/admin.scm index 0b325fddb1..acd7cacd3e 100644 --- a/gnu/services/admin.scm +++ b/gnu/services/admin.scm @@ -23,8 +23,11 @@ (define-module (gnu services admin) #:use-module (gnu packages admin) #:use-module ((gnu packages base) #:select (canonical-package findutils coreutils sed)) + #:use-module (gnu packages bash) #:use-module (gnu packages certs) + #:use-module (gnu packages disk) #:use-module (gnu packages package-management) + #:use-module (gnu packages linux) #:use-module (gnu services) #:use-module (gnu services configuration) #:use-module (gnu services mcron) @@ -93,7 +96,16 @@ (define-module (gnu services admin) unattended-upgrade-configuration-services-to-restart unattended-upgrade-configuration-system-expiration unattended-upgrade-configuration-maximum-duration - unattended-upgrade-configuration-log-file)) + unattended-upgrade-configuration-log-file + + 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)) ;;; Commentary: ;;; @@ -537,4 +549,79 @@ (define unattended-upgrade-service-type "Periodically upgrade the system from the current configuration.") (default-value (unattended-upgrade-configuration)))) + +;;; +;;; Resize filesystem. +;;; + +(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/sdZ") + "The device containing the partition to be resized.") + (partition + (number -1) + "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 + +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 a system image flashed onto a larger medium.") + (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 during boot.") + (extensions + (list + (service-extension shepherd-root-service-type + (compose list resize-fs-shepherd-service)))) + (default-value (resize-fs-configuration)))) + ;;; admin.scm ends here