diskLayout() for linux rewritten - better detection

This commit is contained in:
Sebastian Hildebrandt
2019-03-01 15:03:58 +01:00
parent c1d35b550e
commit 5302d24069
6 changed files with 137 additions and 85 deletions
+83 -37
View File
@@ -601,6 +601,48 @@ exports.disksIO = disksIO;
function diskLayout(callback) {
function getVendorFromModel(model) {
const diskManufacturers = [
{ pattern: '^WESTERN.+', manufacturer: 'Western Digital' },
{ pattern: '^WDC.+', manufacturer: 'Western Digital' },
{ pattern: 'WD.+', manufacturer: 'Western Digital' },
{ pattern: '^TOSHIBA.+', manufacturer: 'Toshiba' },
{ pattern: '^HITACHI.+', manufacturer: 'Hitachi' },
{ pattern: '^IC.+', manufacturer: 'Hitachi' },
{ pattern: '^HTS.+', manufacturer: 'Hitachi' },
{ pattern: '^SANDISK.+', manufacturer: 'SanDisk' },
{ pattern: '^KINGSTON.+', manufacturer: 'Kingston Technonogy' },
{ pattern: '^SONY.+', manufacturer: 'Sony' },
{ pattern: '^TRANSCEND.+', manufacturer: 'Transcend' },
{ pattern: 'SAMSUNG.+', manufacturer: 'Samsung' },
{ pattern: '^ST(?!I\\ ).+', manufacturer: 'Seagate' },
{ pattern: '^STI\\ .+', manufacturer: 'SimpleTech' },
{ pattern: '^D...-.+', manufacturer: 'IBM' },
{ pattern: '^IBM.+', manufacturer: 'IBM' },
{ pattern: '^FUJITSU.+', manufacturer: 'Fujitsu' },
{ pattern: '^MP.+', manufacturer: 'Fujitsu' },
{ pattern: '^MK.+', manufacturer: 'Toshiba' },
{ pattern: '^MAXTOR.+', manufacturer: 'Maxtor' },
{ pattern: '^Pioneer.+', manufacturer: 'Pioneer' },
{ pattern: '^PHILIPS.+', manufacturer: 'Philips' },
{ pattern: '^QUANTUM.+', manufacturer: 'Quantum Technology' },
{ pattern: 'FIREBALL.+', manufacturer: 'Quantum Technology' },
{ pattern: '^VBOX.+', manufacturer: 'VirtualBox' },
{ pattern: 'CORSAIR.+', manufacturer: 'Corsair Components' },
{ pattern: 'CRUCIAL.+', manufacturer: 'Crucial' },
{ pattern: 'ECM.+', manufacturer: 'ECM' },
{ pattern: 'INTEL.+', manufacturer: 'INTEL' },
];
let result = '';
model = model.toUpperCase();
diskManufacturers.forEach((manufacturer) => {
const re = RegExp(manufacturer.pattern);
if (re.test(model)) { result = manufacturer.manufacturer }
})
return result
}
return new Promise((resolve) => {
process.nextTick(() => {
@@ -608,46 +650,50 @@ function diskLayout(callback) {
let cmd = '';
if (_linux) {
exec('export LC_ALL=C; lshw -class disk; unset LC_ALL', function (error, stdout) {
exec('export LC_ALL=C; lsblk -ablJO; unset LC_ALL', function (error, stdout) {
if (!error) {
let devices = stdout.toString().split('*-');
devices.shift();
devices.forEach(function (device) {
let lines = device.split('\n');
let mediumType = '';
const BSDName = util.getValue(lines, 'logical name', ':', true).trim();
const logical = util.getValue(lines, 'logical name', ':', true).trim().replace(/\/dev\//g, '');
try {
mediumType = execSync('cat /sys/block/' + logical + '/queue/rotational').toString().split('\n')[0];
} catch (e) {
util.noop();
try {
const out = stdout.toString().trim();
const outJSON = JSON.parse(out);
if (outJSON && outJSON.hasOwnProperty('blockdevices')) {
let devices = outJSON.blockdevices.filter(item => { return item.group === 'disk' && item.size > 0 && item.model !== null });
devices.forEach((device) => {
let mediumType = '';
const BSDName = '/dev/' + device.name;
const logical = device.name;
try {
mediumType = execSync('cat /sys/block/' + logical + '/queue/rotational').toString().split('\n')[0];
} catch (e) {
util.noop();
}
result.push({
type: (mediumType === '0' ? 'SSD' : (mediumType === '1' ? 'HD' : (device.model && device.model.indexOf('SSD') > -1 ? 'SSD' : 'HD'))),
name: device.model || '',
vendor: getVendorFromModel(device.model) || (device.vendor ? device.vendor.trim() : ''),
size: device.size || 0,
bytesPerSector: -1,
totalCylinders: -1,
totalHeads: -1,
totalSectors: -1,
totalTracks: -1,
tracksPerCylinder: -1,
sectorsPerTrack: -1,
firmwareRevision: device.rev ? device.rev.trim() : '',
serialNum: device.serial ? device.serial.trim() : '',
interfaceType: device.tran ? device.tran.trim() : '',
smartStatus: 'unknown',
BSDName: BSDName
});
cmd = cmd + 'printf "\n' + BSDName + '|"; smartctl -H ' + BSDName + ' | grep overall;';
})
}
const sizeString = util.getValue(lines, 'size', ':', true).trim();
if (sizeString && lines.length > 0 && lines[0].trim() === 'disk') {
const sizeValue = sizeString.match(/\(([^)]+)\)/)[1];
result.push({
type: (mediumType === '0' ? 'SSD' : (mediumType === '1' ? 'HD' : (device.indexOf('SSD') > -1 ? 'SSD' : 'HD'))), // to be tested ... /sys/block/sda/queue/rotational
name: util.getValue(lines, 'product:', ':', true),
vendor: util.getValue(lines, 'vendor:', ':', true),
size: parseInt(sizeValue, 10) * 1000 * 1000 * 1000 * (sizeValue.indexOf('T') >= 0 ? 1000 : 1),
bytesPerSector: -1,
totalCylinders: -1,
totalHeads: -1,
totalSectors: -1,
totalTracks: -1,
tracksPerCylinder: -1,
sectorsPerTrack: -1,
firmwareRevision: util.getValue(lines, 'version:', ':', true).trim(),
serialNum: util.getValue(lines, 'serial:', ':', true).trim(),
interfaceType: '',
smartStatus: 'unknown',
BSDName: BSDName
});
cmd = cmd + 'printf "\n' + BSDName + '|"; smartctl -H ' + BSDName + ' | grep overall;';
}
});
} catch (e) {
util.noop();
}
}
// lshw sometimes returns empty results, try lsblk
// check S.M.A.R.T. status
if (cmd) {
cmd = cmd + 'printf "\n"';
exec(cmd, function (error, stdout) {