From patchwork Fri Mar 15 19:14:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Schantl X-Patchwork-Id: 7629 Return-Path: Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) client-signature ECDSA (secp384r1)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by web04.haj.ipfire.org (Postfix) with ESMTPS id 4TxDTh0mNpz3wnw for ; Fri, 15 Mar 2024 19:15:04 +0000 (UTC) Received: from mail02.haj.ipfire.org (mail02.haj.ipfire.org [172.28.1.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) client-signature ECDSA (secp384r1)) (Client CN "mail02.haj.ipfire.org", Issuer "R3" (verified OK)) by mail01.ipfire.org (Postfix) with ESMTPS id 4TxDTd2Kt9zFKx; Fri, 15 Mar 2024 19:15:01 +0000 (UTC) Received: from mail02.haj.ipfire.org (localhost [127.0.0.1]) by mail02.haj.ipfire.org (Postfix) with ESMTP id 4TxDTd1q0Hz32q4; Fri, 15 Mar 2024 19:15:01 +0000 (UTC) Received: from mail01.ipfire.org (mail01.haj.ipfire.org [172.28.1.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) client-signature ECDSA (secp384r1)) (Client CN "mail01.haj.ipfire.org", Issuer "R3" (verified OK)) by mail02.haj.ipfire.org (Postfix) with ESMTPS id 4TxDTW3JV9z32nQ for ; Fri, 15 Mar 2024 19:14:55 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by mail01.ipfire.org (Postfix) with ESMTPSA id 4TxDTW0wzhznf; Fri, 15 Mar 2024 19:14:55 +0000 (UTC) DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003ed25519; t=1710530095; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=D2bxLu+BHHDNoy7QDFq9QFz4KHU2k0Smc9TDg6W0vPY=; b=z3+g4KYnb81DFoJd7Z/eVt89GsRuKf+5FqGMIDx7wspi5ye3Rygv0BI2eXi3xjuo6iddQA 7cg1/D/s4U/8IaCw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ipfire.org; s=202003rsa; t=1710530095; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=D2bxLu+BHHDNoy7QDFq9QFz4KHU2k0Smc9TDg6W0vPY=; b=aC3u7zAwlPgdfX+S/DCbPwwkVMkiU96VHLdo7lU3sxaxaH+pvTKR39zzBvo/bHdI+3Fs0o Qt9h3+X1nKOS1dCF+HxM/x7bhBjIIeI1J1tZ6eiYwioLF7yPV3gsOveSlntKWa2PjRZelC dRZ/gp8Slq+McagEbJK99qLQG8cEjpDj4eCfiYMaWvXzKIM0auqEZeRPLWRmb/448Dc3QF Wj9D/2n3MjlQaDHbpcrWW2TIESGlTZ9bhHCOliRQlULolngpEYulo24w8tDfoA8J8eXKE4 /Jy5SP+v3dxWZcDvM7xN9I56vZh3/XX2EnwtqJgM0X9NPsorjk/dNxzjaFOzGA== From: Stefan Schantl To: development@lists.ipfire.org Subject: [PATCH 05/17] installer: Add code to create a BTRFS subvolume layout. Date: Fri, 15 Mar 2024 20:14:30 +0100 Message-Id: <20240315191442.3951-6-stefan.schantl@ipfire.org> In-Reply-To: <20240315191442.3951-1-stefan.schantl@ipfire.org> References: <20240315191442.3951-1-stefan.schantl@ipfire.org> MIME-Version: 1.0 Message-ID-Hash: Y6OZZCTQ2OUJNE7SQVAAM7M6H27FQL4Y X-Message-ID-Hash: Y6OZZCTQ2OUJNE7SQVAAM7M6H27FQL4Y X-MailFrom: stefan.schantl@ipfire.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.8 Precedence: list List-Id: IPFire development talk Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Signed-off-by: Stefan Schantl --- src/installer/hw.c | 70 ++++++++++++++++++++++++++++++++++++++++++++-- src/installer/hw.h | 5 ++++ 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/src/installer/hw.c b/src/installer/hw.c index 81be87471..420feaca7 100644 --- a/src/installer/hw.c +++ b/src/installer/hw.c @@ -44,6 +44,20 @@ #include "hw.h" +// Array which contains the subvolumes which will be created when installing +// IPFire on a BTRFS. +const char* btrfs_subvolumes[7][2] = { + {"@root" ,"/"}, + {"@snapshots", "/.snapshots"}, + {"@home", "/home"}, + {"@cache", "/var/cache"}, + {"@lib", "/var/lib"}, + {"@logs", "/var/log"}, + {"@mails", "/var/mail"} +}; + +#define LEN(arr) ((int) (sizeof (arr) / sizeof (arr)[0])) + static int system_chroot(const char* output, const char* path, const char* cmd) { char chroot_cmd[STRING_SIZE]; @@ -805,6 +819,7 @@ int hw_create_partitions(struct hw_destination* dest, const char* output) { static int hw_format_filesystem(const char* path, int fs, const char* output) { char cmd[STRING_SIZE] = "\0"; + int r; // Swap if (fs == HW_FS_SWAP) { @@ -824,7 +839,9 @@ static int hw_format_filesystem(const char* path, int fs, const char* output) { // BTRFS } else if (fs == HW_FS_BTRFS) { - snprintf(cmd, sizeof(cmd), "/usr/bin/mkfs.btrfs -L rootfs -f %s", path); + r = hw_create_btrfs_layout(path, output); + + return r; // FAT32 } else if (fs == HW_FS_FAT32) { @@ -833,7 +850,7 @@ static int hw_format_filesystem(const char* path, int fs, const char* output) { assert(*cmd); - int r = mysystem(output, cmd); + r = mysystem(output, cmd); return r; } @@ -870,6 +887,43 @@ int hw_create_filesystems(struct hw_destination* dest, const char* output) { return 0; } +int hw_create_btrfs_layout(const char* path, const char* output) { + char cmd[STRING_SIZE]; + char subvolume[STRING_SIZE]; + + // Create the main BTRFS. + snprintf(cmd, sizeof(cmd), "/usr/bin/mkfs.btrfs -L IPFire -f %s", path); + int r = mysystem(output, cmd); + + if (r) + return r; + + // We need to mount the FS in order to create any subvolumes. + r = hw_mount(path, DESTINATION_MOUNT_PATH, "btrfs", 0); + + if (r) + return r; + + // Loop through the array of subvolumes to create. + for ( int i = 0; i < LEN(btrfs_subvolumes); i++ ) { + snprintf(subvolume, sizeof(subvolume), "%s", btrfs_subvolumes[i][0]); + + // Call function to create the subvolume + r = hw_create_btrfs_subvolume(output, subvolume); + + if (r) + return r; + } + + // Umount the main BTRFS after subvolume creation. + r = hw_umount(path, 0); + + if (r) + return r; + + return 0; +} + int hw_mount_filesystems(struct hw_destination* dest, const char* prefix) { char target[STRING_SIZE]; @@ -1219,3 +1273,15 @@ int hw_restore_backup(const char* output, const char* backup_path, const char* d return 0; } + +int hw_create_btrfs_subvolume(const char* output, const char* subvolume) { + char command [STRING_SIZE]; + snprintf(command, sizeof(command), "/usr/bin/btrfs subvolume create %s/%s", DESTINATION_MOUNT_PATH, subvolume); + + int r = mysystem(output, command); + + if (r) + return -1; + + return 0; +} diff --git a/src/installer/hw.h b/src/installer/hw.h index e5ee65a6d..2de73a3be 100644 --- a/src/installer/hw.h +++ b/src/installer/hw.h @@ -104,6 +104,8 @@ struct hw_destination { unsigned long long size_root; }; +extern const char* btrfs_subvolumes[][2]; + struct hw* hw_init(); void hw_free(struct hw* hw); @@ -143,4 +145,7 @@ int hw_start_networking(const char* output); void hw_sync(); +int hw_create_btrfs_layout(const char* output, const char* subvolume); +int hw_create_btrfs_subvolume(const char* output, const char* subvolume); + #endif /* HEADER_HW_H */