Skip to content
This repository was archived by the owner on Jan 29, 2026. It is now read-only.

Commit e5fb3fb

Browse files
committed
Guess the filesystem when none is given.
Change-Type: patch
1 parent e1f6412 commit e5fb3fb

3 files changed

Lines changed: 54 additions & 21 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ Example
3636
Here's an example of using `node-lkl` to read a file inside a disk image of
3737
a ext4 partition and pipe it to `process.stdout`:
3838

39+
If you can omit the `filesystem` options parameter, all supported filesystems
40+
will be tried.
41+
3942
``` javascript
4043
const Promise = require('bluebird');
4144
const filedisk = Promise.promisifyAll(require('file-disk'), { multiArgs: true });

lib/lkl.js

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ const bindings = require('bindings')('bindings');
44
const binding = require('./binding');
55

66
const queue = require('./queue');
7+
const constants = require('./constants');
78

89
const lkl = exports;
910

1011
lkl.fs = require('./fs');
1112
lkl.disk = require('./disk');
1213

14+
const SUPPORTED_FILESYSTEMS = [ // TODO: get this list from lkl
15+
'ext4', 'ext3', 'ext2', 'btrfs', 'xfs', 'vfat'
16+
];
17+
1318
lkl.startKernelSync = function(memory) {
1419
bindings.startKernel(memory);
1520
binding.clock_settime(null, function(){});
@@ -39,6 +44,35 @@ function mount(diskId, opts, callback) {
3944
);
4045
}
4146

47+
function mountGuessFS(disk, opts, callback, fsIndex) {
48+
fsIndex = (fsIndex === undefined) ? 0 : fsIndex;
49+
opts.filesystem = SUPPORTED_FILESYSTEMS[fsIndex];
50+
mount(disk, opts, function(err, mountpoint) {
51+
if (err) {
52+
if (
53+
(err.errno === constants.EINVAL) &&
54+
(fsIndex < SUPPORTED_FILESYSTEMS.length - 1)
55+
) {
56+
return mountGuessFS(disk, opts, callback, fsIndex + 1)
57+
}
58+
return callback(err);
59+
}
60+
return callback(null, mountpoint);
61+
})
62+
};
63+
64+
lkl.mount = function(disk, opts, callback) {
65+
if ((callback === undefined) && (typeof opts === 'function')) {
66+
callback = opts;
67+
opts = {};
68+
}
69+
if (SUPPORTED_FILESYSTEMS.indexOf(opts.filesystem) !== -1) {
70+
queue.addOperation(mount, [disk, opts, callback]);
71+
} else {
72+
queue.addOperation(mountGuessFS, [disk, opts, callback]);
73+
}
74+
}
75+
4276
function umount(mountpoint, callback) {
4377
const info = mounts[mountpoint];
4478
bindings.umount(info.diskId, info.partition, function(err) {
@@ -50,6 +84,10 @@ function umount(mountpoint, callback) {
5084
});
5185
}
5286

87+
lkl.umount = function(mountpoint, callback) {
88+
queue.addOperation(umount, [mountpoint, callback]);
89+
};
90+
5391
lkl.diskAdd = function(disk, callback) {
5492
queue.addOperation(
5593
bindings.disk_add,
@@ -61,12 +99,4 @@ lkl.diskRemove = function(diskId, callback) {
6199
queue.addOperation(bindings.disk_remove, [ diskId, callback ]);
62100
};
63101

64-
lkl.mount = function(diskId, opts, callback) {
65-
queue.addOperation(mount, [diskId, opts, callback]);
66-
};
67-
68-
lkl.umount = function(mountpoint, callback) {
69-
queue.addOperation(umount, [mountpoint, callback]);
70-
};
71-
72102
lkl.utils = require('./utils');

test/index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ describe('node-lkl', function() {
4444
});
4545

4646
it('should mount', function() {
47-
return lkl.mountAsync(this.diskId, { readOnly: true, filesystem: 'ext4'})
48-
.then(lkl.umountAsync);
47+
return lkl.mountAsync(this.diskId, { readOnly: true })
48+
.then(lkl.umountAsync)
4949
});
5050

5151
after(function(){
@@ -80,28 +80,28 @@ describe('node-lkl', function() {
8080
});
8181

8282
it('should mount vfat', function() {
83-
return lkl.mountAsync(this.diskId, {readOnly: true, filesystem: 'vfat', partition: 1})
84-
.then(lkl.umountAsync);
83+
return lkl.mountAsync(this.diskId, {readOnly: true, partition: 1})
84+
.then(lkl.umountAsync)
8585
});
8686

8787
it('should mount ext2', function() {
88-
return lkl.mountAsync(this.diskId, {readOnly: true, filesystem: 'ext2', partition: 2})
89-
.then(lkl.umountAsync);
88+
return lkl.mountAsync(this.diskId, {readOnly: true, partition: 2})
89+
.then(lkl.umountAsync)
9090
});
9191

9292
it('should mount ext4', function() {
93-
return lkl.mountAsync(this.diskId, {readOnly: true, filesystem: 'ext4', partition: 3})
94-
.then(lkl.umountAsync);
93+
return lkl.mountAsync(this.diskId, {readOnly: true, partition: 3})
94+
.then(lkl.umountAsync)
9595
});
9696

9797
it('should mount btrfs', function() {
98-
return lkl.mountAsync(this.diskId, {readOnly: true, filesystem: 'btrfs', partition: 5})
99-
.then(lkl.umountAsync);
98+
return lkl.mountAsync(this.diskId, {readOnly: true, partition: 5})
99+
.then(lkl.umountAsync)
100100
});
101101

102102
it('should mount xfs', function() {
103-
return lkl.mountAsync(this.diskId, {readOnly: true, filesystem: 'xfs', partition: 6})
104-
.then(lkl.umountAsync);
103+
return lkl.mountAsync(this.diskId, {readOnly: true, partition: 6})
104+
.then(lkl.umountAsync)
105105
});
106106

107107
it('should be able to mount 4 partitions', function() {
@@ -161,7 +161,7 @@ describe('node-lkl', function() {
161161
})
162162
.then(function(diskId) {
163163
self.diskId = diskId;
164-
return lkl.mountAsync(diskId, {filesystem: 'ext4'});
164+
return lkl.mountAsync(diskId);
165165
})
166166
.then(function (mountpoint) {
167167
self.mountpoint = mountpoint;

0 commit comments

Comments
 (0)