-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpartfs.c
576 lines (482 loc) · 14.7 KB
/
partfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
* fuse part(ition)fs
*
* partfs allows one to access partitions within a device or file.
* the main purpose of partfs is to allow the creation of disk
* images without superuser privileges. this can be useful for the
* enabling automatic partition discovery for containers or for
* building disk images for embedded software.
*
* the mounted directory presents the partitions as files, allowing
* mkfs.* to be used to create file systems on the partitions
*
* to create a disk image with a single bootable
* partition with an ext4 file system:
*
* $ dd if=/dev/zero of=disk.image bs=1M count=4
* 4+0 records in
* 4+0 records out
* 4194304 bytes (4.2 MB, 4.0 MiB) copied, 0.00470867 s, 891 MB/s
* $ parted --script disk.image \
* mktable msdos mkpart primary 2048s 100% set 1 boot on
* $ mkdir mntdir
* $ partfs -o dev=disk.image mntdir
* $ mkfs.ext4 mntdir/p1
* mke2fs 1.42.13 (17-May-2015)
* Creating filesystem with 3072 1k blocks and 768 inodes
*
* Allocating group tables: done
* Writing inode tables: done
* Creating journal (1024 blocks): done
* Writing superblocks and filesystem accounting information: done
*
* $ fusermount -u mntdir
*
* to verify that the partition and file
* system were successfully created:
*
* $ sudo kpartx -a -v disk.image
* add map loop0p1 (253:0): 0 6144 linear 7:0 2048
* $ sudo fsck.ext4 /dev/mapper/loop0p1
* e2fsck 1.42.13 (17-May-2015)
* /dev/mapper/loop0p1: clean, 11/768 files, 1150/3072 blocks
* $ sudo kpartx -d disk.image
* loop deleted : /dev/loop0
*/
#define FUSE_USE_VERSION 26
#include <fuse/fuse.h>
#include <libfdisk/libfdisk.h>
/*
* the standard version of this function in libfdisk returns
* the size in sectors. this returns the size in bytes.
*/
static off_t __fdisk_partition_get_size(
struct fdisk_context * const ctx,
struct fdisk_partition * const pa)
{
return fdisk_partition_has_size(pa) ?
(fdisk_partition_get_size(pa) * fdisk_get_sector_size(ctx)) : 0;
}
#include <unistd.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/param.h>
/*
* file representations of partitions are named as "pX"
* where X is the integer id of the partition (generally 1-4)
*/
#define PARTFS_NAME_PREFIX "p"
/*
* options retrieved from the command line
*/
struct partfs_options
{
/* the device file name as supplied on the command line */
const char * device;
/* whether or not help should be displayed */
int help;
};
/*
* data structure associated with the mounted "device"
*/
struct partfs_device
{
/* absolute path to the device file */
const char * name;
/* partition context associated with the device file */
struct fdisk_context * ctx;
/* stat information about the device file */
struct stat st;
};
/*
* data structure associated with each open of a partition
*/
struct partfs_file
{
/* file descriptor for device file */
int desc;
/*
* starting offset and size of the partition (in bytes)
* within the device file. these are initialized in open call
* and should not be modified after.
*/
off_t start, size;
};
/* supported command line options */
static const struct fuse_opt partfs_optspec[] =
{
/* dev is the name/path of the device file */
{ "dev=%s", offsetof(struct partfs_options, device), 1 },
/* display help */
{ "--help", offsetof(struct partfs_options, help), 1 },
{ "-h", offsetof(struct partfs_options, help), 1 },
FUSE_OPT_END,
};
/*
* extract the partition number from the path name of a partition
*
* returns -1 on error
*/
static ssize_t __partfs_parse_path(const char * const path)
{
size_t n;
int r, c;
r = sscanf(path, "/" PARTFS_NAME_PREFIX "%zu%n", &n, &c);
return (r == 1 && c == (int)strlen(path)) ? ((ssize_t)n - 1) : -1;
}
/*
* populate a stat buffer
*
* ownership and time stamps are populated from the template
*/
static void __partfs_stat(struct stat * const st,
const mode_t mode,
const nlink_t nlink,
const off_t size,
const struct stat * const tmpl)
{
memset(st, 0, sizeof(*st));
st->st_mode = mode;
st->st_nlink = nlink;
st->st_uid = tmpl->st_uid;
st->st_gid = tmpl->st_gid;
st->st_size = size;
st->st_atime = tmpl->st_atime;
st->st_mtime = tmpl->st_mtime;
st->st_ctime = tmpl->st_ctime;
}
/*
* initial open of the device file and parsing of the partitions
*
* pdev must point to an existing structure. existing members will
* be overwritten without being evaluated. device is the name
* of the device file to be opened
*/
static int partfs_open_device(struct partfs_device * const pdev,
const char * const device)
{
int err;
pdev->name = NULL;
pdev->ctx = NULL;
/*
* need the absolute path since fuse may not stay
* in the same directory in which it was started
*/
pdev->name = realpath(device, NULL);
err = pdev->name ? 0 : -errno;
if (!err) {
err = stat(pdev->name, &pdev->st) ? -errno : 0;
if (!err) {
pdev->ctx = fdisk_new_context();
err = pdev->ctx ? 0 : -ENOMEM;
if (!err) {
/* parse the partition table */
err = fdisk_assign_device(pdev->ctx, pdev->name, 1);
if (err) {
fdisk_unref_context(pdev->ctx);
pdev->ctx = NULL;
}
}
}
if (err) {
free((void *)pdev->name);
pdev->name = NULL;
}
}
return err;
}
/*
* called just before the main fuse loop starts
*
* private_data is the partfs_device
*/
static void * partfs_init(struct fuse_conn_info * const conn)
{
return fuse_get_context()->private_data;
}
/* called just before fuse exits */
static void partfs_destroy(void * const priv)
{
struct partfs_device * const pdev = priv;
fdisk_deassign_device(pdev->ctx, 0);
fdisk_unref_context(pdev->ctx);
free((void *)pdev->name);
}
/*
* get attributes--stat(2), essentially--for a file or directory
*/
static int partfs_getattr(const char * const path, struct stat * const st)
{
struct partfs_device * const pdev = fuse_get_context()->private_data;
int ret;
if (strcmp(path, "/") == 0) {
__partfs_stat(st, S_IFDIR | 0755, 2, 0, &pdev->st);
ret = 0;
} else {
ssize_t n;
ret = -ENOENT;
/*
* extract the partition number from the path name
* and gather statistics for it
*/
n = __partfs_parse_path(path);
if (n >= 0) {
struct fdisk_partition * pa;
pa = NULL;
fdisk_get_partition(pdev->ctx, n, &pa);
__partfs_stat(st, pdev->st.st_mode, 1,
__fdisk_partition_get_size(pdev->ctx, pa),
&pdev->st);
fdisk_unref_partition(pa);
ret = 0;
}
}
return ret;
}
/*
* list directory contents
*/
static int partfs_readdir(const char * const path,
void * const buf,
fuse_fill_dir_t fill,
off_t offs,
struct fuse_file_info * const fi)
{
struct partfs_device * const pdev = fuse_get_context()->private_data;
int ret;
ret = -ENOENT;
if (strcmp(path, "/") == 0) {
struct fdisk_table * tb;
struct fdisk_iter * it;
struct fdisk_partition * pa;
struct stat st;
/*
* use ownership and time stamps from the device file
* for the top level directory
*/
__partfs_stat(&st, S_IFDIR | 0755, 2, 0, &pdev->st);
fill(buf, ".", &st, 0);
/* fuse will fill in information for the parent directoy */
fill(buf, "..", NULL, 0);
/* load the entire partition table */
tb = NULL;
fdisk_get_partitions(pdev->ctx, &tb);
/* return directory entries and information for each partition */
it = fdisk_new_iter(FDISK_ITER_FORWARD);
while (fdisk_table_next_partition(tb, it, &pa) == 0) {
char num[16];
snprintf(num, sizeof(num),
PARTFS_NAME_PREFIX "%zu",
fdisk_partition_get_partno(pa) + 1);
__partfs_stat(&st, pdev->st.st_mode, 1,
__fdisk_partition_get_size(pdev->ctx, pa),
&pdev->st);
fill(buf, num, &st, 0);
}
fdisk_free_iter(it);
fdisk_unref_table(tb);
ret = 0;
}
return ret;
}
/*
* open a file (partition) within the device
*/
static int partfs_open(const char * const path,
struct fuse_file_info * const fi)
{
struct partfs_device * const pdev = fuse_get_context()->private_data;
struct partfs_file * const pfi = malloc(sizeof(*pfi));
int err;
err = -ENOMEM;
if (pfi) {
/* open the existing disk/device file */
pfi->desc = open(pdev->name, fi->flags);
if (pfi->desc < 0) {
err = -errno;
} else {
const ssize_t n = __partfs_parse_path(path);
struct fdisk_partition * pa;
pa = NULL;
fdisk_get_partition(pdev->ctx, n, &pa);
/*
* get/save the starting offset
* and size of the partition
*/
pfi->start = fdisk_get_sector_size(pdev->ctx) *
fdisk_partition_get_start(pa);
pfi->size = __fdisk_partition_get_size(pdev->ctx, pa);
fdisk_unref_partition(pa);
/* save the file structure */
fi->fh = (uintptr_t)pfi;
err = 0;
}
}
return err;
}
/*
* this function is not actually part of the fuse api but
* has an signature similar to what it would probably look
* like if it was.
*
* path is the path name to the file
* off is the offset to which to seek within the open
* file/partition; whence is assumed to be SEEK_SET
* fi is the file information associated with the open() call
*
* returns 0 on success or a negative errno on failure
*/
static int partfs_lseek(const char * const path,
const off_t off,
struct fuse_file_info * const fi)
{
struct partfs_file * const pfi = (void *)fi->fh;
int ret;
ret = -EINVAL;
if (off >= 0 && off <= pfi->size) {
/*
* off refers to an offset within a partition; however,
* the file system actually opens the device file itself
* underneath the covers, so the lseek actually seeks to
* the appropriate location within the device file
*/
ret = (lseek(pfi->desc, pfi->start + off, SEEK_SET) >= 0) ? 0 : -errno;
}
return ret;
}
/*
* read data from a partition
*/
static int partfs_read(const char * const path,
char * const buf, size_t len,
off_t off,
struct fuse_file_info * const fi)
{
struct partfs_file * const pfi = (void *)fi->fh;
int ret;
ret = partfs_lseek(path, off, fi);
if (ret == 0) {
ret = read(pfi->desc, buf, MIN(pfi->size - off, len));
if (ret < 0) {
ret = -errno;
}
}
return ret;
}
/*
* write data to a partition
*/
static int partfs_write(const char * const path,
const char * const buf, const size_t len,
const off_t off,
struct fuse_file_info * const fi)
{
struct partfs_file * const pfi = (void *)fi->fh;
int ret;
ret = partfs_lseek(path, off, fi);
if (off >= 0) {
if (ret != 0) {
/*
* if offset was positive, but the lseek failed, then
* the offset is beyond the end of the partition
*/
ret = -EFBIG;
} else {
ret = write(pfi->desc, buf, MIN(pfi->size - off, len));
if (ret < 0) {
ret = -errno;
}
}
}
return ret;
}
/*
* release is called when a partition is closed
*/
static int partfs_release(const char * const path,
struct fuse_file_info * const fi)
{
struct partfs_file * const pfi = (void *)fi->fh;
const int desc = pfi->desc;
free(pfi);
/*
* fuse ignores this error, but return it anyway
*/
return close(desc) ? -errno : 0;
}
/*
* truncate doesn't actually change the size of the partition, but
* implementing it allows using the > operator on the command line
* to cat data into it.
*
* function succeeds if size is less than existing size (without
* doing anything) and fails if size is larger.
*/
static int partfs_truncate(const char * const path, const off_t off)
{
struct partfs_device * const pdev = fuse_get_context()->private_data;
const ssize_t n = __partfs_parse_path(path);
int ret;
ret = -ENOENT;
if (n >= 0) {
struct fdisk_partition * pa;
pa = NULL;
fdisk_get_partition(pdev->ctx, n, &pa);
ret = -EFBIG;
if (off <= __fdisk_partition_get_size(pdev->ctx, pa)) {
ret = 0;
}
}
return ret;
}
/* supported operations for the part(ition)fs */
static const struct fuse_operations partfs_ops =
{
.getattr = partfs_getattr,
.readdir = partfs_readdir,
.open = partfs_open,
.read = partfs_read,
.write = partfs_write,
.release = partfs_release,
.truncate = partfs_truncate,
.init = partfs_init,
.destroy = partfs_destroy,
};
int main(const int argc, char * argv[])
{
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
struct partfs_options opts;
int err;
opts.device = NULL;
opts.help = 0;
err = fuse_opt_parse(&args, &opts, partfs_optspec, NULL);
if (!err) {
struct partfs_device pdev;
if (opts.device) {
err = partfs_open_device(&pdev, opts.device);
if (err) {
fprintf(stderr,
"%s: unable to read partitions\n",
opts.device);
}
} else {
opts.help = 1;
}
if (!err) {
if (opts.help) {
fuse_opt_add_arg(&args, "--help");
}
err = fuse_main(args.argc, args.argv, &partfs_ops, &pdev);
if (opts.help) {
fprintf(stderr, "\n");
fprintf(stderr, "File system-specific options:\n");
fprintf(stderr, "\n");
fprintf(stderr, " -o dev=FILE\n");
}
}
}
return err;
}