Skip to content
This repository was archived by the owner on Aug 7, 2025. It is now read-only.

Commit b8ab190

Browse files
committed
Add support for Btrfs subvolumes
1 parent 9b57ae6 commit b8ab190

File tree

7 files changed

+32
-0
lines changed

7 files changed

+32
-0
lines changed

meson.build

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ ccompiler = meson.get_compiler('c')
4444
dep_blkid = dependency('blkid')
4545
dep_check = dependency('check', version: '>= 0.9')
4646

47+
# other deps
48+
dep_btrfs = ccompiler.find_library('btrfsutil')
49+
if not ccompiler.has_header('btrfsutil.h')
50+
error('Cannot find btrfsutil.h. Is btrfs-progs-dev(el) installed?')
51+
endif
52+
4753
# Grab necessary paths
4854
path_prefix = get_option('prefix')
4955
path_bindir = join_paths(path_prefix, get_option('bindir'))

src/bootloaders/grub2.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ bool grub2_write_kernel(const Grub2Config *config, const Kernel *kernel)
241241
"rd.luks.uuid=%s ",
242242
config->root_dev->luks_uuid);
243243
}
244+
if (config->root_dev->btrfs_sub) {
245+
cbm_writer_append_printf(config->writer,
246+
"rootflags=subvol=%s ",
247+
config->root_dev->btrfs_sub);
248+
}
244249

245250
/* Finish it off with the command line options */
246251
cbm_writer_append_printf(config->writer, "%s\"\n", kernel->meta.cmdline);

src/bootloaders/syslinux-common.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ bool syslinux_common_set_default_kernel(const BootManager *manager, const Kernel
189189
if (root_dev->luks_uuid) {
190190
cbm_writer_append_printf(writer, "rd.luks.uuid=%s ", root_dev->luks_uuid);
191191
}
192+
/* Add Btrfs information if relevant */
193+
if (root_dev->btrfs_sub) {
194+
cbm_writer_append_printf(writer, "rootflags=subvol=%s ", root_dev->btrfs_sub);
195+
}
192196

193197
/* Write out the cmdline */
194198
cbm_writer_append_printf(writer, "%s\n", k->meta.cmdline);

src/bootloaders/systemd-class.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@ bool sd_class_install_kernel(const BootManager *manager, const Kernel *kernel)
284284
if (root_dev->luks_uuid) {
285285
cbm_writer_append_printf(writer, "rd.luks.uuid=%s ", root_dev->luks_uuid);
286286
}
287+
/* Add Btrfs information if relevant */
288+
if (root_dev->btrfs_sub) {
289+
cbm_writer_append_printf(writer, "rootflags=subvol=%s ", root_dev->btrfs_sub);
290+
}
287291

288292
/* Finish it off with the command line options */
289293
cbm_writer_append_printf(writer, "%s\n", kernel->meta.cmdline);

src/lib/probe.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <sys/stat.h>
2222
#include <sys/sysmacros.h>
2323
#include <unistd.h>
24+
#include <btrfsutil.h>
2425

2526
#include "blkid_stub.h"
2627
#include "files.h"
@@ -263,6 +264,16 @@ CbmDeviceProbe *cbm_probe_path(const char *path)
263264
LOG_ERROR("Unable to find UUID for %s: %s", devnode, strerror(errno));
264265
}
265266

267+
/* Check if its a Btrfs device */
268+
if (btrfs_util_is_subvolume(path) == BTRFS_UTIL_OK) {
269+
LOG_DEBUG("Root device is a Btrfs subvolume");
270+
enum btrfs_util_error err = btrfs_util_subvolume_path(path, 0, &probe.btrfs_sub);
271+
if (err != BTRFS_UTIL_OK) {
272+
LOG_ERROR("Failed to get subvolume of Btrfs filesystem %s: %s",
273+
path, btrfs_util_strerror(err));
274+
}
275+
}
276+
266277
/* Check if its a software raid device */
267278
basenom = basename(devnode);
268279
if (strncmp(basenom, "md", 2) == 0) {

src/lib/probe.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ typedef struct CbmDeviceProbe {
2626
char *uuid; /**< UUID for all partition types */
2727
char *part_uuid; /**< PartUUID for GPT partitions */
2828
char *luks_uuid; /**< Parent LUKS UUID for the partition */
29+
char *btrfs_sub; /**< Btrfs subvolume of the rootfs */
2930
dev_t dev; /**< The device itself */
3031
bool gpt; /**<Whether this device belongs to a GPT disk */
3132
} CbmDeviceProbe;

src/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ libcbm_includes = [
6868
libcbm_dependencies = [
6969
link_libnica,
7070
dep_blkid,
71+
dep_btrfs,
7172
]
7273

7374
# Special constraints for efi functionality

0 commit comments

Comments
 (0)