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

Commit d3f769f

Browse files
authored
Merge pull request #238 from silkeh/add-btrfs-support
Add support for non-ext filesystems
2 parents ab76231 + 74dd463 commit d3f769f

File tree

14 files changed

+135
-10
lines changed

14 files changed

+135
-10
lines changed

src/lib/blkid_stub.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ static CbmBlkidOps default_blkid_ops = {
3737
.probe_set_superblocks_flags = blkid_probe_set_superblocks_flags,
3838
.probe_enable_partitions = blkid_probe_enable_partitions,
3939
.probe_set_partitions_flags = blkid_probe_set_partitions_flags,
40+
.probe_get_wholedisk_devno = blkid_probe_get_wholedisk_devno,
4041
.probe_lookup_value = blkid_probe_lookup_value,
4142
.do_safeprobe = blkid_do_safeprobe,
4243
.free_probe = blkid_free_probe,
@@ -54,6 +55,7 @@ static CbmBlkidOps default_blkid_ops = {
5455

5556
/* Misc */
5657
.devno_to_wholedisk = cbm_blkid_devno_to_wholedisk_wrapped,
58+
.devno_to_devname = blkid_devno_to_devname,
5759
};
5860

5961
/**
@@ -79,6 +81,7 @@ void cbm_blkid_set_vtable(CbmBlkidOps *ops)
7981
assert(blkid_ops->probe_set_superblocks_flags != NULL);
8082
assert(blkid_ops->probe_enable_partitions != NULL);
8183
assert(blkid_ops->probe_set_partitions_flags != NULL);
84+
assert(blkid_ops->probe_get_wholedisk_devno != NULL);
8285
assert(blkid_ops->probe_lookup_value != NULL);
8386
assert(blkid_ops->do_safeprobe != NULL);
8487
assert(blkid_ops->free_probe != NULL);
@@ -96,6 +99,7 @@ void cbm_blkid_set_vtable(CbmBlkidOps *ops)
9699

97100
/* misc */
98101
assert(blkid_ops->devno_to_wholedisk != NULL);
102+
assert(blkid_ops->devno_to_devname != NULL);
99103
}
100104

101105
/**
@@ -141,6 +145,11 @@ void cbm_blkid_free_probe(blkid_probe pr)
141145
blkid_ops->free_probe(pr);
142146
}
143147

148+
dev_t cbm_probe_get_wholedisk_devno(blkid_probe pr)
149+
{
150+
return blkid_ops->probe_get_wholedisk_devno(pr);
151+
}
152+
144153
/**
145154
* Partition functions
146155
*/
@@ -193,6 +202,11 @@ int cbm_blkid_devno_to_wholedisk(dev_t dev, char *diskname, size_t len, dev_t *d
193202
return blkid_ops->devno_to_wholedisk(dev, diskname, len, diskdevno);
194203
}
195204

205+
char *cbm_blkid_devno_to_devname(dev_t dev)
206+
{
207+
return blkid_ops->devno_to_devname(dev);
208+
}
209+
196210
/*
197211
* Editor modelines - https://www.wireshark.org/tools/modelines.html
198212
*

src/lib/blkid_stub.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ typedef struct CbmBlkidOps {
2828
int (*probe_lookup_value)(blkid_probe pr, const char *name, const char **data, size_t *len);
2929
int (*do_safeprobe)(blkid_probe pr);
3030
void (*free_probe)(blkid_probe pr);
31+
dev_t (*probe_get_wholedisk_devno)(blkid_probe pr);
3132

3233
/* Partition functions */
3334
blkid_partlist (*probe_get_partitions)(blkid_probe pr);
@@ -42,6 +43,7 @@ typedef struct CbmBlkidOps {
4243

4344
/* Misc functions */
4445
int (*devno_to_wholedisk)(dev_t dev, char *diskname, size_t len, dev_t *diskdevno);
46+
char *(*devno_to_devname)(dev_t dev);
4547
} CbmBlkidOps;
4648

4749
/**
@@ -109,6 +111,7 @@ int cbm_blkid_probe_set_partitions_flags(blkid_probe pr, int flags);
109111
int cbm_blkid_do_safeprobe(blkid_probe pr);
110112
int cbm_blkid_probe_lookup_value(blkid_probe pr, const char *name, const char **data, size_t *len);
111113
void cbm_blkid_free_probe(blkid_probe pr);
114+
dev_t cbm_probe_get_wholedisk_devno(blkid_probe pr);
112115

113116
/**
114117
* Partition related wrappers
@@ -129,6 +132,7 @@ const char *cbm_blkid_parttable_get_type(blkid_parttable tab);
129132
* Misc related wrappers
130133
*/
131134
int cbm_blkid_devno_to_wholedisk(dev_t dev, char *diskname, size_t len, dev_t *diskdevno);
135+
char *cbm_blkid_devno_to_devname(dev_t dev);
132136

133137
/*
134138
* Editor modelines - https://www.wireshark.org/tools/modelines.html

src/lib/files.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,22 @@ char *get_boot_device()
155155
static bool get_parent_disk_devno(char *path, dev_t *diskdevno)
156156
{
157157
struct stat st = { 0 };
158-
dev_t ret;
158+
autofree(char) *dev_path = NULL;
159159

160160
if (stat(path, &st) != 0) {
161161
return false;
162162
}
163163

164-
if (cbm_blkid_devno_to_wholedisk(st.st_dev, NULL, 0, &ret) < 0) {
164+
dev_path = cbm_system_get_device_for_mountpoint(path);
165+
blkid_probe pr = cbm_blkid_new_probe_from_filename(dev_path);
166+
if (cbm_blkid_do_safeprobe(pr) != 0) {
165167
LOG_ERROR("Invalid block device: %s", path);
168+
cbm_blkid_free_probe(pr);
166169
return false;
167170
}
168-
*diskdevno = ret;
171+
172+
*diskdevno = cbm_probe_get_wholedisk_devno(pr);
173+
cbm_blkid_free_probe(pr);
169174
return true;
170175
}
171176

@@ -199,13 +204,12 @@ char *get_parent_disk(char *path)
199204
{
200205
dev_t devt;
201206
autofree(char) *node = NULL;
202-
const char *devfs = cbm_system_get_devfs_path();
203207

204208
if (!get_parent_disk_devno(path, &devt)) {
205209
return NULL;
206210
}
207211

208-
node = string_printf("%s/block/%u:%u", devfs, major(devt), minor(devt));
212+
node = cbm_blkid_devno_to_devname(devt);
209213
return realpath(node, NULL);
210214
}
211215

@@ -518,6 +522,27 @@ char *cbm_get_mountpoint_for_device(const char *device)
518522
return NULL;
519523
}
520524

525+
char *cbm_get_device_for_mountpoint(const char *mount)
526+
{
527+
autofree(char) *dev_path = NULL;
528+
autofree(FILE_MNT) *tab = NULL;
529+
struct mntent *mnt = NULL;
530+
531+
tab = setmntent("/proc/self/mounts", "r");
532+
if (tab == NULL) {
533+
return NULL;
534+
}
535+
536+
while ((mnt = getmntent(tab)) != NULL) {
537+
if (strcmp(mnt->mnt_dir, mount) == 0) {
538+
dev_path = strdup(mnt->mnt_fsname);
539+
break;
540+
}
541+
}
542+
543+
return realpath(dev_path, NULL);
544+
}
545+
521546
bool cbm_system_has_uefi()
522547
{
523548
autofree(char) *p = NULL;

src/lib/files.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ bool cbm_is_mounted(const char *path);
142142
*/
143143
char *cbm_get_mountpoint_for_device(const char *device);
144144

145+
/**
146+
* Determine the device for the given mountpoint
147+
*
148+
* @param mount Mount point to get the device for
149+
* @return Device path for the mount point, or NULL if none was found.
150+
*/
151+
char *cbm_get_device_for_mountpoint(const char *mount);
152+
145153
/**
146154
* Determine whether the system is booted using UEFI
147155
*/

src/lib/probe.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,17 +203,17 @@ CbmDeviceProbe *cbm_probe_path(const char *path)
203203
LOG_ERROR("Path does not exist: %s", path);
204204
return NULL;
205205
}
206-
probe.dev = st.st_dev;
207206

208-
devnode = cbm_system_devnode_to_devpath(probe.dev);
207+
devnode = cbm_system_get_device_for_mountpoint(path);
209208
if (!devnode) {
209+
LOG_ERROR("No device for path: %s", path);
210210
DECLARE_OOM();
211211
return NULL;
212212
}
213213

214214
blk_probe = cbm_blkid_new_probe_from_filename(devnode);
215215
if (!blk_probe) {
216-
fprintf(stderr, "Unable to probe %u:%u", major(st.st_dev), minor(st.st_dev));
216+
fprintf(stderr, "Unable to probe device %s", devnode);
217217
return NULL;
218218
}
219219

src/lib/probe.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ 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-
dev_t dev; /**< The device itself */
3029
bool gpt; /**<Whether this device belongs to a GPT disk */
3130
} CbmDeviceProbe;
3231

src/lib/system_stub.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ static CbmSystemOps default_system_ops = {
5555
.system = system,
5656
.is_mounted = cbm_is_mounted,
5757
.get_mountpoint_for_device = cbm_get_mountpoint_for_device,
58+
.get_device_for_mountpoint = cbm_get_device_for_mountpoint,
5859
.devnode_to_devpath = cbm_devnode_to_devpath,
5960
.get_sysfs_path = cbm_get_sysfs_path,
6061
.get_devfs_path = cbm_get_devfs_path,
@@ -114,6 +115,11 @@ char *cbm_system_get_mountpoint_for_device(const char *device)
114115
return system_ops->get_mountpoint_for_device(device);
115116
}
116117

118+
char *cbm_system_get_device_for_mountpoint(const char *device)
119+
{
120+
return system_ops->get_device_for_mountpoint(device);
121+
}
122+
117123
char *cbm_system_devnode_to_devpath(dev_t d)
118124
{
119125
return system_ops->devnode_to_devpath(d);

src/lib/system_stub.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ typedef struct CbmSystemOps {
3030
/* wrap cbm lib functions */
3131
bool (*is_mounted)(const char *target);
3232
char *(*get_mountpoint_for_device)(const char *device);
33+
char *(*get_device_for_mountpoint)(const char *mount);
3334

3435
/* exec family */
3536
int (*system)(const char *command);
@@ -70,6 +71,11 @@ bool cbm_system_is_mounted(const char *target);
7071
*/
7172
char *cbm_system_get_mountpoint_for_device(const char *device);
7273

74+
/**
75+
* Get the device path for a given mountpoint
76+
*/
77+
char *cbm_system_get_device_for_mountpoint(const char *device);
78+
7379
/**
7480
* Wrap the umount syscall
7581
*/

tests/blkid-harness.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#pragma once
1313

1414
#include "blkid_stub.h"
15+
#include <sys/sysmacros.h>
1516

1617
const char *DEFAULT_UUID = "Test-UUID";
1718
const char *DEFAULT_PART_UUID = "Test-PartUUID";
@@ -48,6 +49,12 @@ static inline int test_blkid_probe_set_partitions_flags(__cbm_unused__ blkid_pro
4849
return 0;
4950
}
5051

52+
static inline dev_t test_blkid_probe_get_wholedisk_devno(__cbm_unused__ blkid_probe pr)
53+
{
54+
/* Prevent legacy testing */
55+
return makedev(0, 0);
56+
}
57+
5158
static inline int test_blkid_do_safeprobe(__cbm_unused__ blkid_probe pr)
5259
{
5360
return 0;
@@ -118,6 +125,12 @@ static inline int test_blkid_devno_to_wholedisk(__cbm_unused__ dev_t dev,
118125
return -1;
119126
}
120127

128+
static inline char *test_blkid_devno_to_devname(dev_t dev)
129+
{
130+
return string_printf("%s/dev/block/%u:%u",
131+
TOP_BUILD_DIR "/tests/update_playground", major(dev), minor(dev));
132+
}
133+
121134
static inline blkid_parttable test_blkid_partlist_get_table(__cbm_unused__ blkid_partlist ls)
122135
{
123136
/* Return a "valid" partition table */
@@ -140,6 +153,7 @@ CbmBlkidOps BlkidTestOps = {
140153
.probe_set_superblocks_flags = test_blkid_probe_set_superblocks_flags,
141154
.probe_enable_partitions = test_blkid_probe_enable_partitions,
142155
.probe_set_partitions_flags = test_blkid_probe_set_partitions_flags,
156+
.probe_get_wholedisk_devno = test_blkid_probe_get_wholedisk_devno,
143157
.probe_lookup_value = test_blkid_probe_lookup_value,
144158
.do_safeprobe = test_blkid_do_safeprobe,
145159
.free_probe = test_blkid_free_probe,
@@ -157,6 +171,7 @@ CbmBlkidOps BlkidTestOps = {
157171

158172
/* Misc */
159173
.devno_to_wholedisk = test_blkid_devno_to_wholedisk,
174+
.devno_to_devname = test_blkid_devno_to_devname,
160175
};
161176

162177
/*

tests/check-legacy.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ static inline int legacy_devno_to_wholedisk(__cbm_unused__ dev_t dev, __cbm_unus
4343
return 0;
4444
}
4545

46+
/**
47+
* Coerce legacy lookup
48+
*/
49+
static inline dev_t legacy_probe_get_wholedisk_devno(__cbm_unused__ blkid_probe pr)
50+
{
51+
return makedev(8, 8);
52+
}
53+
4654
/**
4755
* Forces detection of GPT legacy boot partition
4856
*/
@@ -290,6 +298,7 @@ int main(void)
290298
/* override test ops for legacy testing */
291299
CbmBlkidOps blkid_ops = BlkidTestOps;
292300
blkid_ops.devno_to_wholedisk = legacy_devno_to_wholedisk;
301+
blkid_ops.probe_get_wholedisk_devno = legacy_probe_get_wholedisk_devno;
293302
blkid_ops.partition_get_flags = legacy_partition_get_flags;
294303
blkid_ops.partition_get_uuid = legacy_partition_get_uuid;
295304

0 commit comments

Comments
 (0)