Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

etcher-image-write

resin-io-modules1.9kApache-2.09.1.6

The cross-platform way to stream an OS image to a device

etcher, image, os, flash, burn, write

readme

etcher-image-write

The cross-platform way to stream an OS image to a device

npm version dependencies Build Status Build status

Installation

Install etcher-image-write by running:

$ npm install --save etcher-image-write

Documentation

imageWrite.write(drive, image, options) ⇒ EventEmitter

NOTICE: You might need to run this function as sudo/administrator to avoid permission issues.

We recommend passing file descriptors opened with rs+ flags.

The returned EventEmitter instance emits the following events:

  • progress: A progress event that passes a state object of the form:
{
  type: 'write' // possible values: 'write', 'check'.
  percentage: 9.05,
  transferred: 949624,
  length: 10485760,
  remaining: 9536136,
  eta: 10,
  runtime: 0,
  delta: 295396,
  speed: 949624
}
  • error: An error event.
  • done: An event emitted with a boolean success value.

Enabling the check option is useful to ensure the image was successfully written to the device. This is checked by calculating and comparing checksums from both the original image and the data written to the device.

The transform option is used to handle cases like decompression of an image on the fly. The stream is piped through this transform stream after the progress stream and before any writing and alignment.

This allows the progress to be accurately displayed even when the client doesn't know the final uncompressed size.

For example, to handle writing a compressed file, you pass the compressed stream to .write(), pass the compressed stream size, and a transform stream to decompress the file.

Kind: static method of imageWrite
Summary: Write a readable stream to a device
Returns: EventEmitter - emitter
Access: public

Param Type Default Description
drive Object | drive
drive.device String | drive device
drive.size Number | drive size
drive.fd Number | drive file descriptor
image Object | image
image.stream ReadStream | image readable stream
image.size Number | image stream size
options Object | options
[options.transform] TransformStream | transform stream
[options.check] Boolean false enable write check
[options.bmap] String | bmap file contents
[options.bytesToZeroOutFromTheBeginning] Number | bytes to zero out from the beginning (bmap only)

Example

var emitter = imageWrite.write({
  fd: fs.openSync('/dev/rdisk2', 'rs+'), // '\\\\.\\PHYSICALDRIVE1' in Windows, for example.
  device: '/dev/rdisk2',
  size: 2014314496
}, {
  stream: fs.createReadStream('my/image'),
  size: fs.statSync('my/image').size
}, {
  check: true
});

emitter.on('progress', (state) => {
  console.log(state);
});

emitter.on('error', (error) => {
  console.error(error);
});

emitter.on('done', (results) => {
  console.log('Success!');
});

Errors

The errors we emit can be identified by their code and type properties.

Consult this file for a list of defined errors.

Support

If you're having any problem, please raise an issue on GitHub and the Resin.io team will be happy to help.

Tests

To run the full test suite (Node & Electron), you'll need to have Electron installed either globally or locally in the repository (npm install [--global] electron).

Run the test suite by doing:

$ npm test

To run only the Node or Electron tests, respectively:

$ npm run test-node # OR
$ npm run test-electron

Contribute

Before submitting a PR, please make sure that you include tests, and that jshint runs without any warning:

$ npm run lint

License

etcher-image-write is free software, and may be redistributed under the terms specified in the license.

changelog

Change Log

All notable changes to this project will be documented in this file. This project adheres to Semantic Versioning.

v9.1.6 - 2020-03-03

Misc

  • Update dependences

v9.1.5 - 2019-05-29

Misc

  • update dependencies for Node 12 compilation

v9.1.4 - 2019-01-10

Misc

  • Remove versionist
  • Update drivelist to ^6.4.4

v9.1.3 - 2017-05-09

Changed

  • Get rid of the diskpart optional Windows module

v9.1.2 - 2017-04-21

Changed

  • Improve diskpart clean error message
  • Throw EUNPLUGGED on EBUSY on macOS

v9.1.1 - 2017-04-14

Changed

  • Throw EUNPLUGGED if an SD Card gets unplugged half-way through from an internal reader.

v9.1.0 - 2017-04-14

Changed

  • Throw EUNPLUGGED if drive gets unplugged half-way through.

v9.0.3 - 2017-04-13

Changed

  • Support Node.js v4.

v9.0.2 - 2017-04-11

Changed

  • Properly throw the validation error object.

v9.0.1 - 2017-03-06

Misc

  • package: Add Versionist & npm version hooks (#90)
  • editorconfig: Disable trimming whitespace in Markdown (#89)
  • Update dependencies (#88)
  • Fix ENOSPC / EINVAL caused by chunk padding (#84)
  • package: Update dependencies (#82)
  • Update ImageStreamPosition unit tests (#81)

9.0.0 - 2017-01-05

Changed

  • Fix non-working CLI example.
  • Retry read operations up to 10 times on EIO.
  • Catch unhandled error events from the first stream of utils.safePipe().
  • Don't close drive file descriptor after validation.

8.1.5 - 2017-01-04

Changed

  • Retry write operations up to ten times when EIO is encountered.

8.1.4 - 2016-11-03

Changed

  • Align last block to 512K, and to 1M if the block exceeds 512K.

8.1.3 - 2016-09-23

Changed

  • Fix sporadic EPERM write errors on Windows.
  • Don't consider buffer alignment bytes during the validation phase.

8.1.2 - 2016-09-21

Changed

  • Correctly slice drive stream when performing validation even if the image size refers to the compressed size.

8.1.1 - 2016-09-21 (YANKED)

8.1.0 - 2016-09-07

Added

  • Add bytesToZeroOutFromTheBeginning option for bmap writes.

8.0.0 - 2016-09-06

Removed

  • Remove passedValidation property from results and throw an EVALIDATION error instead.

7.0.1 - 2016-08-24

Changed

  • Don't throw ENOSPC if the drive capacity is equal to the image size.

7.0.0 - 2016-08-23

Changed

  • Take a file descriptor of the drive instead of a path.

6.1.1 - 2016-08-21

Changed

  • Fix certain bmap ranges not being written properly.

6.1.0 - 2016-08-17

Added

  • Add bmap support.

6.0.1 - 2016-07-28

Changed

  • Make sure the device file descriptor is closed after finishing validation.

6.0.0 - 2016-07-21

Added

  • Require drive size to be passed as an option.

Changed

  • Emit an ENOSPC if the drive has no remaining space.

5.0.3 - 2016-07-17

Changed

  • Retry diskpart clean up to five times if it fails.

5.0.2 - 2016-06-27

Changed

  • Fix regressing Windows EPERM issue.

5.0.1 - 2016-06-12

Changed

  • Fix Windows EPERM issue on certain drives.

5.0.0 - 2016-05-17

Added

  • Extend writing results to include the source CRC32 checksum.

4.0.2 - 2016-04-28

Changed

  • Upgrade bluebird to v3.3.5 to avoid incompatibilities in certain environments.

4.0.1 - 2016-04-27

Changed

  • Align unaligned images to prevent EINVAL.

4.0.0 - 2016-04-26

Added

  • Add a size required option.
  • Add a transform option to pass a custom stream transform.

Changed

  • Remove the need for a custom .length property on streams.
  • Heavy refactoring of the module.

Removed

  • Remove Resin.io mentions in error messages.

3.0.4 - 2016-04-20

Changed

  • Set a type property on thrown errors to identify their origin.

3.0.3 - 2016-04-15

Changed

  • Fix stream.push() after EOF with unaligned images.

3.0.2 - 2016-04-04

Changed

  • Fix race condition that caused some drives to be mounted before the check process in OS X.

3.0.1 - 2016-03-29

Changed

  • Make sure a 100% state event is emitted from the check.

3.0.0 - 2016-03-29

Added

  • Add easy to use example script.
  • Report checking progress.

Changed

  • Integrate checking to the .write() function.

Removed

  • Remove .check().

2.1.0 - 2016-02-16

Added

  • Implement .check() to ensure an image was written correctly.

2.0.7 - 2015-12-04

Changed

  • Omit tests from NPM package.

2.0.6 - 2015-11-24

  • Upgrade tmp to v0.0.28.

2.0.5 - 2015-10-20

Changed

  • Improve generic EINVAL error message.

2.0.4 - 2015-10-05

Changed

  • Drastically increase speed on OS X by using raw devices.

2.0.3 - 2015-09-11

Changed

  • Set chunk size to 1M to improve writing time.

2.0.2 - 2015-09-10

Added

  • Chunk stream in 1024 bytes blocks.

2.0.1 - 2015-09-07

Changed

  • Upgrade resin-settings-client to v3.0.0.

2.0.0 - 2015-08-24

Changed

  • Throw an error if no stream size information.
  • Fix test suite Windows issues on NodeJS v0.10.