mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-01-11 23:43:18 +00:00
Use diskpart to rescan the drives
This commit is contained in:
parent
dcebbc0044
commit
8b0ce36955
@ -1,31 +1,32 @@
|
|||||||
|
os = require('os')
|
||||||
fs = require('fs')
|
fs = require('fs')
|
||||||
|
childProcess = require('child_process')
|
||||||
eventStream = require('event-stream')
|
eventStream = require('event-stream')
|
||||||
progressStream = require('progress-stream')
|
progressStream = require('progress-stream')
|
||||||
|
|
||||||
blockAligner = (blockSize) ->
|
IS_WINDOWS = os.platform() is 'win32'
|
||||||
return eventStream.through (chunk) ->
|
|
||||||
size = chunk.length % blockSize
|
|
||||||
|
|
||||||
if size isnt 0
|
exports.rescanDrives = (callback) ->
|
||||||
newChunk = new Buffer(chunk.length + (blockSize - size))
|
return callback() if not IS_WINDOWS
|
||||||
chunk.copy(newChunk)
|
diskpartRescanScriptPath = path.join(__dirname, 'scripts', 'diskpart_rescan')
|
||||||
chunk = newChunk
|
childProcess.exec "diskpart /s #{diskpartRescanScriptPath}", {}, (error) ->
|
||||||
|
console.log("DISKPART RESULT: #{arguments}")
|
||||||
@emit('data', chunk)
|
return callback(error)
|
||||||
|
|
||||||
exports.writeImage = (devicePath, imagePath, options = {}, callback = _.noop) ->
|
exports.writeImage = (devicePath, imagePath, options = {}, callback = _.noop) ->
|
||||||
|
|
||||||
if not fs.existsSync(imagePath)
|
if not fs.existsSync(imagePath)
|
||||||
return callback(new Error("Invalid OS image: #{imagePath}"))
|
return callback(new Error("Invalid OS image: #{imagePath}"))
|
||||||
|
|
||||||
if not fs.existsSync(devicePath)
|
if not IS_WINDOWS and not fs.existsSync(devicePath)
|
||||||
return callback(new Error("Invalid device: #{devicePath}"))
|
return callback(new Error("Invalid device: #{devicePath}"))
|
||||||
|
|
||||||
imageFile = fs.createReadStream(imagePath)
|
imageFile = fs.createReadStream(imagePath)
|
||||||
|
|
||||||
deviceFile = fs.createWriteStream devicePath,
|
deviceFile = fs.createWriteStream devicePath,
|
||||||
|
|
||||||
# Required by Windows to work correctly
|
# Required by Windows to work correctly
|
||||||
flags: 'r+'
|
flags: 'rs+'
|
||||||
|
|
||||||
imageFileSize = fs.statSync(imagePath).size
|
imageFileSize = fs.statSync(imagePath).size
|
||||||
|
|
||||||
@ -36,8 +37,13 @@ exports.writeImage = (devicePath, imagePath, options = {}, callback = _.noop) ->
|
|||||||
if options.progress
|
if options.progress
|
||||||
progress.on('progress', options.onProgress)
|
progress.on('progress', options.onProgress)
|
||||||
|
|
||||||
|
async.waterfall([
|
||||||
|
|
||||||
|
(callback) ->
|
||||||
|
exports.rescanDrives(callback)
|
||||||
|
|
||||||
|
(callback) ->
|
||||||
imageFile
|
imageFile
|
||||||
.pipe(blockAligner(512))
|
|
||||||
.pipe(progress)
|
.pipe(progress)
|
||||||
.pipe(deviceFile)
|
.pipe(deviceFile)
|
||||||
|
|
||||||
@ -49,3 +55,10 @@ exports.writeImage = (devicePath, imagePath, options = {}, callback = _.noop) ->
|
|||||||
return callback(error)
|
return callback(error)
|
||||||
|
|
||||||
.on('close', callback)
|
.on('close', callback)
|
||||||
|
|
||||||
|
(callback) ->
|
||||||
|
exports.rescanDrives(callback)
|
||||||
|
|
||||||
|
], callback)
|
||||||
|
|
||||||
|
|
||||||
|
1
lib/drive/scripts/diskpart_rescan
Normal file
1
lib/drive/scripts/diskpart_rescan
Normal file
@ -0,0 +1 @@
|
|||||||
|
rescan
|
Loading…
Reference in New Issue
Block a user