Files
xen-orchestra/packages/vhd-lib/chain.js
T

33 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-02-22 12:29:26 +01:00
'use strict'
2021-12-23 10:31:29 +01:00
const { dirname, relative } = require('path')
2018-05-14 04:48:16 -07:00
2021-12-23 10:31:29 +01:00
const { openVhd } = require('./openVhd')
const { DISK_TYPES } = require('./_constants')
const { Disposable } = require('promise-toolbox')
2018-05-14 04:48:16 -07:00
2021-12-23 10:31:29 +01:00
module.exports = async function chain(parentHandler, parentPath, childHandler, childPath, force = false) {
await Disposable.use(
[openVhd(parentHandler, parentPath), openVhd(childHandler, childPath, { flags: 'r+' })],
async ([parentVhd, childVhd]) => {
await childVhd.readHeaderAndFooter()
const { header, footer } = childVhd
2018-05-14 04:48:16 -07:00
if (footer.diskType !== DISK_TYPES.DIFFERENCING) {
if (!force) {
throw new Error('cannot chain disk of type ' + footer.diskType)
}
footer.diskType = DISK_TYPES.DIFFERENCING
}
await childVhd.readBlockAllocationTable()
2018-05-14 04:48:16 -07:00
const parentName = relative(dirname(childPath), parentPath)
header.parentUuid = parentVhd.footer.uuid
header.parentUnicodeName = parentName
await childVhd.setUniqueParentLocator(parentName)
await childVhd.writeHeader()
await childVhd.writeFooter()
2018-05-14 04:48:16 -07:00
}
)
2018-05-14 04:48:16 -07:00
}