|
|
|
"use strict";
|
|
|
|
|
|
|
|
const Promise = require("bluebird");
|
|
|
|
const matchValue = require("match-value");
|
|
|
|
const execBinary = require("../exec-binary");
|
|
|
|
// const parseIECBytes = require("../parse-bytes-iec");
|
|
|
|
const { B } = require("../unit-bytes-iec");
|
|
|
|
const createJSONParser = require("../text-parser-json");
|
|
|
|
|
|
|
|
function parseBoolean(value) {
|
|
|
|
if (typeof value === "boolean") {
|
|
|
|
// Newer versions of `lsblk` correctly use boolean values
|
|
|
|
return value;
|
|
|
|
} else {
|
|
|
|
return matchValue(value, {
|
|
|
|
0: false,
|
|
|
|
1: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapType(value) {
|
|
|
|
return matchValue(value, {
|
|
|
|
part: "partition",
|
|
|
|
disk: "disk",
|
|
|
|
loop: "loopDevice",
|
|
|
|
rom: "disk",
|
|
|
|
lvm: "partition"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapSubType(value) {
|
|
|
|
return matchValue(value, {
|
|
|
|
part: null,
|
|
|
|
disk: null,
|
|
|
|
loop: null,
|
|
|
|
rom: "readOnlyMedia",
|
|
|
|
lvm: "lvm"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapDeviceList(devices) {
|
|
|
|
return devices.map((device) => {
|
|
|
|
return {
|
|
|
|
name: device.name,
|
|
|
|
path: device.path,
|
|
|
|
type: mapType(device.type),
|
|
|
|
subType: mapSubType(device.type),
|
|
|
|
mountpoint: device.mountpoint,
|
|
|
|
deviceNumber: device["maj:min"],
|
|
|
|
removable: parseBoolean(device.rm),
|
|
|
|
readOnly: parseBoolean(device.ro),
|
|
|
|
size: B(device.size),
|
|
|
|
children: (device.children != null) ? mapDeviceList(device.children) : []
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = function lsblk() {
|
|
|
|
return Promise.try(() => {
|
|
|
|
return execBinary("lsblk")
|
|
|
|
.withFlags({ json: true, "output-all": true, bytes: true })
|
|
|
|
.requireOnStdout(createJSONParser())
|
|
|
|
.execute();
|
|
|
|
}).then((output) => {
|
|
|
|
return mapDeviceList(output.result.blockdevices);
|
|
|
|
});
|
|
|
|
};
|