From patchwork Thu May 23 20:07:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sent X-Patchwork-Id: 64857 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 0C4EA27BBE9; Thu, 23 May 2024 21:19:24 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,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 615B127BBE2 for ; Thu, 23 May 2024 21:19:21 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sAEtv-0008Te-SZ; Thu, 23 May 2024 16:18:59 -0400 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 1sAEtu-0008R9-7G for guix-patches@gnu.org; Thu, 23 May 2024 16:18:58 -0400 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 1sAEtt-0002CD-Rv; Thu, 23 May 2024 16:18:57 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sAEtx-00075b-UC; Thu, 23 May 2024 16:19:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#69090] [PATCH v2] gnu: services: Add resize-fs-service. References: In-Reply-To: Resent-From: Richard Sent Original-Sender: "Debbugs-submit" Resent-CC: pelzflorian@pelzflorian.de, ludo@gnu.org, matt@excalamus.com, maxim.cournoyer@gmail.com, guix-patches@gnu.org Resent-Date: Thu, 23 May 2024 20:19:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 69090 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 69090@debbugs.gnu.org Cc: Gabriel Wicki , Florian Pelz , Ludovic =?utf-8?q?Court=C3=A8s?= , Matthew Trzcinski , Maxim Cournoyer X-Debbugs-Original-Xcc: Florian Pelz , Ludovic =?utf-8?q?Court=C3=A8s?= , Matthew Trzcinski , Maxim Cournoyer Received: via spool by 69090-submit@debbugs.gnu.org id=B69090.171649552727243 (code B ref 69090); Thu, 23 May 2024 20:19:01 +0000 Received: (at 69090) by debbugs.gnu.org; 23 May 2024 20:18:47 +0000 Received: from localhost ([127.0.0.1]:60772 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sAEti-00075L-9r for submit@debbugs.gnu.org; Thu, 23 May 2024 16:18:46 -0400 Received: from mail-108-mta242.mxroute.com ([136.175.108.242]:42429) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sAEtc-00075F-Gk for 69090@debbugs.gnu.org; Thu, 23 May 2024 16:18:44 -0400 Received: from filter006.mxroute.com ([136.175.111.3] filter006.mxroute.com) (Authenticated sender: mN4UYu2MZsgR) by mail-108-mta242.mxroute.com (ZoneMTA) with ESMTPSA id 18fa71aec9a000efce.001 for <69090@debbugs.gnu.org> (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384); Thu, 23 May 2024 20:18:30 +0000 X-Zone-Loop: 6cf7acee79be6471acf49c2b560a8a6b2e1f70707238 X-Originating-IP: [136.175.111.3] DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=freakingpenguin.com; s=x; h=Content-Transfer-Encoding:MIME-Version: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=Z96Gcmw1oEuVffEwTjG+Wz6jyBWMN1L8rElZWDUzc6I=; b=jdd1bqq+P/ctRT0K/wh+tV5MdG 6k6L42aof1BiaBxpaUV5TGbOMyMTOuISrDNwUirGM52T08giJuT1qv9e47xU5RxE2CSzUmbDspaWl vLw3xrE80LV4rEcNknBfLWNE1TfJr5FlM8LJUrdm/A4mMHN+BL46fMMntYSu5Vajmt0wubZDRMQJi PnO6wQgyeCb+M0F44sqfzrlFcyiKl2Ra8F1mhyAOlQE6ntcAzG26ZZ0WLQMdBzZX3bs3Bmlj7szOT +uDXFyENI0jKxVeBcdklxracNWlE0Ku0Qb/zBpzo/+w/wxivX4xK5siV7iCLWWxBhPAbaPODOZAuI C6Wka6lg==; From: Richard Sent Date: Thu, 23 May 2024 16:07:04 -0400 Message-ID: MIME-Version: 1.0 X-Authenticated-Id: richard@freakingpenguin.com 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 From: Gabriel Wicki * 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 --- Hi Guix! This is a resubmission of V1 this patch. It looks like the email got messed up a bit and QA missed it. If QA still misses it I might send it out again as a separate issue and link back to here. To be 100% clear, this is Gabriel Wicki's code, not mine! Credit goes to them for the contribution. Some feedback: resize-fs-configuration's device field shouldn't be a hardcoded string. It should be as flexible as the file-system record, i.e. allow for labels and UUIDs. This should be possible using canonicalize-device-name in gnu build file-system. The service should also be extendable. Depending on how parted operates this might not be trivial. I'm not familiar with the tool, but I'd hope it can operate on partition device files just as well as a block device + partition #. doc/guix.texi | 41 +++++++++++++++++++ gnu/services/admin.scm | 89 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 129 insertions(+), 1 deletion(-) base-commit: 3597c736588c45efde3c22d533ea8774c3fdd235 diff --git a/doc/guix.texi b/doc/guix.texi index 8073e3f6d4..29fde6bad5 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -41090,6 +41090,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