Name: node-lkl
Owner: Resin.io
Description: NodeJS native bindings to the Linux Kernel Library project
Created: 2015-11-19 03:56:59.0
Updated: 2017-12-05 07:42:09.0
Pushed: 2017-11-16 19:03:24.0
Size: 10752
Language: C
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
node-lkl uses the Linux Kernel Library project to provide access to filesystem drivers from NodeJS in a cross-platform way.
Some things you can do with this module:
fs
module (except that only async methods work).Check the How it works section to learn how it works.
Warning: The API exposed by this library is still forming and can change at any time!
node-lkl
includes a stripped down copy of the linux source code so
there's no need to have it available on your system.
Simply compile and install node-lkl
using npm
:
m install lkl
Here's an example of using node-lkl
to read a file inside a disk image of
a ext4 partition and pipe it to process.stdout
:
t Promise = require('bluebird');
t filedisk = Promise.promisifyAll(require('file-disk'), { multiArgs: true });
t lkl = Promise.promisifyAll(require('lkl'))
fs = Promise.promisifyAll(lkl.fs);
startKernelSync(10 * 1024 * 1024);
ise.using(filedisk.openFile('ext4', 'r'), function(fd) {
const disk = new filedisk.FileDisk(fd, true, false);
return Promise.using(lkl.utils.attachDisk(disk), function(diskId) {
const options = { filesystem: 'ext4', readOnly: true};
return Promise.using(lkl.utils.mountPartition(diskId, options), function(mountpoint) {
const file = lkl.fs.createReadStream(mountpoint + '/some_file');
file.pipe(process.stdout);
return new Promise((resolve, reject) => {
file.on("close", resolve).on("error", reject);
});
});
});
n(function() {
lkl.haltKernelSync();
t Promise = require('bluebird');
t aws = require('aws-sdk');
t filedisk = Promise.promisifyAll(require('file-disk'));
t lkl = Promise.promisifyAll(require('lkl'));
startKernelSync(10 * 1024 * 1024);
t s3 = new aws.S3('access_key', 'secret_key');
t disk = new filedisk.S3Disk(s3, 'some-bucket', 'some-object-key');
ise.using(lkl.utils.attachDisk(disk), function(diskId) {
const options = { partition: 1, filesystem: 'vfat' };
return Promise.using(lkl.utils.mountPartition(diskId, options), function(mountpoint) {
doSomething(mountpoint);
});
n(function() {
return disk.getStreamAsync();
n(function(stream) {
const out = fs.createWriteStream('some-file')
stream.pipe(out)
return new Promise(function(resolve, reject) {
out.on('close', resolve);
out.on('error', reject);
});
n(function() {
lkl.haltKernelSync();
More examples in tests.
node-lkl is based on the LKL (Linux Kernel Library) project. node-lkl is focused on the filesystem capabilities of lkl although future work could allow usage of the kernel networking stack as well. To understand how a userspace process can “mount” an image without actually mounting it, it helps to think about the interface between an actual kernel and a block device.
-------------+
application |
-----+-------+
| syscall interface
-----V---------+
kernel |
------------+ |
fs driver | |
------+-----+ |
| |
------V------+ |
block driver | |
------+------+ |
------|--------+
| block request interface
------V-----+
hard drive |
------------+
Everything inside the box named kernel
doesn't actually need the hardware
to do its job. When an application instructs the kernel to read a file, the
filesystem driver issues some read requests to the block driver and the block
driver issues those to the hard drive. All of the high level filesystem
semantics like files, directories, permissions, etc. are “compiled” down to raw
reads and writes at specific offsets.
This means that as long as we can provide a block request interface to the kernel, and find a way to run the kernel in userspace, we can use all of its functionality. This is how it looks like with node-lkl:
--------------------------------------------------------+
nodejs application |
|
-----------------------------------------------------+ |
V8 +-----------+ | |
| request() |-+ | |
fs.read(); lkl.fs.read(); +------^----+ | | |
---|---------------|-----------------------|------|--+ |
| | | | |
| virtual | syscall interface | | |
| +--------V---------+ | async |
| | liblkl.so | | | |
| | +-------------+ | | | |
| | | fs driver | | async | |
| | +-------+-----+ | | | |
| | | | | | |
| | +-------V------+ | | | |
| | | block driver | | | | |
| | +-------+------+ | | | |
| +---------|--------+ | | |
| virtual block | request interface | | |
| +-------V------+ | | |
| | V8 adapter |---------------+ | |
| +--------------+ | |
| | |
|--------------------------------------------- + |
| |
---|----------------------------------------------------+
| syscall interface
---V------------+
kernel |
-------------+ |
fs driver | |
-------+-----+ |
| |
-------V------+ |
block driver | |
-------+------+ |
-------|--------+
| block request interface
+------V-----+
| hard drive |
+------------+
If you're having any problems, please raise an issue on GitHub.
node-lkl is free software, and may be redistributed under the terms specified in the license.