fix(sdk): Load usbboot adapter on start

Change-type: patch
Changelog-entry: Load usbboot adapter on start on GNU/Linux if running as root.
Signed-off-by: Alexis Svinartchouk <alexis@resin.io>
This commit is contained in:
Alexis Svinartchouk 2018-05-01 19:36:12 +02:00 committed by Lorenzo Alberto Maria Ambrosi
parent fd19af23a6
commit 26779ef1fb
3 changed files with 24 additions and 18 deletions

View File

@ -18,7 +18,6 @@
const settings = require('../models/settings')
const SDK = require('../../../sdk')
const permissions = require('../../../shared/permissions')
const scanner = SDK.createScanner({
blockdevice: {
@ -29,17 +28,4 @@ const scanner = SDK.createScanner({
usbboot: {}
})
// NOTE: Enable USBBoot on Linux if run as root
permissions.isElevated().then((elevated) => {
if (elevated && process.platform === 'linux') {
const UsbbootAdapter = require('../../../sdk/adapters/usbboot')
const adapter = new UsbbootAdapter()
scanner.stop()
scanner.subscribe(adapter)
scanner.start()
}
}).catch((error) => {
console.warn('Could not add usbboot adapter:', error)
})
module.exports = scanner

View File

@ -19,6 +19,8 @@
const _ = require('lodash')
const os = require('os')
const permissions = require('../../shared/permissions')
/**
* @summary The list of loaded adapters
* @type {Object[]}
@ -28,10 +30,8 @@ const ADAPTERS = [
require('./blockdevice')
]
// We don't support usbboot on GNU/Linux yet, given
// that some distributions require root permissions
// to open USB devices.
if (os.platform() !== 'linux') {
// On GNU/Linux, we only support usbboot when running as root.
if ((os.platform() !== 'linux') || permissions.isElevatedUnixSync()) {
ADAPTERS.push(require('./usbboot'))
}

View File

@ -70,6 +70,26 @@ exports.isElevated = () => {
return Bluebird.resolve(process.geteuid() === UNIX_SUPERUSER_USER_ID)
}
/**
* @summary Check if the current process is running with elevated permissions
* @function
* @public
*
* @description
*
* @returns {Boolean}
*
* @example
* permissions.isElevatedUnixSync()
* if (isElevated) {
* console.log('This process has elevated permissions');
* }
* });
*/
exports.isElevatedUnixSync = () => {
return (process.geteuid() === UNIX_SUPERUSER_USER_ID)
}
/**
* @summary Get environment command prefix
* @function